bson 5.4.0 → 6.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.
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() {
@@ -1988,20 +1973,11 @@ class ObjectId extends BSONValue {
1988
1973
  this[kId] = ByteUtils.toLocalBufferType(workingId);
1989
1974
  }
1990
1975
  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)) {
1976
+ if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2001
1977
  this[kId] = ByteUtils.fromHex(workingId);
2002
1978
  }
2003
1979
  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');
1980
+ throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
2005
1981
  }
2006
1982
  }
2007
1983
  else {
@@ -2063,30 +2039,25 @@ class ObjectId extends BSONValue {
2063
2039
  toJSON() {
2064
2040
  return this.toHexString();
2065
2041
  }
2042
+ static is(variable) {
2043
+ return (variable != null &&
2044
+ typeof variable === 'object' &&
2045
+ '_bsontype' in variable &&
2046
+ variable._bsontype === 'ObjectId');
2047
+ }
2066
2048
  equals(otherId) {
2067
2049
  if (otherId === undefined || otherId === null) {
2068
2050
  return false;
2069
2051
  }
2070
- if (otherId instanceof ObjectId) {
2052
+ if (ObjectId.is(otherId)) {
2071
2053
  return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
2072
2054
  }
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) {
2055
+ if (typeof otherId === 'string') {
2080
2056
  return otherId.toLowerCase() === this.toHexString();
2081
2057
  }
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') {
2058
+ if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') {
2088
2059
  const otherIdString = otherId.toHexString();
2089
- const thisIdString = this.toHexString().toLowerCase();
2060
+ const thisIdString = this.toHexString();
2090
2061
  return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
2091
2062
  }
2092
2063
  return false;