bson 6.5.0 → 6.6.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
@@ -144,8 +144,8 @@ class BSONOffsetError extends BSONError {
144
144
  get name() {
145
145
  return 'BSONOffsetError';
146
146
  }
147
- constructor(message, offset) {
148
- super(`${message}. offset: ${offset}`);
147
+ constructor(message, offset, options) {
148
+ super(`${message}. offset: ${offset}`, options);
149
149
  this.offset = offset;
150
150
  }
151
151
  }
@@ -549,16 +549,16 @@ class Binary extends BSONValue {
549
549
  return this.position;
550
550
  }
551
551
  toJSON() {
552
- return ByteUtils.toBase64(this.buffer);
552
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
553
553
  }
554
554
  toString(encoding) {
555
555
  if (encoding === 'hex')
556
- return ByteUtils.toHex(this.buffer);
556
+ return ByteUtils.toHex(this.buffer.subarray(0, this.position));
557
557
  if (encoding === 'base64')
558
- return ByteUtils.toBase64(this.buffer);
558
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
559
559
  if (encoding === 'utf8' || encoding === 'utf-8')
560
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
561
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
560
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
561
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
562
562
  }
563
563
  toExtendedJSON(options) {
564
564
  options = options || {};
@@ -2154,6 +2154,15 @@ const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
2154
2154
  FLOAT[0] = -1;
2155
2155
  const isBigEndian = FLOAT_BYTES[7] === 0;
2156
2156
  const NumberUtils = {
2157
+ getNonnegativeInt32LE(source, offset) {
2158
+ if (source[offset + 3] > 127) {
2159
+ throw new RangeError(`Size cannot be negative at offset: ${offset}`);
2160
+ }
2161
+ return (source[offset] |
2162
+ (source[offset + 1] << 8) |
2163
+ (source[offset + 2] << 16) |
2164
+ (source[offset + 3] << 24));
2165
+ },
2157
2166
  getInt32LE(source, offset) {
2158
2167
  return (source[offset] |
2159
2168
  (source[offset + 1] << 8) |
@@ -4143,13 +4152,12 @@ EJSON.deserialize = EJSONdeserialize;
4143
4152
  Object.freeze(EJSON);
4144
4153
 
4145
4154
  function getSize(source, offset) {
4146
- if (source[offset + 3] > 127) {
4147
- throw new BSONOffsetError('BSON size cannot be negative', offset);
4155
+ try {
4156
+ return NumberUtils.getNonnegativeInt32LE(source, offset);
4157
+ }
4158
+ catch (cause) {
4159
+ throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
4148
4160
  }
4149
- return (source[offset] |
4150
- (source[offset + 1] << 8) |
4151
- (source[offset + 2] << 16) |
4152
- (source[offset + 3] << 24));
4153
4161
  }
4154
4162
  function findNull(bytes, offset) {
4155
4163
  let nullTerminatorOffset = offset;
@@ -4161,6 +4169,7 @@ function findNull(bytes, offset) {
4161
4169
  return nullTerminatorOffset;
4162
4170
  }
4163
4171
  function parseToElements(bytes, startOffset = 0) {
4172
+ startOffset ??= 0;
4164
4173
  if (bytes.length < 5) {
4165
4174
  throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4166
4175
  }
@@ -4186,7 +4195,10 @@ function parseToElements(bytes, startOffset = 0) {
4186
4195
  const nameLength = findNull(bytes, offset) - nameOffset;
4187
4196
  offset += nameLength + 1;
4188
4197
  let length;
4189
- if (type === 1 || type === 18 || type === 9 || type === 17) {
4198
+ if (type === 1 ||
4199
+ type === 18 ||
4200
+ type === 9 ||
4201
+ type === 17) {
4190
4202
  length = 8;
4191
4203
  }
4192
4204
  else if (type === 16) {
@@ -4201,13 +4213,18 @@ function parseToElements(bytes, startOffset = 0) {
4201
4213
  else if (type === 8) {
4202
4214
  length = 1;
4203
4215
  }
4204
- else if (type === 10 || type === 6 || type === 127 || type === 255) {
4216
+ else if (type === 10 ||
4217
+ type === 6 ||
4218
+ type === 127 ||
4219
+ type === 255) {
4205
4220
  length = 0;
4206
4221
  }
4207
4222
  else if (type === 11) {
4208
4223
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4209
4224
  }
4210
- else if (type === 3 || type === 4 || type === 15) {
4225
+ else if (type === 3 ||
4226
+ type === 4 ||
4227
+ type === 15) {
4211
4228
  length = getSize(bytes, offset);
4212
4229
  }
4213
4230
  else if (type === 2 ||
@@ -4237,7 +4254,8 @@ function parseToElements(bytes, startOffset = 0) {
4237
4254
 
4238
4255
  const onDemand = Object.create(null);
4239
4256
  onDemand.parseToElements = parseToElements;
4240
- onDemand.BSONOffsetError = BSONOffsetError;
4257
+ onDemand.ByteUtils = ByteUtils;
4258
+ onDemand.NumberUtils = NumberUtils;
4241
4259
  Object.freeze(onDemand);
4242
4260
 
4243
4261
  const MAXSIZE = 1024 * 1024 * 17;
@@ -4294,6 +4312,7 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4294
4312
  var bson = /*#__PURE__*/Object.freeze({
4295
4313
  __proto__: null,
4296
4314
  BSONError: BSONError,
4315
+ BSONOffsetError: BSONOffsetError,
4297
4316
  BSONRegExp: BSONRegExp,
4298
4317
  BSONRuntimeError: BSONRuntimeError,
4299
4318
  BSONSymbol: BSONSymbol,
@@ -4324,6 +4343,7 @@ var bson = /*#__PURE__*/Object.freeze({
4324
4343
 
4325
4344
  exports.BSON = bson;
4326
4345
  exports.BSONError = BSONError;
4346
+ exports.BSONOffsetError = BSONOffsetError;
4327
4347
  exports.BSONRegExp = BSONRegExp;
4328
4348
  exports.BSONRuntimeError = BSONRuntimeError;
4329
4349
  exports.BSONSymbol = BSONSymbol;