bson 4.5.2 → 4.6.1

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 (56) hide show
  1. package/bower.json +1 -1
  2. package/bson.d.ts +39 -3
  3. package/dist/bson.browser.esm.js +320 -187
  4. package/dist/bson.browser.esm.js.map +1 -1
  5. package/dist/bson.browser.umd.js +323 -186
  6. package/dist/bson.browser.umd.js.map +1 -1
  7. package/dist/bson.bundle.js +323 -186
  8. package/dist/bson.bundle.js.map +1 -1
  9. package/dist/bson.esm.js +320 -187
  10. package/dist/bson.esm.js.map +1 -1
  11. package/lib/binary.js +11 -6
  12. package/lib/binary.js.map +1 -1
  13. package/lib/bson.js +11 -3
  14. package/lib/bson.js.map +1 -1
  15. package/lib/constants.js +5 -1
  16. package/lib/constants.js.map +1 -1
  17. package/lib/decimal128.js +13 -5
  18. package/lib/decimal128.js.map +1 -1
  19. package/lib/ensure_buffer.js +3 -2
  20. package/lib/ensure_buffer.js.map +1 -1
  21. package/lib/error.js +55 -0
  22. package/lib/error.js.map +1 -0
  23. package/lib/extended_json.js +11 -5
  24. package/lib/extended_json.js.map +1 -1
  25. package/lib/int_32.js +1 -1
  26. package/lib/int_32.js.map +1 -1
  27. package/lib/objectid.js +42 -47
  28. package/lib/objectid.js.map +1 -1
  29. package/lib/parser/calculate_size.js +2 -2
  30. package/lib/parser/calculate_size.js.map +1 -1
  31. package/lib/parser/deserializer.js +131 -53
  32. package/lib/parser/deserializer.js.map +1 -1
  33. package/lib/parser/serializer.js +16 -20
  34. package/lib/parser/serializer.js.map +1 -1
  35. package/lib/regexp.js +9 -2
  36. package/lib/regexp.js.map +1 -1
  37. package/lib/uuid.js +2 -1
  38. package/lib/uuid.js.map +1 -1
  39. package/lib/uuid_utils.js +2 -1
  40. package/lib/uuid_utils.js.map +1 -1
  41. package/package.json +4 -2
  42. package/src/binary.ts +11 -6
  43. package/src/bson.ts +7 -1
  44. package/src/constants.ts +6 -0
  45. package/src/decimal128.ts +12 -5
  46. package/src/ensure_buffer.ts +3 -2
  47. package/src/error.ts +23 -0
  48. package/src/extended_json.ts +13 -5
  49. package/src/int_32.ts +1 -1
  50. package/src/objectid.ts +44 -62
  51. package/src/parser/calculate_size.ts +2 -2
  52. package/src/parser/deserializer.ts +159 -57
  53. package/src/parser/serializer.ts +16 -17
  54. package/src/regexp.ts +14 -2
  55. package/src/uuid.ts +2 -1
  56. 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
@@ -766,8 +767,7 @@ export function serializeInto(
766
767
  let value = object[i];
767
768
 
768
769
  // Is there an override value
769
- if (value && value.toBSON) {
770
- if (typeof value.toBSON !== 'function') throw new TypeError('toBSON is not a function');
770
+ if (typeof value?.toBSON === 'function') {
771
771
  value = value.toBSON();
772
772
  }
773
773
 
@@ -776,7 +776,7 @@ export function serializeInto(
776
776
  } else if (typeof value === 'number') {
777
777
  index = serializeNumber(buffer, key, value, index, true);
778
778
  } else if (typeof value === 'bigint') {
779
- throw new TypeError('Unsupported type BigInt, please use Decimal128');
779
+ throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
780
780
  } else if (typeof value === 'boolean') {
781
781
  index = serializeBoolean(buffer, key, value, index, true);
782
782
  } else if (value instanceof Date || isDate(value)) {
@@ -841,7 +841,7 @@ export function serializeInto(
841
841
  } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
842
842
  index = serializeMinMax(buffer, key, value, index, true);
843
843
  } else if (typeof value['_bsontype'] !== 'undefined') {
844
- throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
844
+ throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
845
845
  }
846
846
  }
847
847
  } else if (object instanceof Map || isMap(object)) {
@@ -884,7 +884,7 @@ export function serializeInto(
884
884
  } else if (type === 'number') {
885
885
  index = serializeNumber(buffer, key, value, index);
886
886
  } else if (type === 'bigint' || isBigInt64Array(value) || isBigUInt64Array(value)) {
887
- throw new TypeError('Unsupported type BigInt, please use Decimal128');
887
+ throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
888
888
  } else if (type === 'boolean') {
889
889
  index = serializeBoolean(buffer, key, value, index);
890
890
  } else if (value instanceof Date || isDate(value)) {
@@ -942,24 +942,23 @@ export function serializeInto(
942
942
  } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
943
943
  index = serializeMinMax(buffer, key, value, index);
944
944
  } else if (typeof value['_bsontype'] !== 'undefined') {
945
- throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
945
+ throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
946
946
  }
947
947
  }
948
948
  } else {
949
- // Did we provide a custom serialization method
950
- if (object.toBSON) {
951
- if (typeof object.toBSON !== 'function') throw new TypeError('toBSON is not a function');
949
+ if (typeof object?.toBSON === 'function') {
950
+ // Provided a custom serialization method
952
951
  object = object.toBSON();
953
- if (object != null && typeof object !== 'object')
954
- throw new TypeError('toBSON function did not return an object');
952
+ if (object != null && typeof object !== 'object') {
953
+ throw new BSONTypeError('toBSON function did not return an object');
954
+ }
955
955
  }
956
956
 
957
957
  // Iterate over all the keys
958
958
  for (const key in object) {
959
959
  let value = object[key];
960
960
  // Is there an override value
961
- if (value && value.toBSON) {
962
- if (typeof value.toBSON !== 'function') throw new TypeError('toBSON is not a function');
961
+ if (typeof value?.toBSON === 'function') {
963
962
  value = value.toBSON();
964
963
  }
965
964
 
@@ -988,7 +987,7 @@ export function serializeInto(
988
987
  } else if (type === 'number') {
989
988
  index = serializeNumber(buffer, key, value, index);
990
989
  } else if (type === 'bigint') {
991
- throw new TypeError('Unsupported type BigInt, please use Decimal128');
990
+ throw new BSONTypeError('Unsupported type BigInt, please use Decimal128');
992
991
  } else if (type === 'boolean') {
993
992
  index = serializeBoolean(buffer, key, value, index);
994
993
  } else if (value instanceof Date || isDate(value)) {
@@ -1048,7 +1047,7 @@ export function serializeInto(
1048
1047
  } else if (value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
1049
1048
  index = serializeMinMax(buffer, key, value, index);
1050
1049
  } else if (typeof value['_bsontype'] !== 'undefined') {
1051
- throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
1050
+ throw new BSONTypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
1052
1051
  }
1053
1052
  }
1054
1053
  }
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/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
  }
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
  }