Skip to content

Commit efab49a

Browse files
authored
feat(NODE-5959): make byte parsing utils available on onDemand library (#662)
1 parent 269df91 commit efab49a

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

src/parser/on_demand/index.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { type BSONError, BSONOffsetError } from '../../error';
2-
import { type BSONElement, parseToElements } from './parse_to_elements';
2+
import { ByteUtils } from '../../utils/byte_utils';
3+
import { NumberUtils } from '../../utils/number_utils';
4+
import { type BSONElement, parseToElements, getSize } from './parse_to_elements';
35
import { type BSONReviver, type Container, parseToStructure } from './parse_to_structure';
46
/**
57
* @experimental
@@ -28,6 +30,11 @@ export type OnDemand = {
2830
BSONElement: BSONElement;
2931
Container: Container;
3032
BSONReviver: BSONReviver;
33+
34+
// Utils
35+
ByteUtils: ByteUtils;
36+
NumberUtils: NumberUtils;
37+
getSize: (source: Uint8Array, offset: number) => number;
3138
};
3239

3340
/**
@@ -39,6 +46,9 @@ const onDemand: OnDemand = Object.create(null);
3946
onDemand.parseToElements = parseToElements;
4047
onDemand.parseToStructure = parseToStructure;
4148
onDemand.BSONOffsetError = BSONOffsetError;
49+
onDemand.ByteUtils = ByteUtils;
50+
onDemand.NumberUtils = NumberUtils;
51+
onDemand.getSize = getSize;
4252

4353
Object.freeze(onDemand);
4454

src/parser/on_demand/parse_to_elements.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ export type BSONElement = [
4545
];
4646

4747
/**
48-
* @internal
48+
* @experimental
49+
* @public
50+
*
4951
* Parses a int32 little-endian at offset, throws if it is negative
5052
*/
5153
export function getSize(source: Uint8Array, offset: number): number {

src/utils/byte_utils.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { nodeJsByteUtils } from './node_byte_utils';
22
import { webByteUtils } from './web_byte_utils';
33

4-
/** @internal */
4+
/**
5+
* @public
6+
* @experimental
7+
*
8+
* A collection of functions that help work with data in a Uint8Array.
9+
* ByteUtils is configured at load time to use Node.js or Web based APIs for the internal implementations.
10+
*/
511
export type ByteUtils = {
612
/** Transforms the input to an instance of Buffer if running on node, otherwise Uint8Array */
713
toLocalBufferType(buffer: Uint8Array | ArrayBufferView | ArrayBuffer): Uint8Array;

src/utils/number_utils.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,31 @@ FLOAT[0] = -1;
66
// Big endian [191, 240, 0, 0, 0, 0, 0, 0]
77
const isBigEndian = FLOAT_BYTES[7] === 0;
88

9+
/**
10+
* @experimental
11+
* @public
12+
*
13+
* A collection of functions that get or set various numeric types and bit widths from a Uint8Array.
14+
*/
15+
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;
25+
};
26+
927
/**
1028
* Number parsing and serializing utilities.
1129
*
12-
* @internal
30+
* @experimental
31+
* @public
1332
*/
14-
export const NumberUtils = {
33+
export const NumberUtils: NumberUtils = {
1534
/** Reads a little-endian 32-bit integer from source */
1635
getInt32LE(source: Uint8Array, offset: number): number {
1736
return (

0 commit comments

Comments
 (0)