bson 4.1.0 → 4.2.3

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