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