bson 5.3.0 → 6.0.0-alpha.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 +51 -43
- package/bson.d.ts +10 -23
- package/lib/bson.bundle.js +44 -71
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +44 -71
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +44 -71
- package/lib/bson.mjs.map +1 -1
- package/lib/bson.rn.cjs +4081 -0
- package/lib/bson.rn.cjs.map +1 -0
- package/package.json +26 -24
- package/src/binary.ts +19 -53
- package/src/bson.ts +2 -2
- package/src/constants.ts +1 -1
- package/src/objectid.ts +20 -37
- package/src/parser/deserializer.ts +8 -8
- package/src/timestamp.ts +7 -5
- package/src/utils/byte_utils.ts +2 -2
- package/src/utils/node_byte_utils.ts +3 -3
- package/src/utils/web_byte_utils.ts +2 -2
- package/vendor/base64/LICENSE-MIT.txt +20 -0
- package/vendor/base64/README.md +112 -0
- package/vendor/base64/base64.js +157 -0
- package/vendor/base64/package.json +43 -0
- package/vendor/text-encoding/LICENSE.md +237 -0
- package/vendor/text-encoding/README.md +111 -0
- package/vendor/text-encoding/index.js +9 -0
- package/vendor/text-encoding/lib/encoding-indexes.js +47 -0
- package/vendor/text-encoding/lib/encoding.js +3301 -0
- package/vendor/text-encoding/package.json +37 -0
- package/lib/binary.d.ts +0 -182
- package/lib/binary.d.ts.map +0 -1
- package/lib/bson.d.ts +0 -97
- package/lib/bson.d.ts.map +0 -1
- package/lib/bson_value.d.ts +0 -10
- package/lib/bson_value.d.ts.map +0 -1
- package/lib/code.d.ts +0 -32
- package/lib/code.d.ts.map +0 -1
- package/lib/constants.d.ts +0 -107
- package/lib/constants.d.ts.map +0 -1
- package/lib/db_ref.d.ts +0 -40
- package/lib/db_ref.d.ts.map +0 -1
- package/lib/decimal128.d.ts +0 -34
- package/lib/decimal128.d.ts.map +0 -1
- package/lib/double.d.ts +0 -35
- package/lib/double.d.ts.map +0 -1
- package/lib/error.d.ts +0 -50
- package/lib/error.d.ts.map +0 -1
- package/lib/extended_json.d.ts +0 -82
- package/lib/extended_json.d.ts.map +0 -1
- package/lib/index.d.ts +0 -4
- package/lib/index.d.ts.map +0 -1
- package/lib/int_32.d.ts +0 -35
- package/lib/int_32.d.ts.map +0 -1
- package/lib/long.d.ts +0 -323
- package/lib/long.d.ts.map +0 -1
- package/lib/max_key.d.ts +0 -19
- package/lib/max_key.d.ts.map +0 -1
- package/lib/min_key.d.ts +0 -19
- package/lib/min_key.d.ts.map +0 -1
- package/lib/objectid.d.ts +0 -96
- package/lib/objectid.d.ts.map +0 -1
- package/lib/regexp.d.ts +0 -36
- package/lib/regexp.d.ts.map +0 -1
- package/lib/symbol.d.ts +0 -28
- package/lib/symbol.d.ts.map +0 -1
- package/lib/timestamp.d.ts +0 -66
- package/lib/timestamp.d.ts.map +0 -1
- package/lib/validate_utf8.d.ts +0 -10
- package/lib/validate_utf8.d.ts.map +0 -1
package/lib/bson.mjs
CHANGED
|
@@ -14,7 +14,7 @@ function isDate(d) {
|
|
|
14
14
|
return Object.prototype.toString.call(d) === '[object Date]';
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
const BSON_MAJOR_VERSION =
|
|
17
|
+
const BSON_MAJOR_VERSION = 6;
|
|
18
18
|
const BSON_INT32_MAX = 0x7fffffff;
|
|
19
19
|
const BSON_INT32_MIN = -0x80000000;
|
|
20
20
|
const BSON_INT64_MAX = Math.pow(2, 63) - 1;
|
|
@@ -163,8 +163,8 @@ const nodeJsByteUtils = {
|
|
|
163
163
|
fromUTF8(text) {
|
|
164
164
|
return Buffer.from(text, 'utf8');
|
|
165
165
|
},
|
|
166
|
-
toUTF8(buffer) {
|
|
167
|
-
return nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8');
|
|
166
|
+
toUTF8(buffer, start, end) {
|
|
167
|
+
return nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8', start, end);
|
|
168
168
|
},
|
|
169
169
|
utf8ByteLength(input) {
|
|
170
170
|
return Buffer.byteLength(input, 'utf8');
|
|
@@ -274,8 +274,8 @@ const webByteUtils = {
|
|
|
274
274
|
fromUTF8(text) {
|
|
275
275
|
return new TextEncoder().encode(text);
|
|
276
276
|
},
|
|
277
|
-
toUTF8(uint8array) {
|
|
278
|
-
return new TextDecoder('utf8', { fatal: false }).decode(uint8array);
|
|
277
|
+
toUTF8(uint8array, start, end) {
|
|
278
|
+
return new TextDecoder('utf8', { fatal: false }).decode(uint8array.slice(start, end));
|
|
279
279
|
},
|
|
280
280
|
utf8ByteLength(input) {
|
|
281
281
|
return webByteUtils.fromUTF8(input).byteLength;
|
|
@@ -309,11 +309,11 @@ class Binary extends BSONValue {
|
|
|
309
309
|
constructor(buffer, subType) {
|
|
310
310
|
super();
|
|
311
311
|
if (!(buffer == null) &&
|
|
312
|
-
|
|
312
|
+
typeof buffer === 'string' &&
|
|
313
313
|
!ArrayBuffer.isView(buffer) &&
|
|
314
|
-
!(buffer
|
|
314
|
+
!isAnyArrayBuffer(buffer) &&
|
|
315
315
|
!Array.isArray(buffer)) {
|
|
316
|
-
throw new BSONError('Binary can only be constructed from
|
|
316
|
+
throw new BSONError('Binary can only be constructed from Uint8Array or number[]');
|
|
317
317
|
}
|
|
318
318
|
this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
|
|
319
319
|
if (buffer == null) {
|
|
@@ -321,15 +321,9 @@ class Binary extends BSONValue {
|
|
|
321
321
|
this.position = 0;
|
|
322
322
|
}
|
|
323
323
|
else {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
else if (Array.isArray(buffer)) {
|
|
328
|
-
this.buffer = ByteUtils.fromNumberArray(buffer);
|
|
329
|
-
}
|
|
330
|
-
else {
|
|
331
|
-
this.buffer = ByteUtils.toLocalBufferType(buffer);
|
|
332
|
-
}
|
|
324
|
+
this.buffer = Array.isArray(buffer)
|
|
325
|
+
? ByteUtils.fromNumberArray(buffer)
|
|
326
|
+
: ByteUtils.toLocalBufferType(buffer);
|
|
333
327
|
this.position = this.buffer.byteLength;
|
|
334
328
|
}
|
|
335
329
|
}
|
|
@@ -375,25 +369,17 @@ class Binary extends BSONValue {
|
|
|
375
369
|
offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
|
|
376
370
|
}
|
|
377
371
|
else if (typeof sequence === 'string') {
|
|
378
|
-
|
|
379
|
-
this.buffer.set(bytes, offset);
|
|
380
|
-
this.position =
|
|
381
|
-
offset + sequence.length > this.position ? offset + sequence.length : this.position;
|
|
372
|
+
throw new BSONError('input cannot be string');
|
|
382
373
|
}
|
|
383
374
|
}
|
|
384
375
|
read(position, length) {
|
|
385
376
|
length = length && length > 0 ? length : this.position;
|
|
386
377
|
return this.buffer.slice(position, position + length);
|
|
387
378
|
}
|
|
388
|
-
value(
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
}
|
|
393
|
-
if (asRaw) {
|
|
394
|
-
return this.buffer.slice(0, this.position);
|
|
395
|
-
}
|
|
396
|
-
return ByteUtils.toISO88591(this.buffer.subarray(0, this.position));
|
|
379
|
+
value() {
|
|
380
|
+
return this.buffer.length === this.position
|
|
381
|
+
? this.buffer
|
|
382
|
+
: this.buffer.subarray(0, this.position);
|
|
397
383
|
}
|
|
398
384
|
length() {
|
|
399
385
|
return this.position;
|
|
@@ -407,8 +393,8 @@ class Binary extends BSONValue {
|
|
|
407
393
|
if (encoding === 'base64')
|
|
408
394
|
return ByteUtils.toBase64(this.buffer);
|
|
409
395
|
if (encoding === 'utf8' || encoding === 'utf-8')
|
|
410
|
-
return ByteUtils.toUTF8(this.buffer);
|
|
411
|
-
return ByteUtils.toUTF8(this.buffer);
|
|
396
|
+
return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
|
|
397
|
+
return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
|
|
412
398
|
}
|
|
413
399
|
toExtendedJSON(options) {
|
|
414
400
|
options = options || {};
|
|
@@ -594,7 +580,6 @@ class UUID extends Binary {
|
|
|
594
580
|
return `new UUID("${this.toHexString()}")`;
|
|
595
581
|
}
|
|
596
582
|
}
|
|
597
|
-
UUID.cacheHexString = false;
|
|
598
583
|
|
|
599
584
|
class Code extends BSONValue {
|
|
600
585
|
get _bsontype() {
|
|
@@ -1988,20 +1973,11 @@ class ObjectId extends BSONValue {
|
|
|
1988
1973
|
this[kId] = ByteUtils.toLocalBufferType(workingId);
|
|
1989
1974
|
}
|
|
1990
1975
|
else if (typeof workingId === 'string') {
|
|
1991
|
-
if (workingId.length ===
|
|
1992
|
-
const bytes = ByteUtils.fromUTF8(workingId);
|
|
1993
|
-
if (bytes.byteLength === 12) {
|
|
1994
|
-
this[kId] = bytes;
|
|
1995
|
-
}
|
|
1996
|
-
else {
|
|
1997
|
-
throw new BSONError('Argument passed in must be a string of 12 bytes');
|
|
1998
|
-
}
|
|
1999
|
-
}
|
|
2000
|
-
else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
|
|
1976
|
+
if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
|
|
2001
1977
|
this[kId] = ByteUtils.fromHex(workingId);
|
|
2002
1978
|
}
|
|
2003
1979
|
else {
|
|
2004
|
-
throw new BSONError('
|
|
1980
|
+
throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
|
|
2005
1981
|
}
|
|
2006
1982
|
}
|
|
2007
1983
|
else {
|
|
@@ -2063,30 +2039,25 @@ class ObjectId extends BSONValue {
|
|
|
2063
2039
|
toJSON() {
|
|
2064
2040
|
return this.toHexString();
|
|
2065
2041
|
}
|
|
2042
|
+
static is(variable) {
|
|
2043
|
+
return (variable != null &&
|
|
2044
|
+
typeof variable === 'object' &&
|
|
2045
|
+
'_bsontype' in variable &&
|
|
2046
|
+
variable._bsontype === 'ObjectId');
|
|
2047
|
+
}
|
|
2066
2048
|
equals(otherId) {
|
|
2067
2049
|
if (otherId === undefined || otherId === null) {
|
|
2068
2050
|
return false;
|
|
2069
2051
|
}
|
|
2070
|
-
if (otherId
|
|
2052
|
+
if (ObjectId.is(otherId)) {
|
|
2071
2053
|
return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
|
|
2072
2054
|
}
|
|
2073
|
-
if (typeof otherId === 'string'
|
|
2074
|
-
ObjectId.isValid(otherId) &&
|
|
2075
|
-
otherId.length === 12 &&
|
|
2076
|
-
isUint8Array(this.id)) {
|
|
2077
|
-
return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
|
|
2078
|
-
}
|
|
2079
|
-
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
|
|
2055
|
+
if (typeof otherId === 'string') {
|
|
2080
2056
|
return otherId.toLowerCase() === this.toHexString();
|
|
2081
2057
|
}
|
|
2082
|
-
if (typeof otherId === '
|
|
2083
|
-
return ByteUtils.equals(ByteUtils.fromUTF8(otherId), this.id);
|
|
2084
|
-
}
|
|
2085
|
-
if (typeof otherId === 'object' &&
|
|
2086
|
-
'toHexString' in otherId &&
|
|
2087
|
-
typeof otherId.toHexString === 'function') {
|
|
2058
|
+
if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') {
|
|
2088
2059
|
const otherIdString = otherId.toHexString();
|
|
2089
|
-
const thisIdString = this.toHexString()
|
|
2060
|
+
const thisIdString = this.toHexString();
|
|
2090
2061
|
return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
|
|
2091
2062
|
}
|
|
2092
2063
|
return false;
|
|
@@ -2414,19 +2385,21 @@ class Timestamp extends LongWithoutOverridesClass {
|
|
|
2414
2385
|
if (typeof low.i !== 'number' && (typeof low.i !== 'object' || low.i._bsontype !== 'Int32')) {
|
|
2415
2386
|
throw new BSONError('Timestamp constructed from { t, i } must provide i as a number');
|
|
2416
2387
|
}
|
|
2417
|
-
|
|
2388
|
+
const t = Number(low.t);
|
|
2389
|
+
const i = Number(low.i);
|
|
2390
|
+
if (t < 0 || Number.isNaN(t)) {
|
|
2418
2391
|
throw new BSONError('Timestamp constructed from { t, i } must provide a positive t');
|
|
2419
2392
|
}
|
|
2420
|
-
if (
|
|
2393
|
+
if (i < 0 || Number.isNaN(i)) {
|
|
2421
2394
|
throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
|
|
2422
2395
|
}
|
|
2423
|
-
if (
|
|
2396
|
+
if (t > 4294967295) {
|
|
2424
2397
|
throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
|
|
2425
2398
|
}
|
|
2426
|
-
if (
|
|
2399
|
+
if (i > 4294967295) {
|
|
2427
2400
|
throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
|
|
2428
2401
|
}
|
|
2429
|
-
super(
|
|
2402
|
+
super(i, t, true);
|
|
2430
2403
|
}
|
|
2431
2404
|
else {
|
|
2432
2405
|
throw new BSONError('A Timestamp can only be constructed with: bigint, Long, or { t: number; i: number }');
|
|
@@ -2598,7 +2571,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2598
2571
|
}
|
|
2599
2572
|
if (i >= buffer.byteLength)
|
|
2600
2573
|
throw new BSONError('Bad BSON Document: illegal CString');
|
|
2601
|
-
const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer
|
|
2574
|
+
const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer, index, i);
|
|
2602
2575
|
let shouldValidateKey = true;
|
|
2603
2576
|
if (globalUTFValidation || utf8KeysSet.has(name)) {
|
|
2604
2577
|
shouldValidateKey = validationSetting;
|
|
@@ -2813,7 +2786,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2813
2786
|
}
|
|
2814
2787
|
if (i >= buffer.length)
|
|
2815
2788
|
throw new BSONError('Bad BSON Document: illegal CString');
|
|
2816
|
-
const source = ByteUtils.toUTF8(buffer
|
|
2789
|
+
const source = ByteUtils.toUTF8(buffer, index, i);
|
|
2817
2790
|
index = i + 1;
|
|
2818
2791
|
i = index;
|
|
2819
2792
|
while (buffer[i] !== 0x00 && i < buffer.length) {
|
|
@@ -2821,7 +2794,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2821
2794
|
}
|
|
2822
2795
|
if (i >= buffer.length)
|
|
2823
2796
|
throw new BSONError('Bad BSON Document: illegal CString');
|
|
2824
|
-
const regExpOptions = ByteUtils.toUTF8(buffer
|
|
2797
|
+
const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
|
|
2825
2798
|
index = i + 1;
|
|
2826
2799
|
const optionsArray = new Array(regExpOptions.length);
|
|
2827
2800
|
for (i = 0; i < regExpOptions.length; i++) {
|
|
@@ -2846,7 +2819,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2846
2819
|
}
|
|
2847
2820
|
if (i >= buffer.length)
|
|
2848
2821
|
throw new BSONError('Bad BSON Document: illegal CString');
|
|
2849
|
-
const source = ByteUtils.toUTF8(buffer
|
|
2822
|
+
const source = ByteUtils.toUTF8(buffer, index, i);
|
|
2850
2823
|
index = i + 1;
|
|
2851
2824
|
i = index;
|
|
2852
2825
|
while (buffer[i] !== 0x00 && i < buffer.length) {
|
|
@@ -2854,7 +2827,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2854
2827
|
}
|
|
2855
2828
|
if (i >= buffer.length)
|
|
2856
2829
|
throw new BSONError('Bad BSON Document: illegal CString');
|
|
2857
|
-
const regExpOptions = ByteUtils.toUTF8(buffer
|
|
2830
|
+
const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
|
|
2858
2831
|
index = i + 1;
|
|
2859
2832
|
value = new BSONRegExp(source, regExpOptions);
|
|
2860
2833
|
}
|
|
@@ -2951,7 +2924,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2951
2924
|
throw new BSONError('Invalid UTF-8 string in BSON document');
|
|
2952
2925
|
}
|
|
2953
2926
|
}
|
|
2954
|
-
const namespace = ByteUtils.toUTF8(buffer
|
|
2927
|
+
const namespace = ByteUtils.toUTF8(buffer, index, index + stringSize - 1);
|
|
2955
2928
|
index = index + stringSize;
|
|
2956
2929
|
const oidBuffer = ByteUtils.allocate(12);
|
|
2957
2930
|
oidBuffer.set(buffer.subarray(index, index + 12), 0);
|
|
@@ -2991,7 +2964,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
|
|
|
2991
2964
|
return object;
|
|
2992
2965
|
}
|
|
2993
2966
|
function getValidatedString(buffer, start, end, shouldValidateUtf8) {
|
|
2994
|
-
const value = ByteUtils.toUTF8(buffer
|
|
2967
|
+
const value = ByteUtils.toUTF8(buffer, start, end);
|
|
2995
2968
|
if (shouldValidateUtf8) {
|
|
2996
2969
|
for (let i = 0; i < value.length; i++) {
|
|
2997
2970
|
if (value.charCodeAt(i) === 0xfffd) {
|