Skip to content

Commit d2f973c

Browse files
committed
fix(NODE-6059): clean up experimental APIs
1 parent d7898f9 commit d2f973c

11 files changed

+44
-631
lines changed

src/bson.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export {
5151
Decimal128
5252
};
5353
export { BSONValue } from './bson_value';
54-
export { BSONError, BSONVersionError, BSONRuntimeError } from './error';
54+
export { BSONError, BSONVersionError, BSONRuntimeError, BSONOffsetError } from './error';
5555
export { BSONType } from './constants';
5656
export { EJSON } from './extended_json';
5757
export { onDemand, type OnDemand } from './parser/on_demand/index';

src/parser/on_demand/index.ts

+1-24
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,20 @@
1-
import { type BSONError, BSONOffsetError } from '../../error';
21
import { ByteUtils } from '../../utils/byte_utils';
32
import { NumberUtils } from '../../utils/number_utils';
4-
import { type BSONElement, parseToElements, getSize } from './parse_to_elements';
5-
import { type BSONReviver, type Container, parseToStructure } from './parse_to_structure';
3+
import { type BSONElement, parseToElements } from './parse_to_elements';
64
/**
75
* @experimental
86
* @public
97
*
108
* A new set of BSON APIs that are currently experimental and not intended for production use.
119
*/
1210
export type OnDemand = {
13-
BSONOffsetError: {
14-
new (message: string, offset: number): BSONOffsetError;
15-
isBSONError(value: unknown): value is BSONError;
16-
};
1711
parseToElements: (this: void, bytes: Uint8Array, startOffset?: number) => Iterable<BSONElement>;
18-
parseToStructure: <
19-
TRoot extends Container = {
20-
dest: Record<string, unknown>;
21-
kind: 'object';
22-
}
23-
>(
24-
bytes: Uint8Array,
25-
startOffset?: number,
26-
root?: TRoot,
27-
reviver?: BSONReviver
28-
) => TRoot extends undefined ? Record<string, unknown> : TRoot['dest'];
2912
// Types
3013
BSONElement: BSONElement;
31-
Container: Container;
32-
BSONReviver: BSONReviver;
3314

3415
// Utils
3516
ByteUtils: ByteUtils;
3617
NumberUtils: NumberUtils;
37-
getSize: (source: Uint8Array, offset: number) => number;
3818
};
3919

4020
/**
@@ -44,11 +24,8 @@ export type OnDemand = {
4424
const onDemand: OnDemand = Object.create(null);
4525

4626
onDemand.parseToElements = parseToElements;
47-
onDemand.parseToStructure = parseToStructure;
48-
onDemand.BSONOffsetError = BSONOffsetError;
4927
onDemand.ByteUtils = ByteUtils;
5028
onDemand.NumberUtils = NumberUtils;
51-
onDemand.getSize = getSize;
5229

5330
Object.freeze(onDemand);
5431

src/parser/on_demand/parse_to_elements.ts

+4-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BSONOffsetError } from '../../error';
2+
import { NumberUtils } from '../../utils/number_utils';
23

34
/**
45
* @internal
@@ -44,24 +45,6 @@ export type BSONElement = [
4445
length: number
4546
];
4647

47-
/**
48-
* @experimental
49-
* @public
50-
*
51-
* Parses a int32 little-endian at offset, throws if it is negative
52-
*/
53-
export function getSize(source: Uint8Array, offset: number): number {
54-
if (source[offset + 3] > 127) {
55-
throw new BSONOffsetError('BSON size cannot be negative', offset);
56-
}
57-
return (
58-
source[offset] |
59-
(source[offset + 1] << 8) |
60-
(source[offset + 2] << 16) |
61-
(source[offset + 3] << 24)
62-
);
63-
}
64-
6548
/**
6649
* Searches for null terminator of a BSON element's value (Never the document null terminator)
6750
* **Does not** bounds check since this should **ONLY** be used within parseToElements which has asserted that `bytes` ends with a `0x00`.
@@ -97,7 +80,7 @@ export function parseToElements(
9780
);
9881
}
9982

100-
const documentSize = getSize(bytes, startOffset);
83+
const documentSize = NumberUtils.getSize(bytes, startOffset);
10184

10285
if (documentSize > bytes.length - startOffset) {
10386
throw new BSONOffsetError(
@@ -161,15 +144,15 @@ export function parseToElements(
161144
type === BSONElementType.array ||
162145
type === BSONElementType.javascriptWithScope
163146
) {
164-
length = getSize(bytes, offset);
147+
length = NumberUtils.getSize(bytes, offset);
165148
} else if (
166149
type === BSONElementType.string ||
167150
type === BSONElementType.binData ||
168151
type === BSONElementType.dbPointer ||
169152
type === BSONElementType.javascript ||
170153
type === BSONElementType.symbol
171154
) {
172-
length = getSize(bytes, offset) + 4;
155+
length = NumberUtils.getSize(bytes, offset) + 4;
173156
if (type === BSONElementType.binData) {
174157
// binary subtype
175158
length += 1;

src/parser/on_demand/parse_to_structure.ts

-145
This file was deleted.

src/utils/byte_utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { webByteUtils } from './web_byte_utils';
1010
*/
1111
export type ByteUtils = {
1212
/** Transforms the input to an instance of Buffer if running on node, otherwise Uint8Array */
13-
toLocalBufferType(buffer: Uint8Array | ArrayBufferView | ArrayBuffer): Uint8Array;
13+
toLocalBufferType: (buffer: Uint8Array | ArrayBufferView | ArrayBuffer) => Uint8Array;
1414
/** Create empty space of size */
1515
allocate: (size: number) => Uint8Array;
1616
/** Create empty space of size, use pooled memory when available */
@@ -36,9 +36,9 @@ export type ByteUtils = {
3636
/** Get the utf8 code unit count from a string if it were to be transformed to utf8 */
3737
utf8ByteLength: (input: string) => number;
3838
/** Encode UTF8 bytes generated from `source` string into `destination` at byteOffset. Returns the number of bytes encoded. */
39-
encodeUTF8Into(destination: Uint8Array, source: string, byteOffset: number): number;
39+
encodeUTF8Into: (destination: Uint8Array, source: string, byteOffset: number) => number;
4040
/** Generate a Uint8Array filled with random bytes with byteLength */
41-
randomBytes(byteLength: number): Uint8Array;
41+
randomBytes: (byteLength: number) => Uint8Array;
4242
};
4343

4444
declare const Buffer: { new (): unknown; prototype?: { _isBuffer?: boolean } } | undefined;

src/utils/number_utils.ts

+28-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { BSONOffsetError } from '../error';
2+
13
const FLOAT = new Float64Array(1);
24
const FLOAT_BYTES = new Uint8Array(FLOAT.buffer, 0, 8);
35

@@ -13,15 +15,20 @@ const isBigEndian = FLOAT_BYTES[7] === 0;
1315
* A collection of functions that get or set various numeric types and bit widths from a Uint8Array.
1416
*/
1517
export type NumberUtils = {
16-
getInt32LE(source: Uint8Array, offset: number): number;
17-
getUint32LE(source: Uint8Array, offset: number): number;
18-
getUint32BE(source: Uint8Array, offset: number): number;
19-
getBigInt64LE(source: Uint8Array, offset: number): bigint;
20-
getFloat64LE(source: Uint8Array, offset: number): number;
21-
setInt32BE(destination: Uint8Array, offset: number, value: number): 4;
22-
setInt32LE(destination: Uint8Array, offset: number, value: number): 4;
23-
setBigInt64LE(destination: Uint8Array, offset: number, value: bigint): 8;
24-
setFloat64LE(destination: Uint8Array, offset: number, value: number): 8;
18+
/**
19+
* Parses a int32 little-endian at offset, throws if it is negative.
20+
* - size as in `size_t`
21+
*/
22+
getSize: (source: Uint8Array, offset: number) => number;
23+
getInt32LE: (source: Uint8Array, offset: number) => number;
24+
getUint32LE: (source: Uint8Array, offset: number) => number;
25+
getUint32BE: (source: Uint8Array, offset: number) => number;
26+
getBigInt64LE: (source: Uint8Array, offset: number) => bigint;
27+
getFloat64LE: (source: Uint8Array, offset: number) => number;
28+
setInt32BE: (destination: Uint8Array, offset: number, value: number) => 4;
29+
setInt32LE: (destination: Uint8Array, offset: number, value: number) => 4;
30+
setBigInt64LE: (destination: Uint8Array, offset: number, value: bigint) => 8;
31+
setFloat64LE: (destination: Uint8Array, offset: number, value: number) => 8;
2532
};
2633

2734
/**
@@ -31,6 +38,18 @@ export type NumberUtils = {
3138
* @public
3239
*/
3340
export const NumberUtils: NumberUtils = {
41+
getSize(source: Uint8Array, offset: number): number {
42+
if (source[offset + 3] > 127) {
43+
throw new BSONOffsetError('BSON size cannot be negative', offset);
44+
}
45+
return (
46+
source[offset] |
47+
(source[offset + 1] << 8) |
48+
(source[offset + 2] << 16) |
49+
(source[offset + 3] << 24)
50+
);
51+
},
52+
3453
/** Reads a little-endian 32-bit integer from source */
3554
getInt32LE(source: Uint8Array, offset: number): number {
3655
return (

test/node/error.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
BSONError,
77
BSONVersionError,
88
BSONRuntimeError,
9-
onDemand
9+
BSONOffsetError
1010
} from '../register-bson';
1111

1212
const instanceOfChecksWork = !__isWeb__;
@@ -111,19 +111,19 @@ describe('BSONError', function () {
111111

112112
describe('class BSONOffsetError', () => {
113113
it('is a BSONError instance', function () {
114-
expect(BSONError.isBSONError(new onDemand.BSONOffsetError('Oopsie', 3))).to.be.true;
114+
expect(BSONError.isBSONError(new BSONOffsetError('Oopsie', 3))).to.be.true;
115115
});
116116

117117
it('has a name property equal to "BSONOffsetError"', function () {
118-
expect(new onDemand.BSONOffsetError('Woops!', 3)).to.have.property('name', 'BSONOffsetError');
118+
expect(new BSONOffsetError('Woops!', 3)).to.have.property('name', 'BSONOffsetError');
119119
});
120120

121121
it('sets the offset property', function () {
122-
expect(new onDemand.BSONOffsetError('Woops!', 3)).to.have.property('offset', 3);
122+
expect(new BSONOffsetError('Woops!', 3)).to.have.property('offset', 3);
123123
});
124124

125125
it('includes the offset in the message', function () {
126-
expect(new onDemand.BSONOffsetError('Woops!', 3))
126+
expect(new BSONOffsetError('Woops!', 3))
127127
.to.have.property('message')
128128
.that.matches(/offset: 3/i);
129129
});

test/node/exports.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const EXPECTED_EXPORTS = [
3030
'Decimal128',
3131
'BSONError',
3232
'BSONRuntimeError',
33+
'BSONOffsetError',
3334
'setInternalBufferSize',
3435
'serialize',
3536
'serializeWithBufferAndIndex',

test/node/parser/on_demand/parse_to_elements.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as BSON from '../../../register-bson';
55
import { bufferFromHexArray, stringToUTF8HexBytes, int32LEToHex } from '../../tools/utils';
66

77
const parseToElements = BSON.onDemand.parseToElements;
8-
const BSONOffsetError = BSON.onDemand.BSONOffsetError;
8+
const BSONOffsetError = BSON.BSONOffsetError;
99

1010
describe('parseToElements()', () => {
1111
context('when given less than 5 bytes', () => {

0 commit comments

Comments
 (0)