bson 4.6.5 → 5.0.0-alpha.0

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