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.
Files changed (70) hide show
  1. package/README.md +51 -43
  2. package/bson.d.ts +10 -23
  3. package/lib/bson.bundle.js +44 -71
  4. package/lib/bson.bundle.js.map +1 -1
  5. package/lib/bson.cjs +44 -71
  6. package/lib/bson.cjs.map +1 -1
  7. package/lib/bson.mjs +44 -71
  8. package/lib/bson.mjs.map +1 -1
  9. package/lib/bson.rn.cjs +4081 -0
  10. package/lib/bson.rn.cjs.map +1 -0
  11. package/package.json +26 -24
  12. package/src/binary.ts +19 -53
  13. package/src/bson.ts +2 -2
  14. package/src/constants.ts +1 -1
  15. package/src/objectid.ts +20 -37
  16. package/src/parser/deserializer.ts +8 -8
  17. package/src/timestamp.ts +7 -5
  18. package/src/utils/byte_utils.ts +2 -2
  19. package/src/utils/node_byte_utils.ts +3 -3
  20. package/src/utils/web_byte_utils.ts +2 -2
  21. package/vendor/base64/LICENSE-MIT.txt +20 -0
  22. package/vendor/base64/README.md +112 -0
  23. package/vendor/base64/base64.js +157 -0
  24. package/vendor/base64/package.json +43 -0
  25. package/vendor/text-encoding/LICENSE.md +237 -0
  26. package/vendor/text-encoding/README.md +111 -0
  27. package/vendor/text-encoding/index.js +9 -0
  28. package/vendor/text-encoding/lib/encoding-indexes.js +47 -0
  29. package/vendor/text-encoding/lib/encoding.js +3301 -0
  30. package/vendor/text-encoding/package.json +37 -0
  31. package/lib/binary.d.ts +0 -182
  32. package/lib/binary.d.ts.map +0 -1
  33. package/lib/bson.d.ts +0 -97
  34. package/lib/bson.d.ts.map +0 -1
  35. package/lib/bson_value.d.ts +0 -10
  36. package/lib/bson_value.d.ts.map +0 -1
  37. package/lib/code.d.ts +0 -32
  38. package/lib/code.d.ts.map +0 -1
  39. package/lib/constants.d.ts +0 -107
  40. package/lib/constants.d.ts.map +0 -1
  41. package/lib/db_ref.d.ts +0 -40
  42. package/lib/db_ref.d.ts.map +0 -1
  43. package/lib/decimal128.d.ts +0 -34
  44. package/lib/decimal128.d.ts.map +0 -1
  45. package/lib/double.d.ts +0 -35
  46. package/lib/double.d.ts.map +0 -1
  47. package/lib/error.d.ts +0 -50
  48. package/lib/error.d.ts.map +0 -1
  49. package/lib/extended_json.d.ts +0 -82
  50. package/lib/extended_json.d.ts.map +0 -1
  51. package/lib/index.d.ts +0 -4
  52. package/lib/index.d.ts.map +0 -1
  53. package/lib/int_32.d.ts +0 -35
  54. package/lib/int_32.d.ts.map +0 -1
  55. package/lib/long.d.ts +0 -323
  56. package/lib/long.d.ts.map +0 -1
  57. package/lib/max_key.d.ts +0 -19
  58. package/lib/max_key.d.ts.map +0 -1
  59. package/lib/min_key.d.ts +0 -19
  60. package/lib/min_key.d.ts.map +0 -1
  61. package/lib/objectid.d.ts +0 -96
  62. package/lib/objectid.d.ts.map +0 -1
  63. package/lib/regexp.d.ts +0 -36
  64. package/lib/regexp.d.ts.map +0 -1
  65. package/lib/symbol.d.ts +0 -28
  66. package/lib/symbol.d.ts.map +0 -1
  67. package/lib/timestamp.d.ts +0 -66
  68. package/lib/timestamp.d.ts.map +0 -1
  69. package/lib/validate_utf8.d.ts +0 -10
  70. package/lib/validate_utf8.d.ts.map +0 -1
package/README.md CHANGED
@@ -4,6 +4,7 @@ BSON is short for "Binary JSON," and is the binary-encoded serialization of JSON
4
4
  You can learn more about it in [the specification](http://bsonspec.org).
5
5
 
6
6
  ### Table of Contents
7
+
7
8
  - [Usage](#usage)
8
9
  - [Bugs/Feature Requests](#bugs--feature-requests)
9
10
  - [Installation](#installation)
@@ -18,7 +19,7 @@ Think you've found a bug? Want to see a new feature in `bson`? Please open a cas
18
19
  2. Navigate to the NODE project: [jira.mongodb.org/browse/NODE](https://jira.mongodb.org/browse/NODE)
19
20
  3. Click **Create Issue** - Please provide as much information as possible about the issue and how to reproduce it.
20
21
 
21
- Bug reports in JIRA for all driver projects (i.e. NODE, PYTHON, CSHARP, JAVA) and the Core Server (i.e. SERVER) project are **public**.
22
+ Bug reports in JIRA for the NODE driver project are **public**.
22
23
 
23
24
  ## Usage
24
25
 
@@ -67,9 +68,20 @@ If you are working directly in the browser without a bundler please use the `.mj
67
68
  npm install bson
68
69
  ```
69
70
 
71
+ ### MongoDB Node.js Driver Version Compatibility
72
+
73
+ Only the following version combinations with the [MongoDB Node.js Driver](https://github.com/mongodb/node-mongodb-native) are considered stable.
74
+
75
+ | | `bson@1.x` | `bson@4.x` | `bson@5.x` | `bson@6.x` |
76
+ | ------------- | ---------- | ---------- | ---------- | ---------- |
77
+ | `mongodb@6.x` | N/A | N/A | N/A | ✓ |
78
+ | `mongodb@5.x` | N/A | N/A | ✓ | N/A |
79
+ | `mongodb@4.x` | N/A | ✓ | N/A | N/A |
80
+ | `mongodb@3.x` | ✓ | N/A | N/A | N/A |
81
+
70
82
  ## Documentation
71
83
 
72
- ### BSON
84
+ ### BSON
73
85
 
74
86
  [API documentation](https://mongodb.github.io/node-mongodb-native/Next/modules/BSON.html)
75
87
 
@@ -77,31 +89,31 @@ npm install bson
77
89
 
78
90
  ### EJSON
79
91
 
80
- * [EJSON](#EJSON)
81
-
82
- * [.parse(text, [options])](#EJSON.parse)
92
+ - [EJSON](#EJSON)
83
93
 
84
- * [.stringify(value, [replacer], [space], [options])](#EJSON.stringify)
94
+ - [.parse(text, [options])](#EJSON.parse)
85
95
 
86
- * [.serialize(bson, [options])](#EJSON.serialize)
96
+ - [.stringify(value, [replacer], [space], [options])](#EJSON.stringify)
87
97
 
88
- * [.deserialize(ejson, [options])](#EJSON.deserialize)
98
+ - [.serialize(bson, [options])](#EJSON.serialize)
89
99
 
100
+ - [.deserialize(ejson, [options])](#EJSON.deserialize)
90
101
 
91
102
  <a name="EJSON.parse"></a>
92
103
 
93
- #### *EJSON*.parse(text, [options])
104
+ #### _EJSON_.parse(text, [options])
94
105
 
95
- | Param | Type | Default | Description |
96
- | --- | --- | --- | --- |
97
- | text | <code>string</code> | | |
98
- | [options] | <code>object</code> | | Optional settings |
106
+ | Param | Type | Default | Description |
107
+ | ----------------- | -------------------- | ----------------- | ---------------------------------------------------------------------------------- |
108
+ | text | <code>string</code> | | |
109
+ | [options] | <code>object</code> | | Optional settings |
99
110
  | [options.relaxed] | <code>boolean</code> | <code>true</code> | Attempt to return native JS types where possible, rather than BSON types (if true) |
100
111
 
101
112
  Parse an Extended JSON string, constructing the JavaScript value or object described by that
102
113
  string.
103
114
 
104
115
  **Example**
116
+
105
117
  ```js
106
118
  const { EJSON } = require('bson');
107
119
  const text = '{ "int32": { "$numberInt": "10" } }';
@@ -112,24 +124,26 @@ console.log(EJSON.parse(text, { relaxed: false }));
112
124
  // prints { int32: 10 }
113
125
  console.log(EJSON.parse(text));
114
126
  ```
127
+
115
128
  <a name="EJSON.stringify"></a>
116
129
 
117
- #### *EJSON*.stringify(value, [replacer], [space], [options])
130
+ #### _EJSON_.stringify(value, [replacer], [space], [options])
118
131
 
119
- | Param | Type | Default | Description |
120
- | --- | --- | --- | --- |
121
- | value | <code>object</code> | | The value to convert to extended JSON |
122
- | [replacer] | <code>function</code> \| <code>array</code> | | A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string |
123
- | [space] | <code>string</code> \| <code>number</code> | | A String or Number object that's used to insert white space into the output JSON string for readability purposes. |
124
- | [options] | <code>object</code> | | Optional settings |
125
- | [options.relaxed] | <code>boolean</code> | <code>true</code> | Enabled Extended JSON's `relaxed` mode |
126
- | [options.legacy] | <code>boolean</code> | <code>true</code> | Output in Extended JSON v1 |
132
+ | Param | Type | Default | Description |
133
+ | ----------------- | ------------------------------------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
134
+ | value | <code>object</code> | | The value to convert to extended JSON |
135
+ | [replacer] | <code>function</code> \| <code>array</code> | | A function that alters the behavior of the stringification process, or an array of String and Number objects that serve as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. If this value is null or not provided, all properties of the object are included in the resulting JSON string |
136
+ | [space] | <code>string</code> \| <code>number</code> | | A String or Number object that's used to insert white space into the output JSON string for readability purposes. |
137
+ | [options] | <code>object</code> | | Optional settings |
138
+ | [options.relaxed] | <code>boolean</code> | <code>true</code> | Enabled Extended JSON's `relaxed` mode |
139
+ | [options.legacy] | <code>boolean</code> | <code>true</code> | Output in Extended JSON v1 |
127
140
 
128
141
  Converts a BSON document to an Extended JSON string, optionally replacing values if a replacer
129
142
  function is specified or optionally including only the specified properties if a replacer array
130
143
  is specified.
131
144
 
132
145
  **Example**
146
+
133
147
  ```js
134
148
  const { EJSON } = require('bson');
135
149
  const Int32 = require('mongodb').Int32;
@@ -141,24 +155,25 @@ console.log(EJSON.stringify(doc, { relaxed: false }));
141
155
  // prints '{"int32":10}'
142
156
  console.log(EJSON.stringify(doc));
143
157
  ```
158
+
144
159
  <a name="EJSON.serialize"></a>
145
160
 
146
- #### *EJSON*.serialize(bson, [options])
161
+ #### _EJSON_.serialize(bson, [options])
147
162
 
148
- | Param | Type | Description |
149
- | --- | --- | --- |
150
- | bson | <code>object</code> | The object to serialize |
163
+ | Param | Type | Description |
164
+ | --------- | ------------------- | ---------------------------------------------------- |
165
+ | bson | <code>object</code> | The object to serialize |
151
166
  | [options] | <code>object</code> | Optional settings passed to the `stringify` function |
152
167
 
153
168
  Serializes an object to an Extended JSON string, and reparse it as a JavaScript object.
154
169
 
155
170
  <a name="EJSON.deserialize"></a>
156
171
 
157
- #### *EJSON*.deserialize(ejson, [options])
172
+ #### _EJSON_.deserialize(ejson, [options])
158
173
 
159
- | Param | Type | Description |
160
- | --- | --- | --- |
161
- | ejson | <code>object</code> | The Extended JSON object to deserialize |
174
+ | Param | Type | Description |
175
+ | --------- | ------------------- | -------------------------------------------- |
176
+ | ejson | <code>object</code> | The Extended JSON object to deserialize |
162
177
  | [options] | <code>object</code> | Optional settings passed to the parse method |
163
178
 
164
179
  Deserializes an Extended JSON object into a plain JavaScript object with native/BSON types
@@ -188,23 +203,16 @@ try {
188
203
 
189
204
  ## React Native
190
205
 
191
- BSON requires that `TextEncoder`, `TextDecoder`, `atob`, `btoa`, and `crypto.getRandomValues` are available globally. These are present in most Javascript runtimes but require polyfilling in React Native. Polyfills for the missing functionality can be installed with the following command:
206
+ BSON vendors the required polyfills for `TextEncoder`, `TextDecoder`, `atob`, `btoa` imported from React Native and therefore doesn't expect users to polyfill these. One additional polyfill, `crypto.getRandomValues` is recommended and can be installed with the following command:
207
+
192
208
  ```sh
193
- npm install --save react-native-get-random-values text-encoding-polyfill base-64
209
+ npm install --save react-native-get-random-values
194
210
  ```
195
211
 
196
- The following snippet should be placed at the top of the entrypoint (by default this is the root `index.js` file) for React Native projects using the BSON library. These lines must be placed for any code that imports `BSON`.
212
+ The following snippet should be placed at the top of the entrypoint (by default this is the root `index.js` file) for React Native projects using the BSON library. These lines must be placed for any code that imports `BSON`.
197
213
 
198
214
  ```typescript
199
215
  // Required Polyfills For ReactNative
200
- import {encode, decode} from 'base-64';
201
- if (global.btoa == null) {
202
- global.btoa = encode;
203
- }
204
- if (global.atob == null) {
205
- global.atob = decode;
206
- }
207
- import 'text-encoding-polyfill';
208
216
  import 'react-native-get-random-values';
209
217
  ```
210
218
 
@@ -214,11 +222,11 @@ Finally, import the `BSON` library like so:
214
222
  import { BSON, EJSON } from 'bson';
215
223
  ```
216
224
 
217
- This will cause React Native to import the `node_modules/bson/lib/bson.cjs` bundle (see the `"react-native"` setting we have in the `"exports"` section of our [package.json](./package.json).)
225
+ This will cause React Native to import the `node_modules/bson/lib/bson.rn.cjs` bundle (see the `"react-native"` setting we have in the `"exports"` section of our [package.json](./package.json).)
218
226
 
219
227
  ### Technical Note about React Native module import
220
228
 
221
- The `"exports"` definition in our `package.json` will result in BSON's CommonJS bundle being imported in a React Native project instead of the ES module bundle. Importing the CommonJS bundle is necessary because BSON's ES module bundle of BSON uses top-level await, which is not supported syntax in [React Native's runtime hermes](https://hermesengine.dev/).
229
+ The `"exports"` definition in our `package.json` will result in BSON's CommonJS bundle being imported in a React Native project instead of the ES module bundle. Importing the CommonJS bundle is necessary because BSON's ES module bundle of BSON uses top-level await, which is not supported syntax in [React Native's runtime hermes](https://hermesengine.dev/).
222
230
 
223
231
  ## FAQ
224
232
 
package/bson.d.ts CHANGED
@@ -31,16 +31,10 @@ export declare class Binary extends BSONValue {
31
31
  position: number;
32
32
  /**
33
33
  * Create a new Binary instance.
34
- *
35
- * This constructor can accept a string as its first argument. In this case,
36
- * this string will be encoded using ISO-8859-1, **not** using UTF-8.
37
- * This is almost certainly not what you want. Use `new Binary(Buffer.from(string))`
38
- * instead to convert the string to a Buffer using UTF-8 first.
39
- *
40
34
  * @param buffer - a buffer object containing the binary data.
41
35
  * @param subType - the option binary type.
42
36
  */
43
- constructor(buffer?: string | BinarySequence, subType?: number);
37
+ constructor(buffer?: BinarySequence, subType?: number);
44
38
  /**
45
39
  * Updates this binary with byte_value.
46
40
  *
@@ -48,12 +42,12 @@ export declare class Binary extends BSONValue {
48
42
  */
49
43
  put(byteValue: string | number | Uint8Array | number[]): void;
50
44
  /**
51
- * Writes a buffer or string to the binary.
45
+ * Writes a buffer to the binary.
52
46
  *
53
47
  * @param sequence - a string or buffer to be written to the Binary BSON object.
54
48
  * @param offset - specify the binary of where to write the content.
55
49
  */
56
- write(sequence: string | BinarySequence, offset: number): void;
50
+ write(sequence: BinarySequence, offset: number): void;
57
51
  /**
58
52
  * Reads **length** bytes starting at **position**.
59
53
  *
@@ -61,13 +55,8 @@ export declare class Binary extends BSONValue {
61
55
  * @param length - the number of bytes to read.
62
56
  */
63
57
  read(position: number, length: number): BinarySequence;
64
- /**
65
- * Returns the value of this binary as a string.
66
- * @param asRaw - Will skip converting to a string
67
- * @remarks
68
- * This is handy when calling this function conditionally for some key value pairs and not others
69
- */
70
- value(asRaw?: boolean): string | BinarySequence;
58
+ /** returns a view of the binary value as a Uint8Array */
59
+ value(): Uint8Array;
71
60
  /** the length of the binary sequence */
72
61
  length(): number;
73
62
  toJSON(): string;
@@ -990,7 +979,7 @@ export declare class ObjectId extends BSONValue {
990
979
  */
991
980
  get id(): Uint8Array;
992
981
  set id(value: Uint8Array);
993
- /** Returns the ObjectId id as a 24 character hex string representation */
982
+ /** Returns the ObjectId id as a 24 lowercase character hex string representation */
994
983
  toHexString(): string;
995
984
  /* Excluded from this release type: getInc */
996
985
  /**
@@ -1006,12 +995,13 @@ export declare class ObjectId extends BSONValue {
1006
995
  toString(encoding?: 'hex' | 'base64'): string;
1007
996
  /** Converts to its JSON the 24 character hex string representation. */
1008
997
  toJSON(): string;
998
+ /* Excluded from this release type: is */
1009
999
  /**
1010
1000
  * Compares the equality of this ObjectId with `otherID`.
1011
1001
  *
1012
1002
  * @param otherId - ObjectId instance to compare against.
1013
1003
  */
1014
- equals(otherId: string | ObjectId | ObjectIdLike): boolean;
1004
+ equals(otherId: string | ObjectId | ObjectIdLike | undefined | null): boolean;
1015
1005
  /** Returns the generation date (accurate up to the second) that this ID was generated. */
1016
1006
  getTimestamp(): Date;
1017
1007
  /* Excluded from this release type: createPk */
@@ -1030,9 +1020,8 @@ export declare class ObjectId extends BSONValue {
1030
1020
  /** Creates an ObjectId instance from a base64 string */
1031
1021
  static createFromBase64(base64: string): ObjectId;
1032
1022
  /**
1033
- * Checks if a value is a valid bson ObjectId
1034
- *
1035
- * @param id - ObjectId instance to validate.
1023
+ * Checks if a value can be used to create a valid bson ObjectId
1024
+ * @param id - any JS value
1036
1025
  */
1037
1026
  static isValid(id: string | number | ObjectId | ObjectIdLike | Uint8Array): boolean;
1038
1027
  /* Excluded from this release type: toExtendedJSON */
@@ -1213,8 +1202,6 @@ export declare type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromE
1213
1202
  * @public
1214
1203
  */
1215
1204
  export declare class UUID extends Binary {
1216
- /** @deprecated Hex string is no longer cached, this control will be removed in a future major release */
1217
- static cacheHexString: boolean;
1218
1205
  /**
1219
1206
  * Create a UUID type
1220
1207
  *
@@ -17,7 +17,7 @@ function isDate(d) {
17
17
  return Object.prototype.toString.call(d) === '[object Date]';
18
18
  }
19
19
 
20
- const BSON_MAJOR_VERSION = 5;
20
+ const BSON_MAJOR_VERSION = 6;
21
21
  const BSON_INT32_MAX = 0x7fffffff;
22
22
  const BSON_INT32_MIN = -0x80000000;
23
23
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -166,8 +166,8 @@ const nodeJsByteUtils = {
166
166
  fromUTF8(text) {
167
167
  return Buffer.from(text, 'utf8');
168
168
  },
169
- toUTF8(buffer) {
170
- return nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8');
169
+ toUTF8(buffer, start, end) {
170
+ return nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8', start, end);
171
171
  },
172
172
  utf8ByteLength(input) {
173
173
  return Buffer.byteLength(input, 'utf8');
@@ -277,8 +277,8 @@ const webByteUtils = {
277
277
  fromUTF8(text) {
278
278
  return new TextEncoder().encode(text);
279
279
  },
280
- toUTF8(uint8array) {
281
- return new TextDecoder('utf8', { fatal: false }).decode(uint8array);
280
+ toUTF8(uint8array, start, end) {
281
+ return new TextDecoder('utf8', { fatal: false }).decode(uint8array.slice(start, end));
282
282
  },
283
283
  utf8ByteLength(input) {
284
284
  return webByteUtils.fromUTF8(input).byteLength;
@@ -312,11 +312,11 @@ class Binary extends BSONValue {
312
312
  constructor(buffer, subType) {
313
313
  super();
314
314
  if (!(buffer == null) &&
315
- !(typeof buffer === 'string') &&
315
+ typeof buffer === 'string' &&
316
316
  !ArrayBuffer.isView(buffer) &&
317
- !(buffer instanceof ArrayBuffer) &&
317
+ !isAnyArrayBuffer(buffer) &&
318
318
  !Array.isArray(buffer)) {
319
- throw new BSONError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
319
+ throw new BSONError('Binary can only be constructed from Uint8Array or number[]');
320
320
  }
321
321
  this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
322
322
  if (buffer == null) {
@@ -324,15 +324,9 @@ class Binary extends BSONValue {
324
324
  this.position = 0;
325
325
  }
326
326
  else {
327
- if (typeof buffer === 'string') {
328
- this.buffer = ByteUtils.fromISO88591(buffer);
329
- }
330
- else if (Array.isArray(buffer)) {
331
- this.buffer = ByteUtils.fromNumberArray(buffer);
332
- }
333
- else {
334
- this.buffer = ByteUtils.toLocalBufferType(buffer);
335
- }
327
+ this.buffer = Array.isArray(buffer)
328
+ ? ByteUtils.fromNumberArray(buffer)
329
+ : ByteUtils.toLocalBufferType(buffer);
336
330
  this.position = this.buffer.byteLength;
337
331
  }
338
332
  }
@@ -378,25 +372,17 @@ class Binary extends BSONValue {
378
372
  offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
379
373
  }
380
374
  else if (typeof sequence === 'string') {
381
- const bytes = ByteUtils.fromISO88591(sequence);
382
- this.buffer.set(bytes, offset);
383
- this.position =
384
- offset + sequence.length > this.position ? offset + sequence.length : this.position;
375
+ throw new BSONError('input cannot be string');
385
376
  }
386
377
  }
387
378
  read(position, length) {
388
379
  length = length && length > 0 ? length : this.position;
389
380
  return this.buffer.slice(position, position + length);
390
381
  }
391
- value(asRaw) {
392
- asRaw = !!asRaw;
393
- if (asRaw && this.buffer.length === this.position) {
394
- return this.buffer;
395
- }
396
- if (asRaw) {
397
- return this.buffer.slice(0, this.position);
398
- }
399
- return ByteUtils.toISO88591(this.buffer.subarray(0, this.position));
382
+ value() {
383
+ return this.buffer.length === this.position
384
+ ? this.buffer
385
+ : this.buffer.subarray(0, this.position);
400
386
  }
401
387
  length() {
402
388
  return this.position;
@@ -410,8 +396,8 @@ class Binary extends BSONValue {
410
396
  if (encoding === 'base64')
411
397
  return ByteUtils.toBase64(this.buffer);
412
398
  if (encoding === 'utf8' || encoding === 'utf-8')
413
- return ByteUtils.toUTF8(this.buffer);
414
- return ByteUtils.toUTF8(this.buffer);
399
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
400
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
415
401
  }
416
402
  toExtendedJSON(options) {
417
403
  options = options || {};
@@ -597,7 +583,6 @@ class UUID extends Binary {
597
583
  return `new UUID("${this.toHexString()}")`;
598
584
  }
599
585
  }
600
- UUID.cacheHexString = false;
601
586
 
602
587
  class Code extends BSONValue {
603
588
  get _bsontype() {
@@ -1991,20 +1976,11 @@ class ObjectId extends BSONValue {
1991
1976
  this[kId] = ByteUtils.toLocalBufferType(workingId);
1992
1977
  }
1993
1978
  else if (typeof workingId === 'string') {
1994
- if (workingId.length === 12) {
1995
- const bytes = ByteUtils.fromUTF8(workingId);
1996
- if (bytes.byteLength === 12) {
1997
- this[kId] = bytes;
1998
- }
1999
- else {
2000
- throw new BSONError('Argument passed in must be a string of 12 bytes');
2001
- }
2002
- }
2003
- else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
1979
+ if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2004
1980
  this[kId] = ByteUtils.fromHex(workingId);
2005
1981
  }
2006
1982
  else {
2007
- throw new BSONError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
1983
+ throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
2008
1984
  }
2009
1985
  }
2010
1986
  else {
@@ -2066,30 +2042,25 @@ class ObjectId extends BSONValue {
2066
2042
  toJSON() {
2067
2043
  return this.toHexString();
2068
2044
  }
2045
+ static is(variable) {
2046
+ return (variable != null &&
2047
+ typeof variable === 'object' &&
2048
+ '_bsontype' in variable &&
2049
+ variable._bsontype === 'ObjectId');
2050
+ }
2069
2051
  equals(otherId) {
2070
2052
  if (otherId === undefined || otherId === null) {
2071
2053
  return false;
2072
2054
  }
2073
- if (otherId instanceof ObjectId) {
2055
+ if (ObjectId.is(otherId)) {
2074
2056
  return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
2075
2057
  }
2076
- if (typeof otherId === 'string' &&
2077
- ObjectId.isValid(otherId) &&
2078
- otherId.length === 12 &&
2079
- isUint8Array(this.id)) {
2080
- return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
2081
- }
2082
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
2058
+ if (typeof otherId === 'string') {
2083
2059
  return otherId.toLowerCase() === this.toHexString();
2084
2060
  }
2085
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
2086
- return ByteUtils.equals(ByteUtils.fromUTF8(otherId), this.id);
2087
- }
2088
- if (typeof otherId === 'object' &&
2089
- 'toHexString' in otherId &&
2090
- typeof otherId.toHexString === 'function') {
2061
+ if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') {
2091
2062
  const otherIdString = otherId.toHexString();
2092
- const thisIdString = this.toHexString().toLowerCase();
2063
+ const thisIdString = this.toHexString();
2093
2064
  return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
2094
2065
  }
2095
2066
  return false;
@@ -2417,19 +2388,21 @@ class Timestamp extends LongWithoutOverridesClass {
2417
2388
  if (typeof low.i !== 'number' && (typeof low.i !== 'object' || low.i._bsontype !== 'Int32')) {
2418
2389
  throw new BSONError('Timestamp constructed from { t, i } must provide i as a number');
2419
2390
  }
2420
- if (low.t < 0) {
2391
+ const t = Number(low.t);
2392
+ const i = Number(low.i);
2393
+ if (t < 0 || Number.isNaN(t)) {
2421
2394
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive t');
2422
2395
  }
2423
- if (low.i < 0) {
2396
+ if (i < 0 || Number.isNaN(i)) {
2424
2397
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2425
2398
  }
2426
- if (low.t > 4294967295) {
2399
+ if (t > 4294967295) {
2427
2400
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2428
2401
  }
2429
- if (low.i > 4294967295) {
2402
+ if (i > 4294967295) {
2430
2403
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2431
2404
  }
2432
- super(low.i.valueOf(), low.t.valueOf(), true);
2405
+ super(i, t, true);
2433
2406
  }
2434
2407
  else {
2435
2408
  throw new BSONError('A Timestamp can only be constructed with: bigint, Long, or { t: number; i: number }');
@@ -2601,7 +2574,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2601
2574
  }
2602
2575
  if (i >= buffer.byteLength)
2603
2576
  throw new BSONError('Bad BSON Document: illegal CString');
2604
- const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer.subarray(index, i));
2577
+ const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer, index, i);
2605
2578
  let shouldValidateKey = true;
2606
2579
  if (globalUTFValidation || utf8KeysSet.has(name)) {
2607
2580
  shouldValidateKey = validationSetting;
@@ -2816,7 +2789,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2816
2789
  }
2817
2790
  if (i >= buffer.length)
2818
2791
  throw new BSONError('Bad BSON Document: illegal CString');
2819
- const source = ByteUtils.toUTF8(buffer.subarray(index, i));
2792
+ const source = ByteUtils.toUTF8(buffer, index, i);
2820
2793
  index = i + 1;
2821
2794
  i = index;
2822
2795
  while (buffer[i] !== 0x00 && i < buffer.length) {
@@ -2824,7 +2797,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2824
2797
  }
2825
2798
  if (i >= buffer.length)
2826
2799
  throw new BSONError('Bad BSON Document: illegal CString');
2827
- const regExpOptions = ByteUtils.toUTF8(buffer.subarray(index, i));
2800
+ const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
2828
2801
  index = i + 1;
2829
2802
  const optionsArray = new Array(regExpOptions.length);
2830
2803
  for (i = 0; i < regExpOptions.length; i++) {
@@ -2849,7 +2822,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2849
2822
  }
2850
2823
  if (i >= buffer.length)
2851
2824
  throw new BSONError('Bad BSON Document: illegal CString');
2852
- const source = ByteUtils.toUTF8(buffer.subarray(index, i));
2825
+ const source = ByteUtils.toUTF8(buffer, index, i);
2853
2826
  index = i + 1;
2854
2827
  i = index;
2855
2828
  while (buffer[i] !== 0x00 && i < buffer.length) {
@@ -2857,7 +2830,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2857
2830
  }
2858
2831
  if (i >= buffer.length)
2859
2832
  throw new BSONError('Bad BSON Document: illegal CString');
2860
- const regExpOptions = ByteUtils.toUTF8(buffer.subarray(index, i));
2833
+ const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
2861
2834
  index = i + 1;
2862
2835
  value = new BSONRegExp(source, regExpOptions);
2863
2836
  }
@@ -2954,7 +2927,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2954
2927
  throw new BSONError('Invalid UTF-8 string in BSON document');
2955
2928
  }
2956
2929
  }
2957
- const namespace = ByteUtils.toUTF8(buffer.subarray(index, index + stringSize - 1));
2930
+ const namespace = ByteUtils.toUTF8(buffer, index, index + stringSize - 1);
2958
2931
  index = index + stringSize;
2959
2932
  const oidBuffer = ByteUtils.allocate(12);
2960
2933
  oidBuffer.set(buffer.subarray(index, index + 12), 0);
@@ -2994,7 +2967,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2994
2967
  return object;
2995
2968
  }
2996
2969
  function getValidatedString(buffer, start, end, shouldValidateUtf8) {
2997
- const value = ByteUtils.toUTF8(buffer.subarray(start, end));
2970
+ const value = ByteUtils.toUTF8(buffer, start, end);
2998
2971
  if (shouldValidateUtf8) {
2999
2972
  for (let i = 0; i < value.length; i++) {
3000
2973
  if (value.charCodeAt(i) === 0xfffd) {