bson 6.4.1 → 6.5.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.cjs CHANGED
@@ -126,6 +126,15 @@ class BSONRuntimeError extends BSONError {
126
126
  super(message);
127
127
  }
128
128
  }
129
+ class BSONOffsetError extends BSONError {
130
+ get name() {
131
+ return 'BSONOffsetError';
132
+ }
133
+ constructor(message, offset) {
134
+ super(`${message}. offset: ${offset}`);
135
+ this.offset = offset;
136
+ }
137
+ }
129
138
 
130
139
  const FIRST_BIT = 0x80;
131
140
  const FIRST_TWO_BITS = 0xc0;
@@ -607,6 +616,7 @@ Binary.SUBTYPE_UUID = 4;
607
616
  Binary.SUBTYPE_MD5 = 5;
608
617
  Binary.SUBTYPE_ENCRYPTED = 6;
609
618
  Binary.SUBTYPE_COLUMN = 7;
619
+ Binary.SUBTYPE_SENSITIVE = 8;
610
620
  Binary.SUBTYPE_USER_DEFINED = 128;
611
621
  const UUID_BYTE_LENGTH = 16;
612
622
  const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
@@ -4122,6 +4132,104 @@ EJSON.serialize = EJSONserialize;
4122
4132
  EJSON.deserialize = EJSONdeserialize;
4123
4133
  Object.freeze(EJSON);
4124
4134
 
4135
+ function getSize(source, offset) {
4136
+ if (source[offset + 3] > 127) {
4137
+ throw new BSONOffsetError('BSON size cannot be negative', offset);
4138
+ }
4139
+ return (source[offset] |
4140
+ (source[offset + 1] << 8) |
4141
+ (source[offset + 2] << 16) |
4142
+ (source[offset + 3] << 24));
4143
+ }
4144
+ function findNull(bytes, offset) {
4145
+ let nullTerminatorOffset = offset;
4146
+ for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++)
4147
+ ;
4148
+ if (nullTerminatorOffset === bytes.length - 1) {
4149
+ throw new BSONOffsetError('Null terminator not found', offset);
4150
+ }
4151
+ return nullTerminatorOffset;
4152
+ }
4153
+ function parseToElements(bytes, startOffset = 0) {
4154
+ if (bytes.length < 5) {
4155
+ throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4156
+ }
4157
+ const documentSize = getSize(bytes, startOffset);
4158
+ if (documentSize > bytes.length - startOffset) {
4159
+ throw new BSONOffsetError(`Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`, startOffset);
4160
+ }
4161
+ if (bytes[startOffset + documentSize - 1] !== 0x00) {
4162
+ throw new BSONOffsetError('BSON documents must end in 0x00', startOffset + documentSize);
4163
+ }
4164
+ const elements = [];
4165
+ let offset = startOffset + 4;
4166
+ while (offset <= documentSize + startOffset) {
4167
+ const type = bytes[offset];
4168
+ offset += 1;
4169
+ if (type === 0) {
4170
+ if (offset - startOffset !== documentSize) {
4171
+ throw new BSONOffsetError(`Invalid 0x00 type byte`, offset);
4172
+ }
4173
+ break;
4174
+ }
4175
+ const nameOffset = offset;
4176
+ const nameLength = findNull(bytes, offset) - nameOffset;
4177
+ offset += nameLength + 1;
4178
+ let length;
4179
+ if (type === 1 || type === 18 || type === 9 || type === 17) {
4180
+ length = 8;
4181
+ }
4182
+ else if (type === 16) {
4183
+ length = 4;
4184
+ }
4185
+ else if (type === 7) {
4186
+ length = 12;
4187
+ }
4188
+ else if (type === 19) {
4189
+ length = 16;
4190
+ }
4191
+ else if (type === 8) {
4192
+ length = 1;
4193
+ }
4194
+ else if (type === 10 || type === 6 || type === 127 || type === 255) {
4195
+ length = 0;
4196
+ }
4197
+ else if (type === 11) {
4198
+ length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4199
+ }
4200
+ else if (type === 3 || type === 4 || type === 15) {
4201
+ length = getSize(bytes, offset);
4202
+ }
4203
+ else if (type === 2 ||
4204
+ type === 5 ||
4205
+ type === 12 ||
4206
+ type === 13 ||
4207
+ type === 14) {
4208
+ length = getSize(bytes, offset) + 4;
4209
+ if (type === 5) {
4210
+ length += 1;
4211
+ }
4212
+ if (type === 12) {
4213
+ length += 12;
4214
+ }
4215
+ }
4216
+ else {
4217
+ throw new BSONOffsetError(`Invalid 0x${type.toString(16).padStart(2, '0')} type byte`, offset);
4218
+ }
4219
+ if (length > documentSize) {
4220
+ throw new BSONOffsetError('value reports length larger than document', offset);
4221
+ }
4222
+ elements.push([type, nameOffset, nameLength, offset, length]);
4223
+ offset += length;
4224
+ }
4225
+ return elements;
4226
+ }
4227
+
4228
+ const onDemand = Object.create(null);
4229
+ onDemand.parseToElements = parseToElements;
4230
+ onDemand.BSONOffsetError = BSONOffsetError;
4231
+ Object.freeze(onDemand);
4232
+
4125
4233
  const MAXSIZE = 1024 * 1024 * 17;
4126
4234
  let buffer = ByteUtils.allocate(MAXSIZE);
4127
4235
  function setInternalBufferSize(size) {
@@ -4198,6 +4306,7 @@ var bson = /*#__PURE__*/Object.freeze({
4198
4306
  calculateObjectSize: calculateObjectSize,
4199
4307
  deserialize: deserialize,
4200
4308
  deserializeStream: deserializeStream,
4309
+ onDemand: onDemand,
4201
4310
  serialize: serialize,
4202
4311
  serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4203
4312
  setInternalBufferSize: setInternalBufferSize
@@ -4227,6 +4336,7 @@ exports.UUID = UUID;
4227
4336
  exports.calculateObjectSize = calculateObjectSize;
4228
4337
  exports.deserialize = deserialize;
4229
4338
  exports.deserializeStream = deserializeStream;
4339
+ exports.onDemand = onDemand;
4230
4340
  exports.serialize = serialize;
4231
4341
  exports.serializeWithBufferAndIndex = serializeWithBufferAndIndex;
4232
4342
  exports.setInternalBufferSize = setInternalBufferSize;