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.rn.cjs CHANGED
@@ -78,6 +78,7 @@ const BSON_BINARY_SUBTYPE_UUID_NEW = 4;
78
78
  const BSON_BINARY_SUBTYPE_MD5 = 5;
79
79
  const BSON_BINARY_SUBTYPE_ENCRYPTED = 6;
80
80
  const BSON_BINARY_SUBTYPE_COLUMN = 7;
81
+ const BSON_BINARY_SUBTYPE_SENSITIVE = 8;
81
82
  const BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
82
83
  const BSONType = Object.freeze({
83
84
  double: 1,
@@ -139,6 +140,15 @@ class BSONRuntimeError extends BSONError {
139
140
  super(message);
140
141
  }
141
142
  }
143
+ class BSONOffsetError extends BSONError {
144
+ get name() {
145
+ return 'BSONOffsetError';
146
+ }
147
+ constructor(message, offset) {
148
+ super(`${message}. offset: ${offset}`);
149
+ this.offset = offset;
150
+ }
151
+ }
142
152
 
143
153
  const FIRST_BIT = 0x80;
144
154
  const FIRST_TWO_BITS = 0xc0;
@@ -622,6 +632,7 @@ Binary.SUBTYPE_UUID = 4;
622
632
  Binary.SUBTYPE_MD5 = 5;
623
633
  Binary.SUBTYPE_ENCRYPTED = 6;
624
634
  Binary.SUBTYPE_COLUMN = 7;
635
+ Binary.SUBTYPE_SENSITIVE = 8;
625
636
  Binary.SUBTYPE_USER_DEFINED = 128;
626
637
  const UUID_BYTE_LENGTH = 16;
627
638
  const UUID_WITHOUT_DASHES = /^[0-9A-F]{32}$/i;
@@ -4137,6 +4148,104 @@ EJSON.serialize = EJSONserialize;
4137
4148
  EJSON.deserialize = EJSONdeserialize;
4138
4149
  Object.freeze(EJSON);
4139
4150
 
4151
+ function getSize(source, offset) {
4152
+ if (source[offset + 3] > 127) {
4153
+ throw new BSONOffsetError('BSON size cannot be negative', offset);
4154
+ }
4155
+ return (source[offset] |
4156
+ (source[offset + 1] << 8) |
4157
+ (source[offset + 2] << 16) |
4158
+ (source[offset + 3] << 24));
4159
+ }
4160
+ function findNull(bytes, offset) {
4161
+ let nullTerminatorOffset = offset;
4162
+ for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++)
4163
+ ;
4164
+ if (nullTerminatorOffset === bytes.length - 1) {
4165
+ throw new BSONOffsetError('Null terminator not found', offset);
4166
+ }
4167
+ return nullTerminatorOffset;
4168
+ }
4169
+ function parseToElements(bytes, startOffset = 0) {
4170
+ if (bytes.length < 5) {
4171
+ throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4172
+ }
4173
+ const documentSize = getSize(bytes, startOffset);
4174
+ if (documentSize > bytes.length - startOffset) {
4175
+ throw new BSONOffsetError(`Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`, startOffset);
4176
+ }
4177
+ if (bytes[startOffset + documentSize - 1] !== 0x00) {
4178
+ throw new BSONOffsetError('BSON documents must end in 0x00', startOffset + documentSize);
4179
+ }
4180
+ const elements = [];
4181
+ let offset = startOffset + 4;
4182
+ while (offset <= documentSize + startOffset) {
4183
+ const type = bytes[offset];
4184
+ offset += 1;
4185
+ if (type === 0) {
4186
+ if (offset - startOffset !== documentSize) {
4187
+ throw new BSONOffsetError(`Invalid 0x00 type byte`, offset);
4188
+ }
4189
+ break;
4190
+ }
4191
+ const nameOffset = offset;
4192
+ const nameLength = findNull(bytes, offset) - nameOffset;
4193
+ offset += nameLength + 1;
4194
+ let length;
4195
+ if (type === 1 || type === 18 || type === 9 || type === 17) {
4196
+ length = 8;
4197
+ }
4198
+ else if (type === 16) {
4199
+ length = 4;
4200
+ }
4201
+ else if (type === 7) {
4202
+ length = 12;
4203
+ }
4204
+ else if (type === 19) {
4205
+ length = 16;
4206
+ }
4207
+ else if (type === 8) {
4208
+ length = 1;
4209
+ }
4210
+ else if (type === 10 || type === 6 || type === 127 || type === 255) {
4211
+ length = 0;
4212
+ }
4213
+ else if (type === 11) {
4214
+ length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4215
+ }
4216
+ else if (type === 3 || type === 4 || type === 15) {
4217
+ length = getSize(bytes, offset);
4218
+ }
4219
+ else if (type === 2 ||
4220
+ type === 5 ||
4221
+ type === 12 ||
4222
+ type === 13 ||
4223
+ type === 14) {
4224
+ length = getSize(bytes, offset) + 4;
4225
+ if (type === 5) {
4226
+ length += 1;
4227
+ }
4228
+ if (type === 12) {
4229
+ length += 12;
4230
+ }
4231
+ }
4232
+ else {
4233
+ throw new BSONOffsetError(`Invalid 0x${type.toString(16).padStart(2, '0')} type byte`, offset);
4234
+ }
4235
+ if (length > documentSize) {
4236
+ throw new BSONOffsetError('value reports length larger than document', offset);
4237
+ }
4238
+ elements.push([type, nameOffset, nameLength, offset, length]);
4239
+ offset += length;
4240
+ }
4241
+ return elements;
4242
+ }
4243
+
4244
+ const onDemand = Object.create(null);
4245
+ onDemand.parseToElements = parseToElements;
4246
+ onDemand.BSONOffsetError = BSONOffsetError;
4247
+ Object.freeze(onDemand);
4248
+
4140
4249
  const MAXSIZE = 1024 * 1024 * 17;
4141
4250
  let buffer = ByteUtils.allocate(MAXSIZE);
4142
4251
  function setInternalBufferSize(size) {
@@ -4213,6 +4322,7 @@ var bson = /*#__PURE__*/Object.freeze({
4213
4322
  calculateObjectSize: calculateObjectSize,
4214
4323
  deserialize: deserialize,
4215
4324
  deserializeStream: deserializeStream,
4325
+ onDemand: onDemand,
4216
4326
  serialize: serialize,
4217
4327
  serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4218
4328
  setInternalBufferSize: setInternalBufferSize
@@ -4242,6 +4352,7 @@ exports.UUID = UUID;
4242
4352
  exports.calculateObjectSize = calculateObjectSize;
4243
4353
  exports.deserialize = deserialize;
4244
4354
  exports.deserializeStream = deserializeStream;
4355
+ exports.onDemand = onDemand;
4245
4356
  exports.serialize = serialize;
4246
4357
  exports.serializeWithBufferAndIndex = serializeWithBufferAndIndex;
4247
4358
  exports.setInternalBufferSize = setInternalBufferSize;