bson 6.4.0 → 6.5.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.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;
@@ -2140,6 +2151,8 @@ class MinKey extends BSONValue {
2140
2151
 
2141
2152
  const FLOAT = new Float64Array(1);
2142
2153
  const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
2154
+ FLOAT[0] = -1;
2155
+ const isBigEndian = FLOAT_BYTES[7] === 0;
2143
2156
  const NumberUtils = {
2144
2157
  getInt32LE(source, offset) {
2145
2158
  return (source[offset] |
@@ -2164,17 +2177,29 @@ const NumberUtils = {
2164
2177
  const hi = NumberUtils.getUint32LE(source, offset + 4);
2165
2178
  return (BigInt(hi) << BigInt(32)) + BigInt(lo);
2166
2179
  },
2167
- getFloat64LE(source, offset) {
2168
- FLOAT_BYTES[0] = source[offset];
2169
- FLOAT_BYTES[1] = source[offset + 1];
2170
- FLOAT_BYTES[2] = source[offset + 2];
2171
- FLOAT_BYTES[3] = source[offset + 3];
2172
- FLOAT_BYTES[4] = source[offset + 4];
2173
- FLOAT_BYTES[5] = source[offset + 5];
2174
- FLOAT_BYTES[6] = source[offset + 6];
2175
- FLOAT_BYTES[7] = source[offset + 7];
2176
- return FLOAT[0];
2177
- },
2180
+ getFloat64LE: isBigEndian
2181
+ ? (source, offset) => {
2182
+ FLOAT_BYTES[7] = source[offset];
2183
+ FLOAT_BYTES[6] = source[offset + 1];
2184
+ FLOAT_BYTES[5] = source[offset + 2];
2185
+ FLOAT_BYTES[4] = source[offset + 3];
2186
+ FLOAT_BYTES[3] = source[offset + 4];
2187
+ FLOAT_BYTES[2] = source[offset + 5];
2188
+ FLOAT_BYTES[1] = source[offset + 6];
2189
+ FLOAT_BYTES[0] = source[offset + 7];
2190
+ return FLOAT[0];
2191
+ }
2192
+ : (source, offset) => {
2193
+ FLOAT_BYTES[0] = source[offset];
2194
+ FLOAT_BYTES[1] = source[offset + 1];
2195
+ FLOAT_BYTES[2] = source[offset + 2];
2196
+ FLOAT_BYTES[3] = source[offset + 3];
2197
+ FLOAT_BYTES[4] = source[offset + 4];
2198
+ FLOAT_BYTES[5] = source[offset + 5];
2199
+ FLOAT_BYTES[6] = source[offset + 6];
2200
+ FLOAT_BYTES[7] = source[offset + 7];
2201
+ return FLOAT[0];
2202
+ },
2178
2203
  setInt32BE(destination, offset, value) {
2179
2204
  destination[offset + 3] = value;
2180
2205
  value >>>= 8;
@@ -2215,18 +2240,31 @@ const NumberUtils = {
2215
2240
  destination[offset + 7] = hi;
2216
2241
  return 8;
2217
2242
  },
2218
- setFloat64LE(destination, offset, value) {
2219
- FLOAT[0] = value;
2220
- destination[offset] = FLOAT_BYTES[0];
2221
- destination[offset + 1] = FLOAT_BYTES[1];
2222
- destination[offset + 2] = FLOAT_BYTES[2];
2223
- destination[offset + 3] = FLOAT_BYTES[3];
2224
- destination[offset + 4] = FLOAT_BYTES[4];
2225
- destination[offset + 5] = FLOAT_BYTES[5];
2226
- destination[offset + 6] = FLOAT_BYTES[6];
2227
- destination[offset + 7] = FLOAT_BYTES[7];
2228
- return 8;
2229
- }
2243
+ setFloat64LE: isBigEndian
2244
+ ? (destination, offset, value) => {
2245
+ FLOAT[0] = value;
2246
+ destination[offset] = FLOAT_BYTES[7];
2247
+ destination[offset + 1] = FLOAT_BYTES[6];
2248
+ destination[offset + 2] = FLOAT_BYTES[5];
2249
+ destination[offset + 3] = FLOAT_BYTES[4];
2250
+ destination[offset + 4] = FLOAT_BYTES[3];
2251
+ destination[offset + 5] = FLOAT_BYTES[2];
2252
+ destination[offset + 6] = FLOAT_BYTES[1];
2253
+ destination[offset + 7] = FLOAT_BYTES[0];
2254
+ return 8;
2255
+ }
2256
+ : (destination, offset, value) => {
2257
+ FLOAT[0] = value;
2258
+ destination[offset] = FLOAT_BYTES[0];
2259
+ destination[offset + 1] = FLOAT_BYTES[1];
2260
+ destination[offset + 2] = FLOAT_BYTES[2];
2261
+ destination[offset + 3] = FLOAT_BYTES[3];
2262
+ destination[offset + 4] = FLOAT_BYTES[4];
2263
+ destination[offset + 5] = FLOAT_BYTES[5];
2264
+ destination[offset + 6] = FLOAT_BYTES[6];
2265
+ destination[offset + 7] = FLOAT_BYTES[7];
2266
+ return 8;
2267
+ }
2230
2268
  };
2231
2269
 
2232
2270
  const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
@@ -4104,6 +4142,104 @@ EJSON.serialize = EJSONserialize;
4104
4142
  EJSON.deserialize = EJSONdeserialize;
4105
4143
  Object.freeze(EJSON);
4106
4144
 
4145
+ function getSize(source, offset) {
4146
+ if (source[offset + 3] > 127) {
4147
+ throw new BSONOffsetError('BSON size cannot be negative', offset);
4148
+ }
4149
+ return (source[offset] |
4150
+ (source[offset + 1] << 8) |
4151
+ (source[offset + 2] << 16) |
4152
+ (source[offset + 3] << 24));
4153
+ }
4154
+ function findNull(bytes, offset) {
4155
+ let nullTerminatorOffset = offset;
4156
+ for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++)
4157
+ ;
4158
+ if (nullTerminatorOffset === bytes.length - 1) {
4159
+ throw new BSONOffsetError('Null terminator not found', offset);
4160
+ }
4161
+ return nullTerminatorOffset;
4162
+ }
4163
+ function parseToElements(bytes, startOffset = 0) {
4164
+ if (bytes.length < 5) {
4165
+ throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
4166
+ }
4167
+ const documentSize = getSize(bytes, startOffset);
4168
+ if (documentSize > bytes.length - startOffset) {
4169
+ throw new BSONOffsetError(`Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`, startOffset);
4170
+ }
4171
+ if (bytes[startOffset + documentSize - 1] !== 0x00) {
4172
+ throw new BSONOffsetError('BSON documents must end in 0x00', startOffset + documentSize);
4173
+ }
4174
+ const elements = [];
4175
+ let offset = startOffset + 4;
4176
+ while (offset <= documentSize + startOffset) {
4177
+ const type = bytes[offset];
4178
+ offset += 1;
4179
+ if (type === 0) {
4180
+ if (offset - startOffset !== documentSize) {
4181
+ throw new BSONOffsetError(`Invalid 0x00 type byte`, offset);
4182
+ }
4183
+ break;
4184
+ }
4185
+ const nameOffset = offset;
4186
+ const nameLength = findNull(bytes, offset) - nameOffset;
4187
+ offset += nameLength + 1;
4188
+ let length;
4189
+ if (type === 1 || type === 18 || type === 9 || type === 17) {
4190
+ length = 8;
4191
+ }
4192
+ else if (type === 16) {
4193
+ length = 4;
4194
+ }
4195
+ else if (type === 7) {
4196
+ length = 12;
4197
+ }
4198
+ else if (type === 19) {
4199
+ length = 16;
4200
+ }
4201
+ else if (type === 8) {
4202
+ length = 1;
4203
+ }
4204
+ else if (type === 10 || type === 6 || type === 127 || type === 255) {
4205
+ length = 0;
4206
+ }
4207
+ else if (type === 11) {
4208
+ length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
4209
+ }
4210
+ else if (type === 3 || type === 4 || type === 15) {
4211
+ length = getSize(bytes, offset);
4212
+ }
4213
+ else if (type === 2 ||
4214
+ type === 5 ||
4215
+ type === 12 ||
4216
+ type === 13 ||
4217
+ type === 14) {
4218
+ length = getSize(bytes, offset) + 4;
4219
+ if (type === 5) {
4220
+ length += 1;
4221
+ }
4222
+ if (type === 12) {
4223
+ length += 12;
4224
+ }
4225
+ }
4226
+ else {
4227
+ throw new BSONOffsetError(`Invalid 0x${type.toString(16).padStart(2, '0')} type byte`, offset);
4228
+ }
4229
+ if (length > documentSize) {
4230
+ throw new BSONOffsetError('value reports length larger than document', offset);
4231
+ }
4232
+ elements.push([type, nameOffset, nameLength, offset, length]);
4233
+ offset += length;
4234
+ }
4235
+ return elements;
4236
+ }
4237
+
4238
+ const onDemand = Object.create(null);
4239
+ onDemand.parseToElements = parseToElements;
4240
+ onDemand.BSONOffsetError = BSONOffsetError;
4241
+ Object.freeze(onDemand);
4242
+
4107
4243
  const MAXSIZE = 1024 * 1024 * 17;
4108
4244
  let buffer = ByteUtils.allocate(MAXSIZE);
4109
4245
  function setInternalBufferSize(size) {
@@ -4180,6 +4316,7 @@ var bson = /*#__PURE__*/Object.freeze({
4180
4316
  calculateObjectSize: calculateObjectSize,
4181
4317
  deserialize: deserialize,
4182
4318
  deserializeStream: deserializeStream,
4319
+ onDemand: onDemand,
4183
4320
  serialize: serialize,
4184
4321
  serializeWithBufferAndIndex: serializeWithBufferAndIndex,
4185
4322
  setInternalBufferSize: setInternalBufferSize
@@ -4209,6 +4346,7 @@ exports.UUID = UUID;
4209
4346
  exports.calculateObjectSize = calculateObjectSize;
4210
4347
  exports.deserialize = deserialize;
4211
4348
  exports.deserializeStream = deserializeStream;
4349
+ exports.onDemand = onDemand;
4212
4350
  exports.serialize = serialize;
4213
4351
  exports.serializeWithBufferAndIndex = serializeWithBufferAndIndex;
4214
4352
  exports.setInternalBufferSize = setInternalBufferSize;