bson 4.2.3 → 4.5.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 (66) hide show
  1. package/bower.json +1 -1
  2. package/bson.d.ts +84 -9
  3. package/dist/bson.browser.esm.js +991 -2292
  4. package/dist/bson.browser.esm.js.map +1 -1
  5. package/dist/bson.browser.umd.js +1095 -2397
  6. package/dist/bson.browser.umd.js.map +1 -1
  7. package/dist/bson.bundle.js +1097 -2397
  8. package/dist/bson.bundle.js.map +1 -1
  9. package/dist/bson.esm.js +945 -2254
  10. package/dist/bson.esm.js.map +1 -1
  11. package/lib/binary.js +9 -1
  12. package/lib/binary.js.map +1 -1
  13. package/lib/bson.js +6 -2
  14. package/lib/bson.js.map +1 -1
  15. package/lib/db_ref.js +4 -1
  16. package/lib/db_ref.js.map +1 -1
  17. package/lib/decimal128.js +14 -51
  18. package/lib/decimal128.js.map +1 -1
  19. package/lib/ensure_buffer.js +2 -5
  20. package/lib/ensure_buffer.js.map +1 -1
  21. package/lib/extended_json.js +48 -9
  22. package/lib/extended_json.js.map +1 -1
  23. package/lib/long.js +18 -5
  24. package/lib/long.js.map +1 -1
  25. package/lib/map.js +3 -15
  26. package/lib/map.js.map +1 -1
  27. package/lib/objectid.js +9 -17
  28. package/lib/objectid.js.map +1 -1
  29. package/lib/parser/calculate_size.js +5 -6
  30. package/lib/parser/calculate_size.js.map +1 -1
  31. package/lib/parser/deserializer.js +61 -47
  32. package/lib/parser/deserializer.js.map +1 -1
  33. package/lib/parser/serializer.js +14 -13
  34. package/lib/parser/serializer.js.map +1 -1
  35. package/lib/parser/utils.js +44 -27
  36. package/lib/parser/utils.js.map +1 -1
  37. package/lib/regexp.js +1 -3
  38. package/lib/regexp.js.map +1 -1
  39. package/lib/timestamp.js +8 -2
  40. package/lib/timestamp.js.map +1 -1
  41. package/lib/utils/global.js +18 -0
  42. package/lib/utils/global.js.map +1 -0
  43. package/lib/uuid.js +173 -42
  44. package/lib/uuid.js.map +1 -1
  45. package/lib/uuid_utils.js +34 -0
  46. package/lib/uuid_utils.js.map +1 -0
  47. package/package.json +12 -9
  48. package/src/binary.ts +14 -2
  49. package/src/bson.ts +4 -1
  50. package/src/db_ref.ts +6 -1
  51. package/src/decimal128.ts +14 -52
  52. package/src/ensure_buffer.ts +7 -7
  53. package/src/extended_json.ts +64 -16
  54. package/src/long.ts +19 -7
  55. package/src/map.ts +5 -25
  56. package/src/objectid.ts +12 -19
  57. package/src/parser/calculate_size.ts +8 -11
  58. package/src/parser/deserializer.ts +68 -51
  59. package/src/parser/serializer.ts +13 -12
  60. package/src/parser/utils.ts +56 -18
  61. package/src/regexp.ts +2 -4
  62. package/src/timestamp.ts +15 -7
  63. package/src/utils/global.ts +22 -0
  64. package/src/uuid.ts +192 -40
  65. package/src/uuid_utils.ts +32 -0
  66. package/HISTORY.md +0 -481
package/lib/uuid.js CHANGED
@@ -1,48 +1,179 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseUUID = void 0;
3
+ exports.UUID = void 0;
4
+ var buffer_1 = require("buffer");
5
+ var ensure_buffer_1 = require("./ensure_buffer");
6
+ var binary_1 = require("./binary");
7
+ var uuid_utils_1 = require("./uuid_utils");
8
+ var utils_1 = require("./parser/utils");
9
+ var BYTE_LENGTH = 16;
10
+ var kId = Symbol('id');
4
11
  /**
5
- * UUID regular expression pattern copied from `uuid` npm module.
6
- * @see https://github.com/uuidjs/uuid/blob/master/src/regex.js
12
+ * A class representation of the BSON UUID type.
13
+ * @public
7
14
  */
8
- var UUID_RX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
9
- /**
10
- * Parser function copied from `uuid` npm module.
11
- * @see https://github.com/uuidjs/uuid/blob/master/src/parse.js
12
- * @internal
13
- */
14
- function parseUUID(uuid) {
15
- if (typeof uuid !== 'string') {
16
- throw new TypeError('Invalid type for UUID, expected string but got ' + typeof uuid);
17
- }
18
- if (!UUID_RX.test(uuid)) {
19
- throw new TypeError('Invalid format for UUID: ' + uuid);
15
+ var UUID = /** @class */ (function () {
16
+ /**
17
+ * Create an UUID type
18
+ *
19
+ * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
20
+ */
21
+ function UUID(input) {
22
+ if (typeof input === 'undefined') {
23
+ // The most common use case (blank id, new UUID() instance)
24
+ this.id = UUID.generate();
25
+ }
26
+ else if (input instanceof UUID) {
27
+ this[kId] = buffer_1.Buffer.from(input.id);
28
+ this.__id = input.__id;
29
+ }
30
+ else if (ArrayBuffer.isView(input) && input.byteLength === BYTE_LENGTH) {
31
+ this.id = ensure_buffer_1.ensureBuffer(input);
32
+ }
33
+ else if (typeof input === 'string') {
34
+ this.id = uuid_utils_1.uuidHexStringToBuffer(input);
35
+ }
36
+ else {
37
+ throw new TypeError('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).');
38
+ }
20
39
  }
21
- var v;
22
- var arr = new Uint8Array(16);
23
- // Parse ########-....-....-....-............
24
- arr[0] = (v = parseInt(uuid.slice(0, 8), 16)) >>> 24;
25
- arr[1] = (v >>> 16) & 0xff;
26
- arr[2] = (v >>> 8) & 0xff;
27
- arr[3] = v & 0xff;
28
- // Parse ........-####-....-....-............
29
- arr[4] = (v = parseInt(uuid.slice(9, 13), 16)) >>> 8;
30
- arr[5] = v & 0xff;
31
- // Parse ........-....-####-....-............
32
- arr[6] = (v = parseInt(uuid.slice(14, 18), 16)) >>> 8;
33
- arr[7] = v & 0xff;
34
- // Parse ........-....-....-####-............
35
- arr[8] = (v = parseInt(uuid.slice(19, 23), 16)) >>> 8;
36
- arr[9] = v & 0xff;
37
- // Parse ........-....-....-....-############
38
- // (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
39
- arr[10] = ((v = parseInt(uuid.slice(24, 36), 16)) / 0x10000000000) & 0xff;
40
- arr[11] = (v / 0x100000000) & 0xff;
41
- arr[12] = (v >>> 24) & 0xff;
42
- arr[13] = (v >>> 16) & 0xff;
43
- arr[14] = (v >>> 8) & 0xff;
44
- arr[15] = v & 0xff;
45
- return arr;
46
- }
47
- exports.parseUUID = parseUUID;
40
+ Object.defineProperty(UUID.prototype, "id", {
41
+ /**
42
+ * The UUID bytes
43
+ * @readonly
44
+ */
45
+ get: function () {
46
+ return this[kId];
47
+ },
48
+ set: function (value) {
49
+ this[kId] = value;
50
+ if (UUID.cacheHexString) {
51
+ this.__id = uuid_utils_1.bufferToUuidHexString(value);
52
+ }
53
+ },
54
+ enumerable: false,
55
+ configurable: true
56
+ });
57
+ /**
58
+ * Generate a 16 byte uuid v4 buffer used in UUIDs
59
+ */
60
+ /**
61
+ * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
62
+ * @param includeDashes - should the string exclude dash-separators.
63
+ * */
64
+ UUID.prototype.toHexString = function (includeDashes) {
65
+ if (includeDashes === void 0) { includeDashes = true; }
66
+ if (UUID.cacheHexString && this.__id) {
67
+ return this.__id;
68
+ }
69
+ var uuidHexString = uuid_utils_1.bufferToUuidHexString(this.id, includeDashes);
70
+ if (UUID.cacheHexString) {
71
+ this.__id = uuidHexString;
72
+ }
73
+ return uuidHexString;
74
+ };
75
+ /**
76
+ * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
77
+ * @internal
78
+ */
79
+ UUID.prototype.toString = function (encoding) {
80
+ return encoding ? this.id.toString(encoding) : this.toHexString();
81
+ };
82
+ /**
83
+ * Converts the id into its JSON string representation. A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
84
+ * @internal
85
+ */
86
+ UUID.prototype.toJSON = function () {
87
+ return this.toHexString();
88
+ };
89
+ /**
90
+ * Compares the equality of this UUID with `otherID`.
91
+ *
92
+ * @param otherId - UUID instance to compare against.
93
+ */
94
+ UUID.prototype.equals = function (otherId) {
95
+ if (!otherId) {
96
+ return false;
97
+ }
98
+ if (otherId instanceof UUID) {
99
+ return otherId.id.equals(this.id);
100
+ }
101
+ try {
102
+ return new UUID(otherId).id.equals(this.id);
103
+ }
104
+ catch (_a) {
105
+ return false;
106
+ }
107
+ };
108
+ /**
109
+ * Creates a Binary instance from the current UUID.
110
+ */
111
+ UUID.prototype.toBinary = function () {
112
+ return new binary_1.Binary(this.id, binary_1.Binary.SUBTYPE_UUID);
113
+ };
114
+ /**
115
+ * Generates a populated buffer containing a v4 uuid
116
+ */
117
+ UUID.generate = function () {
118
+ var bytes = utils_1.randomBytes(BYTE_LENGTH);
119
+ // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
120
+ // Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
121
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
122
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
123
+ return buffer_1.Buffer.from(bytes);
124
+ };
125
+ /**
126
+ * Checks if a value is a valid bson UUID
127
+ * @param input - UUID, string or Buffer to validate.
128
+ */
129
+ UUID.isValid = function (input) {
130
+ if (!input) {
131
+ return false;
132
+ }
133
+ if (input instanceof UUID) {
134
+ return true;
135
+ }
136
+ if (typeof input === 'string') {
137
+ return uuid_utils_1.uuidValidateString(input);
138
+ }
139
+ if (utils_1.isUint8Array(input)) {
140
+ // check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
141
+ if (input.length !== BYTE_LENGTH) {
142
+ return false;
143
+ }
144
+ try {
145
+ // get this byte as hex: xxxxxxxx-xxxx-XXxx-xxxx-xxxxxxxxxxxx
146
+ // check first part as uuid version: xxxxxxxx-xxxx-Xxxx-xxxx-xxxxxxxxxxxx
147
+ return parseInt(input[6].toString(16)[0], 10) === binary_1.Binary.SUBTYPE_UUID;
148
+ }
149
+ catch (_a) {
150
+ return false;
151
+ }
152
+ }
153
+ return false;
154
+ };
155
+ /**
156
+ * Creates an UUID from a hex string representation of an UUID.
157
+ * @param hexString - 32 or 36 character hex string (dashes excluded/included).
158
+ */
159
+ UUID.createFromHexString = function (hexString) {
160
+ var buffer = uuid_utils_1.uuidHexStringToBuffer(hexString);
161
+ return new UUID(buffer);
162
+ };
163
+ /**
164
+ * Converts to a string representation of this Id.
165
+ *
166
+ * @returns return the 36 character hex string representation.
167
+ * @internal
168
+ */
169
+ UUID.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
170
+ return this.inspect();
171
+ };
172
+ UUID.prototype.inspect = function () {
173
+ return "new UUID(\"" + this.toHexString() + "\")";
174
+ };
175
+ return UUID;
176
+ }());
177
+ exports.UUID = UUID;
178
+ Object.defineProperty(UUID.prototype, '_bsontype', { value: 'UUID' });
48
179
  //# sourceMappingURL=uuid.js.map
package/lib/uuid.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"uuid.js","sourceRoot":"","sources":["../src/uuid.ts"],"names":[],"mappings":";;;AAAA;;;GAGG;AACH,IAAM,OAAO,GAAG,qHAAqH,CAAC;AAOtI;;;;GAIG;AACH,SAAgB,SAAS,CAAC,IAAY;IACpC,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QAC5B,MAAM,IAAI,SAAS,CAAC,iDAAiD,GAAG,OAAO,IAAI,CAAC,CAAC;KACtF;IAED,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;QACvB,MAAM,IAAI,SAAS,CAAC,2BAA2B,GAAG,IAAI,CAAC,CAAC;KACzD;IAED,IAAI,CAAC,CAAC;IACN,IAAM,GAAG,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAE/B,6CAA6C;IAC7C,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,K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EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,eAAM,CAAC,YAAY,CAAC;aACvE;YAAC,WAAM;gBACN,OAAO,KAAK,CAAC;aACd;SACF;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;OAGG;IACI,wBAAmB,GAA1B,UAA2B,SAAiB;QAC1C,IAAM,MAAM,GAAG,kCAAqB,CAAC,SAAS,CAAC,CAAC;QAChD,OAAO,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC;IAC1B,CAAC;IAED;;;;;OAKG;IACH,eAAC,MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,GAA1C;QACE,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,sBAAO,GAAP;QACE,OAAO,gBAAa,IAAI,CAAC,WAAW,EAAE,QAAI,CAAC;IAC7C,CAAC;IACH,WAAC;AAAD,CAAC,AA3LD,IA2LC;AA3LY,oBAAI;AA6LjB,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bufferToUuidHexString = exports.uuidHexStringToBuffer = exports.uuidValidateString = void 0;
4
+ var buffer_1 = require("buffer");
5
+ // Validation regex for v4 uuid (validates with or without dashes)
6
+ var 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;
7
+ var uuidValidateString = function (str) {
8
+ return typeof str === 'string' && VALIDATION_REGEX.test(str);
9
+ };
10
+ exports.uuidValidateString = uuidValidateString;
11
+ var uuidHexStringToBuffer = function (hexString) {
12
+ if (!exports.uuidValidateString(hexString)) {
13
+ throw new TypeError('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
+ var sanitizedHexString = hexString.replace(/-/g, '');
16
+ return buffer_1.Buffer.from(sanitizedHexString, 'hex');
17
+ };
18
+ exports.uuidHexStringToBuffer = uuidHexStringToBuffer;
19
+ var bufferToUuidHexString = function (buffer, includeDashes) {
20
+ if (includeDashes === void 0) { includeDashes = true; }
21
+ return includeDashes
22
+ ? buffer.toString('hex', 0, 4) +
23
+ '-' +
24
+ buffer.toString('hex', 4, 6) +
25
+ '-' +
26
+ buffer.toString('hex', 6, 8) +
27
+ '-' +
28
+ buffer.toString('hex', 8, 10) +
29
+ '-' +
30
+ buffer.toString('hex', 10, 16)
31
+ : buffer.toString('hex');
32
+ };
33
+ exports.bufferToUuidHexString = bufferToUuidHexString;
34
+ //# sourceMappingURL=uuid_utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid_utils.js","sourceRoot":"","sources":["../src/uuid_utils.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAEhC,kEAAkE;AAClE,IAAM,gBAAgB,GACpB,uHAAuH,CAAC;AAEnH,IAAM,kBAAkB,GAAG,UAAC,GAAW;IAC5C,OAAA,OAAO,GAAG,KAAK,QAAQ,IAAI,gBAAgB,CAAC,IAAI,CAAC,GAAG,CAAC;AAArD,CAAqD,CAAC;AAD3C,QAAA,kBAAkB,sBACyB;AAEjD,IAAM,qBAAqB,GAAG,UAAC,SAAiB;IACrD,IAAI,CAAC,0BAAkB,CAAC,SAAS,CAAC,EAAE;QAClC,MAAM,IAAI,SAAS,CACjB,uLAAuL,CACxL,CAAC;KACH;IAED,IAAM,kBAAkB,GAAG,SAAS,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACvD,OAAO,eAAM,CAAC,IAAI,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;AAChD,CAAC,CAAC;AATW,QAAA,qBAAqB,yBAShC;AAEK,IAAM,qBAAqB,GAAG,UAAC,MAAc,EAAE,aAAoB;IAApB,8BAAA,EAAA,oBAAoB;IACxE,OAAA,aAAa;QACX,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;YAC5B,GAAG;YACH,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;YAC5B,GAAG;YACH,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;YAC5B,GAAG;YACH,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC;YAC7B,GAAG;YACH,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC;QAChC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;AAV1B,CAU0B,CAAC;AAXhB,QAAA,qBAAqB,yBAWL"}
package/package.json CHANGED
@@ -15,14 +15,16 @@
15
15
  "bower.json"
16
16
  ],
17
17
  "types": "bson.d.ts",
18
- "version": "4.2.3",
19
- "author": "Christian Amor Kvalheim <christkv@gmail.com>",
18
+ "version": "4.5.0",
19
+ "author": {
20
+ "name": "The MongoDB NodeJS Team",
21
+ "email": "dbx-node@mongodb.com"
22
+ },
20
23
  "license": "Apache-2.0",
21
24
  "contributors": [],
22
25
  "repository": "mongodb/js-bson",
23
26
  "bugs": {
24
- "mail": "node-mongodb-native@googlegroups.com",
25
- "url": "https://github.com/mongodb/js-bson/issues"
27
+ "url": "https://jira.mongodb.org/projects/NODE/issues/"
26
28
  },
27
29
  "devDependencies": {
28
30
  "@babel/plugin-external-helpers": "^7.10.4",
@@ -43,7 +45,7 @@
43
45
  "eslint-config-prettier": "^6.11.0",
44
46
  "eslint-plugin-prettier": "^3.1.4",
45
47
  "eslint-plugin-tsdoc": "^0.2.6",
46
- "karma": "^5.1.1",
48
+ "karma": "^6.3.4",
47
49
  "karma-chai": "^0.1.0",
48
50
  "karma-chrome-launcher": "^3.1.0",
49
51
  "karma-mocha": "^2.0.1",
@@ -56,14 +58,15 @@
56
58
  "rimraf": "^3.0.2",
57
59
  "rollup": "^2.26.5",
58
60
  "rollup-plugin-commonjs": "^10.1.0",
59
- "rollup-plugin-node-builtins": "^2.1.2",
60
61
  "rollup-plugin-node-globals": "^1.4.0",
61
62
  "rollup-plugin-node-polyfills": "^0.2.1",
62
- "standard-version": "^8.0.1",
63
+ "rollup-plugin-polyfill-node": "^0.7.0",
64
+ "standard-version": "^9.3.0",
63
65
  "ts-node": "^9.0.0",
64
- "typedoc": "^0.18.0",
66
+ "typedoc": "^0.21.2",
65
67
  "typescript": "^4.0.2",
66
- "typescript-cached-transpile": "0.0.6"
68
+ "typescript-cached-transpile": "0.0.6",
69
+ "uuid": "^8.3.2"
67
70
  },
68
71
  "config": {
69
72
  "native": false
package/src/binary.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { Buffer } from 'buffer';
2
2
  import { ensureBuffer } from './ensure_buffer';
3
+ import { uuidHexStringToBuffer } from './uuid_utils';
4
+ import { UUID, UUIDExtended } from './uuid';
3
5
  import type { EJSONOptions } from './extended_json';
4
- import { parseUUID, UUIDExtended } from './uuid';
5
6
 
6
7
  /** @public */
7
8
  export type BinarySequence = Uint8Array | Buffer | number[];
@@ -230,6 +231,17 @@ export class Binary {
230
231
  };
231
232
  }
232
233
 
234
+ /** @internal */
235
+ toUUID(): UUID {
236
+ if (this.sub_type === Binary.SUBTYPE_UUID) {
237
+ return new UUID(this.buffer.slice(0, this.position));
238
+ }
239
+
240
+ throw new Error(
241
+ `Binary sub_type "${this.sub_type}" is not supported for converting to UUID. Only "${Binary.SUBTYPE_UUID}" is currently supported.`
242
+ );
243
+ }
244
+
233
245
  /** @internal */
234
246
  static fromExtendedJSON(
235
247
  doc: BinaryExtendedLegacy | BinaryExtended | UUIDExtended,
@@ -250,7 +262,7 @@ export class Binary {
250
262
  }
251
263
  } else if ('$uuid' in doc) {
252
264
  type = 4;
253
- data = Buffer.from(parseUUID(doc.$uuid));
265
+ data = uuidHexStringToBuffer(doc.$uuid);
254
266
  }
255
267
  if (!data) {
256
268
  throw new TypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
package/src/bson.ts CHANGED
@@ -19,6 +19,7 @@ import { serializeInto as internalSerialize, SerializeOptions } from './parser/s
19
19
  import { BSONRegExp } from './regexp';
20
20
  import { BSONSymbol } from './symbol';
21
21
  import { Timestamp } from './timestamp';
22
+ import { UUID } from './uuid';
22
23
  export { BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
23
24
  export { CodeExtended } from './code';
24
25
  export {
@@ -81,6 +82,7 @@ export {
81
82
  DBRef,
82
83
  Binary,
83
84
  ObjectId,
85
+ UUID,
84
86
  Long,
85
87
  Timestamp,
86
88
  Double,
@@ -214,7 +216,7 @@ export function deserialize(
214
216
  buffer: Buffer | ArrayBufferView | ArrayBuffer,
215
217
  options: DeserializeOptions = {}
216
218
  ): Document {
217
- return internalDeserialize(ensureBuffer(buffer), options);
219
+ return internalDeserialize(buffer instanceof Buffer ? buffer : ensureBuffer(buffer), options);
218
220
  }
219
221
 
220
222
  /** @public */
@@ -307,6 +309,7 @@ const BSON = {
307
309
  Double,
308
310
  Int32,
309
311
  Long,
312
+ UUID,
310
313
  Map,
311
314
  MaxKey,
312
315
  MinKey,
package/src/db_ref.ts CHANGED
@@ -12,7 +12,12 @@ export interface DBRefLike {
12
12
 
13
13
  /** @internal */
14
14
  export function isDBRefLike(value: unknown): value is DBRefLike {
15
- return isObjectLike(value) && value['$id'] != null && value['$ref'] != null;
15
+ return (
16
+ isObjectLike(value) &&
17
+ value.$id != null &&
18
+ typeof value.$ref === 'string' &&
19
+ (value.$db == null || typeof value.$db === 'string')
20
+ );
16
21
  }
17
22
 
18
23
  /**
package/src/decimal128.ts CHANGED
@@ -12,59 +12,14 @@ const MAX_DIGITS = 34;
12
12
 
13
13
  // Nan value bits as 32 bit values (due to lack of longs)
14
14
  const NAN_BUFFER = [
15
- 0x7c,
16
- 0x00,
17
- 0x00,
18
- 0x00,
19
- 0x00,
20
- 0x00,
21
- 0x00,
22
- 0x00,
23
- 0x00,
24
- 0x00,
25
- 0x00,
26
- 0x00,
27
- 0x00,
28
- 0x00,
29
- 0x00,
30
- 0x00
15
+ 0x7c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
31
16
  ].reverse();
32
17
  // Infinity value bits 32 bit values (due to lack of longs)
33
18
  const INF_NEGATIVE_BUFFER = [
34
- 0xf8,
35
- 0x00,
36
- 0x00,
37
- 0x00,
38
- 0x00,
39
- 0x00,
40
- 0x00,
41
- 0x00,
42
- 0x00,
43
- 0x00,
44
- 0x00,
45
- 0x00,
46
- 0x00,
47
- 0x00,
48
- 0x00,
49
- 0x00
19
+ 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
50
20
  ].reverse();
51
21
  const INF_POSITIVE_BUFFER = [
52
- 0x78,
53
- 0x00,
54
- 0x00,
55
- 0x00,
56
- 0x00,
57
- 0x00,
58
- 0x00,
59
- 0x00,
60
- 0x00,
61
- 0x00,
62
- 0x00,
63
- 0x00,
64
- 0x00,
65
- 0x00,
66
- 0x00,
67
- 0x00
22
+ 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
68
23
  ].reverse();
69
24
 
70
25
  const EXPONENT_REGEX = /^([-+])?(\d+)?$/;
@@ -167,11 +122,18 @@ export class Decimal128 {
167
122
 
168
123
  readonly bytes!: Buffer;
169
124
 
170
- /** @param bytes - a buffer containing the raw Decimal128 bytes in little endian order */
171
- constructor(bytes: Buffer) {
125
+ /**
126
+ * @param bytes - a buffer containing the raw Decimal128 bytes in little endian order,
127
+ * or a string representation as returned by .toString()
128
+ */
129
+ constructor(bytes: Buffer | string) {
172
130
  if (!(this instanceof Decimal128)) return new Decimal128(bytes);
173
131
 
174
- this.bytes = bytes;
132
+ if (typeof bytes === 'string') {
133
+ this.bytes = Decimal128.fromString(bytes).bytes;
134
+ } else {
135
+ this.bytes = bytes;
136
+ }
175
137
  }
176
138
 
177
139
  /**
@@ -796,7 +758,7 @@ export class Decimal128 {
796
758
  }
797
759
 
798
760
  inspect(): string {
799
- return `Decimal128.fromString("${this.toString()}")`;
761
+ return `new Decimal128("${this.toString()}")`;
800
762
  }
801
763
  }
802
764
 
@@ -1,5 +1,5 @@
1
1
  import { Buffer } from 'buffer';
2
- import { isBuffer } from './parser/utils';
2
+ import { isAnyArrayBuffer } from './parser/utils';
3
3
 
4
4
  /**
5
5
  * Makes sure that, if a Uint8Array is passed in, it is wrapped in a Buffer.
@@ -10,15 +10,15 @@ import { isBuffer } from './parser/utils';
10
10
  * @throws TypeError If anything other than a Buffer or Uint8Array is passed in
11
11
  */
12
12
  export function ensureBuffer(potentialBuffer: Buffer | ArrayBufferView | ArrayBuffer): Buffer {
13
- if (isBuffer(potentialBuffer)) {
14
- return potentialBuffer;
15
- }
16
-
17
13
  if (ArrayBuffer.isView(potentialBuffer)) {
18
- return Buffer.from(potentialBuffer.buffer);
14
+ return Buffer.from(
15
+ potentialBuffer.buffer,
16
+ potentialBuffer.byteOffset,
17
+ potentialBuffer.byteLength
18
+ );
19
19
  }
20
20
 
21
- if (potentialBuffer instanceof ArrayBuffer) {
21
+ if (isAnyArrayBuffer(potentialBuffer)) {
22
22
  return Buffer.from(potentialBuffer);
23
23
  }
24
24