bson 4.6.5 → 5.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 (101) hide show
  1. package/bson.d.ts +208 -267
  2. package/lib/bson.bundle.js +4033 -0
  3. package/lib/bson.bundle.js.map +1 -0
  4. package/lib/bson.cjs +4028 -0
  5. package/lib/bson.cjs.map +1 -0
  6. package/lib/bson.mjs +4002 -0
  7. package/lib/bson.mjs.map +1 -0
  8. package/package.json +49 -61
  9. package/src/binary.ts +235 -37
  10. package/src/bson.ts +43 -129
  11. package/src/code.ts +24 -14
  12. package/src/constants.ts +28 -0
  13. package/src/db_ref.ts +13 -8
  14. package/src/decimal128.ts +31 -25
  15. package/src/double.ts +7 -5
  16. package/src/error.ts +0 -2
  17. package/src/extended_json.ts +148 -148
  18. package/src/index.ts +19 -0
  19. package/src/int_32.ts +7 -5
  20. package/src/long.ts +16 -16
  21. package/src/max_key.ts +6 -6
  22. package/src/min_key.ts +6 -6
  23. package/src/objectid.ts +41 -74
  24. package/src/parser/calculate_size.ts +39 -63
  25. package/src/parser/deserializer.ts +47 -113
  26. package/src/parser/serializer.ts +234 -341
  27. package/src/parser/utils.ts +1 -99
  28. package/src/regexp.ts +16 -5
  29. package/src/symbol.ts +7 -5
  30. package/src/timestamp.ts +62 -27
  31. package/src/utils/byte_utils.ts +61 -0
  32. package/src/utils/node_byte_utils.ts +141 -0
  33. package/src/utils/web_byte_utils.ts +190 -0
  34. package/src/uuid_utils.ts +15 -15
  35. package/bower.json +0 -26
  36. package/dist/bson.browser.esm.js +0 -7471
  37. package/dist/bson.browser.esm.js.map +0 -1
  38. package/dist/bson.browser.umd.js +0 -7538
  39. package/dist/bson.browser.umd.js.map +0 -1
  40. package/dist/bson.bundle.js +0 -7537
  41. package/dist/bson.bundle.js.map +0 -1
  42. package/dist/bson.esm.js +0 -5437
  43. package/dist/bson.esm.js.map +0 -1
  44. package/lib/binary.js +0 -247
  45. package/lib/binary.js.map +0 -1
  46. package/lib/bson.js +0 -265
  47. package/lib/bson.js.map +0 -1
  48. package/lib/code.js +0 -46
  49. package/lib/code.js.map +0 -1
  50. package/lib/constants.js +0 -82
  51. package/lib/constants.js.map +0 -1
  52. package/lib/db_ref.js +0 -97
  53. package/lib/db_ref.js.map +0 -1
  54. package/lib/decimal128.js +0 -669
  55. package/lib/decimal128.js.map +0 -1
  56. package/lib/double.js +0 -76
  57. package/lib/double.js.map +0 -1
  58. package/lib/ensure_buffer.js +0 -25
  59. package/lib/ensure_buffer.js.map +0 -1
  60. package/lib/error.js +0 -55
  61. package/lib/error.js.map +0 -1
  62. package/lib/extended_json.js +0 -390
  63. package/lib/extended_json.js.map +0 -1
  64. package/lib/int_32.js +0 -58
  65. package/lib/int_32.js.map +0 -1
  66. package/lib/long.js +0 -900
  67. package/lib/long.js.map +0 -1
  68. package/lib/map.js +0 -123
  69. package/lib/map.js.map +0 -1
  70. package/lib/max_key.js +0 -33
  71. package/lib/max_key.js.map +0 -1
  72. package/lib/min_key.js +0 -33
  73. package/lib/min_key.js.map +0 -1
  74. package/lib/objectid.js +0 -299
  75. package/lib/objectid.js.map +0 -1
  76. package/lib/parser/calculate_size.js +0 -194
  77. package/lib/parser/calculate_size.js.map +0 -1
  78. package/lib/parser/deserializer.js +0 -659
  79. package/lib/parser/deserializer.js.map +0 -1
  80. package/lib/parser/serializer.js +0 -867
  81. package/lib/parser/serializer.js.map +0 -1
  82. package/lib/parser/utils.js +0 -115
  83. package/lib/parser/utils.js.map +0 -1
  84. package/lib/regexp.js +0 -74
  85. package/lib/regexp.js.map +0 -1
  86. package/lib/symbol.js +0 -48
  87. package/lib/symbol.js.map +0 -1
  88. package/lib/timestamp.js +0 -102
  89. package/lib/timestamp.js.map +0 -1
  90. package/lib/utils/global.js +0 -18
  91. package/lib/utils/global.js.map +0 -1
  92. package/lib/uuid.js +0 -179
  93. package/lib/uuid.js.map +0 -1
  94. package/lib/uuid_utils.js +0 -35
  95. package/lib/uuid_utils.js.map +0 -1
  96. package/lib/validate_utf8.js +0 -47
  97. package/lib/validate_utf8.js.map +0 -1
  98. package/src/ensure_buffer.ts +0 -27
  99. package/src/map.ts +0 -119
  100. package/src/utils/global.ts +0 -22
  101. package/src/uuid.ts +0 -209
package/src/code.ts CHANGED
@@ -2,7 +2,7 @@ import type { Document } from './bson';
2
2
 
3
3
  /** @public */
4
4
  export interface CodeExtended {
5
- $code: string | Function;
5
+ $code: string;
6
6
  $scope?: Document;
7
7
  }
8
8
 
@@ -12,23 +12,35 @@ export interface CodeExtended {
12
12
  * @category BSONType
13
13
  */
14
14
  export class Code {
15
- _bsontype!: 'Code';
15
+ get _bsontype(): 'Code' {
16
+ return 'Code';
17
+ }
18
+ /** @internal */
19
+ get [Symbol.for('@@mdb.bson.version')](): 5 {
20
+ return 5;
21
+ }
22
+
23
+ code: string;
24
+
25
+ // a code instance having a null scope is what determines whether
26
+ // it is BSONType 0x0D (just code) / 0x0F (code with scope)
27
+ scope: Document | null;
16
28
 
17
- code!: string | Function;
18
- scope?: Document;
19
29
  /**
20
30
  * @param code - a string or function.
21
31
  * @param scope - an optional scope for the function.
22
32
  */
23
- constructor(code: string | Function, scope?: Document) {
24
- if (!(this instanceof Code)) return new Code(code, scope);
25
-
26
- this.code = code;
27
- this.scope = scope;
33
+ constructor(code: string | Function, scope?: Document | null) {
34
+ this.code = code.toString();
35
+ this.scope = scope ?? null;
28
36
  }
29
37
 
30
- toJSON(): { code: string | Function; scope?: Document } {
31
- return { code: this.code, scope: this.scope };
38
+ toJSON(): { code: string; scope?: Document } {
39
+ if (this.scope != null) {
40
+ return { code: this.code, scope: this.scope };
41
+ }
42
+
43
+ return { code: this.code };
32
44
  }
33
45
 
34
46
  /** @internal */
@@ -53,9 +65,7 @@ export class Code {
53
65
  inspect(): string {
54
66
  const codeJson = this.toJSON();
55
67
  return `new Code("${String(codeJson.code)}"${
56
- codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''
68
+ codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''
57
69
  })`;
58
70
  }
59
71
  }
60
-
61
- Object.defineProperty(Code.prototype, '_bsontype', { value: 'Code' });
package/src/constants.ts CHANGED
@@ -108,3 +108,31 @@ export const BSON_BINARY_SUBTYPE_COLUMN = 7;
108
108
 
109
109
  /** Binary User Defined Type @internal */
110
110
  export const BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
111
+
112
+ /** @public */
113
+ export const BSONType = Object.freeze({
114
+ double: 1,
115
+ string: 2,
116
+ object: 3,
117
+ array: 4,
118
+ binData: 5,
119
+ undefined: 6,
120
+ objectId: 7,
121
+ bool: 8,
122
+ date: 9,
123
+ null: 10,
124
+ regex: 11,
125
+ dbPointer: 12,
126
+ javascript: 13,
127
+ symbol: 14,
128
+ javascriptWithScope: 15,
129
+ int: 16,
130
+ timestamp: 17,
131
+ long: 18,
132
+ decimal: 19,
133
+ minKey: -1,
134
+ maxKey: 127
135
+ } as const);
136
+
137
+ /** @public */
138
+ export type BSONType = typeof BSONType[keyof typeof BSONType];
package/src/db_ref.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import type { Document } from './bson';
2
2
  import type { EJSONOptions } from './extended_json';
3
3
  import type { ObjectId } from './objectid';
4
- import { isObjectLike } from './parser/utils';
5
4
 
6
5
  /** @public */
7
6
  export interface DBRefLike {
@@ -13,10 +12,14 @@ export interface DBRefLike {
13
12
  /** @internal */
14
13
  export function isDBRefLike(value: unknown): value is DBRefLike {
15
14
  return (
16
- isObjectLike(value) &&
15
+ value != null &&
16
+ typeof value === 'object' &&
17
+ '$id' in value &&
17
18
  value.$id != null &&
19
+ '$ref' in value &&
18
20
  typeof value.$ref === 'string' &&
19
- (value.$db == null || typeof value.$db === 'string')
21
+ // If '$db' is defined it MUST be a string, otherwise it should be absent
22
+ (!('$db' in value) || ('$db' in value && typeof value.$db === 'string'))
20
23
  );
21
24
  }
22
25
 
@@ -26,7 +29,13 @@ export function isDBRefLike(value: unknown): value is DBRefLike {
26
29
  * @category BSONType
27
30
  */
28
31
  export class DBRef {
29
- _bsontype!: 'DBRef';
32
+ get _bsontype(): 'DBRef' {
33
+ return 'DBRef';
34
+ }
35
+ /** @internal */
36
+ get [Symbol.for('@@mdb.bson.version')](): 5 {
37
+ return 5;
38
+ }
30
39
 
31
40
  collection!: string;
32
41
  oid!: ObjectId;
@@ -39,8 +48,6 @@ export class DBRef {
39
48
  * @param db - optional db name, if omitted the reference is local to the current db.
40
49
  */
41
50
  constructor(collection: string, oid: ObjectId, db?: string, fields?: Document) {
42
- if (!(this instanceof DBRef)) return new DBRef(collection, oid, db, fields);
43
-
44
51
  // check if namespace has been provided
45
52
  const parts = collection.split('.');
46
53
  if (parts.length === 2) {
@@ -120,5 +127,3 @@ export class DBRef {
120
127
  })`;
121
128
  }
122
129
  }
123
-
124
- Object.defineProperty(DBRef.prototype, '_bsontype', { value: 'DBRef' });
package/src/decimal128.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Buffer } from 'buffer';
2
1
  import { BSONTypeError } from './error';
3
2
  import { Long } from './long';
4
3
  import { isUint8Array } from './parser/utils';
4
+ import { ByteUtils } from './utils/byte_utils';
5
5
 
6
6
  const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
7
7
  const PARSE_INF_REGEXP = /^(\+|-)?(Infinity|inf)$/i;
@@ -13,16 +13,22 @@ const EXPONENT_BIAS = 6176;
13
13
  const MAX_DIGITS = 34;
14
14
 
15
15
  // Nan value bits as 32 bit values (due to lack of longs)
16
- const NAN_BUFFER = [
17
- 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
18
- ].reverse();
16
+ const NAN_BUFFER = ByteUtils.fromNumberArray(
17
+ [
18
+ 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
19
+ ].reverse()
20
+ );
19
21
  // Infinity value bits 32 bit values (due to lack of longs)
20
- const INF_NEGATIVE_BUFFER = [
21
- 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
22
- ].reverse();
23
- const INF_POSITIVE_BUFFER = [
24
- 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
25
- ].reverse();
22
+ const INF_NEGATIVE_BUFFER = ByteUtils.fromNumberArray(
23
+ [
24
+ 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
25
+ ].reverse()
26
+ );
27
+ const INF_POSITIVE_BUFFER = ByteUtils.fromNumberArray(
28
+ [
29
+ 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
30
+ ].reverse()
31
+ );
26
32
 
27
33
  const EXPONENT_REGEX = /^([-+])?(\d+)?$/;
28
34
 
@@ -121,17 +127,21 @@ export interface Decimal128Extended {
121
127
  * @category BSONType
122
128
  */
123
129
  export class Decimal128 {
124
- _bsontype!: 'Decimal128';
130
+ get _bsontype(): 'Decimal128' {
131
+ return 'Decimal128';
132
+ }
133
+ /** @internal */
134
+ get [Symbol.for('@@mdb.bson.version')](): 5 {
135
+ return 5;
136
+ }
125
137
 
126
- readonly bytes!: Buffer;
138
+ readonly bytes!: Uint8Array;
127
139
 
128
140
  /**
129
141
  * @param bytes - a buffer containing the raw Decimal128 bytes in little endian order,
130
142
  * or a string representation as returned by .toString()
131
143
  */
132
- constructor(bytes: Buffer | string) {
133
- if (!(this instanceof Decimal128)) return new Decimal128(bytes);
134
-
144
+ constructor(bytes: Uint8Array | string) {
135
145
  if (typeof bytes === 'string') {
136
146
  this.bytes = Decimal128.fromString(bytes).bytes;
137
147
  } else if (isUint8Array(bytes)) {
@@ -239,9 +249,9 @@ export class Decimal128 {
239
249
  // Check if user passed Infinity or NaN
240
250
  if (!isDigit(representation[index]) && representation[index] !== '.') {
241
251
  if (representation[index] === 'i' || representation[index] === 'I') {
242
- return new Decimal128(Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER));
252
+ return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
243
253
  } else if (representation[index] === 'N') {
244
- return new Decimal128(Buffer.from(NAN_BUFFER));
254
+ return new Decimal128(NAN_BUFFER);
245
255
  }
246
256
  }
247
257
 
@@ -285,7 +295,7 @@ export class Decimal128 {
285
295
  const match = representation.substr(++index).match(EXPONENT_REGEX);
286
296
 
287
297
  // No digits read
288
- if (!match || !match[2]) return new Decimal128(Buffer.from(NAN_BUFFER));
298
+ if (!match || !match[2]) return new Decimal128(NAN_BUFFER);
289
299
 
290
300
  // Get exponent
291
301
  exponent = parseInt(match[0], 10);
@@ -295,7 +305,7 @@ export class Decimal128 {
295
305
  }
296
306
 
297
307
  // Return not a number
298
- if (representation[index]) return new Decimal128(Buffer.from(NAN_BUFFER));
308
+ if (representation[index]) return new Decimal128(NAN_BUFFER);
299
309
 
300
310
  // Done reading input
301
311
  // Find first non-zero digit in digits
@@ -423,9 +433,7 @@ export class Decimal128 {
423
433
  exponent = exponent + 1;
424
434
  digits[dIdx] = 1;
425
435
  } else {
426
- return new Decimal128(
427
- Buffer.from(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER)
428
- );
436
+ return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
429
437
  }
430
438
  }
431
439
  }
@@ -503,7 +511,7 @@ export class Decimal128 {
503
511
  }
504
512
 
505
513
  // Encode into a buffer
506
- const buffer = Buffer.alloc(16);
514
+ const buffer = ByteUtils.allocate(16);
507
515
  index = 0;
508
516
 
509
517
  // Encode the low 64 bits of the decimal
@@ -769,5 +777,3 @@ export class Decimal128 {
769
777
  return `new Decimal128("${this.toString()}")`;
770
778
  }
771
779
  }
772
-
773
- Object.defineProperty(Decimal128.prototype, '_bsontype', { value: 'Decimal128' });
package/src/double.ts CHANGED
@@ -11,7 +11,13 @@ export interface DoubleExtended {
11
11
  * @category BSONType
12
12
  */
13
13
  export class Double {
14
- _bsontype!: 'Double';
14
+ get _bsontype(): 'Double' {
15
+ return 'Double';
16
+ }
17
+ /** @internal */
18
+ get [Symbol.for('@@mdb.bson.version')](): 5 {
19
+ return 5;
20
+ }
15
21
 
16
22
  value!: number;
17
23
  /**
@@ -20,8 +26,6 @@ export class Double {
20
26
  * @param value - the number we want to represent as a double.
21
27
  */
22
28
  constructor(value: number) {
23
- if (!(this instanceof Double)) return new Double(value);
24
-
25
29
  if ((value as unknown) instanceof Number) {
26
30
  value = value.valueOf();
27
31
  }
@@ -87,5 +91,3 @@ export class Double {
87
91
  return `new Double(${eJSON.$numberDouble})`;
88
92
  }
89
93
  }
90
-
91
- Object.defineProperty(Double.prototype, '_bsontype', { value: 'Double' });
package/src/error.ts CHANGED
@@ -2,7 +2,6 @@
2
2
  export class BSONError extends Error {
3
3
  constructor(message: string) {
4
4
  super(message);
5
- Object.setPrototypeOf(this, BSONError.prototype);
6
5
  }
7
6
 
8
7
  get name(): string {
@@ -14,7 +13,6 @@ export class BSONError extends Error {
14
13
  export class BSONTypeError extends TypeError {
15
14
  constructor(message: string) {
16
15
  super(message);
17
- Object.setPrototypeOf(this, BSONTypeError.prototype);
18
16
  }
19
17
 
20
18
  get name(): string {