|
| 1 | +import { Buffer } from 'buffer' |
| 2 | + |
| 3 | +// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/errors.js#L758 |
| 4 | +const ERR_BUFFER_OUT_OF_BOUNDS = () => new Error('Attempt to access memory outside buffer bounds') |
| 5 | + |
| 6 | +// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/errors.js#L968 |
| 7 | +const ERR_INVALID_ARG_TYPE = (name: string, expected: string, actual: any) => |
| 8 | + new Error(`The "${name}" argument must be of type ${expected}. Received ${actual}`) |
| 9 | + |
| 10 | +// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/errors.js#L1262 |
| 11 | +const ERR_OUT_OF_RANGE = (str: string, range: string, received: number) => |
| 12 | + new Error(`The value of "${str} is out of range. It must be ${range}. Received ${received}`) |
| 13 | + |
| 14 | +// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/validators.js#L127-L130 |
| 15 | +function validateNumber(value: any, name: string) { |
| 16 | + if (typeof value !== 'number') throw ERR_INVALID_ARG_TYPE(name, 'number', value) |
| 17 | +} |
| 18 | + |
| 19 | +// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/buffer.js#L68-L80 |
| 20 | +function boundsError(value: number, length: number) { |
| 21 | + if (Math.floor(value) !== value) { |
| 22 | + validateNumber(value, 'offset') |
| 23 | + throw ERR_OUT_OF_RANGE('offset', 'an integer', value) |
| 24 | + } |
| 25 | + |
| 26 | + if (length < 0) throw ERR_BUFFER_OUT_OF_BOUNDS() |
| 27 | + |
| 28 | + throw ERR_OUT_OF_RANGE('offset', `>= 0 and <= ${length}`, value) |
| 29 | +} |
| 30 | + |
| 31 | +// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/buffer.js#L129-L145 |
| 32 | +export function readBigInt64LE(buffer: Buffer, offset = 0): bigint { |
| 33 | + validateNumber(offset, 'offset') |
| 34 | + const first = buffer[offset] |
| 35 | + const last = buffer[offset + 7] |
| 36 | + if (first === undefined || last === undefined) boundsError(offset, buffer.length - 8) |
| 37 | + // tslint:disable-next-line:no-bitwise |
| 38 | + const val = buffer[offset + 4] + buffer[offset + 5] * 2 ** 8 + buffer[offset + 6] * 2 ** 16 + (last << 24) // Overflow |
| 39 | + return ( |
| 40 | + (BigInt(val) << BigInt(32)) + // tslint:disable-line:no-bitwise |
| 41 | + BigInt(first + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + buffer[++offset] * 2 ** 24) |
| 42 | + ) |
| 43 | +} |
| 44 | + |
| 45 | +// https://github.com/nodejs/node/blob/v14.17.0/lib/internal/buffer.js#L89-L107 |
| 46 | +export function readBigUInt64LE(buffer: Buffer, offset = 0): bigint { |
| 47 | + validateNumber(offset, 'offset') |
| 48 | + const first = buffer[offset] |
| 49 | + const last = buffer[offset + 7] |
| 50 | + if (first === undefined || last === undefined) boundsError(offset, buffer.length - 8) |
| 51 | + |
| 52 | + const lo = first + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + buffer[++offset] * 2 ** 24 |
| 53 | + |
| 54 | + const hi = buffer[++offset] + buffer[++offset] * 2 ** 8 + buffer[++offset] * 2 ** 16 + last * 2 ** 24 |
| 55 | + |
| 56 | + return BigInt(lo) + (BigInt(hi) << BigInt(32)) // tslint:disable-line:no-bitwise |
| 57 | +} |
0 commit comments