bson 6.1.0 → 6.3.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/lib/bson.rn.cjs CHANGED
@@ -21,6 +21,26 @@ function isMap(d) {
21
21
  function isDate(d) {
22
22
  return Object.prototype.toString.call(d) === '[object Date]';
23
23
  }
24
+ function defaultInspect(x, _options) {
25
+ return JSON.stringify(x, (k, v) => {
26
+ if (typeof v === 'bigint') {
27
+ return { $numberLong: `${v}` };
28
+ }
29
+ else if (isMap(v)) {
30
+ return Object.fromEntries(v);
31
+ }
32
+ return v;
33
+ });
34
+ }
35
+ function getStylizeFunction(options) {
36
+ const stylizeExists = options != null &&
37
+ typeof options === 'object' &&
38
+ 'stylize' in options &&
39
+ typeof options.stylize === 'function';
40
+ if (stylizeExists) {
41
+ return options.stylize;
42
+ }
43
+ }
24
44
 
25
45
  const BSON_MAJOR_VERSION = 6;
26
46
  const BSON_INT32_MAX = 0x7fffffff;
@@ -90,8 +110,8 @@ class BSONError extends Error {
90
110
  get name() {
91
111
  return 'BSONError';
92
112
  }
93
- constructor(message) {
94
- super(message);
113
+ constructor(message, options) {
114
+ super(message, options);
95
115
  }
96
116
  static isBSONError(value) {
97
117
  return (value != null &&
@@ -108,7 +128,7 @@ class BSONVersionError extends BSONError {
108
128
  return 'BSONVersionError';
109
129
  }
110
130
  constructor() {
111
- super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.0 or later`);
131
+ super(`Unsupported BSON version, bson types must be from bson ${BSON_MAJOR_VERSION}.x.x`);
112
132
  }
113
133
  }
114
134
  class BSONRuntimeError extends BSONError {
@@ -120,6 +140,79 @@ class BSONRuntimeError extends BSONError {
120
140
  }
121
141
  }
122
142
 
143
+ const FIRST_BIT = 0x80;
144
+ const FIRST_TWO_BITS = 0xc0;
145
+ const FIRST_THREE_BITS = 0xe0;
146
+ const FIRST_FOUR_BITS = 0xf0;
147
+ const FIRST_FIVE_BITS = 0xf8;
148
+ const TWO_BIT_CHAR = 0xc0;
149
+ const THREE_BIT_CHAR = 0xe0;
150
+ const FOUR_BIT_CHAR = 0xf0;
151
+ const CONTINUING_CHAR = 0x80;
152
+ function validateUtf8(bytes, start, end) {
153
+ let continuation = 0;
154
+ for (let i = start; i < end; i += 1) {
155
+ const byte = bytes[i];
156
+ if (continuation) {
157
+ if ((byte & FIRST_TWO_BITS) !== CONTINUING_CHAR) {
158
+ return false;
159
+ }
160
+ continuation -= 1;
161
+ }
162
+ else if (byte & FIRST_BIT) {
163
+ if ((byte & FIRST_THREE_BITS) === TWO_BIT_CHAR) {
164
+ continuation = 1;
165
+ }
166
+ else if ((byte & FIRST_FOUR_BITS) === THREE_BIT_CHAR) {
167
+ continuation = 2;
168
+ }
169
+ else if ((byte & FIRST_FIVE_BITS) === FOUR_BIT_CHAR) {
170
+ continuation = 3;
171
+ }
172
+ else {
173
+ return false;
174
+ }
175
+ }
176
+ }
177
+ return !continuation;
178
+ }
179
+
180
+ function tryLatin(uint8array, start, end) {
181
+ if (uint8array.length === 0) {
182
+ return '';
183
+ }
184
+ const stringByteLength = end - start;
185
+ if (stringByteLength === 0) {
186
+ return '';
187
+ }
188
+ if (stringByteLength > 20) {
189
+ return null;
190
+ }
191
+ if (stringByteLength === 1 && uint8array[start] < 128) {
192
+ return String.fromCharCode(uint8array[start]);
193
+ }
194
+ if (stringByteLength === 2 && uint8array[start] < 128 && uint8array[start + 1] < 128) {
195
+ return String.fromCharCode(uint8array[start]) + String.fromCharCode(uint8array[start + 1]);
196
+ }
197
+ if (stringByteLength === 3 &&
198
+ uint8array[start] < 128 &&
199
+ uint8array[start + 1] < 128 &&
200
+ uint8array[start + 2] < 128) {
201
+ return (String.fromCharCode(uint8array[start]) +
202
+ String.fromCharCode(uint8array[start + 1]) +
203
+ String.fromCharCode(uint8array[start + 2]));
204
+ }
205
+ const latinBytes = [];
206
+ for (let i = start; i < end; i++) {
207
+ const byte = uint8array[i];
208
+ if (byte > 127) {
209
+ return null;
210
+ }
211
+ latinBytes.push(byte);
212
+ }
213
+ return String.fromCharCode(...latinBytes);
214
+ }
215
+
123
216
  function nodejsMathRandomBytes(byteLength) {
124
217
  return nodeJsByteUtils.fromNumberArray(Array.from({ length: byteLength }, () => Math.floor(Math.random() * 256)));
125
218
  }
@@ -178,8 +271,23 @@ const nodeJsByteUtils = {
178
271
  fromUTF8(text) {
179
272
  return Buffer.from(text, 'utf8');
180
273
  },
181
- toUTF8(buffer, start, end) {
182
- return nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8', start, end);
274
+ toUTF8(buffer, start, end, fatal) {
275
+ const basicLatin = end - start <= 20 ? tryLatin(buffer, start, end) : null;
276
+ if (basicLatin != null) {
277
+ return basicLatin;
278
+ }
279
+ const string = nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8', start, end);
280
+ if (fatal) {
281
+ for (let i = 0; i < string.length; i++) {
282
+ if (string.charCodeAt(i) === 0xfffd) {
283
+ if (!validateUtf8(buffer, start, end)) {
284
+ throw new BSONError('Invalid UTF-8 string in BSON document');
285
+ }
286
+ break;
287
+ }
288
+ }
289
+ }
290
+ return string;
183
291
  },
184
292
  utf8ByteLength(input) {
185
293
  return Buffer.byteLength(input, 'utf8');
@@ -291,8 +399,20 @@ const webByteUtils = {
291
399
  fromUTF8(text) {
292
400
  return new TextEncoder().encode(text);
293
401
  },
294
- toUTF8(uint8array, start, end) {
295
- return new TextDecoder('utf8', { fatal: false }).decode(uint8array.slice(start, end));
402
+ toUTF8(uint8array, start, end, fatal) {
403
+ const basicLatin = end - start <= 20 ? tryLatin(uint8array, start, end) : null;
404
+ if (basicLatin != null) {
405
+ return basicLatin;
406
+ }
407
+ if (fatal) {
408
+ try {
409
+ return new TextDecoder('utf8', { fatal }).decode(uint8array.slice(start, end));
410
+ }
411
+ catch (cause) {
412
+ throw new BSONError('Invalid UTF-8 string in BSON document', { cause });
413
+ }
414
+ }
415
+ return new TextDecoder('utf8', { fatal }).decode(uint8array.slice(start, end));
296
416
  },
297
417
  utf8ByteLength(input) {
298
418
  return webByteUtils.fromUTF8(input).byteLength;
@@ -317,6 +437,9 @@ class BSONValue {
317
437
  get [Symbol.for('@@mdb.bson.version')]() {
318
438
  return BSON_MAJOR_VERSION;
319
439
  }
440
+ [Symbol.for('nodejs.util.inspect.custom')](depth, options, inspect) {
441
+ return this.inspect(depth, options, inspect);
442
+ }
320
443
  }
321
444
 
322
445
  class Binary extends BSONValue {
@@ -410,8 +533,8 @@ class Binary extends BSONValue {
410
533
  if (encoding === 'base64')
411
534
  return ByteUtils.toBase64(this.buffer);
412
535
  if (encoding === 'utf8' || encoding === 'utf-8')
413
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
414
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
536
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
537
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
415
538
  }
416
539
  toExtendedJSON(options) {
417
540
  options = options || {};
@@ -467,12 +590,12 @@ class Binary extends BSONValue {
467
590
  }
468
591
  return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
469
592
  }
470
- [Symbol.for('nodejs.util.inspect.custom')]() {
471
- return this.inspect();
472
- }
473
- inspect() {
593
+ inspect(depth, options, inspect) {
594
+ inspect ??= defaultInspect;
474
595
  const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
475
- return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
596
+ const base64Arg = inspect(base64, options);
597
+ const subTypeArg = inspect(this.sub_type, options);
598
+ return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
476
599
  }
477
600
  }
478
601
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -590,11 +713,9 @@ class UUID extends Binary {
590
713
  static isValidUUIDString(representation) {
591
714
  return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
592
715
  }
593
- [Symbol.for('nodejs.util.inspect.custom')]() {
594
- return this.inspect();
595
- }
596
- inspect() {
597
- return `new UUID("${this.toHexString()}")`;
716
+ inspect(depth, options, inspect) {
717
+ inspect ??= defaultInspect;
718
+ return `new UUID(${inspect(this.toHexString(), options)})`;
598
719
  }
599
720
  }
600
721
 
@@ -622,12 +743,15 @@ class Code extends BSONValue {
622
743
  static fromExtendedJSON(doc) {
623
744
  return new Code(doc.$code, doc.$scope);
624
745
  }
625
- [Symbol.for('nodejs.util.inspect.custom')]() {
626
- return this.inspect();
627
- }
628
- inspect() {
629
- const codeJson = this.toJSON();
630
- return `new Code(${JSON.stringify(String(codeJson.code))}${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
746
+ inspect(depth, options, inspect) {
747
+ inspect ??= defaultInspect;
748
+ let parametersString = inspect(this.code, options);
749
+ const multiLineFn = parametersString.includes('\n');
750
+ if (this.scope != null) {
751
+ parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`;
752
+ }
753
+ const endingNewline = multiLineFn && this.scope === null;
754
+ return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`;
631
755
  }
632
756
  }
633
757
 
@@ -692,12 +816,16 @@ class DBRef extends BSONValue {
692
816
  delete copy.$db;
693
817
  return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
694
818
  }
695
- [Symbol.for('nodejs.util.inspect.custom')]() {
696
- return this.inspect();
697
- }
698
- inspect() {
699
- const oid = this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
700
- return `new DBRef("${this.namespace}", new ObjectId("${String(oid)}")${this.db ? `, "${this.db}"` : ''})`;
819
+ inspect(depth, options, inspect) {
820
+ inspect ??= defaultInspect;
821
+ const args = [
822
+ inspect(this.namespace, options),
823
+ inspect(this.oid, options),
824
+ ...(this.db ? [inspect(this.db, options)] : []),
825
+ ...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : [])
826
+ ];
827
+ args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1];
828
+ return `new DBRef(${args.join(', ')})`;
701
829
  }
702
830
  }
703
831
 
@@ -1324,11 +1452,11 @@ class Long extends BSONValue {
1324
1452
  }
1325
1453
  return longResult;
1326
1454
  }
1327
- [Symbol.for('nodejs.util.inspect.custom')]() {
1328
- return this.inspect();
1329
- }
1330
- inspect() {
1331
- return `new Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
1455
+ inspect(depth, options, inspect) {
1456
+ inspect ??= defaultInspect;
1457
+ const longVal = inspect(this.toString(), options);
1458
+ const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : '';
1459
+ return `new Long(${longVal}${unsignedVal})`;
1332
1460
  }
1333
1461
  }
1334
1462
  Long.TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
@@ -1884,11 +2012,10 @@ class Decimal128 extends BSONValue {
1884
2012
  static fromExtendedJSON(doc) {
1885
2013
  return Decimal128.fromString(doc.$numberDecimal);
1886
2014
  }
1887
- [Symbol.for('nodejs.util.inspect.custom')]() {
1888
- return this.inspect();
1889
- }
1890
- inspect() {
1891
- return `new Decimal128("${this.toString()}")`;
2015
+ inspect(depth, options, inspect) {
2016
+ inspect ??= defaultInspect;
2017
+ const d128string = inspect(this.toString(), options);
2018
+ return `new Decimal128(${d128string})`;
1892
2019
  }
1893
2020
  }
1894
2021
 
@@ -1927,12 +2054,9 @@ class Double extends BSONValue {
1927
2054
  const doubleValue = parseFloat(doc.$numberDouble);
1928
2055
  return options && options.relaxed ? doubleValue : new Double(doubleValue);
1929
2056
  }
1930
- [Symbol.for('nodejs.util.inspect.custom')]() {
1931
- return this.inspect();
1932
- }
1933
- inspect() {
1934
- const eJSON = this.toExtendedJSON();
1935
- return `new Double(${eJSON.$numberDouble})`;
2057
+ inspect(depth, options, inspect) {
2058
+ inspect ??= defaultInspect;
2059
+ return `new Double(${inspect(this.value, options)})`;
1936
2060
  }
1937
2061
  }
1938
2062
 
@@ -1964,11 +2088,9 @@ class Int32 extends BSONValue {
1964
2088
  static fromExtendedJSON(doc, options) {
1965
2089
  return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
1966
2090
  }
1967
- [Symbol.for('nodejs.util.inspect.custom')]() {
1968
- return this.inspect();
1969
- }
1970
- inspect() {
1971
- return `new Int32(${this.valueOf()})`;
2091
+ inspect(depth, options, inspect) {
2092
+ inspect ??= defaultInspect;
2093
+ return `new Int32(${inspect(this.value, options)})`;
1972
2094
  }
1973
2095
  }
1974
2096
 
@@ -1982,9 +2104,6 @@ class MaxKey extends BSONValue {
1982
2104
  static fromExtendedJSON() {
1983
2105
  return new MaxKey();
1984
2106
  }
1985
- [Symbol.for('nodejs.util.inspect.custom')]() {
1986
- return this.inspect();
1987
- }
1988
2107
  inspect() {
1989
2108
  return 'new MaxKey()';
1990
2109
  }
@@ -2000,9 +2119,6 @@ class MinKey extends BSONValue {
2000
2119
  static fromExtendedJSON() {
2001
2120
  return new MinKey();
2002
2121
  }
2003
- [Symbol.for('nodejs.util.inspect.custom')]() {
2004
- return this.inspect();
2005
- }
2006
2122
  inspect() {
2007
2123
  return 'new MinKey()';
2008
2124
  }
@@ -2173,11 +2289,9 @@ class ObjectId extends BSONValue {
2173
2289
  static fromExtendedJSON(doc) {
2174
2290
  return new ObjectId(doc.$oid);
2175
2291
  }
2176
- [Symbol.for('nodejs.util.inspect.custom')]() {
2177
- return this.inspect();
2178
- }
2179
- inspect() {
2180
- return `new ObjectId("${this.toHexString()}")`;
2292
+ inspect(depth, options, inspect) {
2293
+ inspect ??= defaultInspect;
2294
+ return `new ObjectId(${inspect(this.toHexString(), options)})`;
2181
2295
  }
2182
2296
  }
2183
2297
  ObjectId.index = Math.floor(Math.random() * 0xffffff);
@@ -2390,11 +2504,12 @@ class BSONRegExp extends BSONValue {
2390
2504
  }
2391
2505
  throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
2392
2506
  }
2393
- [Symbol.for('nodejs.util.inspect.custom')]() {
2394
- return this.inspect();
2395
- }
2396
- inspect() {
2397
- return `new BSONRegExp(${JSON.stringify(this.pattern)}, ${JSON.stringify(this.options)})`;
2507
+ inspect(depth, options, inspect) {
2508
+ const stylize = getStylizeFunction(options) ?? (v => v);
2509
+ inspect ??= defaultInspect;
2510
+ const pattern = stylize(inspect(this.pattern), 'regexp');
2511
+ const flags = stylize(inspect(this.options), 'regexp');
2512
+ return `new BSONRegExp(${pattern}, ${flags})`;
2398
2513
  }
2399
2514
  }
2400
2515
 
@@ -2412,9 +2527,6 @@ class BSONSymbol extends BSONValue {
2412
2527
  toString() {
2413
2528
  return this.value;
2414
2529
  }
2415
- inspect() {
2416
- return `new BSONSymbol(${JSON.stringify(this.value)})`;
2417
- }
2418
2530
  toJSON() {
2419
2531
  return this.value;
2420
2532
  }
@@ -2424,8 +2536,9 @@ class BSONSymbol extends BSONValue {
2424
2536
  static fromExtendedJSON(doc) {
2425
2537
  return new BSONSymbol(doc.$symbol);
2426
2538
  }
2427
- [Symbol.for('nodejs.util.inspect.custom')]() {
2428
- return this.inspect();
2539
+ inspect(depth, options, inspect) {
2540
+ inspect ??= defaultInspect;
2541
+ return `new BSONSymbol(${inspect(this.value, options)})`;
2429
2542
  }
2430
2543
  }
2431
2544
 
@@ -2500,52 +2613,15 @@ class Timestamp extends LongWithoutOverridesClass {
2500
2613
  : doc.$timestamp.t;
2501
2614
  return new Timestamp({ t, i });
2502
2615
  }
2503
- [Symbol.for('nodejs.util.inspect.custom')]() {
2504
- return this.inspect();
2505
- }
2506
- inspect() {
2507
- return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
2616
+ inspect(depth, options, inspect) {
2617
+ inspect ??= defaultInspect;
2618
+ const t = inspect(this.high >>> 0, options);
2619
+ const i = inspect(this.low >>> 0, options);
2620
+ return `new Timestamp({ t: ${t}, i: ${i} })`;
2508
2621
  }
2509
2622
  }
2510
2623
  Timestamp.MAX_VALUE = Long.MAX_UNSIGNED_VALUE;
2511
2624
 
2512
- const FIRST_BIT = 0x80;
2513
- const FIRST_TWO_BITS = 0xc0;
2514
- const FIRST_THREE_BITS = 0xe0;
2515
- const FIRST_FOUR_BITS = 0xf0;
2516
- const FIRST_FIVE_BITS = 0xf8;
2517
- const TWO_BIT_CHAR = 0xc0;
2518
- const THREE_BIT_CHAR = 0xe0;
2519
- const FOUR_BIT_CHAR = 0xf0;
2520
- const CONTINUING_CHAR = 0x80;
2521
- function validateUtf8(bytes, start, end) {
2522
- let continuation = 0;
2523
- for (let i = start; i < end; i += 1) {
2524
- const byte = bytes[i];
2525
- if (continuation) {
2526
- if ((byte & FIRST_TWO_BITS) !== CONTINUING_CHAR) {
2527
- return false;
2528
- }
2529
- continuation -= 1;
2530
- }
2531
- else if (byte & FIRST_BIT) {
2532
- if ((byte & FIRST_THREE_BITS) === TWO_BIT_CHAR) {
2533
- continuation = 1;
2534
- }
2535
- else if ((byte & FIRST_FOUR_BITS) === THREE_BIT_CHAR) {
2536
- continuation = 2;
2537
- }
2538
- else if ((byte & FIRST_FIVE_BITS) === FOUR_BIT_CHAR) {
2539
- continuation = 3;
2540
- }
2541
- else {
2542
- return false;
2543
- }
2544
- }
2545
- }
2546
- return !continuation;
2547
- }
2548
-
2549
2625
  const JS_INT_MAX_LONG = Long.fromNumber(JS_INT_MAX);
2550
2626
  const JS_INT_MIN_LONG = Long.fromNumber(JS_INT_MIN);
2551
2627
  function internalDeserialize(buffer, options, isArray) {
@@ -2637,7 +2713,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2637
2713
  }
2638
2714
  if (i >= buffer.byteLength)
2639
2715
  throw new BSONError('Bad BSON Document: illegal CString');
2640
- const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer, index, i);
2716
+ const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer, index, i, false);
2641
2717
  let shouldValidateKey = true;
2642
2718
  if (globalUTFValidation || utf8KeysSet.has(name)) {
2643
2719
  shouldValidateKey = validationSetting;
@@ -2660,7 +2736,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2660
2736
  buffer[index + stringSize - 1] !== 0) {
2661
2737
  throw new BSONError('bad string length in bson');
2662
2738
  }
2663
- value = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
2739
+ value = ByteUtils.toUTF8(buffer, index, index + stringSize - 1, shouldValidateKey);
2664
2740
  index = index + stringSize;
2665
2741
  }
2666
2742
  else if (elementType === BSON_DATA_OID) {
@@ -2852,7 +2928,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2852
2928
  }
2853
2929
  if (i >= buffer.length)
2854
2930
  throw new BSONError('Bad BSON Document: illegal CString');
2855
- const source = ByteUtils.toUTF8(buffer, index, i);
2931
+ const source = ByteUtils.toUTF8(buffer, index, i, false);
2856
2932
  index = i + 1;
2857
2933
  i = index;
2858
2934
  while (buffer[i] !== 0x00 && i < buffer.length) {
@@ -2860,7 +2936,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2860
2936
  }
2861
2937
  if (i >= buffer.length)
2862
2938
  throw new BSONError('Bad BSON Document: illegal CString');
2863
- const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
2939
+ const regExpOptions = ByteUtils.toUTF8(buffer, index, i, false);
2864
2940
  index = i + 1;
2865
2941
  const optionsArray = new Array(regExpOptions.length);
2866
2942
  for (i = 0; i < regExpOptions.length; i++) {
@@ -2885,7 +2961,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2885
2961
  }
2886
2962
  if (i >= buffer.length)
2887
2963
  throw new BSONError('Bad BSON Document: illegal CString');
2888
- const source = ByteUtils.toUTF8(buffer, index, i);
2964
+ const source = ByteUtils.toUTF8(buffer, index, i, false);
2889
2965
  index = i + 1;
2890
2966
  i = index;
2891
2967
  while (buffer[i] !== 0x00 && i < buffer.length) {
@@ -2893,7 +2969,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2893
2969
  }
2894
2970
  if (i >= buffer.length)
2895
2971
  throw new BSONError('Bad BSON Document: illegal CString');
2896
- const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
2972
+ const regExpOptions = ByteUtils.toUTF8(buffer, index, i, false);
2897
2973
  index = i + 1;
2898
2974
  value = new BSONRegExp(source, regExpOptions);
2899
2975
  }
@@ -2907,7 +2983,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2907
2983
  buffer[index + stringSize - 1] !== 0) {
2908
2984
  throw new BSONError('bad string length in bson');
2909
2985
  }
2910
- const symbol = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
2986
+ const symbol = ByteUtils.toUTF8(buffer, index, index + stringSize - 1, shouldValidateKey);
2911
2987
  value = promoteValues ? symbol : new BSONSymbol(symbol);
2912
2988
  index = index + stringSize;
2913
2989
  }
@@ -2938,7 +3014,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2938
3014
  buffer[index + stringSize - 1] !== 0) {
2939
3015
  throw new BSONError('bad string length in bson');
2940
3016
  }
2941
- const functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
3017
+ const functionString = ByteUtils.toUTF8(buffer, index, index + stringSize - 1, shouldValidateKey);
2942
3018
  value = new Code(functionString);
2943
3019
  index = index + stringSize;
2944
3020
  }
@@ -2959,7 +3035,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2959
3035
  buffer[index + stringSize - 1] !== 0) {
2960
3036
  throw new BSONError('bad string length in bson');
2961
3037
  }
2962
- const functionString = getValidatedString(buffer, index, index + stringSize - 1, shouldValidateKey);
3038
+ const functionString = ByteUtils.toUTF8(buffer, index, index + stringSize - 1, shouldValidateKey);
2963
3039
  index = index + stringSize;
2964
3040
  const _index = index;
2965
3041
  const objectSize = buffer[index] |
@@ -2990,7 +3066,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2990
3066
  throw new BSONError('Invalid UTF-8 string in BSON document');
2991
3067
  }
2992
3068
  }
2993
- const namespace = ByteUtils.toUTF8(buffer, index, index + stringSize - 1);
3069
+ const namespace = ByteUtils.toUTF8(buffer, index, index + stringSize - 1, false);
2994
3070
  index = index + stringSize;
2995
3071
  const oidBuffer = ByteUtils.allocate(12);
2996
3072
  oidBuffer.set(buffer.subarray(index, index + 12), 0);
@@ -3029,20 +3105,6 @@ function deserializeObject(buffer, index, options, isArray = false) {
3029
3105
  }
3030
3106
  return object;
3031
3107
  }
3032
- function getValidatedString(buffer, start, end, shouldValidateUtf8) {
3033
- const value = ByteUtils.toUTF8(buffer, start, end);
3034
- if (shouldValidateUtf8) {
3035
- for (let i = 0; i < value.length; i++) {
3036
- if (value.charCodeAt(i) === 0xfffd) {
3037
- if (!validateUtf8(buffer, start, end)) {
3038
- throw new BSONError('Invalid UTF-8 string in BSON document');
3039
- }
3040
- break;
3041
- }
3042
- }
3043
- }
3044
- return value;
3045
- }
3046
3108
 
3047
3109
  const regexp = /\x00/;
3048
3110
  const ignoreKeys = new Set(['$db', '$ref', '$id', '$clusterTime']);