bson 6.5.1 → 6.6.1

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) |
@@ -4149,13 +4158,12 @@ EJSON.deserialize = EJSONdeserialize;
4149
4158
  Object.freeze(EJSON);
4150
4159
 
4151
4160
  function getSize(source, offset) {
4152
- if (source[offset + 3] > 127) {
4153
- throw new BSONOffsetError('BSON size cannot be negative', offset);
4161
+ try {
4162
+ return NumberUtils.getNonnegativeInt32LE(source, offset);
4163
+ }
4164
+ catch (cause) {
4165
+ throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
4154
4166
  }
4155
- return (source[offset] |
4156
- (source[offset + 1] << 8) |
4157
- (source[offset + 2] << 16) |
4158
- (source[offset + 3] << 24));
4159
4167
  }
4160
4168
  function findNull(bytes, offset) {
4161
4169
  let nullTerminatorOffset = offset;
@@ -4167,6 +4175,7 @@ function findNull(bytes, offset) {
4167
4175
  return nullTerminatorOffset;
4168
4176
  }
4169
4177
  function parseToElements(bytes, startOffset = 0) {
4178
+ startOffset ??= 0;
4170
4179
  if (bytes.length < 5) {
4171
4180
  throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4172
4181
  }
@@ -4192,7 +4201,10 @@ function parseToElements(bytes, startOffset = 0) {
4192
4201
  const nameLength = findNull(bytes, offset) - nameOffset;
4193
4202
  offset += nameLength + 1;
4194
4203
  let length;
4195
- if (type === 1 || type === 18 || type === 9 || type === 17) {
4204
+ if (type === 1 ||
4205
+ type === 18 ||
4206
+ type === 9 ||
4207
+ type === 17) {
4196
4208
  length = 8;
4197
4209
  }
4198
4210
  else if (type === 16) {
@@ -4207,13 +4219,18 @@ function parseToElements(bytes, startOffset = 0) {
4207
4219
  else if (type === 8) {
4208
4220
  length = 1;
4209
4221
  }
4210
- else if (type === 10 || type === 6 || type === 127 || type === 255) {
4222
+ else if (type === 10 ||
4223
+ type === 6 ||
4224
+ type === 127 ||
4225
+ type === 255) {
4211
4226
  length = 0;
4212
4227
  }
4213
4228
  else if (type === 11) {
4214
4229
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4215
4230
  }
4216
- else if (type === 3 || type === 4 || type === 15) {
4231
+ else if (type === 3 ||
4232
+ type === 4 ||
4233
+ type === 15) {
4217
4234
  length = getSize(bytes, offset);
4218
4235
  }
4219
4236
  else if (type === 2 ||
@@ -4243,7 +4260,8 @@ function parseToElements(bytes, startOffset = 0) {
4243
4260
 
4244
4261
  const onDemand = Object.create(null);
4245
4262
  onDemand.parseToElements = parseToElements;
4246
- onDemand.BSONOffsetError = BSONOffsetError;
4263
+ onDemand.ByteUtils = ByteUtils;
4264
+ onDemand.NumberUtils = NumberUtils;
4247
4265
  Object.freeze(onDemand);
4248
4266
 
4249
4267
  const MAXSIZE = 1024 * 1024 * 17;
@@ -4300,6 +4318,7 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4300
4318
  var bson = /*#__PURE__*/Object.freeze({
4301
4319
  __proto__: null,
4302
4320
  BSONError: BSONError,
4321
+ BSONOffsetError: BSONOffsetError,
4303
4322
  BSONRegExp: BSONRegExp,
4304
4323
  BSONRuntimeError: BSONRuntimeError,
4305
4324
  BSONSymbol: BSONSymbol,
@@ -4330,6 +4349,7 @@ var bson = /*#__PURE__*/Object.freeze({
4330
4349
 
4331
4350
  exports.BSON = bson;
4332
4351
  exports.BSONError = BSONError;
4352
+ exports.BSONOffsetError = BSONOffsetError;
4333
4353
  exports.BSONRegExp = BSONRegExp;
4334
4354
  exports.BSONRuntimeError = BSONRuntimeError;
4335
4355
  exports.BSONSymbol = BSONSymbol;