|
1 | 1 | const msgpack = require('@msgpack/msgpack'); |
2 | | -const _ = require('lodash'); |
3 | 2 | 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'); |
24 | 6 |
|
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; |
35 | 12 | } |
36 | | - } |
37 | 13 |
|
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); |
46 | 18 | } |
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 | + } |
53 | 38 | } |
54 | | - } |
55 | 39 |
|
56 | | - destroy () {}; |
57 | | -} |
| 40 | + destroy() {} |
| 41 | + }; |
| 42 | +}; |
58 | 43 |
|
59 | | -exports = Decoder; |
| 44 | +module.exports = buildDecoder; |
0 commit comments