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.
- package/HISTORY.md +44 -0
- package/bower.json +1 -1
- package/bson.d.ts +77 -6
- package/dist/bson.browser.esm.js +2116 -1292
- package/dist/bson.browser.esm.js.map +1 -1
- package/dist/bson.browser.umd.js +2172 -1347
- package/dist/bson.browser.umd.js.map +1 -1
- package/dist/bson.bundle.js +2174 -1347
- package/dist/bson.bundle.js.map +1 -1
- package/dist/bson.esm.js +2061 -1241
- package/dist/bson.esm.js.map +1 -1
- package/lib/binary.js +71 -60
- package/lib/binary.js.map +1 -1
- package/lib/bson.js +60 -52
- package/lib/bson.js.map +1 -1
- package/lib/code.js +18 -15
- package/lib/code.js.map +1 -1
- package/lib/db_ref.js +40 -30
- package/lib/db_ref.js.map +1 -1
- package/lib/decimal128.js +135 -124
- package/lib/decimal128.js.map +1 -1
- package/lib/double.js +24 -21
- package/lib/double.js.map +1 -1
- package/lib/ensure_buffer.js +4 -7
- package/lib/ensure_buffer.js.map +1 -1
- package/lib/extended_json.js +113 -71
- package/lib/extended_json.js.map +1 -1
- package/lib/float_parser.js +21 -21
- package/lib/float_parser.js.map +1 -1
- package/lib/int_32.js +19 -16
- package/lib/int_32.js.map +1 -1
- package/lib/long.js +248 -230
- package/lib/long.js.map +1 -1
- package/lib/map.js +54 -45
- package/lib/map.js.map +1 -1
- package/lib/max_key.js +16 -11
- package/lib/max_key.js.map +1 -1
- package/lib/min_key.js +16 -11
- package/lib/min_key.js.map +1 -1
- package/lib/objectid.js +96 -93
- package/lib/objectid.js.map +1 -1
- package/lib/parser/calculate_size.js +17 -15
- package/lib/parser/calculate_size.js.map +1 -1
- package/lib/parser/deserializer.js +135 -123
- package/lib/parser/deserializer.js.map +1 -1
- package/lib/parser/serializer.js +108 -88
- package/lib/parser/serializer.js.map +1 -1
- package/lib/parser/utils.js +47 -25
- package/lib/parser/utils.js.map +1 -1
- package/lib/regexp.js +16 -15
- package/lib/regexp.js.map +1 -1
- package/lib/symbol.js +21 -18
- package/lib/symbol.js.map +1 -1
- package/lib/timestamp.js +50 -27
- package/lib/timestamp.js.map +1 -1
- 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/lib/validate_utf8.js +12 -12
- package/lib/validate_utf8.js.map +1 -1
- package/package.json +5 -4
- package/src/binary.ts +20 -6
- package/src/bson.ts +3 -0
- package/src/code.ts +6 -2
- package/src/db_ref.ts +14 -5
- package/src/decimal128.ts +14 -5
- package/src/double.ts +4 -2
- package/src/ensure_buffer.ts +7 -7
- package/src/extended_json.ts +64 -16
- package/src/int_32.ts +4 -2
- package/src/long.ts +22 -8
- package/src/max_key.ts +5 -1
- package/src/min_key.ts +5 -1
- package/src/objectid.ts +15 -20
- package/src/parser/calculate_size.ts +8 -11
- package/src/parser/deserializer.ts +46 -36
- package/src/parser/serializer.ts +13 -12
- package/src/parser/utils.ts +49 -17
- package/src/regexp.ts +5 -5
- package/src/symbol.ts +4 -2
- package/src/timestamp.ts +6 -1
- package/src/uuid.ts +192 -40
- package/src/uuid_utils.ts +31 -0
package/src/parser/serializer.ts
CHANGED
|
@@ -18,8 +18,9 @@ import type { BSONRegExp } from '../regexp';
|
|
|
18
18
|
import {
|
|
19
19
|
isBigInt64Array,
|
|
20
20
|
isBigUInt64Array,
|
|
21
|
-
isBuffer,
|
|
22
21
|
isDate,
|
|
22
|
+
isMap,
|
|
23
|
+
isRegExp,
|
|
23
24
|
isUint8Array,
|
|
24
25
|
normalizedFunctionString
|
|
25
26
|
} from './utils';
|
|
@@ -41,10 +42,6 @@ export interface SerializeOptions {
|
|
|
41
42
|
const regexp = /\x00/; // eslint-disable-line no-control-regex
|
|
42
43
|
const ignoreKeys = new Set(['$db', '$ref', '$id', '$clusterTime']);
|
|
43
44
|
|
|
44
|
-
function isRegExp(d: unknown): d is RegExp {
|
|
45
|
-
return Object.prototype.toString.call(d) === '[object RegExp]';
|
|
46
|
-
}
|
|
47
|
-
|
|
48
45
|
/*
|
|
49
46
|
* isArray indicates if we are writing to a BSON array (type 0x04)
|
|
50
47
|
* which forces the "key" which really an array index as a string to be written as ascii
|
|
@@ -309,8 +306,10 @@ function serializeObjectId(
|
|
|
309
306
|
// Write the objectId into the shared buffer
|
|
310
307
|
if (typeof value.id === 'string') {
|
|
311
308
|
buffer.write(value.id, index, undefined, 'binary');
|
|
312
|
-
} else if (value.id
|
|
313
|
-
|
|
309
|
+
} else if (isUint8Array(value.id)) {
|
|
310
|
+
// Use the standard JS methods here because buffer.copy() is buggy with the
|
|
311
|
+
// browser polyfill
|
|
312
|
+
buffer.set(value.id.subarray(0, 12), index);
|
|
314
313
|
} else {
|
|
315
314
|
throw new TypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
|
|
316
315
|
}
|
|
@@ -409,7 +408,9 @@ function serializeDecimal128(
|
|
|
409
408
|
index = index + numberOfWrittenBytes;
|
|
410
409
|
buffer[index++] = 0;
|
|
411
410
|
// Write the data from the value
|
|
412
|
-
|
|
411
|
+
// Prefer the standard JS methods because their typechecking is not buggy,
|
|
412
|
+
// unlike the `buffer` polyfill's.
|
|
413
|
+
buffer.set(value.bytes.subarray(0, 16), index);
|
|
413
414
|
return index + 16;
|
|
414
415
|
}
|
|
415
416
|
|
|
@@ -786,7 +787,7 @@ export function serializeInto(
|
|
|
786
787
|
index = serializeNull(buffer, key, value, index, true);
|
|
787
788
|
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
|
|
788
789
|
index = serializeObjectId(buffer, key, value, index, true);
|
|
789
|
-
} else if (
|
|
790
|
+
} else if (isUint8Array(value)) {
|
|
790
791
|
index = serializeBuffer(buffer, key, value, index, true);
|
|
791
792
|
} else if (value instanceof RegExp || isRegExp(value)) {
|
|
792
793
|
index = serializeRegExp(buffer, key, value, index, true);
|
|
@@ -843,7 +844,7 @@ export function serializeInto(
|
|
|
843
844
|
throw new TypeError('Unrecognized or invalid _bsontype: ' + value['_bsontype']);
|
|
844
845
|
}
|
|
845
846
|
}
|
|
846
|
-
} else if (object instanceof Map) {
|
|
847
|
+
} else if (object instanceof Map || isMap(object)) {
|
|
847
848
|
const iterator = object.entries();
|
|
848
849
|
let done = false;
|
|
849
850
|
|
|
@@ -892,7 +893,7 @@ export function serializeInto(
|
|
|
892
893
|
index = serializeNull(buffer, key, value, index);
|
|
893
894
|
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
|
|
894
895
|
index = serializeObjectId(buffer, key, value, index);
|
|
895
|
-
} else if (
|
|
896
|
+
} else if (isUint8Array(value)) {
|
|
896
897
|
index = serializeBuffer(buffer, key, value, index);
|
|
897
898
|
} else if (value instanceof RegExp || isRegExp(value)) {
|
|
898
899
|
index = serializeRegExp(buffer, key, value, index);
|
|
@@ -998,7 +999,7 @@ export function serializeInto(
|
|
|
998
999
|
index = serializeNull(buffer, key, value, index);
|
|
999
1000
|
} else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
|
|
1000
1001
|
index = serializeObjectId(buffer, key, value, index);
|
|
1001
|
-
} else if (
|
|
1002
|
+
} else if (isUint8Array(value)) {
|
|
1002
1003
|
index = serializeBuffer(buffer, key, value, index);
|
|
1003
1004
|
} else if (value instanceof RegExp || isRegExp(value)) {
|
|
1004
1005
|
index = serializeRegExp(buffer, key, value, index);
|
package/src/parser/utils.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Buffer } from 'buffer';
|
|
2
2
|
|
|
3
|
+
type RandomBytesFunction = (size: number) => Uint8Array;
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* Normalizes our expected stringified form of a function across versions of node
|
|
5
7
|
* @param fn - The function to stringify
|
|
@@ -8,11 +10,20 @@ export function normalizedFunctionString(fn: Function): string {
|
|
|
8
10
|
return fn.toString().replace('function(', 'function (');
|
|
9
11
|
}
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
const isReactNative =
|
|
14
|
+
typeof global.navigator === 'object' && global.navigator.product === 'ReactNative';
|
|
15
|
+
|
|
16
|
+
const insecureWarning = isReactNative
|
|
17
|
+
? 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
|
|
18
|
+
: 'BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.';
|
|
19
|
+
|
|
20
|
+
const insecureRandomBytes: RandomBytesFunction = function insecureRandomBytes(size: number) {
|
|
21
|
+
console.warn(insecureWarning);
|
|
22
|
+
|
|
12
23
|
const result = Buffer.alloc(size);
|
|
13
24
|
for (let i = 0; i < size; ++i) result[i] = Math.floor(Math.random() * 256);
|
|
14
25
|
return result;
|
|
15
|
-
}
|
|
26
|
+
};
|
|
16
27
|
|
|
17
28
|
/* We do not want to have to include DOM types just for this check */
|
|
18
29
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -22,21 +33,39 @@ declare let require: Function;
|
|
|
22
33
|
declare let global: any;
|
|
23
34
|
declare const self: unknown;
|
|
24
35
|
|
|
25
|
-
|
|
26
|
-
if (typeof window !== 'undefined'
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
const detectRandomBytes = (): RandomBytesFunction => {
|
|
37
|
+
if (typeof window !== 'undefined') {
|
|
38
|
+
// browser crypto implementation(s)
|
|
39
|
+
const target = window.crypto || window.msCrypto; // allow for IE11
|
|
40
|
+
if (target && target.getRandomValues) {
|
|
41
|
+
return size => target.getRandomValues(Buffer.alloc(size));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (typeof global !== 'undefined' && global.crypto && global.crypto.getRandomValues) {
|
|
46
|
+
// allow for RN packages such as https://www.npmjs.com/package/react-native-get-random-values to populate global
|
|
47
|
+
return size => global.crypto.getRandomValues(Buffer.alloc(size));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let requiredRandomBytes: RandomBytesFunction | null | undefined;
|
|
29
51
|
try {
|
|
30
52
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
31
|
-
|
|
53
|
+
requiredRandomBytes = require('crypto').randomBytes;
|
|
32
54
|
} catch (e) {
|
|
33
55
|
// keep the fallback
|
|
34
56
|
}
|
|
35
57
|
|
|
36
58
|
// NOTE: in transpiled cases the above require might return null/undefined
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
59
|
+
|
|
60
|
+
return requiredRandomBytes || insecureRandomBytes;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const randomBytes = detectRandomBytes();
|
|
64
|
+
|
|
65
|
+
export function isAnyArrayBuffer(value: unknown): value is ArrayBuffer {
|
|
66
|
+
return ['[object ArrayBuffer]', '[object SharedArrayBuffer]'].includes(
|
|
67
|
+
Object.prototype.toString.call(value)
|
|
68
|
+
);
|
|
40
69
|
}
|
|
41
70
|
|
|
42
71
|
export function isUint8Array(value: unknown): value is Uint8Array {
|
|
@@ -51,16 +80,19 @@ export function isBigUInt64Array(value: unknown): value is BigUint64Array {
|
|
|
51
80
|
return Object.prototype.toString.call(value) === '[object BigUint64Array]';
|
|
52
81
|
}
|
|
53
82
|
|
|
83
|
+
export function isRegExp(d: unknown): d is RegExp {
|
|
84
|
+
return Object.prototype.toString.call(d) === '[object RegExp]';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function isMap(d: unknown): d is Map<unknown, unknown> {
|
|
88
|
+
return Object.prototype.toString.call(d) === '[object Map]';
|
|
89
|
+
}
|
|
90
|
+
|
|
54
91
|
/** Call to check if your environment has `Buffer` */
|
|
55
92
|
export function haveBuffer(): boolean {
|
|
56
93
|
return typeof global !== 'undefined' && typeof global.Buffer !== 'undefined';
|
|
57
94
|
}
|
|
58
95
|
|
|
59
|
-
/** Callable in any environment to check if value is a Buffer */
|
|
60
|
-
export function isBuffer(value: unknown): value is Buffer {
|
|
61
|
-
return typeof value === 'object' && value?.constructor?.name === 'Buffer';
|
|
62
|
-
}
|
|
63
|
-
|
|
64
96
|
// To ensure that 0.4 of node works correctly
|
|
65
97
|
export function isDate(d: unknown): d is Date {
|
|
66
98
|
return isObjectLike(d) && Object.prototype.toString.call(d) === '[object Date]';
|
|
@@ -77,7 +109,7 @@ export function isObjectLike(candidate: unknown): candidate is Record<string, un
|
|
|
77
109
|
|
|
78
110
|
declare let console: { warn(...message: unknown[]): void };
|
|
79
111
|
export function deprecate<T extends Function>(fn: T, message: string): T {
|
|
80
|
-
if (typeof window === 'undefined'
|
|
112
|
+
if (typeof window === 'undefined' && typeof self === 'undefined') {
|
|
81
113
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
82
114
|
return require('util').deprecate(fn, message);
|
|
83
115
|
}
|
|
@@ -87,7 +119,7 @@ export function deprecate<T extends Function>(fn: T, message: string): T {
|
|
|
87
119
|
console.warn(message);
|
|
88
120
|
warned = true;
|
|
89
121
|
}
|
|
90
|
-
return fn.apply(this,
|
|
122
|
+
return fn.apply(this, args);
|
|
91
123
|
}
|
|
92
124
|
return (deprecated as unknown) as T;
|
|
93
125
|
}
|
package/src/regexp.ts
CHANGED
|
@@ -25,17 +25,17 @@ export interface BSONRegExpExtended {
|
|
|
25
25
|
export class BSONRegExp {
|
|
26
26
|
_bsontype!: 'BSONRegExp';
|
|
27
27
|
|
|
28
|
-
pattern
|
|
29
|
-
options
|
|
28
|
+
pattern!: string;
|
|
29
|
+
options!: string;
|
|
30
30
|
/**
|
|
31
31
|
* @param pattern - The regular expression pattern to match
|
|
32
32
|
* @param options - The regular expression options
|
|
33
33
|
*/
|
|
34
34
|
constructor(pattern: string, options?: string) {
|
|
35
|
+
if (!(this instanceof BSONRegExp)) return new BSONRegExp(pattern, options);
|
|
36
|
+
|
|
35
37
|
this.pattern = pattern;
|
|
36
|
-
this.options = options ?? '';
|
|
37
|
-
// Execute
|
|
38
|
-
alphabetize(this.options);
|
|
38
|
+
this.options = alphabetize(options ?? '');
|
|
39
39
|
|
|
40
40
|
// Validate options
|
|
41
41
|
for (let i = 0; i < this.options.length; i++) {
|
package/src/symbol.ts
CHANGED
|
@@ -10,11 +10,13 @@ export interface BSONSymbolExtended {
|
|
|
10
10
|
export class BSONSymbol {
|
|
11
11
|
_bsontype!: 'Symbol';
|
|
12
12
|
|
|
13
|
-
value
|
|
13
|
+
value!: string;
|
|
14
14
|
/**
|
|
15
15
|
* @param value - the string representing the symbol.
|
|
16
16
|
*/
|
|
17
17
|
constructor(value: string) {
|
|
18
|
+
if (!(this instanceof BSONSymbol)) return new BSONSymbol(value);
|
|
19
|
+
|
|
18
20
|
this.value = value;
|
|
19
21
|
}
|
|
20
22
|
|
|
@@ -30,7 +32,7 @@ export class BSONSymbol {
|
|
|
30
32
|
|
|
31
33
|
/** @internal */
|
|
32
34
|
inspect(): string {
|
|
33
|
-
return `BSONSymbol("${this.value}")`;
|
|
35
|
+
return `new BSONSymbol("${this.value}")`;
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
/** @internal */
|
package/src/timestamp.ts
CHANGED
|
@@ -31,8 +31,13 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
31
31
|
* @param low - the low (signed) 32 bits of the Timestamp.
|
|
32
32
|
* @param high - the high (signed) 32 bits of the Timestamp.
|
|
33
33
|
*/
|
|
34
|
+
constructor(low: Long);
|
|
34
35
|
constructor(low: number, high: number);
|
|
35
36
|
constructor(low: number | Long, high?: number) {
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
38
|
+
///@ts-expect-error
|
|
39
|
+
if (!(this instanceof Timestamp)) return new Timestamp(low, high);
|
|
40
|
+
|
|
36
41
|
if (Long.isLong(low)) {
|
|
37
42
|
super(low.low, low.high, true);
|
|
38
43
|
} else {
|
|
@@ -98,6 +103,6 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
98
103
|
}
|
|
99
104
|
|
|
100
105
|
inspect(): string {
|
|
101
|
-
return `Timestamp(${this.getLowBits().toString()}, ${this.getHighBits().toString()})`;
|
|
106
|
+
return `new Timestamp(${this.getLowBits().toString()}, ${this.getHighBits().toString()})`;
|
|
102
107
|
}
|
|
103
108
|
}
|
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,31 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
|
|
3
|
+
// Validation regex for v4 uuid (validates with or without dashes)
|
|
4
|
+
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;
|
|
5
|
+
|
|
6
|
+
export const uuidValidateString = (str: string): boolean =>
|
|
7
|
+
typeof str === 'string' && VALIDATION_REGEX.test(str);
|
|
8
|
+
|
|
9
|
+
export const uuidHexStringToBuffer = (hexString: string): Buffer => {
|
|
10
|
+
if (!uuidValidateString(hexString)) {
|
|
11
|
+
throw new TypeError(
|
|
12
|
+
'UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".'
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const sanitizedHexString = hexString.replace(/-/g, '');
|
|
17
|
+
return Buffer.from(sanitizedHexString, 'hex');
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const bufferToUuidHexString = (buffer: Buffer, includeDashes = true): string =>
|
|
21
|
+
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');
|