bson 4.5.1 → 4.6.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/HISTORY.md +572 -0
- package/bower.json +1 -1
- package/bson.d.ts +70 -13
- package/dist/bson.browser.esm.js +314 -177
- package/dist/bson.browser.esm.js.map +1 -1
- package/dist/bson.browser.umd.js +317 -176
- package/dist/bson.browser.umd.js.map +1 -1
- package/dist/bson.bundle.js +317 -176
- package/dist/bson.bundle.js.map +1 -1
- package/dist/bson.esm.js +314 -177
- package/dist/bson.esm.js.map +1 -1
- package/lib/binary.js +11 -9
- package/lib/binary.js.map +1 -1
- package/lib/bson.js +11 -3
- package/lib/bson.js.map +1 -1
- package/lib/code.js +0 -1
- package/lib/code.js.map +1 -1
- package/lib/constants.js +5 -1
- package/lib/constants.js.map +1 -1
- package/lib/db_ref.js +0 -1
- package/lib/db_ref.js.map +1 -1
- package/lib/decimal128.js +6 -5
- package/lib/decimal128.js.map +1 -1
- package/lib/double.js +3 -1
- package/lib/double.js.map +1 -1
- package/lib/ensure_buffer.js +3 -2
- package/lib/ensure_buffer.js.map +1 -1
- package/lib/error.js +55 -0
- package/lib/error.js.map +1 -0
- package/lib/extended_json.js +11 -5
- package/lib/extended_json.js.map +1 -1
- package/lib/int_32.js +4 -2
- package/lib/int_32.js.map +1 -1
- package/lib/objectid.js +39 -35
- package/lib/objectid.js.map +1 -1
- package/lib/parser/deserializer.js +131 -53
- package/lib/parser/deserializer.js.map +1 -1
- package/lib/parser/serializer.js +13 -12
- package/lib/parser/serializer.js.map +1 -1
- package/lib/regexp.js +9 -2
- package/lib/regexp.js.map +1 -1
- package/lib/symbol.js +0 -2
- package/lib/symbol.js.map +1 -1
- package/lib/uuid.js +4 -4
- package/lib/uuid.js.map +1 -1
- package/lib/uuid_utils.js +2 -1
- package/lib/uuid_utils.js.map +1 -1
- package/package.json +16 -3
- package/src/binary.ts +12 -10
- package/src/bson.ts +7 -1
- package/src/code.ts +0 -1
- package/src/constants.ts +6 -0
- package/src/db_ref.ts +0 -1
- package/src/decimal128.ts +6 -5
- package/src/double.ts +4 -1
- package/src/ensure_buffer.ts +3 -2
- package/src/error.ts +23 -0
- package/src/extended_json.ts +13 -5
- package/src/int_32.ts +5 -2
- package/src/objectid.ts +40 -43
- package/src/parser/deserializer.ts +159 -57
- package/src/parser/serializer.ts +13 -12
- package/src/regexp.ts +14 -2
- package/src/symbol.ts +0 -2
- package/src/uuid.ts +4 -4
- package/src/uuid_utils.ts +2 -1
package/src/parser/serializer.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type { DBRefLike } from '../db_ref';
|
|
|
7
7
|
import type { Decimal128 } from '../decimal128';
|
|
8
8
|
import type { Double } from '../double';
|
|
9
9
|
import { ensureBuffer } from '../ensure_buffer';
|
|
10
|
+
import { BSONError, BSONTypeError } from '../error';
|
|
10
11
|
import { isBSONType } from '../extended_json';
|
|
11
12
|
import { writeIEEE754 } from '../float_parser';
|
|
12
13
|
import type { Int32 } from '../int_32';
|
|
@@ -311,7 +312,7 @@ function serializeObjectId(
|
|
|
311
312
|
// browser polyfill
|
|
312
313
|
buffer.set(value.id.subarray(0, 12), index);
|
|
313
314
|
} else {
|
|
314
|
-
throw new
|
|
315
|
+
throw new BSONTypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
|
|
315
316
|
}
|
|
316
317
|
|
|
317
318
|
// Adjust index
|
|
@@ -363,7 +364,7 @@ function serializeObject(
|
|
|
363
364
|
path: Document[] = []
|
|
364
365
|
) {
|
|
365
366
|
for (let i = 0; i < path.length; i++) {
|
|
366
|
-
if (path[i] === value) throw new
|
|
367
|
+
if (path[i] === value) throw new BSONError('cyclic dependency detected');
|
|
367
368
|
}
|
|
368
369
|
|
|
369
370
|
// Push value to stack
|
|
@@ -767,7 +768,7 @@ export function serializeInto(
|
|
|
767
768
|
|
|
768
769
|
// Is there an override value
|
|
769
770
|
if (value && value.toBSON) {
|
|
770
|
-
if (typeof value.toBSON !== 'function') throw new
|
|
771
|
+
if (typeof value.toBSON !== 'function') throw new BSONTypeError('toBSON is not a function');
|
|
771
772
|
value = value.toBSON();
|
|
772
773
|
}
|
|
773
774
|
|
|
@@ -776,7 +777,7 @@ export function serializeInto(
|
|
|
776
777
|
} else if (typeof value === 'number') {
|
|
777
778
|
index = serializeNumber(buffer, key, value, index, true);
|
|
778
779
|
} else if (typeof value === 'bigint') {
|
|
779
|
-
throw new
|
|
780
|
+
throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
|
|
780
781
|
} else if (typeof value === 'boolean') {
|
|
781
782
|
index = serializeBoolean(buffer, key, value, index, true);
|
|
782
783
|
} else if (value instanceof Date || isDate(value)) {
|
|
@@ -841,7 +842,7 @@ export function serializeInto(
|
|
|
841
842
|
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
842
843
|
index = serializeMinMax(buffer, key, value, index, true);
|
|
843
844
|
} else if (typeof value['_bsontype'] !== 'undefined') {
|
|
844
|
-
throw new
|
|
845
|
+
throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
|
|
845
846
|
}
|
|
846
847
|
}
|
|
847
848
|
} else if (object instanceof Map || isMap(object)) {
|
|
@@ -884,7 +885,7 @@ export function serializeInto(
|
|
|
884
885
|
} else if (type === 'number') {
|
|
885
886
|
index = serializeNumber(buffer, key, value, index);
|
|
886
887
|
} else if (type === 'bigint' || isBigInt64Array(value) || isBigUInt64Array(value)) {
|
|
887
|
-
throw new
|
|
888
|
+
throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
|
|
888
889
|
} else if (type === 'boolean') {
|
|
889
890
|
index = serializeBoolean(buffer, key, value, index);
|
|
890
891
|
} else if (value instanceof Date || isDate(value)) {
|
|
@@ -942,16 +943,16 @@ export function serializeInto(
|
|
|
942
943
|
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
943
944
|
index = serializeMinMax(buffer, key, value, index);
|
|
944
945
|
} else if (typeof value['_bsontype'] !== 'undefined') {
|
|
945
|
-
throw new
|
|
946
|
+
throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
|
|
946
947
|
}
|
|
947
948
|
}
|
|
948
949
|
} else {
|
|
949
950
|
// Did we provide a custom serialization method
|
|
950
951
|
if (object.toBSON) {
|
|
951
|
-
if (typeof object.toBSON !== 'function') throw new
|
|
952
|
+
if (typeof object.toBSON !== 'function') throw new BSONTypeError('toBSON is not a function');
|
|
952
953
|
object = object.toBSON();
|
|
953
954
|
if (object != null && typeof object !== 'object')
|
|
954
|
-
throw new
|
|
955
|
+
throw new BSONTypeError('toBSON function did not return an object');
|
|
955
956
|
}
|
|
956
957
|
|
|
957
958
|
// Iterate over all the keys
|
|
@@ -959,7 +960,7 @@ export function serializeInto(
|
|
|
959
960
|
let value = object[key];
|
|
960
961
|
// Is there an override value
|
|
961
962
|
if (value && value.toBSON) {
|
|
962
|
-
if (typeof value.toBSON !== 'function') throw new
|
|
963
|
+
if (typeof value.toBSON !== 'function') throw new BSONTypeError('toBSON is not a function');
|
|
963
964
|
value = value.toBSON();
|
|
964
965
|
}
|
|
965
966
|
|
|
@@ -988,7 +989,7 @@ export function serializeInto(
|
|
|
988
989
|
} else if (type === 'number') {
|
|
989
990
|
index = serializeNumber(buffer, key, value, index);
|
|
990
991
|
} else if (type === 'bigint') {
|
|
991
|
-
throw new
|
|
992
|
+
throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
|
|
992
993
|
} else if (type === 'boolean') {
|
|
993
994
|
index = serializeBoolean(buffer, key, value, index);
|
|
994
995
|
} else if (value instanceof Date || isDate(value)) {
|
|
@@ -1048,7 +1049,7 @@ export function serializeInto(
|
|
|
1048
1049
|
} else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
|
|
1049
1050
|
index = serializeMinMax(buffer, key, value, index);
|
|
1050
1051
|
} else if (typeof value['_bsontype'] !== 'undefined') {
|
|
1051
|
-
throw new
|
|
1052
|
+
throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
|
|
1052
1053
|
}
|
|
1053
1054
|
}
|
|
1054
1055
|
}
|
package/src/regexp.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { BSONError, BSONTypeError } from './error';
|
|
1
2
|
import type { EJSONOptions } from './extended_json';
|
|
2
3
|
|
|
3
4
|
function alphabetize(str: string): string {
|
|
@@ -37,6 +38,17 @@ export class BSONRegExp {
|
|
|
37
38
|
this.pattern = pattern;
|
|
38
39
|
this.options = alphabetize(options ?? '');
|
|
39
40
|
|
|
41
|
+
if (this.pattern.indexOf('\x00') !== -1) {
|
|
42
|
+
throw new BSONError(
|
|
43
|
+
`BSON Regex patterns cannot contain null bytes, found: ${JSON.stringify(this.pattern)}`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
if (this.options.indexOf('\x00') !== -1) {
|
|
47
|
+
throw new BSONError(
|
|
48
|
+
`BSON Regex options cannot contain null bytes, found: ${JSON.stringify(this.options)}`
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
40
52
|
// Validate options
|
|
41
53
|
for (let i = 0; i < this.options.length; i++) {
|
|
42
54
|
if (
|
|
@@ -49,7 +61,7 @@ export class BSONRegExp {
|
|
|
49
61
|
this.options[i] === 'u'
|
|
50
62
|
)
|
|
51
63
|
) {
|
|
52
|
-
throw new
|
|
64
|
+
throw new BSONError(`The regular expression option [${this.options[i]}] is not supported`);
|
|
53
65
|
}
|
|
54
66
|
}
|
|
55
67
|
}
|
|
@@ -85,7 +97,7 @@ export class BSONRegExp {
|
|
|
85
97
|
BSONRegExp.parseOptions(doc.$regularExpression.options)
|
|
86
98
|
);
|
|
87
99
|
}
|
|
88
|
-
throw new
|
|
100
|
+
throw new BSONTypeError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
|
|
89
101
|
}
|
|
90
102
|
}
|
|
91
103
|
|
package/src/symbol.ts
CHANGED
|
@@ -25,7 +25,6 @@ export class BSONSymbol {
|
|
|
25
25
|
return this.value;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
/** @internal */
|
|
29
28
|
toString(): string {
|
|
30
29
|
return this.value;
|
|
31
30
|
}
|
|
@@ -35,7 +34,6 @@ export class BSONSymbol {
|
|
|
35
34
|
return `new BSONSymbol("${this.value}")`;
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
/** @internal */
|
|
39
37
|
toJSON(): string {
|
|
40
38
|
return this.value;
|
|
41
39
|
}
|
package/src/uuid.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { ensureBuffer } from './ensure_buffer';
|
|
|
3
3
|
import { Binary } from './binary';
|
|
4
4
|
import { bufferToUuidHexString, uuidHexStringToBuffer, uuidValidateString } from './uuid_utils';
|
|
5
5
|
import { isUint8Array, randomBytes } from './parser/utils';
|
|
6
|
+
import { BSONTypeError } from './error';
|
|
6
7
|
|
|
7
8
|
/** @public */
|
|
8
9
|
export type UUIDExtended = {
|
|
@@ -45,7 +46,7 @@ export class UUID {
|
|
|
45
46
|
} else if (typeof input === 'string') {
|
|
46
47
|
this.id = uuidHexStringToBuffer(input);
|
|
47
48
|
} else {
|
|
48
|
-
throw new
|
|
49
|
+
throw new BSONTypeError(
|
|
49
50
|
'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).'
|
|
50
51
|
);
|
|
51
52
|
}
|
|
@@ -91,15 +92,14 @@ export class UUID {
|
|
|
91
92
|
|
|
92
93
|
/**
|
|
93
94
|
* Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
|
|
94
|
-
* @internal
|
|
95
95
|
*/
|
|
96
96
|
toString(encoding?: string): string {
|
|
97
97
|
return encoding ? this.id.toString(encoding) : this.toHexString();
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
/**
|
|
101
|
-
* Converts the id into its JSON string representation.
|
|
102
|
-
*
|
|
101
|
+
* Converts the id into its JSON string representation.
|
|
102
|
+
* A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
|
103
103
|
*/
|
|
104
104
|
toJSON(): string {
|
|
105
105
|
return this.toHexString();
|
package/src/uuid_utils.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Buffer } from 'buffer';
|
|
2
|
+
import { BSONTypeError } from './error';
|
|
2
3
|
|
|
3
4
|
// Validation regex for v4 uuid (validates with or without dashes)
|
|
4
5
|
const VALIDATION_REGEX =
|
|
@@ -9,7 +10,7 @@ export const uuidValidateString = (str: string): boolean =>
|
|
|
9
10
|
|
|
10
11
|
export const uuidHexStringToBuffer = (hexString: string): Buffer => {
|
|
11
12
|
if (!uuidValidateString(hexString)) {
|
|
12
|
-
throw new
|
|
13
|
+
throw new BSONTypeError(
|
|
13
14
|
'UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".'
|
|
14
15
|
);
|
|
15
16
|
}
|