bson 5.0.0-alpha.0 → 5.0.0-alpha.2
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 +85 -93
- package/bson.d.ts +20 -11
- package/lib/bson.bundle.js +119 -96
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +119 -96
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +120 -96
- package/lib/bson.mjs.map +1 -1
- package/package.json +4 -4
- package/src/binary.ts +12 -12
- package/src/bson.ts +1 -1
- package/src/code.ts +3 -2
- package/src/constants.ts +5 -0
- package/src/db_ref.ts +3 -2
- package/src/decimal128.ts +10 -9
- package/src/double.ts +9 -16
- package/src/error.ts +33 -9
- package/src/extended_json.ts +11 -5
- package/src/int_32.ts +3 -2
- package/src/long.ts +9 -7
- package/src/max_key.ts +4 -2
- package/src/min_key.ts +4 -2
- package/src/objectid.ts +9 -10
- package/src/parser/calculate_size.ts +12 -1
- package/src/parser/deserializer.ts +24 -9
- package/src/parser/serializer.ts +53 -34
- package/src/regexp.ts +5 -4
- package/src/symbol.ts +4 -2
- package/src/timestamp.ts +3 -2
- package/src/uuid_utils.ts +2 -2
package/src/parser/serializer.ts
CHANGED
|
@@ -5,22 +5,14 @@ import * as constants from '../constants';
|
|
|
5
5
|
import type { DBRefLike } from '../db_ref';
|
|
6
6
|
import type { Decimal128 } from '../decimal128';
|
|
7
7
|
import type { Double } from '../double';
|
|
8
|
-
import { BSONError
|
|
8
|
+
import { BSONError } from '../error';
|
|
9
9
|
import type { Int32 } from '../int_32';
|
|
10
10
|
import { Long } from '../long';
|
|
11
11
|
import type { MinKey } from '../min_key';
|
|
12
12
|
import type { ObjectId } from '../objectid';
|
|
13
13
|
import type { BSONRegExp } from '../regexp';
|
|
14
14
|
import { ByteUtils } from '../utils/byte_utils';
|
|
15
|
-
import {
|
|
16
|
-
isAnyArrayBuffer,
|
|
17
|
-
isBigInt64Array,
|
|
18
|
-
isBigUInt64Array,
|
|
19
|
-
isDate,
|
|
20
|
-
isMap,
|
|
21
|
-
isRegExp,
|
|
22
|
-
isUint8Array
|
|
23
|
-
} from './utils';
|
|
15
|
+
import { isAnyArrayBuffer, isDate, isMap, isRegExp, isUint8Array } from './utils';
|
|
24
16
|
|
|
25
17
|
/** @public */
|
|
26
18
|
export interface SerializeOptions {
|
|
@@ -103,6 +95,20 @@ function serializeNumber(buffer: Uint8Array, key: string, value: number, index:
|
|
|
103
95
|
return index;
|
|
104
96
|
}
|
|
105
97
|
|
|
98
|
+
function serializeBigInt(buffer: Uint8Array, key: string, value: bigint, index: number) {
|
|
99
|
+
buffer[index++] = constants.BSON_DATA_LONG;
|
|
100
|
+
// Number of written bytes
|
|
101
|
+
const numberOfWrittenBytes = ByteUtils.encodeUTF8Into(buffer, key, index);
|
|
102
|
+
// Encode the name
|
|
103
|
+
index += numberOfWrittenBytes;
|
|
104
|
+
buffer[index++] = 0;
|
|
105
|
+
NUMBER_SPACE.setBigInt64(0, value, true);
|
|
106
|
+
// Write BigInt value
|
|
107
|
+
buffer.set(EIGHT_BYTE_VIEW_ON_NUMBER, index);
|
|
108
|
+
index += EIGHT_BYTE_VIEW_ON_NUMBER.byteLength;
|
|
109
|
+
return index;
|
|
110
|
+
}
|
|
111
|
+
|
|
106
112
|
function serializeNull(buffer: Uint8Array, key: string, _: unknown, index: number) {
|
|
107
113
|
// Set long type
|
|
108
114
|
buffer[index++] = constants.BSON_DATA_NULL;
|
|
@@ -165,7 +171,7 @@ function serializeRegExp(buffer: Uint8Array, key: string, value: RegExp, index:
|
|
|
165
171
|
index = index + numberOfWrittenBytes;
|
|
166
172
|
buffer[index++] = 0;
|
|
167
173
|
if (value.source && value.source.match(regexp) != null) {
|
|
168
|
-
throw
|
|
174
|
+
throw new BSONError('value ' + value.source + ' must not contain null bytes');
|
|
169
175
|
}
|
|
170
176
|
// Adjust the index
|
|
171
177
|
index = index + ByteUtils.encodeUTF8Into(buffer, value.source, index);
|
|
@@ -194,7 +200,7 @@ function serializeBSONRegExp(buffer: Uint8Array, key: string, value: BSONRegExp,
|
|
|
194
200
|
if (value.pattern.match(regexp) != null) {
|
|
195
201
|
// The BSON spec doesn't allow keys with null bytes because keys are
|
|
196
202
|
// null-terminated.
|
|
197
|
-
throw
|
|
203
|
+
throw new BSONError('pattern ' + value.pattern + ' must not contain null bytes');
|
|
198
204
|
}
|
|
199
205
|
|
|
200
206
|
// Adjust the index
|
|
@@ -241,7 +247,7 @@ function serializeObjectId(buffer: Uint8Array, key: string, value: ObjectId, ind
|
|
|
241
247
|
if (isUint8Array(value.id)) {
|
|
242
248
|
buffer.set(value.id.subarray(0, 12), index);
|
|
243
249
|
} else {
|
|
244
|
-
throw new
|
|
250
|
+
throw new BSONError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
|
|
245
251
|
}
|
|
246
252
|
|
|
247
253
|
// Adjust index
|
|
@@ -675,7 +681,7 @@ export function serializeInto(
|
|
|
675
681
|
} else if (typeof value === 'number') {
|
|
676
682
|
index = serializeNumber(buffer, key, value, index);
|
|
677
683
|
} else if (typeof value === 'bigint') {
|
|
678
|
-
|
|
684
|
+
index = serializeBigInt(buffer, key, value, index);
|
|
679
685
|
} else if (typeof value === 'boolean') {
|
|
680
686
|
index = serializeBoolean(buffer, key, value, index);
|
|
681
687
|
} else if (value instanceof Date || isDate(value)) {
|
|
@@ -700,7 +706,10 @@ export function serializeInto(
|
|
|
700
706
|
ignoreUndefined,
|
|
701
707
|
path
|
|
702
708
|
);
|
|
703
|
-
} else if (
|
|
709
|
+
} else if (
|
|
710
|
+
typeof value === 'object' &&
|
|
711
|
+
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
|
|
712
|
+
) {
|
|
704
713
|
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
|
|
705
714
|
} else if (value._bsontype === 'ObjectId') {
|
|
706
715
|
index = serializeObjectId(buffer, key, value, index);
|
|
@@ -726,7 +735,7 @@ export function serializeInto(
|
|
|
726
735
|
);
|
|
727
736
|
} else if (value._bsontype === 'Binary') {
|
|
728
737
|
index = serializeBinary(buffer, key, value, index);
|
|
729
|
-
} else if (value._bsontype === '
|
|
738
|
+
} else if (value._bsontype === 'BSONSymbol') {
|
|
730
739
|
index = serializeSymbol(buffer, key, value, index);
|
|
731
740
|
} else if (value._bsontype === 'DBRef') {
|
|
732
741
|
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
|
|
@@ -737,7 +746,7 @@ export function serializeInto(
|
|
|
737
746
|
} else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
|
|
738
747
|
index = serializeMinMax(buffer, key, value, index);
|
|
739
748
|
} else if (typeof value._bsontype !== 'undefined') {
|
|
740
|
-
throw new
|
|
749
|
+
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
741
750
|
}
|
|
742
751
|
}
|
|
743
752
|
} else if (object instanceof Map || isMap(object)) {
|
|
@@ -753,7 +762,11 @@ export function serializeInto(
|
|
|
753
762
|
|
|
754
763
|
// Get the entry values
|
|
755
764
|
const key = entry.value[0];
|
|
756
|
-
|
|
765
|
+
let value = entry.value[1];
|
|
766
|
+
|
|
767
|
+
if (typeof value?.toBSON === 'function') {
|
|
768
|
+
value = value.toBSON();
|
|
769
|
+
}
|
|
757
770
|
|
|
758
771
|
// Check the type of the value
|
|
759
772
|
const type = typeof value;
|
|
@@ -763,14 +776,14 @@ export function serializeInto(
|
|
|
763
776
|
if (key.match(regexp) != null) {
|
|
764
777
|
// The BSON spec doesn't allow keys with null bytes because keys are
|
|
765
778
|
// null-terminated.
|
|
766
|
-
throw
|
|
779
|
+
throw new BSONError('key ' + key + ' must not contain null bytes');
|
|
767
780
|
}
|
|
768
781
|
|
|
769
782
|
if (checkKeys) {
|
|
770
783
|
if ('$' === key[0]) {
|
|
771
|
-
throw
|
|
784
|
+
throw new BSONError('key ' + key + " must not start with '$'");
|
|
772
785
|
} else if (~key.indexOf('.')) {
|
|
773
|
-
throw
|
|
786
|
+
throw new BSONError('key ' + key + " must not contain '.'");
|
|
774
787
|
}
|
|
775
788
|
}
|
|
776
789
|
}
|
|
@@ -779,8 +792,8 @@ export function serializeInto(
|
|
|
779
792
|
index = serializeString(buffer, key, value, index);
|
|
780
793
|
} else if (type === 'number') {
|
|
781
794
|
index = serializeNumber(buffer, key, value, index);
|
|
782
|
-
} else if (type === 'bigint'
|
|
783
|
-
|
|
795
|
+
} else if (type === 'bigint') {
|
|
796
|
+
index = serializeBigInt(buffer, key, value, index);
|
|
784
797
|
} else if (type === 'boolean') {
|
|
785
798
|
index = serializeBoolean(buffer, key, value, index);
|
|
786
799
|
} else if (value instanceof Date || isDate(value)) {
|
|
@@ -803,7 +816,10 @@ export function serializeInto(
|
|
|
803
816
|
ignoreUndefined,
|
|
804
817
|
path
|
|
805
818
|
);
|
|
806
|
-
} else if (
|
|
819
|
+
} else if (
|
|
820
|
+
typeof value === 'object' &&
|
|
821
|
+
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
|
|
822
|
+
) {
|
|
807
823
|
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
|
|
808
824
|
} else if (value._bsontype === 'ObjectId') {
|
|
809
825
|
index = serializeObjectId(buffer, key, value, index);
|
|
@@ -829,7 +845,7 @@ export function serializeInto(
|
|
|
829
845
|
index = serializeFunction(buffer, key, value, index);
|
|
830
846
|
} else if (value._bsontype === 'Binary') {
|
|
831
847
|
index = serializeBinary(buffer, key, value, index);
|
|
832
|
-
} else if (value._bsontype === '
|
|
848
|
+
} else if (value._bsontype === 'BSONSymbol') {
|
|
833
849
|
index = serializeSymbol(buffer, key, value, index);
|
|
834
850
|
} else if (value._bsontype === 'DBRef') {
|
|
835
851
|
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
|
|
@@ -840,7 +856,7 @@ export function serializeInto(
|
|
|
840
856
|
} else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
|
|
841
857
|
index = serializeMinMax(buffer, key, value, index);
|
|
842
858
|
} else if (typeof value._bsontype !== 'undefined') {
|
|
843
|
-
throw new
|
|
859
|
+
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
844
860
|
}
|
|
845
861
|
}
|
|
846
862
|
} else {
|
|
@@ -848,7 +864,7 @@ export function serializeInto(
|
|
|
848
864
|
// Provided a custom serialization method
|
|
849
865
|
object = object.toBSON();
|
|
850
866
|
if (object != null && typeof object !== 'object') {
|
|
851
|
-
throw new
|
|
867
|
+
throw new BSONError('toBSON function did not return an object');
|
|
852
868
|
}
|
|
853
869
|
}
|
|
854
870
|
|
|
@@ -868,14 +884,14 @@ export function serializeInto(
|
|
|
868
884
|
if (key.match(regexp) != null) {
|
|
869
885
|
// The BSON spec doesn't allow keys with null bytes because keys are
|
|
870
886
|
// null-terminated.
|
|
871
|
-
throw
|
|
887
|
+
throw new BSONError('key ' + key + ' must not contain null bytes');
|
|
872
888
|
}
|
|
873
889
|
|
|
874
890
|
if (checkKeys) {
|
|
875
891
|
if ('$' === key[0]) {
|
|
876
|
-
throw
|
|
892
|
+
throw new BSONError('key ' + key + " must not start with '$'");
|
|
877
893
|
} else if (~key.indexOf('.')) {
|
|
878
|
-
throw
|
|
894
|
+
throw new BSONError('key ' + key + " must not contain '.'");
|
|
879
895
|
}
|
|
880
896
|
}
|
|
881
897
|
}
|
|
@@ -885,7 +901,7 @@ export function serializeInto(
|
|
|
885
901
|
} else if (type === 'number') {
|
|
886
902
|
index = serializeNumber(buffer, key, value, index);
|
|
887
903
|
} else if (type === 'bigint') {
|
|
888
|
-
|
|
904
|
+
index = serializeBigInt(buffer, key, value, index);
|
|
889
905
|
} else if (type === 'boolean') {
|
|
890
906
|
index = serializeBoolean(buffer, key, value, index);
|
|
891
907
|
} else if (value instanceof Date || isDate(value)) {
|
|
@@ -910,7 +926,10 @@ export function serializeInto(
|
|
|
910
926
|
ignoreUndefined,
|
|
911
927
|
path
|
|
912
928
|
);
|
|
913
|
-
} else if (
|
|
929
|
+
} else if (
|
|
930
|
+
typeof value === 'object' &&
|
|
931
|
+
value[Symbol.for('@@mdb.bson.version')] !== constants.BSON_MAJOR_VERSION
|
|
932
|
+
) {
|
|
914
933
|
throw new BSONError('Unsupported BSON version, bson types must be from bson 5.0 or later');
|
|
915
934
|
} else if (value._bsontype === 'ObjectId') {
|
|
916
935
|
index = serializeObjectId(buffer, key, value, index);
|
|
@@ -936,7 +955,7 @@ export function serializeInto(
|
|
|
936
955
|
index = serializeFunction(buffer, key, value, index);
|
|
937
956
|
} else if (value._bsontype === 'Binary') {
|
|
938
957
|
index = serializeBinary(buffer, key, value, index);
|
|
939
|
-
} else if (value._bsontype === '
|
|
958
|
+
} else if (value._bsontype === 'BSONSymbol') {
|
|
940
959
|
index = serializeSymbol(buffer, key, value, index);
|
|
941
960
|
} else if (value._bsontype === 'DBRef') {
|
|
942
961
|
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions, path);
|
|
@@ -947,7 +966,7 @@ export function serializeInto(
|
|
|
947
966
|
} else if (value._bsontype === 'MinKey' || value._bsontype === 'MaxKey') {
|
|
948
967
|
index = serializeMinMax(buffer, key, value, index);
|
|
949
968
|
} else if (typeof value._bsontype !== 'undefined') {
|
|
950
|
-
throw new
|
|
969
|
+
throw new BSONError(`Unrecognized or invalid _bsontype: ${String(value._bsontype)}`);
|
|
951
970
|
}
|
|
952
971
|
}
|
|
953
972
|
}
|
package/src/regexp.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BSON_MAJOR_VERSION } from './constants';
|
|
2
|
+
import { BSONError } from './error';
|
|
2
3
|
import type { EJSONOptions } from './extended_json';
|
|
3
4
|
|
|
4
5
|
function alphabetize(str: string): string {
|
|
@@ -29,8 +30,8 @@ export class BSONRegExp {
|
|
|
29
30
|
return 'BSONRegExp';
|
|
30
31
|
}
|
|
31
32
|
/** @internal */
|
|
32
|
-
get [Symbol.for('@@mdb.bson.version')]():
|
|
33
|
-
return
|
|
33
|
+
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
34
|
+
return BSON_MAJOR_VERSION;
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
pattern!: string;
|
|
@@ -102,7 +103,7 @@ export class BSONRegExp {
|
|
|
102
103
|
BSONRegExp.parseOptions(doc.$regularExpression.options)
|
|
103
104
|
);
|
|
104
105
|
}
|
|
105
|
-
throw new
|
|
106
|
+
throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
|
|
106
107
|
}
|
|
107
108
|
|
|
108
109
|
/** @internal */
|
package/src/symbol.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { BSON_MAJOR_VERSION } from './constants';
|
|
2
|
+
|
|
1
3
|
/** @public */
|
|
2
4
|
export interface BSONSymbolExtended {
|
|
3
5
|
$symbol: string;
|
|
@@ -13,8 +15,8 @@ export class BSONSymbol {
|
|
|
13
15
|
return 'BSONSymbol';
|
|
14
16
|
}
|
|
15
17
|
/** @internal */
|
|
16
|
-
get [Symbol.for('@@mdb.bson.version')]():
|
|
17
|
-
return
|
|
18
|
+
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
19
|
+
return BSON_MAJOR_VERSION;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
22
|
value!: string;
|
package/src/timestamp.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { BSON_MAJOR_VERSION } from './constants';
|
|
1
2
|
import { BSONError } from './error';
|
|
2
3
|
import type { Int32 } from './int_32';
|
|
3
4
|
import { Long } from './long';
|
|
@@ -33,8 +34,8 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
33
34
|
return 'Timestamp';
|
|
34
35
|
}
|
|
35
36
|
/** @internal */
|
|
36
|
-
get [Symbol.for('@@mdb.bson.version')]():
|
|
37
|
-
return
|
|
37
|
+
get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
|
|
38
|
+
return BSON_MAJOR_VERSION;
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
static readonly MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
|
package/src/uuid_utils.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BSONError } from './error';
|
|
2
2
|
import { ByteUtils } from './utils/byte_utils';
|
|
3
3
|
|
|
4
4
|
// Validation regex for v4 uuid (validates with or without dashes)
|
|
@@ -10,7 +10,7 @@ export const uuidValidateString = (str: string): boolean =>
|
|
|
10
10
|
|
|
11
11
|
export const uuidHexStringToBuffer = (hexString: string): Uint8Array => {
|
|
12
12
|
if (!uuidValidateString(hexString)) {
|
|
13
|
-
throw new
|
|
13
|
+
throw new BSONError(
|
|
14
14
|
'UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".'
|
|
15
15
|
);
|
|
16
16
|
}
|