-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
469 lines (375 loc) · 8.86 KB
/
index.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
const empty = Buffer.alloc(0)
// a global index for parsing the options and the payload
// we can do this as the parsing is a sync operation
let index
// last five bits are 1
// 31.toString(2) => '111111'
const lowerCodeMask = 31
let nextMsgId = Math.floor(Math.random() * 65535)
const codes = {
GET: 1,
POST: 2,
PUT: 3,
DELETE: 4,
FETCH: 5,
PATCH: 6,
iPATCH: 7,
get: 1,
post: 2,
put: 3,
delete: 4,
fetch: 5,
patch: 6,
ipatch: 7
}
module.exports.generate = function generate (packet, maxLength = 1280) {
let pos = 0
packet = fillGenDefaults(packet)
const options = prepareOptions(packet)
const length = calculateLength(packet, options)
const tokenLength = packet.token.length
let tokenMask
if (tokenLength < 13) {
tokenMask = tokenLength
} else if (tokenLength < 270) {
tokenMask = 13
} else if (tokenLength < 65805) {
tokenMask = 14
}
if (length > maxLength) {
throw new Error(`Max packet size is ${maxLength}: current is ${length}`)
}
const buffer = Buffer.alloc(length)
// first byte
let byte = 0
byte |= 1 << 6 // first two bits are version
byte |= confirmableAckResetMask(packet)
byte |= tokenMask
buffer.writeUInt8(byte, pos++)
// code can be humized or not
if (codes[packet.code]) {
buffer.writeUInt8(codes[packet.code], pos++)
} else {
buffer.writeUInt8(toCode(packet.code), pos++)
}
// two bytes for the message id
buffer.writeUInt16BE(packet.messageId, pos)
pos += 2
if (tokenLength > 268) {
buffer.writeUInt16BE(tokenLength - 269, pos)
pos += 2
} else if (tokenLength > 12) {
buffer.writeUInt8(tokenLength - 13, pos)
pos++
}
// the token might be an empty buffer
packet.token.copy(buffer, pos)
pos += tokenLength
// write the options
for (let i = 0; i < options.length; i++) {
options[i].copy(buffer, pos)
pos += options[i].length
}
if (packet.code !== '0.00' && packet.payload.toString() !== '') {
// payload separator
buffer.writeUInt8(255, pos++)
packet.payload.copy(buffer, pos)
}
return buffer
}
module.exports.parse = function parse (buffer) {
index = 4
parseVersion(buffer)
const result = {
code: parseCode(buffer),
confirmable: parseConfirmable(buffer),
reset: parseReset(buffer),
ack: parseAck(buffer),
messageId: buffer.readUInt16BE(2),
token: parseToken(buffer),
options: null,
payload: null
}
if (result.code !== '0.00') {
result.options = parseOptions(buffer)
result.payload = buffer.slice(index + 1)
} else {
if (buffer.length !== 4) {
throw new Error('Empty messages must be empty')
}
result.options = []
result.payload = empty
}
return result
}
function parseVersion (buffer) {
const version = buffer.readUInt8(0) >> 6
if (version !== 1) {
throw new Error('Unsupported version')
}
return version
}
function parseConfirmable (buffer) {
return (buffer.readUInt8(0) & 48) === 0
}
function parseReset (buffer) {
// 110000 is 48
return (buffer.readUInt8(0) & 48) === 48
}
function parseAck (buffer) {
// 100000 is 32
return (buffer.readUInt8(0) & 48) === 32
}
function parseCode (buffer) {
let byte = buffer.readUInt8(1)
let code = '' + (byte >> 5) + '.'
byte = byte & lowerCodeMask
if (byte < 10) {
code += '0' + byte
} else {
code += byte
}
return code
}
function parseToken (buffer) {
let length = buffer.readUInt8(0) & 15
if (length === 13) {
length = buffer.readUInt8(index) + 13
index += 1
} else if (length === 14) {
length = buffer.readUInt16BE(index) + 269
index += 2
} else if (length === 15) {
throw new Error('Token length not allowed')
}
const result = buffer.slice(index, index + length)
index += length
return result
}
const OPTIONS_BY_NAME = {
'If-Match': 1,
'Uri-Host': 3,
ETag: 4,
'If-None-Match': 5,
Observe: 6,
'Uri-Port': 7,
'Location-Path': 8,
OSCORE: 9,
'Uri-Path': 11,
'Content-Format': 12,
'Max-Age': 14,
'Uri-Query': 15,
'Hop-Limit': 16,
Accept: 17,
'Q-Block1': 19,
'Location-Query': 20,
Block2: 23,
Block1: 27,
Size2: 28,
'Q-Block2': 31,
'Proxy-Uri': 35,
'Proxy-Scheme': 39,
Size1: 60,
'No-Response': 258,
'OCF-Accept-Content-Format-Version': 2049,
'OCF-Content-Format-Version': 2053
}
const OPTIONS_BY_NUMBER = new Array(2054)
for (const key in OPTIONS_BY_NAME) {
const number = OPTIONS_BY_NAME[key]
OPTIONS_BY_NUMBER[number] = key
}
function optionNumberToString (number) {
const string = OPTIONS_BY_NUMBER[number]
if (string) return string
return '' + number
}
function optionStringToNumber (string) {
const number = OPTIONS_BY_NAME[string]
if (number) return number
return parseInt(string)
}
function parseOptions (buffer) {
let number = 0
const options = []
while (index < buffer.length) {
const byte = buffer.readUInt8(index)
if (byte === 255 || index > buffer.length) {
break
}
let delta = byte >> 4
let length = byte & 15
index += 1
if (delta === 13) {
delta = buffer.readUInt8(index) + 13
index += 1
} else if (delta === 14) {
delta = buffer.readUInt16BE(index) + 269
index += 2
} else if (delta === 15) {
throw new Error('Wrong option delta')
}
if (length === 13) {
length = buffer.readUInt8(index) + 13
index += 1
} else if (length === 14) {
length = buffer.readUInt16BE(index) + 269
index += 2
} else if (length === 15) {
throw new Error('Wrong option length')
}
number += delta
options.push({
name: optionNumberToString(number),
value: buffer.slice(index, index + length)
})
index += length
}
return options
}
function toCode (code) {
const split = code.split && code.split('.')
let by = 0
if (split && split.length === 2) {
by |= parseInt(split[0]) << 5
by |= parseInt(split[1])
} else {
if (!split) {
code = parseInt(code)
}
by |= (code / 100) << 5
by |= (code % 100)
}
return by
}
function fillGenDefaults (packet) {
if (!packet) {
packet = {}
}
if (!packet.payload) {
packet.payload = empty
}
if (!packet.token) {
packet.token = empty
}
if (packet.token.length > 65804) {
throw new Error('Token too long')
}
if (!packet.code) {
packet.code = '0.01'
}
if (packet.messageId == null) {
packet.messageId = nextMsgId++
}
if (!packet.options) {
packet.options = []
}
if (nextMsgId === 65535) {
nextMsgId = 0
}
if (!packet.confirmable) {
packet.confirmable = false
}
if (!packet.reset) {
packet.reset = false
}
if (!packet.ack) {
packet.ack = false
}
return packet
}
function confirmableAckResetMask (packet) {
let result
if (packet.confirmable) {
result = 0 << 4
} else if (packet.ack) {
result = 2 << 4
} else if (packet.reset) {
result = 3 << 4
} else {
result = 1 << 4 // the message is non-confirmable
}
return result
}
function calculateLength (packet, options) {
let length = 4 + packet.payload.length + packet.token.length
if (packet.token.length > 12) {
length += 1
}
if (packet.token.length > 268) {
length += 1
}
if (packet.code !== '0.00' && packet.payload.toString() !== '') {
length += 1
}
for (let i = 0; i < options.length; i++) {
length += options[i].length
}
return length
}
function optionSorter (a, b) {
a = a.name
b = b.name
a = parseInt(OPTIONS_BY_NAME[a] || a)
b = parseInt(OPTIONS_BY_NAME[b] || b)
if (a < b) {
return -1
}
if (a > b) {
return 1
}
return 0
}
function prepareOptions (packet) {
const options = []
let total = 0
let delta
let buffer
let byte
let option
let i
let pos
let value
packet.options.sort(optionSorter)
for (i = 0; i < packet.options.length; i++) {
pos = 0
option = packet.options[i].name
delta = optionStringToNumber(option) - total
value = packet.options[i].value
// max option length is 1 header, 2 ext numb, 2 ext length
buffer = Buffer.alloc(value.length + 5)
byte = 0
if (delta <= 12) {
byte |= delta << 4
} else if (delta > 12 && delta < 269) {
byte |= 13 << 4
} else {
byte |= 14 << 4
}
if (value.length <= 12) {
byte |= value.length
} else if (value.length > 12 && value.length < 269) {
byte |= 13
} else {
byte |= 14
}
buffer.writeUInt8(byte, pos++)
if (delta > 12 && delta < 269) {
buffer.writeUInt8(delta - 13, pos++)
} else if (delta >= 269) {
buffer.writeUInt16BE(delta - 269, pos)
pos += 2
}
if (value.length > 12 && value.length < 269) {
buffer.writeUInt8(value.length - 13, pos++)
} else if (value.length >= 269) {
buffer.writeUInt16BE(value.length - 269, pos)
pos += 2
}
value.copy(buffer, pos)
pos += value.length
total += delta
options.push(buffer.slice(0, pos))
}
return options
}