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.mjs CHANGED
@@ -128,8 +128,8 @@ class BSONOffsetError extends BSONError {
128
128
  get name() {
129
129
  return 'BSONOffsetError';
130
130
  }
131
- constructor(message, offset) {
132
- super(`${message}. offset: ${offset}`);
131
+ constructor(message, offset, options) {
132
+ super(`${message}. offset: ${offset}`, options);
133
133
  this.offset = offset;
134
134
  }
135
135
  }
@@ -531,16 +531,16 @@ class Binary extends BSONValue {
531
531
  return this.position;
532
532
  }
533
533
  toJSON() {
534
- return ByteUtils.toBase64(this.buffer);
534
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
535
535
  }
536
536
  toString(encoding) {
537
537
  if (encoding === 'hex')
538
- return ByteUtils.toHex(this.buffer);
538
+ return ByteUtils.toHex(this.buffer.subarray(0, this.position));
539
539
  if (encoding === 'base64')
540
- return ByteUtils.toBase64(this.buffer);
540
+ return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
541
541
  if (encoding === 'utf8' || encoding === 'utf-8')
542
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
543
- return ByteUtils.toUTF8(this.buffer, 0, this.buffer.byteLength, false);
542
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
543
+ return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
544
544
  }
545
545
  toExtendedJSON(options) {
546
546
  options = options || {};
@@ -2136,6 +2136,15 @@ const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
2136
2136
  FLOAT[0] = -1;
2137
2137
  const isBigEndian = FLOAT_BYTES[7] === 0;
2138
2138
  const NumberUtils = {
2139
+ getNonnegativeInt32LE(source, offset) {
2140
+ if (source[offset + 3] > 127) {
2141
+ throw new RangeError(`Size cannot be negative at offset: ${offset}`);
2142
+ }
2143
+ return (source[offset] |
2144
+ (source[offset + 1] << 8) |
2145
+ (source[offset + 2] << 16) |
2146
+ (source[offset + 3] << 24));
2147
+ },
2139
2148
  getInt32LE(source, offset) {
2140
2149
  return (source[offset] |
2141
2150
  (source[offset + 1] << 8) |
@@ -4125,13 +4134,12 @@ EJSON.deserialize = EJSONdeserialize;
4125
4134
  Object.freeze(EJSON);
4126
4135
 
4127
4136
  function getSize(source, offset) {
4128
- if (source[offset + 3] > 127) {
4129
- throw new BSONOffsetError('BSON size cannot be negative', offset);
4137
+ try {
4138
+ return NumberUtils.getNonnegativeInt32LE(source, offset);
4139
+ }
4140
+ catch (cause) {
4141
+ throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
4130
4142
  }
4131
- return (source[offset] |
4132
- (source[offset + 1] << 8) |
4133
- (source[offset + 2] << 16) |
4134
- (source[offset + 3] << 24));
4135
4143
  }
4136
4144
  function findNull(bytes, offset) {
4137
4145
  let nullTerminatorOffset = offset;
@@ -4143,6 +4151,7 @@ function findNull(bytes, offset) {
4143
4151
  return nullTerminatorOffset;
4144
4152
  }
4145
4153
  function parseToElements(bytes, startOffset = 0) {
4154
+ startOffset ??= 0;
4146
4155
  if (bytes.length < 5) {
4147
4156
  throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4148
4157
  }
@@ -4168,7 +4177,10 @@ function parseToElements(bytes, startOffset = 0) {
4168
4177
  const nameLength = findNull(bytes, offset) - nameOffset;
4169
4178
  offset += nameLength + 1;
4170
4179
  let length;
4171
- if (type === 1 || type === 18 || type === 9 || type === 17) {
4180
+ if (type === 1 ||
4181
+ type === 18 ||
4182
+ type === 9 ||
4183
+ type === 17) {
4172
4184
  length = 8;
4173
4185
  }
4174
4186
  else if (type === 16) {
@@ -4183,13 +4195,18 @@ function parseToElements(bytes, startOffset = 0) {
4183
4195
  else if (type === 8) {
4184
4196
  length = 1;
4185
4197
  }
4186
- else if (type === 10 || type === 6 || type === 127 || type === 255) {
4198
+ else if (type === 10 ||
4199
+ type === 6 ||
4200
+ type === 127 ||
4201
+ type === 255) {
4187
4202
  length = 0;
4188
4203
  }
4189
4204
  else if (type === 11) {
4190
4205
  length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4191
4206
  }
4192
- else if (type === 3 || type === 4 || type === 15) {
4207
+ else if (type === 3 ||
4208
+ type === 4 ||
4209
+ type === 15) {
4193
4210
  length = getSize(bytes, offset);
4194
4211
  }
4195
4212
  else if (type === 2 ||
@@ -4219,7 +4236,8 @@ function parseToElements(bytes, startOffset = 0) {
4219
4236
 
4220
4237
  const onDemand = Object.create(null);
4221
4238
  onDemand.parseToElements = parseToElements;
4222
- onDemand.BSONOffsetError = BSONOffsetError;
4239
+ onDemand.ByteUtils = ByteUtils;
4240
+ onDemand.NumberUtils = NumberUtils;
4223
4241
  Object.freeze(onDemand);
4224
4242
 
4225
4243
  const MAXSIZE = 1024 * 1024 * 17;
@@ -4276,6 +4294,7 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
4276
4294
  var bson = /*#__PURE__*/Object.freeze({
4277
4295
  __proto__: null,
4278
4296
  BSONError: BSONError,
4297
+ BSONOffsetError: BSONOffsetError,
4279
4298
  BSONRegExp: BSONRegExp,
4280
4299
  BSONRuntimeError: BSONRuntimeError,
4281
4300
  BSONSymbol: BSONSymbol,
@@ -4304,5 +4323,5 @@ var bson = /*#__PURE__*/Object.freeze({
4304
4323
  setInternalBufferSize: setInternalBufferSize
4305
4324
  });
4306
4325
 
4307
- export { bson as BSON, BSONError, BSONRegExp, BSONRuntimeError, BSONSymbol, BSONType, BSONValue, BSONVersionError, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, onDemand, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
4326
+ export { bson as BSON, BSONError, BSONOffsetError, BSONRegExp, BSONRuntimeError, BSONSymbol, BSONType, BSONValue, BSONVersionError, Binary, Code, DBRef, Decimal128, Double, EJSON, Int32, Long, MaxKey, MinKey, ObjectId, Timestamp, UUID, calculateObjectSize, deserialize, deserializeStream, onDemand, serialize, serializeWithBufferAndIndex, setInternalBufferSize };
4308
4327
  //# sourceMappingURL=bson.mjs.map