bson 5.1.0 → 5.3.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 (58) hide show
  1. package/README.md +3 -126
  2. package/bson.d.ts +79 -22
  3. package/lib/binary.d.ts +182 -0
  4. package/lib/binary.d.ts.map +1 -0
  5. package/lib/bson.bundle.js +87 -89
  6. package/lib/bson.bundle.js.map +1 -1
  7. package/lib/bson.cjs +87 -89
  8. package/lib/bson.cjs.map +1 -1
  9. package/lib/bson.d.ts +97 -0
  10. package/lib/bson.d.ts.map +1 -0
  11. package/lib/bson.mjs +87 -89
  12. package/lib/bson.mjs.map +1 -1
  13. package/lib/bson_value.d.ts +10 -0
  14. package/lib/bson_value.d.ts.map +1 -0
  15. package/lib/code.d.ts +32 -0
  16. package/lib/code.d.ts.map +1 -0
  17. package/lib/constants.d.ts +107 -0
  18. package/lib/constants.d.ts.map +1 -0
  19. package/lib/db_ref.d.ts +40 -0
  20. package/lib/db_ref.d.ts.map +1 -0
  21. package/lib/decimal128.d.ts +34 -0
  22. package/lib/decimal128.d.ts.map +1 -0
  23. package/lib/double.d.ts +35 -0
  24. package/lib/double.d.ts.map +1 -0
  25. package/lib/error.d.ts +50 -0
  26. package/lib/error.d.ts.map +1 -0
  27. package/lib/extended_json.d.ts +82 -0
  28. package/lib/extended_json.d.ts.map +1 -0
  29. package/lib/index.d.ts +4 -0
  30. package/lib/index.d.ts.map +1 -0
  31. package/lib/int_32.d.ts +35 -0
  32. package/lib/int_32.d.ts.map +1 -0
  33. package/lib/long.d.ts +323 -0
  34. package/lib/long.d.ts.map +1 -0
  35. package/lib/max_key.d.ts +19 -0
  36. package/lib/max_key.d.ts.map +1 -0
  37. package/lib/min_key.d.ts +19 -0
  38. package/lib/min_key.d.ts.map +1 -0
  39. package/lib/objectid.d.ts +96 -0
  40. package/lib/objectid.d.ts.map +1 -0
  41. package/lib/regexp.d.ts +36 -0
  42. package/lib/regexp.d.ts.map +1 -0
  43. package/lib/symbol.d.ts +28 -0
  44. package/lib/symbol.d.ts.map +1 -0
  45. package/lib/timestamp.d.ts +66 -0
  46. package/lib/timestamp.d.ts.map +1 -0
  47. package/lib/validate_utf8.d.ts +10 -0
  48. package/lib/validate_utf8.d.ts.map +1 -0
  49. package/package.json +20 -20
  50. package/src/binary.ts +67 -43
  51. package/src/bson.ts +1 -1
  52. package/src/constants.ts +1 -1
  53. package/src/extended_json.ts +11 -3
  54. package/src/objectid.ts +11 -5
  55. package/src/parser/deserializer.ts +37 -12
  56. package/src/parser/serializer.ts +17 -4
  57. package/src/timestamp.ts +1 -1
  58. package/src/uuid_utils.ts +0 -33
package/lib/bson.d.ts ADDED
@@ -0,0 +1,97 @@
1
+ import { Binary, UUID } from './binary';
2
+ import { Code } from './code';
3
+ import { DBRef } from './db_ref';
4
+ import { Decimal128 } from './decimal128';
5
+ import { Double } from './double';
6
+ import { Int32 } from './int_32';
7
+ import { Long } from './long';
8
+ import { MaxKey } from './max_key';
9
+ import { MinKey } from './min_key';
10
+ import { ObjectId } from './objectid';
11
+ import { DeserializeOptions } from './parser/deserializer';
12
+ import { SerializeOptions } from './parser/serializer';
13
+ import { BSONRegExp } from './regexp';
14
+ import { BSONSymbol } from './symbol';
15
+ import { Timestamp } from './timestamp';
16
+ export type { UUIDExtended, BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
17
+ export type { CodeExtended } from './code';
18
+ export type { DBRefLike } from './db_ref';
19
+ export type { Decimal128Extended } from './decimal128';
20
+ export type { DoubleExtended } from './double';
21
+ export type { EJSONOptions } from './extended_json';
22
+ export type { Int32Extended } from './int_32';
23
+ export type { LongExtended } from './long';
24
+ export type { MaxKeyExtended } from './max_key';
25
+ export type { MinKeyExtended } from './min_key';
26
+ export type { ObjectIdExtended, ObjectIdLike } from './objectid';
27
+ export type { BSONRegExpExtended, BSONRegExpExtendedLegacy } from './regexp';
28
+ export type { BSONSymbolExtended } from './symbol';
29
+ export type { LongWithoutOverrides, TimestampExtended, TimestampOverrides } from './timestamp';
30
+ export type { LongWithoutOverridesClass } from './timestamp';
31
+ export type { SerializeOptions, DeserializeOptions };
32
+ export { Code, BSONSymbol, DBRef, Binary, ObjectId, UUID, Long, Timestamp, Double, Int32, MinKey, MaxKey, BSONRegExp, Decimal128 };
33
+ export { BSONValue } from './bson_value';
34
+ export { BSONError, BSONVersionError, BSONRuntimeError } from './error';
35
+ export { BSONType } from './constants';
36
+ export { EJSON } from './extended_json';
37
+ /** @public */
38
+ export interface Document {
39
+ [key: string]: any;
40
+ }
41
+ /**
42
+ * Sets the size of the internal serialization buffer.
43
+ *
44
+ * @param size - The desired size for the internal serialization buffer in bytes
45
+ * @public
46
+ */
47
+ export declare function setInternalBufferSize(size: number): void;
48
+ /**
49
+ * Serialize a Javascript object.
50
+ *
51
+ * @param object - the Javascript object to serialize.
52
+ * @returns Buffer object containing the serialized object.
53
+ * @public
54
+ */
55
+ export declare function serialize(object: Document, options?: SerializeOptions): Uint8Array;
56
+ /**
57
+ * Serialize a Javascript object using a predefined Buffer and index into the buffer,
58
+ * useful when pre-allocating the space for serialization.
59
+ *
60
+ * @param object - the Javascript object to serialize.
61
+ * @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object.
62
+ * @returns the index pointing to the last written byte in the buffer.
63
+ * @public
64
+ */
65
+ export declare function serializeWithBufferAndIndex(object: Document, finalBuffer: Uint8Array, options?: SerializeOptions): number;
66
+ /**
67
+ * Deserialize data as BSON.
68
+ *
69
+ * @param buffer - the buffer containing the serialized set of BSON documents.
70
+ * @returns returns the deserialized Javascript Object.
71
+ * @public
72
+ */
73
+ export declare function deserialize(buffer: Uint8Array, options?: DeserializeOptions): Document;
74
+ /** @public */
75
+ export type CalculateObjectSizeOptions = Pick<SerializeOptions, 'serializeFunctions' | 'ignoreUndefined'>;
76
+ /**
77
+ * Calculate the bson size for a passed in Javascript object.
78
+ *
79
+ * @param object - the Javascript object to calculate the BSON byte size for
80
+ * @returns size of BSON object in bytes
81
+ * @public
82
+ */
83
+ export declare function calculateObjectSize(object: Document, options?: CalculateObjectSizeOptions): number;
84
+ /**
85
+ * Deserialize stream data as BSON documents.
86
+ *
87
+ * @param data - the buffer containing the serialized set of BSON documents.
88
+ * @param startIndex - the start index in the data Buffer where the deserialization is to start.
89
+ * @param numberOfDocuments - number of documents to deserialize.
90
+ * @param documents - an array where to store the deserialized documents.
91
+ * @param docStartIndex - the index in the documents array from where to start inserting documents.
92
+ * @param options - additional options used for the deserialization.
93
+ * @returns next index in the buffer after deserialization **x** numbers of documents.
94
+ * @public
95
+ */
96
+ export declare function deserializeStream(data: Uint8Array | ArrayBuffer, startIndex: number, numberOfDocuments: number, documents: Document[], docStartIndex: number, options: DeserializeOptions): number;
97
+ //# sourceMappingURL=bson.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bson.d.ts","sourceRoot":"","sources":["../src/bson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC,OAAO,EAAuB,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAChF,OAAO,EAAiB,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AACtC,O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Buffer]'].includes(Object.prototype.toString.call(value));
3
+ }
4
+ function isUint8Array(value) {
5
+ return Object.prototype.toString.call(value) === '[object Uint8Array]';
6
+ }
7
+ function isRegExp(d) {
8
+ return Object.prototype.toString.call(d) === '[object RegExp]';
9
+ }
10
+ function isMap(d) {
11
+ return Object.prototype.toString.call(d) === '[object Map]';
12
+ }
13
+ function isDate(d) {
14
+ return Object.prototype.toString.call(d) === '[object Date]';
15
+ }
16
+
1
17
  const BSON_MAJOR_VERSION = 5;
2
18
  const BSON_INT32_MAX = 0x7fffffff;
3
19
  const BSON_INT32_MIN = -0x80000000;
@@ -280,44 +296,6 @@ class BSONDataView extends DataView {
280
296
  }
281
297
  }
282
298
 
283
- const VALIDATION_REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|[0-9a-f]{12}4[0-9a-f]{3}[89ab][0-9a-f]{15})$/i;
284
- const uuidValidateString = (str) => typeof str === 'string' && VALIDATION_REGEX.test(str);
285
- const uuidHexStringToBuffer = (hexString) => {
286
- if (!uuidValidateString(hexString)) {
287
- throw new BSONError('UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".');
288
- }
289
- const sanitizedHexString = hexString.replace(/-/g, '');
290
- return ByteUtils.fromHex(sanitizedHexString);
291
- };
292
- function bufferToUuidHexString(buffer, includeDashes = true) {
293
- if (includeDashes) {
294
- return [
295
- ByteUtils.toHex(buffer.subarray(0, 4)),
296
- ByteUtils.toHex(buffer.subarray(4, 6)),
297
- ByteUtils.toHex(buffer.subarray(6, 8)),
298
- ByteUtils.toHex(buffer.subarray(8, 10)),
299
- ByteUtils.toHex(buffer.subarray(10, 16))
300
- ].join('-');
301
- }
302
- return ByteUtils.toHex(buffer);
303
- }
304
-
305
- function isAnyArrayBuffer(value) {
306
- return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(Object.prototype.toString.call(value));
307
- }
308
- function isUint8Array(value) {
309
- return Object.prototype.toString.call(value) === '[object Uint8Array]';
310
- }
311
- function isRegExp(d) {
312
- return Object.prototype.toString.call(d) === '[object RegExp]';
313
- }
314
- function isMap(d) {
315
- return Object.prototype.toString.call(d) === '[object Map]';
316
- }
317
- function isDate(d) {
318
- return Object.prototype.toString.call(d) === '[object Date]';
319
- }
320
-
321
299
  class BSONValue {
322
300
  get [Symbol.for('@@mdb.bson.version')]() {
323
301
  return BSON_MAJOR_VERSION;
@@ -455,6 +433,12 @@ class Binary extends BSONValue {
455
433
  }
456
434
  throw new BSONError(`Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`);
457
435
  }
436
+ static createFromHexString(hex, subType) {
437
+ return new Binary(ByteUtils.fromHex(hex), subType);
438
+ }
439
+ static createFromBase64(base64, subType) {
440
+ return new Binary(ByteUtils.fromBase64(base64), subType);
441
+ }
458
442
  static fromExtendedJSON(doc, options) {
459
443
  options = options || {};
460
444
  let data;
@@ -473,7 +457,7 @@ class Binary extends BSONValue {
473
457
  }
474
458
  else if ('$uuid' in doc) {
475
459
  type = 4;
476
- data = uuidHexStringToBuffer(doc.$uuid);
460
+ data = UUID.bytesFromString(doc.$uuid);
477
461
  }
478
462
  if (!data) {
479
463
  throw new BSONError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
@@ -484,7 +468,8 @@ class Binary extends BSONValue {
484
468
  return this.inspect();
485
469
  }
486
470
  inspect() {
487
- return `new Binary(Buffer.from("${ByteUtils.toHex(this.buffer)}", "hex"), ${this.sub_type})`;
471
+ const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
472
+ return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
488
473
  }
489
474
  }
490
475
  Binary.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -499,47 +484,45 @@ Binary.SUBTYPE_ENCRYPTED = 6;
499
484
  Binary.SUBTYPE_COLUMN = 7;
500
485
  Binary.SUBTYPE_USER_DEFINED = 128;
501
486
  const UUID_BYTE_LENGTH = 16;
487
+ const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
488
+ const UUID_WITH_DASHES = /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
502
489
  class UUID extends Binary {
503
490
  constructor(input) {
504
491
  let bytes;
505
- let hexStr;
506
492
  if (input == null) {
507
493
  bytes = UUID.generate();
508
494
  }
509
495
  else if (input instanceof UUID) {
510
496
  bytes = ByteUtils.toLocalBufferType(new Uint8Array(input.buffer));
511
- hexStr = input.__id;
512
497
  }
513
498
  else if (ArrayBuffer.isView(input) && input.byteLength === UUID_BYTE_LENGTH) {
514
499
  bytes = ByteUtils.toLocalBufferType(input);
515
500
  }
516
501
  else if (typeof input === 'string') {
517
- bytes = uuidHexStringToBuffer(input);
502
+ bytes = UUID.bytesFromString(input);
518
503
  }
519
504
  else {
520
505
  throw new BSONError('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).');
521
506
  }
522
507
  super(bytes, BSON_BINARY_SUBTYPE_UUID_NEW);
523
- this.__id = hexStr;
524
508
  }
525
509
  get id() {
526
510
  return this.buffer;
527
511
  }
528
512
  set id(value) {
529
513
  this.buffer = value;
530
- if (UUID.cacheHexString) {
531
- this.__id = bufferToUuidHexString(value);
532
- }
533
514
  }
534
515
  toHexString(includeDashes = true) {
535
- if (UUID.cacheHexString && this.__id) {
536
- return this.__id;
537
- }
538
- const uuidHexString = bufferToUuidHexString(this.id, includeDashes);
539
- if (UUID.cacheHexString) {
540
- this.__id = uuidHexString;
516
+ if (includeDashes) {
517
+ return [
518
+ ByteUtils.toHex(this.buffer.subarray(0, 4)),
519
+ ByteUtils.toHex(this.buffer.subarray(4, 6)),
520
+ ByteUtils.toHex(this.buffer.subarray(6, 8)),
521
+ ByteUtils.toHex(this.buffer.subarray(8, 10)),
522
+ ByteUtils.toHex(this.buffer.subarray(10, 16))
523
+ ].join('-');
541
524
  }
542
- return uuidHexString;
525
+ return ByteUtils.toHex(this.buffer);
543
526
  }
544
527
  toString(encoding) {
545
528
  if (encoding === 'hex')
@@ -578,24 +561,32 @@ class UUID extends Binary {
578
561
  if (!input) {
579
562
  return false;
580
563
  }
581
- if (input instanceof UUID) {
582
- return true;
583
- }
584
564
  if (typeof input === 'string') {
585
- return uuidValidateString(input);
565
+ return UUID.isValidUUIDString(input);
586
566
  }
587
567
  if (isUint8Array(input)) {
588
- if (input.byteLength !== UUID_BYTE_LENGTH) {
589
- return false;
590
- }
591
- return (input[6] & 0xf0) === 0x40 && (input[8] & 0x80) === 0x80;
568
+ return input.byteLength === UUID_BYTE_LENGTH;
592
569
  }
593
- return false;
570
+ return (input._bsontype === 'Binary' &&
571
+ input.sub_type === this.SUBTYPE_UUID &&
572
+ input.buffer.byteLength === 16);
594
573
  }
595
574
  static createFromHexString(hexString) {
596
- const buffer = uuidHexStringToBuffer(hexString);
575
+ const buffer = UUID.bytesFromString(hexString);
597
576
  return new UUID(buffer);
598
577
  }
578
+ static createFromBase64(base64) {
579
+ return new UUID(ByteUtils.fromBase64(base64));
580
+ }
581
+ static bytesFromString(representation) {
582
+ if (!UUID.isValidUUIDString(representation)) {
583
+ throw new BSONError('UUID string representation must be 32 hex digits or canonical hyphenated representation');
584
+ }
585
+ return ByteUtils.fromHex(representation.replace(/-/g, ''));
586
+ }
587
+ static isValidUUIDString(representation) {
588
+ return UUID_WITHOUT_DASHES.test(representation) || UUID_WITH_DASHES.test(representation);
589
+ }
599
590
  [Symbol.for('nodejs.util.inspect.custom')]() {
600
591
  return this.inspect();
601
592
  }
@@ -603,6 +594,7 @@ class UUID extends Binary {
603
594
  return `new UUID("${this.toHexString()}")`;
604
595
  }
605
596
  }
597
+ UUID.cacheHexString = false;
606
598
 
607
599
  class Code extends BSONValue {
608
600
  get _bsontype() {
@@ -2114,11 +2106,17 @@ class ObjectId extends BSONValue {
2114
2106
  return new ObjectId(buffer);
2115
2107
  }
2116
2108
  static createFromHexString(hexString) {
2117
- if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
2118
- throw new BSONError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
2109
+ if (hexString?.length !== 24) {
2110
+ throw new BSONError('hex string must be 24 characters');
2119
2111
  }
2120
2112
  return new ObjectId(ByteUtils.fromHex(hexString));
2121
2113
  }
2114
+ static createFromBase64(base64) {
2115
+ if (base64?.length !== 16) {
2116
+ throw new BSONError('base64 string must be 16 characters');
2117
+ }
2118
+ return new ObjectId(ByteUtils.fromBase64(base64));
2119
+ }
2122
2120
  static isValid(id) {
2123
2121
  if (id == null)
2124
2122
  return false;
@@ -2773,7 +2771,7 @@ function deserializeObject(buffer, index, options, isArray = false) {
2773
2771
  }
2774
2772
  else {
2775
2773
  value = new Binary(buffer.slice(index, index + binarySize), subType);
2776
- if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
2774
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
2777
2775
  value = value.toUUID();
2778
2776
  }
2779
2777
  }
@@ -2799,11 +2797,11 @@ function deserializeObject(buffer, index, options, isArray = false) {
2799
2797
  if (promoteBuffers && promoteValues) {
2800
2798
  value = _buffer;
2801
2799
  }
2802
- else if (subType === BSON_BINARY_SUBTYPE_UUID_NEW) {
2803
- value = new Binary(buffer.slice(index, index + binarySize), subType).toUUID();
2804
- }
2805
2800
  else {
2806
2801
  value = new Binary(buffer.slice(index, index + binarySize), subType);
2802
+ if (subType === BSON_BINARY_SUBTYPE_UUID_NEW && UUID.isValid(value)) {
2803
+ value = value.toUUID();
2804
+ }
2807
2805
  }
2808
2806
  }
2809
2807
  index = index + binarySize;
@@ -4035,32 +4033,32 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4035
4033
 
4036
4034
  var bson = /*#__PURE__*/Object.freeze({
4037
4035
  __proto__: null,
4038
- Code: Code,
4036
+ BSONError: BSONError,
4037
+ BSONRegExp: BSONRegExp,
4038
+ BSONRuntimeError: BSONRuntimeError,
4039
4039
  BSONSymbol: BSONSymbol,
4040
- DBRef: DBRef,
4040
+ BSONType: BSONType,
4041
+ BSONValue: BSONValue,
4042
+ BSONVersionError: BSONVersionError,
4041
4043
  Binary: Binary,
4042
- ObjectId: ObjectId,
4043
- UUID: UUID,
4044
- Long: Long,
4045
- Timestamp: Timestamp,
4044
+ Code: Code,
4045
+ DBRef: DBRef,
4046
+ Decimal128: Decimal128,
4046
4047
  Double: Double,
4048
+ EJSON: EJSON,
4047
4049
  Int32: Int32,
4048
- MinKey: MinKey,
4050
+ Long: Long,
4049
4051
  MaxKey: MaxKey,
4050
- BSONRegExp: BSONRegExp,
4051
- Decimal128: Decimal128,
4052
- setInternalBufferSize: setInternalBufferSize,
4053
- serialize: serialize,
4054
- serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4055
- deserialize: deserialize,
4052
+ MinKey: MinKey,
4053
+ ObjectId: ObjectId,
4054
+ Timestamp: Timestamp,
4055
+ UUID: UUID,
4056
4056
  calculateObjectSize: calculateObjectSize,
4057
+ deserialize: deserialize,
4057
4058
  deserializeStream: deserializeStream,
4058
- BSONValue: BSONValue,
4059
- BSONError: BSONError,
4060
- BSONVersionError: BSONVersionError,
4061
- BSONRuntimeError: BSONRuntimeError,
4062
- BSONType: BSONType,
4063
- EJSON: EJSON
4059
+ serialize: serialize,
4060
+ serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4061
+ setInternalBufferSize: setInternalBufferSize
4064
4062
  });
4065
4063
 
4066
4064
  export { bson as BSON, BSONError, BSONRegExp, BSONRuntimeError, BSONSymbol, BSONType, BSONValue, BSONVersionError, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, serialize, serializeWithBufferAndIndex, setInternalBufferSize };