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/src/uuid.ts CHANGED
@@ -1,57 +1,209 @@
1
- /**
2
- * UUID regular expression pattern copied from `uuid` npm module.
3
- * @see https://github.com/uuidjs/uuid/blob/master/src/regex.js
4
- */
5
- 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;
1
+ import { Buffer } from 'buffer';
2
+ import { ensureBuffer } from './ensure_buffer';
3
+ import { Binary } from './binary';
4
+ import { bufferToUuidHexString, uuidHexStringToBuffer, uuidValidateString } from './uuid_utils';
5
+ import { isUint8Array, randomBytes } from './parser/utils';
6
6
 
7
7
  /** @public */
8
- export interface UUIDExtended {
8
+ export type UUIDExtended = {
9
9
  $uuid: string;
10
- }
10
+ };
11
+
12
+ const BYTE_LENGTH = 16;
13
+
14
+ const kId = Symbol('id');
11
15
 
12
16
  /**
13
- * Parser function copied from `uuid` npm module.
14
- * @see https://github.com/uuidjs/uuid/blob/master/src/parse.js
15
- * @internal
17
+ * A class representation of the BSON UUID type.
18
+ * @public
16
19
  */
17
- export function parseUUID(uuid: string): Uint8Array {
18
- if (typeof uuid !== 'string') {
19
- throw new TypeError('Invalid type for UUID, expected string but got ' + typeof uuid);
20
+ export class UUID {
21
+ // This property is not meant for direct serialization, but simply an indication that this type originates from this package.
22
+ _bsontype!: 'UUID';
23
+
24
+ static cacheHexString: boolean;
25
+
26
+ /** UUID Bytes @internal */
27
+ private [kId]: Buffer;
28
+ /** UUID hexString cache @internal */
29
+ private __id?: string;
30
+
31
+ /**
32
+ * Create an UUID type
33
+ *
34
+ * @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
35
+ */
36
+ constructor(input?: string | Buffer | UUID) {
37
+ if (typeof input === 'undefined') {
38
+ // The most common use case (blank id, new UUID() instance)
39
+ this.id = UUID.generate();
40
+ } else if (input instanceof UUID) {
41
+ this[kId] = Buffer.from(input.id);
42
+ this.__id = input.__id;
43
+ } else if (ArrayBuffer.isView(input) && input.byteLength === BYTE_LENGTH) {
44
+ this.id = ensureBuffer(input);
45
+ } else if (typeof input === 'string') {
46
+ this.id = uuidHexStringToBuffer(input);
47
+ } else {
48
+ throw new TypeError(
49
+ '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).'
50
+ );
51
+ }
52
+ }
53
+
54
+ /**
55
+ * The UUID bytes
56
+ * @readonly
57
+ */
58
+ get id(): Buffer {
59
+ return this[kId];
60
+ }
61
+
62
+ set id(value: Buffer) {
63
+ this[kId] = value;
64
+
65
+ if (UUID.cacheHexString) {
66
+ this.__id = bufferToUuidHexString(value);
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Generate a 16 byte uuid v4 buffer used in UUIDs
72
+ */
73
+
74
+ /**
75
+ * Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
76
+ * @param includeDashes - should the string exclude dash-separators.
77
+ * */
78
+ toHexString(includeDashes = true): string {
79
+ if (UUID.cacheHexString && this.__id) {
80
+ return this.__id;
81
+ }
82
+
83
+ const uuidHexString = bufferToUuidHexString(this.id, includeDashes);
84
+
85
+ if (UUID.cacheHexString) {
86
+ this.__id = uuidHexString;
87
+ }
88
+
89
+ return uuidHexString;
20
90
  }
21
91
 
22
- if (!UUID_RX.test(uuid)) {
23
- throw new TypeError('Invalid format for UUID: ' + uuid);
92
+ /**
93
+ * Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
94
+ * @internal
95
+ */
96
+ toString(encoding?: string): string {
97
+ return encoding ? this.id.toString(encoding) : this.toHexString();
24
98
  }
25
99
 
26
- let v;
27
- const arr = new Uint8Array(16);
100
+ /**
101
+ * Converts the id into its JSON string representation. A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
102
+ * @internal
103
+ */
104
+ toJSON(): string {
105
+ return this.toHexString();
106
+ }
107
+
108
+ /**
109
+ * Compares the equality of this UUID with `otherID`.
110
+ *
111
+ * @param otherId - UUID instance to compare against.
112
+ */
113
+ equals(otherId: string | Buffer | UUID): boolean {
114
+ if (!otherId) {
115
+ return false;
116
+ }
117
+
118
+ if (otherId instanceof UUID) {
119
+ return otherId.id.equals(this.id);
120
+ }
121
+
122
+ try {
123
+ return new UUID(otherId).id.equals(this.id);
124
+ } catch {
125
+ return false;
126
+ }
127
+ }
28
128
 
29
- // Parse ########-....-....-....-............
30
- arr[0] = (v = parseInt(uuid.slice(0, 8), 16)) >>> 24;
31
- arr[1] = (v >>> 16) & 0xff;
32
- arr[2] = (v >>> 8) & 0xff;
33
- arr[3] = v & 0xff;
129
+ /**
130
+ * Creates a Binary instance from the current UUID.
131
+ */
132
+ toBinary(): Binary {
133
+ return new Binary(this.id, Binary.SUBTYPE_UUID);
134
+ }
135
+
136
+ /**
137
+ * Generates a populated buffer containing a v4 uuid
138
+ */
139
+ static generate(): Buffer {
140
+ const bytes = randomBytes(BYTE_LENGTH);
141
+
142
+ // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
143
+ // Kindly borrowed from https://github.com/uuidjs/uuid/blob/master/src/v4.js
144
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
145
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
146
+
147
+ return Buffer.from(bytes);
148
+ }
34
149
 
35
- // Parse ........-####-....-....-............
36
- arr[4] = (v = parseInt(uuid.slice(9, 13), 16)) >>> 8;
37
- arr[5] = v & 0xff;
150
+ /**
151
+ * Checks if a value is a valid bson UUID
152
+ * @param input - UUID, string or Buffer to validate.
153
+ */
154
+ static isValid(input: string | Buffer | UUID): boolean {
155
+ if (!input) {
156
+ return false;
157
+ }
38
158
 
39
- // Parse ........-....-####-....-............
40
- arr[6] = (v = parseInt(uuid.slice(14, 18), 16)) >>> 8;
41
- arr[7] = v & 0xff;
159
+ if (input instanceof UUID) {
160
+ return true;
161
+ }
42
162
 
43
- // Parse ........-....-....-####-............
44
- arr[8] = (v = parseInt(uuid.slice(19, 23), 16)) >>> 8;
45
- arr[9] = v & 0xff;
163
+ if (typeof input === 'string') {
164
+ return uuidValidateString(input);
165
+ }
46
166
 
47
- // Parse ........-....-....-....-############
48
- // (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
49
- arr[10] = ((v = parseInt(uuid.slice(24, 36), 16)) / 0x10000000000) & 0xff;
50
- arr[11] = (v / 0x100000000) & 0xff;
51
- arr[12] = (v >>> 24) & 0xff;
52
- arr[13] = (v >>> 16) & 0xff;
53
- arr[14] = (v >>> 8) & 0xff;
54
- arr[15] = v & 0xff;
167
+ if (isUint8Array(input)) {
168
+ // check for length & uuid version (https://tools.ietf.org/html/rfc4122#section-4.1.3)
169
+ if (input.length !== BYTE_LENGTH) {
170
+ return false;
171
+ }
55
172
 
56
- return arr;
173
+ try {
174
+ // get this byte as hex: xxxxxxxx-xxxx-XXxx-xxxx-xxxxxxxxxxxx
175
+ // check first part as uuid version: xxxxxxxx-xxxx-Xxxx-xxxx-xxxxxxxxxxxx
176
+ return parseInt(input[6].toString(16)[0], 10) === Binary.SUBTYPE_UUID;
177
+ } catch {
178
+ return false;
179
+ }
180
+ }
181
+
182
+ return false;
183
+ }
184
+
185
+ /**
186
+ * Creates an UUID from a hex string representation of an UUID.
187
+ * @param hexString - 32 or 36 character hex string (dashes excluded/included).
188
+ */
189
+ static createFromHexString(hexString: string): UUID {
190
+ const buffer = uuidHexStringToBuffer(hexString);
191
+ return new UUID(buffer);
192
+ }
193
+
194
+ /**
195
+ * Converts to a string representation of this Id.
196
+ *
197
+ * @returns return the 36 character hex string representation.
198
+ * @internal
199
+ */
200
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
201
+ return this.inspect();
202
+ }
203
+
204
+ inspect(): string {
205
+ return `new UUID("${this.toHexString()}")`;
206
+ }
57
207
  }
208
+
209
+ Object.defineProperty(UUID.prototype, '_bsontype', { value: 'UUID' });
@@ -0,0 +1,32 @@
1
+ import { Buffer } from 'buffer';
2
+
3
+ // Validation regex for v4 uuid (validates with or without dashes)
4
+ const VALIDATION_REGEX =
5
+ /^(?:[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;
6
+
7
+ export const uuidValidateString = (str: string): boolean =>
8
+ typeof str === 'string' && VALIDATION_REGEX.test(str);
9
+
10
+ export const uuidHexStringToBuffer = (hexString: string): Buffer => {
11
+ if (!uuidValidateString(hexString)) {
12
+ throw new TypeError(
13
+ '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
+ }
16
+
17
+ const sanitizedHexString = hexString.replace(/-/g, '');
18
+ return Buffer.from(sanitizedHexString, 'hex');
19
+ };
20
+
21
+ export const bufferToUuidHexString = (buffer: Buffer, includeDashes = true): string =>
22
+ includeDashes
23
+ ? buffer.toString('hex', 0, 4) +
24
+ '-' +
25
+ buffer.toString('hex', 4, 6) +
26
+ '-' +
27
+ buffer.toString('hex', 6, 8) +
28
+ '-' +
29
+ buffer.toString('hex', 8, 10) +
30
+ '-' +
31
+ buffer.toString('hex', 10, 16)
32
+ : buffer.toString('hex');