|
1 | 1 | /* eslint-disable no-restricted-imports */ |
2 | | -import { BSON, ByteUtils, type DeserializeOptions, type SerializeOptions } from 'bson'; |
| 2 | +import { BSON, type DeserializeOptions, type SerializeOptions } from 'bson'; |
3 | 3 |
|
4 | 4 | export { |
5 | 5 | Binary, |
@@ -43,28 +43,19 @@ export const getInt32LE = BSON.NumberUtils.getInt32LE; |
43 | 43 | export const getFloat64LE = BSON.NumberUtils.getFloat64LE; |
44 | 44 | export const getBigInt64LE = BSON.NumberUtils.getBigInt64LE; |
45 | 45 | export const toUTF8 = BSON.ByteUtils.toUTF8; |
46 | | - |
47 | | -// BSON wrappers |
| 46 | +export const fromUTF8 = BSON.ByteUtils.fromUTF8; |
| 47 | +export const fromBase64 = BSON.ByteUtils.fromBase64; |
| 48 | +export const fromNumberArray = BSON.ByteUtils.fromNumberArray; |
| 49 | +export const concatBuffers = BSON.ByteUtils.concat; |
| 50 | +export const allocateBuffer = BSON.ByteUtils.allocate; |
| 51 | +export const allocateUnsafeBuffer = BSON.ByteUtils.allocateUnsafe; |
48 | 52 |
|
49 | 53 | // writeInt32LE, same order of arguments as Buffer.writeInt32LE |
50 | 54 | export const writeInt32LE = (destination: Uint8Array, value: number, offset: number) => |
51 | 55 | BSON.NumberUtils.setInt32LE(destination, offset, value); |
52 | 56 |
|
53 | | -// various wrappers that consume and return local buffer types |
54 | | - |
55 | | -export const fromUTF8 = (text: string) => |
56 | | - ByteUtils.toLocalBufferType(BSON.ByteUtils.fromUTF8(text)); |
57 | | -export const fromBase64 = (b64: string) => |
58 | | - ByteUtils.toLocalBufferType(BSON.ByteUtils.fromBase64(b64)); |
59 | | -export const fromNumberArray = (array: number[]) => |
60 | | - ByteUtils.toLocalBufferType(BSON.ByteUtils.fromNumberArray(array)); |
61 | | -export const concatBuffers = (list: Uint8Array[]) => { |
62 | | - return ByteUtils.toLocalBufferType(BSON.ByteUtils.concat(list)); |
63 | | -}; |
64 | | -export const allocateBuffer = (size: number) => |
65 | | - ByteUtils.toLocalBufferType(BSON.ByteUtils.allocate(size)); |
66 | | -export const allocateUnsafeBuffer = (size: number) => |
67 | | - ByteUtils.toLocalBufferType(BSON.ByteUtils.allocateUnsafe(size)); |
| 57 | +// copyBuffer: copies from source buffer to target buffer, returns number of bytes copied |
| 58 | +// inputs are explicitly named to avoid confusion |
68 | 59 | export const copyBuffer = (input: { |
69 | 60 | source: Uint8Array; |
70 | 61 | target: Uint8Array; |
@@ -92,6 +83,8 @@ const validateBufferInputs = (buffer: Uint8Array, offset: number, length: number |
92 | 83 | } |
93 | 84 | }; |
94 | 85 |
|
| 86 | +// readInt32LE, reads a 32-bit integer from buffer at given offset |
| 87 | +// throws if offset is out of bounds |
95 | 88 | export const readInt32LE = (buffer: Uint8Array, offset: number): number => { |
96 | 89 | validateBufferInputs(buffer, offset, 4); |
97 | 90 | return getInt32LE(buffer, offset); |
|
0 commit comments