bson 4.5.1 → 4.6.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 (66) hide show
  1. package/HISTORY.md +572 -0
  2. package/bower.json +1 -1
  3. package/bson.d.ts +70 -13
  4. package/dist/bson.browser.esm.js +314 -177
  5. package/dist/bson.browser.esm.js.map +1 -1
  6. package/dist/bson.browser.umd.js +317 -176
  7. package/dist/bson.browser.umd.js.map +1 -1
  8. package/dist/bson.bundle.js +317 -176
  9. package/dist/bson.bundle.js.map +1 -1
  10. package/dist/bson.esm.js +314 -177
  11. package/dist/bson.esm.js.map +1 -1
  12. package/lib/binary.js +11 -9
  13. package/lib/binary.js.map +1 -1
  14. package/lib/bson.js +11 -3
  15. package/lib/bson.js.map +1 -1
  16. package/lib/code.js +0 -1
  17. package/lib/code.js.map +1 -1
  18. package/lib/constants.js +5 -1
  19. package/lib/constants.js.map +1 -1
  20. package/lib/db_ref.js +0 -1
  21. package/lib/db_ref.js.map +1 -1
  22. package/lib/decimal128.js +6 -5
  23. package/lib/decimal128.js.map +1 -1
  24. package/lib/double.js +3 -1
  25. package/lib/double.js.map +1 -1
  26. package/lib/ensure_buffer.js +3 -2
  27. package/lib/ensure_buffer.js.map +1 -1
  28. package/lib/error.js +55 -0
  29. package/lib/error.js.map +1 -0
  30. package/lib/extended_json.js +11 -5
  31. package/lib/extended_json.js.map +1 -1
  32. package/lib/int_32.js +4 -2
  33. package/lib/int_32.js.map +1 -1
  34. package/lib/objectid.js +39 -35
  35. package/lib/objectid.js.map +1 -1
  36. package/lib/parser/deserializer.js +131 -53
  37. package/lib/parser/deserializer.js.map +1 -1
  38. package/lib/parser/serializer.js +13 -12
  39. package/lib/parser/serializer.js.map +1 -1
  40. package/lib/regexp.js +9 -2
  41. package/lib/regexp.js.map +1 -1
  42. package/lib/symbol.js +0 -2
  43. package/lib/symbol.js.map +1 -1
  44. package/lib/uuid.js +4 -4
  45. package/lib/uuid.js.map +1 -1
  46. package/lib/uuid_utils.js +2 -1
  47. package/lib/uuid_utils.js.map +1 -1
  48. package/package.json +16 -3
  49. package/src/binary.ts +12 -10
  50. package/src/bson.ts +7 -1
  51. package/src/code.ts +0 -1
  52. package/src/constants.ts +6 -0
  53. package/src/db_ref.ts +0 -1
  54. package/src/decimal128.ts +6 -5
  55. package/src/double.ts +4 -1
  56. package/src/ensure_buffer.ts +3 -2
  57. package/src/error.ts +23 -0
  58. package/src/extended_json.ts +13 -5
  59. package/src/int_32.ts +5 -2
  60. package/src/objectid.ts +40 -43
  61. package/src/parser/deserializer.ts +159 -57
  62. package/src/parser/serializer.ts +13 -12
  63. package/src/regexp.ts +14 -2
  64. package/src/symbol.ts +0 -2
  65. package/src/uuid.ts +4 -4
  66. package/src/uuid_utils.ts +2 -1
@@ -7,6 +7,7 @@ import type { DBRefLike } from '../db_ref';
7
7
  import type { Decimal128 } from '../decimal128';
8
8
  import type { Double } from '../double';
9
9
  import { ensureBuffer } from '../ensure_buffer';
10
+ import { BSONError, BSONTypeError } from '../error';
10
11
  import { isBSONType } from '../extended_json';
11
12
  import { writeIEEE754 } from '../float_parser';
12
13
  import type { Int32 } from '../int_32';
@@ -311,7 +312,7 @@ function serializeObjectId(
311
312
  // browser polyfill
312
313
  buffer.set(value.id.subarray(0, 12), index);
313
314
  } else {
314
- throw new TypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
315
+ throw new BSONTypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
315
316
  }
316
317
 
317
318
  // Adjust index
@@ -363,7 +364,7 @@ function serializeObject(
363
364
  path: Document[] = []
364
365
  ) {
365
366
  for (let i = 0; i < path.length; i++) {
366
- if (path[i] === value) throw new Error('cyclic dependency detected');
367
+ if (path[i] === value) throw new BSONError('cyclic dependency detected');
367
368
  }
368
369
 
369
370
  // Push value to stack
@@ -767,7 +768,7 @@ export function serializeInto(
767
768
 
768
769
  // Is there an override value
769
770
  if (value && value.toBSON) {
770
- if (typeof value.toBSON !== 'function') throw new TypeError('toBSON is not a function');
771
+ if (typeof value.toBSON !== 'function') throw new BSONTypeError('toBSON is not a function');
771
772
  value = value.toBSON();
772
773
  }
773
774
 
@@ -776,7 +777,7 @@ export function serializeInto(
776
777
  } else if (typeof value === 'number') {
777
778
  index = serializeNumber(buffer, key, value, index, true);
778
779
  } else if (typeof value === 'bigint') {
779
- throw new TypeError('Unsupported type BigInt, please use Decimal128');
780
+ throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
780
781
  } else if (typeof value === 'boolean') {
781
782
  index = serializeBoolean(buffer, key, value, index, true);
782
783
  } else if (value instanceof Date || isDate(value)) {
@@ -841,7 +842,7 @@ export function serializeInto(
841
842
  } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
842
843
  index = serializeMinMax(buffer, key, value, index, true);
843
844
  } else if (typeof value['_bsontype'] !== 'undefined') {
844
- throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
845
+ throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
845
846
  }
846
847
  }
847
848
  } else if (object instanceof Map || isMap(object)) {
@@ -884,7 +885,7 @@ export function serializeInto(
884
885
  } else if (type === 'number') {
885
886
  index = serializeNumber(buffer, key, value, index);
886
887
  } else if (type === 'bigint' || isBigInt64Array(value) || isBigUInt64Array(value)) {
887
- throw new TypeError('Unsupported type BigInt, please use Decimal128');
888
+ throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
888
889
  } else if (type === 'boolean') {
889
890
  index = serializeBoolean(buffer, key, value, index);
890
891
  } else if (value instanceof Date || isDate(value)) {
@@ -942,16 +943,16 @@ export function serializeInto(
942
943
  } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
943
944
  index = serializeMinMax(buffer, key, value, index);
944
945
  } else if (typeof value['_bsontype'] !== 'undefined') {
945
- throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
946
+ throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
946
947
  }
947
948
  }
948
949
  } else {
949
950
  // Did we provide a custom serialization method
950
951
  if (object.toBSON) {
951
- if (typeof object.toBSON !== 'function') throw new TypeError('toBSON is not a function');
952
+ if (typeof object.toBSON !== 'function') throw new BSONTypeError('toBSON is not a function');
952
953
  object = object.toBSON();
953
954
  if (object != null && typeof object !== 'object')
954
- throw new TypeError('toBSON function did not return an object');
955
+ throw new BSONTypeError('toBSON function did not return an object');
955
956
  }
956
957
 
957
958
  // Iterate over all the keys
@@ -959,7 +960,7 @@ export function serializeInto(
959
960
  let value = object[key];
960
961
  // Is there an override value
961
962
  if (value && value.toBSON) {
962
- if (typeof value.toBSON !== 'function') throw new TypeError('toBSON is not a function');
963
+ if (typeof value.toBSON !== 'function') throw new BSONTypeError('toBSON is not a function');
963
964
  value = value.toBSON();
964
965
  }
965
966
 
@@ -988,7 +989,7 @@ export function serializeInto(
988
989
  } else if (type === 'number') {
989
990
  index = serializeNumber(buffer, key, value, index);
990
991
  } else if (type === 'bigint') {
991
- throw new TypeError('Unsupported type BigInt, please use Decimal128');
992
+ throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
992
993
  } else if (type === 'boolean') {
993
994
  index = serializeBoolean(buffer, key, value, index);
994
995
  } else if (value instanceof Date || isDate(value)) {
@@ -1048,7 +1049,7 @@ export function serializeInto(
1048
1049
  } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
1049
1050
  index = serializeMinMax(buffer, key, value, index);
1050
1051
  } else if (typeof value['_bsontype'] !== 'undefined') {
1051
- throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
1052
+ throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
1052
1053
  }
1053
1054
  }
1054
1055
  }
package/src/regexp.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { BSONError, BSONTypeError } from './error';
1
2
  import type { EJSONOptions } from './extended_json';
2
3
 
3
4
  function alphabetize(str: string): string {
@@ -37,6 +38,17 @@ export class BSONRegExp {
37
38
  this.pattern = pattern;
38
39
  this.options = alphabetize(options ?? '');
39
40
 
41
+ if (this.pattern.indexOf('\x00') !== -1) {
42
+ throw new BSONError(
43
+ `BSON Regex patterns cannot contain null bytes, found: ${JSON.stringify(this.pattern)}`
44
+ );
45
+ }
46
+ if (this.options.indexOf('\x00') !== -1) {
47
+ throw new BSONError(
48
+ `BSON Regex options cannot contain null bytes, found: ${JSON.stringify(this.options)}`
49
+ );
50
+ }
51
+
40
52
  // Validate options
41
53
  for (let i = 0; i < this.options.length; i++) {
42
54
  if (
@@ -49,7 +61,7 @@ export class BSONRegExp {
49
61
  this.options[i] === 'u'
50
62
  )
51
63
  ) {
52
- throw new Error(`The regular expression option [${this.options[i]}] is not supported`);
64
+ throw new BSONError(`The regular expression option [${this.options[i]}] is not supported`);
53
65
  }
54
66
  }
55
67
  }
@@ -85,7 +97,7 @@ export class BSONRegExp {
85
97
  BSONRegExp.parseOptions(doc.$regularExpression.options)
86
98
  );
87
99
  }
88
- throw new TypeError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
100
+ throw new BSONTypeError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
89
101
  }
90
102
  }
91
103
 
package/src/symbol.ts CHANGED
@@ -25,7 +25,6 @@ export class BSONSymbol {
25
25
  return this.value;
26
26
  }
27
27
 
28
- /** @internal */
29
28
  toString(): string {
30
29
  return this.value;
31
30
  }
@@ -35,7 +34,6 @@ export class BSONSymbol {
35
34
  return `new BSONSymbol("${this.value}")`;
36
35
  }
37
36
 
38
- /** @internal */
39
37
  toJSON(): string {
40
38
  return this.value;
41
39
  }
package/src/uuid.ts CHANGED
@@ -3,6 +3,7 @@ import { ensureBuffer } from './ensure_buffer';
3
3
  import { Binary } from './binary';
4
4
  import { bufferToUuidHexString, uuidHexStringToBuffer, uuidValidateString } from './uuid_utils';
5
5
  import { isUint8Array, randomBytes } from './parser/utils';
6
+ import { BSONTypeError } from './error';
6
7
 
7
8
  /** @public */
8
9
  export type UUIDExtended = {
@@ -45,7 +46,7 @@ export class UUID {
45
46
  } else if (typeof input === 'string') {
46
47
  this.id = uuidHexStringToBuffer(input);
47
48
  } else {
48
- throw new TypeError(
49
+ throw new BSONTypeError(
49
50
  'Argument passed in UUID constructor must be a UUID, a 16 byte Buffer or a 32/36 character hex string (dashes excluded/included, format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'
50
51
  );
51
52
  }
@@ -91,15 +92,14 @@ export class UUID {
91
92
 
92
93
  /**
93
94
  * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
94
- * @internal
95
95
  */
96
96
  toString(encoding?: string): string {
97
97
  return encoding ? this.id.toString(encoding) : this.toHexString();
98
98
  }
99
99
 
100
100
  /**
101
- * Converts the id into its JSON string representation. A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
102
- * @internal
101
+ * Converts the id into its JSON string representation.
102
+ * A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
103
103
  */
104
104
  toJSON(): string {
105
105
  return this.toHexString();
package/src/uuid_utils.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Buffer } from 'buffer';
2
+ import { BSONTypeError } from './error';
2
3
 
3
4
  // Validation regex for v4 uuid (validates with or without dashes)
4
5
  const VALIDATION_REGEX =
@@ -9,7 +10,7 @@ export const uuidValidateString = (str: string): boolean =>
9
10
 
10
11
  export const uuidHexStringToBuffer = (hexString: string): Buffer => {
11
12
  if (!uuidValidateString(hexString)) {
12
- throw new TypeError(
13
+ throw new BSONTypeError(
13
14
  'UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".'
14
15
  );
15
16
  }