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