Skip to content

Commit 3289184

Browse files
authored
fix(NODE-6059): clean up experimental APIs (#665)
1 parent d7898f9 commit 3289184

File tree

13 files changed

+63
-627
lines changed

13 files changed

+63
-627
lines changed

src/bson.ts

Lines changed: 1 addition & 1 deletion
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/error.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ export class BSONOffsetError extends BSONError {
9898

9999
public offset: number;
100100

101-
constructor(message: string, offset: number) {
102-
super(`${message}. offset: ${offset}`);
101+
constructor(message: string, offset: number, options?: { cause?: unknown }) {
102+
super(`${message}. offset: ${offset}`, options);
103103
this.offset = offset;
104104
}
105105
}

src/parser/on_demand/index.ts

Lines changed: 1 addition & 24 deletions
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

Lines changed: 6 additions & 15 deletions
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,22 +45,12 @@ 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);
48+
function getSize(source: Uint8Array, offset: number) {
49+
try {
50+
return NumberUtils.getNonnegativeInt32LE(source, offset);
51+
} catch (cause) {
52+
throw new BSONOffsetError('BSON size cannot be negative', offset, { cause });
5653
}
57-
return (
58-
source[offset] |
59-
(source[offset + 1] << 8) |
60-
(source[offset + 2] << 16) |
61-
(source[offset + 3] << 24)
62-
);
6354
}
6455

6556
/**

src/parser/on_demand/parse_to_structure.ts

Lines changed: 0 additions & 145 deletions
This file was deleted.

src/utils/byte_utils.ts

Lines changed: 3 additions & 3 deletions
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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ const isBigEndian = FLOAT_BYTES[7] === 0;
1313
* A collection of functions that get or set various numeric types and bit widths from a Uint8Array.
1414
*/
1515
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;
16+
/**
17+
* Parses a signed int32 at offset. Throws a `RangeError` if value is negative.
18+
*/
19+
getNonnegativeInt32LE: (source: Uint8Array, offset: number) => number;
20+
getInt32LE: (source: Uint8Array, offset: number) => number;
21+
getUint32LE: (source: Uint8Array, offset: number) => number;
22+
getUint32BE: (source: Uint8Array, offset: number) => number;
23+
getBigInt64LE: (source: Uint8Array, offset: number) => bigint;
24+
getFloat64LE: (source: Uint8Array, offset: number) => number;
25+
setInt32BE: (destination: Uint8Array, offset: number, value: number) => 4;
26+
setInt32LE: (destination: Uint8Array, offset: number, value: number) => 4;
27+
setBigInt64LE: (destination: Uint8Array, offset: number, value: bigint) => 8;
28+
setFloat64LE: (destination: Uint8Array, offset: number, value: number) => 8;
2529
};
2630

2731
/**
@@ -31,6 +35,18 @@ export type NumberUtils = {
3135
* @public
3236
*/
3337
export const NumberUtils: NumberUtils = {
38+
getNonnegativeInt32LE(source: Uint8Array, offset: number): number {
39+
if (source[offset + 3] > 127) {
40+
throw new RangeError(`Size cannot be negative at offset: ${offset}`);
41+
}
42+
return (
43+
source[offset] |
44+
(source[offset + 1] << 8) |
45+
(source[offset + 2] << 16) |
46+
(source[offset + 3] << 24)
47+
);
48+
},
49+
3450
/** Reads a little-endian 32-bit integer from source */
3551
getInt32LE(source: Uint8Array, offset: number): number {
3652
return (

test/node/error.test.ts

Lines changed: 5 additions & 5 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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)