bson 5.4.0 → 6.0.0-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/bson.mjs CHANGED
@@ -14,7 +14,7 @@ function isDate(d) {
14
14
  return Object.prototype.toString.call(d) === '[object Date]';
15
15
  }
16
16
 
17
- const BSON_MAJOR_VERSION = 5;
17
+ const BSON_MAJOR_VERSION = 6;
18
18
  const BSON_INT32_MAX = 0x7fffffff;
19
19
  const BSON_INT32_MIN = -0x80000000;
20
20
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -309,11 +309,11 @@ class Binary extends BSONValue {
309
309
  constructor(buffer, subType) {
310
310
  super();
311
311
  if (!(buffer == null) &&
312
- !(typeof buffer === 'string') &&
312
+ typeof buffer === 'string' &&
313
313
  !ArrayBuffer.isView(buffer) &&
314
- !(buffer instanceof ArrayBuffer) &&
314
+ !isAnyArrayBuffer(buffer) &&
315
315
  !Array.isArray(buffer)) {
316
- throw new BSONError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
316
+ throw new BSONError('Binary can only be constructed from Uint8Array or number[]');
317
317
  }
318
318
  this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
319
319
  if (buffer == null) {
@@ -321,15 +321,9 @@ class Binary extends BSONValue {
321
321
  this.position = 0;
322
322
  }
323
323
  else {
324
- if (typeof buffer === 'string') {
325
- this.buffer = ByteUtils.fromISO88591(buffer);
326
- }
327
- else if (Array.isArray(buffer)) {
328
- this.buffer = ByteUtils.fromNumberArray(buffer);
329
- }
330
- else {
331
- this.buffer = ByteUtils.toLocalBufferType(buffer);
332
- }
324
+ this.buffer = Array.isArray(buffer)
325
+ ? ByteUtils.fromNumberArray(buffer)
326
+ : ByteUtils.toLocalBufferType(buffer);
333
327
  this.position = this.buffer.byteLength;
334
328
  }
335
329
  }
@@ -375,25 +369,17 @@ class Binary extends BSONValue {
375
369
  offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
376
370
  }
377
371
  else if (typeof sequence === 'string') {
378
- const bytes = ByteUtils.fromISO88591(sequence);
379
- this.buffer.set(bytes, offset);
380
- this.position =
381
- offset + sequence.length > this.position ? offset + sequence.length : this.position;
372
+ throw new BSONError('input cannot be string');
382
373
  }
383
374
  }
384
375
  read(position, length) {
385
376
  length = length && length > 0 ? length : this.position;
386
377
  return this.buffer.slice(position, position + length);
387
378
  }
388
- value(asRaw) {
389
- asRaw = !!asRaw;
390
- if (asRaw && this.buffer.length === this.position) {
391
- return this.buffer;
392
- }
393
- if (asRaw) {
394
- return this.buffer.slice(0, this.position);
395
- }
396
- return ByteUtils.toISO88591(this.buffer.subarray(0, this.position));
379
+ value() {
380
+ return this.buffer.length === this.position
381
+ ? this.buffer
382
+ : this.buffer.subarray(0, this.position);
397
383
  }
398
384
  length() {
399
385
  return this.position;
@@ -594,7 +580,6 @@ class UUID extends Binary {
594
580
  return `new UUID("${this.toHexString()}")`;
595
581
  }
596
582
  }
597
- UUID.cacheHexString = false;
598
583
 
599
584
  class Code extends BSONValue {
600
585
  get _bsontype() {
@@ -625,7 +610,7 @@ class Code extends BSONValue {
625
610
  }
626
611
  inspect() {
627
612
  const codeJson = this.toJSON();
628
- return `new Code("${String(codeJson.code)}"${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
613
+ return `new Code(${JSON.stringify(String(codeJson.code))}${codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
629
614
  }
630
615
  }
631
616
 
@@ -1435,6 +1420,7 @@ class Decimal128 extends BSONValue {
1435
1420
  }
1436
1421
  static fromString(representation) {
1437
1422
  let isNegative = false;
1423
+ let sawSign = false;
1438
1424
  let sawRadix = false;
1439
1425
  let foundNonZero = false;
1440
1426
  let significantDigits = 0;
@@ -1445,10 +1431,8 @@ class Decimal128 extends BSONValue {
1445
1431
  const digits = [0];
1446
1432
  let nDigitsStored = 0;
1447
1433
  let digitsInsert = 0;
1448
- let firstDigit = 0;
1449
1434
  let lastDigit = 0;
1450
1435
  let exponent = 0;
1451
- let i = 0;
1452
1436
  let significandHigh = new Long(0, 0);
1453
1437
  let significandLow = new Long(0, 0);
1454
1438
  let biasedExponent = 0;
@@ -1476,6 +1460,7 @@ class Decimal128 extends BSONValue {
1476
1460
  }
1477
1461
  }
1478
1462
  if (representation[index] === '+' || representation[index] === '-') {
1463
+ sawSign = true;
1479
1464
  isNegative = representation[index++] === '-';
1480
1465
  }
1481
1466
  if (!isDigit(representation[index]) && representation[index] !== '.') {
@@ -1494,7 +1479,7 @@ class Decimal128 extends BSONValue {
1494
1479
  index = index + 1;
1495
1480
  continue;
1496
1481
  }
1497
- if (nDigitsStored < 34) {
1482
+ if (nDigitsStored < MAX_DIGITS) {
1498
1483
  if (representation[index] !== '0' || foundNonZero) {
1499
1484
  if (!foundNonZero) {
1500
1485
  firstNonZero = nDigitsRead;
@@ -1522,10 +1507,7 @@ class Decimal128 extends BSONValue {
1522
1507
  }
1523
1508
  if (representation[index])
1524
1509
  return new Decimal128(NAN_BUFFER);
1525
- firstDigit = 0;
1526
1510
  if (!nDigitsStored) {
1527
- firstDigit = 0;
1528
- lastDigit = 0;
1529
1511
  digits[0] = 0;
1530
1512
  nDigits = 1;
1531
1513
  nDigitsStored = 1;
@@ -1535,12 +1517,12 @@ class Decimal128 extends BSONValue {
1535
1517
  lastDigit = nDigitsStored - 1;
1536
1518
  significantDigits = nDigits;
1537
1519
  if (significantDigits !== 1) {
1538
- while (digits[firstNonZero + significantDigits - 1] === 0) {
1520
+ while (representation[firstNonZero + significantDigits - 1 + Number(sawSign) + Number(sawRadix)] === '0') {
1539
1521
  significantDigits = significantDigits - 1;
1540
1522
  }
1541
1523
  }
1542
1524
  }
1543
- if (exponent <= radixPosition && radixPosition - exponent > 1 << 14) {
1525
+ if (exponent <= radixPosition && radixPosition > exponent + (1 << 14)) {
1544
1526
  exponent = EXPONENT_MIN;
1545
1527
  }
1546
1528
  else {
@@ -1548,9 +1530,8 @@ class Decimal128 extends BSONValue {
1548
1530
  }
1549
1531
  while (exponent > EXPONENT_MAX) {
1550
1532
  lastDigit = lastDigit + 1;
1551
- if (lastDigit - firstDigit > MAX_DIGITS) {
1552
- const digitsString = digits.join('');
1553
- if (digitsString.match(/^0+$/)) {
1533
+ if (lastDigit >= MAX_DIGITS) {
1534
+ if (significantDigits === 0) {
1554
1535
  exponent = EXPONENT_MAX;
1555
1536
  break;
1556
1537
  }
@@ -1559,69 +1540,43 @@ class Decimal128 extends BSONValue {
1559
1540
  exponent = exponent - 1;
1560
1541
  }
1561
1542
  while (exponent < EXPONENT_MIN || nDigitsStored < nDigits) {
1562
- if (lastDigit === 0 && significantDigits < nDigitsStored) {
1563
- exponent = EXPONENT_MIN;
1564
- significantDigits = 0;
1565
- break;
1543
+ if (lastDigit === 0) {
1544
+ if (significantDigits === 0) {
1545
+ exponent = EXPONENT_MIN;
1546
+ break;
1547
+ }
1548
+ invalidErr(representation, 'exponent underflow');
1566
1549
  }
1567
1550
  if (nDigitsStored < nDigits) {
1551
+ if (representation[nDigits - 1 + Number(sawSign) + Number(sawRadix)] !== '0' &&
1552
+ significantDigits !== 0) {
1553
+ invalidErr(representation, 'inexact rounding');
1554
+ }
1568
1555
  nDigits = nDigits - 1;
1569
1556
  }
1570
1557
  else {
1558
+ if (digits[lastDigit] !== 0) {
1559
+ invalidErr(representation, 'inexact rounding');
1560
+ }
1571
1561
  lastDigit = lastDigit - 1;
1572
1562
  }
1573
1563
  if (exponent < EXPONENT_MAX) {
1574
1564
  exponent = exponent + 1;
1575
1565
  }
1576
1566
  else {
1577
- const digitsString = digits.join('');
1578
- if (digitsString.match(/^0+$/)) {
1579
- exponent = EXPONENT_MAX;
1580
- break;
1581
- }
1582
1567
  invalidErr(representation, 'overflow');
1583
1568
  }
1584
1569
  }
1585
- if (lastDigit - firstDigit + 1 < significantDigits) {
1586
- let endOfString = nDigitsRead;
1570
+ if (lastDigit + 1 < significantDigits) {
1587
1571
  if (sawRadix) {
1588
1572
  firstNonZero = firstNonZero + 1;
1589
- endOfString = endOfString + 1;
1590
1573
  }
1591
- if (isNegative) {
1574
+ if (sawSign) {
1592
1575
  firstNonZero = firstNonZero + 1;
1593
- endOfString = endOfString + 1;
1594
1576
  }
1595
1577
  const roundDigit = parseInt(representation[firstNonZero + lastDigit + 1], 10);
1596
- let roundBit = 0;
1597
- if (roundDigit >= 5) {
1598
- roundBit = 1;
1599
- if (roundDigit === 5) {
1600
- roundBit = digits[lastDigit] % 2 === 1 ? 1 : 0;
1601
- for (i = firstNonZero + lastDigit + 2; i < endOfString; i++) {
1602
- if (parseInt(representation[i], 10)) {
1603
- roundBit = 1;
1604
- break;
1605
- }
1606
- }
1607
- }
1608
- }
1609
- if (roundBit) {
1610
- let dIdx = lastDigit;
1611
- for (; dIdx >= 0; dIdx--) {
1612
- if (++digits[dIdx] > 9) {
1613
- digits[dIdx] = 0;
1614
- if (dIdx === 0) {
1615
- if (exponent < EXPONENT_MAX) {
1616
- exponent = exponent + 1;
1617
- digits[dIdx] = 1;
1618
- }
1619
- else {
1620
- return new Decimal128(isNegative ? INF_NEGATIVE_BUFFER : INF_POSITIVE_BUFFER);
1621
- }
1622
- }
1623
- }
1624
- }
1578
+ if (roundDigit !== 0) {
1579
+ invalidErr(representation, 'inexact rounding');
1625
1580
  }
1626
1581
  }
1627
1582
  significandHigh = Long.fromNumber(0);
@@ -1630,8 +1585,8 @@ class Decimal128 extends BSONValue {
1630
1585
  significandHigh = Long.fromNumber(0);
1631
1586
  significandLow = Long.fromNumber(0);
1632
1587
  }
1633
- else if (lastDigit - firstDigit < 17) {
1634
- let dIdx = firstDigit;
1588
+ else if (lastDigit < 17) {
1589
+ let dIdx = 0;
1635
1590
  significandLow = Long.fromNumber(digits[dIdx++]);
1636
1591
  significandHigh = new Long(0, 0);
1637
1592
  for (; dIdx <= lastDigit; dIdx++) {
@@ -1640,7 +1595,7 @@ class Decimal128 extends BSONValue {
1640
1595
  }
1641
1596
  }
1642
1597
  else {
1643
- let dIdx = firstDigit;
1598
+ let dIdx = 0;
1644
1599
  significandHigh = Long.fromNumber(digits[dIdx++]);
1645
1600
  for (; dIdx <= lastDigit - 17; dIdx++) {
1646
1601
  significandHigh = significandHigh.multiply(Long.fromNumber(10));
@@ -1988,20 +1943,11 @@ class ObjectId extends BSONValue {
1988
1943
  this[kId] = ByteUtils.toLocalBufferType(workingId);
1989
1944
  }
1990
1945
  else if (typeof workingId === 'string') {
1991
- if (workingId.length === 12) {
1992
- const bytes = ByteUtils.fromUTF8(workingId);
1993
- if (bytes.byteLength === 12) {
1994
- this[kId] = bytes;
1995
- }
1996
- else {
1997
- throw new BSONError('Argument passed in must be a string of 12 bytes');
1998
- }
1999
- }
2000
- else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
1946
+ if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2001
1947
  this[kId] = ByteUtils.fromHex(workingId);
2002
1948
  }
2003
1949
  else {
2004
- throw new BSONError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
1950
+ throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
2005
1951
  }
2006
1952
  }
2007
1953
  else {
@@ -2063,30 +2009,25 @@ class ObjectId extends BSONValue {
2063
2009
  toJSON() {
2064
2010
  return this.toHexString();
2065
2011
  }
2012
+ static is(variable) {
2013
+ return (variable != null &&
2014
+ typeof variable === 'object' &&
2015
+ '_bsontype' in variable &&
2016
+ variable._bsontype === 'ObjectId');
2017
+ }
2066
2018
  equals(otherId) {
2067
2019
  if (otherId === undefined || otherId === null) {
2068
2020
  return false;
2069
2021
  }
2070
- if (otherId instanceof ObjectId) {
2022
+ if (ObjectId.is(otherId)) {
2071
2023
  return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
2072
2024
  }
2073
- if (typeof otherId === 'string' &&
2074
- ObjectId.isValid(otherId) &&
2075
- otherId.length === 12 &&
2076
- isUint8Array(this.id)) {
2077
- return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
2078
- }
2079
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
2025
+ if (typeof otherId === 'string') {
2080
2026
  return otherId.toLowerCase() === this.toHexString();
2081
2027
  }
2082
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
2083
- return ByteUtils.equals(ByteUtils.fromUTF8(otherId), this.id);
2084
- }
2085
- if (typeof otherId === 'object' &&
2086
- 'toHexString' in otherId &&
2087
- typeof otherId.toHexString === 'function') {
2028
+ if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') {
2088
2029
  const otherIdString = otherId.toHexString();
2089
- const thisIdString = this.toHexString().toLowerCase();
2030
+ const thisIdString = this.toHexString();
2090
2031
  return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
2091
2032
  }
2092
2033
  return false;
@@ -2376,7 +2317,7 @@ class BSONSymbol extends BSONValue {
2376
2317
  return this.value;
2377
2318
  }
2378
2319
  inspect() {
2379
- return `new BSONSymbol("${this.value}")`;
2320
+ return `new BSONSymbol(${JSON.stringify(this.value)})`;
2380
2321
  }
2381
2322
  toJSON() {
2382
2323
  return this.value;