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.
- package/bower.json +1 -1
- package/bson.d.ts +84 -9
- package/dist/bson.browser.esm.js +991 -2292
- package/dist/bson.browser.esm.js.map +1 -1
- package/dist/bson.browser.umd.js +1095 -2397
- package/dist/bson.browser.umd.js.map +1 -1
- package/dist/bson.bundle.js +1097 -2397
- package/dist/bson.bundle.js.map +1 -1
- package/dist/bson.esm.js +945 -2254
- package/dist/bson.esm.js.map +1 -1
- package/lib/binary.js +9 -1
- package/lib/binary.js.map +1 -1
- package/lib/bson.js +6 -2
- package/lib/bson.js.map +1 -1
- package/lib/db_ref.js +4 -1
- package/lib/db_ref.js.map +1 -1
- package/lib/decimal128.js +14 -51
- package/lib/decimal128.js.map +1 -1
- package/lib/ensure_buffer.js +2 -5
- package/lib/ensure_buffer.js.map +1 -1
- package/lib/extended_json.js +48 -9
- package/lib/extended_json.js.map +1 -1
- package/lib/long.js +18 -5
- package/lib/long.js.map +1 -1
- package/lib/map.js +3 -15
- package/lib/map.js.map +1 -1
- package/lib/objectid.js +9 -17
- package/lib/objectid.js.map +1 -1
- package/lib/parser/calculate_size.js +5 -6
- package/lib/parser/calculate_size.js.map +1 -1
- package/lib/parser/deserializer.js +61 -47
- package/lib/parser/deserializer.js.map +1 -1
- package/lib/parser/serializer.js +14 -13
- package/lib/parser/serializer.js.map +1 -1
- package/lib/parser/utils.js +44 -27
- package/lib/parser/utils.js.map +1 -1
- package/lib/regexp.js +1 -3
- package/lib/regexp.js.map +1 -1
- package/lib/timestamp.js +8 -2
- package/lib/timestamp.js.map +1 -1
- package/lib/utils/global.js +18 -0
- package/lib/utils/global.js.map +1 -0
- package/lib/uuid.js +173 -42
- package/lib/uuid.js.map +1 -1
- package/lib/uuid_utils.js +34 -0
- package/lib/uuid_utils.js.map +1 -0
- package/package.json +12 -9
- package/src/binary.ts +14 -2
- package/src/bson.ts +4 -1
- package/src/db_ref.ts +6 -1
- package/src/decimal128.ts +14 -52
- package/src/ensure_buffer.ts +7 -7
- package/src/extended_json.ts +64 -16
- package/src/long.ts +19 -7
- package/src/map.ts +5 -25
- package/src/objectid.ts +12 -19
- package/src/parser/calculate_size.ts +8 -11
- package/src/parser/deserializer.ts +68 -51
- package/src/parser/serializer.ts +13 -12
- package/src/parser/utils.ts +56 -18
- package/src/regexp.ts +2 -4
- package/src/timestamp.ts +15 -7
- package/src/utils/global.ts +22 -0
- package/src/uuid.ts +192 -40
- package/src/uuid_utils.ts +32 -0
- package/HISTORY.md +0 -481
package/src/uuid.ts
CHANGED
|
@@ -1,57 +1,209 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
|
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
|
-
*
|
|
14
|
-
* @
|
|
15
|
-
* @internal
|
|
17
|
+
* A class representation of the BSON UUID type.
|
|
18
|
+
* @public
|
|
16
19
|
*/
|
|
17
|
-
export
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
23
|
-
|
|
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
|
-
|
|
27
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
159
|
+
if (input instanceof UUID) {
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
42
162
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
163
|
+
if (typeof input === 'string') {
|
|
164
|
+
return uuidValidateString(input);
|
|
165
|
+
}
|
|
46
166
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
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');
|