bson 5.3.0 → 5.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.
Files changed (67) hide show
  1. package/README.md +4 -11
  2. package/lib/bson.bundle.js +20 -18
  3. package/lib/bson.bundle.js.map +1 -1
  4. package/lib/bson.cjs +20 -18
  5. package/lib/bson.cjs.map +1 -1
  6. package/lib/bson.mjs +20 -18
  7. package/lib/bson.mjs.map +1 -1
  8. package/lib/bson.rn.cjs +4110 -0
  9. package/lib/bson.rn.cjs.map +1 -0
  10. package/package.json +8 -7
  11. package/src/binary.ts +3 -2
  12. package/src/bson.ts +2 -2
  13. package/src/parser/deserializer.ts +8 -8
  14. package/src/timestamp.ts +7 -5
  15. package/src/utils/byte_utils.ts +1 -1
  16. package/src/utils/node_byte_utils.ts +3 -3
  17. package/src/utils/web_byte_utils.ts +2 -2
  18. package/vendor/base64/LICENSE-MIT.txt +20 -0
  19. package/vendor/base64/README.md +112 -0
  20. package/vendor/base64/base64.js +157 -0
  21. package/vendor/base64/package.json +43 -0
  22. package/vendor/text-encoding/LICENSE.md +237 -0
  23. package/vendor/text-encoding/README.md +111 -0
  24. package/vendor/text-encoding/index.js +9 -0
  25. package/vendor/text-encoding/lib/encoding-indexes.js +47 -0
  26. package/vendor/text-encoding/lib/encoding.js +3301 -0
  27. package/vendor/text-encoding/package.json +37 -0
  28. package/lib/binary.d.ts +0 -182
  29. package/lib/binary.d.ts.map +0 -1
  30. package/lib/bson.d.ts +0 -97
  31. package/lib/bson.d.ts.map +0 -1
  32. package/lib/bson_value.d.ts +0 -10
  33. package/lib/bson_value.d.ts.map +0 -1
  34. package/lib/code.d.ts +0 -32
  35. package/lib/code.d.ts.map +0 -1
  36. package/lib/constants.d.ts +0 -107
  37. package/lib/constants.d.ts.map +0 -1
  38. package/lib/db_ref.d.ts +0 -40
  39. package/lib/db_ref.d.ts.map +0 -1
  40. package/lib/decimal128.d.ts +0 -34
  41. package/lib/decimal128.d.ts.map +0 -1
  42. package/lib/double.d.ts +0 -35
  43. package/lib/double.d.ts.map +0 -1
  44. package/lib/error.d.ts +0 -50
  45. package/lib/error.d.ts.map +0 -1
  46. package/lib/extended_json.d.ts +0 -82
  47. package/lib/extended_json.d.ts.map +0 -1
  48. package/lib/index.d.ts +0 -4
  49. package/lib/index.d.ts.map +0 -1
  50. package/lib/int_32.d.ts +0 -35
  51. package/lib/int_32.d.ts.map +0 -1
  52. package/lib/long.d.ts +0 -323
  53. package/lib/long.d.ts.map +0 -1
  54. package/lib/max_key.d.ts +0 -19
  55. package/lib/max_key.d.ts.map +0 -1
  56. package/lib/min_key.d.ts +0 -19
  57. package/lib/min_key.d.ts.map +0 -1
  58. package/lib/objectid.d.ts +0 -96
  59. package/lib/objectid.d.ts.map +0 -1
  60. package/lib/regexp.d.ts +0 -36
  61. package/lib/regexp.d.ts.map +0 -1
  62. package/lib/symbol.d.ts +0 -28
  63. package/lib/symbol.d.ts.map +0 -1
  64. package/lib/timestamp.d.ts +0 -66
  65. package/lib/timestamp.d.ts.map +0 -1
  66. package/lib/validate_utf8.d.ts +0 -10
  67. package/lib/validate_utf8.d.ts.map +0 -1
package/README.md CHANGED
@@ -188,23 +188,16 @@ try {
188
188
 
189
189
  ## React Native
190
190
 
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:
191
+ 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:
192
+
192
193
  ```sh
193
- npm install --save react-native-get-random-values text-encoding-polyfill base-64
194
+ npm install --save react-native-get-random-values
194
195
  ```
195
196
 
196
197
  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
198
 
198
199
  ```typescript
199
200
  // 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
201
  import 'react-native-get-random-values';
209
202
  ```
210
203
 
@@ -214,7 +207,7 @@ Finally, import the `BSON` library like so:
214
207
  import { BSON, EJSON } from 'bson';
215
208
  ```
216
209
 
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).)
210
+ 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
211
 
219
212
  ### Technical Note about React Native module import
220
213
 
@@ -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;
@@ -410,8 +410,8 @@ class Binary extends BSONValue {
410
410
  if (encoding === 'base64')
411
411
  return ByteUtils.toBase64(this.buffer);
412
412
  if (encoding === 'utf8' || encoding === 'utf-8')
413
- return ByteUtils.toUTF8(this.buffer);
414
- return ByteUtils.toUTF8(this.buffer);
413
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
414
+ return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength);
415
415
  }
416
416
  toExtendedJSON(options) {
417
417
  options = options || {};
@@ -2417,19 +2417,21 @@ class Timestamp extends LongWithoutOverridesClass {
2417
2417
  if (typeof low.i !== 'number' && (typeof low.i !== 'object' || low.i._bsontype !== 'Int32')) {
2418
2418
  throw new BSONError('Timestamp constructed from { t, i } must provide i as a number');
2419
2419
  }
2420
- if (low.t < 0) {
2420
+ const t = Number(low.t);
2421
+ const i = Number(low.i);
2422
+ if (t < 0 || Number.isNaN(t)) {
2421
2423
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive t');
2422
2424
  }
2423
- if (low.i < 0) {
2425
+ if (i < 0 || Number.isNaN(i)) {
2424
2426
  throw new BSONError('Timestamp constructed from { t, i } must provide a positive i');
2425
2427
  }
2426
- if (low.t > 4294967295) {
2428
+ if (t > 4294967295) {
2427
2429
  throw new BSONError('Timestamp constructed from { t, i } must provide t equal or less than uint32 max');
2428
2430
  }
2429
- if (low.i > 4294967295) {
2431
+ if (i > 4294967295) {
2430
2432
  throw new BSONError('Timestamp constructed from { t, i } must provide i equal or less than uint32 max');
2431
2433
  }
2432
- super(low.i.valueOf(), low.t.valueOf(), true);
2434
+ super(i, t, true);
2433
2435
  }
2434
2436
  else {
2435
2437
  throw new BSONError('A Timestamp can only be constructed with: bigint, Long, or { t: number; i: number }');
@@ -2601,7 +2603,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2601
2603
  }
2602
2604
  if (i >= buffer.byteLength)
2603
2605
  throw new BSONError('Bad BSON Document: illegal CString');
2604
- const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer.subarray(index, i));
2606
+ const name = isArray ? arrayIndex++ : ByteUtils.toUTF8(buffer, index, i);
2605
2607
  let shouldValidateKey = true;
2606
2608
  if (globalUTFValidation || utf8KeysSet.has(name)) {
2607
2609
  shouldValidateKey = validationSetting;
@@ -2816,7 +2818,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2816
2818
  }
2817
2819
  if (i >= buffer.length)
2818
2820
  throw new BSONError('Bad BSON Document: illegal CString');
2819
- const source = ByteUtils.toUTF8(buffer.subarray(index, i));
2821
+ const source = ByteUtils.toUTF8(buffer, index, i);
2820
2822
  index = i + 1;
2821
2823
  i = index;
2822
2824
  while (buffer[i] !== 0x00 && i < buffer.length) {
@@ -2824,7 +2826,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2824
2826
  }
2825
2827
  if (i >= buffer.length)
2826
2828
  throw new BSONError('Bad BSON Document: illegal CString');
2827
- const regExpOptions = ByteUtils.toUTF8(buffer.subarray(index, i));
2829
+ const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
2828
2830
  index = i + 1;
2829
2831
  const optionsArray = new Array(regExpOptions.length);
2830
2832
  for (i = 0; i < regExpOptions.length; i++) {
@@ -2849,7 +2851,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2849
2851
  }
2850
2852
  if (i >= buffer.length)
2851
2853
  throw new BSONError('Bad BSON Document: illegal CString');
2852
- const source = ByteUtils.toUTF8(buffer.subarray(index, i));
2854
+ const source = ByteUtils.toUTF8(buffer, index, i);
2853
2855
  index = i + 1;
2854
2856
  i = index;
2855
2857
  while (buffer[i] !== 0x00 && i < buffer.length) {
@@ -2857,7 +2859,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2857
2859
  }
2858
2860
  if (i >= buffer.length)
2859
2861
  throw new BSONError('Bad BSON Document: illegal CString');
2860
- const regExpOptions = ByteUtils.toUTF8(buffer.subarray(index, i));
2862
+ const regExpOptions = ByteUtils.toUTF8(buffer, index, i);
2861
2863
  index = i + 1;
2862
2864
  value = new BSONRegExp(source, regExpOptions);
2863
2865
  }
@@ -2954,7 +2956,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2954
2956
  throw new BSONError('Invalid UTF-8 string in BSON document');
2955
2957
  }
2956
2958
  }
2957
- const namespace = ByteUtils.toUTF8(buffer.subarray(index, index + stringSize - 1));
2959
+ const namespace = ByteUtils.toUTF8(buffer, index, index + stringSize - 1);
2958
2960
  index = index + stringSize;
2959
2961
  const oidBuffer = ByteUtils.allocate(12);
2960
2962
  oidBuffer.set(buffer.subarray(index, index + 12), 0);
@@ -2994,7 +2996,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2994
2996
  return object;
2995
2997
  }
2996
2998
  function getValidatedString(buffer, start, end, shouldValidateUtf8) {
2997
- const value = ByteUtils.toUTF8(buffer.subarray(start, end));
2999
+ const value = ByteUtils.toUTF8(buffer, start, end);
2998
3000
  if (shouldValidateUtf8) {
2999
3001
  for (let i = 0; i < value.length; i++) {
3000
3002
  if (value.charCodeAt(i) === 0xfffd) {