Skip to content

Commit 7f33ffa

Browse files
author
Georgios Giatsidis
committed
Fix typos, create builder functions, add prettierrc file
1 parent cdf976d commit 7f33ffa

7 files changed

Lines changed: 109 additions & 67 deletions

File tree

‎.prettierrc‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"printWidth": 120,
3+
"useTabs": false,
4+
"tabWidth": 4,
5+
"semi": true,
6+
"singleQuote": true
7+
}

‎index.js‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
exports = {};
1+
const buildEncoder = require('./lib/encoder');
2+
const buildDecoder = require('./lib/decoder');
3+
const PacketType = require('./lib/packetType');
24

35
exports.build = function (options = {}) {
46
return {
57
protocol: 5,
6-
Encoder: new Encoder(options.encoder),
7-
Decoder: new Decoder(options.decoder)
8-
}
9-
}
8+
Encoder: buildEncoder(options.encoder),
9+
Decoder: buildDecoder(options.decoder),
10+
PacketType: PacketType,
11+
};
12+
};

‎lib/decoder.js‎

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,44 @@
11
const msgpack = require('@msgpack/msgpack');
2-
const _ = require('lodash');
32
const Emitter = require('component-emitter');
4-
const PacketType = (exports.PacketType = {
5-
CONNECT: 0,
6-
DISCONNECT: 1,
7-
EVENT: 2,
8-
ACK: 3,
9-
CONNECT_ERROR: 4,
10-
});
11-
12-
class Decoder {
13-
14-
constructor(options) {
15-
this.options = options;
16-
this.emitter = new Emitter();
17-
}
18-
19-
add (obj) {
20-
let decoded = msgpack.decode(obj, this.options);
21-
this.checkPacket(decoded);
22-
this.emitter.emit('decoded', decoded);
23-
}
3+
const _ = require('lodash');
4+
const PacketType = require('./packetType');
5+
const { isDataValid } = require('./helpers');
246

25-
isDataValid(decoded) {
26-
switch (decoded.type) {
27-
case PacketType.CONNECT:
28-
return decoded.data === undefined || _.isObject(decoded.data);
29-
case PacketType.DISCONNECT:
30-
return decoded.data === undefined;
31-
case PacketType.CONNECT_ERROR:
32-
return _.isString(decoded.data) || _.isObject(decoded.data);
33-
default:
34-
return _.isArray(decoded.data);
7+
const buildDecoder = (options = {}) => {
8+
return class Decoder extends Emitter {
9+
constructor() {
10+
super();
11+
this.options = options;
3512
}
36-
}
3713

38-
checkPacket = function (decoded) {
39-
var isTypeValid =
40-
_.isInteger(decoded.type) && decoded.type >= PacketType.CONNECT && decoded.type <= PacketType.CONNECT_ERROR;
41-
if (!isTypeValid) {
42-
throw new Error('invalid packet type');
43-
}
44-
if (!_.isString(decoded.nsp)) {
45-
throw new Error('invalid namespace');
14+
add(obj) {
15+
let decoded = msgpack.decode(obj, this.options);
16+
this.checkPacket(decoded);
17+
this.emit('decoded', decoded);
4618
}
47-
if (!isDataValid(decoded)) {
48-
throw new Error('invalid payload');
49-
}
50-
var isAckValid = decoded.id === undefined || _.isInteger(decoded.id);
51-
if (!isAckValid) {
52-
throw new Error('invalid packet id');
19+
20+
checkPacket(decoded) {
21+
var isTypeValid =
22+
_.isInteger(decoded.type) &&
23+
decoded.type >= PacketType.CONNECT &&
24+
decoded.type <= PacketType.CONNECT_ERROR;
25+
if (!isTypeValid) {
26+
throw new Error('invalid packet type');
27+
}
28+
if (!_.isString(decoded.nsp)) {
29+
throw new Error('invalid namespace');
30+
}
31+
if (!isDataValid(decoded)) {
32+
throw new Error('invalid payload');
33+
}
34+
var isAckValid = decoded.id === undefined || _.isInteger(decoded.id);
35+
if (!isAckValid) {
36+
throw new Error('invalid packet id');
37+
}
5338
}
54-
}
5539

56-
destroy () {};
57-
}
40+
destroy() {}
41+
};
42+
};
5843

59-
exports = Decoder;
44+
module.exports = buildDecoder;

‎lib/encoder.js‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
const msgpack = require('@msgpack/msgpack');
22

3-
class Encoder {
4-
5-
constructor(options = {}) {
6-
this.soptions = options;
7-
}
8-
9-
encode () {
10-
return turn [msgpack.encode(packet, options)];
11-
}
12-
}
13-
14-
exports = Encoder;
3+
const buildEncoder = (options = {}) => {
4+
return class Encoder {
5+
constructor() {
6+
this.options = options;
7+
}
8+
9+
encode(packet) {
10+
return [msgpack.encode(packet), options];
11+
}
12+
};
13+
};
14+
15+
module.exports = buildEncoder;

‎lib/helpers.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const PacketType = require('./packetType');
2+
const _ = require('lodash');
3+
4+
function isDataValid(decoded) {
5+
switch (decoded.type) {
6+
case PacketType.CONNECT:
7+
return decoded.data === undefined || _.isObject(decoded.data);
8+
case PacketType.DISCONNECT:
9+
return decoded.data === undefined;
10+
case PacketType.CONNECT_ERROR:
11+
return _.isString(decoded.data) || _.isObject(decoded.data);
12+
default:
13+
return Array.isArray(decoded.data);
14+
}
15+
}
16+
17+
module.exports = {
18+
isDataValid,
19+
};

‎lib/packetType.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const PacketType = {
2+
CONNECT: 0,
3+
DISCONNECT: 1,
4+
EVENT: 2,
5+
ACK: 3,
6+
CONNECT_ERROR: 4,
7+
};
8+
9+
module.exports = PacketType;

‎package-lock.json‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)