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/bson.ts
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
import { Binary } from './binary';
|
|
3
|
+
import { Code } from './code';
|
|
4
|
+
import { DBRef } from './db_ref';
|
|
5
|
+
import { Decimal128 } from './decimal128';
|
|
6
|
+
import { Double } from './double';
|
|
7
|
+
import { ensureBuffer } from './ensure_buffer';
|
|
8
|
+
import { EJSON } from './extended_json';
|
|
9
|
+
import { Int32 } from './int_32';
|
|
10
|
+
import { Long } from './long';
|
|
11
|
+
import { Map } from './map';
|
|
12
|
+
import { MaxKey } from './max_key';
|
|
13
|
+
import { MinKey } from './min_key';
|
|
14
|
+
import { ObjectId } from './objectid';
|
|
15
|
+
import { calculateObjectSize as internalCalculateObjectSize } from './parser/calculate_size';
|
|
16
|
+
// Parts of the parser
|
|
17
|
+
import { deserialize as internalDeserialize, DeserializeOptions } from './parser/deserializer';
|
|
18
|
+
import { serializeInto as internalSerialize, SerializeOptions } from './parser/serializer';
|
|
19
|
+
import { BSONRegExp } from './regexp';
|
|
20
|
+
import { BSONSymbol } from './symbol';
|
|
21
|
+
import { Timestamp } from './timestamp';
|
|
22
|
+
export { BinaryExtended, BinaryExtendedLegacy, BinarySequence } from './binary';
|
|
23
|
+
export { CodeExtended } from './code';
|
|
24
|
+
export {
|
|
25
|
+
BSON_BINARY_SUBTYPE_BYTE_ARRAY,
|
|
26
|
+
BSON_BINARY_SUBTYPE_DEFAULT,
|
|
27
|
+
BSON_BINARY_SUBTYPE_FUNCTION,
|
|
28
|
+
BSON_BINARY_SUBTYPE_MD5,
|
|
29
|
+
BSON_BINARY_SUBTYPE_USER_DEFINED,
|
|
30
|
+
BSON_BINARY_SUBTYPE_UUID,
|
|
31
|
+
BSON_BINARY_SUBTYPE_UUID_NEW,
|
|
32
|
+
BSON_DATA_ARRAY,
|
|
33
|
+
BSON_DATA_BINARY,
|
|
34
|
+
BSON_DATA_BOOLEAN,
|
|
35
|
+
BSON_DATA_CODE,
|
|
36
|
+
BSON_DATA_CODE_W_SCOPE,
|
|
37
|
+
BSON_DATA_DATE,
|
|
38
|
+
BSON_DATA_DBPOINTER,
|
|
39
|
+
BSON_DATA_DECIMAL128,
|
|
40
|
+
BSON_DATA_INT,
|
|
41
|
+
BSON_DATA_LONG,
|
|
42
|
+
BSON_DATA_MAX_KEY,
|
|
43
|
+
BSON_DATA_MIN_KEY,
|
|
44
|
+
BSON_DATA_NULL,
|
|
45
|
+
BSON_DATA_NUMBER,
|
|
46
|
+
BSON_DATA_OBJECT,
|
|
47
|
+
BSON_DATA_OID,
|
|
48
|
+
BSON_DATA_REGEXP,
|
|
49
|
+
BSON_DATA_STRING,
|
|
50
|
+
BSON_DATA_SYMBOL,
|
|
51
|
+
BSON_DATA_TIMESTAMP,
|
|
52
|
+
BSON_DATA_UNDEFINED,
|
|
53
|
+
BSON_INT32_MAX,
|
|
54
|
+
BSON_INT32_MIN,
|
|
55
|
+
BSON_INT64_MAX,
|
|
56
|
+
BSON_INT64_MIN
|
|
57
|
+
} from './constants';
|
|
58
|
+
export { DBRefLike } from './db_ref';
|
|
59
|
+
export { Decimal128Extended } from './decimal128';
|
|
60
|
+
export { DoubleExtended } from './double';
|
|
61
|
+
export { EJSON, EJSONOptions } from './extended_json';
|
|
62
|
+
export { Int32Extended } from './int_32';
|
|
63
|
+
export { LongExtended } from './long';
|
|
64
|
+
export { MaxKeyExtended } from './max_key';
|
|
65
|
+
export { MinKeyExtended } from './min_key';
|
|
66
|
+
export { ObjectIdExtended, ObjectIdLike } from './objectid';
|
|
67
|
+
export { BSONRegExpExtended, BSONRegExpExtendedLegacy } from './regexp';
|
|
68
|
+
export { BSONSymbolExtended } from './symbol';
|
|
69
|
+
export {
|
|
70
|
+
LongWithoutOverrides,
|
|
71
|
+
LongWithoutOverridesClass,
|
|
72
|
+
TimestampExtended,
|
|
73
|
+
TimestampOverrides
|
|
74
|
+
} from './timestamp';
|
|
75
|
+
export { UUIDExtended } from './uuid';
|
|
76
|
+
export { SerializeOptions, DeserializeOptions };
|
|
77
|
+
export {
|
|
78
|
+
Code,
|
|
79
|
+
Map,
|
|
80
|
+
BSONSymbol,
|
|
81
|
+
DBRef,
|
|
82
|
+
Binary,
|
|
83
|
+
ObjectId,
|
|
84
|
+
Long,
|
|
85
|
+
Timestamp,
|
|
86
|
+
Double,
|
|
87
|
+
Int32,
|
|
88
|
+
MinKey,
|
|
89
|
+
MaxKey,
|
|
90
|
+
BSONRegExp,
|
|
91
|
+
Decimal128,
|
|
92
|
+
// In 4.0.0 and 4.0.1, this property name was changed to ObjectId to match the class name.
|
|
93
|
+
// This caused interoperability problems with previous versions of the library, so in
|
|
94
|
+
// later builds we changed it back to ObjectID (capital D) to match legacy implementations.
|
|
95
|
+
ObjectId as ObjectID
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/** @public */
|
|
99
|
+
export interface Document {
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
+
[key: string]: any;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** @internal */
|
|
105
|
+
// Default Max Size
|
|
106
|
+
const MAXSIZE = 1024 * 1024 * 17;
|
|
107
|
+
|
|
108
|
+
// Current Internal Temporary Serialization Buffer
|
|
109
|
+
let buffer = Buffer.alloc(MAXSIZE);
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Sets the size of the internal serialization buffer.
|
|
113
|
+
*
|
|
114
|
+
* @param size - The desired size for the internal serialization buffer
|
|
115
|
+
* @public
|
|
116
|
+
*/
|
|
117
|
+
export function setInternalBufferSize(size: number): void {
|
|
118
|
+
// Resize the internal serialization buffer if needed
|
|
119
|
+
if (buffer.length < size) {
|
|
120
|
+
buffer = Buffer.alloc(size);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Serialize a Javascript object.
|
|
126
|
+
*
|
|
127
|
+
* @param object - the Javascript object to serialize.
|
|
128
|
+
* @returns Buffer object containing the serialized object.
|
|
129
|
+
* @public
|
|
130
|
+
*/
|
|
131
|
+
export function serialize(object: Document, options: SerializeOptions = {}): Buffer {
|
|
132
|
+
// Unpack the options
|
|
133
|
+
const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
|
|
134
|
+
const serializeFunctions =
|
|
135
|
+
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
|
|
136
|
+
const ignoreUndefined =
|
|
137
|
+
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
|
|
138
|
+
const minInternalBufferSize =
|
|
139
|
+
typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE;
|
|
140
|
+
|
|
141
|
+
// Resize the internal serialization buffer if needed
|
|
142
|
+
if (buffer.length < minInternalBufferSize) {
|
|
143
|
+
buffer = Buffer.alloc(minInternalBufferSize);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Attempt to serialize
|
|
147
|
+
const serializationIndex = internalSerialize(
|
|
148
|
+
buffer,
|
|
149
|
+
object,
|
|
150
|
+
checkKeys,
|
|
151
|
+
0,
|
|
152
|
+
0,
|
|
153
|
+
serializeFunctions,
|
|
154
|
+
ignoreUndefined,
|
|
155
|
+
[]
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
// Create the final buffer
|
|
159
|
+
const finishedBuffer = Buffer.alloc(serializationIndex);
|
|
160
|
+
|
|
161
|
+
// Copy into the finished buffer
|
|
162
|
+
buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
|
|
163
|
+
|
|
164
|
+
// Return the buffer
|
|
165
|
+
return finishedBuffer;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Serialize a Javascript object using a predefined Buffer and index into the buffer,
|
|
170
|
+
* useful when pre-allocating the space for serialization.
|
|
171
|
+
*
|
|
172
|
+
* @param object - the Javascript object to serialize.
|
|
173
|
+
* @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object.
|
|
174
|
+
* @returns the index pointing to the last written byte in the buffer.
|
|
175
|
+
* @public
|
|
176
|
+
*/
|
|
177
|
+
export function serializeWithBufferAndIndex(
|
|
178
|
+
object: Document,
|
|
179
|
+
finalBuffer: Buffer,
|
|
180
|
+
options: SerializeOptions = {}
|
|
181
|
+
): number {
|
|
182
|
+
// Unpack the options
|
|
183
|
+
const checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
|
|
184
|
+
const serializeFunctions =
|
|
185
|
+
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
|
|
186
|
+
const ignoreUndefined =
|
|
187
|
+
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
|
|
188
|
+
const startIndex = typeof options.index === 'number' ? options.index : 0;
|
|
189
|
+
|
|
190
|
+
// Attempt to serialize
|
|
191
|
+
const serializationIndex = internalSerialize(
|
|
192
|
+
buffer,
|
|
193
|
+
object,
|
|
194
|
+
checkKeys,
|
|
195
|
+
0,
|
|
196
|
+
0,
|
|
197
|
+
serializeFunctions,
|
|
198
|
+
ignoreUndefined
|
|
199
|
+
);
|
|
200
|
+
buffer.copy(finalBuffer, startIndex, 0, serializationIndex);
|
|
201
|
+
|
|
202
|
+
// Return the index
|
|
203
|
+
return startIndex + serializationIndex - 1;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Deserialize data as BSON.
|
|
208
|
+
*
|
|
209
|
+
* @param buffer - the buffer containing the serialized set of BSON documents.
|
|
210
|
+
* @returns returns the deserialized Javascript Object.
|
|
211
|
+
* @public
|
|
212
|
+
*/
|
|
213
|
+
export function deserialize(
|
|
214
|
+
buffer: Buffer | ArrayBufferView | ArrayBuffer,
|
|
215
|
+
options: DeserializeOptions = {}
|
|
216
|
+
): Document {
|
|
217
|
+
return internalDeserialize(ensureBuffer(buffer), options);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** @public */
|
|
221
|
+
export type CalculateObjectSizeOptions = Pick<
|
|
222
|
+
SerializeOptions,
|
|
223
|
+
'serializeFunctions' | 'ignoreUndefined'
|
|
224
|
+
>;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Calculate the bson size for a passed in Javascript object.
|
|
228
|
+
*
|
|
229
|
+
* @param object - the Javascript object to calculate the BSON byte size for
|
|
230
|
+
* @returns size of BSON object in bytes
|
|
231
|
+
* @public
|
|
232
|
+
*/
|
|
233
|
+
export function calculateObjectSize(
|
|
234
|
+
object: Document,
|
|
235
|
+
options: CalculateObjectSizeOptions = {}
|
|
236
|
+
): number {
|
|
237
|
+
options = options || {};
|
|
238
|
+
|
|
239
|
+
const serializeFunctions =
|
|
240
|
+
typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
|
|
241
|
+
const ignoreUndefined =
|
|
242
|
+
typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
|
|
243
|
+
|
|
244
|
+
return internalCalculateObjectSize(object, serializeFunctions, ignoreUndefined);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Deserialize stream data as BSON documents.
|
|
249
|
+
*
|
|
250
|
+
* @param data - the buffer containing the serialized set of BSON documents.
|
|
251
|
+
* @param startIndex - the start index in the data Buffer where the deserialization is to start.
|
|
252
|
+
* @param numberOfDocuments - number of documents to deserialize.
|
|
253
|
+
* @param documents - an array where to store the deserialized documents.
|
|
254
|
+
* @param docStartIndex - the index in the documents array from where to start inserting documents.
|
|
255
|
+
* @param options - additional options used for the deserialization.
|
|
256
|
+
* @returns next index in the buffer after deserialization **x** numbers of documents.
|
|
257
|
+
* @public
|
|
258
|
+
*/
|
|
259
|
+
export function deserializeStream(
|
|
260
|
+
data: Buffer | ArrayBufferView | ArrayBuffer,
|
|
261
|
+
startIndex: number,
|
|
262
|
+
numberOfDocuments: number,
|
|
263
|
+
documents: Document[],
|
|
264
|
+
docStartIndex: number,
|
|
265
|
+
options: DeserializeOptions
|
|
266
|
+
): number {
|
|
267
|
+
const internalOptions = Object.assign(
|
|
268
|
+
{ allowObjectSmallerThanBufferSize: true, index: 0 },
|
|
269
|
+
options
|
|
270
|
+
);
|
|
271
|
+
const bufferData = ensureBuffer(data);
|
|
272
|
+
|
|
273
|
+
let index = startIndex;
|
|
274
|
+
// Loop over all documents
|
|
275
|
+
for (let i = 0; i < numberOfDocuments; i++) {
|
|
276
|
+
// Find size of the document
|
|
277
|
+
const size =
|
|
278
|
+
bufferData[index] |
|
|
279
|
+
(bufferData[index + 1] << 8) |
|
|
280
|
+
(bufferData[index + 2] << 16) |
|
|
281
|
+
(bufferData[index + 3] << 24);
|
|
282
|
+
// Update options with index
|
|
283
|
+
internalOptions.index = index;
|
|
284
|
+
// Parse the document at this point
|
|
285
|
+
documents[docStartIndex + i] = internalDeserialize(bufferData, internalOptions);
|
|
286
|
+
// Adjust index by the document size
|
|
287
|
+
index = index + size;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Return object containing end index of parsing and list of documents
|
|
291
|
+
return index;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* BSON default export
|
|
296
|
+
* @deprecated Please use named exports
|
|
297
|
+
* @privateRemarks
|
|
298
|
+
* We want to someday deprecate the default export,
|
|
299
|
+
* so none of the new TS types are being exported on the default
|
|
300
|
+
* @public
|
|
301
|
+
*/
|
|
302
|
+
const BSON = {
|
|
303
|
+
Binary,
|
|
304
|
+
Code,
|
|
305
|
+
DBRef,
|
|
306
|
+
Decimal128,
|
|
307
|
+
Double,
|
|
308
|
+
Int32,
|
|
309
|
+
Long,
|
|
310
|
+
Map,
|
|
311
|
+
MaxKey,
|
|
312
|
+
MinKey,
|
|
313
|
+
ObjectId,
|
|
314
|
+
ObjectID: ObjectId,
|
|
315
|
+
BSONRegExp,
|
|
316
|
+
BSONSymbol,
|
|
317
|
+
Timestamp,
|
|
318
|
+
EJSON,
|
|
319
|
+
setInternalBufferSize,
|
|
320
|
+
serialize,
|
|
321
|
+
serializeWithBufferAndIndex,
|
|
322
|
+
deserialize,
|
|
323
|
+
calculateObjectSize,
|
|
324
|
+
deserializeStream
|
|
325
|
+
};
|
|
326
|
+
export default BSON;
|
package/src/code.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Document } from './bson';
|
|
2
|
+
|
|
3
|
+
/** @public */
|
|
4
|
+
export interface CodeExtended {
|
|
5
|
+
$code: string | Function;
|
|
6
|
+
$scope?: Document;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A class representation of the BSON Code type.
|
|
11
|
+
* @public
|
|
12
|
+
*/
|
|
13
|
+
export class Code {
|
|
14
|
+
_bsontype!: 'Code';
|
|
15
|
+
|
|
16
|
+
code: string | Function;
|
|
17
|
+
scope?: Document;
|
|
18
|
+
/**
|
|
19
|
+
* @param code - a string or function.
|
|
20
|
+
* @param scope - an optional scope for the function.
|
|
21
|
+
*/
|
|
22
|
+
constructor(code: string | Function, scope?: Document) {
|
|
23
|
+
this.code = code;
|
|
24
|
+
this.scope = scope;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** @internal */
|
|
28
|
+
toJSON(): { code: string | Function; scope?: Document } {
|
|
29
|
+
return { code: this.code, scope: this.scope };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @internal */
|
|
33
|
+
toExtendedJSON(): CodeExtended {
|
|
34
|
+
if (this.scope) {
|
|
35
|
+
return { $code: this.code, $scope: this.scope };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return { $code: this.code };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** @internal */
|
|
42
|
+
static fromExtendedJSON(doc: CodeExtended): Code {
|
|
43
|
+
return new Code(doc.$code, doc.$scope);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** @internal */
|
|
47
|
+
[Symbol.for('nodejs.util.inspect.custom')](): string {
|
|
48
|
+
return this.inspect();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
inspect(): string {
|
|
52
|
+
const codeJson = this.toJSON();
|
|
53
|
+
return `Code("${codeJson.code}"${codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
Object.defineProperty(Code.prototype, '_bsontype', { value: 'Code' });
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
export const BSON_INT32_MAX = 0x7fffffff;
|
|
3
|
+
/** @internal */
|
|
4
|
+
export const BSON_INT32_MIN = -0x80000000;
|
|
5
|
+
/** @internal */
|
|
6
|
+
export const BSON_INT64_MAX = Math.pow(2, 63) - 1;
|
|
7
|
+
/** @internal */
|
|
8
|
+
export const BSON_INT64_MIN = -Math.pow(2, 63);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Any integer up to 2^53 can be precisely represented by a double.
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export const JS_INT_MAX = Math.pow(2, 53);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Any integer down to -2^53 can be precisely represented by a double.
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export const JS_INT_MIN = -Math.pow(2, 53);
|
|
21
|
+
|
|
22
|
+
/** Number BSON Type @internal */
|
|
23
|
+
export const BSON_DATA_NUMBER = 1;
|
|
24
|
+
|
|
25
|
+
/** String BSON Type @internal */
|
|
26
|
+
export const BSON_DATA_STRING = 2;
|
|
27
|
+
|
|
28
|
+
/** Object BSON Type @internal */
|
|
29
|
+
export const BSON_DATA_OBJECT = 3;
|
|
30
|
+
|
|
31
|
+
/** Array BSON Type @internal */
|
|
32
|
+
export const BSON_DATA_ARRAY = 4;
|
|
33
|
+
|
|
34
|
+
/** Binary BSON Type @internal */
|
|
35
|
+
export const BSON_DATA_BINARY = 5;
|
|
36
|
+
|
|
37
|
+
/** Binary BSON Type @internal */
|
|
38
|
+
export const BSON_DATA_UNDEFINED = 6;
|
|
39
|
+
|
|
40
|
+
/** ObjectId BSON Type @internal */
|
|
41
|
+
export const BSON_DATA_OID = 7;
|
|
42
|
+
|
|
43
|
+
/** Boolean BSON Type @internal */
|
|
44
|
+
export const BSON_DATA_BOOLEAN = 8;
|
|
45
|
+
|
|
46
|
+
/** Date BSON Type @internal */
|
|
47
|
+
export const BSON_DATA_DATE = 9;
|
|
48
|
+
|
|
49
|
+
/** null BSON Type @internal */
|
|
50
|
+
export const BSON_DATA_NULL = 10;
|
|
51
|
+
|
|
52
|
+
/** RegExp BSON Type @internal */
|
|
53
|
+
export const BSON_DATA_REGEXP = 11;
|
|
54
|
+
|
|
55
|
+
/** Code BSON Type @internal */
|
|
56
|
+
export const BSON_DATA_DBPOINTER = 12;
|
|
57
|
+
|
|
58
|
+
/** Code BSON Type @internal */
|
|
59
|
+
export const BSON_DATA_CODE = 13;
|
|
60
|
+
|
|
61
|
+
/** Symbol BSON Type @internal */
|
|
62
|
+
export const BSON_DATA_SYMBOL = 14;
|
|
63
|
+
|
|
64
|
+
/** Code with Scope BSON Type @internal */
|
|
65
|
+
export const BSON_DATA_CODE_W_SCOPE = 15;
|
|
66
|
+
|
|
67
|
+
/** 32 bit Integer BSON Type @internal */
|
|
68
|
+
export const BSON_DATA_INT = 16;
|
|
69
|
+
|
|
70
|
+
/** Timestamp BSON Type @internal */
|
|
71
|
+
export const BSON_DATA_TIMESTAMP = 17;
|
|
72
|
+
|
|
73
|
+
/** Long BSON Type @internal */
|
|
74
|
+
export const BSON_DATA_LONG = 18;
|
|
75
|
+
|
|
76
|
+
/** Decimal128 BSON Type @internal */
|
|
77
|
+
export const BSON_DATA_DECIMAL128 = 19;
|
|
78
|
+
|
|
79
|
+
/** MinKey BSON Type @internal */
|
|
80
|
+
export const BSON_DATA_MIN_KEY = 0xff;
|
|
81
|
+
|
|
82
|
+
/** MaxKey BSON Type @internal */
|
|
83
|
+
export const BSON_DATA_MAX_KEY = 0x7f;
|
|
84
|
+
|
|
85
|
+
/** Binary Default Type @internal */
|
|
86
|
+
export const BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
|
87
|
+
|
|
88
|
+
/** Binary Function Type @internal */
|
|
89
|
+
export const BSON_BINARY_SUBTYPE_FUNCTION = 1;
|
|
90
|
+
|
|
91
|
+
/** Binary Byte Array Type @internal */
|
|
92
|
+
export const BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
|
|
93
|
+
|
|
94
|
+
/** Binary Deprecated UUID Type @deprecated Please use BSON_BINARY_SUBTYPE_UUID_NEW @internal */
|
|
95
|
+
export const BSON_BINARY_SUBTYPE_UUID = 3;
|
|
96
|
+
|
|
97
|
+
/** Binary UUID Type @internal */
|
|
98
|
+
export const BSON_BINARY_SUBTYPE_UUID_NEW = 4;
|
|
99
|
+
|
|
100
|
+
/** Binary MD5 Type @internal */
|
|
101
|
+
export const BSON_BINARY_SUBTYPE_MD5 = 5;
|
|
102
|
+
|
|
103
|
+
/** Binary User Defined Type @internal */
|
|
104
|
+
export const BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
|
package/src/db_ref.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { Document } from './bson';
|
|
2
|
+
import type { EJSONOptions } from './extended_json';
|
|
3
|
+
import type { ObjectId } from './objectid';
|
|
4
|
+
import { isObjectLike } from './parser/utils';
|
|
5
|
+
|
|
6
|
+
/** @public */
|
|
7
|
+
export interface DBRefLike {
|
|
8
|
+
$ref: string;
|
|
9
|
+
$id: ObjectId;
|
|
10
|
+
$db?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
export function isDBRefLike(value: unknown): value is DBRefLike {
|
|
15
|
+
return isObjectLike(value) && value['$id'] != null && value['$ref'] != null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A class representation of the BSON DBRef type.
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
export class DBRef {
|
|
23
|
+
_bsontype!: 'DBRef';
|
|
24
|
+
|
|
25
|
+
collection: string;
|
|
26
|
+
oid: ObjectId;
|
|
27
|
+
db?: string;
|
|
28
|
+
fields: Document;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param collection - the collection name.
|
|
32
|
+
* @param oid - the reference ObjectId.
|
|
33
|
+
* @param db - optional db name, if omitted the reference is local to the current db.
|
|
34
|
+
*/
|
|
35
|
+
constructor(collection: string, oid: ObjectId, db?: string, fields?: Document) {
|
|
36
|
+
// check if namespace has been provided
|
|
37
|
+
const parts = collection.split('.');
|
|
38
|
+
if (parts.length === 2) {
|
|
39
|
+
db = parts.shift();
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
41
|
+
collection = parts.shift()!;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
this.collection = collection;
|
|
45
|
+
this.oid = oid;
|
|
46
|
+
this.db = db;
|
|
47
|
+
this.fields = fields || {};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Property provided for compatibility with the 1.x parser
|
|
51
|
+
// the 1.x parser used a "namespace" property, while 4.x uses "collection"
|
|
52
|
+
|
|
53
|
+
/** @internal */
|
|
54
|
+
get namespace(): string {
|
|
55
|
+
return this.collection;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
set namespace(value: string) {
|
|
59
|
+
this.collection = value;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** @internal */
|
|
63
|
+
toJSON(): DBRefLike & Document {
|
|
64
|
+
const o = Object.assign(
|
|
65
|
+
{
|
|
66
|
+
$ref: this.collection,
|
|
67
|
+
$id: this.oid
|
|
68
|
+
},
|
|
69
|
+
this.fields
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
if (this.db != null) o.$db = this.db;
|
|
73
|
+
return o;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** @internal */
|
|
77
|
+
toExtendedJSON(options?: EJSONOptions): DBRefLike {
|
|
78
|
+
options = options || {};
|
|
79
|
+
let o: DBRefLike = {
|
|
80
|
+
$ref: this.collection,
|
|
81
|
+
$id: this.oid
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
if (options.legacy) {
|
|
85
|
+
return o;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (this.db) o.$db = this.db;
|
|
89
|
+
o = Object.assign(o, this.fields);
|
|
90
|
+
return o;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** @internal */
|
|
94
|
+
static fromExtendedJSON(doc: DBRefLike): DBRef {
|
|
95
|
+
const copy = Object.assign({}, doc) as Partial<DBRefLike>;
|
|
96
|
+
delete copy.$ref;
|
|
97
|
+
delete copy.$id;
|
|
98
|
+
delete copy.$db;
|
|
99
|
+
return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @internal */
|
|
103
|
+
[Symbol.for('nodejs.util.inspect.custom')](): string {
|
|
104
|
+
return this.inspect();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
inspect(): string {
|
|
108
|
+
// NOTE: if OID is an ObjectId class it will just print the oid string.
|
|
109
|
+
const oid =
|
|
110
|
+
this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
|
|
111
|
+
return `DBRef("${this.namespace}", "${oid}"${this.db ? `, "${this.db}"` : ''})`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
Object.defineProperty(DBRef.prototype, '_bsontype', { value: 'DBRef' });
|