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.
Files changed (90) hide show
  1. package/HISTORY.md +56 -1
  2. package/README.md +7 -9
  3. package/bower.json +1 -1
  4. package/bson.d.ts +983 -0
  5. package/dist/bson.browser.esm.js +7261 -5004
  6. package/dist/bson.browser.esm.js.map +1 -0
  7. package/dist/bson.browser.umd.js +7319 -5099
  8. package/dist/bson.browser.umd.js.map +1 -0
  9. package/dist/bson.bundle.js +8168 -9216
  10. package/dist/bson.bundle.js.map +1 -0
  11. package/dist/bson.esm.js +5643 -5409
  12. package/dist/bson.esm.js.map +1 -0
  13. package/etc/prepare.js +19 -0
  14. package/lib/binary.js +194 -377
  15. package/lib/binary.js.map +1 -0
  16. package/lib/bson.js +200 -243
  17. package/lib/bson.js.map +1 -0
  18. package/lib/code.js +36 -39
  19. package/lib/code.js.map +1 -0
  20. package/lib/constants.js +78 -203
  21. package/lib/constants.js.map +1 -0
  22. package/lib/db_ref.js +79 -79
  23. package/lib/db_ref.js.map +1 -0
  24. package/lib/decimal128.js +647 -760
  25. package/lib/decimal128.js.map +1 -0
  26. package/lib/double.js +61 -58
  27. package/lib/double.js.map +1 -0
  28. package/lib/ensure_buffer.js +22 -18
  29. package/lib/ensure_buffer.js.map +1 -0
  30. package/lib/extended_json.js +305 -322
  31. package/lib/extended_json.js.map +1 -0
  32. package/lib/float_parser.js +98 -104
  33. package/lib/float_parser.js.map +1 -0
  34. package/lib/int_32.js +45 -47
  35. package/lib/int_32.js.map +1 -0
  36. package/lib/long.js +876 -16
  37. package/lib/long.js.map +1 -0
  38. package/lib/map.js +123 -124
  39. package/lib/map.js.map +1 -0
  40. package/lib/max_key.js +21 -23
  41. package/lib/max_key.js.map +1 -0
  42. package/lib/min_key.js +21 -23
  43. package/lib/min_key.js.map +1 -0
  44. package/lib/objectid.js +264 -382
  45. package/lib/objectid.js.map +1 -0
  46. package/lib/parser/calculate_size.js +185 -224
  47. package/lib/parser/calculate_size.js.map +1 -0
  48. package/lib/parser/deserializer.js +543 -620
  49. package/lib/parser/deserializer.js.map +1 -0
  50. package/lib/parser/serializer.js +774 -918
  51. package/lib/parser/serializer.js.map +1 -0
  52. package/lib/parser/utils.js +81 -30
  53. package/lib/parser/utils.js.map +1 -0
  54. package/lib/regexp.js +54 -70
  55. package/lib/regexp.js.map +1 -0
  56. package/lib/symbol.js +40 -56
  57. package/lib/symbol.js.map +1 -0
  58. package/lib/timestamp.js +70 -95
  59. package/lib/timestamp.js.map +1 -0
  60. package/lib/uuid.js +48 -0
  61. package/lib/uuid.js.map +1 -0
  62. package/lib/validate_utf8.js +32 -33
  63. package/lib/validate_utf8.js.map +1 -0
  64. package/package.json +53 -31
  65. package/src/binary.ts +270 -0
  66. package/src/bson.ts +326 -0
  67. package/src/code.ts +57 -0
  68. package/src/constants.ts +104 -0
  69. package/src/db_ref.ts +115 -0
  70. package/src/decimal128.ts +801 -0
  71. package/src/double.ts +85 -0
  72. package/src/ensure_buffer.ts +26 -0
  73. package/src/extended_json.ts +395 -0
  74. package/src/float_parser.ts +152 -0
  75. package/src/int_32.ts +64 -0
  76. package/src/long.ts +1000 -0
  77. package/src/map.ts +139 -0
  78. package/src/max_key.ts +33 -0
  79. package/src/min_key.ts +33 -0
  80. package/src/objectid.ts +377 -0
  81. package/src/parser/calculate_size.ts +230 -0
  82. package/src/parser/deserializer.ts +655 -0
  83. package/src/parser/serializer.ts +1069 -0
  84. package/src/parser/utils.ts +93 -0
  85. package/src/regexp.ts +92 -0
  86. package/src/symbol.ts +57 -0
  87. package/src/timestamp.ts +103 -0
  88. package/src/uuid.ts +57 -0
  89. package/src/validate_utf8.ts +47 -0
  90. package/lib/fnv1a.js +0 -48
package/src/long.ts ADDED
@@ -0,0 +1,1000 @@
1
+ import type { EJSONOptions } from './extended_json';
2
+ import { isObjectLike } from './parser/utils';
3
+ import type { Timestamp } from './timestamp';
4
+
5
+ interface LongWASMHelpers {
6
+ /** Gets the high bits of the last operation performed */
7
+ get_high(): number;
8
+ div_u(lowBits: number, highBits: number, lowBitsDivisor: number, highBitsDivisor: number): number;
9
+ div_s(lowBits: number, highBits: number, lowBitsDivisor: number, highBitsDivisor: number): number;
10
+ rem_u(lowBits: number, highBits: number, lowBitsDivisor: number, highBitsDivisor: number): number;
11
+ rem_s(lowBits: number, highBits: number, lowBitsDivisor: number, highBitsDivisor: number): number;
12
+ mul(
13
+ lowBits: number,
14
+ highBits: number,
15
+ lowBitsMultiplier: number,
16
+ highBitsMultiplier: number
17
+ ): number;
18
+ }
19
+
20
+ /**
21
+ * wasm optimizations, to do native i64 multiplication and divide
22
+ */
23
+ let wasm: LongWASMHelpers | undefined = undefined;
24
+
25
+ /* We do not want to have to include DOM types just for this check */
26
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
+ declare const WebAssembly: any;
28
+
29
+ try {
30
+ wasm = (new WebAssembly.Instance(
31
+ new WebAssembly.Module(
32
+ // prettier-ignore
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
+ ),
35
+ {}
36
+ ).exports as unknown) as LongWASMHelpers;
37
+ } catch {
38
+ // no wasm support
39
+ }
40
+
41
+ const TWO_PWR_16_DBL = 1 << 16;
42
+ const TWO_PWR_24_DBL = 1 << 24;
43
+ const TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
44
+ const TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
45
+ const TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
46
+
47
+ /** A cache of the Long representations of small integer values. */
48
+ const INT_CACHE: { [key: number]: Long } = {};
49
+
50
+ /** A cache of the Long representations of small unsigned integer values. */
51
+ const UINT_CACHE: { [key: number]: Long } = {};
52
+
53
+ /** @public */
54
+ export interface LongExtended {
55
+ $numberLong: string;
56
+ }
57
+
58
+ /**
59
+ * A class representing a 64-bit integer
60
+ * @public
61
+ * @remarks
62
+ * The internal representation of a long is the two given signed, 32-bit values.
63
+ * We use 32-bit pieces because these are the size of integers on which
64
+ * Javascript performs bit-operations. For operations like addition and
65
+ * multiplication, we split each number into 16 bit pieces, which can easily be
66
+ * multiplied within Javascript's floating-point representation without overflow
67
+ * or change in sign.
68
+ * In the algorithms below, we frequently reduce the negative case to the
69
+ * positive case by negating the input(s) and then post-processing the result.
70
+ * Note that we must ALWAYS check specially whether those values are MIN_VALUE
71
+ * (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
72
+ * a positive number, it overflows back into a negative). Not handling this
73
+ * case would often result in infinite recursion.
74
+ * Common constant values ZERO, ONE, NEG_ONE, etc. are found as static properties on this class.
75
+ */
76
+ export class Long {
77
+ _bsontype!: 'Long';
78
+
79
+ /** An indicator used to reliably determine if an object is a Long or not. */
80
+ __isLong__!: true;
81
+
82
+ /**
83
+ * The high 32 bits as a signed value.
84
+ */
85
+ high: number;
86
+
87
+ /**
88
+ * The low 32 bits as a signed value.
89
+ */
90
+ low: number;
91
+
92
+ /**
93
+ * Whether unsigned or not.
94
+ */
95
+ unsigned: boolean;
96
+
97
+ /**
98
+ * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
99
+ * See the from* functions below for more convenient ways of constructing Longs.
100
+ * @param low - The low (signed) 32 bits of the long
101
+ * @param high - The high (signed) 32 bits of the long
102
+ * @param unsigned - Whether unsigned or not, defaults to signed
103
+ */
104
+ constructor(low = 0, high = 0, unsigned?: boolean) {
105
+ this.low = low | 0;
106
+ this.high = high | 0;
107
+ this.unsigned = !!unsigned;
108
+
109
+ Object.defineProperty(this, '__isLong__', {
110
+ value: true,
111
+ configurable: false,
112
+ writable: false,
113
+ enumerable: false
114
+ });
115
+ }
116
+
117
+ static TWO_PWR_24 = Long.fromInt(TWO_PWR_24_DBL);
118
+
119
+ /** Maximum unsigned value. */
120
+ static MAX_UNSIGNED_VALUE = Long.fromBits(0xffffffff | 0, 0xffffffff | 0, true);
121
+ /** Signed zero */
122
+ static ZERO = Long.fromInt(0);
123
+ /** Unsigned zero. */
124
+ static UZERO = Long.fromInt(0, true);
125
+ /** Signed one. */
126
+ static ONE = Long.fromInt(1);
127
+ /** Unsigned one. */
128
+ static UONE = Long.fromInt(1, true);
129
+ /** Signed negative one. */
130
+ static NEG_ONE = Long.fromInt(-1);
131
+ /** Maximum signed value. */
132
+ static MAX_VALUE = Long.fromBits(0xffffffff | 0, 0x7fffffff | 0, false);
133
+ /** Minimum signed value. */
134
+ static MIN_VALUE = Long.fromBits(0, 0x80000000 | 0, false);
135
+
136
+ /**
137
+ * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits.
138
+ * Each is assumed to use 32 bits.
139
+ * @param lowBits - The low 32 bits
140
+ * @param highBits - The high 32 bits
141
+ * @param unsigned - Whether unsigned or not, defaults to signed
142
+ * @returns The corresponding Long value
143
+ */
144
+ static fromBits(lowBits: number, highBits: number, unsigned?: boolean): Long {
145
+ return new Long(lowBits, highBits, unsigned);
146
+ }
147
+
148
+ /**
149
+ * Returns a Long representing the given 32 bit integer value.
150
+ * @param value - The 32 bit integer in question
151
+ * @param unsigned - Whether unsigned or not, defaults to signed
152
+ * @returns The corresponding Long value
153
+ */
154
+ static fromInt(value: number, unsigned?: boolean): Long {
155
+ let obj, cachedObj, cache;
156
+ if (unsigned) {
157
+ value >>>= 0;
158
+ if ((cache = 0 <= value && value < 256)) {
159
+ cachedObj = UINT_CACHE[value];
160
+ if (cachedObj) return cachedObj;
161
+ }
162
+ obj = Long.fromBits(value, (value | 0) < 0 ? -1 : 0, true);
163
+ if (cache) UINT_CACHE[value] = obj;
164
+ return obj;
165
+ } else {
166
+ value |= 0;
167
+ if ((cache = -128 <= value && value < 128)) {
168
+ cachedObj = INT_CACHE[value];
169
+ if (cachedObj) return cachedObj;
170
+ }
171
+ obj = Long.fromBits(value, value < 0 ? -1 : 0, false);
172
+ if (cache) INT_CACHE[value] = obj;
173
+ return obj;
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
179
+ * @param value - The number in question
180
+ * @param unsigned - Whether unsigned or not, defaults to signed
181
+ * @returns The corresponding Long value
182
+ */
183
+ static fromNumber(value: number, unsigned?: boolean): Long {
184
+ if (isNaN(value)) return unsigned ? Long.UZERO : Long.ZERO;
185
+ if (unsigned) {
186
+ if (value < 0) return Long.UZERO;
187
+ if (value >= TWO_PWR_64_DBL) return Long.MAX_UNSIGNED_VALUE;
188
+ } else {
189
+ if (value <= -TWO_PWR_63_DBL) return Long.MIN_VALUE;
190
+ if (value + 1 >= TWO_PWR_63_DBL) return Long.MAX_VALUE;
191
+ }
192
+ if (value < 0) return Long.fromNumber(-value, unsigned).neg();
193
+ return Long.fromBits(value % TWO_PWR_32_DBL | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
194
+ }
195
+
196
+ /**
197
+ * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
198
+ * @param value - The number in question
199
+ * @param unsigned - Whether unsigned or not, defaults to signed
200
+ * @returns The corresponding Long value
201
+ */
202
+ static fromBigInt(value: bigint, unsigned?: boolean): Long {
203
+ return Long.fromString(value.toString(), unsigned);
204
+ }
205
+
206
+ /**
207
+ * Returns a Long representation of the given string, written using the specified radix.
208
+ * @param str - The textual representation of the Long
209
+ * @param unsigned - Whether unsigned or not, defaults to signed
210
+ * @param radix - The radix in which the text is written (2-36), defaults to 10
211
+ * @returns The corresponding Long value
212
+ */
213
+ static fromString(str: string, unsigned?: boolean, radix?: number): Long {
214
+ if (str.length === 0) throw Error('empty string');
215
+ if (str === 'NaN' || str === 'Infinity' || str === '+Infinity' || str === '-Infinity')
216
+ return Long.ZERO;
217
+ if (typeof unsigned === 'number') {
218
+ // For goog.math.long compatibility
219
+ (radix = unsigned), (unsigned = false);
220
+ } else {
221
+ unsigned = !!unsigned;
222
+ }
223
+ radix = radix || 10;
224
+ if (radix < 2 || 36 < radix) throw RangeError('radix');
225
+
226
+ let p;
227
+ if ((p = str.indexOf('-')) > 0) throw Error('interior hyphen');
228
+ else if (p === 0) {
229
+ return Long.fromString(str.substring(1), unsigned, radix).neg();
230
+ }
231
+
232
+ // Do several (8) digits each time through the loop, so as to
233
+ // minimize the calls to the very expensive emulated div.
234
+ const radixToPower = Long.fromNumber(Math.pow(radix, 8));
235
+
236
+ let result = Long.ZERO;
237
+ for (let i = 0; i < str.length; i += 8) {
238
+ const size = Math.min(8, str.length - i),
239
+ value = parseInt(str.substring(i, i + size), radix);
240
+ if (size < 8) {
241
+ const power = Long.fromNumber(Math.pow(radix, size));
242
+ result = result.mul(power).add(Long.fromNumber(value));
243
+ } else {
244
+ result = result.mul(radixToPower);
245
+ result = result.add(Long.fromNumber(value));
246
+ }
247
+ }
248
+ result.unsigned = unsigned;
249
+ return result;
250
+ }
251
+
252
+ /**
253
+ * Creates a Long from its byte representation.
254
+ * @param bytes - Byte representation
255
+ * @param unsigned - Whether unsigned or not, defaults to signed
256
+ * @param le - Whether little or big endian, defaults to big endian
257
+ * @returns The corresponding Long value
258
+ */
259
+ static fromBytes(bytes: number[], unsigned?: boolean, le?: boolean): Long {
260
+ return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned);
261
+ }
262
+
263
+ /**
264
+ * Creates a Long from its little endian byte representation.
265
+ * @param bytes - Little endian byte representation
266
+ * @param unsigned - Whether unsigned or not, defaults to signed
267
+ * @returns The corresponding Long value
268
+ */
269
+ static fromBytesLE(bytes: number[], unsigned?: boolean): Long {
270
+ return new Long(
271
+ bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24),
272
+ bytes[4] | (bytes[5] << 8) | (bytes[6] << 16) | (bytes[7] << 24),
273
+ unsigned
274
+ );
275
+ }
276
+
277
+ /**
278
+ * Creates a Long from its big endian byte representation.
279
+ * @param bytes - Big endian byte representation
280
+ * @param unsigned - Whether unsigned or not, defaults to signed
281
+ * @returns The corresponding Long value
282
+ */
283
+ static fromBytesBE(bytes: number[], unsigned?: boolean): Long {
284
+ return new Long(
285
+ (bytes[4] << 24) | (bytes[5] << 16) | (bytes[6] << 8) | bytes[7],
286
+ (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3],
287
+ unsigned
288
+ );
289
+ }
290
+
291
+ /**
292
+ * Tests if the specified object is a Long.
293
+ */
294
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
295
+ static isLong(value: any): value is Long {
296
+ return isObjectLike(value) && value['__isLong__'] === true;
297
+ }
298
+
299
+ /**
300
+ * Converts the specified value to a Long.
301
+ * @param unsigned - Whether unsigned or not, defaults to signed
302
+ */
303
+ static fromValue(
304
+ val: number | string | { low: number; high: number; unsigned?: boolean },
305
+ unsigned?: boolean
306
+ ): Long {
307
+ if (typeof val === 'number') return Long.fromNumber(val, unsigned);
308
+ if (typeof val === 'string') return Long.fromString(val, unsigned);
309
+ // Throws for non-objects, converts non-instanceof Long:
310
+ return Long.fromBits(
311
+ val.low,
312
+ val.high,
313
+ typeof unsigned === 'boolean' ? unsigned : val.unsigned
314
+ );
315
+ }
316
+
317
+ /** Returns the sum of this and the specified Long. */
318
+ add(addend: string | number | Long | Timestamp): Long {
319
+ if (!Long.isLong(addend)) addend = Long.fromValue(addend);
320
+
321
+ // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
322
+
323
+ const a48 = this.high >>> 16;
324
+ const a32 = this.high & 0xffff;
325
+ const a16 = this.low >>> 16;
326
+ const a00 = this.low & 0xffff;
327
+
328
+ const b48 = addend.high >>> 16;
329
+ const b32 = addend.high & 0xffff;
330
+ const b16 = addend.low >>> 16;
331
+ const b00 = addend.low & 0xffff;
332
+
333
+ let c48 = 0,
334
+ c32 = 0,
335
+ c16 = 0,
336
+ c00 = 0;
337
+ c00 += a00 + b00;
338
+ c16 += c00 >>> 16;
339
+ c00 &= 0xffff;
340
+ c16 += a16 + b16;
341
+ c32 += c16 >>> 16;
342
+ c16 &= 0xffff;
343
+ c32 += a32 + b32;
344
+ c48 += c32 >>> 16;
345
+ c32 &= 0xffff;
346
+ c48 += a48 + b48;
347
+ c48 &= 0xffff;
348
+ return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
349
+ }
350
+
351
+ /**
352
+ * Returns the sum of this and the specified Long.
353
+ * @returns Sum
354
+ */
355
+ and(other: string | number | Long | Timestamp): Long {
356
+ if (!Long.isLong(other)) other = Long.fromValue(other);
357
+ return Long.fromBits(this.low & other.low, this.high & other.high, this.unsigned);
358
+ }
359
+
360
+ /**
361
+ * Compares this Long's value with the specified's.
362
+ * @returns 0 if they are the same, 1 if the this is greater and -1 if the given one is greater
363
+ */
364
+ compare(other: string | number | Long | Timestamp): 0 | 1 | -1 {
365
+ if (!Long.isLong(other)) other = Long.fromValue(other);
366
+ if (this.eq(other)) return 0;
367
+ const thisNeg = this.isNegative(),
368
+ otherNeg = other.isNegative();
369
+ if (thisNeg && !otherNeg) return -1;
370
+ if (!thisNeg && otherNeg) return 1;
371
+ // At this point the sign bits are the same
372
+ if (!this.unsigned) return this.sub(other).isNegative() ? -1 : 1;
373
+ // Both are positive if at least one is unsigned
374
+ return other.high >>> 0 > this.high >>> 0 ||
375
+ (other.high === this.high && other.low >>> 0 > this.low >>> 0)
376
+ ? -1
377
+ : 1;
378
+ }
379
+
380
+ /** This is an alias of {@link Long.compare} */
381
+ comp(other: string | number | Long | Timestamp): 0 | 1 | -1 {
382
+ return this.compare(other);
383
+ }
384
+
385
+ /**
386
+ * Returns this Long divided by the specified. The result is signed if this Long is signed or unsigned if this Long is unsigned.
387
+ * @returns Quotient
388
+ */
389
+ divide(divisor: string | number | Long | Timestamp): Long {
390
+ if (!Long.isLong(divisor)) divisor = Long.fromValue(divisor);
391
+ if (divisor.isZero()) throw Error('division by zero');
392
+
393
+ // use wasm support if present
394
+ if (wasm) {
395
+ // guard against signed division overflow: the largest
396
+ // negative number / -1 would be 1 larger than the largest
397
+ // positive number, due to two's complement.
398
+ if (
399
+ !this.unsigned &&
400
+ this.high === -0x80000000 &&
401
+ divisor.low === -1 &&
402
+ divisor.high === -1
403
+ ) {
404
+ // be consistent with non-wasm code path
405
+ return this;
406
+ }
407
+ const low = (this.unsigned ? wasm.div_u : wasm.div_s)(
408
+ this.low,
409
+ this.high,
410
+ divisor.low,
411
+ divisor.high
412
+ );
413
+ return Long.fromBits(low, wasm.get_high(), this.unsigned);
414
+ }
415
+
416
+ if (this.isZero()) return this.unsigned ? Long.UZERO : Long.ZERO;
417
+ let approx, rem, res;
418
+ if (!this.unsigned) {
419
+ // This section is only relevant for signed longs and is derived from the
420
+ // closure library as a whole.
421
+ if (this.eq(Long.MIN_VALUE)) {
422
+ if (divisor.eq(Long.ONE) || divisor.eq(Long.NEG_ONE)) return Long.MIN_VALUE;
423
+ // recall that -MIN_VALUE == MIN_VALUE
424
+ else if (divisor.eq(Long.MIN_VALUE)) return Long.ONE;
425
+ else {
426
+ // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
427
+ const halfThis = this.shr(1);
428
+ approx = halfThis.div(divisor).shl(1);
429
+ if (approx.eq(Long.ZERO)) {
430
+ return divisor.isNegative() ? Long.ONE : Long.NEG_ONE;
431
+ } else {
432
+ rem = this.sub(divisor.mul(approx));
433
+ res = approx.add(rem.div(divisor));
434
+ return res;
435
+ }
436
+ }
437
+ } else if (divisor.eq(Long.MIN_VALUE)) return this.unsigned ? Long.UZERO : Long.ZERO;
438
+ if (this.isNegative()) {
439
+ if (divisor.isNegative()) return this.neg().div(divisor.neg());
440
+ return this.neg().div(divisor).neg();
441
+ } else if (divisor.isNegative()) return this.div(divisor.neg()).neg();
442
+ res = Long.ZERO;
443
+ } else {
444
+ // The algorithm below has not been made for unsigned longs. It's therefore
445
+ // required to take special care of the MSB prior to running it.
446
+ if (!divisor.unsigned) divisor = divisor.toUnsigned();
447
+ if (divisor.gt(this)) return Long.UZERO;
448
+ if (divisor.gt(this.shru(1)))
449
+ // 15 >>> 1 = 7 ; with divisor = 8 ; true
450
+ return Long.UONE;
451
+ res = Long.UZERO;
452
+ }
453
+
454
+ // Repeat the following until the remainder is less than other: find a
455
+ // floating-point that approximates remainder / other *from below*, add this
456
+ // into the result, and subtract it from the remainder. It is critical that
457
+ // the approximate value is less than or equal to the real value so that the
458
+ // remainder never becomes negative.
459
+ rem = this;
460
+ while (rem.gte(divisor)) {
461
+ // Approximate the result of division. This may be a little greater or
462
+ // smaller than the actual value.
463
+ approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber()));
464
+
465
+ // We will tweak the approximate result by changing it in the 48-th digit or
466
+ // the smallest non-fractional digit, whichever is larger.
467
+ const log2 = Math.ceil(Math.log(approx) / Math.LN2);
468
+ const delta = log2 <= 48 ? 1 : Math.pow(2, log2 - 48);
469
+ // Decrease the approximation until it is smaller than the remainder. Note
470
+ // that if it is too large, the product overflows and is negative.
471
+ let approxRes = Long.fromNumber(approx);
472
+ let approxRem = approxRes.mul(divisor);
473
+ while (approxRem.isNegative() || approxRem.gt(rem)) {
474
+ approx -= delta;
475
+ approxRes = Long.fromNumber(approx, this.unsigned);
476
+ approxRem = approxRes.mul(divisor);
477
+ }
478
+
479
+ // We know the answer can't be zero... and actually, zero would cause
480
+ // infinite recursion since we would make no progress.
481
+ if (approxRes.isZero()) approxRes = Long.ONE;
482
+
483
+ res = res.add(approxRes);
484
+ rem = rem.sub(approxRem);
485
+ }
486
+ return res;
487
+ }
488
+
489
+ /**This is an alias of {@link Long.divide} */
490
+ div(divisor: string | number | Long | Timestamp): Long {
491
+ return this.divide(divisor);
492
+ }
493
+
494
+ /**
495
+ * Tests if this Long's value equals the specified's.
496
+ * @param other - Other value
497
+ */
498
+ equals(other: string | number | Long | Timestamp): boolean {
499
+ if (!Long.isLong(other)) other = Long.fromValue(other);
500
+ if (this.unsigned !== other.unsigned && this.high >>> 31 === 1 && other.high >>> 31 === 1)
501
+ return false;
502
+ return this.high === other.high && this.low === other.low;
503
+ }
504
+
505
+ /** This is an alias of {@link Long.equals} */
506
+ eq(other: string | number | Long | Timestamp): boolean {
507
+ return this.equals(other);
508
+ }
509
+
510
+ /** Gets the high 32 bits as a signed integer. */
511
+ getHighBits(): number {
512
+ return this.high;
513
+ }
514
+
515
+ /** Gets the high 32 bits as an unsigned integer. */
516
+ getHighBitsUnsigned(): number {
517
+ return this.high >>> 0;
518
+ }
519
+
520
+ /** Gets the low 32 bits as a signed integer. */
521
+ getLowBits(): number {
522
+ return this.low;
523
+ }
524
+
525
+ /** Gets the low 32 bits as an unsigned integer. */
526
+ getLowBitsUnsigned(): number {
527
+ return this.low >>> 0;
528
+ }
529
+
530
+ /** Gets the number of bits needed to represent the absolute value of this Long. */
531
+ getNumBitsAbs(): number {
532
+ if (this.isNegative()) {
533
+ // Unsigned Longs are never negative
534
+ return this.eq(Long.MIN_VALUE) ? 64 : this.neg().getNumBitsAbs();
535
+ }
536
+ const val = this.high !== 0 ? this.high : this.low;
537
+ let bit: number;
538
+ for (bit = 31; bit > 0; bit--) if ((val & (1 << bit)) !== 0) break;
539
+ return this.high !== 0 ? bit + 33 : bit + 1;
540
+ }
541
+
542
+ /** Tests if this Long's value is greater than the specified's. */
543
+ greaterThan(other: string | number | Long | Timestamp): boolean {
544
+ return this.comp(other) > 0;
545
+ }
546
+
547
+ /** This is an alias of {@link Long.greaterThan} */
548
+ gt(other: string | number | Long | Timestamp): boolean {
549
+ return this.greaterThan(other);
550
+ }
551
+
552
+ /** Tests if this Long's value is greater than or equal the specified's. */
553
+ greaterThanOrEqual(other: string | number | Long | Timestamp): boolean {
554
+ return this.comp(other) >= 0;
555
+ }
556
+
557
+ /** This is an alias of {@link Long.greaterThanOrEqual} */
558
+ gte(other: string | number | Long | Timestamp): boolean {
559
+ return this.greaterThanOrEqual(other);
560
+ }
561
+ /** This is an alias of {@link Long.greaterThanOrEqual} */
562
+ ge(other: string | number | Long | Timestamp): boolean {
563
+ return this.greaterThanOrEqual(other);
564
+ }
565
+
566
+ /** Tests if this Long's value is even. */
567
+ isEven(): boolean {
568
+ return (this.low & 1) === 0;
569
+ }
570
+
571
+ /** Tests if this Long's value is negative. */
572
+ isNegative(): boolean {
573
+ return !this.unsigned && this.high < 0;
574
+ }
575
+
576
+ /** Tests if this Long's value is odd. */
577
+ isOdd(): boolean {
578
+ return (this.low & 1) === 1;
579
+ }
580
+
581
+ /** Tests if this Long's value is positive. */
582
+ isPositive(): boolean {
583
+ return this.unsigned || this.high >= 0;
584
+ }
585
+
586
+ /** Tests if this Long's value equals zero. */
587
+ isZero(): boolean {
588
+ return this.high === 0 && this.low === 0;
589
+ }
590
+
591
+ /** Tests if this Long's value is less than the specified's. */
592
+ lessThan(other: string | number | Long | Timestamp): boolean {
593
+ return this.comp(other) < 0;
594
+ }
595
+
596
+ /** This is an alias of {@link Long#lessThan}. */
597
+ lt(other: string | number | Long | Timestamp): boolean {
598
+ return this.lessThan(other);
599
+ }
600
+
601
+ /** Tests if this Long's value is less than or equal the specified's. */
602
+ lessThanOrEqual(other: string | number | Long | Timestamp): boolean {
603
+ return this.comp(other) <= 0;
604
+ }
605
+
606
+ /** This is an alias of {@link Long.lessThanOrEqual} */
607
+ lte(other: string | number | Long | Timestamp): boolean {
608
+ return this.lessThanOrEqual(other);
609
+ }
610
+
611
+ /** Returns this Long modulo the specified. */
612
+ modulo(divisor: string | number | Long | Timestamp): Long {
613
+ if (!Long.isLong(divisor)) divisor = Long.fromValue(divisor);
614
+
615
+ // use wasm support if present
616
+ if (wasm) {
617
+ const low = (this.unsigned ? wasm.rem_u : wasm.rem_s)(
618
+ this.low,
619
+ this.high,
620
+ divisor.low,
621
+ divisor.high
622
+ );
623
+ return Long.fromBits(low, wasm.get_high(), this.unsigned);
624
+ }
625
+
626
+ return this.sub(this.div(divisor).mul(divisor));
627
+ }
628
+
629
+ /** This is an alias of {@link Long.modulo} */
630
+ mod(divisor: string | number | Long | Timestamp): Long {
631
+ return this.modulo(divisor);
632
+ }
633
+ /** This is an alias of {@link Long.modulo} */
634
+ rem(divisor: string | number | Long | Timestamp): Long {
635
+ return this.modulo(divisor);
636
+ }
637
+
638
+ /**
639
+ * Returns the product of this and the specified Long.
640
+ * @param multiplier - Multiplier
641
+ * @returns Product
642
+ */
643
+ multiply(multiplier: string | number | Long | Timestamp): Long {
644
+ if (this.isZero()) return Long.ZERO;
645
+ if (!Long.isLong(multiplier)) multiplier = Long.fromValue(multiplier);
646
+
647
+ // use wasm support if present
648
+ if (wasm) {
649
+ const low = wasm.mul(this.low, this.high, multiplier.low, multiplier.high);
650
+ return Long.fromBits(low, wasm.get_high(), this.unsigned);
651
+ }
652
+
653
+ if (multiplier.isZero()) return Long.ZERO;
654
+ if (this.eq(Long.MIN_VALUE)) return multiplier.isOdd() ? Long.MIN_VALUE : Long.ZERO;
655
+ if (multiplier.eq(Long.MIN_VALUE)) return this.isOdd() ? Long.MIN_VALUE : Long.ZERO;
656
+
657
+ if (this.isNegative()) {
658
+ if (multiplier.isNegative()) return this.neg().mul(multiplier.neg());
659
+ else return this.neg().mul(multiplier).neg();
660
+ } else if (multiplier.isNegative()) return this.mul(multiplier.neg()).neg();
661
+
662
+ // If both longs are small, use float multiplication
663
+ if (this.lt(Long.TWO_PWR_24) && multiplier.lt(Long.TWO_PWR_24))
664
+ return Long.fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned);
665
+
666
+ // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
667
+ // We can skip products that would overflow.
668
+
669
+ const a48 = this.high >>> 16;
670
+ const a32 = this.high & 0xffff;
671
+ const a16 = this.low >>> 16;
672
+ const a00 = this.low & 0xffff;
673
+
674
+ const b48 = multiplier.high >>> 16;
675
+ const b32 = multiplier.high & 0xffff;
676
+ const b16 = multiplier.low >>> 16;
677
+ const b00 = multiplier.low & 0xffff;
678
+
679
+ let c48 = 0,
680
+ c32 = 0,
681
+ c16 = 0,
682
+ c00 = 0;
683
+ c00 += a00 * b00;
684
+ c16 += c00 >>> 16;
685
+ c00 &= 0xffff;
686
+ c16 += a16 * b00;
687
+ c32 += c16 >>> 16;
688
+ c16 &= 0xffff;
689
+ c16 += a00 * b16;
690
+ c32 += c16 >>> 16;
691
+ c16 &= 0xffff;
692
+ c32 += a32 * b00;
693
+ c48 += c32 >>> 16;
694
+ c32 &= 0xffff;
695
+ c32 += a16 * b16;
696
+ c48 += c32 >>> 16;
697
+ c32 &= 0xffff;
698
+ c32 += a00 * b32;
699
+ c48 += c32 >>> 16;
700
+ c32 &= 0xffff;
701
+ c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
702
+ c48 &= 0xffff;
703
+ return Long.fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
704
+ }
705
+
706
+ /** This is an alias of {@link Long.multiply} */
707
+ mul(multiplier: string | number | Long | Timestamp): Long {
708
+ return this.multiply(multiplier);
709
+ }
710
+
711
+ /** Returns the Negation of this Long's value. */
712
+ negate(): Long {
713
+ if (!this.unsigned && this.eq(Long.MIN_VALUE)) return Long.MIN_VALUE;
714
+ return this.not().add(Long.ONE);
715
+ }
716
+
717
+ /** This is an alias of {@link Long.negate} */
718
+ neg(): Long {
719
+ return this.negate();
720
+ }
721
+
722
+ /** Returns the bitwise NOT of this Long. */
723
+ not(): Long {
724
+ return Long.fromBits(~this.low, ~this.high, this.unsigned);
725
+ }
726
+
727
+ /** Tests if this Long's value differs from the specified's. */
728
+ notEquals(other: string | number | Long | Timestamp): boolean {
729
+ return !this.equals(other);
730
+ }
731
+
732
+ /** This is an alias of {@link Long.notEquals} */
733
+ neq(other: string | number | Long | Timestamp): boolean {
734
+ return this.notEquals(other);
735
+ }
736
+ /** This is an alias of {@link Long.notEquals} */
737
+ ne(other: string | number | Long | Timestamp): boolean {
738
+ return this.notEquals(other);
739
+ }
740
+
741
+ /**
742
+ * Returns the bitwise OR of this Long and the specified.
743
+ */
744
+ or(other: number | string | Long): Long {
745
+ if (!Long.isLong(other)) other = Long.fromValue(other);
746
+ return Long.fromBits(this.low | other.low, this.high | other.high, this.unsigned);
747
+ }
748
+
749
+ /**
750
+ * Returns this Long with bits shifted to the left by the given amount.
751
+ * @param numBits - Number of bits
752
+ * @returns Shifted Long
753
+ */
754
+ shiftLeft(numBits: number | Long): Long {
755
+ if (Long.isLong(numBits)) numBits = numBits.toInt();
756
+ if ((numBits &= 63) === 0) return this;
757
+ else if (numBits < 32)
758
+ return Long.fromBits(
759
+ this.low << numBits,
760
+ (this.high << numBits) | (this.low >>> (32 - numBits)),
761
+ this.unsigned
762
+ );
763
+ else return Long.fromBits(0, this.low << (numBits - 32), this.unsigned);
764
+ }
765
+
766
+ /** This is an alias of {@link Long.shiftLeft} */
767
+ shl(numBits: number | Long): Long {
768
+ return this.shiftLeft(numBits);
769
+ }
770
+
771
+ /**
772
+ * Returns this Long with bits arithmetically shifted to the right by the given amount.
773
+ * @param numBits - Number of bits
774
+ * @returns Shifted Long
775
+ */
776
+ shiftRight(numBits: number | Long): Long {
777
+ if (Long.isLong(numBits)) numBits = numBits.toInt();
778
+ if ((numBits &= 63) === 0) return this;
779
+ else if (numBits < 32)
780
+ return Long.fromBits(
781
+ (this.low >>> numBits) | (this.high << (32 - numBits)),
782
+ this.high >> numBits,
783
+ this.unsigned
784
+ );
785
+ else return Long.fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned);
786
+ }
787
+
788
+ /** This is an alias of {@link Long.shiftRight} */
789
+ shr(numBits: number | Long): Long {
790
+ return this.shiftRight(numBits);
791
+ }
792
+
793
+ /**
794
+ * Returns this Long with bits logically shifted to the right by the given amount.
795
+ * @param numBits - Number of bits
796
+ * @returns Shifted Long
797
+ */
798
+ shiftRightUnsigned(numBits: Long | number): Long {
799
+ if (Long.isLong(numBits)) numBits = numBits.toInt();
800
+ numBits &= 63;
801
+ if (numBits === 0) return this;
802
+ else {
803
+ const high = this.high;
804
+ if (numBits < 32) {
805
+ const low = this.low;
806
+ return Long.fromBits(
807
+ (low >>> numBits) | (high << (32 - numBits)),
808
+ high >>> numBits,
809
+ this.unsigned
810
+ );
811
+ } else if (numBits === 32) return Long.fromBits(high, 0, this.unsigned);
812
+ else return Long.fromBits(high >>> (numBits - 32), 0, this.unsigned);
813
+ }
814
+ }
815
+
816
+ /** This is an alias of {@link Long.shiftRightUnsigned} */
817
+ shr_u(numBits: number | Long): Long {
818
+ return this.shiftRightUnsigned(numBits);
819
+ }
820
+ /** This is an alias of {@link Long.shiftRightUnsigned} */
821
+ shru(numBits: number | Long): Long {
822
+ return this.shiftRightUnsigned(numBits);
823
+ }
824
+
825
+ /**
826
+ * Returns the difference of this and the specified Long.
827
+ * @param subtrahend - Subtrahend
828
+ * @returns Difference
829
+ */
830
+ subtract(subtrahend: string | number | Long | Timestamp): Long {
831
+ if (!Long.isLong(subtrahend)) subtrahend = Long.fromValue(subtrahend);
832
+ return this.add(subtrahend.neg());
833
+ }
834
+
835
+ /** This is an alias of {@link Long.subtract} */
836
+ sub(subtrahend: string | number | Long | Timestamp): Long {
837
+ return this.subtract(subtrahend);
838
+ }
839
+
840
+ /** Converts the Long to a 32 bit integer, assuming it is a 32 bit integer. */
841
+ toInt(): number {
842
+ return this.unsigned ? this.low >>> 0 : this.low;
843
+ }
844
+
845
+ /** Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa). */
846
+ toNumber(): number {
847
+ if (this.unsigned) return (this.high >>> 0) * TWO_PWR_32_DBL + (this.low >>> 0);
848
+ return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
849
+ }
850
+
851
+ /** Converts the Long to a BigInt (arbitrary precision). */
852
+ toBigInt(): bigint {
853
+ return BigInt(this.toString());
854
+ }
855
+
856
+ /**
857
+ * Converts this Long to its byte representation.
858
+ * @param le - Whether little or big endian, defaults to big endian
859
+ * @returns Byte representation
860
+ */
861
+ toBytes(le?: boolean): number[] {
862
+ return le ? this.toBytesLE() : this.toBytesBE();
863
+ }
864
+
865
+ /**
866
+ * Converts this Long to its little endian byte representation.
867
+ * @returns Little endian byte representation
868
+ */
869
+ toBytesLE(): number[] {
870
+ const hi = this.high,
871
+ lo = this.low;
872
+ return [
873
+ lo & 0xff,
874
+ (lo >>> 8) & 0xff,
875
+ (lo >>> 16) & 0xff,
876
+ lo >>> 24,
877
+ hi & 0xff,
878
+ (hi >>> 8) & 0xff,
879
+ (hi >>> 16) & 0xff,
880
+ hi >>> 24
881
+ ];
882
+ }
883
+
884
+ /**
885
+ * Converts this Long to its big endian byte representation.
886
+ * @returns Big endian byte representation
887
+ */
888
+ toBytesBE(): number[] {
889
+ const hi = this.high,
890
+ lo = this.low;
891
+ return [
892
+ hi >>> 24,
893
+ (hi >>> 16) & 0xff,
894
+ (hi >>> 8) & 0xff,
895
+ hi & 0xff,
896
+ lo >>> 24,
897
+ (lo >>> 16) & 0xff,
898
+ (lo >>> 8) & 0xff,
899
+ lo & 0xff
900
+ ];
901
+ }
902
+
903
+ /**
904
+ * Converts this Long to signed.
905
+ */
906
+ toSigned(): Long {
907
+ if (!this.unsigned) return this;
908
+ return Long.fromBits(this.low, this.high, false);
909
+ }
910
+
911
+ /**
912
+ * Converts the Long to a string written in the specified radix.
913
+ * @param radix - Radix (2-36), defaults to 10
914
+ * @throws RangeError If `radix` is out of range
915
+ */
916
+ toString(radix?: number): string {
917
+ radix = radix || 10;
918
+ if (radix < 2 || 36 < radix) throw RangeError('radix');
919
+ if (this.isZero()) return '0';
920
+ if (this.isNegative()) {
921
+ // Unsigned Longs are never negative
922
+ if (this.eq(Long.MIN_VALUE)) {
923
+ // We need to change the Long value before it can be negated, so we remove
924
+ // the bottom-most digit in this base and then recurse to do the rest.
925
+ const radixLong = Long.fromNumber(radix),
926
+ div = this.div(radixLong),
927
+ rem1 = div.mul(radixLong).sub(this);
928
+ return div.toString(radix) + rem1.toInt().toString(radix);
929
+ } else return '-' + this.neg().toString(radix);
930
+ }
931
+
932
+ // Do several (6) digits each time through the loop, so as to
933
+ // minimize the calls to the very expensive emulated div.
934
+ const radixToPower = Long.fromNumber(Math.pow(radix, 6), this.unsigned);
935
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
936
+ let rem: Long = this;
937
+ let result = '';
938
+ // eslint-disable-next-line no-constant-condition
939
+ while (true) {
940
+ const remDiv = rem.div(radixToPower);
941
+ const intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0;
942
+ let digits = intval.toString(radix);
943
+ rem = remDiv;
944
+ if (rem.isZero()) {
945
+ return digits + result;
946
+ } else {
947
+ while (digits.length < 6) digits = '0' + digits;
948
+ result = '' + digits + result;
949
+ }
950
+ }
951
+ }
952
+
953
+ /** Converts this Long to unsigned. */
954
+ toUnsigned(): Long {
955
+ if (this.unsigned) return this;
956
+ return Long.fromBits(this.low, this.high, true);
957
+ }
958
+
959
+ /** Returns the bitwise XOR of this Long and the given one. */
960
+ xor(other: Long | number | string): Long {
961
+ if (!Long.isLong(other)) other = Long.fromValue(other);
962
+ return Long.fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
963
+ }
964
+
965
+ /** This is an alias of {@link Long.isZero} */
966
+ eqz(): boolean {
967
+ return this.isZero();
968
+ }
969
+
970
+ /** This is an alias of {@link Long.lessThanOrEqual} */
971
+ le(other: string | number | Long | Timestamp): boolean {
972
+ return this.lessThanOrEqual(other);
973
+ }
974
+
975
+ /*
976
+ ****************************************************************
977
+ * BSON SPECIFIC ADDITIONS *
978
+ ****************************************************************
979
+ */
980
+ toExtendedJSON(options?: EJSONOptions): number | LongExtended {
981
+ if (options && options.relaxed) return this.toNumber();
982
+ return { $numberLong: this.toString() };
983
+ }
984
+ static fromExtendedJSON(doc: { $numberLong: string }, options?: EJSONOptions): number | Long {
985
+ const result = Long.fromString(doc.$numberLong);
986
+ return options && options.relaxed ? result.toNumber() : result;
987
+ }
988
+
989
+ /** @internal */
990
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
991
+ return this.inspect();
992
+ }
993
+
994
+ inspect(): string {
995
+ return `Long("${this.toString()}")`;
996
+ }
997
+ }
998
+
999
+ Object.defineProperty(Long.prototype, '__isLong__', { value: true });
1000
+ Object.defineProperty(Long.prototype, '_bsontype', { value: 'Long' });