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