-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1207 lines (1181 loc) · 47 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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
/**
* @file Services Descriptor Bundle encoding and decoding library.
*
* @version 0.2.2
* @author Patrick Bay (Monican Agent)
* @copyright MIT License
*/
/**
* @class Handles encoding and decoding of Services Descriptor Bundle data to / from
* various formats.
*
* @see Ascii85/Base85 encoding and decoding adapted from ascii85.js by Yuri Konotopov -
* <a href="https://github.com/nE0sIghT/ascii85.js">https://github.com/nE0sIghT/ascii85.js</a>
*/
module.exports = class SDB {
/**
* Creates a new SDB instance.
*/
constructor() {
}
/**
* @property {Number} version=0 The SDB header / data format version
* used with coded binary data.
*/
static get version() {
return (1);
}
/**
* Validates a version 0 SDB entity object to ensure that there are no
* invalid or missing data properties.
*
* @param {Object} entityObj The entity object to validate.
*
* @return {null|String} A <code>null</code> is returned if all validation
* passed, otherwise a string is returned describing the validation failure.
*
* @static
*/
static validateEntityObject (entityObj) {
if (typeof(entityObj) != "object") {
return ("Entity must be an object.");
}
if (entityObj.entity == undefined) {
return ("\"entity\" property is required.");
}
if (typeof(entityObj.entity) != "string") {
return ("\"entity\" property must be a string.");
}
for (var dataType in entityObj) {
switch (dataType) {
case "entity":
var entType = entityObj[dataType];
if ((entType != "api") && (entType != "p2p") && (entType != "peer")) {
return ("\""+entType+"\" is not a recognized entity type.");
}
break;
case "name":
if (typeof(entityObj[dataType]) != "string") {
return ("\"name\" property must be a string.");
}
break;
case "description":
if (typeof(entityObj[dataType]) != "string") {
return ("\"description\" property must be a string.");
}
break;
case "transport":
switch (entityObj[dataType]) {
case "http": break;
case "wss": break;
case "wsst": break;
case "webrtc": break;
default:
return ("\""+entityObj[dataType]+"\" is not a recognized transport.");
break;
}
break;
case "protocol":
switch (entityObj[dataType]) {
case "http": break;
case "https": break;
case "ws": break;
case "wss": break;
default:
return ("\""+entityObj[dataType]+"\" is not a recognized protocol.");
break;
}
break;
case "host":
if (typeof(entityObj[dataType]) != "string") {
return ("\"host\" property must be a string.");
}
break;
case "port":
if (typeof(entityObj[dataType]) != "number") {
return ("\"port\" must be a number.");
}
break;
case "parameters":
if (typeof(entityObj[dataType]) != "string") {
return ("\"parameters\" property must be a string.");
}
break;
}
}
return (null); // everything looks okay
}
/**
* @property {Array} data=null The native JavaScript indexed array containing
* the SDB associated with this instance.
*
* @readonly
*/
get data() {
if (this._data == undefined) {
this._data = null;
}
return (this._data);
}
/**
* @property {Buffer} bin=null The native binary Buffer containing the SDB
* associated with this instance.
*
* @readonly
*/
get bin() {
if (this._bin == undefined) {
this._bin = null;
}
return (this._bin);
}
/**
* Encodes the native SDB [data]{@link SDB@data} object to a compressed
* base85 or base64 string. The processed binary data is also set to the
* [bin]{@link SDB@bin} Buffer.
*
* @param {String} [encoding="base85"] The desired data encoding to use,
* either "base85" or "base64".
* @param {Function} [processPipe=null] An optional processing function
* to be applied to the SDB binary Buffer before applying the <code>encoding</code>.
* If the referenced function is asynchronous (it returns a <code>Promise</code>),
* then This function will also return a <code>Promise</code> that will
* resolve when the <code>processPipe</code> has resolved, otherwise
* this function will be trated as a synchronous inline function.
*
* @return {Promise} An asynchronous promise is returned that resolves withthe
* SDB [data]{@link SDB@data} in the desired encoding.
*
*/
encode(encoding="base85", processPipe=null) {
if (this.data == null) {
throw (new Error("No SDB to encode."));
}
var entitiesBuff = Buffer.alloc(0);
var historyArr = new Array();
for (var count=0; count < this.data.length; count++) {
var entityData = this.data[count];
if (typeof(entityData.entity) != "string") {
throw (new Error ("Missing or wrong type \"+entity+\" property."));
}
var encodedEntity = this.encodeEntity(entityData, count, historyArr);
var newLength = entitiesBuff.length + encodedEntity.length;
entitiesBuff = Buffer.concat([entitiesBuff, encodedEntity], newLength);
}
//prepend version
var versionBuff = Buffer.from([SDB.version]);
newLength = entitiesBuff.length + 1;
entitiesBuff = Buffer.concat([versionBuff, entitiesBuff], newLength);
this._bin = entitiesBuff;
var promise = new Promise((resolve, reject) => {
if (processPipe != null) {
var result = processPipe(entitiesBuff);
if (result instanceof Promise) {
result.then(entitiesBuff => {
if ((encoding == "base85") || (encoding == "ascii85")) {
var returnStr = this.bufferToBase85(entitiesBuff);
} else if (encoding == "base64") {
returnStr = entitiesBuff.toString(encoding);
} else if (encoding == "none") {
returnStr = null;
}
resolve(returnStr);
})
} else {
if ((encoding == "base85") || (encoding == "ascii85")) {
var returnStr = this.bufferToBase85(entitiesBuff);
} else if (encoding == "base64") {
returnStr = entitiesBuff.toString(encoding);
} else if (encoding == "none") {
returnStr = null;
}
resolve(returnStr);
}
} else {
if ((encoding == "base85") || (encoding == "ascii85")) {
var returnStr = this.bufferToBase85(entitiesBuff);
} else if (encoding == "base64") {
returnStr = entitiesBuff.toString(encoding);
} else if (encoding == "none") {
returnStr = null;
}
resolve(returnStr);
}
});
return (promise);
}
/**
* Decodes the supplied SDB data and assigns it to the [data]{@link SDB@data}
* and [bin]{@link SDB@bin} properties if successful.
*
* @param {String|Array|Buffer} SDBData The data to decode. If this is a string,
* it is decoded using either the detected encoding method or the one specified
* by the <code>encoding</code> parameter. If this is an array it's assumed to
* be a native object and assigned to [data]{@link SDB@data} and [data]{@link SDB@data}.
* If this is a Buffer, it's assigned directly to the [data]{@link SDB@data} and
* decoded to [data]{@link SDB@data}.
* @param {String} [encoding=null] The string encoding used for <code>SDBData</code>
* if it's a string. If <code>SDBData</code> is not a string this parameter is ignored.
* If <code>processPipe</code> was supplied and was asynchronous (it returned a
* <code>Promise</code>) then a <code>Promise</code> will also be returned here and
* resolved when the <code>processPipe</code> resolves.
* @param {Function} [processPipe=null] An optional processing function
* to be applied to the decoded but unparsed SDB binary Buffer before applying
* the <code>decoding</code>.
*
* @return {Promise} An asynchronous promise is returned that resolves when the
* decoding process has completed.
*/
decode(SDBData, encoding=null, processPipe=null) {
if (SDBData instanceof Array) {
//no need to parse this
this._data = SDBData;
var promise = new Promise((resolve, reject) => {
this.encode("none").then(result => {
if (processPipe != null) {
if (processPipe instanceof Promise) {
processPipe(this.bin).then(result => {
resolve (this.bin);
})
} else {
result = processPipe(this.bin);
resolve (this.bin);
}
} else {
resolve (this.bin);
}
});
});
return (promise);
} else if (SDBData instanceof Buffer) {
//already native
var decodeBuff = SDBData;
} else if (typeof(SDBData) == "string") {
//either Base85 or Base64
SDBData = SDBData.trim();
if ((SDBData.indexOf("<~") > -1) || (encoding == "base85") || (encoding == "ascii85")) {
decodeBuff = this.base85ToBuffer(SDBData);
} else {
decodeBuff = Buffer.from(SDBData, "base64");
}
} else {
throw (new Error("Data type not recognized."));
}
var promise = new Promise((resolve, reject) => {
if (processPipe != null) {
var response = processPipe(decodeBuff);
if (response instanceof Promise) {
response.then(decodeBuff => {
var sdbVersion = decodeBuff.readUInt8(0);
if (sdbVersion != SDB.version) {
//future revisions may need more comlex checks
throw (new Error("SDB verion "+sdbVersion+ " does not match supported version "+SDB.version));
}
this._data = new Array();
var historyArr = new Array();
var entityIndex = 0;
var offset = 1;
while (offset < decodeBuff.length) {
try {
var entityObj = this.readEntity(decodeBuff, offset);
offset = entityObj.nextOffset;
this._data.push(this.decodeEntity(entityObj, entityIndex, historyArr));
entityIndex++;
} catch (err) {
offset = decodeBuff.length + 1;
}
}
});
resolve(true);
} else {
var sdbVersion = decodeBuff.readUInt8(0);
if (sdbVersion != SDB.version) {
//future revisions may need more comlex checks
throw (new Error("SDB verion "+sdbVersion+ " does not match supported version "+SDB.version));
}
this._data = new Array();
var historyArr = new Array();
var entityIndex = 0;
var offset = 1;
while (offset < decodeBuff.length) {
try {
var entityObj = this.readEntity(decodeBuff, offset);
offset = entityObj.nextOffset;
this._data.push(this.decodeEntity(entityObj, entityIndex, historyArr));
entityIndex++;
} catch (err) {
offset = decodeBuff.length + 1;
}
}
resolve(true);
}
} else {
var sdbVersion = decodeBuff.readUInt8(0);
if (sdbVersion != SDB.version) {
//future revisions may need more comlex checks
throw (new Error("SDB verion "+sdbVersion+ " does not match supported version "+SDB.version));
}
this._data = new Array();
var historyArr = new Array();
var entityIndex = 0;
var offset = 1;
while (offset < decodeBuff.length) {
try {
var entityObj = this.readEntity(decodeBuff, offset);
offset = entityObj.nextOffset;
this._data.push(this.decodeEntity(entityObj, entityIndex, historyArr));
entityIndex++;
} catch (err) {
offset = decodeBuff.length + 1;
}
}
resolve(true);
}
});
return (promise);
}
/**
* Encodes a binary Buffer to a Base85 / Ascii85 string.
*
* @param {Buffer} dataBuff The binary buffer to encode.
* @param {Boolean} [useDelimiters=true] If true, the Base85 <code><~ .. ~></code>
* bookend delimiters are included with the encoded string, otherwise the string
* is returned without them.
*
* @return {String} The Base85 / Ascii85 encoded string representation of the
* <code>dataBuff</code> Buffer.
*/
bufferToBase85(dataBuff, useDelimiters=true) {
var output = new Array();
if (useDelimiters) {
output.push(0x3c);
output.push(0x7e);
}
for (var count = 0; count < dataBuff.length; count += 4) {
let uint32 = Buffer.alloc(4);
let bytes = 4;
for (var count2 = 0; count2 < 4; count2++) {
if ((count + count2) < dataBuff.length) {
uint32[count2] = dataBuff[count + count2];
} else {
uint32[count2] = 0x00;
bytes--;
}
}
var chunk = this.getB85EncChunk(uint32, bytes);
for (count2 = 0; count2 < chunk.length; count2++) {
output.push(chunk[count2]);
}
}
if (useDelimiters) {
output.push(0x7e);
output.push(0x3e);
}
var outBuff = Buffer.from(output);
output = outBuff.toString("ascii");
return (output);
}
/**
* Encodes a Base85 / Ascii85 representation of a 32-bit unsigned integer.
*
* @param {Array} uint32 The data array containing values to encode.
*
* @return {String} A Base85 / Ascii85 representation if the input <code>uint32</code>.
*
* @private
*/
getB85EncChunk(uint32) {
var bytes = 4;
var dataChunk = ((uint32[0] << 24) | (uint32[1] << 16) | (uint32[2] << 8) | uint32[3]) >>> 0;
if (dataChunk === 0 && bytes == 4) {
var output = Buffer.alloc(1);
output[0] = 0x7a;
} else {
output = Buffer.alloc(bytes + 1);
for (var count = 4; count >= 0; count--) {
if (count <= bytes) {
output[count] = dataChunk % 85 + 0x21;
}
dataChunk /= 85;
}
}
return (output);
}
/**
* Converts a native 32-bit value to a multibyte array.
*
* @param {Number} uint32 The 32-bit value to split into 4 bytes.
* @param {Number} bytes The number of bytes in the <code>uint32</code>
* parameter to process.
*
* @return {Array} A byte array containg the binary representation of
* the input <code>uint32</code>
*
* @private
*/
uint32ToArray(uint32, bytes=4) {
var bitArr = [24, 16, 8, 0];
let output = Buffer.alloc(bytes);
for (var count = 0; count < bytes; count++) {
output[count] = (uint32 >> bitArr[count]) & 0x00ff;
}
return (output);
}
/**
* Converts a 32-bit unsigned integer value to a 4-byte array and
* pushes it onto an existing array of 4-byte values.
*
* @param {Number} uint32 The unsigned 32-bit integer value to push.
* @param {Number} uintIndex The number of bytes, minus 1, to convert
* to a byte array from <code>uint32</code>/
* @param {Array} uint32Array The byte array to which to append the byte
* array created from <code>uint32</code>.
*
* @private
*/
pushUint32Array(uint32, uintIndex, uint32Array) {
var byteArray = this.uint32ToArray(uint32, uintIndex - 1);
for (var count = 0; count < byteArray.length; count++) {
uint32Array.push(byteArray[count]);
}
}
/**
* Converts a Base85 / Ascii85 string to a Buffer.
*
* @param {String} b85String The Base58-encoded string to convert.
*
* @return {Buffer} The binary data represented by the <code>b85String</code>.
*
* @private
*/
base85ToBuffer(b85String) {
var pow85Arr = [Math.pow(85,4), Math.pow(85,3), Math.pow(85,2), 85, 1];
var output = new Array();
var stop = false;
var uint32 = 0;
var uint32Index = 0;
var position = 0;
if ((b85String.startsWith("<~") && b85String.length) > 2) {
var position = 2;
}
do {
if (b85String.charAt(position).trim().length === 0) {
//skip whitespace
continue;
}
var charCode = b85String.charCodeAt(position);
switch(charCode) {
case 0x7a:
if (uint32Index != 0) {
throw (new Error("Unexpected 'z' character at position " + i));
}
for (var count = 0; count < 4; count++) {
output.push(0x00);
}
break;
case 0x7e:
var nextChar = '';
var count = position + 1; // Skip whitespace + 1;
while (count < b85String.length && nextChar.trim().length == 0) {
nextChar = b85String.charAt(count++);
}
if (nextChar != '>') {
throw (new Error("Broken EOD at position " + j));
}
if (uint32Index) {
uint32 += pow85Arr[uint32Index - 1];
this.pushUint32Array(uint32, uint32Index, output);
uint32 = uint32Index = 0;
}
stop = true;
break;
default:
if ((charCode < 0x21) || (charCode > 0x75)) {
throw (new Error("Unexpected character with code " + charCode + " at position " + position));
}
uint32 += (charCode - 0x21) * pow85Arr[uint32Index++];
if (uint32Index >= 5) {
this.pushUint32Array(uint32, uint32Index, output);
uint32 = uint32Index = 0;
}
}
} while ((position++ < b85String.length) && (stop == false));
var outputBuff = Buffer.from(output);
return (outputBuff);
}
/**
* Reads a single SDB entity binary object and returns information about it.
*
* @param {Buffer} SDBBuffer The Buffer cotaining the entity to read.
* @param {Number} offset The byte offset at which the SDB entity starts within
* the <code>SDBBuffer</code>
*
* @return {Object} Contains the starting <code>offset</code> of the entity,
* the <code>nextOffset</code> (offset of the next entity), SDB <code>headerSize</code>,
* SDB <code>dataSize</code>, the <code>totalSize</code> (header plus data), the
* SDB entity <code>type</code>, and a pointer Buffer to the entity <code>data</code>.
*
* @private
*/
readEntity(SDBBuffer, offset) {
var headerSize = 5; //includes type and size
var entityType = SDBBuffer.readUInt8(offset + 0);
var dataSize = SDBBuffer.readUInt8(offset + 1) << 24;
dataSize = dataSize | SDBBuffer.readUInt8(offset + 2) << 16;
dataSize = dataSize | (SDBBuffer.readUInt8(offset + 3) << 8);
dataSize = dataSize | SDBBuffer.readUInt8(offset + 4);
var entityData = SDBBuffer.slice(offset + headerSize, offset + headerSize + dataSize); //note that this is a shared reference, not a new instance
var returnObj = new Object();
returnObj.offset = offset;
returnObj.nextOffset = offset + dataSize + headerSize;
returnObj.headerSize = headerSize;
returnObj.dataSize = dataSize;
returnObj.totalSize = dataSize + headerSize;
returnObj.type = entityType;
returnObj.data = entityData;
return (returnObj);
}
/**
* Decodes a single SDB entity and all of the contained data elements.
*
* @param {Object} entityObj An entity information object such as that cerated by
* [readEntity]{@link SDB#readEntity}.
* @param {Number} entityIndex The index of the entity being decoded, usually reflected
* in the entity's position within the [data]{@link SDB#data} array.
* @param {Array} historyArr Array of objects to use to determine reference data.
*
* @return {Object} The native JavaScript object representation of the SDB entity
* stored in <code>entityObj<code>.
*
* @private
*/
decodeEntity(entityObj, entityIndex, historyArr) {
var returnObj = new Object();
switch(entityObj.type) {
case 0:
returnObj.entity = "api";
break;
case 1:
returnObj.entity = "p2p";
break;
case 2:
returnObj.entity = "peer";
break;
default:
break;
}
var offset = 0;
var entityData = this.readEntityData(entityObj.data, offset);
while (entityData != null) {
offset = entityData.offset;
var entPropObj = this.getEntPropHistory(entityData.name, entityData.value, historyArr);
var previousIndex = entPropObj.entityIndex;
if (previousIndex > -1) {
//previous entity property already exists in history
for (var count=0; count < historyArr.length; count++) {
var historyObj = historyArr[count];
if ((historyObj.propName == entityData.name) && (historyObj.entityIndex == previousIndex)) {
entityData.value = historyObj.propValue;
}
}
}
returnObj[entityData.name] = entityData.value;
this.addEntPropHistory (entityData.name, entityData.value, entityIndex, historyArr);
entityData = this.readEntityData(entityObj.data, offset);
}
return (returnObj);
}
/**
* Reads the data, properties, or name-value pair of a SDB entity.
*
* @param {Buffer} entityBuffer The Buffer from which to extract the entity data.
* @param {Number} offset The offset, in bytes, of the data to extract within the
* entity data.
*
* @return {Object} The extracted data <code>name</code>, <code>value</code>,
* and an updated <code>offset</code> pointing to the next entity data object.
*
* @private
*/
readEntityData(entityBuffer, offset) {
if (entityBuffer.length == 0) {
return (null);
}
if (offset >= entityBuffer.length) {
return (null);
}
var returnObj = new Object();
var descriptorType = entityBuffer.readUInt8(offset);
var typeHeaderSize = 1; //1 byte for descriptorType
switch (descriptorType) {
case 0:
//name
var dataLength = entityBuffer.readUInt8(offset + 1) << 8;
dataLength = dataLength | entityBuffer.readUInt8(offset + 2);
typeHeaderSize += 2; //2 bytes for dataLength
var sliceStart = typeHeaderSize + offset;
var sliceEnd = dataLength + typeHeaderSize + offset;
var entityData = entityBuffer.slice(sliceStart, sliceEnd);
returnObj.name = "name";
returnObj.value = entityData.toString("utf8");
returnObj.offset = offset + entityData.length + typeHeaderSize;
break;
case 1:
//description
dataLength = entityBuffer.readUInt8(offset + 1) << 8;
dataLength = dataLength | entityBuffer.readUInt8(offset + 2);
typeHeaderSize += 2; //2 bytes for dataLength
sliceStart = typeHeaderSize + offset;
sliceEnd = dataLength + typeHeaderSize + offset;
entityData = entityBuffer.slice(sliceStart, sliceEnd);
returnObj.name = "description";
returnObj.value = entityData.toString("utf8");
returnObj.offset = offset + dataLength + typeHeaderSize;
break;
case 2:
//type
var typeVal = entityBuffer.readUInt8(offset + 1);
typeHeaderSize += 1;
returnObj.name = "transport";
switch (typeVal) {
case 0:
returnObj.value = "http";
break;
case 1:
returnObj.value = "wss";
break;
case 2:
returnObj.value = "wsst";
break;
case 3:
returnObj.value = "webrtc";
break;
default:
returnObj.value = "";
break;
}
returnObj.offset = offset + typeHeaderSize;
break;
case 3:
//protocol
returnObj.name = "protocol";
typeVal = entityBuffer.readUInt8(offset + 1);
typeHeaderSize += 1;
switch (typeVal) {
case 0:
returnObj.value = "http";
break;
case 1:
returnObj.value = "htps";
break;
case 2:
returnObj.value = "ws";
break;
case 3:
returnObj.value = "wss";
break;
default:
returnObj.value = "";
break;
}
returnObj.offset = offset + typeHeaderSize;
break;
case 4:
//host
returnObj.name = "host";
typeVal = entityBuffer.readUInt8(offset + 1);
typeHeaderSize += 1;
switch (typeVal) {
case 0:
//IPv4
var IPAddr = String(entityBuffer.readUInt8(offset + 2)) + ".";
IPAddr += String(entityBuffer.readUInt8(offset + 3)) + ".";
IPAddr += String(entityBuffer.readUInt8(offset + 4)) + ".";
IPAddr += String(entityBuffer.readUInt8(offset + 5));
typeHeaderSize += 4;
returnObj.value = IPAddr;
returnObj.offset = offset + typeHeaderSize;
break;
case 1:
//IPv6 -- not currently handled
//typeHeaderSize += 16;
returnObj.value = "";
break;
case 2:
//named
dataLength = entityBuffer.readUInt8(offset + 2) << 8;
dataLength = dataLength | entityBuffer.readUInt8(offset + 3);
typeHeaderSize += 2; //2 bytes for dataLength
sliceStart = typeHeaderSize + offset;
sliceEnd = dataLength + typeHeaderSize + offset;
entityData = entityBuffer.slice(sliceStart, sliceEnd);
returnObj.value = entityData.toString("utf8");
returnObj.offset = offset + dataLength + typeHeaderSize;
break;
default:
break;
}
break;
case 5:
//port
returnObj.name = "port";
entityData = entityBuffer.readUInt8(offset + 1) << 8;
entityData = entityData | entityBuffer.readUInt8(offset + 2);
typeHeaderSize += 2;
returnObj.value = entityData;
returnObj.offset = offset + typeHeaderSize;
break;
case 6:
//parameters
returnObj.name = "parameters";
dataLength = entityBuffer.readUInt8(offset + 1) << 16;
dataLength = dataLength | (entityBuffer.readUInt8(offset + 2) << 8);
dataLength = dataLength | entityBuffer.readUInt8(offset + 3);
typeHeaderSize += 3;
sliceStart = typeHeaderSize + offset;
sliceEnd = dataLength + typeHeaderSize + offset;
entityData = entityBuffer.slice(sliceStart, sliceEnd);
returnObj.value = entityData.toString("utf8");
returnObj.offset = offset + dataLength + typeHeaderSize;
break;
case 7:
//name reference
returnObj.name = "name";
var refIndex = entityBuffer.readUInt8(offset + 1) << 8;
refIndex = refIndex | entityBuffer.readUInt8(offset + 2);
//get history ref here
typeHeaderSize += 2;
returnObj.value = null;
returnObj.offset = offset + typeHeaderSize;
break;
case 8:
//description reference
returnObj.name = "description";
var refIndex = entityBuffer.readUInt8(offset + 1) << 8;
refIndex = refIndex | entityBuffer.readUInt8(offset + 2);
//get history ref here
typeHeaderSize += 2;
returnObj.value = null;
returnObj.offset = offset + typeHeaderSize;
break;
case 9:
//host reference
returnObj.name = "host";
var refIndex = entityBuffer.readUInt8(offset + 1) << 8;
refIndex = refIndex | entityBuffer.readUInt8(offset + 2);
//get history ref here
typeHeaderSize += 2;
returnObj.value = null;
returnObj.offset = offset + typeHeaderSize;
break;
case 10:
//port reference
returnObj.name = "port";
var refIndex = entityBuffer.readUInt8(offset + 1) << 8;
refIndex = refIndex | entityBuffer.readUInt8(offset + 2);
//get history ref here
typeHeaderSize += 2;
returnObj.value = null;
returnObj.offset = offset + typeHeaderSize;
break;
case 11:
//parameters reference
returnObj.name = "parameters";
var refIndex = entityBuffer.readUInt8(offset + 1) << 8;
refIndex = refIndex | entityBuffer.readUInt8(offset + 2);
//get history ref here
typeHeaderSize += 2;
returnObj.value = null;
returnObj.offset = offset + typeHeaderSize;
break;
default:
break;
}
return (returnObj);
}
/**
* Converts a native JavaScript SDB entity to a compressed binary one.
*
* @param {Object} entityData The native JavaScript entity object to convert
* @param {Number} entityIndex The index of the entitiy object, usually as found
* within the [data]{@link SDB#data} array.
* @param {Array} historyArr The history array to use to create reference
* data entries.
*
* @return {Buffer} The compressed binary representaion of the <code>entityData</code>
* including all headers.
*
* @private
*/
encodeEntity(entityData, entityIndex, historyArr) {
var returnBuff = Buffer.alloc(0);
var header = Buffer.alloc(0);
for (var entityProperty in entityData) {
var entityValue = entityData[entityProperty];
switch (entityProperty) {
case "entity":
//assign to header instead of returnBuff
var header = this.encodeSDBEntityData(entityProperty, entityValue, entityIndex, historyArr);
break;
case "url":
//parse compact form url
var urlObj = new URL(entityValue);
var protocol = urlObj.protocol;
protocol = protocol.split(":")[0]; //no trailing ":"
if (protocol == "") {
throw (new Error("Entity URL \""+entityValue+"\" uses an invalid protocol"));
}
var entityBuff = this.encodeSDBEntityData("protocol", protocol, entityIndex, historyArr);
var newLength = returnBuff.length + entityBuff.length;
returnBuff = Buffer.concat([returnBuff, entityBuff], newLength);
var host = urlObj.hostname;
//strip out IPv6 URL enclosure, as per https://tools.ietf.org/html/rfc2732
host = host.split("[").join("").split("]").join("");
entityBuff = this.encodeSDBEntityData("host", host, entityIndex, historyArr);
newLength = returnBuff.length + entityBuff.length;
returnBuff = Buffer.concat([returnBuff, entityBuff], newLength);
var port = Number(urlObj.port);
if (port != "") {
entityBuff = this.encodeSDBEntityData("port", port, entityIndex, historyArr);
newLength = returnBuff.length + entityBuff.length;
returnBuff = Buffer.concat([returnBuff, entityBuff], newLength);
}
var search = urlObj.search;
if (search != "") {
entityBuff = this.encodeSDBEntityData("parameters", search, entityIndex, historyArr);
newLength = returnBuff.length + entityBuff.length;
returnBuff = Buffer.concat([returnBuff, entityBuff], newLength);
}
break;
default:
//all other entity properties encoded as-is
var entityBuff = this.encodeSDBEntityData(entityProperty, entityValue, entityIndex, historyArr);
newLength = returnBuff.length + entityBuff.length;
returnBuff = Buffer.concat([returnBuff, entityBuff], newLength);
break;
}
}
//concatenate entity length to header
var lengthHeader = new Array();
lengthHeader.push((returnBuff.length & 0xFF000000) >> 24);
lengthHeader.push((returnBuff.length & 0xFF0000) >> 16);
lengthHeader.push((returnBuff.length & 0xFF00) >> 8);
lengthHeader.push(returnBuff.length & 0xFF);
var entityLength = Buffer.from(lengthHeader);
newLength = header.length + entityLength.length;
header = Buffer.concat([header, entityLength], newLength);
newLength = header.length + returnBuff.length;
//concatenate entity data to header
returnBuff = Buffer.concat([header, returnBuff], newLength);
return (returnBuff);
}
/**
* Encodes an entity's data to compressed SDB-formatted binary data.
*
* @param {String} propName The name of the data property to encode.
* @param {*} propValue The value of the data property to encode.
* @param {Number} entityIndex The index of containing entity, usually within
* the [data]{@link SDB@data} array.
* @param {Array} historyArr The history array to add the data to.
*
* @return {Buffer} The SDB-encoded binary data representation of the
* input data.
*
* @private
*/
encodeSDBEntityData(propName, propValue, propIndex, historyArr) {
var entPropObj = this.getEntPropHistory(propName, propValue, historyArr);
propName = entPropObj.propName;
propValue = entPropObj.propValue;
var encData = new Array();
switch (propName) {
case "entity":
switch (propValue) {
case "api":
encData.push(0);
break;
case "p2p":
encData.push(1);
break;
case "peer":
encData.push(2);
break;
default:
break;
}
break;
case "name":
encData.push(0);
var propLength = propValue.length;
if (propLength > 65535) {
//too long, cut off remainder
propValue = propValue.substring(0, 65535);
}
encData.push ((propLength & 0xFF00) >> 8);
encData.push (propLength & 0xFF);
for (var count=0; count < propValue.length; count++) {
encData.push(propValue.charCodeAt(count));
}
break;
case "description":
encData.push(1);
var propLength = propValue.length;
if (propLength > 65535) {
//too long, cut off remainder
propValue = propValue.substring(0, 65535);
}
encData.push ((propLength & 0xFF00) >> 8);
encData.push (propLength & 0xFF);
for (count=0; count < propValue.length; count++) {
encData.push(propValue.charCodeAt(count));
}
break;
case "transport":
encData.push(2);
switch (propValue) {
case "http":
encData.push(0);
break;
case "wss":
encData.push(1);
break;
case "wsst":
encData.push(2);
break;
case "webrtc":
encData.push(3);
break;
default:
break;
}
break;
case "protocol":
encData.push(3);
switch (propValue) {
case "http":
encData.push(0);
break;
case "https":
encData.push(1);
break;
case "ws":
encData.push(2);
break;
case "wss":
encData.push(3);
break;
default:
break;
}
break;
case "host":
encData.push(4);
if (this.isIPv4(propValue)) {
//resolved IPv4 host
encData.push(0);
var IPSplit = propValue.split(".");
for (count = 0; count < IPSplit.length; count++) {
encData.push(parseInt(IPSplit[count]));