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/lib/bson.cjs CHANGED
@@ -16,7 +16,7 @@ function isDate(d) {
16
16
  return Object.prototype.toString.call(d) === '[object Date]';
17
17
  }
18
18
 
19
- const BSON_MAJOR_VERSION = 5;
19
+ const BSON_MAJOR_VERSION = 6;
20
20
  const BSON_INT32_MAX = 0x7fffffff;
21
21
  const BSON_INT32_MIN = -0x80000000;
22
22
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -165,8 +165,8 @@ const nodeJsByteUtils = {
165
165
  fromUTF8(text) {
166
166
  return Buffer.from(text, 'utf8');
167
167
  },
168
- toUTF8(buffer) {
169
- return nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8');
168
+ toUTF8(buffer, start, end) {
169
+ return nodeJsByteUtils.toLocalBufferType(buffer).toString('utf8', start, end);
170
170
  },
171
171
  utf8ByteLength(input) {
172
172
  return Buffer.byteLength(input, 'utf8');
@@ -276,8 +276,8 @@ const webByteUtils = {
276
276
  fromUTF8(text) {
277
277
  return new TextEncoder().encode(text);
278
278
  },
279
- toUTF8(uint8array) {
280
- return new TextDecoder('utf8', { fatal: false }).decode(uint8array);
279
+ toUTF8(uint8array, start, end) {
280
+ return new TextDecoder('utf8', { fatal: false }).decode(uint8array.slice(start, end));
281
281
  },
282
282
  utf8ByteLength(input) {
283
283
  return webByteUtils.fromUTF8(input).byteLength;
@@ -311,11 +311,11 @@ class Binary extends BSONValue {
311
311
  constructor(buffer, subType) {
312
312
  super();
313
313
  if (!(buffer == null) &&
314
- !(typeof buffer === 'string') &&
314
+ typeof buffer === 'string' &&
315
315
  !ArrayBuffer.isView(buffer) &&
316
- !(buffer instanceof ArrayBuffer) &&
316
+ !isAnyArrayBuffer(buffer) &&
317
317
  !Array.isArray(buffer)) {
318
- throw new BSONError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
318
+ throw new BSONError('Binary can only be constructed from Uint8Array or number[]');
319
319
  }
320
320
  this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
321
321
  if (buffer == null) {
@@ -323,15 +323,9 @@ class Binary extends BSONValue {
323
323
  this.position = 0;
324
324
  }
325
325
  else {
326
- if (typeof buffer === 'string') {
327
- this.buffer = ByteUtils.fromISO88591(buffer);
328
- }
329
- else if (Array.isArray(buffer)) {
330
- this.buffer = ByteUtils.fromNumberArray(buffer);
331
- }
332
- else {
333
- this.buffer = ByteUtils.toLocalBufferType(buffer);
334
- }
326
+ this.buffer = Array.isArray(buffer)
327
+ ? ByteUtils.fromNumberArray(buffer)
328
+ : ByteUtils.toLocalBufferType(buffer);
335
329
  this.position = this.buffer.byteLength;
336
330
  }
337
331
  }
@@ -377,25 +371,17 @@ class Binary extends BSONValue {
377
371
  offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
378
372
  }
379
373
  else if (typeof sequence === 'string') {
380
- const bytes = ByteUtils.fromISO88591(sequence);
381
- this.buffer.set(bytes, offset);
382
- this.position =
383
- offset + sequence.length > this.position ? offset + sequence.length : this.position;
374
+ throw new BSONError('input cannot be string');
384
375
  }
385
376
  }
386
377
  read(position, length) {
387
378
  length = length && length > 0 ? length : this.position;
388
379
  return this.buffer.slice(position, position + length);
389
380
  }
390
- value(asRaw) {
391
- asRaw = !!asRaw;
392
- if (asRaw && this.buffer.length === this.position) {
393
- return this.buffer;
394
- }
395
- if (asRaw) {
396
- return this.buffer.slice(0, this.position);
397
- }
398
- return ByteUtils.toISO88591(this.buffer.subarray(0, this.position));
381
+ value() {
382
+ return this.buffer.length === this.position
383
+ ? this.buffer
384
+ : this.buffer.subarray(0, this.position);
399
385
  }
400
386
  length() {
401
387
  return this.position;
@@ -409,8 +395,8 @@ class Binary extends BSONValue {
409
395
  if (encoding === 'base64')
410
396
  return ByteUtils.toBase64(this.buffer);
411
397
  if (encoding === 'utf8' || encoding === 'utf-8')
412
- return ByteUtils.toUTF8(this.buffer);
413
- return ByteUtils.toUTF8(this.buffer);
398
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
399
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
414
400
  }
415
401
  toExtendedJSON(options) {
416
402
  options = options || {};
@@ -596,7 +582,6 @@ class UUID extends Binary {
596
582
  return `new UUID("${this.toHexString()}")`;
597
583
  }
598
584
  }
599
- UUID.cacheHexString = false;
600
585
 
601
586
  class Code extends BSONValue {
602
587
  get _bsontype() {
@@ -1990,20 +1975,11 @@ class ObjectId extends BSONValue {
1990
1975
  this[kId] = ByteUtils.toLocalBufferType(workingId);
1991
1976
  }
1992
1977
  else if (typeof workingId === 'string') {
1993
- if (workingId.length === 12) {
1994
- const bytes = ByteUtils.fromUTF8(workingId);
1995
- if (bytes.byteLength === 12) {
1996
- this[kId] = bytes;
1997
- }
1998
- else {
1999
- throw new BSONError('Argument passed in must be a string of 12 bytes');
2000
- }
2001
- }
2002
- else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
1978
+ if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2003
1979
  this[kId] = ByteUtils.fromHex(workingId);
2004
1980
  }
2005
1981
  else {
2006
- throw new BSONError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
1982
+ throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
2007
1983
  }
2008
1984
  }
2009
1985
  else {
@@ -2065,30 +2041,25 @@ class ObjectId extends BSONValue {
2065
2041
  toJSON() {
2066
2042
  return this.toHexString();
2067
2043
  }
2044
+ static is(variable) {
2045
+ return (variable != null &&
2046
+ typeof variable === 'object' &&
2047
+ '_bsontype' in variable &&
2048
+ variable._bsontype === 'ObjectId');
2049
+ }
2068
2050
  equals(otherId) {
2069
2051
  if (otherId === undefined || otherId === null) {
2070
2052
  return false;
2071
2053
  }
2072
- if (otherId instanceof ObjectId) {
2054
+ if (ObjectId.is(otherId)) {
2073
2055
  return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
2074
2056
  }
2075
- if (typeof otherId === 'string' &&
2076
- ObjectId.isValid(otherId) &&
2077
- otherId.length === 12 &&
2078
- isUint8Array(this.id)) {
2079
- return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
2080
- }
2081
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
2057
+ if (typeof otherId === 'string') {
2082
2058
  return otherId.toLowerCase() === this.toHexString();
2083
2059
  }
2084
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
2085
- return ByteUtils.equals(ByteUtils.fromUTF8(otherId), this.id);
2086
- }
2087
- if (typeof otherId === 'object' &&
2088
- 'toHexString' in otherId &&
2089
- typeof otherId.toHexString === 'function') {
2060
+ if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') {
2090
2061
  const otherIdString = otherId.toHexString();
2091
- const thisIdString = this.toHexString().toLowerCase();
2062
+ const thisIdString = this.toHexString();
2092
2063
  return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
2093
2064
  }
2094
2065
  return false;
@@ -2416,19 +2387,21 @@ class Timestamp extends LongWithoutOverridesClass {
2416
2387
  if (typeof low.i !== 'number' && (typeof low.i !== 'object' || low.i._bsontype !== 'Int32')) {
2417
2388
  throw new BSONError('Timestamp constructed from { t, i } must provide i as a number');
2418
2389
  }
2419
- if (low.t < 0) {
2390
+ const t = Number(low.t);
2391
+ const i = Number(low.i);
2392
+ if (t < 0 || Number.isNaN(t)) {
2420
2393
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive t');
2421
2394
  }
2422
- if (low.i < 0) {
2395
+ if (i < 0 || Number.isNaN(i)) {
2423
2396
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2424
2397
  }
2425
- if (low.t > 4294967295) {
2398
+ if (t > 4294967295) {
2426
2399
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2427
2400
  }
2428
- if (low.i > 4294967295) {
2401
+ if (i > 4294967295) {
2429
2402
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2430
2403
  }
2431
- super(low.i.valueOf(), low.t.valueOf(), true);
2404
+ super(i, t, true);
2432
2405
  }
2433
2406
  else {
2434
2407
  throw new BSONError('A Timestamp can only be constructed with: bigint, Long, or { t: number; i: number }');
@@ -2600,7 +2573,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2600
2573
  }
2601
2574
  if (i >= buffer.byteLength)
2602
2575
  throw new BSONError('Bad BSON Document: illegal CString');
2603
- const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer.subarray(index, i));
2576
+ const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer, index, i);
2604
2577
  let shouldValidateKey = true;
2605
2578
  if (globalUTFValidation || utf8KeysSet.has(name)) {
2606
2579
  shouldValidateKey = validationSetting;
@@ -2815,7 +2788,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2815
2788
  }
2816
2789
  if (i >= buffer.length)
2817
2790
  throw new BSONError('Bad BSON Document: illegal CString');
2818
- const source = ByteUtils.toUTF8(buffer.subarray(index, i));
2791
+ const source = ByteUtils.toUTF8(buffer, index, i);
2819
2792
  index = i + 1;
2820
2793
  i = index;
2821
2794
  while (buffer[i] !== 0x00 && i < buffer.length) {
@@ -2823,7 +2796,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2823
2796
  }
2824
2797
  if (i >= buffer.length)
2825
2798
  throw new BSONError('Bad BSON Document: illegal CString');
2826
- const regExpOptions = ByteUtils.toUTF8(buffer.subarray(index, i));
2799
+ const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
2827
2800
  index = i + 1;
2828
2801
  const optionsArray = new Array(regExpOptions.length);
2829
2802
  for (i = 0; i < regExpOptions.length; i++) {
@@ -2848,7 +2821,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2848
2821
  }
2849
2822
  if (i >= buffer.length)
2850
2823
  throw new BSONError('Bad BSON Document: illegal CString');
2851
- const source = ByteUtils.toUTF8(buffer.subarray(index, i));
2824
+ const source = ByteUtils.toUTF8(buffer, index, i);
2852
2825
  index = i + 1;
2853
2826
  i = index;
2854
2827
  while (buffer[i] !== 0x00 && i < buffer.length) {
@@ -2856,7 +2829,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2856
2829
  }
2857
2830
  if (i >= buffer.length)
2858
2831
  throw new BSONError('Bad BSON Document: illegal CString');
2859
- const regExpOptions = ByteUtils.toUTF8(buffer.subarray(index, i));
2832
+ const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
2860
2833
  index = i + 1;
2861
2834
  value = new BSONRegExp(source, regExpOptions);
2862
2835
  }
@@ -2953,7 +2926,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2953
2926
  throw new BSONError('Invalid UTF-8 string in BSON document');
2954
2927
  }
2955
2928
  }
2956
- const namespace = ByteUtils.toUTF8(buffer.subarray(index, index + stringSize - 1));
2929
+ const namespace = ByteUtils.toUTF8(buffer, index, index + stringSize - 1);
2957
2930
  index = index + stringSize;
2958
2931
  const oidBuffer = ByteUtils.allocate(12);
2959
2932
  oidBuffer.set(buffer.subarray(index, index + 12), 0);
@@ -2993,7 +2966,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2993
2966
  return object;
2994
2967
  }
2995
2968
  function getValidatedString(buffer, start, end, shouldValidateUtf8) {
2996
- const value = ByteUtils.toUTF8(buffer.subarray(start, end));
2969
+ const value = ByteUtils.toUTF8(buffer, start, end);
2997
2970
  if (shouldValidateUtf8) {
2998
2971
  for (let i = 0; i < value.length; i++) {
2999
2972
  if (value.charCodeAt(i) === 0xfffd) {