bson 4.4.0 → 4.5.2
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 +42 -14
- package/dist/bson.browser.esm.js +628 -2199
- package/dist/bson.browser.esm.js.map +1 -1
- package/dist/bson.browser.umd.js +681 -2254
- package/dist/bson.browser.umd.js.map +1 -1
- package/dist/bson.bundle.js +681 -2254
- package/dist/bson.bundle.js.map +1 -1
- package/dist/bson.esm.js +622 -2197
- package/dist/bson.esm.js.map +1 -1
- package/lib/binary.js +0 -3
- package/lib/binary.js.map +1 -1
- package/lib/bson.js +1 -1
- package/lib/bson.js.map +1 -1
- package/lib/code.js +0 -1
- package/lib/code.js.map +1 -1
- package/lib/db_ref.js +4 -2
- package/lib/db_ref.js.map +1 -1
- package/lib/decimal128.js +4 -49
- package/lib/decimal128.js.map +1 -1
- package/lib/double.js +3 -1
- package/lib/double.js.map +1 -1
- package/lib/extended_json.js +3 -3
- package/lib/extended_json.js.map +1 -1
- package/lib/int_32.js +3 -1
- package/lib/int_32.js.map +1 -1
- package/lib/long.js.map +1 -1
- package/lib/map.js +3 -15
- package/lib/map.js.map +1 -1
- package/lib/objectid.js +1 -5
- package/lib/objectid.js.map +1 -1
- package/lib/parser/deserializer.js +15 -12
- package/lib/parser/deserializer.js.map +1 -1
- package/lib/parser/serializer.js +7 -3
- package/lib/parser/serializer.js.map +1 -1
- package/lib/parser/utils.js +8 -8
- package/lib/parser/utils.js.map +1 -1
- package/lib/regexp.js +1 -3
- package/lib/regexp.js.map +1 -1
- package/lib/symbol.js +0 -2
- package/lib/symbol.js.map +1 -1
- package/lib/timestamp.js +6 -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 +2 -3
- package/lib/uuid.js.map +1 -1
- package/lib/uuid_utils.js.map +1 -1
- package/package.json +22 -9
- package/src/binary.ts +1 -4
- package/src/bson.ts +1 -1
- package/src/code.ts +0 -1
- package/src/db_ref.ts +6 -2
- package/src/decimal128.ts +4 -49
- package/src/double.ts +4 -1
- package/src/extended_json.ts +6 -6
- package/src/int_32.ts +4 -1
- package/src/long.ts +2 -2
- package/src/map.ts +5 -25
- package/src/objectid.ts +1 -5
- package/src/parser/deserializer.ts +23 -16
- package/src/parser/serializer.ts +7 -3
- package/src/parser/utils.ts +9 -11
- package/src/regexp.ts +2 -4
- package/src/symbol.ts +0 -2
- package/src/timestamp.ts +15 -7
- package/src/utils/global.ts +22 -0
- package/src/uuid.ts +2 -3
- package/src/uuid_utils.ts +2 -1
- package/HISTORY.md +0 -505
package/src/extended_json.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Binary } from './binary';
|
|
2
2
|
import type { Document } from './bson';
|
|
3
3
|
import { Code } from './code';
|
|
4
|
-
import { DBRef } from './db_ref';
|
|
4
|
+
import { DBRef, isDBRefLike } from './db_ref';
|
|
5
5
|
import { Decimal128 } from './decimal128';
|
|
6
6
|
import { Double } from './double';
|
|
7
7
|
import { Int32 } from './int_32';
|
|
@@ -120,7 +120,7 @@ function deserializeValue(value: any, options: EJSON.Options = {}) {
|
|
|
120
120
|
return Code.fromExtendedJSON(value);
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
if (value
|
|
123
|
+
if (isDBRefLike(value) || value.$dbPointer) {
|
|
124
124
|
const v = value.$ref ? value : value.$dbPointer;
|
|
125
125
|
|
|
126
126
|
// we run into this in a "degenerate EJSON" case (with $id and $ref order flipped)
|
|
@@ -213,7 +213,7 @@ function serializeValue(value: any, options: EJSONSerializeOptions): any {
|
|
|
213
213
|
: { $date: { $numberLong: value.getTime().toString() } };
|
|
214
214
|
}
|
|
215
215
|
|
|
216
|
-
if (typeof value === 'number' && !options.relaxed) {
|
|
216
|
+
if (typeof value === 'number' && (!options.relaxed || !isFinite(value))) {
|
|
217
217
|
// it's an integer
|
|
218
218
|
if (Math.floor(value) === value) {
|
|
219
219
|
const int32Range = value >= BSON_INT32_MIN && value <= BSON_INT32_MAX,
|
|
@@ -310,10 +310,10 @@ function serializeDocument(doc: any, options: EJSONSerializeOptions) {
|
|
|
310
310
|
outDoc = new Code(outDoc.code, serializeValue(outDoc.scope, options));
|
|
311
311
|
} else if (bsontype === 'DBRef' && outDoc.oid) {
|
|
312
312
|
outDoc = new DBRef(
|
|
313
|
-
outDoc.collection,
|
|
313
|
+
serializeValue(outDoc.collection, options),
|
|
314
314
|
serializeValue(outDoc.oid, options),
|
|
315
|
-
outDoc.db,
|
|
316
|
-
outDoc.fields
|
|
315
|
+
serializeValue(outDoc.db, options),
|
|
316
|
+
serializeValue(outDoc.fields, options)
|
|
317
317
|
);
|
|
318
318
|
}
|
|
319
319
|
|
package/src/int_32.ts
CHANGED
package/src/long.ts
CHANGED
|
@@ -27,13 +27,13 @@ let wasm: LongWASMHelpers | undefined = undefined;
|
|
|
27
27
|
declare const WebAssembly: any;
|
|
28
28
|
|
|
29
29
|
try {
|
|
30
|
-
wasm =
|
|
30
|
+
wasm = new WebAssembly.Instance(
|
|
31
31
|
new WebAssembly.Module(
|
|
32
32
|
// prettier-ignore
|
|
33
33
|
new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11])
|
|
34
34
|
),
|
|
35
35
|
{}
|
|
36
|
-
).exports as unknown
|
|
36
|
+
).exports as unknown as LongWASMHelpers;
|
|
37
37
|
} catch {
|
|
38
38
|
// no wasm support
|
|
39
39
|
}
|
package/src/map.ts
CHANGED
|
@@ -1,37 +1,17 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
// We have an ES6 Map available, return the native instance
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
declare const window: unknown;
|
|
6
|
-
declare const self: unknown;
|
|
7
|
-
declare const global: unknown;
|
|
4
|
+
import { getGlobal } from './utils/global';
|
|
8
5
|
|
|
9
6
|
/** @public */
|
|
10
7
|
let bsonMap: MapConstructor;
|
|
11
8
|
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
return potentialGlobal && potentialGlobal.Math == Math && potentialGlobal;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
// https://github.com/zloirock/core-js/issues/86#issuecomment-115759028
|
|
18
|
-
function getGlobal() {
|
|
19
|
-
// eslint-disable-next-line no-undef
|
|
20
|
-
return (
|
|
21
|
-
check(typeof globalThis === 'object' && globalThis) ||
|
|
22
|
-
check(typeof window === 'object' && window) ||
|
|
23
|
-
check(typeof self === 'object' && self) ||
|
|
24
|
-
check(typeof global === 'object' && global) ||
|
|
25
|
-
Function('return this')()
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const bsonGlobal = getGlobal();
|
|
30
|
-
if (Object.prototype.hasOwnProperty.call(bsonGlobal, 'Map')) {
|
|
9
|
+
const bsonGlobal = getGlobal<{ Map?: MapConstructor }>();
|
|
10
|
+
if (bsonGlobal.Map) {
|
|
31
11
|
bsonMap = bsonGlobal.Map;
|
|
32
12
|
} else {
|
|
33
13
|
// We will return a polyfill
|
|
34
|
-
bsonMap =
|
|
14
|
+
bsonMap = class Map {
|
|
35
15
|
private _keys: string[];
|
|
36
16
|
private _values: Record<string, any>;
|
|
37
17
|
constructor(array: [string, any][] = []) {
|
|
@@ -133,7 +113,7 @@ if (Object.prototype.hasOwnProperty.call(bsonGlobal, 'Map')) {
|
|
|
133
113
|
get size() {
|
|
134
114
|
return this._keys.length;
|
|
135
115
|
}
|
|
136
|
-
} as unknown
|
|
116
|
+
} as unknown as MapConstructor;
|
|
137
117
|
}
|
|
138
118
|
|
|
139
119
|
export { bsonMap as Map };
|
package/src/objectid.ts
CHANGED
|
@@ -188,7 +188,6 @@ export class ObjectId {
|
|
|
188
188
|
* Converts the id into a 24 character hex string for printing
|
|
189
189
|
*
|
|
190
190
|
* @param format - The Buffer toString format parameter.
|
|
191
|
-
* @internal
|
|
192
191
|
*/
|
|
193
192
|
toString(format?: string): string {
|
|
194
193
|
// Is the id a buffer then use the buffer toString method to return the format
|
|
@@ -196,10 +195,7 @@ export class ObjectId {
|
|
|
196
195
|
return this.toHexString();
|
|
197
196
|
}
|
|
198
197
|
|
|
199
|
-
/**
|
|
200
|
-
* Converts to its JSON the 24 character hex string representation.
|
|
201
|
-
* @internal
|
|
202
|
-
*/
|
|
198
|
+
/** Converts to its JSON the 24 character hex string representation. */
|
|
203
199
|
toJSON(): string {
|
|
204
200
|
return this.toHexString();
|
|
205
201
|
}
|
|
@@ -93,6 +93,8 @@ export function deserialize(
|
|
|
93
93
|
return deserializeObject(buffer, index, options, isArray);
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
const allowedDBRefKeys = /^\$ref$|^\$id$|^\$db$/;
|
|
97
|
+
|
|
96
98
|
function deserializeObject(
|
|
97
99
|
buffer: Buffer,
|
|
98
100
|
index: number,
|
|
@@ -134,6 +136,8 @@ function deserializeObject(
|
|
|
134
136
|
let arrayIndex = 0;
|
|
135
137
|
const done = false;
|
|
136
138
|
|
|
139
|
+
let isPossibleDBRef = isArray ? false : null;
|
|
140
|
+
|
|
137
141
|
// While we have more left data left keep parsing
|
|
138
142
|
while (!done) {
|
|
139
143
|
// Read the type
|
|
@@ -152,6 +156,9 @@ function deserializeObject(
|
|
|
152
156
|
// If are at the end of the buffer there is a problem with the document
|
|
153
157
|
if (i >= buffer.byteLength) throw new Error('Bad BSON Document: illegal CString');
|
|
154
158
|
const name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
|
|
159
|
+
if (isPossibleDBRef !== false && (name as string)[0] === '$') {
|
|
160
|
+
isPossibleDBRef = allowedDBRefKeys.test(name as string);
|
|
161
|
+
}
|
|
155
162
|
let value;
|
|
156
163
|
|
|
157
164
|
index = i + 1;
|
|
@@ -169,12 +176,17 @@ function deserializeObject(
|
|
|
169
176
|
)
|
|
170
177
|
throw new Error('bad string length in bson');
|
|
171
178
|
|
|
172
|
-
if (!validateUtf8(buffer, index, index + stringSize - 1)) {
|
|
173
|
-
throw new Error('Invalid UTF-8 string in BSON document');
|
|
174
|
-
}
|
|
175
|
-
|
|
176
179
|
value = buffer.toString('utf8', index, index + stringSize - 1);
|
|
177
180
|
|
|
181
|
+
for (let i = 0; i < value.length; i++) {
|
|
182
|
+
if (value.charCodeAt(i) === 0xfffd) {
|
|
183
|
+
if (!validateUtf8(buffer, index, index + stringSize - 1)) {
|
|
184
|
+
throw new Error('Invalid UTF-8 string in BSON document');
|
|
185
|
+
}
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
178
190
|
index = index + stringSize;
|
|
179
191
|
} else if (elementType === constants.BSON_DATA_OID) {
|
|
180
192
|
const oid = Buffer.alloc(12);
|
|
@@ -246,9 +258,11 @@ function deserializeObject(
|
|
|
246
258
|
if (fieldsAsRaw && fieldsAsRaw[name]) {
|
|
247
259
|
arrayOptions = {};
|
|
248
260
|
for (const n in options) {
|
|
249
|
-
(
|
|
250
|
-
|
|
251
|
-
|
|
261
|
+
(
|
|
262
|
+
arrayOptions as {
|
|
263
|
+
[key: string]: DeserializeOptions[keyof DeserializeOptions];
|
|
264
|
+
}
|
|
265
|
+
)[n] = options[n as keyof DeserializeOptions];
|
|
252
266
|
}
|
|
253
267
|
arrayOptions['raw'] = true;
|
|
254
268
|
}
|
|
@@ -623,15 +637,8 @@ function deserializeObject(
|
|
|
623
637
|
throw new Error('corrupt object bson');
|
|
624
638
|
}
|
|
625
639
|
|
|
626
|
-
//
|
|
627
|
-
|
|
628
|
-
let valid = true;
|
|
629
|
-
dollarKeys.forEach(k => {
|
|
630
|
-
if (['$ref', '$id', '$db'].indexOf(k) === -1) valid = false;
|
|
631
|
-
});
|
|
632
|
-
|
|
633
|
-
// if a $key not in "$ref", "$id", "$db", don't make a DBRef
|
|
634
|
-
if (!valid) return object;
|
|
640
|
+
// if we did not find "$ref", "$id", "$db", or found an extraneous $key, don't make a DBRef
|
|
641
|
+
if (!isPossibleDBRef) return object;
|
|
635
642
|
|
|
636
643
|
if (isDBRefLike(object)) {
|
|
637
644
|
const copy = Object.assign({}, object) as Partial<DBRefLike>;
|
package/src/parser/serializer.ts
CHANGED
|
@@ -306,8 +306,10 @@ function serializeObjectId(
|
|
|
306
306
|
// Write the objectId into the shared buffer
|
|
307
307
|
if (typeof value.id === 'string') {
|
|
308
308
|
buffer.write(value.id, index, undefined, 'binary');
|
|
309
|
-
} else if (value.id
|
|
310
|
-
|
|
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);
|
|
311
313
|
} else {
|
|
312
314
|
throw new TypeError('object [' + JSON.stringify(value) + '] is not a valid ObjectId');
|
|
313
315
|
}
|
|
@@ -406,7 +408,9 @@ function serializeDecimal128(
|
|
|
406
408
|
index = index + numberOfWrittenBytes;
|
|
407
409
|
buffer[index++] = 0;
|
|
408
410
|
// Write the data from the value
|
|
409
|
-
|
|
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);
|
|
410
414
|
return index + 16;
|
|
411
415
|
}
|
|
412
416
|
|
package/src/parser/utils.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Buffer } from 'buffer';
|
|
2
|
+
import { getGlobal } from '../utils/global';
|
|
2
3
|
|
|
3
4
|
type RandomBytesFunction = (size: number) => Uint8Array;
|
|
4
5
|
|
|
@@ -10,14 +11,15 @@ export function normalizedFunctionString(fn: Function): string {
|
|
|
10
11
|
return fn.toString().replace('function(', 'function (');
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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.';
|
|
14
|
+
function isReactNative() {
|
|
15
|
+
const g = getGlobal<{ navigator?: { product?: string } }>();
|
|
16
|
+
return typeof g.navigator === 'object' && g.navigator.product === 'ReactNative';
|
|
17
|
+
}
|
|
19
18
|
|
|
20
19
|
const insecureRandomBytes: RandomBytesFunction = function insecureRandomBytes(size: number) {
|
|
20
|
+
const insecureWarning = isReactNative()
|
|
21
|
+
? 'BSON: For React Native please polyfill crypto.getRandomValues, e.g. using: https://www.npmjs.com/package/react-native-get-random-values.'
|
|
22
|
+
: 'BSON: No cryptographic implementation for random bytes present, falling back to a less secure implementation.';
|
|
21
23
|
console.warn(insecureWarning);
|
|
22
24
|
|
|
23
25
|
const result = Buffer.alloc(size);
|
|
@@ -109,10 +111,6 @@ export function isObjectLike(candidate: unknown): candidate is Record<string, un
|
|
|
109
111
|
|
|
110
112
|
declare let console: { warn(...message: unknown[]): void };
|
|
111
113
|
export function deprecate<T extends Function>(fn: T, message: string): T {
|
|
112
|
-
if (typeof window === 'undefined' && typeof self === 'undefined') {
|
|
113
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
114
|
-
return require('util').deprecate(fn, message);
|
|
115
|
-
}
|
|
116
114
|
let warned = false;
|
|
117
115
|
function deprecated(this: unknown, ...args: unknown[]) {
|
|
118
116
|
if (!warned) {
|
|
@@ -121,5 +119,5 @@ export function deprecate<T extends Function>(fn: T, message: string): T {
|
|
|
121
119
|
}
|
|
122
120
|
return fn.apply(this, args);
|
|
123
121
|
}
|
|
124
|
-
return
|
|
122
|
+
return deprecated as unknown as T;
|
|
125
123
|
}
|
package/src/regexp.ts
CHANGED
|
@@ -35,9 +35,7 @@ export class BSONRegExp {
|
|
|
35
35
|
if (!(this instanceof BSONRegExp)) return new BSONRegExp(pattern, options);
|
|
36
36
|
|
|
37
37
|
this.pattern = pattern;
|
|
38
|
-
this.options = options ?? '';
|
|
39
|
-
// Execute
|
|
40
|
-
alphabetize(this.options);
|
|
38
|
+
this.options = alphabetize(options ?? '');
|
|
41
39
|
|
|
42
40
|
// Validate options
|
|
43
41
|
for (let i = 0; i < this.options.length; i++) {
|
|
@@ -75,7 +73,7 @@ export class BSONRegExp {
|
|
|
75
73
|
if (typeof doc.$regex !== 'string') {
|
|
76
74
|
// This is for $regex query operators that have extended json values.
|
|
77
75
|
if (doc.$regex._bsontype === 'BSONRegExp') {
|
|
78
|
-
return
|
|
76
|
+
return doc as unknown as BSONRegExp;
|
|
79
77
|
}
|
|
80
78
|
} else {
|
|
81
79
|
return new BSONRegExp(doc.$regex, BSONRegExp.parseOptions(doc.$options));
|
package/src/symbol.ts
CHANGED
|
@@ -25,7 +25,6 @@ export class BSONSymbol {
|
|
|
25
25
|
return this.value;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
/** @internal */
|
|
29
28
|
toString(): string {
|
|
30
29
|
return this.value;
|
|
31
30
|
}
|
|
@@ -35,7 +34,6 @@ export class BSONSymbol {
|
|
|
35
34
|
return `new BSONSymbol("${this.value}")`;
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
/** @internal */
|
|
39
37
|
toJSON(): string {
|
|
40
38
|
return this.value;
|
|
41
39
|
}
|
package/src/timestamp.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { Long } from './long';
|
|
2
|
+
import { isObjectLike } from './parser/utils';
|
|
2
3
|
|
|
3
4
|
/** @public */
|
|
4
5
|
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
|
|
5
6
|
/** @public */
|
|
6
|
-
export type LongWithoutOverrides = new (low:
|
|
7
|
+
export type LongWithoutOverrides = new (low: unknown, high?: number, unsigned?: boolean) => {
|
|
7
8
|
[P in Exclude<keyof Long, TimestampOverrides>]: Long[P];
|
|
8
9
|
};
|
|
9
10
|
/** @public */
|
|
10
|
-
export const LongWithoutOverridesClass: LongWithoutOverrides =
|
|
11
|
+
export const LongWithoutOverridesClass: LongWithoutOverrides =
|
|
12
|
+
Long as unknown as LongWithoutOverrides;
|
|
11
13
|
|
|
12
14
|
/** @public */
|
|
13
15
|
export interface TimestampExtended {
|
|
@@ -26,20 +28,26 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
26
28
|
/**
|
|
27
29
|
* @param low - A 64-bit Long representing the Timestamp.
|
|
28
30
|
*/
|
|
29
|
-
constructor(
|
|
31
|
+
constructor(long: Long);
|
|
32
|
+
/**
|
|
33
|
+
* @param value - A pair of two values indicating timestamp and increment.
|
|
34
|
+
*/
|
|
35
|
+
constructor(value: { t: number; i: number });
|
|
30
36
|
/**
|
|
31
37
|
* @param low - the low (signed) 32 bits of the Timestamp.
|
|
32
38
|
* @param high - the high (signed) 32 bits of the Timestamp.
|
|
39
|
+
* @deprecated Please use `Timestamp({ t: high, i: low })` or `Timestamp(Long(low, high))` instead.
|
|
33
40
|
*/
|
|
34
|
-
constructor(low: Long);
|
|
35
41
|
constructor(low: number, high: number);
|
|
36
|
-
constructor(low: number | Long, high?: number) {
|
|
42
|
+
constructor(low: number | Long | { t: number; i: number }, high?: number) {
|
|
37
43
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
38
44
|
///@ts-expect-error
|
|
39
45
|
if (!(this instanceof Timestamp)) return new Timestamp(low, high);
|
|
40
46
|
|
|
41
47
|
if (Long.isLong(low)) {
|
|
42
48
|
super(low.low, low.high, true);
|
|
49
|
+
} else if (isObjectLike(low) && typeof low.t !== 'undefined' && typeof low.i !== 'undefined') {
|
|
50
|
+
super(low.i, low.t, true);
|
|
43
51
|
} else {
|
|
44
52
|
super(low, high, true);
|
|
45
53
|
}
|
|
@@ -94,7 +102,7 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
94
102
|
|
|
95
103
|
/** @internal */
|
|
96
104
|
static fromExtendedJSON(doc: TimestampExtended): Timestamp {
|
|
97
|
-
return new Timestamp(doc.$timestamp
|
|
105
|
+
return new Timestamp(doc.$timestamp);
|
|
98
106
|
}
|
|
99
107
|
|
|
100
108
|
/** @internal */
|
|
@@ -103,6 +111,6 @@ export class Timestamp extends LongWithoutOverridesClass {
|
|
|
103
111
|
}
|
|
104
112
|
|
|
105
113
|
inspect(): string {
|
|
106
|
-
return `new Timestamp(${this.
|
|
114
|
+
return `new Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`;
|
|
107
115
|
}
|
|
108
116
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
// eslint-disable-next-line no-undef
|
|
15
|
+
return (
|
|
16
|
+
checkForMath(typeof globalThis === 'object' && globalThis) ||
|
|
17
|
+
checkForMath(typeof window === 'object' && window) ||
|
|
18
|
+
checkForMath(typeof self === 'object' && self) ||
|
|
19
|
+
checkForMath(typeof global === 'object' && global) ||
|
|
20
|
+
Function('return this')()
|
|
21
|
+
);
|
|
22
|
+
}
|
package/src/uuid.ts
CHANGED
|
@@ -91,15 +91,14 @@ export class UUID {
|
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
93
|
* Converts the id into a 36 character (dashes included) hex string, unless a encoding is specified.
|
|
94
|
-
* @internal
|
|
95
94
|
*/
|
|
96
95
|
toString(encoding?: string): string {
|
|
97
96
|
return encoding ? this.id.toString(encoding) : this.toHexString();
|
|
98
97
|
}
|
|
99
98
|
|
|
100
99
|
/**
|
|
101
|
-
* Converts the id into its JSON string representation.
|
|
102
|
-
*
|
|
100
|
+
* Converts the id into its JSON string representation.
|
|
101
|
+
* A 36 character (dashes included) hex string in the format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
|
103
102
|
*/
|
|
104
103
|
toJSON(): string {
|
|
105
104
|
return this.toHexString();
|
package/src/uuid_utils.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Buffer } from 'buffer';
|
|
2
2
|
|
|
3
3
|
// Validation regex for v4 uuid (validates with or without dashes)
|
|
4
|
-
const VALIDATION_REGEX =
|
|
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;
|
|
5
6
|
|
|
6
7
|
export const uuidValidateString = (str: string): boolean =>
|
|
7
8
|
typeof str === 'string' && VALIDATION_REGEX.test(str);
|