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/lib/objectid.js
CHANGED
|
@@ -1,419 +1,301 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ObjectId = void 0;
|
|
4
|
+
const buffer_1 = require("buffer");
|
|
5
|
+
const ensure_buffer_1 = require("./ensure_buffer");
|
|
6
|
+
const utils_1 = require("./parser/utils");
|
|
8
7
|
// constants
|
|
9
|
-
const PROCESS_UNIQUE = randomBytes(5);
|
|
10
|
-
|
|
8
|
+
const PROCESS_UNIQUE = utils_1.randomBytes(5);
|
|
11
9
|
// Regular expression that checks for hex value
|
|
12
10
|
const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
|
|
13
|
-
let hasBufferType = false;
|
|
14
|
-
|
|
15
|
-
// Check if buffer exists
|
|
16
|
-
try {
|
|
17
|
-
if (Buffer && Buffer.from) hasBufferType = true;
|
|
18
|
-
} catch (err) {
|
|
19
|
-
hasBufferType = false;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
11
|
// Precomputed hex table enables speedy hex string conversion
|
|
23
12
|
const hexTable = [];
|
|
24
13
|
for (let i = 0; i < 256; i++) {
|
|
25
|
-
|
|
14
|
+
hexTable[i] = (i <= 15 ? '0' : '') + i.toString(16);
|
|
26
15
|
}
|
|
27
|
-
|
|
28
16
|
// Lookup tables
|
|
29
17
|
const decodeLookup = [];
|
|
30
18
|
let i = 0;
|
|
31
|
-
while (i < 10)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return bytes.toString('hex');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function makeObjectIdError(invalidString, index) {
|
|
40
|
-
const invalidCharacter = invalidString[index];
|
|
41
|
-
return new TypeError(
|
|
42
|
-
`ObjectId string "${invalidString}" contains invalid character "${invalidCharacter}" with character code (${invalidString.charCodeAt(
|
|
43
|
-
index
|
|
44
|
-
)}). All character codes for a non-hex string must be less than 256.`
|
|
45
|
-
);
|
|
46
|
-
}
|
|
47
|
-
|
|
19
|
+
while (i < 10)
|
|
20
|
+
decodeLookup[0x30 + i] = i++;
|
|
21
|
+
while (i < 16)
|
|
22
|
+
decodeLookup[0x41 - 10 + i] = decodeLookup[0x61 - 10 + i] = i++;
|
|
23
|
+
const kId = Symbol('id');
|
|
48
24
|
/**
|
|
49
25
|
* A class representation of the BSON ObjectId type.
|
|
26
|
+
* @public
|
|
50
27
|
*/
|
|
51
28
|
class ObjectId {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Create an ObjectId type
|
|
31
|
+
*
|
|
32
|
+
* @param id - Can be a 24 character hex string, 12 byte binary Buffer, or a number.
|
|
33
|
+
*/
|
|
34
|
+
constructor(id) {
|
|
35
|
+
// Duck-typing to support ObjectId from different npm packages
|
|
36
|
+
if (id instanceof ObjectId) {
|
|
37
|
+
this[kId] = id.id;
|
|
38
|
+
this.__id = id.__id;
|
|
39
|
+
}
|
|
40
|
+
if (typeof id === 'object' && id && 'id' in id) {
|
|
41
|
+
if ('toHexString' in id && typeof id.toHexString === 'function') {
|
|
42
|
+
this[kId] = buffer_1.Buffer.from(id.toHexString(), 'hex');
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
this[kId] = typeof id.id === 'string' ? buffer_1.Buffer.from(id.id) : id.id;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
// The most common use case (blank id, new objectId instance)
|
|
49
|
+
if (id == null || typeof id === 'number') {
|
|
50
|
+
// Generate a new id
|
|
51
|
+
this[kId] = ObjectId.generate(typeof id === 'number' ? id : undefined);
|
|
52
|
+
// If we are caching the hex string
|
|
53
|
+
if (ObjectId.cacheHexString) {
|
|
54
|
+
this.__id = this.id.toString('hex');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (ArrayBuffer.isView(id) && id.byteLength === 12) {
|
|
58
|
+
this[kId] = ensure_buffer_1.ensureBuffer(id);
|
|
59
|
+
}
|
|
60
|
+
if (typeof id === 'string') {
|
|
61
|
+
if (id.length === 12) {
|
|
62
|
+
const bytes = buffer_1.Buffer.from(id);
|
|
63
|
+
if (bytes.byteLength === 12) {
|
|
64
|
+
this[kId] = bytes;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else if (id.length === 24 && checkForHexRegExp.test(id)) {
|
|
68
|
+
this[kId] = buffer_1.Buffer.from(id, 'hex');
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
throw new TypeError('Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (ObjectId.cacheHexString) {
|
|
75
|
+
this.__id = this.id.toString('hex');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The ObjectId bytes
|
|
80
|
+
* @readonly
|
|
81
|
+
*/
|
|
82
|
+
get id() {
|
|
83
|
+
return this[kId];
|
|
71
84
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (!valid && id != null) {
|
|
78
|
-
throw new TypeError(
|
|
79
|
-
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
|
|
80
|
-
);
|
|
81
|
-
} else if (valid && typeof id === 'string' && id.length === 24 && hasBufferType) {
|
|
82
|
-
return new ObjectId(Buffer.from(id, 'hex'));
|
|
83
|
-
} else if (valid && typeof id === 'string' && id.length === 24) {
|
|
84
|
-
return ObjectId.createFromHexString(id);
|
|
85
|
-
} else if (id != null && id.length === 12) {
|
|
86
|
-
// assume 12 byte string
|
|
87
|
-
this.id = id;
|
|
88
|
-
} else if (id != null && id.toHexString) {
|
|
89
|
-
// Duck-typing to support ObjectId from different npm packages
|
|
90
|
-
return ObjectId.createFromHexString(id.toHexString());
|
|
91
|
-
} else {
|
|
92
|
-
throw new TypeError(
|
|
93
|
-
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
|
|
94
|
-
);
|
|
85
|
+
set id(value) {
|
|
86
|
+
this[kId] = value;
|
|
87
|
+
if (ObjectId.cacheHexString) {
|
|
88
|
+
this.__id = value.toString('hex');
|
|
89
|
+
}
|
|
95
90
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
*
|
|
103
|
-
* @method
|
|
104
|
-
* @return {string} return the 24 byte hex string representation.
|
|
105
|
-
*/
|
|
106
|
-
toHexString() {
|
|
107
|
-
if (ObjectId.cacheHexString && this.__id) return this.__id;
|
|
108
|
-
|
|
109
|
-
let hexString = '';
|
|
110
|
-
if (!this.id || !this.id.length) {
|
|
111
|
-
throw new TypeError(
|
|
112
|
-
'invalid ObjectId, ObjectId.id must be either a string or a Buffer, but is [' +
|
|
113
|
-
JSON.stringify(this.id) +
|
|
114
|
-
']'
|
|
115
|
-
);
|
|
91
|
+
/**
|
|
92
|
+
* The generation time of this ObjectId instance
|
|
93
|
+
* @deprecated Please use getTimestamp / createFromTime which returns an int32 epoch
|
|
94
|
+
*/
|
|
95
|
+
get generationTime() {
|
|
96
|
+
return this.id.readInt32BE(0);
|
|
116
97
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
if (ObjectId.cacheHexString) this.__id = hexString;
|
|
121
|
-
return hexString;
|
|
98
|
+
set generationTime(value) {
|
|
99
|
+
// Encode time into first 4 bytes
|
|
100
|
+
this.id.writeUInt32BE(value, 0);
|
|
122
101
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
102
|
+
/** Returns the ObjectId id as a 24 character hex string representation */
|
|
103
|
+
toHexString() {
|
|
104
|
+
if (ObjectId.cacheHexString && this.__id) {
|
|
105
|
+
return this.__id;
|
|
106
|
+
}
|
|
107
|
+
const hexString = this.id.toString('hex');
|
|
108
|
+
if (ObjectId.cacheHexString && !this.__id) {
|
|
109
|
+
this.__id = hexString;
|
|
110
|
+
}
|
|
111
|
+
return hexString;
|
|
130
112
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
* @method
|
|
140
|
-
* @return {number} returns next index value.
|
|
141
|
-
* @ignore
|
|
142
|
-
*/
|
|
143
|
-
static getInc() {
|
|
144
|
-
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Generate a 12 byte id buffer used in ObjectId's
|
|
149
|
-
*
|
|
150
|
-
* @method
|
|
151
|
-
* @param {number} [time] optional parameter allowing to pass in a second based timestamp.
|
|
152
|
-
* @return {Buffer} return the 12 byte id buffer string.
|
|
153
|
-
*/
|
|
154
|
-
static generate(time) {
|
|
155
|
-
if ('number' !== typeof time) {
|
|
156
|
-
time = ~~(Date.now() / 1000);
|
|
113
|
+
/**
|
|
114
|
+
* Update the ObjectId index
|
|
115
|
+
* @privateRemarks
|
|
116
|
+
* Used in generating new ObjectId's on the driver
|
|
117
|
+
* @internal
|
|
118
|
+
*/
|
|
119
|
+
static getInc() {
|
|
120
|
+
return (ObjectId.index = (ObjectId.index + 1) % 0xffffff);
|
|
157
121
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Converts the id into a 24 byte hex string for printing
|
|
185
|
-
*
|
|
186
|
-
* @param {String} format The Buffer toString format parameter.
|
|
187
|
-
* @return {String} return the 24 byte hex string representation.
|
|
188
|
-
* @ignore
|
|
189
|
-
*/
|
|
190
|
-
toString(format) {
|
|
191
|
-
// Is the id a buffer then use the buffer toString method to return the format
|
|
192
|
-
if (this.id && this.id.copy) {
|
|
193
|
-
return this.id.toString(typeof format === 'string' ? format : 'hex');
|
|
122
|
+
/**
|
|
123
|
+
* Generate a 12 byte id buffer used in ObjectId's
|
|
124
|
+
*
|
|
125
|
+
* @param time - pass in a second based timestamp.
|
|
126
|
+
*/
|
|
127
|
+
static generate(time) {
|
|
128
|
+
if ('number' !== typeof time) {
|
|
129
|
+
time = ~~(Date.now() / 1000);
|
|
130
|
+
}
|
|
131
|
+
const inc = ObjectId.getInc();
|
|
132
|
+
const buffer = buffer_1.Buffer.alloc(12);
|
|
133
|
+
// 4-byte timestamp
|
|
134
|
+
buffer.writeUInt32BE(time, 0);
|
|
135
|
+
// 5-byte process unique
|
|
136
|
+
buffer[4] = PROCESS_UNIQUE[0];
|
|
137
|
+
buffer[5] = PROCESS_UNIQUE[1];
|
|
138
|
+
buffer[6] = PROCESS_UNIQUE[2];
|
|
139
|
+
buffer[7] = PROCESS_UNIQUE[3];
|
|
140
|
+
buffer[8] = PROCESS_UNIQUE[4];
|
|
141
|
+
// 3-byte counter
|
|
142
|
+
buffer[11] = inc & 0xff;
|
|
143
|
+
buffer[10] = (inc >> 8) & 0xff;
|
|
144
|
+
buffer[9] = (inc >> 16) & 0xff;
|
|
145
|
+
return buffer;
|
|
194
146
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
return this.toHexString();
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Compares the equality of this ObjectId with `otherID`.
|
|
211
|
-
*
|
|
212
|
-
* @method
|
|
213
|
-
* @param {object} otherId ObjectId instance to compare against.
|
|
214
|
-
* @return {boolean} the result of comparing two ObjectId's
|
|
215
|
-
*/
|
|
216
|
-
equals(otherId) {
|
|
217
|
-
if (otherId instanceof ObjectId) {
|
|
218
|
-
return this.toString() === otherId.toString();
|
|
147
|
+
/**
|
|
148
|
+
* Converts the id into a 24 character hex string for printing
|
|
149
|
+
*
|
|
150
|
+
* @param format - The Buffer toString format parameter.
|
|
151
|
+
* @internal
|
|
152
|
+
*/
|
|
153
|
+
toString(format) {
|
|
154
|
+
// Is the id a buffer then use the buffer toString method to return the format
|
|
155
|
+
if (format)
|
|
156
|
+
return this.id.toString(format);
|
|
157
|
+
return this.toHexString();
|
|
219
158
|
}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
) {
|
|
227
|
-
return otherId === this.id.toString('binary');
|
|
159
|
+
/**
|
|
160
|
+
* Converts to its JSON the 24 character hex string representation.
|
|
161
|
+
* @internal
|
|
162
|
+
*/
|
|
163
|
+
toJSON() {
|
|
164
|
+
return this.toHexString();
|
|
228
165
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Compares the equality of this ObjectId with `otherID`.
|
|
168
|
+
*
|
|
169
|
+
* @param otherId - ObjectId instance to compare against.
|
|
170
|
+
*/
|
|
171
|
+
equals(otherId) {
|
|
172
|
+
if (otherId === undefined || otherId === null) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
if (otherId instanceof ObjectId) {
|
|
176
|
+
return this.toString() === otherId.toString();
|
|
177
|
+
}
|
|
178
|
+
if (typeof otherId === 'string' &&
|
|
179
|
+
ObjectId.isValid(otherId) &&
|
|
180
|
+
otherId.length === 12 &&
|
|
181
|
+
this.id instanceof buffer_1.Buffer) {
|
|
182
|
+
return otherId === this.id.toString('binary');
|
|
183
|
+
}
|
|
184
|
+
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
|
|
185
|
+
return otherId.toLowerCase() === this.toHexString();
|
|
186
|
+
}
|
|
187
|
+
if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
|
|
188
|
+
return buffer_1.Buffer.from(otherId).equals(this.id);
|
|
189
|
+
}
|
|
190
|
+
if (typeof otherId === 'object' &&
|
|
191
|
+
'toHexString' in otherId &&
|
|
192
|
+
typeof otherId.toHexString === 'function') {
|
|
193
|
+
return otherId.toHexString() === this.toHexString();
|
|
194
|
+
}
|
|
195
|
+
return false;
|
|
232
196
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
197
|
+
/** Returns the generation date (accurate up to the second) that this ID was generated. */
|
|
198
|
+
getTimestamp() {
|
|
199
|
+
const timestamp = new Date();
|
|
200
|
+
const time = this.id.readUInt32BE(0);
|
|
201
|
+
timestamp.setTime(Math.floor(time) * 1000);
|
|
202
|
+
return timestamp;
|
|
236
203
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
204
|
+
/** @internal */
|
|
205
|
+
static createPk() {
|
|
206
|
+
return new ObjectId();
|
|
240
207
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
const timestamp = new Date();
|
|
253
|
-
const time = this.id.readUInt32BE(0);
|
|
254
|
-
timestamp.setTime(Math.floor(time) * 1000);
|
|
255
|
-
return timestamp;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* @ignore
|
|
260
|
-
*/
|
|
261
|
-
static createPk() {
|
|
262
|
-
return new ObjectId();
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
|
|
267
|
-
*
|
|
268
|
-
* @method
|
|
269
|
-
* @param {number} time an integer number representing a number of seconds.
|
|
270
|
-
* @return {ObjectId} return the created ObjectId
|
|
271
|
-
*/
|
|
272
|
-
static createFromTime(time) {
|
|
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[3] = time & 0xff;
|
|
276
|
-
buffer[2] = (time >> 8) & 0xff;
|
|
277
|
-
buffer[1] = (time >> 16) & 0xff;
|
|
278
|
-
buffer[0] = (time >> 24) & 0xff;
|
|
279
|
-
// Return the new objectId
|
|
280
|
-
return new ObjectId(buffer);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Creates an ObjectId from a hex string representation of an ObjectId.
|
|
285
|
-
*
|
|
286
|
-
* @method
|
|
287
|
-
* @param {string} hexString create a ObjectId from a passed in 24 byte hexstring.
|
|
288
|
-
* @return {ObjectId} return the created ObjectId
|
|
289
|
-
*/
|
|
290
|
-
static createFromHexString(string) {
|
|
291
|
-
// Throw an error if it's not a valid setup
|
|
292
|
-
if (typeof string === 'undefined' || (string != null && string.length !== 24)) {
|
|
293
|
-
throw new TypeError(
|
|
294
|
-
'Argument passed in must be a single String of 12 bytes or a string of 24 hex characters'
|
|
295
|
-
);
|
|
208
|
+
/**
|
|
209
|
+
* Creates an ObjectId from a second based number, with the rest of the ObjectId zeroed out. Used for comparisons or sorting the ObjectId.
|
|
210
|
+
*
|
|
211
|
+
* @param time - an integer number representing a number of seconds.
|
|
212
|
+
*/
|
|
213
|
+
static createFromTime(time) {
|
|
214
|
+
const buffer = buffer_1.Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
215
|
+
// Encode time into first 4 bytes
|
|
216
|
+
buffer.writeUInt32BE(time, 0);
|
|
217
|
+
// Return the new objectId
|
|
218
|
+
return new ObjectId(buffer);
|
|
296
219
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
(decodeLookup[string.charCodeAt(i++)] << 4) | decodeLookup[string.charCodeAt(i++)];
|
|
220
|
+
/**
|
|
221
|
+
* Creates an ObjectId from a hex string representation of an ObjectId.
|
|
222
|
+
*
|
|
223
|
+
* @param hexString - create a ObjectId from a passed in 24 character hexstring.
|
|
224
|
+
*/
|
|
225
|
+
static createFromHexString(hexString) {
|
|
226
|
+
// Throw an error if it's not a valid setup
|
|
227
|
+
if (typeof hexString === 'undefined' || (hexString != null && hexString.length !== 24)) {
|
|
228
|
+
throw new TypeError('Argument passed in must be a single String of 12 bytes or a string of 24 hex characters');
|
|
229
|
+
}
|
|
230
|
+
return new ObjectId(buffer_1.Buffer.from(hexString, 'hex'));
|
|
309
231
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
232
|
+
/**
|
|
233
|
+
* Checks if a value is a valid bson ObjectId
|
|
234
|
+
*
|
|
235
|
+
* @param id - ObjectId instance to validate.
|
|
236
|
+
*/
|
|
237
|
+
static isValid(id) {
|
|
238
|
+
if (id == null)
|
|
239
|
+
return false;
|
|
240
|
+
if (typeof id === 'number') {
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
if (typeof id === 'string') {
|
|
244
|
+
return id.length === 12 || (id.length === 24 && checkForHexRegExp.test(id));
|
|
245
|
+
}
|
|
246
|
+
if (id instanceof ObjectId) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
if (id instanceof buffer_1.Buffer && id.length === 12) {
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
// Duck-Typing detection of ObjectId like objects
|
|
253
|
+
if (typeof id === 'object' && 'toHexString' in id && typeof id.toHexString === 'function') {
|
|
254
|
+
if (typeof id.id === 'string') {
|
|
255
|
+
return id.id.length === 12;
|
|
256
|
+
}
|
|
257
|
+
return id.toHexString().length === 24 && checkForHexRegExp.test(id.id.toString('hex'));
|
|
258
|
+
}
|
|
259
|
+
return false;
|
|
326
260
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
261
|
+
/** @internal */
|
|
262
|
+
toExtendedJSON() {
|
|
263
|
+
if (this.toHexString)
|
|
264
|
+
return { $oid: this.toHexString() };
|
|
265
|
+
return { $oid: this.toString('hex') };
|
|
330
266
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
267
|
+
/** @internal */
|
|
268
|
+
static fromExtendedJSON(doc) {
|
|
269
|
+
return new ObjectId(doc.$oid);
|
|
334
270
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
271
|
+
/**
|
|
272
|
+
* Converts to a string representation of this Id.
|
|
273
|
+
*
|
|
274
|
+
* @returns return the 24 character hex string representation.
|
|
275
|
+
* @internal
|
|
276
|
+
*/
|
|
277
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
278
|
+
return this.inspect();
|
|
338
279
|
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
if (id.toHexString) {
|
|
342
|
-
return id.id.length === 12 || (id.id.length === 24 && checkForHexRegExp.test(id.id));
|
|
280
|
+
inspect() {
|
|
281
|
+
return `ObjectId("${this.toHexString()}")`;
|
|
343
282
|
}
|
|
344
|
-
|
|
345
|
-
return false;
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* @ignore
|
|
350
|
-
*/
|
|
351
|
-
toExtendedJSON() {
|
|
352
|
-
if (this.toHexString) return { $oid: this.toHexString() };
|
|
353
|
-
return { $oid: this.toString('hex') };
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* @ignore
|
|
358
|
-
*/
|
|
359
|
-
static fromExtendedJSON(doc) {
|
|
360
|
-
return new ObjectId(doc.$oid);
|
|
361
|
-
}
|
|
362
283
|
}
|
|
363
|
-
|
|
284
|
+
exports.ObjectId = ObjectId;
|
|
285
|
+
/** @internal */
|
|
286
|
+
ObjectId.index = ~~(Math.random() * 0xffffff);
|
|
364
287
|
// Deprecated methods
|
|
365
|
-
ObjectId.
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
);
|
|
374
|
-
|
|
375
|
-
ObjectId.
|
|
376
|
-
() => ObjectId.getInc(),
|
|
377
|
-
'Please use the static `ObjectId.getInc()` instead'
|
|
378
|
-
);
|
|
379
|
-
|
|
380
|
-
ObjectId.prototype.generate = deprecate(
|
|
381
|
-
time => ObjectId.generate(time),
|
|
382
|
-
'Please use the static `ObjectId.generate(time)` instead'
|
|
383
|
-
);
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
* @ignore
|
|
387
|
-
*/
|
|
388
|
-
Object.defineProperty(ObjectId.prototype, 'generationTime', {
|
|
389
|
-
enumerable: true,
|
|
390
|
-
get: function() {
|
|
391
|
-
return this.id[3] | (this.id[2] << 8) | (this.id[1] << 16) | (this.id[0] << 24);
|
|
392
|
-
},
|
|
393
|
-
set: function(value) {
|
|
394
|
-
// Encode time into first 4 bytes
|
|
395
|
-
this.id[3] = value & 0xff;
|
|
396
|
-
this.id[2] = (value >> 8) & 0xff;
|
|
397
|
-
this.id[1] = (value >> 16) & 0xff;
|
|
398
|
-
this.id[0] = (value >> 24) & 0xff;
|
|
399
|
-
}
|
|
288
|
+
Object.defineProperty(ObjectId.prototype, 'generate', {
|
|
289
|
+
value: utils_1.deprecate((time) => ObjectId.generate(time), 'Please use the static `ObjectId.generate(time)` instead')
|
|
290
|
+
});
|
|
291
|
+
Object.defineProperty(ObjectId.prototype, 'getInc', {
|
|
292
|
+
value: utils_1.deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
|
|
293
|
+
});
|
|
294
|
+
Object.defineProperty(ObjectId.prototype, 'get_inc', {
|
|
295
|
+
value: utils_1.deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
|
|
296
|
+
});
|
|
297
|
+
Object.defineProperty(ObjectId, 'get_inc', {
|
|
298
|
+
value: utils_1.deprecate(() => ObjectId.getInc(), 'Please use the static `ObjectId.getInc()` instead')
|
|
400
299
|
});
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Converts to a string representation of this Id.
|
|
404
|
-
*
|
|
405
|
-
* @return {String} return the 24 byte hex string representation.
|
|
406
|
-
* @ignore
|
|
407
|
-
*/
|
|
408
|
-
ObjectId.prototype[util.inspect.custom || 'inspect'] = ObjectId.prototype.toString;
|
|
409
|
-
|
|
410
|
-
/**
|
|
411
|
-
* @ignore
|
|
412
|
-
*/
|
|
413
|
-
ObjectId.index = ~~(Math.random() * 0xffffff);
|
|
414
|
-
|
|
415
|
-
// In 4.0.0 and 4.0.1, this property name was changed to ObjectId to match the class name.
|
|
416
|
-
// This caused interoperability problems with previous versions of the library, so in
|
|
417
|
-
// later builds we changed it back to ObjectID (capital D) to match legacy implementations.
|
|
418
300
|
Object.defineProperty(ObjectId.prototype, '_bsontype', { value: 'ObjectID' });
|
|
419
|
-
|
|
301
|
+
//# sourceMappingURL=objectid.js.map
|