-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate.js
379 lines (340 loc) · 9.36 KB
/
generate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
'use strict';
var protocol = require('./constants');
function getReturnCode(returnType) {
var returnCode = protocol.return_codes[returnType];
if (returnCode !== undefined) {
return returnCode;
} else {
return protocol.return_codes['Rejected: not supported'];
}
}
function streamWriteLength(buffer, length) {
if (length >= 256) {
buffer.writeUInt8(1, 0);
buffer.writeUInt16BE(length, 1);
return 3;
} else {
buffer.writeUInt8(length, 0);
return 1;
}
}
function advertise(opts) {
var gwId = opts.gwId || 0,
duration = opts.duration || 60,
result = Buffer.from([5, protocol.codes.advertise, gwId, 0, 0]);
result.writeUInt16BE(duration, 3);
return result;
}
function searchGW(opts) {
var radius = opts.radius || 0;
return Buffer.from([3, protocol.codes.searchgw, radius]);
}
function gwInfo(opts) {
var gwId = opts.gwId || 0,
gwAdd = opts.gwAdd,
isClient = opts.isClient || false,
length = 3,
pos = 0,
result;
if (isClient) {
length += 1;
if (typeof gwAdd === 'string') {
length += Buffer.byteLength(gwAdd);
} else {
length += gwAdd.length;
}
}
result = Buffer.alloc(length);
pos = streamWriteLength(result, length);
result.writeUInt8(protocol.codes.gwinfo, pos);
pos += 1;
result.writeUInt8(gwId, pos);
pos += 1;
if (isClient) {
if (typeof gwAdd === 'string') {
result.writeUInt8(Buffer.byteLength(gwAdd), pos);
pos += 1;
result.write(gwAdd, pos, 'utf8');
} else {
result.writeUInt8(gwAdd.length, pos);
pos += 1;
gwAdd.copy(result, pos);
}
}
return result;
}
function connect(opts) {
var flags = 0,
duration = opts.duration || 0,
length = 6 + Buffer.byteLength(opts.clientId),
pos = 0,
result = Buffer.alloc(length);
flags |= opts.will ? protocol.WILL_MASK : 0;
flags |= opts.cleanSession ? protocol.CLEAN_MASK : 0;
pos = streamWriteLength(result, length);
result.writeUInt8(protocol.codes.connect, pos);
pos += 1;
result.writeUInt8(flags, pos);
pos += 1;
result.writeUInt8(protocol.ID, pos);
pos += 1;
result.writeUInt16BE(duration, pos);
pos += 2;
result.write(opts.clientId, pos, 'utf8');
return result;
}
function respCode(opts) {
var returnCode = getReturnCode(opts.returnCode);
return Buffer.from([3, protocol.codes[opts.cmd], returnCode]);
}
function request(opts) {
return Buffer.from([2, protocol.codes[opts.cmd]]);
}
function willtopic(opts) {
var length = 2,
pos = 0,
result;
if (opts.willTopic) {
length = 3 + Buffer.byteLength(opts.willTopic);
}
result = Buffer.alloc(length);
pos = streamWriteLength(result, length);
result.writeUInt8(protocol.codes[opts.cmd], pos);
pos += 1;
if (opts.willTopic) {
var flags = 0,
qos = opts.qos || 0,
retain = opts.retain || false;
flags |= (qos << protocol.QOS_SHIFT) & protocol.QOS_MASK;
flags |= retain ? protocol.RETAIN_MASK : 0;
result.writeUInt8(flags, pos);
pos += 1;
result.write(opts.willTopic, pos, 'utf8');
}
return result;
}
function willmsg(opts) {
var willMsg = opts.willMsg || '',
length = 2 + Buffer.byteLength(willMsg),
pos = 0,
result = Buffer.alloc(length);
pos = streamWriteLength(result, length);
result.writeUInt8(protocol.codes[opts.cmd], pos);
pos += 1;
result.write(willMsg, pos, 'utf8');
return result;
}
function register(opts) {
var topicName = opts.topicName || '',
topicId = opts.topicId || 0,
msgId = opts.msgId || 0,
length = 6 + Buffer.byteLength(topicName),
result = Buffer.alloc(length),
pos = 0;
pos = streamWriteLength(result, length);
result.writeUInt8(protocol.codes.register, pos);
pos += 1;
result.writeUInt16BE(topicId, pos);
pos += 2;
result.writeUInt16BE(msgId, pos);
pos += 2;
result.write(topicName, pos, 'utf8');
return result;
}
function regack(opts) {
var topicId = opts.topicId || 0,
msgId = opts.msgId || 0,
retCode = getReturnCode(opts.returnCode),
result = Buffer.from([7, protocol.codes.regack, 0, 0, 0, 0, retCode]);
result.writeUInt16BE(topicId, 2);
result.writeUInt16BE(msgId, 4);
return result;
}
function publish(opts) {
var flags = 0,
topicId = opts.topicId || 0,
msgId = opts.msgId || 0,
topicIdType = 0,
length = 7,
payload = opts.payload || '',
pos = 0,
result;
if (typeof opts.payload === 'string') {
length += Buffer.byteLength(payload);
} else {
length += payload.length;
}
flags |= opts.dup ? protocol.DUP_MASK : 0;
flags |= ((opts.qos || 0) << protocol.QOS_SHIFT) & protocol.QOS_MASK;
flags |= opts.retain ? protocol.RETAIN_MASK : 0;
if (protocol.topicIdCodes[opts.topicIdType] === undefined) {
throw new Error('Invalid topic Id Type');
} else {
topicIdType = opts.topicIdType;
flags |= protocol.topicIdCodes[opts.topicIdType];
}
result = Buffer.alloc(length);
pos = streamWriteLength(result, length);
result.writeUInt8(protocol.codes.publish, pos);
pos += 1;
result.writeUInt8(flags, pos);
pos += 1;
if ((topicIdType === 'short topic') && (typeof topicId === 'string')) {
if (Buffer.byteLength(topicId) !== 2) {
throw new Error('short topic must be exactly 2 bytes long');
}
result.write(topicId, pos, 'utf8');
} else {
result.writeUInt16BE(topicId, pos);
}
pos += 2;
result.writeUInt16BE(msgId, pos);
pos += 2;
if (typeof payload === 'string') {
result.write(payload, pos, 'utf8');
} else {
payload.copy(result, pos);
}
return result;
}
function puback(opts) {
var topicId = opts.topicId || 0,
msgId = opts.msgId || 0,
returnCode = getReturnCode(opts.returnCode),
result = Buffer.from([7, protocol.codes.puback, 0, 0, 0, 0, returnCode]);
result.writeUInt16BE(topicId, 2);
result.writeUInt16BE(msgId, 4);
return result;
}
function pubcomp(opts) {
var msgId = opts.msgId || 0,
result = Buffer.from([4, protocol.codes[opts.cmd], 0, 0]);
result.writeUInt16BE(msgId, 2);
return result;
}
function subscribe(opts) {
var length = 5,
flags = 0,
msgId = opts.msgId || 0,
topicIdCode,
topicId = opts.topicId,
topicName = opts.topicName,
result,
pos = 0;
flags |= opts.dup ? protocol.DUP_MASK : 0;
flags |= ((opts.qos || 0) << protocol.QOS_SHIFT) & protocol.QOS_MASK;
if (topicName) {
if (typeof topicName !== 'string') {
throw new Error('topicName must be of type string');
}
length += Buffer.byteLength(topicName);
} else {
if (protocol.topicIdCodes[opts.topicIdType] === undefined) {
throw new Error('Invalid topic Id Type');
}
flags |= protocol.topicIdCodes[opts.topicIdType];
length += 2;
}
result = Buffer.alloc(length);
pos = streamWriteLength(result, length);
result.writeUInt8(protocol.codes[opts.cmd], pos);
pos += 1;
result.writeUInt8(flags, pos);
pos += 1;
result.writeUInt16BE(msgId, pos);
pos += 2;
if (topicName) {
result.write(topicName, pos, 'utf8');
} else {
if ((typeof topicId === 'string') && (Buffer.byteLength(topicId) !== 2)) {
throw new Error('short topic must be exactly 2 bytes long');
}
result.writeUInt16BE(topicId, pos);
}
return result;
}
function suback(opts) {
var flags = 0,
topicId = opts.topicId || 0,
msgId = opts.msgId || 0,
returnCode = getReturnCode(opts.returnCode),
result;
flags |= ((opts.qos || 0) << protocol.QOS_SHIFT) & protocol.QOS_MASK;
result = Buffer.from([8, protocol.codes.suback, flags, 0, 0, 0, 0, returnCode]);
result.writeUInt16BE(topicId, 3);
result.writeUInt16BE(msgId, 5);
return result;
}
function pingreq(opts) {
var length = 2,
result,
pos = 0;
if (opts.clientId) {
length += Buffer.byteLength(opts.clientId);
}
result = Buffer.alloc(length);
pos = streamWriteLength(result, length);
result.writeUInt8(protocol.codes.pingreq, pos);
pos += 1;
if (opts.clientId) {
result.write(opts.clientId, pos, 'utf8');
}
return result;
}
function disconnect(opts) {
var duration = opts.duration || 0,
result = Buffer.from([4, protocol.codes.disconnect, 0, 0]);
result.writeUInt16BE(duration, 2);
return result;
}
function generate(packet) {
switch (packet.cmd) {
case 'advertise':
return advertise(packet);
case 'searchgw':
return searchGW(packet);
case 'gwinfo':
return gwInfo(packet);
case 'connect':
return connect(packet);
case 'connack':
case 'willtopicresp':
case 'willmsgresp':
return respCode(packet);
case 'willtopicreq':
case 'willmsgreq':
case 'pingresp':
return request(packet);
case 'willtopic':
case 'willtopicupd':
return willtopic(packet);
case 'willmsg':
case 'willmsgupd':
return willmsg(packet);
case 'register':
return register(packet);
case 'regack':
return regack(packet);
case 'publish':
return publish(packet);
case 'puback':
return puback(packet);
case 'pubcomp':
case 'pubrec':
case 'pubrel':
case 'unsuback':
return pubcomp(packet);
case 'unsubscribe':
case 'subscribe':
return subscribe(packet);
case 'suback':
return suback(packet);
case 'pingreq':
return pingreq(packet);
case 'disconnect':
return disconnect(packet);
default:
throw new Error('command not supported');
}
}
module.exports = generate;