bson 4.2.2 → 4.4.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 (84) hide show
  1. package/HISTORY.md +44 -0
  2. package/bower.json +1 -1
  3. package/bson.d.ts +77 -6
  4. package/dist/bson.browser.esm.js +2116 -1292
  5. package/dist/bson.browser.esm.js.map +1 -1
  6. package/dist/bson.browser.umd.js +2172 -1347
  7. package/dist/bson.browser.umd.js.map +1 -1
  8. package/dist/bson.bundle.js +2174 -1347
  9. package/dist/bson.bundle.js.map +1 -1
  10. package/dist/bson.esm.js +2061 -1241
  11. package/dist/bson.esm.js.map +1 -1
  12. package/lib/binary.js +71 -60
  13. package/lib/binary.js.map +1 -1
  14. package/lib/bson.js +60 -52
  15. package/lib/bson.js.map +1 -1
  16. package/lib/code.js +18 -15
  17. package/lib/code.js.map +1 -1
  18. package/lib/db_ref.js +40 -30
  19. package/lib/db_ref.js.map +1 -1
  20. package/lib/decimal128.js +135 -124
  21. package/lib/decimal128.js.map +1 -1
  22. package/lib/double.js +24 -21
  23. package/lib/double.js.map +1 -1
  24. package/lib/ensure_buffer.js +4 -7
  25. package/lib/ensure_buffer.js.map +1 -1
  26. package/lib/extended_json.js +113 -71
  27. package/lib/extended_json.js.map +1 -1
  28. package/lib/float_parser.js +21 -21
  29. package/lib/float_parser.js.map +1 -1
  30. package/lib/int_32.js +19 -16
  31. package/lib/int_32.js.map +1 -1
  32. package/lib/long.js +248 -230
  33. package/lib/long.js.map +1 -1
  34. package/lib/map.js +54 -45
  35. package/lib/map.js.map +1 -1
  36. package/lib/max_key.js +16 -11
  37. package/lib/max_key.js.map +1 -1
  38. package/lib/min_key.js +16 -11
  39. package/lib/min_key.js.map +1 -1
  40. package/lib/objectid.js +96 -93
  41. package/lib/objectid.js.map +1 -1
  42. package/lib/parser/calculate_size.js +17 -15
  43. package/lib/parser/calculate_size.js.map +1 -1
  44. package/lib/parser/deserializer.js +135 -123
  45. package/lib/parser/deserializer.js.map +1 -1
  46. package/lib/parser/serializer.js +108 -88
  47. package/lib/parser/serializer.js.map +1 -1
  48. package/lib/parser/utils.js +47 -25
  49. package/lib/parser/utils.js.map +1 -1
  50. package/lib/regexp.js +16 -15
  51. package/lib/regexp.js.map +1 -1
  52. package/lib/symbol.js +21 -18
  53. package/lib/symbol.js.map +1 -1
  54. package/lib/timestamp.js +50 -27
  55. package/lib/timestamp.js.map +1 -1
  56. package/lib/uuid.js +173 -42
  57. package/lib/uuid.js.map +1 -1
  58. package/lib/uuid_utils.js +34 -0
  59. package/lib/uuid_utils.js.map +1 -0
  60. package/lib/validate_utf8.js +12 -12
  61. package/lib/validate_utf8.js.map +1 -1
  62. package/package.json +5 -4
  63. package/src/binary.ts +20 -6
  64. package/src/bson.ts +3 -0
  65. package/src/code.ts +6 -2
  66. package/src/db_ref.ts +14 -5
  67. package/src/decimal128.ts +14 -5
  68. package/src/double.ts +4 -2
  69. package/src/ensure_buffer.ts +7 -7
  70. package/src/extended_json.ts +64 -16
  71. package/src/int_32.ts +4 -2
  72. package/src/long.ts +22 -8
  73. package/src/max_key.ts +5 -1
  74. package/src/min_key.ts +5 -1
  75. package/src/objectid.ts +15 -20
  76. package/src/parser/calculate_size.ts +8 -11
  77. package/src/parser/deserializer.ts +46 -36
  78. package/src/parser/serializer.ts +13 -12
  79. package/src/parser/utils.ts +49 -17
  80. package/src/regexp.ts +5 -5
  81. package/src/symbol.ts +4 -2
  82. package/src/timestamp.ts +6 -1
  83. package/src/uuid.ts +192 -40
  84. package/src/uuid_utils.ts +31 -0
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
- const 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
- let v;
22
- const 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,MAAM,OAAO,GAAG,qHAAqH,CAAC;AAOtI;;;;GAIG;AACH,SAAgB,SAAS,CAAC,IAAY;IACpC,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE;QAC5B,MAAM,IAAI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exStringToBuffer = 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,GAAG,uHAAuH,CAAC;AAE1I,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"}
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.validateUtf8 = void 0;
4
- const FIRST_BIT = 0x80;
5
- const FIRST_TWO_BITS = 0xc0;
6
- const FIRST_THREE_BITS = 0xe0;
7
- const FIRST_FOUR_BITS = 0xf0;
8
- const FIRST_FIVE_BITS = 0xf8;
9
- const TWO_BIT_CHAR = 0xc0;
10
- const THREE_BIT_CHAR = 0xe0;
11
- const FOUR_BIT_CHAR = 0xf0;
12
- const CONTINUING_CHAR = 0x80;
4
+ var FIRST_BIT = 0x80;
5
+ var FIRST_TWO_BITS = 0xc0;
6
+ var FIRST_THREE_BITS = 0xe0;
7
+ var FIRST_FOUR_BITS = 0xf0;
8
+ var FIRST_FIVE_BITS = 0xf8;
9
+ var TWO_BIT_CHAR = 0xc0;
10
+ var THREE_BIT_CHAR = 0xe0;
11
+ var FOUR_BIT_CHAR = 0xf0;
12
+ var CONTINUING_CHAR = 0x80;
13
13
  /**
14
14
  * Determines if the passed in bytes are valid utf8
15
15
  * @param bytes - An array of 8-bit bytes. Must be indexable and have length property
@@ -17,9 +17,9 @@ const CONTINUING_CHAR = 0x80;
17
17
  * @param end - The index to end validating
18
18
  */
19
19
  function validateUtf8(bytes, start, end) {
20
- let continuation = 0;
21
- for (let i = start; i < end; i += 1) {
22
- const byte = bytes[i];
20
+ var continuation = 0;
21
+ for (var i = start; i < end; i += 1) {
22
+ var byte = bytes[i];
23
23
  if (continuation) {
24
24
  if ((byte & FIRST_TWO_BITS) !== CONTINUING_CHAR) {
25
25
  return false;
@@ -1 +1 @@
1
- {"version":3,"file":"validate_utf8.js","sourceRoot":"","sources":["../src/validate_utf8.ts"],"names":[],"mappings":";;;AAAA,MAAM,SAAS,GAAG,IAAI,CAAC;AACvB,MAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAC9B,MAAM,eAAe,GAAG,IAAI,CAAC;AAC7B,MAAM,eAAe,GAAG,IAAI,CAAC;AAE7B,MAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,MAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,MAAM,aAAa,GAAG,IAAI,CAAC;AAC3B,MAAM,eAAe,GA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AC;AAC3B,IAAM,eAAe,GAAG,IAAI,CAAC;AAE7B;;;;;GAKG;AACH,SAAgB,YAAY,CAC1B,KAAkC,EAClC,KAAa,EACb,GAAW;IAEX,IAAI,YAAY,GAAG,CAAC,CAAC;IAErB,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE;QACnC,IAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QAEtB,IAAI,YAAY,EAAE;YAChB,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC,KAAK,eAAe,EAAE;gBAC/C,OAAO,KAAK,CAAC;aACd;YACD,YAAY,IAAI,CAAC,CAAC;SACnB;aAAM,IAAI,IAAI,GAAG,SAAS,EAAE;YAC3B,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC,KAAK,YAAY,EAAE;gBAC9C,YAAY,GAAG,CAAC,CAAC;aAClB;iBAAM,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC,KAAK,cAAc,EAAE;gBACtD,YAAY,GAAG,CAAC,CAAC;aAClB;iBAAM,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC,KAAK,aAAa,EAAE;gBACrD,YAAY,GAAG,CAAC,CAAC;aAClB;iBAAM;gBACL,OAAO,KAAK,CAAC;aACd;SACF;KACF;IAED,OAAO,CAAC,YAAY,CAAC;AACvB,CAAC;AA7BD,oCA6BC"}
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "bower.json"
16
16
  ],
17
17
  "types": "bson.d.ts",
18
- "version": "4.2.2",
18
+ "version": "4.4.1",
19
19
  "author": "Christian Amor Kvalheim <christkv@gmail.com>",
20
20
  "license": "Apache-2.0",
21
21
  "contributors": [],
@@ -59,11 +59,12 @@
59
59
  "rollup-plugin-node-builtins": "^2.1.2",
60
60
  "rollup-plugin-node-globals": "^1.4.0",
61
61
  "rollup-plugin-node-polyfills": "^0.2.1",
62
- "standard-version": "^8.0.1",
62
+ "standard-version": "^9.3.0",
63
63
  "ts-node": "^9.0.0",
64
- "typedoc": "^0.18.0",
64
+ "typedoc": "^0.21.2",
65
65
  "typescript": "^4.0.2",
66
- "typescript-cached-transpile": "0.0.6"
66
+ "typescript-cached-transpile": "0.0.6",
67
+ "uuid": "^8.3.2"
67
68
  },
68
69
  "config": {
69
70
  "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[];
@@ -50,15 +51,17 @@ export class Binary {
50
51
  /** User BSON type */
51
52
  static readonly SUBTYPE_USER_DEFINED = 128;
52
53
 
53
- buffer: Buffer;
54
- sub_type: number;
55
- position: number;
54
+ buffer!: Buffer;
55
+ sub_type!: number;
56
+ position!: number;
56
57
 
57
58
  /**
58
59
  * @param buffer - a buffer object containing the binary data.
59
60
  * @param subType - the option binary type.
60
61
  */
61
62
  constructor(buffer?: string | BinarySequence, subType?: number) {
63
+ if (!(this instanceof Binary)) return new Binary(buffer, subType);
64
+
62
65
  if (
63
66
  !(buffer == null) &&
64
67
  !(typeof buffer === 'string') &&
@@ -228,6 +231,17 @@ export class Binary {
228
231
  };
229
232
  }
230
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
+
231
245
  /** @internal */
232
246
  static fromExtendedJSON(
233
247
  doc: BinaryExtendedLegacy | BinaryExtended | UUIDExtended,
@@ -248,7 +262,7 @@ export class Binary {
248
262
  }
249
263
  } else if ('$uuid' in doc) {
250
264
  type = 4;
251
- data = Buffer.from(parseUUID(doc.$uuid));
265
+ data = uuidHexStringToBuffer(doc.$uuid);
252
266
  }
253
267
  if (!data) {
254
268
  throw new TypeError(`Unexpected Binary Extended JSON format ${JSON.stringify(doc)}`);
@@ -263,7 +277,7 @@ export class Binary {
263
277
 
264
278
  inspect(): string {
265
279
  const asBuffer = this.value(true);
266
- return `Binary("${asBuffer.toString('hex')}", ${this.sub_type})`;
280
+ return `new Binary(Buffer.from("${asBuffer.toString('hex')}", "hex"), ${this.sub_type})`;
267
281
  }
268
282
  }
269
283
 
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,
@@ -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/code.ts CHANGED
@@ -13,13 +13,15 @@ export interface CodeExtended {
13
13
  export class Code {
14
14
  _bsontype!: 'Code';
15
15
 
16
- code: string | Function;
16
+ code!: string | Function;
17
17
  scope?: Document;
18
18
  /**
19
19
  * @param code - a string or function.
20
20
  * @param scope - an optional scope for the function.
21
21
  */
22
22
  constructor(code: string | Function, scope?: Document) {
23
+ if (!(this instanceof Code)) return new Code(code, scope);
24
+
23
25
  this.code = code;
24
26
  this.scope = scope;
25
27
  }
@@ -50,7 +52,9 @@ export class Code {
50
52
 
51
53
  inspect(): string {
52
54
  const codeJson = this.toJSON();
53
- return `Code("${codeJson.code}"${codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
55
+ return `new Code("${codeJson.code}"${
56
+ codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''
57
+ })`;
54
58
  }
55
59
  }
56
60
 
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
  /**
@@ -22,10 +27,10 @@ export function isDBRefLike(value: unknown): value is DBRefLike {
22
27
  export class DBRef {
23
28
  _bsontype!: 'DBRef';
24
29
 
25
- collection: string;
26
- oid: ObjectId;
30
+ collection!: string;
31
+ oid!: ObjectId;
27
32
  db?: string;
28
- fields: Document;
33
+ fields!: Document;
29
34
 
30
35
  /**
31
36
  * @param collection - the collection name.
@@ -33,6 +38,8 @@ export class DBRef {
33
38
  * @param db - optional db name, if omitted the reference is local to the current db.
34
39
  */
35
40
  constructor(collection: string, oid: ObjectId, db?: string, fields?: Document) {
41
+ if (!(this instanceof DBRef)) return new DBRef(collection, oid, db, fields);
42
+
36
43
  // check if namespace has been provided
37
44
  const parts = collection.split('.');
38
45
  if (parts.length === 2) {
@@ -108,7 +115,9 @@ export class DBRef {
108
115
  // NOTE: if OID is an ObjectId class it will just print the oid string.
109
116
  const oid =
110
117
  this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
111
- return `DBRef("${this.namespace}", "${oid}"${this.db ? `, "${this.db}"` : ''})`;
118
+ return `new DBRef("${this.namespace}", new ObjectId("${oid}")${
119
+ this.db ? `, "${this.db}"` : ''
120
+ })`;
112
121
  }
113
122
  }
114
123
 
package/src/decimal128.ts CHANGED
@@ -165,11 +165,20 @@ export interface Decimal128Extended {
165
165
  export class Decimal128 {
166
166
  _bsontype!: 'Decimal128';
167
167
 
168
- readonly bytes: Buffer;
168
+ readonly bytes!: Buffer;
169
169
 
170
- /** @param bytes - a buffer containing the raw Decimal128 bytes in little endian order */
171
- constructor(bytes: Buffer) {
172
- this.bytes = bytes;
170
+ /**
171
+ * @param bytes - a buffer containing the raw Decimal128 bytes in little endian order,
172
+ * or a string representation as returned by .toString()
173
+ */
174
+ constructor(bytes: Buffer | string) {
175
+ if (!(this instanceof Decimal128)) return new Decimal128(bytes);
176
+
177
+ if (typeof bytes === 'string') {
178
+ this.bytes = Decimal128.fromString(bytes).bytes;
179
+ } else {
180
+ this.bytes = bytes;
181
+ }
173
182
  }
174
183
 
175
184
  /**
@@ -794,7 +803,7 @@ export class Decimal128 {
794
803
  }
795
804
 
796
805
  inspect(): string {
797
- return `Decimal128("${this.toString()}")`;
806
+ return `new Decimal128("${this.toString()}")`;
798
807
  }
799
808
  }
800
809
 
package/src/double.ts CHANGED
@@ -12,13 +12,15 @@ export interface DoubleExtended {
12
12
  export class Double {
13
13
  _bsontype!: 'Double';
14
14
 
15
- value: number;
15
+ value!: number;
16
16
  /**
17
17
  * Create a Double type
18
18
  *
19
19
  * @param value - the number we want to represent as a double.
20
20
  */
21
21
  constructor(value: number) {
22
+ if (!(this instanceof Double)) return new Double(value);
23
+
22
24
  if ((value as unknown) instanceof Number) {
23
25
  value = value.valueOf();
24
26
  }
@@ -78,7 +80,7 @@ export class Double {
78
80
 
79
81
  inspect(): string {
80
82
  const eJSON = this.toExtendedJSON() as DoubleExtended;
81
- return `Double(${eJSON.$numberDouble})`;
83
+ return `new Double(${eJSON.$numberDouble})`;
82
84
  }
83
85
  }
84
86
 
@@ -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