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.cjs CHANGED
@@ -16,7 +16,7 @@ function isDate(d) {
16
16
  return Object.prototype.toString.call(d) === '[object Date]';
17
17
  }
18
18
 
19
- const BSON_MAJOR_VERSION = 5;
19
+ const BSON_MAJOR_VERSION = 6;
20
20
  const BSON_INT32_MAX = 0x7fffffff;
21
21
  const BSON_INT32_MIN = -0x80000000;
22
22
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -311,11 +311,11 @@ class Binary extends BSONValue {
311
311
  constructor(buffer, subType) {
312
312
  super();
313
313
  if (!(buffer == null) &&
314
- !(typeof buffer === 'string') &&
314
+ typeof buffer === 'string' &&
315
315
  !ArrayBuffer.isView(buffer) &&
316
- !(buffer instanceof ArrayBuffer) &&
316
+ !isAnyArrayBuffer(buffer) &&
317
317
  !Array.isArray(buffer)) {
318
- throw new BSONError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
318
+ throw new BSONError('Binary can only be constructed from Uint8Array or number[]');
319
319
  }
320
320
  this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
321
321
  if (buffer == null) {
@@ -323,15 +323,9 @@ class Binary extends BSONValue {
323
323
  this.position = 0;
324
324
  }
325
325
  else {
326
- if (typeof buffer === 'string') {
327
- this.buffer = ByteUtils.fromISO88591(buffer);
328
- }
329
- else if (Array.isArray(buffer)) {
330
- this.buffer = ByteUtils.fromNumberArray(buffer);
331
- }
332
- else {
333
- this.buffer = ByteUtils.toLocalBufferType(buffer);
334
- }
326
+ this.buffer = Array.isArray(buffer)
327
+ ? ByteUtils.fromNumberArray(buffer)
328
+ : ByteUtils.toLocalBufferType(buffer);
335
329
  this.position = this.buffer.byteLength;
336
330
  }
337
331
  }
@@ -377,25 +371,17 @@ class Binary extends BSONValue {
377
371
  offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
378
372
  }
379
373
  else if (typeof sequence === 'string') {
380
- const bytes = ByteUtils.fromISO88591(sequence);
381
- this.buffer.set(bytes, offset);
382
- this.position =
383
- offset + sequence.length > this.position ? offset + sequence.length : this.position;
374
+ throw new BSONError('input cannot be string');
384
375
  }
385
376
  }
386
377
  read(position, length) {
387
378
  length = length && length > 0 ? length : this.position;
388
379
  return this.buffer.slice(position, position + length);
389
380
  }
390
- value(asRaw) {
391
- asRaw = !!asRaw;
392
- if (asRaw && this.buffer.length === this.position) {
393
- return this.buffer;
394
- }
395
- if (asRaw) {
396
- return this.buffer.slice(0, this.position);
397
- }
398
- return ByteUtils.toISO88591(this.buffer.subarray(0, this.position));
381
+ value() {
382
+ return this.buffer.length === this.position
383
+ ? this.buffer
384
+ : this.buffer.subarray(0, this.position);
399
385
  }
400
386
  length() {
401
387
  return this.position;
@@ -596,7 +582,6 @@ class UUID extends Binary {
596
582
  return `new UUID("${this.toHexString()}")`;
597
583
  }
598
584
  }
599
- UUID.cacheHexString = false;
600
585
 
601
586
  class Code extends BSONValue {
602
587
  get _bsontype() {
@@ -1990,20 +1975,11 @@ class ObjectId extends BSONValue {
1990
1975
  this[kId] = ByteUtils.toLocalBufferType(workingId);
1991
1976
  }
1992
1977
  else if (typeof workingId === 'string') {
1993
- if (workingId.length === 12) {
1994
- const bytes = ByteUtils.fromUTF8(workingId);
1995
- if (bytes.byteLength === 12) {
1996
- this[kId] = bytes;
1997
- }
1998
- else {
1999
- throw new BSONError('Argument passed in must be a string of 12 bytes');
2000
- }
2001
- }
2002
- else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
1978
+ if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2003
1979
  this[kId] = ByteUtils.fromHex(workingId);
2004
1980
  }
2005
1981
  else {
2006
- throw new BSONError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
1982
+ throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
2007
1983
  }
2008
1984
  }
2009
1985
  else {
@@ -2065,30 +2041,25 @@ class ObjectId extends BSONValue {
2065
2041
  toJSON() {
2066
2042
  return this.toHexString();
2067
2043
  }
2044
+ static is(variable) {
2045
+ return (variable != null &&
2046
+ typeof variable === 'object' &&
2047
+ '_bsontype' in variable &&
2048
+ variable._bsontype === 'ObjectId');
2049
+ }
2068
2050
  equals(otherId) {
2069
2051
  if (otherId === undefined || otherId === null) {
2070
2052
  return false;
2071
2053
  }
2072
- if (otherId instanceof ObjectId) {
2054
+ if (ObjectId.is(otherId)) {
2073
2055
  return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
2074
2056
  }
2075
- if (typeof otherId === 'string' &&
2076
- ObjectId.isValid(otherId) &&
2077
- otherId.length === 12 &&
2078
- isUint8Array(this.id)) {
2079
- return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
2080
- }
2081
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
2057
+ if (typeof otherId === 'string') {
2082
2058
  return otherId.toLowerCase() === this.toHexString();
2083
2059
  }
2084
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
2085
- return ByteUtils.equals(ByteUtils.fromUTF8(otherId), this.id);
2086
- }
2087
- if (typeof otherId === 'object' &&
2088
- 'toHexString' in otherId &&
2089
- typeof otherId.toHexString === 'function') {
2060
+ if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') {
2090
2061
  const otherIdString = otherId.toHexString();
2091
- const thisIdString = this.toHexString().toLowerCase();
2062
+ const thisIdString = this.toHexString();
2092
2063
  return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
2093
2064
  }
2094
2065
  return false;