serializer.js
35.8 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
'use strict';
var writeIEEE754 = require('../float_parser').writeIEEE754,
Long = require('../long').Long,
Map = require('../map'),
Binary = require('../binary').Binary;
var normalizedFunctionString = require('./utils').normalizedFunctionString;
// try {
// var _Buffer = Uint8Array;
// } catch (e) {
// _Buffer = Buffer;
// }
var regexp = /\x00/; // eslint-disable-line no-control-regex
var ignoreKeys = ['$db', '$ref', '$id', '$clusterTime'];
// To ensure that 0.4 of node works correctly
var isDate = function isDate(d) {
return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]';
};
var isRegExp = function isRegExp(d) {
return Object.prototype.toString.call(d) === '[object RegExp]';
};
var serializeString = function(buffer, key, value, index, isArray) {
// Encode String type
buffer[index++] = BSON.BSON_DATA_STRING;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes + 1;
buffer[index - 1] = 0;
// Write the string
var size = buffer.write(value, index + 4, 'utf8');
// Write the size of the string to buffer
buffer[index + 3] = ((size + 1) >> 24) & 0xff;
buffer[index + 2] = ((size + 1) >> 16) & 0xff;
buffer[index + 1] = ((size + 1) >> 8) & 0xff;
buffer[index] = (size + 1) & 0xff;
// Update index
index = index + 4 + size;
// Write zero
buffer[index++] = 0;
return index;
};
var serializeNumber = function(buffer, key, value, index, isArray) {
// We have an integer value
if (Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
// If the value fits in 32 bits encode as int, if it fits in a double
// encode it as a double, otherwise long
if (value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) {
// Set int type 32 bits or less
buffer[index++] = BSON.BSON_DATA_INT;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the int value
buffer[index++] = value & 0xff;
buffer[index++] = (value >> 8) & 0xff;
buffer[index++] = (value >> 16) & 0xff;
buffer[index++] = (value >> 24) & 0xff;
} else if (value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
// Encode as double
buffer[index++] = BSON.BSON_DATA_NUMBER;
// Number of written bytes
numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write float
writeIEEE754(buffer, value, index, 'little', 52, 8);
// Ajust index
index = index + 8;
} else {
// Set long type
buffer[index++] = BSON.BSON_DATA_LONG;
// Number of written bytes
numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
var longVal = Long.fromNumber(value);
var lowBits = longVal.getLowBits();
var highBits = longVal.getHighBits();
// Encode low bits
buffer[index++] = lowBits & 0xff;
buffer[index++] = (lowBits >> 8) & 0xff;
buffer[index++] = (lowBits >> 16) & 0xff;
buffer[index++] = (lowBits >> 24) & 0xff;
// Encode high bits
buffer[index++] = highBits & 0xff;
buffer[index++] = (highBits >> 8) & 0xff;
buffer[index++] = (highBits >> 16) & 0xff;
buffer[index++] = (highBits >> 24) & 0xff;
}
} else {
// Encode as double
buffer[index++] = BSON.BSON_DATA_NUMBER;
// Number of written bytes
numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write float
writeIEEE754(buffer, value, index, 'little', 52, 8);
// Ajust index
index = index + 8;
}
return index;
};
var serializeNull = function(buffer, key, value, index, isArray) {
// Set long type
buffer[index++] = BSON.BSON_DATA_NULL;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
return index;
};
var serializeBoolean = function(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = BSON.BSON_DATA_BOOLEAN;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Encode the boolean value
buffer[index++] = value ? 1 : 0;
return index;
};
var serializeDate = function(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = BSON.BSON_DATA_DATE;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the date
var dateInMilis = Long.fromNumber(value.getTime());
var lowBits = dateInMilis.getLowBits();
var highBits = dateInMilis.getHighBits();
// Encode low bits
buffer[index++] = lowBits & 0xff;
buffer[index++] = (lowBits >> 8) & 0xff;
buffer[index++] = (lowBits >> 16) & 0xff;
buffer[index++] = (lowBits >> 24) & 0xff;
// Encode high bits
buffer[index++] = highBits & 0xff;
buffer[index++] = (highBits >> 8) & 0xff;
buffer[index++] = (highBits >> 16) & 0xff;
buffer[index++] = (highBits >> 24) & 0xff;
return index;
};
var serializeRegExp = function(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = BSON.BSON_DATA_REGEXP;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
if (value.source && value.source.match(regexp) != null) {
throw Error('value ' + value.source + ' must not contain null bytes');
}
// Adjust the index
index = index + buffer.write(value.source, index, 'utf8');
// Write zero
buffer[index++] = 0x00;
// Write the parameters
if (value.global) buffer[index++] = 0x73; // s
if (value.ignoreCase) buffer[index++] = 0x69; // i
if (value.multiline) buffer[index++] = 0x6d; // m
// Add ending zero
buffer[index++] = 0x00;
return index;
};
var serializeBSONRegExp = function(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = BSON.BSON_DATA_REGEXP;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Check the pattern for 0 bytes
if (value.pattern.match(regexp) != null) {
// The BSON spec doesn't allow keys with null bytes because keys are
// null-terminated.
throw Error('pattern ' + value.pattern + ' must not contain null bytes');
}
// Adjust the index
index = index + buffer.write(value.pattern, index, 'utf8');
// Write zero
buffer[index++] = 0x00;
// Write the options
index =
index +
buffer.write(
value.options
.split('')
.sort()
.join(''),
index,
'utf8'
);
// Add ending zero
buffer[index++] = 0x00;
return index;
};
var serializeMinMax = function(buffer, key, value, index, isArray) {
// Write the type of either min or max key
if (value === null) {
buffer[index++] = BSON.BSON_DATA_NULL;
} else if (value._bsontype === 'MinKey') {
buffer[index++] = BSON.BSON_DATA_MIN_KEY;
} else {
buffer[index++] = BSON.BSON_DATA_MAX_KEY;
}
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
return index;
};
var serializeObjectId = function(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = BSON.BSON_DATA_OID;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the objectId into the shared buffer
if (typeof value.id === 'string') {
buffer.write(value.id, index, 'binary');
} else if (value.id && value.id.copy) {
value.id.copy(buffer, index, 0, 12);
} else {
throw new Error('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
}
// Ajust index
return index + 12;
};
var serializeBuffer = function(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = BSON.BSON_DATA_BINARY;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Get size of the buffer (current write point)
var size = value.length;
// Write the size of the string to buffer
buffer[index++] = size & 0xff;
buffer[index++] = (size >> 8) & 0xff;
buffer[index++] = (size >> 16) & 0xff;
buffer[index++] = (size >> 24) & 0xff;
// Write the default subtype
buffer[index++] = BSON.BSON_BINARY_SUBTYPE_DEFAULT;
// Copy the content form the binary field to the buffer
value.copy(buffer, index, 0, size);
// Adjust the index
index = index + size;
return index;
};
var serializeObject = function(
buffer,
key,
value,
index,
checkKeys,
depth,
serializeFunctions,
ignoreUndefined,
isArray,
path
) {
for (var i = 0; i < path.length; i++) {
if (path[i] === value) throw new Error('cyclic dependency detected');
}
// Push value to stack
path.push(value);
// Write the type
buffer[index++] = Array.isArray(value) ? BSON.BSON_DATA_ARRAY : BSON.BSON_DATA_OBJECT;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
var endIndex = serializeInto(
buffer,
value,
checkKeys,
index,
depth + 1,
serializeFunctions,
ignoreUndefined,
path
);
// Pop stack
path.pop();
// Write size
return endIndex;
};
var serializeDecimal128 = function(buffer, key, value, index, isArray) {
buffer[index++] = BSON.BSON_DATA_DECIMAL128;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the data from the value
value.bytes.copy(buffer, index, 0, 16);
return index + 16;
};
var serializeLong = function(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = value._bsontype === 'Long' ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_TIMESTAMP;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the date
var lowBits = value.getLowBits();
var highBits = value.getHighBits();
// Encode low bits
buffer[index++] = lowBits & 0xff;
buffer[index++] = (lowBits >> 8) & 0xff;
buffer[index++] = (lowBits >> 16) & 0xff;
buffer[index++] = (lowBits >> 24) & 0xff;
// Encode high bits
buffer[index++] = highBits & 0xff;
buffer[index++] = (highBits >> 8) & 0xff;
buffer[index++] = (highBits >> 16) & 0xff;
buffer[index++] = (highBits >> 24) & 0xff;
return index;
};
var serializeInt32 = function(buffer, key, value, index, isArray) {
// Set int type 32 bits or less
buffer[index++] = BSON.BSON_DATA_INT;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the int value
buffer[index++] = value & 0xff;
buffer[index++] = (value >> 8) & 0xff;
buffer[index++] = (value >> 16) & 0xff;
buffer[index++] = (value >> 24) & 0xff;
return index;
};
var serializeDouble = function(buffer, key, value, index, isArray) {
// Encode as double
buffer[index++] = BSON.BSON_DATA_NUMBER;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write float
writeIEEE754(buffer, value, index, 'little', 52, 8);
// Ajust index
index = index + 8;
return index;
};
var serializeFunction = function(buffer, key, value, index, checkKeys, depth, isArray) {
buffer[index++] = BSON.BSON_DATA_CODE;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Function string
var functionString = normalizedFunctionString(value);
// Write the string
var size = buffer.write(functionString, index + 4, 'utf8') + 1;
// Write the size of the string to buffer
buffer[index] = size & 0xff;
buffer[index + 1] = (size >> 8) & 0xff;
buffer[index + 2] = (size >> 16) & 0xff;
buffer[index + 3] = (size >> 24) & 0xff;
// Update index
index = index + 4 + size - 1;
// Write zero
buffer[index++] = 0;
return index;
};
var serializeCode = function(
buffer,
key,
value,
index,
checkKeys,
depth,
serializeFunctions,
ignoreUndefined,
isArray
) {
if (value.scope && typeof value.scope === 'object') {
// Write the type
buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Starting index
var startIndex = index;
// Serialize the function
// Get the function string
var functionString = typeof value.code === 'string' ? value.code : value.code.toString();
// Index adjustment
index = index + 4;
// Write string into buffer
var codeSize = buffer.write(functionString, index + 4, 'utf8') + 1;
// Write the size of the string to buffer
buffer[index] = codeSize & 0xff;
buffer[index + 1] = (codeSize >> 8) & 0xff;
buffer[index + 2] = (codeSize >> 16) & 0xff;
buffer[index + 3] = (codeSize >> 24) & 0xff;
// Write end 0
buffer[index + 4 + codeSize - 1] = 0;
// Write the
index = index + codeSize + 4;
//
// Serialize the scope value
var endIndex = serializeInto(
buffer,
value.scope,
checkKeys,
index,
depth + 1,
serializeFunctions,
ignoreUndefined
);
index = endIndex - 1;
// Writ the total
var totalSize = endIndex - startIndex;
// Write the total size of the object
buffer[startIndex++] = totalSize & 0xff;
buffer[startIndex++] = (totalSize >> 8) & 0xff;
buffer[startIndex++] = (totalSize >> 16) & 0xff;
buffer[startIndex++] = (totalSize >> 24) & 0xff;
// Write trailing zero
buffer[index++] = 0;
} else {
buffer[index++] = BSON.BSON_DATA_CODE;
// Number of written bytes
numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Function string
functionString = value.code.toString();
// Write the string
var size = buffer.write(functionString, index + 4, 'utf8') + 1;
// Write the size of the string to buffer
buffer[index] = size & 0xff;
buffer[index + 1] = (size >> 8) & 0xff;
buffer[index + 2] = (size >> 16) & 0xff;
buffer[index + 3] = (size >> 24) & 0xff;
// Update index
index = index + 4 + size - 1;
// Write zero
buffer[index++] = 0;
}
return index;
};
var serializeBinary = function(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = BSON.BSON_DATA_BINARY;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Extract the buffer
var data = value.value(true);
// Calculate size
var size = value.position;
// Add the deprecated 02 type 4 bytes of size to total
if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) size = size + 4;
// Write the size of the string to buffer
buffer[index++] = size & 0xff;
buffer[index++] = (size >> 8) & 0xff;
buffer[index++] = (size >> 16) & 0xff;
buffer[index++] = (size >> 24) & 0xff;
// Write the subtype to the buffer
buffer[index++] = value.sub_type;
// If we have binary type 2 the 4 first bytes are the size
if (value.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
size = size - 4;
buffer[index++] = size & 0xff;
buffer[index++] = (size >> 8) & 0xff;
buffer[index++] = (size >> 16) & 0xff;
buffer[index++] = (size >> 24) & 0xff;
}
// Write the data to the object
data.copy(buffer, index, 0, value.position);
// Adjust the index
index = index + value.position;
return index;
};
var serializeSymbol = function(buffer, key, value, index, isArray) {
// Write the type
buffer[index++] = BSON.BSON_DATA_SYMBOL;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
// Write the string
var size = buffer.write(value.value, index + 4, 'utf8') + 1;
// Write the size of the string to buffer
buffer[index] = size & 0xff;
buffer[index + 1] = (size >> 8) & 0xff;
buffer[index + 2] = (size >> 16) & 0xff;
buffer[index + 3] = (size >> 24) & 0xff;
// Update index
index = index + 4 + size - 1;
// Write zero
buffer[index++] = 0x00;
return index;
};
var serializeDBRef = function(buffer, key, value, index, depth, serializeFunctions, isArray) {
// Write the type
buffer[index++] = BSON.BSON_DATA_OBJECT;
// Number of written bytes
var numberOfWrittenBytes = !isArray
? buffer.write(key, index, 'utf8')
: buffer.write(key, index, 'ascii');
// Encode the name
index = index + numberOfWrittenBytes;
buffer[index++] = 0;
var startIndex = index;
var endIndex;
// Serialize object
if (null != value.db) {
endIndex = serializeInto(
buffer,
{
$ref: value.namespace,
$id: value.oid,
$db: value.db
},
false,
index,
depth + 1,
serializeFunctions
);
} else {
endIndex = serializeInto(
buffer,
{
$ref: value.namespace,
$id: value.oid
},
false,
index,
depth + 1,
serializeFunctions
);
}
// Calculate object size
var size = endIndex - startIndex;
// Write the size
buffer[startIndex++] = size & 0xff;
buffer[startIndex++] = (size >> 8) & 0xff;
buffer[startIndex++] = (size >> 16) & 0xff;
buffer[startIndex++] = (size >> 24) & 0xff;
// Set index
return endIndex;
};
var serializeInto = function serializeInto(
buffer,
object,
checkKeys,
startingIndex,
depth,
serializeFunctions,
ignoreUndefined,
path
) {
startingIndex = startingIndex || 0;
path = path || [];
// Push the object to the path
path.push(object);
// Start place to serialize into
var index = startingIndex + 4;
// var self = this;
// Special case isArray
if (Array.isArray(object)) {
// Get object keys
for (var i = 0; i < object.length; i++) {
var key = '' + i;
var value = object[i];
// Is there an override value
if (value && value.toBSON) {
if (typeof value.toBSON !== 'function') throw new Error('toBSON is not a function');
value = value.toBSON();
}
var type = typeof value;
if (type === 'string') {
index = serializeString(buffer, key, value, index, true);
} else if (type === 'number') {
index = serializeNumber(buffer, key, value, index, true);
} else if (type === 'boolean') {
index = serializeBoolean(buffer, key, value, index, true);
} else if (value instanceof Date || isDate(value)) {
index = serializeDate(buffer, key, value, index, true);
} else if (value === undefined) {
index = serializeNull(buffer, key, value, index, true);
} else if (value === null) {
index = serializeNull(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') {
index = serializeObjectId(buffer, key, value, index, true);
} else if (Buffer.isBuffer(value)) {
index = serializeBuffer(buffer, key, value, index, true);
} else if (value instanceof RegExp || isRegExp(value)) {
index = serializeRegExp(buffer, key, value, index, true);
} else if (type === 'object' && value['_bsontype'] == null) {
index = serializeObject(
buffer,
key,
value,
index,
checkKeys,
depth,
serializeFunctions,
ignoreUndefined,
true,
path
);
} else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
index = serializeDecimal128(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
index = serializeLong(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'Double') {
index = serializeDouble(buffer, key, value, index, true);
} else if (typeof value === 'function' && serializeFunctions) {
index = serializeFunction(
buffer,
key,
value,
index,
checkKeys,
depth,
serializeFunctions,
true
);
} else if (value['_bsontype'] === 'Code') {
index = serializeCode(
buffer,
key,
value,
index,
checkKeys,
depth,
serializeFunctions,
ignoreUndefined,
true
);
} else if (value['_bsontype'] === 'Binary') {
index = serializeBinary(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'Symbol') {
index = serializeSymbol(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'DBRef') {
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, true);
} else if (value['_bsontype'] === 'BSONRegExp') {
index = serializeBSONRegExp(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'Int32') {
index = serializeInt32(buffer, key, value, index, true);
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
index = serializeMinMax(buffer, key, value, index, true);
} else if (typeof value['_bsontype'] !== 'undefined') {
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
}
}
} else if (object instanceof Map) {
var iterator = object.entries();
var done = false;
while (!done) {
// Unpack the next entry
var entry = iterator.next();
done = entry.done;
// Are we done, then skip and terminate
if (done) continue;
// Get the entry values
key = entry.value[0];
value = entry.value[1];
// Check the type of the value
type = typeof value;
// Check the key and throw error if it's illegal
if (typeof key === 'string' && ignoreKeys.indexOf(key) === -1) {
if (key.match(regexp) != null) {
// The BSON spec doesn't allow keys with null bytes because keys are
// null-terminated.
throw Error('key ' + key + ' must not contain null bytes');
}
if (checkKeys) {
if ('$' === key[0]) {
throw Error('key ' + key + " must not start with '$'");
} else if (~key.indexOf('.')) {
throw Error('key ' + key + " must not contain '.'");
}
}
}
if (type === 'string') {
index = serializeString(buffer, key, value, index);
} else if (type === 'number') {
index = serializeNumber(buffer, key, value, index);
} else if (type === 'boolean') {
index = serializeBoolean(buffer, key, value, index);
} else if (value instanceof Date || isDate(value)) {
index = serializeDate(buffer, key, value, index);
// } else if (value === undefined && ignoreUndefined === true) {
} else if (value === null || (value === undefined && ignoreUndefined === false)) {
index = serializeNull(buffer, key, value, index);
} else if (value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') {
index = serializeObjectId(buffer, key, value, index);
} else if (Buffer.isBuffer(value)) {
index = serializeBuffer(buffer, key, value, index);
} else if (value instanceof RegExp || isRegExp(value)) {
index = serializeRegExp(buffer, key, value, index);
} else if (type === 'object' && value['_bsontype'] == null) {
index = serializeObject(
buffer,
key,
value,
index,
checkKeys,
depth,
serializeFunctions,
ignoreUndefined,
false,
path
);
} else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
index = serializeDecimal128(buffer, key, value, index);
} else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
index = serializeLong(buffer, key, value, index);
} else if (value['_bsontype'] === 'Double') {
index = serializeDouble(buffer, key, value, index);
} else if (value['_bsontype'] === 'Code') {
index = serializeCode(
buffer,
key,
value,
index,
checkKeys,
depth,
serializeFunctions,
ignoreUndefined
);
} else if (typeof value === 'function' && serializeFunctions) {
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
} else if (value['_bsontype'] === 'Binary') {
index = serializeBinary(buffer, key, value, index);
} else if (value['_bsontype'] === 'Symbol') {
index = serializeSymbol(buffer, key, value, index);
} else if (value['_bsontype'] === 'DBRef') {
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
} else if (value['_bsontype'] === 'BSONRegExp') {
index = serializeBSONRegExp(buffer, key, value, index);
} else if (value['_bsontype'] === 'Int32') {
index = serializeInt32(buffer, key, value, index);
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
index = serializeMinMax(buffer, key, value, index);
} else if (typeof value['_bsontype'] !== 'undefined') {
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
}
}
} else {
// Did we provide a custom serialization method
if (object.toBSON) {
if (typeof object.toBSON !== 'function') throw new Error('toBSON is not a function');
object = object.toBSON();
if (object != null && typeof object !== 'object')
throw new Error('toBSON function did not return an object');
}
// Iterate over all the keys
for (key in object) {
value = object[key];
// Is there an override value
if (value && value.toBSON) {
if (typeof value.toBSON !== 'function') throw new Error('toBSON is not a function');
value = value.toBSON();
}
// Check the type of the value
type = typeof value;
// Check the key and throw error if it's illegal
if (typeof key === 'string' && ignoreKeys.indexOf(key) === -1) {
if (key.match(regexp) != null) {
// The BSON spec doesn't allow keys with null bytes because keys are
// null-terminated.
throw Error('key ' + key + ' must not contain null bytes');
}
if (checkKeys) {
if ('$' === key[0]) {
throw Error('key ' + key + " must not start with '$'");
} else if (~key.indexOf('.')) {
throw Error('key ' + key + " must not contain '.'");
}
}
}
if (type === 'string') {
index = serializeString(buffer, key, value, index);
} else if (type === 'number') {
index = serializeNumber(buffer, key, value, index);
} else if (type === 'boolean') {
index = serializeBoolean(buffer, key, value, index);
} else if (value instanceof Date || isDate(value)) {
index = serializeDate(buffer, key, value, index);
} else if (value === undefined) {
if (ignoreUndefined === false) index = serializeNull(buffer, key, value, index);
} else if (value === null) {
index = serializeNull(buffer, key, value, index);
} else if (value['_bsontype'] === 'ObjectID' || value['_bsontype'] === 'ObjectId') {
index = serializeObjectId(buffer, key, value, index);
} else if (Buffer.isBuffer(value)) {
index = serializeBuffer(buffer, key, value, index);
} else if (value instanceof RegExp || isRegExp(value)) {
index = serializeRegExp(buffer, key, value, index);
} else if (type === 'object' && value['_bsontype'] == null) {
index = serializeObject(
buffer,
key,
value,
index,
checkKeys,
depth,
serializeFunctions,
ignoreUndefined,
false,
path
);
} else if (type === 'object' && value['_bsontype'] === 'Decimal128') {
index = serializeDecimal128(buffer, key, value, index);
} else if (value['_bsontype'] === 'Long' || value['_bsontype'] === 'Timestamp') {
index = serializeLong(buffer, key, value, index);
} else if (value['_bsontype'] === 'Double') {
index = serializeDouble(buffer, key, value, index);
} else if (value['_bsontype'] === 'Code') {
index = serializeCode(
buffer,
key,
value,
index,
checkKeys,
depth,
serializeFunctions,
ignoreUndefined
);
} else if (typeof value === 'function' && serializeFunctions) {
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
} else if (value['_bsontype'] === 'Binary') {
index = serializeBinary(buffer, key, value, index);
} else if (value['_bsontype'] === 'Symbol') {
index = serializeSymbol(buffer, key, value, index);
} else if (value['_bsontype'] === 'DBRef') {
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
} else if (value['_bsontype'] === 'BSONRegExp') {
index = serializeBSONRegExp(buffer, key, value, index);
} else if (value['_bsontype'] === 'Int32') {
index = serializeInt32(buffer, key, value, index);
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
index = serializeMinMax(buffer, key, value, index);
} else if (typeof value['_bsontype'] !== 'undefined') {
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
}
}
}
// Remove the path
path.pop();
// Final padding byte for object
buffer[index++] = 0x00;
// Final size
var size = index - startingIndex;
// Write the size of the object
buffer[startingIndex++] = size & 0xff;
buffer[startingIndex++] = (size >> 8) & 0xff;
buffer[startingIndex++] = (size >> 16) & 0xff;
buffer[startingIndex++] = (size >> 24) & 0xff;
return index;
};
var BSON = {};
/**
* Contains the function cache if we have that enable to allow for avoiding the eval step on each deserialization, comparison is by md5
*
* @ignore
* @api private
*/
// var functionCache = (BSON.functionCache = {});
/**
* Number BSON Type
*
* @classconstant BSON_DATA_NUMBER
**/
BSON.BSON_DATA_NUMBER = 1;
/**
* String BSON Type
*
* @classconstant BSON_DATA_STRING
**/
BSON.BSON_DATA_STRING = 2;
/**
* Object BSON Type
*
* @classconstant BSON_DATA_OBJECT
**/
BSON.BSON_DATA_OBJECT = 3;
/**
* Array BSON Type
*
* @classconstant BSON_DATA_ARRAY
**/
BSON.BSON_DATA_ARRAY = 4;
/**
* Binary BSON Type
*
* @classconstant BSON_DATA_BINARY
**/
BSON.BSON_DATA_BINARY = 5;
/**
* ObjectID BSON Type, deprecated
*
* @classconstant BSON_DATA_UNDEFINED
**/
BSON.BSON_DATA_UNDEFINED = 6;
/**
* ObjectID BSON Type
*
* @classconstant BSON_DATA_OID
**/
BSON.BSON_DATA_OID = 7;
/**
* Boolean BSON Type
*
* @classconstant BSON_DATA_BOOLEAN
**/
BSON.BSON_DATA_BOOLEAN = 8;
/**
* Date BSON Type
*
* @classconstant BSON_DATA_DATE
**/
BSON.BSON_DATA_DATE = 9;
/**
* null BSON Type
*
* @classconstant BSON_DATA_NULL
**/
BSON.BSON_DATA_NULL = 10;
/**
* RegExp BSON Type
*
* @classconstant BSON_DATA_REGEXP
**/
BSON.BSON_DATA_REGEXP = 11;
/**
* Code BSON Type
*
* @classconstant BSON_DATA_CODE
**/
BSON.BSON_DATA_CODE = 13;
/**
* Symbol BSON Type
*
* @classconstant BSON_DATA_SYMBOL
**/
BSON.BSON_DATA_SYMBOL = 14;
/**
* Code with Scope BSON Type
*
* @classconstant BSON_DATA_CODE_W_SCOPE
**/
BSON.BSON_DATA_CODE_W_SCOPE = 15;
/**
* 32 bit Integer BSON Type
*
* @classconstant BSON_DATA_INT
**/
BSON.BSON_DATA_INT = 16;
/**
* Timestamp BSON Type
*
* @classconstant BSON_DATA_TIMESTAMP
**/
BSON.BSON_DATA_TIMESTAMP = 17;
/**
* Long BSON Type
*
* @classconstant BSON_DATA_LONG
**/
BSON.BSON_DATA_LONG = 18;
/**
* Long BSON Type
*
* @classconstant BSON_DATA_DECIMAL128
**/
BSON.BSON_DATA_DECIMAL128 = 19;
/**
* MinKey BSON Type
*
* @classconstant BSON_DATA_MIN_KEY
**/
BSON.BSON_DATA_MIN_KEY = 0xff;
/**
* MaxKey BSON Type
*
* @classconstant BSON_DATA_MAX_KEY
**/
BSON.BSON_DATA_MAX_KEY = 0x7f;
/**
* Binary Default Type
*
* @classconstant BSON_BINARY_SUBTYPE_DEFAULT
**/
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
/**
* Binary Function Type
*
* @classconstant BSON_BINARY_SUBTYPE_FUNCTION
**/
BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
/**
* Binary Byte Array Type
*
* @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
**/
BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
/**
* Binary UUID Type
*
* @classconstant BSON_BINARY_SUBTYPE_UUID
**/
BSON.BSON_BINARY_SUBTYPE_UUID = 3;
/**
* Binary MD5 Type
*
* @classconstant BSON_BINARY_SUBTYPE_MD5
**/
BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
/**
* Binary User Defined Type
*
* @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
**/
BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
// BSON MAX VALUES
BSON.BSON_INT32_MAX = 0x7fffffff;
BSON.BSON_INT32_MIN = -0x80000000;
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
// JS MAX PRECISE VALUES
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
// Internal long versions
// var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double.
// var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double.
module.exports = serializeInto;