bson 4.7.0 → 5.0.0-alpha.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 (98) hide show
  1. package/bson.d.ts +207 -259
  2. package/lib/bson.bundle.js +4034 -0
  3. package/lib/bson.bundle.js.map +1 -0
  4. package/lib/bson.cjs +4029 -0
  5. package/lib/bson.cjs.map +1 -0
  6. package/lib/bson.mjs +4003 -0
  7. package/lib/bson.mjs.map +1 -0
  8. package/package.json +49 -62
  9. package/src/binary.ts +64 -53
  10. package/src/bson.ts +27 -108
  11. package/src/code.ts +25 -14
  12. package/src/constants.ts +33 -0
  13. package/src/db_ref.ts +14 -8
  14. package/src/decimal128.ts +32 -25
  15. package/src/double.ts +8 -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 +8 -5
  20. package/src/long.ts +17 -16
  21. package/src/max_key.ts +8 -6
  22. package/src/min_key.ts +8 -6
  23. package/src/objectid.ts +42 -74
  24. package/src/parser/calculate_size.ts +39 -63
  25. package/src/parser/deserializer.ts +41 -112
  26. package/src/parser/serializer.ts +234 -341
  27. package/src/parser/utils.ts +1 -99
  28. package/src/regexp.ts +17 -5
  29. package/src/symbol.ts +9 -5
  30. package/src/timestamp.ts +63 -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 -7470
  37. package/dist/bson.browser.esm.js.map +0 -1
  38. package/dist/bson.browser.umd.js +0 -7537
  39. package/dist/bson.browser.umd.js.map +0 -1
  40. package/dist/bson.bundle.js +0 -7536
  41. package/dist/bson.bundle.js.map +0 -1
  42. package/dist/bson.esm.js +0 -5436
  43. package/dist/bson.esm.js.map +0 -1
  44. package/lib/binary.js +0 -426
  45. package/lib/binary.js.map +0 -1
  46. package/lib/bson.js +0 -251
  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 -665
  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_utils.js +0 -35
  93. package/lib/uuid_utils.js.map +0 -1
  94. package/lib/validate_utf8.js +0 -47
  95. package/lib/validate_utf8.js.map +0 -1
  96. package/src/ensure_buffer.ts +0 -27
  97. package/src/map.ts +0 -119
  98. package/src/utils/global.ts +0 -22
package/src/long.ts CHANGED
@@ -1,5 +1,5 @@
1
+ import { BSON_MAJOR_VERSION } from './constants';
1
2
  import type { EJSONOptions } from './extended_json';
2
- import { isObjectLike } from './parser/utils';
3
3
  import type { Timestamp } from './timestamp';
4
4
 
5
5
  interface LongWASMHelpers {
@@ -100,10 +100,18 @@ export interface LongExtended {
100
100
  * Common constant values ZERO, ONE, NEG_ONE, etc. are found as static properties on this class.
101
101
  */
102
102
  export class Long {
103
- _bsontype!: 'Long';
103
+ get _bsontype(): 'Long' {
104
+ return 'Long';
105
+ }
106
+ /** @internal */
107
+ get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
108
+ return BSON_MAJOR_VERSION;
109
+ }
104
110
 
105
111
  /** An indicator used to reliably determine if an object is a Long or not. */
106
- __isLong__!: true;
112
+ get __isLong__(): boolean {
113
+ return true;
114
+ }
107
115
 
108
116
  /**
109
117
  * The high 32 bits as a signed value.
@@ -134,8 +142,6 @@ export class Long {
134
142
  * @param unsigned - Whether unsigned or not, defaults to signed
135
143
  */
136
144
  constructor(low: number | bigint | string = 0, high?: number | boolean, unsigned?: boolean) {
137
- if (!(this instanceof Long)) return new Long(low, high, unsigned);
138
-
139
145
  if (typeof low === 'bigint') {
140
146
  Object.assign(this, Long.fromBigInt(low, !!high));
141
147
  } else if (typeof low === 'string') {
@@ -145,13 +151,6 @@ export class Long {
145
151
  this.high = (high as number) | 0;
146
152
  this.unsigned = !!unsigned;
147
153
  }
148
-
149
- Object.defineProperty(this, '__isLong__', {
150
- value: true,
151
- configurable: false,
152
- writable: false,
153
- enumerable: false
154
- });
155
154
  }
156
155
 
157
156
  static TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
@@ -332,7 +331,12 @@ export class Long {
332
331
  * Tests if the specified object is a Long.
333
332
  */
334
333
  static isLong(value: unknown): value is Long {
335
- return isObjectLike(value) && value['__isLong__'] === true;
334
+ return (
335
+ value != null &&
336
+ typeof value === 'object' &&
337
+ '__isLong__' in value &&
338
+ value.__isLong__ === true
339
+ );
336
340
  }
337
341
 
338
342
  /**
@@ -1035,6 +1039,3 @@ export class Long {
1035
1039
  return `new Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
1036
1040
  }
1037
1041
  }
1038
-
1039
- Object.defineProperty(Long.prototype, '__isLong__', { value: true });
1040
- Object.defineProperty(Long.prototype, '_bsontype', { value: 'Long' });
package/src/max_key.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { BSON_MAJOR_VERSION } from './constants';
2
+
1
3
  /** @public */
2
4
  export interface MaxKeyExtended {
3
5
  $maxKey: 1;
@@ -9,10 +11,12 @@ export interface MaxKeyExtended {
9
11
  * @category BSONType
10
12
  */
11
13
  export class MaxKey {
12
- _bsontype!: 'MaxKey';
13
-
14
- constructor() {
15
- if (!(this instanceof MaxKey)) return new MaxKey();
14
+ get _bsontype(): 'MaxKey' {
15
+ return 'MaxKey';
16
+ }
17
+ /** @internal */
18
+ get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
19
+ return BSON_MAJOR_VERSION;
16
20
  }
17
21
 
18
22
  /** @internal */
@@ -34,5 +38,3 @@ export class MaxKey {
34
38
  return 'new MaxKey()';
35
39
  }
36
40
  }
37
-
38
- Object.defineProperty(MaxKey.prototype, '_bsontype', { value: 'MaxKey' });
package/src/min_key.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { BSON_MAJOR_VERSION } from './constants';
2
+
1
3
  /** @public */
2
4
  export interface MinKeyExtended {
3
5
  $minKey: 1;
@@ -9,10 +11,12 @@ export interface MinKeyExtended {
9
11
  * @category BSONType
10
12
  */
11
13
  export class MinKey {
12
- _bsontype!: 'MinKey';
13
-
14
- constructor() {
15
- if (!(this instanceof MinKey)) return new MinKey();
14
+ get _bsontype(): 'MinKey' {
15
+ return 'MinKey';
16
+ }
17
+ /** @internal */
18
+ get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
19
+ return BSON_MAJOR_VERSION;
16
20
  }
17
21
 
18
22
  /** @internal */
@@ -34,5 +38,3 @@ export class MinKey {
34
38
  return 'new MinKey()';
35
39
  }
36
40
  }
37
-
38
- Object.defineProperty(MinKey.prototype, '_bsontype', { value: 'MinKey' });
package/src/objectid.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Buffer } from 'buffer';
2
- import { ensureBuffer } from './ensure_buffer';
1
+ import { BSON_MAJOR_VERSION } from './constants';
3
2
  import { BSONTypeError } from './error';
4
- import { deprecate, isUint8Array, randomBytes } from './parser/utils';
3
+ import { isUint8Array } from './parser/utils';
4
+ import { BSONDataView, ByteUtils } from './utils/byte_utils';
5
5
 
6
6
  // Regular expression that checks for hex value
7
7
  const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
@@ -11,7 +11,7 @@ let PROCESS_UNIQUE: Uint8Array | null = null;
11
11
 
12
12
  /** @public */
13
13
  export interface ObjectIdLike {
14
- id: string | Buffer;
14
+ id: string | Uint8Array;
15
15
  __id?: string;
16
16
  toHexString(): string;
17
17
  }
@@ -29,15 +29,21 @@ const kId = Symbol('id');
29
29
  * @category BSONType
30
30
  */
31
31
  export class ObjectId {
32
- _bsontype!: 'ObjectID';
32
+ get _bsontype(): 'ObjectId' {
33
+ return 'ObjectId';
34
+ }
35
+ /** @internal */
36
+ get [Symbol.for('@@mdb.bson.version')](): BSON_MAJOR_VERSION {
37
+ return BSON_MAJOR_VERSION;
38
+ }
33
39
 
34
40
  /** @internal */
35
- static index = Math.floor(Math.random() * 0xffffff);
41
+ private static index = Math.floor(Math.random() * 0xffffff);
36
42
 
37
43
  static cacheHexString: boolean;
38
44
 
39
45
  /** ObjectId Bytes @internal */
40
- private [kId]!: Buffer;
46
+ private [kId]!: Uint8Array;
41
47
  /** ObjectId hexString cache @internal */
42
48
  private __id?: string;
43
49
 
@@ -46,9 +52,7 @@ export class ObjectId {
46
52
  *
47
53
  * @param inputId - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
48
54
  */
49
- constructor(inputId?: string | number | ObjectId | ObjectIdLike | Buffer | Uint8Array) {
50
- if (!(this instanceof ObjectId)) return new ObjectId(inputId);
51
-
55
+ constructor(inputId?: string | number | ObjectId | ObjectIdLike | Uint8Array) {
52
56
  // workingId is set based on type of input and whether valid id exists for the input
53
57
  let workingId;
54
58
  if (typeof inputId === 'object' && inputId && 'id' in inputId) {
@@ -58,7 +62,7 @@ export class ObjectId {
58
62
  );
59
63
  }
60
64
  if ('toHexString' in inputId && typeof inputId.toHexString === 'function') {
61
- workingId = Buffer.from(inputId.toHexString(), 'hex');
65
+ workingId = ByteUtils.fromHex(inputId.toHexString());
62
66
  } else {
63
67
  workingId = inputId.id;
64
68
  }
@@ -73,17 +77,18 @@ export class ObjectId {
73
77
  this[kId] = ObjectId.generate(typeof workingId === 'number' ? workingId : undefined);
74
78
  } else if (ArrayBuffer.isView(workingId) && workingId.byteLength === 12) {
75
79
  // If intstanceof matches we can escape calling ensure buffer in Node.js environments
76
- this[kId] = workingId instanceof Buffer ? workingId : ensureBuffer(workingId);
80
+ this[kId] = ByteUtils.toLocalBufferType(workingId);
77
81
  } else if (typeof workingId === 'string') {
78
82
  if (workingId.length === 12) {
79
- const bytes = Buffer.from(workingId);
83
+ // TODO(NODE-4361): Remove string of length 12 support
84
+ const bytes = ByteUtils.fromUTF8(workingId);
80
85
  if (bytes.byteLength === 12) {
81
86
  this[kId] = bytes;
82
87
  } else {
83
88
  throw new BSONTypeError('Argument passed in must be a string of 12 bytes');
84
89
  }
85
90
  } else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
86
- this[kId] = Buffer.from(workingId, 'hex');
91
+ this[kId] = ByteUtils.fromHex(workingId);
87
92
  } else {
88
93
  throw new BSONTypeError(
89
94
  'Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer'
@@ -94,7 +99,7 @@ export class ObjectId {
94
99
  }
95
100
  // If we are caching the hex string
96
101
  if (ObjectId.cacheHexString) {
97
- this.__id = this.id.toString('hex');
102
+ this.__id = ByteUtils.toHex(this.id);
98
103
  }
99
104
  }
100
105
 
@@ -102,37 +107,24 @@ export class ObjectId {
102
107
  * The ObjectId bytes
103
108
  * @readonly
104
109
  */
105
- get id(): Buffer {
110
+ get id(): Uint8Array {
106
111
  return this[kId];
107
112
  }
108
113
 
109
- set id(value: Buffer) {
114
+ set id(value: Uint8Array) {
110
115
  this[kId] = value;
111
116
  if (ObjectId.cacheHexString) {
112
- this.__id = value.toString('hex');
117
+ this.__id = ByteUtils.toHex(value);
113
118
  }
114
119
  }
115
120
 
116
- /**
117
- * The generation time of this ObjectId instance
118
- * @deprecated Please use getTimestamp / createFromTime which returns an int32 epoch
119
- */
120
- get generationTime(): number {
121
- return this.id.readInt32BE(0);
122
- }
123
-
124
- set generationTime(value: number) {
125
- // Encode time into first 4 bytes
126
- this.id.writeUInt32BE(value, 0);
127
- }
128
-
129
121
  /** Returns the ObjectId id as a 24 character hex string representation */
130
122
  toHexString(): string {
131
123
  if (ObjectId.cacheHexString && this.__id) {
132
124
  return this.__id;
133
125
  }
134
126
 
135
- const hexString = this.id.toString('hex');
127
+ const hexString = ByteUtils.toHex(this.id);
136
128
 
137
129
  if (ObjectId.cacheHexString && !this.__id) {
138
130
  this.__id = hexString;
@@ -143,11 +135,9 @@ export class ObjectId {
143
135
 
144
136
  /**
145
137
  * Update the ObjectId index
146
- * @privateRemarks
147
- * Used in generating new ObjectId's on the driver
148
138
  * @internal
149
139
  */
150
- static getInc(): number {
140
+ private static getInc(): number {
151
141
  return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
152
142
  }
153
143
 
@@ -156,20 +146,20 @@ export class ObjectId {
156
146
  *
157
147
  * @param time - pass in a second based timestamp.
158
148
  */
159
- static generate(time?: number): Buffer {
149
+ static generate(time?: number): Uint8Array {
160
150
  if ('number' !== typeof time) {
161
151
  time = Math.floor(Date.now() / 1000);
162
152
  }
163
153
 
164
154
  const inc = ObjectId.getInc();
165
- const buffer = Buffer.alloc(12);
155
+ const buffer = ByteUtils.allocate(12);
166
156
 
167
157
  // 4-byte timestamp
168
- buffer.writeUInt32BE(time, 0);
158
+ BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
169
159
 
170
160
  // set PROCESS_UNIQUE if yet not initialized
171
161
  if (PROCESS_UNIQUE === null) {
172
- PROCESS_UNIQUE = randomBytes(5);
162
+ PROCESS_UNIQUE = ByteUtils.randomBytes(5);
173
163
  }
174
164
 
175
165
  // 5-byte process unique
@@ -188,13 +178,13 @@ export class ObjectId {
188
178
  }
189
179
 
190
180
  /**
191
- * Converts the id into a 24 character hex string for printing
192
- *
193
- * @param format - The Buffer toString format parameter.
181
+ * Converts the id into a 24 character hex string for printing, unless encoding is provided.
182
+ * @param encoding - hex or base64
194
183
  */
195
- toString(format?: string): string {
184
+ toString(encoding?: 'hex' | 'base64'): string {
196
185
  // Is the id a buffer then use the buffer toString method to return the format
197
- if (format) return this.id.toString(format);
186
+ if (encoding === 'base64') return ByteUtils.toBase64(this.id);
187
+ if (encoding === 'hex') return this.toHexString();
198
188
  return this.toHexString();
199
189
  }
200
190
 
@@ -214,7 +204,7 @@ export class ObjectId {
214
204
  }
215
205
 
216
206
  if (otherId instanceof ObjectId) {
217
- return this[kId][11] === otherId[kId][11] && this[kId].equals(otherId[kId]);
207
+ return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
218
208
  }
219
209
 
220
210
  if (
@@ -223,7 +213,7 @@ export class ObjectId {
223
213
  otherId.length === 12 &&
224
214
  isUint8Array(this.id)
225
215
  ) {
226
- return otherId === Buffer.prototype.toString.call(this.id, 'latin1');
216
+ return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
227
217
  }
228
218
 
229
219
  if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
@@ -231,7 +221,7 @@ export class ObjectId {
231
221
  }
232
222
 
233
223
  if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
234
- return Buffer.from(otherId).equals(this.id);
224
+ return ByteUtils.equals(ByteUtils.fromUTF8(otherId), this.id);
235
225
  }
236
226
 
237
227
  if (
@@ -250,7 +240,7 @@ export class ObjectId {
250
240
  /** Returns the generation date (accurate up to the second) that this ID was generated. */
251
241
  getTimestamp(): Date {
252
242
  const timestamp = new Date();
253
- const time = this.id.readUInt32BE(0);
243
+ const time = BSONDataView.fromUint8Array(this.id).getUint32(0, false);
254
244
  timestamp.setTime(Math.floor(time) * 1000);
255
245
  return timestamp;
256
246
  }
@@ -266,9 +256,9 @@ export class ObjectId {
266
256
  * @param time - an integer number representing a number of seconds.
267
257
  */
268
258
  static createFromTime(time: number): ObjectId {
269
- const buffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
259
+ const buffer = ByteUtils.fromNumberArray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
270
260
  // Encode time into first 4 bytes
271
- buffer.writeUInt32BE(time, 0);
261
+ BSONDataView.fromUint8Array(buffer).setUint32(0, time, false);
272
262
  // Return the new objectId
273
263
  return new ObjectId(buffer);
274
264
  }
@@ -286,7 +276,7 @@ export class ObjectId {
286
276
  );
287
277
  }
288
278
 
289
- return new ObjectId(Buffer.from(hexString, 'hex'));
279
+ return new ObjectId(ByteUtils.fromHex(hexString));
290
280
  }
291
281
 
292
282
  /**
@@ -294,7 +284,7 @@ export class ObjectId {
294
284
  *
295
285
  * @param id - ObjectId instance to validate.
296
286
  */
297
- static isValid(id: string | number | ObjectId | ObjectIdLike | Buffer | Uint8Array): boolean {
287
+ static isValid(id: string | number | ObjectId | ObjectIdLike | Uint8Array): boolean {
298
288
  if (id == null) return false;
299
289
 
300
290
  try {
@@ -330,25 +320,3 @@ export class ObjectId {
330
320
  return `new ObjectId("${this.toHexString()}")`;
331
321
  }
332
322
  }
333
-
334
- // Deprecated methods
335
- Object.defineProperty(ObjectId.prototype, 'generate', {
336
- value: deprecate(
337
- (time: number) => ObjectId.generate(time),
338
- 'Please use the static `ObjectId.generate(time)` instead'
339
- )
340
- });
341
-
342
- Object.defineProperty(ObjectId.prototype, 'getInc', {
343
- value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
344
- });
345
-
346
- Object.defineProperty(ObjectId.prototype, 'get_inc', {
347
- value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
348
- });
349
-
350
- Object.defineProperty(ObjectId, 'get_inc', {
351
- value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
352
- });
353
-
354
- Object.defineProperty(ObjectId.prototype, '_bsontype', { value: 'ObjectID' });
@@ -1,10 +1,10 @@
1
- import { Buffer } from 'buffer';
2
1
  import { Binary } from '../binary';
3
2
  import type { Document } from '../bson';
4
3
  import * as constants from '../constants';
5
- import { isAnyArrayBuffer, isDate, isRegExp, normalizedFunctionString } from './utils';
4
+ import { ByteUtils } from '../utils/byte_utils';
5
+ import { isAnyArrayBuffer, isDate, isRegExp } from './utils';
6
6
 
7
- export function calculateObjectSize(
7
+ export function internalCalculateObjectSize(
8
8
  object: Document,
9
9
  serializeFunctions?: boolean,
10
10
  ignoreUndefined?: boolean
@@ -29,7 +29,7 @@ export function calculateObjectSize(
29
29
  }
30
30
 
31
31
  // Calculate size
32
- for (const key in object) {
32
+ for (const key of Object.keys(object)) {
33
33
  totalLength += calculateElement(key, object[key], serializeFunctions, false, ignoreUndefined);
34
34
  }
35
35
  }
@@ -53,7 +53,7 @@ function calculateElement(
53
53
 
54
54
  switch (typeof value) {
55
55
  case 'string':
56
- return 1 + Buffer.byteLength(name, 'utf8') + 1 + 4 + Buffer.byteLength(value, 'utf8') + 1;
56
+ return 1 + ByteUtils.utf8ByteLength(name) + 1 + 4 + ByteUtils.utf8ByteLength(value) + 1;
57
57
  case 'number':
58
58
  if (
59
59
  Math.floor(value) === value &&
@@ -62,61 +62,61 @@ function calculateElement(
62
62
  ) {
63
63
  if (value >= constants.BSON_INT32_MIN && value <= constants.BSON_INT32_MAX) {
64
64
  // 32 bit
65
- return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (4 + 1);
65
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (4 + 1);
66
66
  } else {
67
- return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
67
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
68
68
  }
69
69
  } else {
70
70
  // 64 bit
71
- return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
71
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
72
72
  }
73
73
  case 'undefined':
74
74
  if (isArray || !ignoreUndefined)
75
- return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
75
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + 1;
76
76
  return 0;
77
77
  case 'boolean':
78
- return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 1);
78
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (1 + 1);
79
79
  case 'object':
80
80
  if (value == null || value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
81
- return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
82
- } else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
83
- return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1);
81
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + 1;
82
+ } else if (value['_bsontype'] === 'ObjectId') {
83
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (12 + 1);
84
84
  } else if (value instanceof Date || isDate(value)) {
85
- return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
85
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
86
86
  } else if (
87
87
  ArrayBuffer.isView(value) ||
88
88
  value instanceof ArrayBuffer ||
89
89
  isAnyArrayBuffer(value)
90
90
  ) {
91
91
  return (
92
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 4 + 1) + value.byteLength
92
+ (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (1 + 4 + 1) + value.byteLength
93
93
  );
94
94
  } else if (
95
95
  value['_bsontype'] === 'Long' ||
96
96
  value['_bsontype'] === 'Double' ||
97
97
  value['_bsontype'] === 'Timestamp'
98
98
  ) {
99
- return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
99
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (8 + 1);
100
100
  } else if (value['_bsontype'] === 'Decimal128') {
101
- return (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (16 + 1);
101
+ return (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (16 + 1);
102
102
  } else if (value['_bsontype'] === 'Code') {
103
103
  // Calculate size depending on the availability of a scope
104
104
  if (value.scope != null && Object.keys(value.scope).length > 0) {
105
105
  return (
106
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
106
+ (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
107
107
  1 +
108
108
  4 +
109
109
  4 +
110
- Buffer.byteLength(value.code.toString(), 'utf8') +
110
+ ByteUtils.utf8ByteLength(value.code.toString()) +
111
111
  1 +
112
- calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
112
+ internalCalculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
113
113
  );
114
114
  } else {
115
115
  return (
116
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
116
+ (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
117
117
  1 +
118
118
  4 +
119
- Buffer.byteLength(value.code.toString(), 'utf8') +
119
+ ByteUtils.utf8ByteLength(value.code.toString()) +
120
120
  1
121
121
  );
122
122
  }
@@ -125,18 +125,18 @@ function calculateElement(
125
125
  // Check what kind of subtype we have
126
126
  if (binary.sub_type === Binary.SUBTYPE_BYTE_ARRAY) {
127
127
  return (
128
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
128
+ (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
129
129
  (binary.position + 1 + 4 + 1 + 4)
130
130
  );
131
131
  } else {
132
132
  return (
133
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) + (binary.position + 1 + 4 + 1)
133
+ (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) + (binary.position + 1 + 4 + 1)
134
134
  );
135
135
  }
136
136
  } else if (value['_bsontype'] === 'Symbol') {
137
137
  return (
138
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
139
- Buffer.byteLength(value.value, 'utf8') +
138
+ (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
139
+ ByteUtils.utf8ByteLength(value.value) +
140
140
  4 +
141
141
  1 +
142
142
  1
@@ -157,15 +157,15 @@ function calculateElement(
157
157
  }
158
158
 
159
159
  return (
160
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
160
+ (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
161
161
  1 +
162
- calculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined)
162
+ internalCalculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined)
163
163
  );
164
164
  } else if (value instanceof RegExp || isRegExp(value)) {
165
165
  return (
166
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
166
+ (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
167
167
  1 +
168
- Buffer.byteLength(value.source, 'utf8') +
168
+ ByteUtils.utf8ByteLength(value.source) +
169
169
  1 +
170
170
  (value.global ? 1 : 0) +
171
171
  (value.ignoreCase ? 1 : 0) +
@@ -174,53 +174,29 @@ function calculateElement(
174
174
  );
175
175
  } else if (value['_bsontype'] === 'BSONRegExp') {
176
176
  return (
177
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
177
+ (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
178
178
  1 +
179
- Buffer.byteLength(value.pattern, 'utf8') +
179
+ ByteUtils.utf8ByteLength(value.pattern) +
180
180
  1 +
181
- Buffer.byteLength(value.options, 'utf8') +
181
+ ByteUtils.utf8ByteLength(value.options) +
182
182
  1
183
183
  );
184
184
  } else {
185
185
  return (
186
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
187
- calculateObjectSize(value, serializeFunctions, ignoreUndefined) +
186
+ (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
187
+ internalCalculateObjectSize(value, serializeFunctions, ignoreUndefined) +
188
188
  1
189
189
  );
190
190
  }
191
191
  case 'function':
192
- // WTF for 0.4.X where typeof /someregexp/ === 'function'
193
- if (value instanceof RegExp || isRegExp(value) || String.call(value) === '[object RegExp]') {
192
+ if (serializeFunctions) {
194
193
  return (
195
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
194
+ (name != null ? ByteUtils.utf8ByteLength(name) + 1 : 0) +
196
195
  1 +
197
- Buffer.byteLength(value.source, 'utf8') +
198
- 1 +
199
- (value.global ? 1 : 0) +
200
- (value.ignoreCase ? 1 : 0) +
201
- (value.multiline ? 1 : 0) +
196
+ 4 +
197
+ ByteUtils.utf8ByteLength(value.toString()) +
202
198
  1
203
199
  );
204
- } else {
205
- if (serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
206
- return (
207
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
208
- 1 +
209
- 4 +
210
- 4 +
211
- Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
212
- 1 +
213
- calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined)
214
- );
215
- } else if (serializeFunctions) {
216
- return (
217
- (name != null ? Buffer.byteLength(name, 'utf8') + 1 : 0) +
218
- 1 +
219
- 4 +
220
- Buffer.byteLength(normalizedFunctionString(value), 'utf8') +
221
- 1
222
- );
223
- }
224
200
  }
225
201
  }
226
202