bson 4.0.4 → 4.2.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/HISTORY.md +56 -1
- package/README.md +7 -9
- package/bower.json +1 -1
- package/bson.d.ts +983 -0
- package/dist/bson.browser.esm.js +7261 -5004
- package/dist/bson.browser.esm.js.map +1 -0
- package/dist/bson.browser.umd.js +7319 -5099
- package/dist/bson.browser.umd.js.map +1 -0
- package/dist/bson.bundle.js +8168 -9216
- package/dist/bson.bundle.js.map +1 -0
- package/dist/bson.esm.js +5643 -5409
- package/dist/bson.esm.js.map +1 -0
- package/etc/prepare.js +19 -0
- package/lib/binary.js +194 -377
- package/lib/binary.js.map +1 -0
- package/lib/bson.js +200 -243
- package/lib/bson.js.map +1 -0
- package/lib/code.js +36 -39
- package/lib/code.js.map +1 -0
- package/lib/constants.js +78 -203
- package/lib/constants.js.map +1 -0
- package/lib/db_ref.js +79 -79
- package/lib/db_ref.js.map +1 -0
- package/lib/decimal128.js +647 -760
- package/lib/decimal128.js.map +1 -0
- package/lib/double.js +61 -58
- package/lib/double.js.map +1 -0
- package/lib/ensure_buffer.js +22 -18
- package/lib/ensure_buffer.js.map +1 -0
- package/lib/extended_json.js +305 -322
- package/lib/extended_json.js.map +1 -0
- package/lib/float_parser.js +98 -104
- package/lib/float_parser.js.map +1 -0
- package/lib/int_32.js +45 -47
- package/lib/int_32.js.map +1 -0
- package/lib/long.js +876 -16
- package/lib/long.js.map +1 -0
- package/lib/map.js +123 -124
- package/lib/map.js.map +1 -0
- package/lib/max_key.js +21 -23
- package/lib/max_key.js.map +1 -0
- package/lib/min_key.js +21 -23
- package/lib/min_key.js.map +1 -0
- package/lib/objectid.js +264 -382
- package/lib/objectid.js.map +1 -0
- package/lib/parser/calculate_size.js +185 -224
- package/lib/parser/calculate_size.js.map +1 -0
- package/lib/parser/deserializer.js +543 -620
- package/lib/parser/deserializer.js.map +1 -0
- package/lib/parser/serializer.js +774 -918
- package/lib/parser/serializer.js.map +1 -0
- package/lib/parser/utils.js +81 -30
- package/lib/parser/utils.js.map +1 -0
- package/lib/regexp.js +54 -70
- package/lib/regexp.js.map +1 -0
- package/lib/symbol.js +40 -56
- package/lib/symbol.js.map +1 -0
- package/lib/timestamp.js +70 -95
- package/lib/timestamp.js.map +1 -0
- package/lib/uuid.js +48 -0
- package/lib/uuid.js.map +1 -0
- package/lib/validate_utf8.js +32 -33
- package/lib/validate_utf8.js.map +1 -0
- package/package.json +53 -31
- package/src/binary.ts +270 -0
- package/src/bson.ts +326 -0
- package/src/code.ts +57 -0
- package/src/constants.ts +104 -0
- package/src/db_ref.ts +115 -0
- package/src/decimal128.ts +801 -0
- package/src/double.ts +85 -0
- package/src/ensure_buffer.ts +26 -0
- package/src/extended_json.ts +395 -0
- package/src/float_parser.ts +152 -0
- package/src/int_32.ts +64 -0
- package/src/long.ts +1000 -0
- package/src/map.ts +139 -0
- package/src/max_key.ts +33 -0
- package/src/min_key.ts +33 -0
- package/src/objectid.ts +377 -0
- package/src/parser/calculate_size.ts +230 -0
- package/src/parser/deserializer.ts +655 -0
- package/src/parser/serializer.ts +1069 -0
- package/src/parser/utils.ts +93 -0
- package/src/regexp.ts +92 -0
- package/src/symbol.ts +57 -0
- package/src/timestamp.ts +103 -0
- package/src/uuid.ts +57 -0
- package/src/validate_utf8.ts +47 -0
- package/lib/fnv1a.js +0 -48
package/src/map.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
// We have an ES6 Map available, return the native instance
|
|
3
|
+
|
|
4
|
+
/* We do not want to have to include DOM types just for this check */
|
|
5
|
+
declare const window: unknown;
|
|
6
|
+
declare const self: unknown;
|
|
7
|
+
declare const global: unknown;
|
|
8
|
+
|
|
9
|
+
/** @public */
|
|
10
|
+
let bsonMap: MapConstructor;
|
|
11
|
+
|
|
12
|
+
const check = function (potentialGlobal: any) {
|
|
13
|
+
// eslint-disable-next-line eqeqeq
|
|
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')) {
|
|
31
|
+
bsonMap = bsonGlobal.Map;
|
|
32
|
+
} else {
|
|
33
|
+
// We will return a polyfill
|
|
34
|
+
bsonMap = (class Map {
|
|
35
|
+
private _keys: string[];
|
|
36
|
+
private _values: Record<string, any>;
|
|
37
|
+
constructor(array: [string, any][] = []) {
|
|
38
|
+
this._keys = [];
|
|
39
|
+
this._values = {};
|
|
40
|
+
|
|
41
|
+
for (let i = 0; i < array.length; i++) {
|
|
42
|
+
if (array[i] == null) continue; // skip null and undefined
|
|
43
|
+
const entry = array[i];
|
|
44
|
+
const key = entry[0];
|
|
45
|
+
const value = entry[1];
|
|
46
|
+
// Add the key to the list of keys in order
|
|
47
|
+
this._keys.push(key);
|
|
48
|
+
// Add the key and value to the values dictionary with a point
|
|
49
|
+
// to the location in the ordered keys list
|
|
50
|
+
this._values[key] = { v: value, i: this._keys.length - 1 };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
clear() {
|
|
54
|
+
this._keys = [];
|
|
55
|
+
this._values = {};
|
|
56
|
+
}
|
|
57
|
+
delete(key: string) {
|
|
58
|
+
const value = this._values[key];
|
|
59
|
+
if (value == null) return false;
|
|
60
|
+
// Delete entry
|
|
61
|
+
delete this._values[key];
|
|
62
|
+
// Remove the key from the ordered keys list
|
|
63
|
+
this._keys.splice(value.i, 1);
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
entries() {
|
|
67
|
+
let index = 0;
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
next: () => {
|
|
71
|
+
const key = this._keys[index++];
|
|
72
|
+
return {
|
|
73
|
+
value: key !== undefined ? [key, this._values[key].v] : undefined,
|
|
74
|
+
done: key !== undefined ? false : true
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
forEach(callback: (this: this, value: any, key: string, self: this) => void, self?: this) {
|
|
80
|
+
self = self || this;
|
|
81
|
+
|
|
82
|
+
for (let i = 0; i < this._keys.length; i++) {
|
|
83
|
+
const key = this._keys[i];
|
|
84
|
+
// Call the forEach callback
|
|
85
|
+
callback.call(self, this._values[key].v, key, self);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
get(key: string) {
|
|
89
|
+
return this._values[key] ? this._values[key].v : undefined;
|
|
90
|
+
}
|
|
91
|
+
has(key: string) {
|
|
92
|
+
return this._values[key] != null;
|
|
93
|
+
}
|
|
94
|
+
keys() {
|
|
95
|
+
let index = 0;
|
|
96
|
+
|
|
97
|
+
return {
|
|
98
|
+
next: () => {
|
|
99
|
+
const key = this._keys[index++];
|
|
100
|
+
return {
|
|
101
|
+
value: key !== undefined ? key : undefined,
|
|
102
|
+
done: key !== undefined ? false : true
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
set(key: string, value: any) {
|
|
108
|
+
if (this._values[key]) {
|
|
109
|
+
this._values[key].v = value;
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Add the key to the list of keys in order
|
|
114
|
+
this._keys.push(key);
|
|
115
|
+
// Add the key and value to the values dictionary with a point
|
|
116
|
+
// to the location in the ordered keys list
|
|
117
|
+
this._values[key] = { v: value, i: this._keys.length - 1 };
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
values() {
|
|
121
|
+
let index = 0;
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
next: () => {
|
|
125
|
+
const key = this._keys[index++];
|
|
126
|
+
return {
|
|
127
|
+
value: key !== undefined ? this._values[key].v : undefined,
|
|
128
|
+
done: key !== undefined ? false : true
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
get size() {
|
|
134
|
+
return this._keys.length;
|
|
135
|
+
}
|
|
136
|
+
} as unknown) as MapConstructor;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { bsonMap as Map };
|
package/src/max_key.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** @public */
|
|
2
|
+
export interface MaxKeyExtended {
|
|
3
|
+
$maxKey: 1;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A class representation of the BSON MaxKey type.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export class MaxKey {
|
|
11
|
+
_bsontype!: 'MaxKey';
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
toExtendedJSON(): MaxKeyExtended {
|
|
15
|
+
return { $maxKey: 1 };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** @internal */
|
|
19
|
+
static fromExtendedJSON(): MaxKey {
|
|
20
|
+
return new MaxKey();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @internal */
|
|
24
|
+
[Symbol.for('nodejs.util.inspect.custom')](): string {
|
|
25
|
+
return this.inspect();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
inspect(): string {
|
|
29
|
+
return 'MaxKey()';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
Object.defineProperty(MaxKey.prototype, '_bsontype', { value: 'MaxKey' });
|
package/src/min_key.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** @public */
|
|
2
|
+
export interface MinKeyExtended {
|
|
3
|
+
$minKey: 1;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A class representation of the BSON MinKey type.
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
10
|
+
export class MinKey {
|
|
11
|
+
_bsontype!: 'MinKey';
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
toExtendedJSON(): MinKeyExtended {
|
|
15
|
+
return { $minKey: 1 };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** @internal */
|
|
19
|
+
static fromExtendedJSON(): MinKey {
|
|
20
|
+
return new MinKey();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @internal */
|
|
24
|
+
[Symbol.for('nodejs.util.inspect.custom')](): string {
|
|
25
|
+
return this.inspect();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
inspect(): string {
|
|
29
|
+
return 'MinKey()';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
Object.defineProperty(MinKey.prototype, '_bsontype', { value: 'MinKey' });
|
package/src/objectid.ts
ADDED
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
import { ensureBuffer } from './ensure_buffer';
|
|
3
|
+
import { deprecate, randomBytes } from './parser/utils';
|
|
4
|
+
|
|
5
|
+
// constants
|
|
6
|
+
const PROCESS_UNIQUE = randomBytes(5);
|
|
7
|
+
|
|
8
|
+
// Regular expression that checks for hex value
|
|
9
|
+
const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
|
|
10
|
+
|
|
11
|
+
// Precomputed hex table enables speedy hex string conversion
|
|
12
|
+
const hexTable: string[] = [];
|
|
13
|
+
for (let i = 0; i < 256; i++) {
|
|
14
|
+
hexTable[i] = (i <= 15 ? '0' : '') + i.toString(16);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Lookup tables
|
|
18
|
+
const decodeLookup: number[] = [];
|
|
19
|
+
let i = 0;
|
|
20
|
+
while (i < 10) decodeLookup[0x30 + i] = i++;
|
|
21
|
+
while (i < 16) decodeLookup[0x41 - 10 + i] = decodeLookup[0x61 - 10 + i] = i++;
|
|
22
|
+
|
|
23
|
+
/** @public */
|
|
24
|
+
export interface ObjectIdLike {
|
|
25
|
+
id: string | Buffer;
|
|
26
|
+
__id?: string;
|
|
27
|
+
toHexString(): string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @public */
|
|
31
|
+
export interface ObjectIdExtended {
|
|
32
|
+
$oid: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const kId = Symbol('id');
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A class representation of the BSON ObjectId type.
|
|
39
|
+
* @public
|
|
40
|
+
*/
|
|
41
|
+
export class ObjectId {
|
|
42
|
+
_bsontype!: 'ObjectId';
|
|
43
|
+
|
|
44
|
+
/** @internal */
|
|
45
|
+
static index = ~~(Math.random() * 0xffffff);
|
|
46
|
+
|
|
47
|
+
static cacheHexString: boolean;
|
|
48
|
+
|
|
49
|
+
/** ObjectId Bytes @internal */
|
|
50
|
+
private [kId]: Buffer;
|
|
51
|
+
/** ObjectId hexString cache @internal */
|
|
52
|
+
private __id?: string;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Create an ObjectId type
|
|
56
|
+
*
|
|
57
|
+
* @param id - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
|
|
58
|
+
*/
|
|
59
|
+
constructor(id?: string | Buffer | number | ObjectIdLike | ObjectId) {
|
|
60
|
+
// Duck-typing to support ObjectId from different npm packages
|
|
61
|
+
if (id instanceof ObjectId) {
|
|
62
|
+
this[kId] = id.id;
|
|
63
|
+
this.__id = id.__id;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (typeof id === 'object' && id && 'id' in id) {
|
|
67
|
+
if ('toHexString' in id && typeof id.toHexString === 'function') {
|
|
68
|
+
this[kId] = Buffer.from(id.toHexString(), 'hex');
|
|
69
|
+
} else {
|
|
70
|
+
this[kId] = typeof id.id === 'string' ? Buffer.from(id.id) : id.id;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// The most common use case (blank id, new objectId instance)
|
|
75
|
+
if (id == null || typeof id === 'number') {
|
|
76
|
+
// Generate a new id
|
|
77
|
+
this[kId] = ObjectId.generate(typeof id === 'number' ? id : undefined);
|
|
78
|
+
// If we are caching the hex string
|
|
79
|
+
if (ObjectId.cacheHexString) {
|
|
80
|
+
this.__id = this.id.toString('hex');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (ArrayBuffer.isView(id) && id.byteLength === 12) {
|
|
85
|
+
this[kId] = ensureBuffer(id);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (typeof id === 'string') {
|
|
89
|
+
if (id.length === 12) {
|
|
90
|
+
const bytes = Buffer.from(id);
|
|
91
|
+
if (bytes.byteLength === 12) {
|
|
92
|
+
this[kId] = bytes;
|
|
93
|
+
}
|
|
94
|
+
} else if (id.length === 24 && checkForHexRegExp.test(id)) {
|
|
95
|
+
this[kId] = Buffer.from(id, 'hex');
|
|
96
|
+
} else {
|
|
97
|
+
throw new TypeError(
|
|
98
|
+
'Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters'
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (ObjectId.cacheHexString) {
|
|
104
|
+
this.__id = this.id.toString('hex');
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The ObjectId bytes
|
|
110
|
+
* @readonly
|
|
111
|
+
*/
|
|
112
|
+
get id(): Buffer {
|
|
113
|
+
return this[kId];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
set id(value: Buffer) {
|
|
117
|
+
this[kId] = value;
|
|
118
|
+
if (ObjectId.cacheHexString) {
|
|
119
|
+
this.__id = value.toString('hex');
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The generation time of this ObjectId instance
|
|
125
|
+
* @deprecated Please use getTimestamp / createFromTime which returns an int32 epoch
|
|
126
|
+
*/
|
|
127
|
+
get generationTime(): number {
|
|
128
|
+
return this.id.readInt32BE(0);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
set generationTime(value: number) {
|
|
132
|
+
// Encode time into first 4 bytes
|
|
133
|
+
this.id.writeUInt32BE(value, 0);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Returns the ObjectId id as a 24 character hex string representation */
|
|
137
|
+
toHexString(): string {
|
|
138
|
+
if (ObjectId.cacheHexString && this.__id) {
|
|
139
|
+
return this.__id;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const hexString = this.id.toString('hex');
|
|
143
|
+
|
|
144
|
+
if (ObjectId.cacheHexString && !this.__id) {
|
|
145
|
+
this.__id = hexString;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return hexString;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Update the ObjectId index
|
|
153
|
+
* @privateRemarks
|
|
154
|
+
* Used in generating new ObjectId's on the driver
|
|
155
|
+
* @internal
|
|
156
|
+
*/
|
|
157
|
+
static getInc(): number {
|
|
158
|
+
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Generate a 12 byte id buffer used in ObjectId's
|
|
163
|
+
*
|
|
164
|
+
* @param time - pass in a second based timestamp.
|
|
165
|
+
*/
|
|
166
|
+
static generate(time?: number): Buffer {
|
|
167
|
+
if ('number' !== typeof time) {
|
|
168
|
+
time = ~~(Date.now() / 1000);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const inc = ObjectId.getInc();
|
|
172
|
+
const buffer = Buffer.alloc(12);
|
|
173
|
+
|
|
174
|
+
// 4-byte timestamp
|
|
175
|
+
buffer.writeUInt32BE(time, 0);
|
|
176
|
+
|
|
177
|
+
// 5-byte process unique
|
|
178
|
+
buffer[4] = PROCESS_UNIQUE[0];
|
|
179
|
+
buffer[5] = PROCESS_UNIQUE[1];
|
|
180
|
+
buffer[6] = PROCESS_UNIQUE[2];
|
|
181
|
+
buffer[7] = PROCESS_UNIQUE[3];
|
|
182
|
+
buffer[8] = PROCESS_UNIQUE[4];
|
|
183
|
+
|
|
184
|
+
// 3-byte counter
|
|
185
|
+
buffer[11] = inc & 0xff;
|
|
186
|
+
buffer[10] = (inc >> 8) & 0xff;
|
|
187
|
+
buffer[9] = (inc >> 16) & 0xff;
|
|
188
|
+
|
|
189
|
+
return buffer;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Converts the id into a 24 character hex string for printing
|
|
194
|
+
*
|
|
195
|
+
* @param format - The Buffer toString format parameter.
|
|
196
|
+
* @internal
|
|
197
|
+
*/
|
|
198
|
+
toString(format?: string): string {
|
|
199
|
+
// Is the id a buffer then use the buffer toString method to return the format
|
|
200
|
+
if (format) return this.id.toString(format);
|
|
201
|
+
return this.toHexString();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Converts to its JSON the 24 character hex string representation.
|
|
206
|
+
* @internal
|
|
207
|
+
*/
|
|
208
|
+
toJSON(): string {
|
|
209
|
+
return this.toHexString();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Compares the equality of this ObjectId with `otherID`.
|
|
214
|
+
*
|
|
215
|
+
* @param otherId - ObjectId instance to compare against.
|
|
216
|
+
*/
|
|
217
|
+
equals(otherId: string | ObjectId | ObjectIdLike): boolean {
|
|
218
|
+
if (otherId === undefined || otherId === null) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (otherId instanceof ObjectId) {
|
|
223
|
+
return this.toString() === otherId.toString();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (
|
|
227
|
+
typeof otherId === 'string' &&
|
|
228
|
+
ObjectId.isValid(otherId) &&
|
|
229
|
+
otherId.length === 12 &&
|
|
230
|
+
this.id instanceof Buffer
|
|
231
|
+
) {
|
|
232
|
+
return otherId === this.id.toString('binary');
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
|
|
236
|
+
return otherId.toLowerCase() === this.toHexString();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
|
|
240
|
+
return Buffer.from(otherId).equals(this.id);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (
|
|
244
|
+
typeof otherId === 'object' &&
|
|
245
|
+
'toHexString' in otherId &&
|
|
246
|
+
typeof otherId.toHexString === 'function'
|
|
247
|
+
) {
|
|
248
|
+
return otherId.toHexString() === this.toHexString();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Returns the generation date (accurate up to the second) that this ID was generated. */
|
|
255
|
+
getTimestamp(): Date {
|
|
256
|
+
const timestamp = new Date();
|
|
257
|
+
const time = this.id.readUInt32BE(0);
|
|
258
|
+
timestamp.setTime(Math.floor(time) * 1000);
|
|
259
|
+
return timestamp;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** @internal */
|
|
263
|
+
static createPk(): ObjectId {
|
|
264
|
+
return new ObjectId();
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
|
|
269
|
+
*
|
|
270
|
+
* @param time - an integer number representing a number of seconds.
|
|
271
|
+
*/
|
|
272
|
+
static createFromTime(time: number): ObjectId {
|
|
273
|
+
const buffer = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
274
|
+
// Encode time into first 4 bytes
|
|
275
|
+
buffer.writeUInt32BE(time, 0);
|
|
276
|
+
// Return the new objectId
|
|
277
|
+
return new ObjectId(buffer);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Creates an ObjectId from a hex string representation of an ObjectId.
|
|
282
|
+
*
|
|
283
|
+
* @param hexString - create a ObjectId from a passed in 24 character hexstring.
|
|
284
|
+
*/
|
|
285
|
+
static createFromHexString(hexString: string): ObjectId {
|
|
286
|
+
// Throw an error if it's not a valid setup
|
|
287
|
+
if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
|
|
288
|
+
throw new TypeError(
|
|
289
|
+
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return new ObjectId(Buffer.from(hexString, 'hex'));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Checks if a value is a valid bson ObjectId
|
|
298
|
+
*
|
|
299
|
+
* @param id - ObjectId instance to validate.
|
|
300
|
+
*/
|
|
301
|
+
static isValid(id: number | string | ObjectId | Buffer | ObjectIdLike): boolean {
|
|
302
|
+
if (id == null) return false;
|
|
303
|
+
|
|
304
|
+
if (typeof id === 'number') {
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (typeof id === 'string') {
|
|
309
|
+
return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
if (id instanceof ObjectId) {
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (id instanceof Buffer && id.length === 12) {
|
|
317
|
+
return true;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// Duck-Typing detection of ObjectId like objects
|
|
321
|
+
if (typeof id === 'object' && 'toHexString' in id && typeof id.toHexString === 'function') {
|
|
322
|
+
if (typeof id.id === 'string') {
|
|
323
|
+
return id.id.length === 12;
|
|
324
|
+
}
|
|
325
|
+
return id.toHexString().length === 24 && checkForHexRegExp.test(id.id.toString('hex'));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** @internal */
|
|
332
|
+
toExtendedJSON(): ObjectIdExtended {
|
|
333
|
+
if (this.toHexString) return { $oid: this.toHexString() };
|
|
334
|
+
return { $oid: this.toString('hex') };
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** @internal */
|
|
338
|
+
static fromExtendedJSON(doc: ObjectIdExtended): ObjectId {
|
|
339
|
+
return new ObjectId(doc.$oid);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Converts to a string representation of this Id.
|
|
344
|
+
*
|
|
345
|
+
* @returns return the 24 character hex string representation.
|
|
346
|
+
* @internal
|
|
347
|
+
*/
|
|
348
|
+
[Symbol.for('nodejs.util.inspect.custom')](): string {
|
|
349
|
+
return this.inspect();
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
inspect(): string {
|
|
353
|
+
return `ObjectId("${this.toHexString()}")`;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Deprecated methods
|
|
358
|
+
Object.defineProperty(ObjectId.prototype, 'generate', {
|
|
359
|
+
value: deprecate(
|
|
360
|
+
(time: number) => ObjectId.generate(time),
|
|
361
|
+
'Please use the static `ObjectId.generate(time)` instead'
|
|
362
|
+
)
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
Object.defineProperty(ObjectId.prototype, 'getInc', {
|
|
366
|
+
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
Object.defineProperty(ObjectId.prototype, 'get_inc', {
|
|
370
|
+
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
Object.defineProperty(ObjectId, 'get_inc', {
|
|
374
|
+
value: deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
Object.defineProperty(ObjectId.prototype, '_bsontype', { value: 'ObjectID' });
|