bson 4.6.5 → 5.0.0-alpha.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/bson.d.ts +208 -267
- package/lib/bson.bundle.js +4033 -0
- package/lib/bson.bundle.js.map +1 -0
- package/lib/bson.cjs +4028 -0
- package/lib/bson.cjs.map +1 -0
- package/lib/bson.mjs +4002 -0
- package/lib/bson.mjs.map +1 -0
- package/package.json +49 -61
- package/src/binary.ts +235 -37
- package/src/bson.ts +43 -129
- package/src/code.ts +24 -14
- package/src/constants.ts +28 -0
- package/src/db_ref.ts +13 -8
- package/src/decimal128.ts +31 -25
- package/src/double.ts +7 -5
- package/src/error.ts +0 -2
- package/src/extended_json.ts +148 -148
- package/src/index.ts +19 -0
- package/src/int_32.ts +7 -5
- package/src/long.ts +16 -16
- package/src/max_key.ts +6 -6
- package/src/min_key.ts +6 -6
- package/src/objectid.ts +41 -74
- package/src/parser/calculate_size.ts +39 -63
- package/src/parser/deserializer.ts +47 -113
- package/src/parser/serializer.ts +234 -341
- package/src/parser/utils.ts +1 -99
- package/src/regexp.ts +16 -5
- package/src/symbol.ts +7 -5
- package/src/timestamp.ts +62 -27
- package/src/utils/byte_utils.ts +61 -0
- package/src/utils/node_byte_utils.ts +141 -0
- package/src/utils/web_byte_utils.ts +190 -0
- package/src/uuid_utils.ts +15 -15
- package/bower.json +0 -26
- package/dist/bson.browser.esm.js +0 -7471
- package/dist/bson.browser.esm.js.map +0 -1
- package/dist/bson.browser.umd.js +0 -7538
- package/dist/bson.browser.umd.js.map +0 -1
- package/dist/bson.bundle.js +0 -7537
- package/dist/bson.bundle.js.map +0 -1
- package/dist/bson.esm.js +0 -5437
- package/dist/bson.esm.js.map +0 -1
- package/lib/binary.js +0 -247
- package/lib/binary.js.map +0 -1
- package/lib/bson.js +0 -265
- package/lib/bson.js.map +0 -1
- package/lib/code.js +0 -46
- package/lib/code.js.map +0 -1
- package/lib/constants.js +0 -82
- package/lib/constants.js.map +0 -1
- package/lib/db_ref.js +0 -97
- package/lib/db_ref.js.map +0 -1
- package/lib/decimal128.js +0 -669
- package/lib/decimal128.js.map +0 -1
- package/lib/double.js +0 -76
- package/lib/double.js.map +0 -1
- package/lib/ensure_buffer.js +0 -25
- package/lib/ensure_buffer.js.map +0 -1
- package/lib/error.js +0 -55
- package/lib/error.js.map +0 -1
- package/lib/extended_json.js +0 -390
- package/lib/extended_json.js.map +0 -1
- package/lib/int_32.js +0 -58
- package/lib/int_32.js.map +0 -1
- package/lib/long.js +0 -900
- package/lib/long.js.map +0 -1
- package/lib/map.js +0 -123
- package/lib/map.js.map +0 -1
- package/lib/max_key.js +0 -33
- package/lib/max_key.js.map +0 -1
- package/lib/min_key.js +0 -33
- package/lib/min_key.js.map +0 -1
- package/lib/objectid.js +0 -299
- package/lib/objectid.js.map +0 -1
- package/lib/parser/calculate_size.js +0 -194
- package/lib/parser/calculate_size.js.map +0 -1
- package/lib/parser/deserializer.js +0 -659
- package/lib/parser/deserializer.js.map +0 -1
- package/lib/parser/serializer.js +0 -867
- package/lib/parser/serializer.js.map +0 -1
- package/lib/parser/utils.js +0 -115
- package/lib/parser/utils.js.map +0 -1
- package/lib/regexp.js +0 -74
- package/lib/regexp.js.map +0 -1
- package/lib/symbol.js +0 -48
- package/lib/symbol.js.map +0 -1
- package/lib/timestamp.js +0 -102
- package/lib/timestamp.js.map +0 -1
- package/lib/utils/global.js +0 -18
- package/lib/utils/global.js.map +0 -1
- package/lib/uuid.js +0 -179
- package/lib/uuid.js.map +0 -1
- package/lib/uuid_utils.js +0 -35
- package/lib/uuid_utils.js.map +0 -1
- package/lib/validate_utf8.js +0 -47
- package/lib/validate_utf8.js.map +0 -1
- package/src/ensure_buffer.ts +0 -27
- package/src/map.ts +0 -119
- package/src/utils/global.ts +0 -22
- package/src/uuid.ts +0 -209
package/lib/uuid.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"uuid.js","sourceRoot":"","s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MAAM,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC,GAA1C;QACE,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;IACxB,CAAC;IAED,sBAAO,GAAP;QACE,OAAO,qBAAa,IAAI,CAAC,WAAW,EAAE,QAAI,CAAC;IAC7C,CAAC;IACH,WAAC;AAAD,CAAC,AA1LD,IA0LC;AA1LY,oBAAI;AA4LjB,MAAM,CAAC,cAAc,CAAC,IAAI,CAAC,SAAS,EAAE,WAAW,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC"}
|
package/lib/uuid_utils.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
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
|
-
var error_1 = require("./error");
|
|
6
|
-
// Validation regex for v4 uuid (validates with or without dashes)
|
|
7
|
-
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;
|
|
8
|
-
var uuidValidateString = function (str) {
|
|
9
|
-
return typeof str === 'string' && VALIDATION_REGEX.test(str);
|
|
10
|
-
};
|
|
11
|
-
exports.uuidValidateString = uuidValidateString;
|
|
12
|
-
var uuidHexStringToBuffer = function (hexString) {
|
|
13
|
-
if (!(0, exports.uuidValidateString)(hexString)) {
|
|
14
|
-
throw new error_1.BSONTypeError('UUID string representations must be a 32 or 36 character hex string (dashes excluded/included). Format: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" or "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".');
|
|
15
|
-
}
|
|
16
|
-
var sanitizedHexString = hexString.replace(/-/g, '');
|
|
17
|
-
return buffer_1.Buffer.from(sanitizedHexString, 'hex');
|
|
18
|
-
};
|
|
19
|
-
exports.uuidHexStringToBuffer = uuidHexStringToBuffer;
|
|
20
|
-
var bufferToUuidHexString = function (buffer, includeDashes) {
|
|
21
|
-
if (includeDashes === void 0) { includeDashes = true; }
|
|
22
|
-
return 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');
|
|
33
|
-
};
|
|
34
|
-
exports.bufferToUuidHexString = bufferToUuidHexString;
|
|
35
|
-
//# sourceMappingURL=uuid_utils.js.map
|
package/lib/uuid_utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"uuid_utils.js","sourceRoot":"","sources":["../src/uuid_utils.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAChC,iCAAwC;AAExC,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,IAAA,0BAAkB,EAAC,SAAS,CAAC,EAAE;QAClC,MAAM,IAAI,qBAAa,CACrB,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/lib/validate_utf8.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.validateUtf8 = void 0;
|
|
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
|
-
/**
|
|
14
|
-
* Determines if the passed in bytes are valid utf8
|
|
15
|
-
* @param bytes - An array of 8-bit bytes. Must be indexable and have length property
|
|
16
|
-
* @param start - The index to start validating
|
|
17
|
-
* @param end - The index to end validating
|
|
18
|
-
*/
|
|
19
|
-
function validateUtf8(bytes, start, end) {
|
|
20
|
-
var continuation = 0;
|
|
21
|
-
for (var i = start; i < end; i += 1) {
|
|
22
|
-
var byte = bytes[i];
|
|
23
|
-
if (continuation) {
|
|
24
|
-
if ((byte & FIRST_TWO_BITS) !== CONTINUING_CHAR) {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
continuation -= 1;
|
|
28
|
-
}
|
|
29
|
-
else if (byte & FIRST_BIT) {
|
|
30
|
-
if ((byte & FIRST_THREE_BITS) === TWO_BIT_CHAR) {
|
|
31
|
-
continuation = 1;
|
|
32
|
-
}
|
|
33
|
-
else if ((byte & FIRST_FOUR_BITS) === THREE_BIT_CHAR) {
|
|
34
|
-
continuation = 2;
|
|
35
|
-
}
|
|
36
|
-
else if ((byte & FIRST_FIVE_BITS) === FOUR_BIT_CHAR) {
|
|
37
|
-
continuation = 3;
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return !continuation;
|
|
45
|
-
}
|
|
46
|
-
exports.validateUtf8 = validateUtf8;
|
|
47
|
-
//# sourceMappingURL=validate_utf8.js.map
|
package/lib/validate_utf8.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validate_utf8.js","sourceRoot":"","sources":["../src/validate_utf8.ts"],"names":[],"mappings":";;;AAAA,IAAM,SAAS,GAAG,IAAI,CAAC;AACvB,IAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,IAAM,gBAAgB,GAAG,IAAI,CAAC;AAC9B,IAAM,eAAe,GAAG,IAAI,CAAC;AAC7B,IAAM,eAAe,GAAG,IAAI,CAAC;AAE7B,IAAM,YAAY,GAAG,IAAI,CAAC;AAC1B,IAAM,cAAc,GAAG,IAAI,CAAC;AAC5B,IAAM,aAAa,GAAG,IAAI,CAAC;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/src/ensure_buffer.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { Buffer } from 'buffer';
|
|
2
|
-
import { BSONTypeError } from './error';
|
|
3
|
-
import { isAnyArrayBuffer } from './parser/utils';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Makes sure that, if a Uint8Array is passed in, it is wrapped in a Buffer.
|
|
7
|
-
*
|
|
8
|
-
* @param potentialBuffer - The potential buffer
|
|
9
|
-
* @returns Buffer the input if potentialBuffer is a buffer, or a buffer that
|
|
10
|
-
* wraps a passed in Uint8Array
|
|
11
|
-
* @throws BSONTypeError If anything other than a Buffer or Uint8Array is passed in
|
|
12
|
-
*/
|
|
13
|
-
export function ensureBuffer(potentialBuffer: Buffer | ArrayBufferView | ArrayBuffer): Buffer {
|
|
14
|
-
if (ArrayBuffer.isView(potentialBuffer)) {
|
|
15
|
-
return Buffer.from(
|
|
16
|
-
potentialBuffer.buffer,
|
|
17
|
-
potentialBuffer.byteOffset,
|
|
18
|
-
potentialBuffer.byteLength
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (isAnyArrayBuffer(potentialBuffer)) {
|
|
23
|
-
return Buffer.from(potentialBuffer);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
throw new BSONTypeError('Must use either Buffer or TypedArray');
|
|
27
|
-
}
|
package/src/map.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
// We have an ES6 Map available, return the native instance
|
|
3
|
-
|
|
4
|
-
import { getGlobal } from './utils/global';
|
|
5
|
-
|
|
6
|
-
/** @public */
|
|
7
|
-
let bsonMap: MapConstructor;
|
|
8
|
-
|
|
9
|
-
const bsonGlobal = getGlobal<{ Map?: MapConstructor }>();
|
|
10
|
-
if (bsonGlobal.Map) {
|
|
11
|
-
bsonMap = bsonGlobal.Map;
|
|
12
|
-
} else {
|
|
13
|
-
// We will return a polyfill
|
|
14
|
-
bsonMap = class Map {
|
|
15
|
-
private _keys: string[];
|
|
16
|
-
private _values: Record<string, any>;
|
|
17
|
-
constructor(array: [string, any][] = []) {
|
|
18
|
-
this._keys = [];
|
|
19
|
-
this._values = {};
|
|
20
|
-
|
|
21
|
-
for (let i = 0; i < array.length; i++) {
|
|
22
|
-
if (array[i] == null) continue; // skip null and undefined
|
|
23
|
-
const entry = array[i];
|
|
24
|
-
const key = entry[0];
|
|
25
|
-
const value = entry[1];
|
|
26
|
-
// Add the key to the list of keys in order
|
|
27
|
-
this._keys.push(key);
|
|
28
|
-
// Add the key and value to the values dictionary with a point
|
|
29
|
-
// to the location in the ordered keys list
|
|
30
|
-
this._values[key] = { v: value, i: this._keys.length - 1 };
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
clear() {
|
|
34
|
-
this._keys = [];
|
|
35
|
-
this._values = {};
|
|
36
|
-
}
|
|
37
|
-
delete(key: string) {
|
|
38
|
-
const value = this._values[key];
|
|
39
|
-
if (value == null) return false;
|
|
40
|
-
// Delete entry
|
|
41
|
-
delete this._values[key];
|
|
42
|
-
// Remove the key from the ordered keys list
|
|
43
|
-
this._keys.splice(value.i, 1);
|
|
44
|
-
return true;
|
|
45
|
-
}
|
|
46
|
-
entries() {
|
|
47
|
-
let index = 0;
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
next: () => {
|
|
51
|
-
const key = this._keys[index++];
|
|
52
|
-
return {
|
|
53
|
-
value: key !== undefined ? [key, this._values[key].v] : undefined,
|
|
54
|
-
done: key !== undefined ? false : true
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
forEach(callback: (this: this, value: any, key: string, self: this) => void, self?: this) {
|
|
60
|
-
self = self || this;
|
|
61
|
-
|
|
62
|
-
for (let i = 0; i < this._keys.length; i++) {
|
|
63
|
-
const key = this._keys[i];
|
|
64
|
-
// Call the forEach callback
|
|
65
|
-
callback.call(self, this._values[key].v, key, self);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
get(key: string) {
|
|
69
|
-
return this._values[key] ? this._values[key].v : undefined;
|
|
70
|
-
}
|
|
71
|
-
has(key: string) {
|
|
72
|
-
return this._values[key] != null;
|
|
73
|
-
}
|
|
74
|
-
keys() {
|
|
75
|
-
let index = 0;
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
next: () => {
|
|
79
|
-
const key = this._keys[index++];
|
|
80
|
-
return {
|
|
81
|
-
value: key !== undefined ? key : undefined,
|
|
82
|
-
done: key !== undefined ? false : true
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
set(key: string, value: any) {
|
|
88
|
-
if (this._values[key]) {
|
|
89
|
-
this._values[key].v = value;
|
|
90
|
-
return this;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Add the key to the list of keys in order
|
|
94
|
-
this._keys.push(key);
|
|
95
|
-
// Add the key and value to the values dictionary with a point
|
|
96
|
-
// to the location in the ordered keys list
|
|
97
|
-
this._values[key] = { v: value, i: this._keys.length - 1 };
|
|
98
|
-
return this;
|
|
99
|
-
}
|
|
100
|
-
values() {
|
|
101
|
-
let index = 0;
|
|
102
|
-
|
|
103
|
-
return {
|
|
104
|
-
next: () => {
|
|
105
|
-
const key = this._keys[index++];
|
|
106
|
-
return {
|
|
107
|
-
value: key !== undefined ? this._values[key].v : undefined,
|
|
108
|
-
done: key !== undefined ? false : true
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
get size() {
|
|
114
|
-
return this._keys.length;
|
|
115
|
-
}
|
|
116
|
-
} as unknown as MapConstructor;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export { bsonMap as Map };
|
package/src/utils/global.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
/* We do not want to have to include DOM types just for this check */
|
|
3
|
-
declare const window: unknown;
|
|
4
|
-
declare const self: unknown;
|
|
5
|
-
declare const global: unknown;
|
|
6
|
-
|
|
7
|
-
function checkForMath(potentialGlobal: any) {
|
|
8
|
-
// eslint-disable-next-line eqeqeq
|
|
9
|
-
return potentialGlobal && potentialGlobal.Math == Math && potentialGlobal;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
|
|
13
|
-
export function getGlobal<T = Record<string, unknown>>(): T {
|
|
14
|
-
return (
|
|
15
|
-
checkForMath(typeof globalThis === 'object' && globalThis) ||
|
|
16
|
-
checkForMath(typeof window === 'object' && window) ||
|
|
17
|
-
checkForMath(typeof self === 'object' && self) ||
|
|
18
|
-
checkForMath(typeof global === 'object' && global) ||
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
20
|
-
Function('return this')()
|
|
21
|
-
);
|
|
22
|
-
}
|
package/src/uuid.ts
DELETED
|
@@ -1,209 +0,0 @@
|
|
|
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
|
-
import { BSONTypeError } from './error';
|
|
7
|
-
|
|
8
|
-
/** @public */
|
|
9
|
-
export type UUIDExtended = {
|
|
10
|
-
$uuid: string;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
const BYTE_LENGTH = 16;
|
|
14
|
-
|
|
15
|
-
const kId = Symbol('id');
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* A class representation of the BSON UUID type.
|
|
19
|
-
* @public
|
|
20
|
-
*/
|
|
21
|
-
export class UUID {
|
|
22
|
-
// This property is not meant for direct serialization, but simply an indication that this type originates from this package.
|
|
23
|
-
_bsontype!: 'UUID';
|
|
24
|
-
|
|
25
|
-
static cacheHexString: boolean;
|
|
26
|
-
|
|
27
|
-
/** UUID Bytes @internal */
|
|
28
|
-
private [kId]!: Buffer;
|
|
29
|
-
/** UUID hexString cache @internal */
|
|
30
|
-
private __id?: string;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Create an UUID type
|
|
34
|
-
*
|
|
35
|
-
* @param input - Can be a 32 or 36 character hex string (dashes excluded/included) or a 16 byte binary Buffer.
|
|
36
|
-
*/
|
|
37
|
-
constructor(input?: string | Buffer | UUID) {
|
|
38
|
-
if (typeof input === 'undefined') {
|
|
39
|
-
// The most common use case (blank id, new UUID() instance)
|
|
40
|
-
this.id = UUID.generate();
|
|
41
|
-
} else if (input instanceof UUID) {
|
|
42
|
-
this[kId] = Buffer.from(input.id);
|
|
43
|
-
this.__id = input.__id;
|
|
44
|
-
} else if (ArrayBuffer.isView(input) && input.byteLength === BYTE_LENGTH) {
|
|
45
|
-
this.id = ensureBuffer(input);
|
|
46
|
-
} else if (typeof input === 'string') {
|
|
47
|
-
this.id = uuidHexStringToBuffer(input);
|
|
48
|
-
} else {
|
|
49
|
-
throw new BSONTypeError(
|
|
50
|
-
'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).'
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* The UUID bytes
|
|
57
|
-
* @readonly
|
|
58
|
-
*/
|
|
59
|
-
get id(): Buffer {
|
|
60
|
-
return this[kId];
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
set id(value: Buffer) {
|
|
64
|
-
this[kId] = value;
|
|
65
|
-
|
|
66
|
-
if (UUID.cacheHexString) {
|
|
67
|
-
this.__id = bufferToUuidHexString(value);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Generate a 16 byte uuid v4 buffer used in UUIDs
|
|
73
|
-
*/
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Returns the UUID id as a 32 or 36 character hex string representation, excluding/including dashes (defaults to 36 character dash separated)
|
|
77
|
-
* @param includeDashes - should the string exclude dash-separators.
|
|
78
|
-
* */
|
|
79
|
-
toHexString(includeDashes = true): string {
|
|
80
|
-
if (UUID.cacheHexString && this.__id) {
|
|
81
|
-
return this.__id;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const uuidHexString = bufferToUuidHexString(this.id, includeDashes);
|
|
85
|
-
|
|
86
|
-
if (UUID.cacheHexString) {
|
|
87
|
-
this.__id = uuidHexString;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return uuidHexString;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
|
|
95
|
-
*/
|
|
96
|
-
toString(encoding?: string): string {
|
|
97
|
-
return encoding ? this.id.toString(encoding) : this.toHexString();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Converts the id into its JSON string representation.
|
|
102
|
-
* A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
|
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
|
-
}
|
|
128
|
-
|
|
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
|
-
}
|
|
149
|
-
|
|
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
|
-
}
|
|
158
|
-
|
|
159
|
-
if (input instanceof UUID) {
|
|
160
|
-
return true;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
if (typeof input === 'string') {
|
|
164
|
-
return uuidValidateString(input);
|
|
165
|
-
}
|
|
166
|
-
|
|
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
|
-
}
|
|
172
|
-
|
|
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
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
Object.defineProperty(UUID.prototype, '_bsontype', { value: 'UUID' });
|