Skip to content

Commit

Permalink
chore: fix type errors in bitfield-rle tests
Browse files Browse the repository at this point in the history
This fixes all the type errors in the tests for `bitfield-rle`. Some of
the type errors were entirely test-only, but others required changes to
the "real" code. Most notably, many `Buffer` types were changed to
`Uint8Array`.
  • Loading branch information
EvanHahn committed May 7, 2024
1 parent 7f8fcdd commit 4b16cd4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
25 changes: 14 additions & 11 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,8 +31,8 @@ class State {
encode.bytes = 0

/**
* @param {Uint32Array} bitfield
* @param {Buffer} [buffer]
* @param {import('type-fest').TypedArray} bitfield
* @param {Uint8Array} [buffer]
* @param {number} [offset]
*/
export function encode(bitfield, buffer, offset) {
Expand All @@ -55,7 +55,7 @@ export function encode(bitfield, buffer, offset) {
}

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

decode.bytes = 0
/**
* @param {Buffer} buffer
* @param {Uint8Array} buffer
* @param {number} [offset]
* @returns {Uint32Array}
*/
Expand All @@ -85,7 +85,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 +105,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 +166,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 +232,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

0 comments on commit 4b16cd4

Please sign in to comment.