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.cjs CHANGED
@@ -130,8 +130,8 @@ class BSONOffsetError extends BSONError {
130
130
  get name() {
131
131
  return 'BSONOffsetError';
132
132
  }
133
- constructor(message, offset) {
134
- super(`${message}. offset: ${offset}`);
133
+ constructor(message, offset, options) {
134
+ super(`${message}. offset: ${offset}`, options);
135
135
  this.offset = offset;
136
136
  }
137
137
  }
@@ -533,16 +533,16 @@ class Binary extends BSONValue {
533
533
  return this.position;
534
534
  }
535
535
  toJSON() {
536
- return ByteUtils.toBase64(this.buffer);
536
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
537
537
  }
538
538
  toString(encoding) {
539
539
  if (encoding === 'hex')
540
- return ByteUtils.toHex(this.buffer);
540
+ return ByteUtils.toHex(this.buffer.subarray(0, this.position));
541
541
  if (encoding === 'base64')
542
- return ByteUtils.toBase64(this.buffer);
542
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
543
543
  if (encoding === 'utf8' || encoding === 'utf-8')
544
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
545
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
544
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
545
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
546
546
  }
547
547
  toExtendedJSON(options) {
548
548
  options = options || {};
@@ -2138,6 +2138,15 @@ const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
2138
2138
  FLOAT[0] = -1;
2139
2139
  const isBigEndian = FLOAT_BYTES[7] === 0;
2140
2140
  const NumberUtils = {
2141
+ getNonnegativeInt32LE(source, offset) {
2142
+ if (source[offset + 3] > 127) {
2143
+ throw new RangeError(`Size cannot be negative at offset: ${offset}`);
2144
+ }
2145
+ return (source[offset] |
2146
+ (source[offset + 1] << 8) |
2147
+ (source[offset + 2] << 16) |
2148
+ (source[offset + 3] << 24));
2149
+ },
2141
2150
  getInt32LE(source, offset) {
2142
2151
  return (source[offset] |
2143
2152
  (source[offset + 1] << 8) |
@@ -4127,13 +4136,12 @@ EJSON.deserialize = EJSONdeserialize;
4127
4136
  Object.freeze(EJSON);
4128
4137
 
4129
4138
  function getSize(source, offset) {
4130
- if (source[offset + 3] > 127) {
4131
- throw new BSONOffsetError('BSON size cannot be negative', offset);
4139
+ try {
4140
+ return NumberUtils.getNonnegativeInt32LE(source, offset);
4141
+ }
4142
+ catch (cause) {
4143
+ throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
4132
4144
  }
4133
- return (source[offset] |
4134
- (source[offset + 1] << 8) |
4135
- (source[offset + 2] << 16) |
4136
- (source[offset + 3] << 24));
4137
4145
  }
4138
4146
  function findNull(bytes, offset) {
4139
4147
  let nullTerminatorOffset = offset;
@@ -4145,6 +4153,7 @@ function findNull(bytes, offset) {
4145
4153
  return nullTerminatorOffset;
4146
4154
  }
4147
4155
  function parseToElements(bytes, startOffset = 0) {
4156
+ startOffset ??= 0;
4148
4157
  if (bytes.length < 5) {
4149
4158
  throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4150
4159
  }
@@ -4170,7 +4179,10 @@ function parseToElements(bytes, startOffset = 0) {
4170
4179
  const nameLength = findNull(bytes, offset) - nameOffset;
4171
4180
  offset += nameLength + 1;
4172
4181
  let length;
4173
- if (type === 1 || type === 18 || type === 9 || type === 17) {
4182
+ if (type === 1 ||
4183
+ type === 18 ||
4184
+ type === 9 ||
4185
+ type === 17) {
4174
4186
  length = 8;
4175
4187
  }
4176
4188
  else if (type === 16) {
@@ -4185,13 +4197,18 @@ function parseToElements(bytes, startOffset = 0) {
4185
4197
  else if (type === 8) {
4186
4198
  length = 1;
4187
4199
  }
4188
- else if (type === 10 || type === 6 || type === 127 || type === 255) {
4200
+ else if (type === 10 ||
4201
+ type === 6 ||
4202
+ type === 127 ||
4203
+ type === 255) {
4189
4204
  length = 0;
4190
4205
  }
4191
4206
  else if (type === 11) {
4192
4207
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4193
4208
  }
4194
- else if (type === 3 || type === 4 || type === 15) {
4209
+ else if (type === 3 ||
4210
+ type === 4 ||
4211
+ type === 15) {
4195
4212
  length = getSize(bytes, offset);
4196
4213
  }
4197
4214
  else if (type === 2 ||
@@ -4221,7 +4238,8 @@ function parseToElements(bytes, startOffset = 0) {
4221
4238
 
4222
4239
  const onDemand = Object.create(null);
4223
4240
  onDemand.parseToElements = parseToElements;
4224
- onDemand.BSONOffsetError = BSONOffsetError;
4241
+ onDemand.ByteUtils = ByteUtils;
4242
+ onDemand.NumberUtils = NumberUtils;
4225
4243
  Object.freeze(onDemand);
4226
4244
 
4227
4245
  const MAXSIZE = 1024 * 1024 * 17;
@@ -4278,6 +4296,7 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4278
4296
  var bson = /*#__PURE__*/Object.freeze({
4279
4297
  __proto__: null,
4280
4298
  BSONError: BSONError,
4299
+ BSONOffsetError: BSONOffsetError,
4281
4300
  BSONRegExp: BSONRegExp,
4282
4301
  BSONRuntimeError: BSONRuntimeError,
4283
4302
  BSONSymbol: BSONSymbol,
@@ -4308,6 +4327,7 @@ var bson = /*#__PURE__*/Object.freeze({
4308
4327
 
4309
4328
  exports.BSON = bson;
4310
4329
  exports.BSONError = BSONError;
4330
+ exports.BSONOffsetError = BSONOffsetError;
4311
4331
  exports.BSONRegExp = BSONRegExp;
4312
4332
  exports.BSONRuntimeError = BSONRuntimeError;
4313
4333
  exports.BSONSymbol = BSONSymbol;