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