bson 6.2.0 → 6.4.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/bson.d.ts +41 -7
- package/lib/bson.bundle.js +384 -315
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +384 -315
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +384 -315
- package/lib/bson.mjs.map +1 -1
- package/lib/bson.rn.cjs +384 -315
- package/lib/bson.rn.cjs.map +1 -1
- package/package.json +29 -26
- package/src/binary.ts +2 -2
- package/src/bson.ts +3 -6
- package/src/db_ref.ts +0 -1
- package/src/decimal128.ts +1 -1
- package/src/error.ts +3 -3
- package/src/objectid.ts +76 -18
- package/src/parser/deserializer.ts +85 -175
- package/src/parser/serializer.ts +41 -104
- package/src/utils/byte_utils.ts +4 -10
- package/src/utils/latin.ts +104 -0
- package/src/utils/node_byte_utils.ts +30 -5
- package/src/utils/number_utils.ts +135 -0
- package/src/utils/web_byte_utils.ts +22 -9
package/package.json
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"vendor"
|
|
15
15
|
],
|
|
16
16
|
"types": "bson.d.ts",
|
|
17
|
-
"version": "6.
|
|
17
|
+
"version": "6.4.0",
|
|
18
18
|
"author": {
|
|
19
19
|
"name": "The MongoDB NodeJS Team",
|
|
20
20
|
"email": "dbx-node@mongodb.com"
|
|
@@ -27,41 +27,42 @@
|
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@istanbuljs/nyc-config-typescript": "^1.0.2",
|
|
30
|
-
"@microsoft/api-extractor": "^7.
|
|
31
|
-
"@octokit/core": "^
|
|
32
|
-
"@rollup/plugin-node-resolve": "^15.
|
|
33
|
-
"@rollup/plugin-typescript": "^11.1.
|
|
34
|
-
"@types/chai": "^4.3.
|
|
35
|
-
"@types/mocha": "^10.0.
|
|
36
|
-
"@types/node": "^
|
|
37
|
-
"@types/sinon": "^
|
|
38
|
-
"@types/sinon-chai": "^3.2.
|
|
39
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
40
|
-
"@typescript-eslint/parser": "^
|
|
30
|
+
"@microsoft/api-extractor": "^7.40.5",
|
|
31
|
+
"@octokit/core": "^5.1.0",
|
|
32
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
33
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
34
|
+
"@types/chai": "^4.3.11",
|
|
35
|
+
"@types/mocha": "^10.0.6",
|
|
36
|
+
"@types/node": "^20.11.19",
|
|
37
|
+
"@types/sinon": "^17.0.3",
|
|
38
|
+
"@types/sinon-chai": "^3.2.12",
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^7.0.2",
|
|
40
|
+
"@typescript-eslint/parser": "^7.0.2",
|
|
41
41
|
"benchmark": "^2.1.4",
|
|
42
|
-
"chai": "^4.3.
|
|
42
|
+
"chai": "^4.3.10",
|
|
43
43
|
"chalk": "^5.3.0",
|
|
44
|
-
"
|
|
45
|
-
"eslint
|
|
44
|
+
"dbx-js-tools": "github:mongodb-js/dbx-js-tools",
|
|
45
|
+
"eslint": "^8.56.0",
|
|
46
|
+
"eslint-config-prettier": "^9.1.0",
|
|
46
47
|
"eslint-plugin-no-bigint-usage": "file:etc/eslint/no-bigint-usage",
|
|
47
|
-
"eslint-plugin-prettier": "^
|
|
48
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
48
49
|
"eslint-plugin-tsdoc": "^0.2.17",
|
|
49
|
-
"magic-string": "^0.30.
|
|
50
|
-
"mocha": "10.
|
|
50
|
+
"magic-string": "^0.30.7",
|
|
51
|
+
"mocha": "10.3.0",
|
|
51
52
|
"node-fetch": "^3.3.2",
|
|
52
53
|
"nyc": "^15.1.0",
|
|
53
|
-
"prettier": "^2.
|
|
54
|
-
"rollup": "^
|
|
55
|
-
"sinon": "^
|
|
54
|
+
"prettier": "^3.2.5",
|
|
55
|
+
"rollup": "^4.12.0",
|
|
56
|
+
"sinon": "^17.0.1",
|
|
56
57
|
"sinon-chai": "^3.7.0",
|
|
57
58
|
"source-map-support": "^0.5.21",
|
|
58
59
|
"standard-version": "^9.5.0",
|
|
59
|
-
"tar": "^6.
|
|
60
|
-
"ts-node": "^10.9.
|
|
61
|
-
"tsd": "^0.
|
|
60
|
+
"tar": "^6.2.0",
|
|
61
|
+
"ts-node": "^10.9.2",
|
|
62
|
+
"tsd": "^0.30.5",
|
|
62
63
|
"typescript": "^5.0.4",
|
|
63
64
|
"typescript-cached-transpile": "0.0.6",
|
|
64
|
-
"uuid": "^9.0.
|
|
65
|
+
"uuid": "^9.0.1"
|
|
65
66
|
},
|
|
66
67
|
"tsd": {
|
|
67
68
|
"directory": "test/types",
|
|
@@ -103,7 +104,9 @@
|
|
|
103
104
|
"check:tsd": "npm run build:dts && tsd",
|
|
104
105
|
"check:web": "WEB=true mocha test/node",
|
|
105
106
|
"check:web-no-bigint": "WEB=true NO_BIGINT=true mocha test/node",
|
|
106
|
-
"check:bench": "
|
|
107
|
+
"check:granular-bench": "npm run build:bench && node ./test/bench/etc/run_granular_benchmarks.js",
|
|
108
|
+
"check:spec-bench": "npm run build:bench && node ./test/bench/lib/spec/bsonBench.js",
|
|
109
|
+
"build:bench": "cd test/bench && npx tsc",
|
|
107
110
|
"build:ts": "node ./node_modules/typescript/bin/tsc",
|
|
108
111
|
"build:dts": "npm run build:ts && api-extractor run --typescript-compiler-folder node_modules/typescript --local && node etc/clean_definition_files.cjs",
|
|
109
112
|
"build:bundle": "rollup -c rollup.config.mjs",
|
package/src/binary.ts
CHANGED
|
@@ -191,8 +191,8 @@ export class Binary extends BSONValue {
|
|
|
191
191
|
if (encoding === 'hex') return ByteUtils.toHex(this.buffer);
|
|
192
192
|
if (encoding === 'base64') return ByteUtils.toBase64(this.buffer);
|
|
193
193
|
if (encoding === 'utf8' || encoding === 'utf-8')
|
|
194
|
-
return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
|
|
195
|
-
return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
|
|
194
|
+
return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
|
|
195
|
+
return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
/** @internal */
|
package/src/bson.ts
CHANGED
|
@@ -16,6 +16,7 @@ import { BSONRegExp } from './regexp';
|
|
|
16
16
|
import { BSONSymbol } from './symbol';
|
|
17
17
|
import { Timestamp } from './timestamp';
|
|
18
18
|
import { ByteUtils } from './utils/byte_utils';
|
|
19
|
+
import { NumberUtils } from './utils/number_utils';
|
|
19
20
|
export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
|
|
20
21
|
export type { CodeExtended } from './code';
|
|
21
22
|
export type { DBRefLike } from './db_ref';
|
|
@@ -115,7 +116,7 @@ export function serialize(object: Document, options: SerializeOptions = {}): Uin
|
|
|
115
116
|
);
|
|
116
117
|
|
|
117
118
|
// Create the final buffer
|
|
118
|
-
const finishedBuffer = ByteUtils.
|
|
119
|
+
const finishedBuffer = ByteUtils.allocateUnsafe(serializationIndex);
|
|
119
120
|
|
|
120
121
|
// Copy into the finished buffer
|
|
121
122
|
finishedBuffer.set(buffer.subarray(0, serializationIndex), 0);
|
|
@@ -232,11 +233,7 @@ export function deserializeStream(
|
|
|
232
233
|
// Loop over all documents
|
|
233
234
|
for (let i = 0; i < numberOfDocuments; i++) {
|
|
234
235
|
// Find size of the document
|
|
235
|
-
const size =
|
|
236
|
-
bufferData[index] |
|
|
237
|
-
(bufferData[index + 1] << 8) |
|
|
238
|
-
(bufferData[index + 2] << 16) |
|
|
239
|
-
(bufferData[index + 3] << 24);
|
|
236
|
+
const size = NumberUtils.getInt32LE(bufferData, index);
|
|
240
237
|
// Update options with index
|
|
241
238
|
internalOptions.index = index;
|
|
242
239
|
// Parse the document at this point
|
package/src/db_ref.ts
CHANGED
package/src/decimal128.ts
CHANGED
package/src/error.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { BSON_MAJOR_VERSION } from './constants';
|
|
|
4
4
|
* @public
|
|
5
5
|
* @category Error
|
|
6
6
|
*
|
|
7
|
-
* `BSONError` objects are thrown when BSON
|
|
7
|
+
* `BSONError` objects are thrown when BSON encounters an error.
|
|
8
8
|
*
|
|
9
9
|
* This is the parent class for all the other errors thrown by this library.
|
|
10
10
|
*/
|
|
@@ -23,8 +23,8 @@ export class BSONError extends Error {
|
|
|
23
23
|
return 'BSONError';
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
constructor(message: string) {
|
|
27
|
-
super(message);
|
|
26
|
+
constructor(message: string, options?: { cause?: unknown }) {
|
|
27
|
+
super(message, options);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
/**
|
package/src/objectid.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { BSONValue } from './bson_value';
|
|
2
2
|
import { BSONError } from './error';
|
|
3
3
|
import { type InspectFn, defaultInspect } from './parser/utils';
|
|
4
|
-
import {
|
|
4
|
+
import { ByteUtils } from './utils/byte_utils';
|
|
5
|
+
import { NumberUtils } from './utils/number_utils';
|
|
5
6
|
|
|
6
7
|
// Regular expression that checks for hex value
|
|
7
8
|
const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
|
|
@@ -21,8 +22,6 @@ export interface ObjectIdExtended {
|
|
|
21
22
|
$oid: string;
|
|
22
23
|
}
|
|
23
24
|
|
|
24
|
-
const kId = Symbol('id');
|
|
25
|
-
|
|
26
25
|
/**
|
|
27
26
|
* A class representation of the BSON ObjectId type.
|
|
28
27
|
* @public
|
|
@@ -39,14 +38,53 @@ export class ObjectId extends BSONValue {
|
|
|
39
38
|
static cacheHexString: boolean;
|
|
40
39
|
|
|
41
40
|
/** ObjectId Bytes @internal */
|
|
42
|
-
private
|
|
41
|
+
private buffer!: Uint8Array;
|
|
43
42
|
/** ObjectId hexString cache @internal */
|
|
44
43
|
private __id?: string;
|
|
45
44
|
|
|
46
45
|
/**
|
|
47
|
-
* Create
|
|
46
|
+
* Create ObjectId from a number.
|
|
47
|
+
*
|
|
48
|
+
* @param inputId - A number.
|
|
49
|
+
* @deprecated Instead, use `static createFromTime()` to set a numeric value for the new ObjectId.
|
|
50
|
+
*/
|
|
51
|
+
constructor(inputId: number);
|
|
52
|
+
/**
|
|
53
|
+
* Create ObjectId from a 24 character hex string.
|
|
54
|
+
*
|
|
55
|
+
* @param inputId - A 24 character hex string.
|
|
56
|
+
*/
|
|
57
|
+
constructor(inputId: string);
|
|
58
|
+
/**
|
|
59
|
+
* Create ObjectId from the BSON ObjectId type.
|
|
60
|
+
*
|
|
61
|
+
* @param inputId - The BSON ObjectId type.
|
|
62
|
+
*/
|
|
63
|
+
constructor(inputId: ObjectId);
|
|
64
|
+
/**
|
|
65
|
+
* Create ObjectId from the object type that has the toHexString method.
|
|
66
|
+
*
|
|
67
|
+
* @param inputId - The ObjectIdLike type.
|
|
68
|
+
*/
|
|
69
|
+
constructor(inputId: ObjectIdLike);
|
|
70
|
+
/**
|
|
71
|
+
* Create ObjectId from a 12 byte binary Buffer.
|
|
72
|
+
*
|
|
73
|
+
* @param inputId - A 12 byte binary Buffer.
|
|
74
|
+
*/
|
|
75
|
+
constructor(inputId: Uint8Array);
|
|
76
|
+
/** To generate a new ObjectId, use ObjectId() with no argument. */
|
|
77
|
+
constructor();
|
|
78
|
+
/**
|
|
79
|
+
* Implementation overload.
|
|
80
|
+
*
|
|
81
|
+
* @param inputId - All input types that are used in the constructor implementation.
|
|
82
|
+
*/
|
|
83
|
+
constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array);
|
|
84
|
+
/**
|
|
85
|
+
* Create a new ObjectId.
|
|
48
86
|
*
|
|
49
|
-
* @param inputId -
|
|
87
|
+
* @param inputId - An input value to create a new ObjectId from.
|
|
50
88
|
*/
|
|
51
89
|
constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array) {
|
|
52
90
|
super();
|
|
@@ -65,17 +103,17 @@ export class ObjectId extends BSONValue {
|
|
|
65
103
|
workingId = inputId;
|
|
66
104
|
}
|
|
67
105
|
|
|
68
|
-
//
|
|
106
|
+
// The following cases use workingId to construct an ObjectId
|
|
69
107
|
if (workingId == null || typeof workingId === 'number') {
|
|
70
108
|
// The most common use case (blank id, new objectId instance)
|
|
71
109
|
// Generate a new id
|
|
72
|
-
this
|
|
110
|
+
this.buffer = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
|
|
73
111
|
} else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
|
|
74
112
|
// If intstanceof matches we can escape calling ensure buffer in Node.js environments
|
|
75
|
-
this
|
|
113
|
+
this.buffer = ByteUtils.toLocalBufferType(workingId);
|
|
76
114
|
} else if (typeof workingId === 'string') {
|
|
77
115
|
if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
|
|
78
|
-
this
|
|
116
|
+
this.buffer = ByteUtils.fromHex(workingId);
|
|
79
117
|
} else {
|
|
80
118
|
throw new BSONError(
|
|
81
119
|
'input must be a 24 character hex string, 12 byte Uint8Array, or an integer'
|
|
@@ -95,11 +133,11 @@ export class ObjectId extends BSONValue {
|
|
|
95
133
|
* @readonly
|
|
96
134
|
*/
|
|
97
135
|
get id(): Uint8Array {
|
|
98
|
-
return this
|
|
136
|
+
return this.buffer;
|
|
99
137
|
}
|
|
100
138
|
|
|
101
139
|
set id(value: Uint8Array) {
|
|
102
|
-
this
|
|
140
|
+
this.buffer = value;
|
|
103
141
|
if (ObjectId.cacheHexString) {
|
|
104
142
|
this.__id = ByteUtils.toHex(value);
|
|
105
143
|
}
|
|
@@ -139,10 +177,10 @@ export class ObjectId extends BSONValue {
|
|
|
139
177
|
}
|
|
140
178
|
|
|
141
179
|
const inc = ObjectId.getInc();
|
|
142
|
-
const buffer = ByteUtils.
|
|
180
|
+
const buffer = ByteUtils.allocateUnsafe(12);
|
|
143
181
|
|
|
144
182
|
// 4-byte timestamp
|
|
145
|
-
|
|
183
|
+
NumberUtils.setInt32BE(buffer, 0, time);
|
|
146
184
|
|
|
147
185
|
// set PROCESS_UNIQUE if yet not initialized
|
|
148
186
|
if (PROCESS_UNIQUE === null) {
|
|
@@ -201,7 +239,9 @@ export class ObjectId extends BSONValue {
|
|
|
201
239
|
}
|
|
202
240
|
|
|
203
241
|
if (ObjectId.is(otherId)) {
|
|
204
|
-
return
|
|
242
|
+
return (
|
|
243
|
+
this.buffer[11] === otherId.buffer[11] && ByteUtils.equals(this.buffer, otherId.buffer)
|
|
244
|
+
);
|
|
205
245
|
}
|
|
206
246
|
|
|
207
247
|
if (typeof otherId === 'string') {
|
|
@@ -220,7 +260,7 @@ export class ObjectId extends BSONValue {
|
|
|
220
260
|
/** Returns the generation date (accurate up to the second) that this ID was generated. */
|
|
221
261
|
getTimestamp(): Date {
|
|
222
262
|
const timestamp = new Date();
|
|
223
|
-
const time =
|
|
263
|
+
const time = NumberUtils.getUint32BE(this.buffer, 0);
|
|
224
264
|
timestamp.setTime(Math.floor(time) * 1000);
|
|
225
265
|
return timestamp;
|
|
226
266
|
}
|
|
@@ -230,15 +270,33 @@ export class ObjectId extends BSONValue {
|
|
|
230
270
|
return new ObjectId();
|
|
231
271
|
}
|
|
232
272
|
|
|
273
|
+
/** @internal */
|
|
274
|
+
serializeInto(uint8array: Uint8Array, index: number): 12 {
|
|
275
|
+
uint8array[index] = this.buffer[0];
|
|
276
|
+
uint8array[index + 1] = this.buffer[1];
|
|
277
|
+
uint8array[index + 2] = this.buffer[2];
|
|
278
|
+
uint8array[index + 3] = this.buffer[3];
|
|
279
|
+
uint8array[index + 4] = this.buffer[4];
|
|
280
|
+
uint8array[index + 5] = this.buffer[5];
|
|
281
|
+
uint8array[index + 6] = this.buffer[6];
|
|
282
|
+
uint8array[index + 7] = this.buffer[7];
|
|
283
|
+
uint8array[index + 8] = this.buffer[8];
|
|
284
|
+
uint8array[index + 9] = this.buffer[9];
|
|
285
|
+
uint8array[index + 10] = this.buffer[10];
|
|
286
|
+
uint8array[index + 11] = this.buffer[11];
|
|
287
|
+
return 12;
|
|
288
|
+
}
|
|
289
|
+
|
|
233
290
|
/**
|
|
234
291
|
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
|
|
235
292
|
*
|
|
236
293
|
* @param time - an integer number representing a number of seconds.
|
|
237
294
|
*/
|
|
238
295
|
static createFromTime(time: number): ObjectId {
|
|
239
|
-
const buffer = ByteUtils.
|
|
296
|
+
const buffer = ByteUtils.allocate(12);
|
|
297
|
+
for (let i = 11; i >= 4; i--) buffer[i] = 0;
|
|
240
298
|
// Encode time into first 4 bytes
|
|
241
|
-
|
|
299
|
+
NumberUtils.setInt32BE(buffer, 0, time);
|
|
242
300
|
// Return the new objectId
|
|
243
301
|
return new ObjectId(buffer);
|
|
244
302
|
}
|