bson 6.9.0 → 6.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bson.d.ts +104 -3
- package/lib/bson.bundle.js +326 -218
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +326 -218
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +326 -218
- package/lib/bson.mjs.map +1 -1
- package/lib/bson.rn.cjs +326 -226
- package/lib/bson.rn.cjs.map +1 -1
- package/package.json +1 -2
- package/src/binary.ts +265 -9
- package/src/objectid.ts +18 -11
- package/src/parser/deserializer.ts +17 -45
- package/src/parser/serializer.ts +5 -1
- package/src/parser/utils.ts +30 -51
- package/src/utils/byte_utils.ts +2 -0
- package/src/utils/node_byte_utils.ts +7 -2
- package/src/utils/number_utils.ts +4 -0
- package/src/utils/web_byte_utils.ts +21 -2
package/lib/bson.bundle.js
CHANGED
|
@@ -1,46 +1,31 @@
|
|
|
1
1
|
var BSON = (function (exports) {
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
BigUint64Array: '[object BigUint64Array]',
|
|
11
|
-
RegExp: '[object RegExp]',
|
|
12
|
-
Map: '[object Map]',
|
|
13
|
-
Date: '[object Date]'
|
|
14
|
-
};
|
|
15
|
-
function getPrototypeString(value) {
|
|
16
|
-
let str = map.get(value);
|
|
17
|
-
if (!str) {
|
|
18
|
-
str = Object.prototype.toString.call(value);
|
|
19
|
-
if (value !== null && typeof value === 'object') {
|
|
20
|
-
map.set(value, str);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return str;
|
|
4
|
+
const TypedArrayPrototypeGetSymbolToStringTag = (() => {
|
|
5
|
+
const g = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(Uint8Array.prototype), Symbol.toStringTag).get;
|
|
6
|
+
return (value) => g.call(value);
|
|
7
|
+
})();
|
|
8
|
+
function isUint8Array(value) {
|
|
9
|
+
return TypedArrayPrototypeGetSymbolToStringTag(value) === 'Uint8Array';
|
|
24
10
|
}
|
|
25
11
|
function isAnyArrayBuffer(value) {
|
|
26
|
-
|
|
27
|
-
|
|
12
|
+
return (typeof value === 'object' &&
|
|
13
|
+
value != null &&
|
|
14
|
+
Symbol.toStringTag in value &&
|
|
15
|
+
(value[Symbol.toStringTag] === 'ArrayBuffer' ||
|
|
16
|
+
value[Symbol.toStringTag] === 'SharedArrayBuffer'));
|
|
28
17
|
}
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
return type === TYPES.Uint8Array;
|
|
32
|
-
}
|
|
33
|
-
function isRegExp(d) {
|
|
34
|
-
const type = getPrototypeString(d);
|
|
35
|
-
return type === TYPES.RegExp;
|
|
18
|
+
function isRegExp(regexp) {
|
|
19
|
+
return regexp instanceof RegExp || Object.prototype.toString.call(regexp) === '[object RegExp]';
|
|
36
20
|
}
|
|
37
|
-
function isMap(
|
|
38
|
-
|
|
39
|
-
|
|
21
|
+
function isMap(value) {
|
|
22
|
+
return (typeof value === 'object' &&
|
|
23
|
+
value != null &&
|
|
24
|
+
Symbol.toStringTag in value &&
|
|
25
|
+
value[Symbol.toStringTag] === 'Map');
|
|
40
26
|
}
|
|
41
|
-
function isDate(
|
|
42
|
-
|
|
43
|
-
return type === TYPES.Date;
|
|
27
|
+
function isDate(date) {
|
|
28
|
+
return date instanceof Date || Object.prototype.toString.call(date) === '[object Date]';
|
|
44
29
|
}
|
|
45
30
|
function defaultInspect(x, _options) {
|
|
46
31
|
return JSON.stringify(x, (k, v) => {
|
|
@@ -257,7 +242,7 @@ const nodeJsByteUtils = {
|
|
|
257
242
|
stringTag === '[object SharedArrayBuffer]') {
|
|
258
243
|
return Buffer.from(potentialBuffer);
|
|
259
244
|
}
|
|
260
|
-
throw new BSONError(`Cannot create Buffer from
|
|
245
|
+
throw new BSONError(`Cannot create Buffer from the passed potentialBuffer.`);
|
|
261
246
|
},
|
|
262
247
|
allocate(size) {
|
|
263
248
|
return Buffer.alloc(size);
|
|
@@ -315,7 +300,10 @@ const nodeJsByteUtils = {
|
|
|
315
300
|
}
|
|
316
301
|
return nodeJsByteUtils.toLocalBufferType(buffer).write(source, byteOffset, undefined, 'utf8');
|
|
317
302
|
},
|
|
318
|
-
randomBytes: nodejsRandomBytes
|
|
303
|
+
randomBytes: nodejsRandomBytes,
|
|
304
|
+
swap32(buffer) {
|
|
305
|
+
return nodeJsByteUtils.toLocalBufferType(buffer).swap32();
|
|
306
|
+
}
|
|
319
307
|
};
|
|
320
308
|
|
|
321
309
|
function isReactNative() {
|
|
@@ -360,7 +348,7 @@ const webByteUtils = {
|
|
|
360
348
|
stringTag === '[object SharedArrayBuffer]') {
|
|
361
349
|
return new Uint8Array(potentialUint8array);
|
|
362
350
|
}
|
|
363
|
-
throw new BSONError(`Cannot make a Uint8Array from
|
|
351
|
+
throw new BSONError(`Cannot make a Uint8Array from passed potentialBuffer.`);
|
|
364
352
|
},
|
|
365
353
|
allocate(size) {
|
|
366
354
|
if (typeof size !== 'number') {
|
|
@@ -432,7 +420,23 @@ const webByteUtils = {
|
|
|
432
420
|
uint8array.set(bytes, byteOffset);
|
|
433
421
|
return bytes.byteLength;
|
|
434
422
|
},
|
|
435
|
-
randomBytes: webRandomBytes
|
|
423
|
+
randomBytes: webRandomBytes,
|
|
424
|
+
swap32(buffer) {
|
|
425
|
+
if (buffer.length % 4 !== 0) {
|
|
426
|
+
throw new RangeError('Buffer size must be a multiple of 32-bits');
|
|
427
|
+
}
|
|
428
|
+
for (let i = 0; i < buffer.length; i += 4) {
|
|
429
|
+
const byte0 = buffer[i];
|
|
430
|
+
const byte1 = buffer[i + 1];
|
|
431
|
+
const byte2 = buffer[i + 2];
|
|
432
|
+
const byte3 = buffer[i + 3];
|
|
433
|
+
buffer[i] = byte3;
|
|
434
|
+
buffer[i + 1] = byte2;
|
|
435
|
+
buffer[i + 2] = byte1;
|
|
436
|
+
buffer[i + 3] = byte0;
|
|
437
|
+
}
|
|
438
|
+
return buffer;
|
|
439
|
+
}
|
|
436
440
|
};
|
|
437
441
|
|
|
438
442
|
const hasGlobalBuffer = typeof Buffer === 'function' && Buffer.prototype?._isBuffer !== true;
|
|
@@ -447,6 +451,134 @@ class BSONValue {
|
|
|
447
451
|
}
|
|
448
452
|
}
|
|
449
453
|
|
|
454
|
+
const FLOAT = new Float64Array(1);
|
|
455
|
+
const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
|
|
456
|
+
FLOAT[0] = -1;
|
|
457
|
+
const isBigEndian = FLOAT_BYTES[7] === 0;
|
|
458
|
+
const NumberUtils = {
|
|
459
|
+
isBigEndian,
|
|
460
|
+
getNonnegativeInt32LE(source, offset) {
|
|
461
|
+
if (source[offset + 3] > 127) {
|
|
462
|
+
throw new RangeError(`Size cannot be negative at offset: ${offset}`);
|
|
463
|
+
}
|
|
464
|
+
return (source[offset] |
|
|
465
|
+
(source[offset + 1] << 8) |
|
|
466
|
+
(source[offset + 2] << 16) |
|
|
467
|
+
(source[offset + 3] << 24));
|
|
468
|
+
},
|
|
469
|
+
getInt32LE(source, offset) {
|
|
470
|
+
return (source[offset] |
|
|
471
|
+
(source[offset + 1] << 8) |
|
|
472
|
+
(source[offset + 2] << 16) |
|
|
473
|
+
(source[offset + 3] << 24));
|
|
474
|
+
},
|
|
475
|
+
getUint32LE(source, offset) {
|
|
476
|
+
return (source[offset] +
|
|
477
|
+
source[offset + 1] * 256 +
|
|
478
|
+
source[offset + 2] * 65536 +
|
|
479
|
+
source[offset + 3] * 16777216);
|
|
480
|
+
},
|
|
481
|
+
getUint32BE(source, offset) {
|
|
482
|
+
return (source[offset + 3] +
|
|
483
|
+
source[offset + 2] * 256 +
|
|
484
|
+
source[offset + 1] * 65536 +
|
|
485
|
+
source[offset] * 16777216);
|
|
486
|
+
},
|
|
487
|
+
getBigInt64LE(source, offset) {
|
|
488
|
+
const lo = NumberUtils.getUint32LE(source, offset);
|
|
489
|
+
const hi = NumberUtils.getUint32LE(source, offset + 4);
|
|
490
|
+
return (BigInt(hi) << BigInt(32)) + BigInt(lo);
|
|
491
|
+
},
|
|
492
|
+
getFloat64LE: isBigEndian
|
|
493
|
+
? (source, offset) => {
|
|
494
|
+
FLOAT_BYTES[7] = source[offset];
|
|
495
|
+
FLOAT_BYTES[6] = source[offset + 1];
|
|
496
|
+
FLOAT_BYTES[5] = source[offset + 2];
|
|
497
|
+
FLOAT_BYTES[4] = source[offset + 3];
|
|
498
|
+
FLOAT_BYTES[3] = source[offset + 4];
|
|
499
|
+
FLOAT_BYTES[2] = source[offset + 5];
|
|
500
|
+
FLOAT_BYTES[1] = source[offset + 6];
|
|
501
|
+
FLOAT_BYTES[0] = source[offset + 7];
|
|
502
|
+
return FLOAT[0];
|
|
503
|
+
}
|
|
504
|
+
: (source, offset) => {
|
|
505
|
+
FLOAT_BYTES[0] = source[offset];
|
|
506
|
+
FLOAT_BYTES[1] = source[offset + 1];
|
|
507
|
+
FLOAT_BYTES[2] = source[offset + 2];
|
|
508
|
+
FLOAT_BYTES[3] = source[offset + 3];
|
|
509
|
+
FLOAT_BYTES[4] = source[offset + 4];
|
|
510
|
+
FLOAT_BYTES[5] = source[offset + 5];
|
|
511
|
+
FLOAT_BYTES[6] = source[offset + 6];
|
|
512
|
+
FLOAT_BYTES[7] = source[offset + 7];
|
|
513
|
+
return FLOAT[0];
|
|
514
|
+
},
|
|
515
|
+
setInt32BE(destination, offset, value) {
|
|
516
|
+
destination[offset + 3] = value;
|
|
517
|
+
value >>>= 8;
|
|
518
|
+
destination[offset + 2] = value;
|
|
519
|
+
value >>>= 8;
|
|
520
|
+
destination[offset + 1] = value;
|
|
521
|
+
value >>>= 8;
|
|
522
|
+
destination[offset] = value;
|
|
523
|
+
return 4;
|
|
524
|
+
},
|
|
525
|
+
setInt32LE(destination, offset, value) {
|
|
526
|
+
destination[offset] = value;
|
|
527
|
+
value >>>= 8;
|
|
528
|
+
destination[offset + 1] = value;
|
|
529
|
+
value >>>= 8;
|
|
530
|
+
destination[offset + 2] = value;
|
|
531
|
+
value >>>= 8;
|
|
532
|
+
destination[offset + 3] = value;
|
|
533
|
+
return 4;
|
|
534
|
+
},
|
|
535
|
+
setBigInt64LE(destination, offset, value) {
|
|
536
|
+
const mask32bits = BigInt(0xffff_ffff);
|
|
537
|
+
let lo = Number(value & mask32bits);
|
|
538
|
+
destination[offset] = lo;
|
|
539
|
+
lo >>= 8;
|
|
540
|
+
destination[offset + 1] = lo;
|
|
541
|
+
lo >>= 8;
|
|
542
|
+
destination[offset + 2] = lo;
|
|
543
|
+
lo >>= 8;
|
|
544
|
+
destination[offset + 3] = lo;
|
|
545
|
+
let hi = Number((value >> BigInt(32)) & mask32bits);
|
|
546
|
+
destination[offset + 4] = hi;
|
|
547
|
+
hi >>= 8;
|
|
548
|
+
destination[offset + 5] = hi;
|
|
549
|
+
hi >>= 8;
|
|
550
|
+
destination[offset + 6] = hi;
|
|
551
|
+
hi >>= 8;
|
|
552
|
+
destination[offset + 7] = hi;
|
|
553
|
+
return 8;
|
|
554
|
+
},
|
|
555
|
+
setFloat64LE: isBigEndian
|
|
556
|
+
? (destination, offset, value) => {
|
|
557
|
+
FLOAT[0] = value;
|
|
558
|
+
destination[offset] = FLOAT_BYTES[7];
|
|
559
|
+
destination[offset + 1] = FLOAT_BYTES[6];
|
|
560
|
+
destination[offset + 2] = FLOAT_BYTES[5];
|
|
561
|
+
destination[offset + 3] = FLOAT_BYTES[4];
|
|
562
|
+
destination[offset + 4] = FLOAT_BYTES[3];
|
|
563
|
+
destination[offset + 5] = FLOAT_BYTES[2];
|
|
564
|
+
destination[offset + 6] = FLOAT_BYTES[1];
|
|
565
|
+
destination[offset + 7] = FLOAT_BYTES[0];
|
|
566
|
+
return 8;
|
|
567
|
+
}
|
|
568
|
+
: (destination, offset, value) => {
|
|
569
|
+
FLOAT[0] = value;
|
|
570
|
+
destination[offset] = FLOAT_BYTES[0];
|
|
571
|
+
destination[offset + 1] = FLOAT_BYTES[1];
|
|
572
|
+
destination[offset + 2] = FLOAT_BYTES[2];
|
|
573
|
+
destination[offset + 3] = FLOAT_BYTES[3];
|
|
574
|
+
destination[offset + 4] = FLOAT_BYTES[4];
|
|
575
|
+
destination[offset + 5] = FLOAT_BYTES[5];
|
|
576
|
+
destination[offset + 6] = FLOAT_BYTES[6];
|
|
577
|
+
destination[offset + 7] = FLOAT_BYTES[7];
|
|
578
|
+
return 8;
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
|
|
450
582
|
class Binary extends BSONValue {
|
|
451
583
|
get _bsontype() {
|
|
452
584
|
return 'Binary';
|
|
@@ -519,7 +651,8 @@ class Binary extends BSONValue {
|
|
|
519
651
|
}
|
|
520
652
|
read(position, length) {
|
|
521
653
|
length = length && length > 0 ? length : this.position;
|
|
522
|
-
|
|
654
|
+
const end = position + length;
|
|
655
|
+
return this.buffer.subarray(position, end > this.position ? this.position : end);
|
|
523
656
|
}
|
|
524
657
|
value() {
|
|
525
658
|
return this.buffer.length === this.position
|
|
@@ -543,6 +676,9 @@ class Binary extends BSONValue {
|
|
|
543
676
|
}
|
|
544
677
|
toExtendedJSON(options) {
|
|
545
678
|
options = options || {};
|
|
679
|
+
if (this.sub_type === Binary.SUBTYPE_VECTOR) {
|
|
680
|
+
validateBinaryVector(this);
|
|
681
|
+
}
|
|
546
682
|
const base64String = ByteUtils.toBase64(this.buffer);
|
|
547
683
|
const subType = Number(this.sub_type).toString(16);
|
|
548
684
|
if (options.legacy) {
|
|
@@ -560,7 +696,7 @@ class Binary extends BSONValue {
|
|
|
560
696
|
}
|
|
561
697
|
toUUID() {
|
|
562
698
|
if (this.sub_type === Binary.SUBTYPE_UUID) {
|
|
563
|
-
return new UUID(this.buffer.
|
|
699
|
+
return new UUID(this.buffer.subarray(0, this.position));
|
|
564
700
|
}
|
|
565
701
|
throw new BSONError(`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`);
|
|
566
702
|
}
|
|
@@ -602,6 +738,99 @@ class Binary extends BSONValue {
|
|
|
602
738
|
const subTypeArg = inspect(this.sub_type, options);
|
|
603
739
|
return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
|
|
604
740
|
}
|
|
741
|
+
toInt8Array() {
|
|
742
|
+
if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
|
|
743
|
+
throw new BSONError('Binary sub_type is not Vector');
|
|
744
|
+
}
|
|
745
|
+
if (this.buffer[0] !== Binary.VECTOR_TYPE.Int8) {
|
|
746
|
+
throw new BSONError('Binary datatype field is not Int8');
|
|
747
|
+
}
|
|
748
|
+
return new Int8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
|
|
749
|
+
}
|
|
750
|
+
toFloat32Array() {
|
|
751
|
+
if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
|
|
752
|
+
throw new BSONError('Binary sub_type is not Vector');
|
|
753
|
+
}
|
|
754
|
+
if (this.buffer[0] !== Binary.VECTOR_TYPE.Float32) {
|
|
755
|
+
throw new BSONError('Binary datatype field is not Float32');
|
|
756
|
+
}
|
|
757
|
+
const floatBytes = new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
|
|
758
|
+
if (NumberUtils.isBigEndian)
|
|
759
|
+
ByteUtils.swap32(floatBytes);
|
|
760
|
+
return new Float32Array(floatBytes.buffer);
|
|
761
|
+
}
|
|
762
|
+
toPackedBits() {
|
|
763
|
+
if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
|
|
764
|
+
throw new BSONError('Binary sub_type is not Vector');
|
|
765
|
+
}
|
|
766
|
+
if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
|
|
767
|
+
throw new BSONError('Binary datatype field is not packed bit');
|
|
768
|
+
}
|
|
769
|
+
return new Uint8Array(this.buffer.buffer.slice(this.buffer.byteOffset + 2, this.buffer.byteOffset + this.position));
|
|
770
|
+
}
|
|
771
|
+
toBits() {
|
|
772
|
+
if (this.sub_type !== Binary.SUBTYPE_VECTOR) {
|
|
773
|
+
throw new BSONError('Binary sub_type is not Vector');
|
|
774
|
+
}
|
|
775
|
+
if (this.buffer[0] !== Binary.VECTOR_TYPE.PackedBit) {
|
|
776
|
+
throw new BSONError('Binary datatype field is not packed bit');
|
|
777
|
+
}
|
|
778
|
+
const byteCount = this.length() - 2;
|
|
779
|
+
const bitCount = byteCount * 8 - this.buffer[1];
|
|
780
|
+
const bits = new Int8Array(bitCount);
|
|
781
|
+
for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) {
|
|
782
|
+
const byteOffset = (bitOffset / 8) | 0;
|
|
783
|
+
const byte = this.buffer[byteOffset + 2];
|
|
784
|
+
const shift = 7 - (bitOffset % 8);
|
|
785
|
+
const bit = (byte >> shift) & 1;
|
|
786
|
+
bits[bitOffset] = bit;
|
|
787
|
+
}
|
|
788
|
+
return bits;
|
|
789
|
+
}
|
|
790
|
+
static fromInt8Array(array) {
|
|
791
|
+
const buffer = ByteUtils.allocate(array.byteLength + 2);
|
|
792
|
+
buffer[0] = Binary.VECTOR_TYPE.Int8;
|
|
793
|
+
buffer[1] = 0;
|
|
794
|
+
const intBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
795
|
+
buffer.set(intBytes, 2);
|
|
796
|
+
return new this(buffer, this.SUBTYPE_VECTOR);
|
|
797
|
+
}
|
|
798
|
+
static fromFloat32Array(array) {
|
|
799
|
+
const binaryBytes = ByteUtils.allocate(array.byteLength + 2);
|
|
800
|
+
binaryBytes[0] = Binary.VECTOR_TYPE.Float32;
|
|
801
|
+
binaryBytes[1] = 0;
|
|
802
|
+
const floatBytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
803
|
+
binaryBytes.set(floatBytes, 2);
|
|
804
|
+
if (NumberUtils.isBigEndian)
|
|
805
|
+
ByteUtils.swap32(new Uint8Array(binaryBytes.buffer, 2));
|
|
806
|
+
return new this(binaryBytes, this.SUBTYPE_VECTOR);
|
|
807
|
+
}
|
|
808
|
+
static fromPackedBits(array, padding = 0) {
|
|
809
|
+
const buffer = ByteUtils.allocate(array.byteLength + 2);
|
|
810
|
+
buffer[0] = Binary.VECTOR_TYPE.PackedBit;
|
|
811
|
+
buffer[1] = padding;
|
|
812
|
+
buffer.set(array, 2);
|
|
813
|
+
return new this(buffer, this.SUBTYPE_VECTOR);
|
|
814
|
+
}
|
|
815
|
+
static fromBits(bits) {
|
|
816
|
+
const byteLength = (bits.length + 7) >>> 3;
|
|
817
|
+
const bytes = new Uint8Array(byteLength + 2);
|
|
818
|
+
bytes[0] = Binary.VECTOR_TYPE.PackedBit;
|
|
819
|
+
const remainder = bits.length % 8;
|
|
820
|
+
bytes[1] = remainder === 0 ? 0 : 8 - remainder;
|
|
821
|
+
for (let bitOffset = 0; bitOffset < bits.length; bitOffset++) {
|
|
822
|
+
const byteOffset = bitOffset >>> 3;
|
|
823
|
+
const bit = bits[bitOffset];
|
|
824
|
+
if (bit !== 0 && bit !== 1) {
|
|
825
|
+
throw new BSONError(`Invalid bit value at ${bitOffset}: must be 0 or 1, found ${bits[bitOffset]}`);
|
|
826
|
+
}
|
|
827
|
+
if (bit === 0)
|
|
828
|
+
continue;
|
|
829
|
+
const shift = 7 - (bitOffset % 8);
|
|
830
|
+
bytes[byteOffset + 2] |= bit << shift;
|
|
831
|
+
}
|
|
832
|
+
return new this(bytes, Binary.SUBTYPE_VECTOR);
|
|
833
|
+
}
|
|
605
834
|
}
|
|
606
835
|
Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
|
607
836
|
Binary.BUFFER_SIZE = 256;
|
|
@@ -614,7 +843,30 @@ Binary.SUBTYPE_MD5 = 5;
|
|
|
614
843
|
Binary.SUBTYPE_ENCRYPTED = 6;
|
|
615
844
|
Binary.SUBTYPE_COLUMN = 7;
|
|
616
845
|
Binary.SUBTYPE_SENSITIVE = 8;
|
|
846
|
+
Binary.SUBTYPE_VECTOR = 9;
|
|
617
847
|
Binary.SUBTYPE_USER_DEFINED = 128;
|
|
848
|
+
Binary.VECTOR_TYPE = Object.freeze({
|
|
849
|
+
Int8: 0x03,
|
|
850
|
+
Float32: 0x27,
|
|
851
|
+
PackedBit: 0x10
|
|
852
|
+
});
|
|
853
|
+
function validateBinaryVector(vector) {
|
|
854
|
+
if (vector.sub_type !== Binary.SUBTYPE_VECTOR)
|
|
855
|
+
return;
|
|
856
|
+
const size = vector.position;
|
|
857
|
+
const datatype = vector.buffer[0];
|
|
858
|
+
const padding = vector.buffer[1];
|
|
859
|
+
if ((datatype === Binary.VECTOR_TYPE.Float32 || datatype === Binary.VECTOR_TYPE.Int8) &&
|
|
860
|
+
padding !== 0) {
|
|
861
|
+
throw new BSONError('Invalid Vector: padding must be zero for int8 and float32 vectors');
|
|
862
|
+
}
|
|
863
|
+
if (datatype === Binary.VECTOR_TYPE.PackedBit && padding !== 0 && size === 2) {
|
|
864
|
+
throw new BSONError('Invalid Vector: padding must be zero for packed bit vectors that are empty');
|
|
865
|
+
}
|
|
866
|
+
if (datatype === Binary.VECTOR_TYPE.PackedBit && padding > 7) {
|
|
867
|
+
throw new BSONError(`Invalid Vector: padding must be a value between 0 and 7. found: ${padding}`);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
618
870
|
const UUID_BYTE_LENGTH = 16;
|
|
619
871
|
const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
|
|
620
872
|
const UUID_WITH_DASHES = /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
|
|
@@ -2226,134 +2478,8 @@ class MinKey extends BSONValue {
|
|
|
2226
2478
|
}
|
|
2227
2479
|
}
|
|
2228
2480
|
|
|
2229
|
-
const FLOAT = new Float64Array(1);
|
|
2230
|
-
const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
|
|
2231
|
-
FLOAT[0] = -1;
|
|
2232
|
-
const isBigEndian = FLOAT_BYTES[7] === 0;
|
|
2233
|
-
const NumberUtils = {
|
|
2234
|
-
getNonnegativeInt32LE(source, offset) {
|
|
2235
|
-
if (source[offset + 3] > 127) {
|
|
2236
|
-
throw new RangeError(`Size cannot be negative at offset: ${offset}`);
|
|
2237
|
-
}
|
|
2238
|
-
return (source[offset] |
|
|
2239
|
-
(source[offset + 1] << 8) |
|
|
2240
|
-
(source[offset + 2] << 16) |
|
|
2241
|
-
(source[offset + 3] << 24));
|
|
2242
|
-
},
|
|
2243
|
-
getInt32LE(source, offset) {
|
|
2244
|
-
return (source[offset] |
|
|
2245
|
-
(source[offset + 1] << 8) |
|
|
2246
|
-
(source[offset + 2] << 16) |
|
|
2247
|
-
(source[offset + 3] << 24));
|
|
2248
|
-
},
|
|
2249
|
-
getUint32LE(source, offset) {
|
|
2250
|
-
return (source[offset] +
|
|
2251
|
-
source[offset + 1] * 256 +
|
|
2252
|
-
source[offset + 2] * 65536 +
|
|
2253
|
-
source[offset + 3] * 16777216);
|
|
2254
|
-
},
|
|
2255
|
-
getUint32BE(source, offset) {
|
|
2256
|
-
return (source[offset + 3] +
|
|
2257
|
-
source[offset + 2] * 256 +
|
|
2258
|
-
source[offset + 1] * 65536 +
|
|
2259
|
-
source[offset] * 16777216);
|
|
2260
|
-
},
|
|
2261
|
-
getBigInt64LE(source, offset) {
|
|
2262
|
-
const lo = NumberUtils.getUint32LE(source, offset);
|
|
2263
|
-
const hi = NumberUtils.getUint32LE(source, offset + 4);
|
|
2264
|
-
return (BigInt(hi) << BigInt(32)) + BigInt(lo);
|
|
2265
|
-
},
|
|
2266
|
-
getFloat64LE: isBigEndian
|
|
2267
|
-
? (source, offset) => {
|
|
2268
|
-
FLOAT_BYTES[7] = source[offset];
|
|
2269
|
-
FLOAT_BYTES[6] = source[offset + 1];
|
|
2270
|
-
FLOAT_BYTES[5] = source[offset + 2];
|
|
2271
|
-
FLOAT_BYTES[4] = source[offset + 3];
|
|
2272
|
-
FLOAT_BYTES[3] = source[offset + 4];
|
|
2273
|
-
FLOAT_BYTES[2] = source[offset + 5];
|
|
2274
|
-
FLOAT_BYTES[1] = source[offset + 6];
|
|
2275
|
-
FLOAT_BYTES[0] = source[offset + 7];
|
|
2276
|
-
return FLOAT[0];
|
|
2277
|
-
}
|
|
2278
|
-
: (source, offset) => {
|
|
2279
|
-
FLOAT_BYTES[0] = source[offset];
|
|
2280
|
-
FLOAT_BYTES[1] = source[offset + 1];
|
|
2281
|
-
FLOAT_BYTES[2] = source[offset + 2];
|
|
2282
|
-
FLOAT_BYTES[3] = source[offset + 3];
|
|
2283
|
-
FLOAT_BYTES[4] = source[offset + 4];
|
|
2284
|
-
FLOAT_BYTES[5] = source[offset + 5];
|
|
2285
|
-
FLOAT_BYTES[6] = source[offset + 6];
|
|
2286
|
-
FLOAT_BYTES[7] = source[offset + 7];
|
|
2287
|
-
return FLOAT[0];
|
|
2288
|
-
},
|
|
2289
|
-
setInt32BE(destination, offset, value) {
|
|
2290
|
-
destination[offset + 3] = value;
|
|
2291
|
-
value >>>= 8;
|
|
2292
|
-
destination[offset + 2] = value;
|
|
2293
|
-
value >>>= 8;
|
|
2294
|
-
destination[offset + 1] = value;
|
|
2295
|
-
value >>>= 8;
|
|
2296
|
-
destination[offset] = value;
|
|
2297
|
-
return 4;
|
|
2298
|
-
},
|
|
2299
|
-
setInt32LE(destination, offset, value) {
|
|
2300
|
-
destination[offset] = value;
|
|
2301
|
-
value >>>= 8;
|
|
2302
|
-
destination[offset + 1] = value;
|
|
2303
|
-
value >>>= 8;
|
|
2304
|
-
destination[offset + 2] = value;
|
|
2305
|
-
value >>>= 8;
|
|
2306
|
-
destination[offset + 3] = value;
|
|
2307
|
-
return 4;
|
|
2308
|
-
},
|
|
2309
|
-
setBigInt64LE(destination, offset, value) {
|
|
2310
|
-
const mask32bits = BigInt(0xffff_ffff);
|
|
2311
|
-
let lo = Number(value & mask32bits);
|
|
2312
|
-
destination[offset] = lo;
|
|
2313
|
-
lo >>= 8;
|
|
2314
|
-
destination[offset + 1] = lo;
|
|
2315
|
-
lo >>= 8;
|
|
2316
|
-
destination[offset + 2] = lo;
|
|
2317
|
-
lo >>= 8;
|
|
2318
|
-
destination[offset + 3] = lo;
|
|
2319
|
-
let hi = Number((value >> BigInt(32)) & mask32bits);
|
|
2320
|
-
destination[offset + 4] = hi;
|
|
2321
|
-
hi >>= 8;
|
|
2322
|
-
destination[offset + 5] = hi;
|
|
2323
|
-
hi >>= 8;
|
|
2324
|
-
destination[offset + 6] = hi;
|
|
2325
|
-
hi >>= 8;
|
|
2326
|
-
destination[offset + 7] = hi;
|
|
2327
|
-
return 8;
|
|
2328
|
-
},
|
|
2329
|
-
setFloat64LE: isBigEndian
|
|
2330
|
-
? (destination, offset, value) => {
|
|
2331
|
-
FLOAT[0] = value;
|
|
2332
|
-
destination[offset] = FLOAT_BYTES[7];
|
|
2333
|
-
destination[offset + 1] = FLOAT_BYTES[6];
|
|
2334
|
-
destination[offset + 2] = FLOAT_BYTES[5];
|
|
2335
|
-
destination[offset + 3] = FLOAT_BYTES[4];
|
|
2336
|
-
destination[offset + 4] = FLOAT_BYTES[3];
|
|
2337
|
-
destination[offset + 5] = FLOAT_BYTES[2];
|
|
2338
|
-
destination[offset + 6] = FLOAT_BYTES[1];
|
|
2339
|
-
destination[offset + 7] = FLOAT_BYTES[0];
|
|
2340
|
-
return 8;
|
|
2341
|
-
}
|
|
2342
|
-
: (destination, offset, value) => {
|
|
2343
|
-
FLOAT[0] = value;
|
|
2344
|
-
destination[offset] = FLOAT_BYTES[0];
|
|
2345
|
-
destination[offset + 1] = FLOAT_BYTES[1];
|
|
2346
|
-
destination[offset + 2] = FLOAT_BYTES[2];
|
|
2347
|
-
destination[offset + 3] = FLOAT_BYTES[3];
|
|
2348
|
-
destination[offset + 4] = FLOAT_BYTES[4];
|
|
2349
|
-
destination[offset + 5] = FLOAT_BYTES[5];
|
|
2350
|
-
destination[offset + 6] = FLOAT_BYTES[6];
|
|
2351
|
-
destination[offset + 7] = FLOAT_BYTES[7];
|
|
2352
|
-
return 8;
|
|
2353
|
-
}
|
|
2354
|
-
};
|
|
2355
|
-
|
|
2356
2481
|
let PROCESS_UNIQUE = null;
|
|
2482
|
+
const __idCache = new WeakMap();
|
|
2357
2483
|
class ObjectId extends BSONValue {
|
|
2358
2484
|
get _bsontype() {
|
|
2359
2485
|
return 'ObjectId';
|
|
@@ -2384,6 +2510,9 @@ class ObjectId extends BSONValue {
|
|
|
2384
2510
|
else if (typeof workingId === 'string') {
|
|
2385
2511
|
if (ObjectId.validateHexString(workingId)) {
|
|
2386
2512
|
this.buffer = ByteUtils.fromHex(workingId);
|
|
2513
|
+
if (ObjectId.cacheHexString) {
|
|
2514
|
+
__idCache.set(this, workingId);
|
|
2515
|
+
}
|
|
2387
2516
|
}
|
|
2388
2517
|
else {
|
|
2389
2518
|
throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
|
|
@@ -2392,9 +2521,6 @@ class ObjectId extends BSONValue {
|
|
|
2392
2521
|
else {
|
|
2393
2522
|
throw new BSONError('Argument passed in does not match the accepted types');
|
|
2394
2523
|
}
|
|
2395
|
-
if (ObjectId.cacheHexString) {
|
|
2396
|
-
this.__id = ByteUtils.toHex(this.id);
|
|
2397
|
-
}
|
|
2398
2524
|
}
|
|
2399
2525
|
get id() {
|
|
2400
2526
|
return this.buffer;
|
|
@@ -2402,7 +2528,7 @@ class ObjectId extends BSONValue {
|
|
|
2402
2528
|
set id(value) {
|
|
2403
2529
|
this.buffer = value;
|
|
2404
2530
|
if (ObjectId.cacheHexString) {
|
|
2405
|
-
this
|
|
2531
|
+
__idCache.set(this, ByteUtils.toHex(value));
|
|
2406
2532
|
}
|
|
2407
2533
|
}
|
|
2408
2534
|
static validateHexString(string) {
|
|
@@ -2420,12 +2546,14 @@ class ObjectId extends BSONValue {
|
|
|
2420
2546
|
return true;
|
|
2421
2547
|
}
|
|
2422
2548
|
toHexString() {
|
|
2423
|
-
if (ObjectId.cacheHexString
|
|
2424
|
-
|
|
2549
|
+
if (ObjectId.cacheHexString) {
|
|
2550
|
+
const __id = __idCache.get(this);
|
|
2551
|
+
if (__id)
|
|
2552
|
+
return __id;
|
|
2425
2553
|
}
|
|
2426
2554
|
const hexString = ByteUtils.toHex(this.id);
|
|
2427
|
-
if (ObjectId.cacheHexString
|
|
2428
|
-
this
|
|
2555
|
+
if (ObjectId.cacheHexString) {
|
|
2556
|
+
__idCache.set(this, hexString);
|
|
2429
2557
|
}
|
|
2430
2558
|
return hexString;
|
|
2431
2559
|
}
|
|
@@ -2549,6 +2677,9 @@ class ObjectId extends BSONValue {
|
|
|
2549
2677
|
static fromExtendedJSON(doc) {
|
|
2550
2678
|
return new ObjectId(doc.$oid);
|
|
2551
2679
|
}
|
|
2680
|
+
isCached() {
|
|
2681
|
+
return ObjectId.cacheHexString && __idCache.has(this);
|
|
2682
|
+
}
|
|
2552
2683
|
inspect(depth, options, inspect) {
|
|
2553
2684
|
inspect ??= defaultInspect;
|
|
2554
2685
|
return `new ObjectId(${inspect(this.toHexString(), options)})`;
|
|
@@ -3039,7 +3170,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
3039
3170
|
if (objectSize <= 0 || objectSize > buffer.length - index)
|
|
3040
3171
|
throw new BSONError('bad embedded document length in bson');
|
|
3041
3172
|
if (raw) {
|
|
3042
|
-
value = buffer.
|
|
3173
|
+
value = buffer.subarray(index, index + objectSize);
|
|
3043
3174
|
}
|
|
3044
3175
|
else {
|
|
3045
3176
|
let objectOptions = options;
|
|
@@ -3111,49 +3242,23 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
3111
3242
|
throw new BSONError('Negative binary type element size found');
|
|
3112
3243
|
if (binarySize > buffer.byteLength)
|
|
3113
3244
|
throw new BSONError('Binary type size larger than document size');
|
|
3114
|
-
if (
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
3119
|
-
|
|
3120
|
-
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
value = ByteUtils.toLocalBufferType(buffer.slice(index, index + binarySize));
|
|
3127
|
-
}
|
|
3128
|
-
else {
|
|
3129
|
-
value = new Binary(buffer.slice(index, index + binarySize), subType);
|
|
3130
|
-
if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
|
|
3131
|
-
value = value.toUUID();
|
|
3132
|
-
}
|
|
3133
|
-
}
|
|
3245
|
+
if (subType === Binary.SUBTYPE_BYTE_ARRAY) {
|
|
3246
|
+
binarySize = NumberUtils.getInt32LE(buffer, index);
|
|
3247
|
+
index += 4;
|
|
3248
|
+
if (binarySize < 0)
|
|
3249
|
+
throw new BSONError('Negative binary type element size found for subtype 0x02');
|
|
3250
|
+
if (binarySize > totalBinarySize - 4)
|
|
3251
|
+
throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
|
|
3252
|
+
if (binarySize < totalBinarySize - 4)
|
|
3253
|
+
throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
|
|
3254
|
+
}
|
|
3255
|
+
if (promoteBuffers && promoteValues) {
|
|
3256
|
+
value = ByteUtils.toLocalBufferType(buffer.subarray(index, index + binarySize));
|
|
3134
3257
|
}
|
|
3135
3258
|
else {
|
|
3136
|
-
|
|
3137
|
-
|
|
3138
|
-
|
|
3139
|
-
if (binarySize < 0)
|
|
3140
|
-
throw new BSONError('Negative binary type element size found for subtype 0x02');
|
|
3141
|
-
if (binarySize > totalBinarySize - 4)
|
|
3142
|
-
throw new BSONError('Binary type with subtype 0x02 contains too long binary size');
|
|
3143
|
-
if (binarySize < totalBinarySize - 4)
|
|
3144
|
-
throw new BSONError('Binary type with subtype 0x02 contains too short binary size');
|
|
3145
|
-
}
|
|
3146
|
-
if (promoteBuffers && promoteValues) {
|
|
3147
|
-
value = ByteUtils.allocateUnsafe(binarySize);
|
|
3148
|
-
for (i = 0; i < binarySize; i++) {
|
|
3149
|
-
value[i] = buffer[index + i];
|
|
3150
|
-
}
|
|
3151
|
-
}
|
|
3152
|
-
else {
|
|
3153
|
-
value = new Binary(buffer.slice(index, index + binarySize), subType);
|
|
3154
|
-
if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
|
|
3155
|
-
value = value.toUUID();
|
|
3156
|
-
}
|
|
3259
|
+
value = new Binary(buffer.subarray(index, index + binarySize), subType);
|
|
3260
|
+
if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
|
|
3261
|
+
value = value.toUUID();
|
|
3157
3262
|
}
|
|
3158
3263
|
}
|
|
3159
3264
|
index = index + binarySize;
|
|
@@ -3575,6 +3680,9 @@ function serializeBinary(buffer, key, value, index) {
|
|
|
3575
3680
|
size = size - 4;
|
|
3576
3681
|
index += NumberUtils.setInt32LE(buffer, index, size);
|
|
3577
3682
|
}
|
|
3683
|
+
if (value.sub_type === Binary.SUBTYPE_VECTOR) {
|
|
3684
|
+
validateBinaryVector(value);
|
|
3685
|
+
}
|
|
3578
3686
|
if (size <= 16) {
|
|
3579
3687
|
for (let i = 0; i < size; i++)
|
|
3580
3688
|
buffer[index + i] = data[i];
|