bson 6.4.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/bson.d.ts +125 -0
- package/lib/bson.bundle.js +185 -28
- package/lib/bson.bundle.js.map +1 -1
- package/lib/bson.cjs +185 -28
- package/lib/bson.cjs.map +1 -1
- package/lib/bson.mjs +184 -29
- package/lib/bson.mjs.map +1 -1
- package/lib/bson.rn.cjs +186 -28
- package/lib/bson.rn.cjs.map +1 -1
- package/package.json +1 -1
- package/src/binary.ts +7 -5
- package/src/bson.ts +2 -1
- package/src/constants.ts +3 -0
- package/src/error.ts +22 -0
- package/src/parser/on_demand/index.ts +32 -0
- package/src/parser/on_demand/parse_to_elements.ts +188 -0
- package/src/utils/byte_utils.ts +10 -4
- package/src/utils/number_utils.ts +90 -25
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, options) {
|
|
148
|
+
super(`${message}. offset: ${offset}`, options);
|
|
149
|
+
this.offset = offset;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
142
152
|
|
|
143
153
|
const FIRST_BIT = 0x80;
|
|
144
154
|
const FIRST_TWO_BITS = 0xc0;
|
|
@@ -539,16 +549,16 @@ class Binary extends BSONValue {
|
|
|
539
549
|
return this.position;
|
|
540
550
|
}
|
|
541
551
|
toJSON() {
|
|
542
|
-
return ByteUtils.toBase64(this.buffer);
|
|
552
|
+
return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
|
|
543
553
|
}
|
|
544
554
|
toString(encoding) {
|
|
545
555
|
if (encoding === 'hex')
|
|
546
|
-
return ByteUtils.toHex(this.buffer);
|
|
556
|
+
return ByteUtils.toHex(this.buffer.subarray(0, this.position));
|
|
547
557
|
if (encoding === 'base64')
|
|
548
|
-
return ByteUtils.toBase64(this.buffer);
|
|
558
|
+
return ByteUtils.toBase64(this.buffer.subarray(0, this.position));
|
|
549
559
|
if (encoding === 'utf8' || encoding === 'utf-8')
|
|
550
|
-
return ByteUtils.toUTF8(this.buffer, 0, this.
|
|
551
|
-
return ByteUtils.toUTF8(this.buffer, 0, this.
|
|
560
|
+
return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
|
|
561
|
+
return ByteUtils.toUTF8(this.buffer, 0, this.position, false);
|
|
552
562
|
}
|
|
553
563
|
toExtendedJSON(options) {
|
|
554
564
|
options = options || {};
|
|
@@ -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,7 +2151,18 @@ 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 = {
|
|
2157
|
+
getNonnegativeInt32LE(source, offset) {
|
|
2158
|
+
if (source[offset + 3] > 127) {
|
|
2159
|
+
throw new RangeError(`Size cannot be negative at offset: ${offset}`);
|
|
2160
|
+
}
|
|
2161
|
+
return (source[offset] |
|
|
2162
|
+
(source[offset + 1] << 8) |
|
|
2163
|
+
(source[offset + 2] << 16) |
|
|
2164
|
+
(source[offset + 3] << 24));
|
|
2165
|
+
},
|
|
2144
2166
|
getInt32LE(source, offset) {
|
|
2145
2167
|
return (source[offset] |
|
|
2146
2168
|
(source[offset + 1] << 8) |
|
|
@@ -2164,17 +2186,29 @@ const NumberUtils = {
|
|
|
2164
2186
|
const hi = NumberUtils.getUint32LE(source, offset + 4);
|
|
2165
2187
|
return (BigInt(hi) << BigInt(32)) + BigInt(lo);
|
|
2166
2188
|
},
|
|
2167
|
-
getFloat64LE
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2189
|
+
getFloat64LE: isBigEndian
|
|
2190
|
+
? (source, offset) => {
|
|
2191
|
+
FLOAT_BYTES[7] = source[offset];
|
|
2192
|
+
FLOAT_BYTES[6] = source[offset + 1];
|
|
2193
|
+
FLOAT_BYTES[5] = source[offset + 2];
|
|
2194
|
+
FLOAT_BYTES[4] = source[offset + 3];
|
|
2195
|
+
FLOAT_BYTES[3] = source[offset + 4];
|
|
2196
|
+
FLOAT_BYTES[2] = source[offset + 5];
|
|
2197
|
+
FLOAT_BYTES[1] = source[offset + 6];
|
|
2198
|
+
FLOAT_BYTES[0] = source[offset + 7];
|
|
2199
|
+
return FLOAT[0];
|
|
2200
|
+
}
|
|
2201
|
+
: (source, offset) => {
|
|
2202
|
+
FLOAT_BYTES[0] = source[offset];
|
|
2203
|
+
FLOAT_BYTES[1] = source[offset + 1];
|
|
2204
|
+
FLOAT_BYTES[2] = source[offset + 2];
|
|
2205
|
+
FLOAT_BYTES[3] = source[offset + 3];
|
|
2206
|
+
FLOAT_BYTES[4] = source[offset + 4];
|
|
2207
|
+
FLOAT_BYTES[5] = source[offset + 5];
|
|
2208
|
+
FLOAT_BYTES[6] = source[offset + 6];
|
|
2209
|
+
FLOAT_BYTES[7] = source[offset + 7];
|
|
2210
|
+
return FLOAT[0];
|
|
2211
|
+
},
|
|
2178
2212
|
setInt32BE(destination, offset, value) {
|
|
2179
2213
|
destination[offset + 3] = value;
|
|
2180
2214
|
value >>>= 8;
|
|
@@ -2215,18 +2249,31 @@ const NumberUtils = {
|
|
|
2215
2249
|
destination[offset + 7] = hi;
|
|
2216
2250
|
return 8;
|
|
2217
2251
|
},
|
|
2218
|
-
setFloat64LE
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2252
|
+
setFloat64LE: isBigEndian
|
|
2253
|
+
? (destination, offset, value) => {
|
|
2254
|
+
FLOAT[0] = value;
|
|
2255
|
+
destination[offset] = FLOAT_BYTES[7];
|
|
2256
|
+
destination[offset + 1] = FLOAT_BYTES[6];
|
|
2257
|
+
destination[offset + 2] = FLOAT_BYTES[5];
|
|
2258
|
+
destination[offset + 3] = FLOAT_BYTES[4];
|
|
2259
|
+
destination[offset + 4] = FLOAT_BYTES[3];
|
|
2260
|
+
destination[offset + 5] = FLOAT_BYTES[2];
|
|
2261
|
+
destination[offset + 6] = FLOAT_BYTES[1];
|
|
2262
|
+
destination[offset + 7] = FLOAT_BYTES[0];
|
|
2263
|
+
return 8;
|
|
2264
|
+
}
|
|
2265
|
+
: (destination, offset, value) => {
|
|
2266
|
+
FLOAT[0] = value;
|
|
2267
|
+
destination[offset] = FLOAT_BYTES[0];
|
|
2268
|
+
destination[offset + 1] = FLOAT_BYTES[1];
|
|
2269
|
+
destination[offset + 2] = FLOAT_BYTES[2];
|
|
2270
|
+
destination[offset + 3] = FLOAT_BYTES[3];
|
|
2271
|
+
destination[offset + 4] = FLOAT_BYTES[4];
|
|
2272
|
+
destination[offset + 5] = FLOAT_BYTES[5];
|
|
2273
|
+
destination[offset + 6] = FLOAT_BYTES[6];
|
|
2274
|
+
destination[offset + 7] = FLOAT_BYTES[7];
|
|
2275
|
+
return 8;
|
|
2276
|
+
}
|
|
2230
2277
|
};
|
|
2231
2278
|
|
|
2232
2279
|
const checkForHexRegExp = new RegExp('^[0-9a-fA-F]{24}$');
|
|
@@ -4104,6 +4151,113 @@ EJSON.serialize = EJSONserialize;
|
|
|
4104
4151
|
EJSON.deserialize = EJSONdeserialize;
|
|
4105
4152
|
Object.freeze(EJSON);
|
|
4106
4153
|
|
|
4154
|
+
function getSize(source, offset) {
|
|
4155
|
+
try {
|
|
4156
|
+
return NumberUtils.getNonnegativeInt32LE(source, offset);
|
|
4157
|
+
}
|
|
4158
|
+
catch (cause) {
|
|
4159
|
+
throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
|
|
4160
|
+
}
|
|
4161
|
+
}
|
|
4162
|
+
function findNull(bytes, offset) {
|
|
4163
|
+
let nullTerminatorOffset = offset;
|
|
4164
|
+
for (; bytes[nullTerminatorOffset] !== 0x00; nullTerminatorOffset++)
|
|
4165
|
+
;
|
|
4166
|
+
if (nullTerminatorOffset === bytes.length - 1) {
|
|
4167
|
+
throw new BSONOffsetError('Null terminator not found', offset);
|
|
4168
|
+
}
|
|
4169
|
+
return nullTerminatorOffset;
|
|
4170
|
+
}
|
|
4171
|
+
function parseToElements(bytes, startOffset = 0) {
|
|
4172
|
+
startOffset ??= 0;
|
|
4173
|
+
if (bytes.length < 5) {
|
|
4174
|
+
throw new BSONOffsetError(`Input must be at least 5 bytes, got ${bytes.length} bytes`, startOffset);
|
|
4175
|
+
}
|
|
4176
|
+
const documentSize = getSize(bytes, startOffset);
|
|
4177
|
+
if (documentSize > bytes.length - startOffset) {
|
|
4178
|
+
throw new BSONOffsetError(`Parsed documentSize (${documentSize} bytes) does not match input length (${bytes.length} bytes)`, startOffset);
|
|
4179
|
+
}
|
|
4180
|
+
if (bytes[startOffset + documentSize - 1] !== 0x00) {
|
|
4181
|
+
throw new BSONOffsetError('BSON documents must end in 0x00', startOffset + documentSize);
|
|
4182
|
+
}
|
|
4183
|
+
const elements = [];
|
|
4184
|
+
let offset = startOffset + 4;
|
|
4185
|
+
while (offset <= documentSize + startOffset) {
|
|
4186
|
+
const type = bytes[offset];
|
|
4187
|
+
offset += 1;
|
|
4188
|
+
if (type === 0) {
|
|
4189
|
+
if (offset - startOffset !== documentSize) {
|
|
4190
|
+
throw new BSONOffsetError(`Invalid 0x00 type byte`, offset);
|
|
4191
|
+
}
|
|
4192
|
+
break;
|
|
4193
|
+
}
|
|
4194
|
+
const nameOffset = offset;
|
|
4195
|
+
const nameLength = findNull(bytes, offset) - nameOffset;
|
|
4196
|
+
offset += nameLength + 1;
|
|
4197
|
+
let length;
|
|
4198
|
+
if (type === 1 ||
|
|
4199
|
+
type === 18 ||
|
|
4200
|
+
type === 9 ||
|
|
4201
|
+
type === 17) {
|
|
4202
|
+
length = 8;
|
|
4203
|
+
}
|
|
4204
|
+
else if (type === 16) {
|
|
4205
|
+
length = 4;
|
|
4206
|
+
}
|
|
4207
|
+
else if (type === 7) {
|
|
4208
|
+
length = 12;
|
|
4209
|
+
}
|
|
4210
|
+
else if (type === 19) {
|
|
4211
|
+
length = 16;
|
|
4212
|
+
}
|
|
4213
|
+
else if (type === 8) {
|
|
4214
|
+
length = 1;
|
|
4215
|
+
}
|
|
4216
|
+
else if (type === 10 ||
|
|
4217
|
+
type === 6 ||
|
|
4218
|
+
type === 127 ||
|
|
4219
|
+
type === 255) {
|
|
4220
|
+
length = 0;
|
|
4221
|
+
}
|
|
4222
|
+
else if (type === 11) {
|
|
4223
|
+
length = findNull(bytes, findNull(bytes, offset) + 1) + 1 - offset;
|
|
4224
|
+
}
|
|
4225
|
+
else if (type === 3 ||
|
|
4226
|
+
type === 4 ||
|
|
4227
|
+
type === 15) {
|
|
4228
|
+
length = getSize(bytes, offset);
|
|
4229
|
+
}
|
|
4230
|
+
else if (type === 2 ||
|
|
4231
|
+
type === 5 ||
|
|
4232
|
+
type === 12 ||
|
|
4233
|
+
type === 13 ||
|
|
4234
|
+
type === 14) {
|
|
4235
|
+
length = getSize(bytes, offset) + 4;
|
|
4236
|
+
if (type === 5) {
|
|
4237
|
+
length += 1;
|
|
4238
|
+
}
|
|
4239
|
+
if (type === 12) {
|
|
4240
|
+
length += 12;
|
|
4241
|
+
}
|
|
4242
|
+
}
|
|
4243
|
+
else {
|
|
4244
|
+
throw new BSONOffsetError(`Invalid 0x${type.toString(16).padStart(2, '0')} type byte`, offset);
|
|
4245
|
+
}
|
|
4246
|
+
if (length > documentSize) {
|
|
4247
|
+
throw new BSONOffsetError('value reports length larger than document', offset);
|
|
4248
|
+
}
|
|
4249
|
+
elements.push([type, nameOffset, nameLength, offset, length]);
|
|
4250
|
+
offset += length;
|
|
4251
|
+
}
|
|
4252
|
+
return elements;
|
|
4253
|
+
}
|
|
4254
|
+
|
|
4255
|
+
const onDemand = Object.create(null);
|
|
4256
|
+
onDemand.parseToElements = parseToElements;
|
|
4257
|
+
onDemand.ByteUtils = ByteUtils;
|
|
4258
|
+
onDemand.NumberUtils = NumberUtils;
|
|
4259
|
+
Object.freeze(onDemand);
|
|
4260
|
+
|
|
4107
4261
|
const MAXSIZE = 1024 * 1024 * 17;
|
|
4108
4262
|
let buffer = ByteUtils.allocate(MAXSIZE);
|
|
4109
4263
|
function setInternalBufferSize(size) {
|
|
@@ -4158,6 +4312,7 @@ function deserializeStream(data, startIndex, numberOfDocuments, documents, docSt
|
|
|
4158
4312
|
var bson = /*#__PURE__*/Object.freeze({
|
|
4159
4313
|
__proto__: null,
|
|
4160
4314
|
BSONError: BSONError,
|
|
4315
|
+
BSONOffsetError: BSONOffsetError,
|
|
4161
4316
|
BSONRegExp: BSONRegExp,
|
|
4162
4317
|
BSONRuntimeError: BSONRuntimeError,
|
|
4163
4318
|
BSONSymbol: BSONSymbol,
|
|
@@ -4180,6 +4335,7 @@ var bson = /*#__PURE__*/Object.freeze({
|
|
|
4180
4335
|
calculateObjectSize: calculateObjectSize,
|
|
4181
4336
|
deserialize: deserialize,
|
|
4182
4337
|
deserializeStream: deserializeStream,
|
|
4338
|
+
onDemand: onDemand,
|
|
4183
4339
|
serialize: serialize,
|
|
4184
4340
|
serializeWithBufferAndIndex: serializeWithBufferAndIndex,
|
|
4185
4341
|
setInternalBufferSize: setInternalBufferSize
|
|
@@ -4187,6 +4343,7 @@ var bson = /*#__PURE__*/Object.freeze({
|
|
|
4187
4343
|
|
|
4188
4344
|
exports.BSON = bson;
|
|
4189
4345
|
exports.BSONError = BSONError;
|
|
4346
|
+
exports.BSONOffsetError = BSONOffsetError;
|
|
4190
4347
|
exports.BSONRegExp = BSONRegExp;
|
|
4191
4348
|
exports.BSONRuntimeError = BSONRuntimeError;
|
|
4192
4349
|
exports.BSONSymbol = BSONSymbol;
|
|
@@ -4209,6 +4366,7 @@ exports.UUID = UUID;
|
|
|
4209
4366
|
exports.calculateObjectSize = calculateObjectSize;
|
|
4210
4367
|
exports.deserialize = deserialize;
|
|
4211
4368
|
exports.deserializeStream = deserializeStream;
|
|
4369
|
+
exports.onDemand = onDemand;
|
|
4212
4370
|
exports.serialize = serialize;
|
|
4213
4371
|
exports.serializeWithBufferAndIndex = serializeWithBufferAndIndex;
|
|
4214
4372
|
exports.setInternalBufferSize = setInternalBufferSize;
|