Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix type errors in bitfield-rle tests #624

Merged
merged 5 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions src/core-manager/bitfield-rle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const n = 4
class State {
/**
*
* @param {Buffer} input
* @param {Buffer | undefined} output
* @param {Uint8Array} input
* @param {Uint8Array | undefined} output
* @param {number} offset
*/
constructor(input, output, offset) {
Expand All @@ -31,13 +31,10 @@ class State {
encode.bytes = 0

/**
* @param {Uint32Array} bitfield
* @param {Buffer} [buffer]
* @param {number} [offset]
* @param {import('type-fest').TypedArray} bitfield
EvanHahn marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Buffer}
*/
export function encode(bitfield, buffer, offset) {
EvanHahn marked this conversation as resolved.
Show resolved Hide resolved
if (!offset) offset = 0

export function encode(bitfield) {
const bitfieldBuf = Buffer.from(
bitfield.buffer,
bitfield.byteOffset,
Expand All @@ -47,15 +44,16 @@ export function encode(bitfield, buffer, offset) {
// Encoded as little endian
if (isBigEndian) bitfieldBuf.swap32()

if (!buffer) buffer = Buffer.allocUnsafe(encodingLength(bitfieldBuf))
const buffer = Buffer.allocUnsafe(encodingLength(bitfieldBuf))
const offset = 0
var state = new State(bitfieldBuf, buffer, offset)
rle(state)
encode.bytes = state.outputOffset - offset
return buffer
}

/**
* @param {Buffer} bitfield
* @param {Uint8Array} bitfield
*/
export function encodingLength(bitfield) {
var state = new State(bitfield, undefined, 0)
Expand All @@ -65,7 +63,7 @@ export function encodingLength(bitfield) {

decode.bytes = 0
/**
* @param {Buffer} buffer
* @param {Uint8Array} buffer
* @param {number} [offset]
* @returns {Uint32Array}
*/
Expand All @@ -85,7 +83,7 @@ export function decode(buffer, offset) {
if (repeat) {
bitfieldBuf.fill(next & 2 ? 255 : 0, ptr, ptr + len)
} else {
buffer.copy(bitfieldBuf, ptr, offset, offset + len)
bitfieldBuf.set(buffer.subarray(offset, offset + len), ptr)
offset += len
}

Expand All @@ -105,7 +103,7 @@ export function decode(buffer, offset) {
}

/**
* @param {Buffer} buffer
* @param {Uint8Array} buffer
* @param {number} offset
*/
export function decodingLength(buffer, offset) {
Expand Down Expand Up @@ -166,14 +164,17 @@ function rle(state) {
}

/**
* @param {State & { output: Buffer }} state
* @param {State & { output: Uint8Array }} state
* @param {number} end
*/
function encodeHead(state, end) {
var headLength = end - state.inputOffset
varint.encode(2 * headLength, state.output, state.outputOffset)
state.outputOffset += varint.encode.bytes || 0
state.input.copy(state.output, state.outputOffset, state.inputOffset, end)
state.output.set(
state.input.subarray(state.inputOffset, end),
state.outputOffset
)
state.outputOffset += headLength
}

Expand Down Expand Up @@ -229,7 +230,7 @@ function encodeUpdate(state, i, len, bit) {
/**
*
* @param {State} state
* @returns {state is State & { output: Buffer }}
* @returns {state is State & { output: Uint8Array }}
*/
function stateHasOutput(state) {
return !!state.output
Expand Down
6 changes: 3 additions & 3 deletions tests/bitfield-rle.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ test('encodes empty bitfield', function (t) {

test('throws on bad input', function (t) {
t.exception(function () {
rle.decode(toUint32Array([100, 0, 0, 0]))
rle.decode(new Uint8Array([100, 0, 0, 0]))
}, 'invalid delta count')
// t.exception.all also catches RangeErrors, which is what we expect from this
t.exception.all(function () {
rle.decode(
toUint32Array([
new Uint8Array([
10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0,
10, 0,
])
Expand All @@ -107,7 +107,7 @@ test('not power of two', function (t) {
)
})

/** @param {Bitfield | Buffer | Array<number>} b */
/** @param {Bitfield | Uint8Array | Array<number>} b */
function toUint32Array(b) {
if (Array.isArray(b)) {
b = Buffer.from(b)
Expand Down
Loading