bson 5.1.0 → 5.3.0
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/README.md +3 -126
- package/bson.d.ts +79 -22
- package/lib/binary.d.ts +182 -0
- package/lib/binary.d.ts.map +1 -0
- package/lib/bson.bundle.js +87 -89
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +87 -89
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.d.ts +97 -0
- package/lib/bson.d.ts.map +1 -0
- package/lib/bson.mjs +87 -89
- package/lib/bson.mjs.map +1 -1
- package/lib/bson_value.d.ts +10 -0
- package/lib/bson_value.d.ts.map +1 -0
- package/lib/code.d.ts +32 -0
- package/lib/code.d.ts.map +1 -0
- package/lib/constants.d.ts +107 -0
- package/lib/constants.d.ts.map +1 -0
- package/lib/db_ref.d.ts +40 -0
- package/lib/db_ref.d.ts.map +1 -0
- package/lib/decimal128.d.ts +34 -0
- package/lib/decimal128.d.ts.map +1 -0
- package/lib/double.d.ts +35 -0
- package/lib/double.d.ts.map +1 -0
- package/lib/error.d.ts +50 -0
- package/lib/error.d.ts.map +1 -0
- package/lib/extended_json.d.ts +82 -0
- package/lib/extended_json.d.ts.map +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/int_32.d.ts +35 -0
- package/lib/int_32.d.ts.map +1 -0
- package/lib/long.d.ts +323 -0
- package/lib/long.d.ts.map +1 -0
- package/lib/max_key.d.ts +19 -0
- package/lib/max_key.d.ts.map +1 -0
- package/lib/min_key.d.ts +19 -0
- package/lib/min_key.d.ts.map +1 -0
- package/lib/objectid.d.ts +96 -0
- package/lib/objectid.d.ts.map +1 -0
- package/lib/regexp.d.ts +36 -0
- package/lib/regexp.d.ts.map +1 -0
- package/lib/symbol.d.ts +28 -0
- package/lib/symbol.d.ts.map +1 -0
- package/lib/timestamp.d.ts +66 -0
- package/lib/timestamp.d.ts.map +1 -0
- package/lib/validate_utf8.d.ts +10 -0
- package/lib/validate_utf8.d.ts.map +1 -0
- package/package.json +20 -20
- package/src/binary.ts +67 -43
- package/src/bson.ts +1 -1
- package/src/constants.ts +1 -1
- package/src/extended_json.ts +11 -3
- package/src/objectid.ts +11 -5
- package/src/parser/deserializer.ts +37 -12
- package/src/parser/serializer.ts +17 -4
- package/src/timestamp.ts +1 -1
- package/src/uuid_utils.ts +0 -33
package/lib/bson.cjs
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
function isAnyArrayBuffer(value) {
|
|
4
|
+
return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
|
|
5
|
+
}
|
|
6
|
+
function isUint8Array(value) {
|
|
7
|
+
return Object.prototype.toString.call(value) === '[object Uint8Array]';
|
|
8
|
+
}
|
|
9
|
+
function isRegExp(d) {
|
|
10
|
+
return Object.prototype.toString.call(d) === '[object RegExp]';
|
|
11
|
+
}
|
|
12
|
+
function isMap(d) {
|
|
13
|
+
return Object.prototype.toString.call(d) === '[object Map]';
|
|
14
|
+
}
|
|
15
|
+
function isDate(d) {
|
|
16
|
+
return Object.prototype.toString.call(d) === '[object Date]';
|
|
17
|
+
}
|
|
18
|
+
|
|
3
19
|
const BSON_MAJOR_VERSION = 5;
|
|
4
20
|
const BSON_INT32_MAX = 0x7fffffff;
|
|
5
21
|
const BSON_INT32_MIN = -0x80000000;
|
|
@@ -282,44 +298,6 @@ class BSONDataView extends DataView {
|
|
|
282
298
|
}
|
|
283
299
|
}
|
|
284
300
|
|
|
285
|
-
const VALIDATION_REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15})$/i;
|
|
286
|
-
const uuidValidateString = (str) => typeof str === 'string' && VALIDATION_REGEX.test(str);
|
|
287
|
-
const uuidHexStringToBuffer = (hexString) => {
|
|
288
|
-
if (!uuidValidateString(hexString)) {
|
|
289
|
-
throw new BSONError('UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".');
|
|
290
|
-
}
|
|
291
|
-
const sanitizedHexString = hexString.replace(/-/g, '');
|
|
292
|
-
return ByteUtils.fromHex(sanitizedHexString);
|
|
293
|
-
};
|
|
294
|
-
function bufferToUuidHexString(buffer, includeDashes = true) {
|
|
295
|
-
if (includeDashes) {
|
|
296
|
-
return [
|
|
297
|
-
ByteUtils.toHex(buffer.subarray(0, 4)),
|
|
298
|
-
ByteUtils.toHex(buffer.subarray(4, 6)),
|
|
299
|
-
ByteUtils.toHex(buffer.subarray(6, 8)),
|
|
300
|
-
ByteUtils.toHex(buffer.subarray(8, 10)),
|
|
301
|
-
ByteUtils.toHex(buffer.subarray(10, 16))
|
|
302
|
-
].join('-');
|
|
303
|
-
}
|
|
304
|
-
return ByteUtils.toHex(buffer);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
function isAnyArrayBuffer(value) {
|
|
308
|
-
return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
|
|
309
|
-
}
|
|
310
|
-
function isUint8Array(value) {
|
|
311
|
-
return Object.prototype.toString.call(value) === '[object Uint8Array]';
|
|
312
|
-
}
|
|
313
|
-
function isRegExp(d) {
|
|
314
|
-
return Object.prototype.toString.call(d) === '[object RegExp]';
|
|
315
|
-
}
|
|
316
|
-
function isMap(d) {
|
|
317
|
-
return Object.prototype.toString.call(d) === '[object Map]';
|
|
318
|
-
}
|
|
319
|
-
function isDate(d) {
|
|
320
|
-
return Object.prototype.toString.call(d) === '[object Date]';
|
|
321
|
-
}
|
|
322
|
-
|
|
323
301
|
class BSONValue {
|
|
324
302
|
get [Symbol.for('@@mdb.bson.version')]() {
|
|
325
303
|
return BSON_MAJOR_VERSION;
|
|
@@ -457,6 +435,12 @@ class Binary extends BSONValue {
|
|
|
457
435
|
}
|
|
458
436
|
throw new BSONError(`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`);
|
|
459
437
|
}
|
|
438
|
+
static createFromHexString(hex, subType) {
|
|
439
|
+
return new Binary(ByteUtils.fromHex(hex), subType);
|
|
440
|
+
}
|
|
441
|
+
static createFromBase64(base64, subType) {
|
|
442
|
+
return new Binary(ByteUtils.fromBase64(base64), subType);
|
|
443
|
+
}
|
|
460
444
|
static fromExtendedJSON(doc, options) {
|
|
461
445
|
options = options || {};
|
|
462
446
|
let data;
|
|
@@ -475,7 +459,7 @@ class Binary extends BSONValue {
|
|
|
475
459
|
}
|
|
476
460
|
else if ('$uuid' in doc) {
|
|
477
461
|
type = 4;
|
|
478
|
-
data =
|
|
462
|
+
data = UUID.bytesFromString(doc.$uuid);
|
|
479
463
|
}
|
|
480
464
|
if (!data) {
|
|
481
465
|
throw new BSONError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
|
|
@@ -486,7 +470,8 @@ class Binary extends BSONValue {
|
|
|
486
470
|
return this.inspect();
|
|
487
471
|
}
|
|
488
472
|
inspect() {
|
|
489
|
-
|
|
473
|
+
const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
|
|
474
|
+
return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
|
|
490
475
|
}
|
|
491
476
|
}
|
|
492
477
|
Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
|
@@ -501,47 +486,45 @@ Binary.SUBTYPE_ENCRYPTED = 6;
|
|
|
501
486
|
Binary.SUBTYPE_COLUMN = 7;
|
|
502
487
|
Binary.SUBTYPE_USER_DEFINED = 128;
|
|
503
488
|
const UUID_BYTE_LENGTH = 16;
|
|
489
|
+
const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
|
|
490
|
+
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;
|
|
504
491
|
class UUID extends Binary {
|
|
505
492
|
constructor(input) {
|
|
506
493
|
let bytes;
|
|
507
|
-
let hexStr;
|
|
508
494
|
if (input == null) {
|
|
509
495
|
bytes = UUID.generate();
|
|
510
496
|
}
|
|
511
497
|
else if (input instanceof UUID) {
|
|
512
498
|
bytes = ByteUtils.toLocalBufferType(new Uint8Array(input.buffer));
|
|
513
|
-
hexStr = input.__id;
|
|
514
499
|
}
|
|
515
500
|
else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
|
|
516
501
|
bytes = ByteUtils.toLocalBufferType(input);
|
|
517
502
|
}
|
|
518
503
|
else if (typeof input === 'string') {
|
|
519
|
-
bytes =
|
|
504
|
+
bytes = UUID.bytesFromString(input);
|
|
520
505
|
}
|
|
521
506
|
else {
|
|
522
507
|
throw new BSONError('Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).');
|
|
523
508
|
}
|
|
524
509
|
super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
|
|
525
|
-
this.__id = hexStr;
|
|
526
510
|
}
|
|
527
511
|
get id() {
|
|
528
512
|
return this.buffer;
|
|
529
513
|
}
|
|
530
514
|
set id(value) {
|
|
531
515
|
this.buffer = value;
|
|
532
|
-
if (UUID.cacheHexString) {
|
|
533
|
-
this.__id = bufferToUuidHexString(value);
|
|
534
|
-
}
|
|
535
516
|
}
|
|
536
517
|
toHexString(includeDashes = true) {
|
|
537
|
-
if (
|
|
538
|
-
return
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
518
|
+
if (includeDashes) {
|
|
519
|
+
return [
|
|
520
|
+
ByteUtils.toHex(this.buffer.subarray(0, 4)),
|
|
521
|
+
ByteUtils.toHex(this.buffer.subarray(4, 6)),
|
|
522
|
+
ByteUtils.toHex(this.buffer.subarray(6, 8)),
|
|
523
|
+
ByteUtils.toHex(this.buffer.subarray(8, 10)),
|
|
524
|
+
ByteUtils.toHex(this.buffer.subarray(10, 16))
|
|
525
|
+
].join('-');
|
|
543
526
|
}
|
|
544
|
-
return
|
|
527
|
+
return ByteUtils.toHex(this.buffer);
|
|
545
528
|
}
|
|
546
529
|
toString(encoding) {
|
|
547
530
|
if (encoding === 'hex')
|
|
@@ -580,24 +563,32 @@ class UUID extends Binary {
|
|
|
580
563
|
if (!input) {
|
|
581
564
|
return false;
|
|
582
565
|
}
|
|
583
|
-
if (input instanceof UUID) {
|
|
584
|
-
return true;
|
|
585
|
-
}
|
|
586
566
|
if (typeof input === 'string') {
|
|
587
|
-
return
|
|
567
|
+
return UUID.isValidUUIDString(input);
|
|
588
568
|
}
|
|
589
569
|
if (isUint8Array(input)) {
|
|
590
|
-
|
|
591
|
-
return false;
|
|
592
|
-
}
|
|
593
|
-
return (input[6] & 0xf0) === 0x40 && (input[8] & 0x80) === 0x80;
|
|
570
|
+
return input.byteLength === UUID_BYTE_LENGTH;
|
|
594
571
|
}
|
|
595
|
-
return
|
|
572
|
+
return (input._bsontype === 'Binary' &&
|
|
573
|
+
input.sub_type === this.SUBTYPE_UUID &&
|
|
574
|
+
input.buffer.byteLength === 16);
|
|
596
575
|
}
|
|
597
576
|
static createFromHexString(hexString) {
|
|
598
|
-
const buffer =
|
|
577
|
+
const buffer = UUID.bytesFromString(hexString);
|
|
599
578
|
return new UUID(buffer);
|
|
600
579
|
}
|
|
580
|
+
static createFromBase64(base64) {
|
|
581
|
+
return new UUID(ByteUtils.fromBase64(base64));
|
|
582
|
+
}
|
|
583
|
+
static bytesFromString(representation) {
|
|
584
|
+
if (!UUID.isValidUUIDString(representation)) {
|
|
585
|
+
throw new BSONError('UUID string representation must be 32 hex digits or canonical hyphenated representation');
|
|
586
|
+
}
|
|
587
|
+
return ByteUtils.fromHex(representation.replace(/-/g, ''));
|
|
588
|
+
}
|
|
589
|
+
static isValidUUIDString(representation) {
|
|
590
|
+
return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
|
|
591
|
+
}
|
|
601
592
|
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
602
593
|
return this.inspect();
|
|
603
594
|
}
|
|
@@ -605,6 +596,7 @@ class UUID extends Binary {
|
|
|
605
596
|
return `new UUID("${this.toHexString()}")`;
|
|
606
597
|
}
|
|
607
598
|
}
|
|
599
|
+
UUID.cacheHexString = false;
|
|
608
600
|
|
|
609
601
|
class Code extends BSONValue {
|
|
610
602
|
get _bsontype() {
|
|
@@ -2116,11 +2108,17 @@ class ObjectId extends BSONValue {
|
|
|
2116
2108
|
return new ObjectId(buffer);
|
|
2117
2109
|
}
|
|
2118
2110
|
static createFromHexString(hexString) {
|
|
2119
|
-
if (
|
|
2120
|
-
throw new BSONError('
|
|
2111
|
+
if (hexString?.length !== 24) {
|
|
2112
|
+
throw new BSONError('hex string must be 24 characters');
|
|
2121
2113
|
}
|
|
2122
2114
|
return new ObjectId(ByteUtils.fromHex(hexString));
|
|
2123
2115
|
}
|
|
2116
|
+
static createFromBase64(base64) {
|
|
2117
|
+
if (base64?.length !== 16) {
|
|
2118
|
+
throw new BSONError('base64 string must be 16 characters');
|
|
2119
|
+
}
|
|
2120
|
+
return new ObjectId(ByteUtils.fromBase64(base64));
|
|
2121
|
+
}
|
|
2124
2122
|
static isValid(id) {
|
|
2125
2123
|
if (id == null)
|
|
2126
2124
|
return false;
|
|
@@ -2775,7 +2773,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2775
2773
|
}
|
|
2776
2774
|
else {
|
|
2777
2775
|
value = new Binary(buffer.slice(index, index + binarySize), subType);
|
|
2778
|
-
if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
|
|
2776
|
+
if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
|
|
2779
2777
|
value = value.toUUID();
|
|
2780
2778
|
}
|
|
2781
2779
|
}
|
|
@@ -2801,11 +2799,11 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2801
2799
|
if (promoteBuffers && promoteValues) {
|
|
2802
2800
|
value = _buffer;
|
|
2803
2801
|
}
|
|
2804
|
-
else if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
|
|
2805
|
-
value = new Binary(buffer.slice(index, index + binarySize), subType).toUUID();
|
|
2806
|
-
}
|
|
2807
2802
|
else {
|
|
2808
2803
|
value = new Binary(buffer.slice(index, index + binarySize), subType);
|
|
2804
|
+
if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
|
|
2805
|
+
value = value.toUUID();
|
|
2806
|
+
}
|
|
2809
2807
|
}
|
|
2810
2808
|
}
|
|
2811
2809
|
index = index + binarySize;
|
|
@@ -4037,32 +4035,32 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
|
|
|
4037
4035
|
|
|
4038
4036
|
var bson = /*#__PURE__*/Object.freeze({
|
|
4039
4037
|
__proto__: null,
|
|
4040
|
-
|
|
4038
|
+
BSONError: BSONError,
|
|
4039
|
+
BSONRegExp: BSONRegExp,
|
|
4040
|
+
BSONRuntimeError: BSONRuntimeError,
|
|
4041
4041
|
BSONSymbol: BSONSymbol,
|
|
4042
|
-
|
|
4042
|
+
BSONType: BSONType,
|
|
4043
|
+
BSONValue: BSONValue,
|
|
4044
|
+
BSONVersionError: BSONVersionError,
|
|
4043
4045
|
Binary: Binary,
|
|
4044
|
-
|
|
4045
|
-
|
|
4046
|
-
|
|
4047
|
-
Timestamp: Timestamp,
|
|
4046
|
+
Code: Code,
|
|
4047
|
+
DBRef: DBRef,
|
|
4048
|
+
Decimal128: Decimal128,
|
|
4048
4049
|
Double: Double,
|
|
4050
|
+
EJSON: EJSON,
|
|
4049
4051
|
Int32: Int32,
|
|
4050
|
-
|
|
4052
|
+
Long: Long,
|
|
4051
4053
|
MaxKey: MaxKey,
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
serializeWithBufferAndIndex: serializeWithBufferAndIndex,
|
|
4057
|
-
deserialize: deserialize,
|
|
4054
|
+
MinKey: MinKey,
|
|
4055
|
+
ObjectId: ObjectId,
|
|
4056
|
+
Timestamp: Timestamp,
|
|
4057
|
+
UUID: UUID,
|
|
4058
4058
|
calculateObjectSize: calculateObjectSize,
|
|
4059
|
+
deserialize: deserialize,
|
|
4059
4060
|
deserializeStream: deserializeStream,
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
BSONRuntimeError: BSONRuntimeError,
|
|
4064
|
-
BSONType: BSONType,
|
|
4065
|
-
EJSON: EJSON
|
|
4061
|
+
serialize: serialize,
|
|
4062
|
+
serializeWithBufferAndIndex: serializeWithBufferAndIndex,
|
|
4063
|
+
setInternalBufferSize: setInternalBufferSize
|
|
4066
4064
|
});
|
|
4067
4065
|
|
|
4068
4066
|
exports.BSON = bson;
|