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.rn.cjs CHANGED
@@ -22,7 +22,7 @@ function isDate(d) {
22
22
  return Object.prototype.toString.call(d) === '[object Date]';
23
23
  }
24
24
 
25
- const BSON_MAJOR_VERSION = 5;
25
+ const BSON_MAJOR_VERSION = 6;
26
26
  const BSON_INT32_MAX = 0x7fffffff;
27
27
  const BSON_INT32_MIN = -0x80000000;
28
28
  const BSON_INT64_MAX = Math.pow(2, 63) - 1;
@@ -326,11 +326,11 @@ class Binary extends BSONValue {
326
326
  constructor(buffer, subType) {
327
327
  super();
328
328
  if (!(buffer == null) &&
329
- !(typeof buffer === 'string') &&
329
+ typeof buffer === 'string' &&
330
330
  !ArrayBuffer.isView(buffer) &&
331
- !(buffer instanceof ArrayBuffer) &&
331
+ !isAnyArrayBuffer(buffer) &&
332
332
  !Array.isArray(buffer)) {
333
- throw new BSONError('Binary can only be constructed from string, Buffer, TypedArray, or Array<number>');
333
+ throw new BSONError('Binary can only be constructed from Uint8Array or number[]');
334
334
  }
335
335
  this.sub_type = subType ?? Binary.BSON_BINARY_SUBTYPE_DEFAULT;
336
336
  if (buffer == null) {
@@ -338,15 +338,9 @@ class Binary extends BSONValue {
338
338
  this.position = 0;
339
339
  }
340
340
  else {
341
- if (typeof buffer === 'string') {
342
- this.buffer = ByteUtils.fromISO88591(buffer);
343
- }
344
- else if (Array.isArray(buffer)) {
345
- this.buffer = ByteUtils.fromNumberArray(buffer);
346
- }
347
- else {
348
- this.buffer = ByteUtils.toLocalBufferType(buffer);
349
- }
341
+ this.buffer = Array.isArray(buffer)
342
+ ? ByteUtils.fromNumberArray(buffer)
343
+ : ByteUtils.toLocalBufferType(buffer);
350
344
  this.position = this.buffer.byteLength;
351
345
  }
352
346
  }
@@ -392,25 +386,17 @@ class Binary extends BSONValue {
392
386
  offset + sequence.byteLength > this.position ? offset + sequence.length : this.position;
393
387
  }
394
388
  else if (typeof sequence === 'string') {
395
- const bytes = ByteUtils.fromISO88591(sequence);
396
- this.buffer.set(bytes, offset);
397
- this.position =
398
- offset + sequence.length > this.position ? offset + sequence.length : this.position;
389
+ throw new BSONError('input cannot be string');
399
390
  }
400
391
  }
401
392
  read(position, length) {
402
393
  length = length && length > 0 ? length : this.position;
403
394
  return this.buffer.slice(position, position + length);
404
395
  }
405
- value(asRaw) {
406
- asRaw = !!asRaw;
407
- if (asRaw && this.buffer.length === this.position) {
408
- return this.buffer;
409
- }
410
- if (asRaw) {
411
- return this.buffer.slice(0, this.position);
412
- }
413
- return ByteUtils.toISO88591(this.buffer.subarray(0, this.position));
396
+ value() {
397
+ return this.buffer.length === this.position
398
+ ? this.buffer
399
+ : this.buffer.subarray(0, this.position);
414
400
  }
415
401
  length() {
416
402
  return this.position;
@@ -611,7 +597,6 @@ class UUID extends Binary {
611
597
  return `new UUID("${this.toHexString()}")`;
612
598
  }
613
599
  }
614
- UUID.cacheHexString = false;
615
600
 
616
601
  class Code extends BSONValue {
617
602
  get _bsontype() {
@@ -2005,20 +1990,11 @@ class ObjectId extends BSONValue {
2005
1990
  this[kId] = ByteUtils.toLocalBufferType(workingId);
2006
1991
  }
2007
1992
  else if (typeof workingId === 'string') {
2008
- if (workingId.length === 12) {
2009
- const bytes = ByteUtils.fromUTF8(workingId);
2010
- if (bytes.byteLength === 12) {
2011
- this[kId] = bytes;
2012
- }
2013
- else {
2014
- throw new BSONError('Argument passed in must be a string of 12 bytes');
2015
- }
2016
- }
2017
- else if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
1993
+ if (workingId.length === 24 && checkForHexRegExp.test(workingId)) {
2018
1994
  this[kId] = ByteUtils.fromHex(workingId);
2019
1995
  }
2020
1996
  else {
2021
- throw new BSONError('Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer');
1997
+ throw new BSONError('input must be a 24 character hex string, 12 byte Uint8Array, or an integer');
2022
1998
  }
2023
1999
  }
2024
2000
  else {
@@ -2080,30 +2056,25 @@ class ObjectId extends BSONValue {
2080
2056
  toJSON() {
2081
2057
  return this.toHexString();
2082
2058
  }
2059
+ static is(variable) {
2060
+ return (variable != null &&
2061
+ typeof variable === 'object' &&
2062
+ '_bsontype' in variable &&
2063
+ variable._bsontype === 'ObjectId');
2064
+ }
2083
2065
  equals(otherId) {
2084
2066
  if (otherId === undefined || otherId === null) {
2085
2067
  return false;
2086
2068
  }
2087
- if (otherId instanceof ObjectId) {
2069
+ if (ObjectId.is(otherId)) {
2088
2070
  return this[kId][11] === otherId[kId][11] && ByteUtils.equals(this[kId], otherId[kId]);
2089
2071
  }
2090
- if (typeof otherId === 'string' &&
2091
- ObjectId.isValid(otherId) &&
2092
- otherId.length === 12 &&
2093
- isUint8Array(this.id)) {
2094
- return ByteUtils.equals(this.id, ByteUtils.fromISO88591(otherId));
2095
- }
2096
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 24) {
2072
+ if (typeof otherId === 'string') {
2097
2073
  return otherId.toLowerCase() === this.toHexString();
2098
2074
  }
2099
- if (typeof otherId === 'string' && ObjectId.isValid(otherId) && otherId.length === 12) {
2100
- return ByteUtils.equals(ByteUtils.fromUTF8(otherId), this.id);
2101
- }
2102
- if (typeof otherId === 'object' &&
2103
- 'toHexString' in otherId &&
2104
- typeof otherId.toHexString === 'function') {
2075
+ if (typeof otherId === 'object' && typeof otherId.toHexString === 'function') {
2105
2076
  const otherIdString = otherId.toHexString();
2106
- const thisIdString = this.toHexString().toLowerCase();
2077
+ const thisIdString = this.toHexString();
2107
2078
  return typeof otherIdString === 'string' && otherIdString.toLowerCase() === thisIdString;
2108
2079
  }
2109
2080
  return false;