From c66fba7e0b6e47053c5ea888b9374f7d107a1079 Mon Sep 17 00:00:00 2001 From: Evan Hahn Date: Tue, 7 May 2024 22:17:50 +0000 Subject: [PATCH 1/2] chore: fix type errors in bitfield-rle tests 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`, and some unused code was removed. --- src/core-manager/bitfield-rle.js | 33 ++++++++++++++++---------------- tests/bitfield-rle.js | 6 +++--- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/core-manager/bitfield-rle.js b/src/core-manager/bitfield-rle.js index 50ed976c1..70eae6401 100644 --- a/src/core-manager/bitfield-rle.js +++ b/src/core-manager/bitfield-rle.js @@ -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) { @@ -31,13 +31,10 @@ class State { encode.bytes = 0 /** - * @param {Uint32Array} bitfield - * @param {Buffer} [buffer] - * @param {number} [offset] + * @param {import('type-fest').TypedArray} bitfield + * @returns {Buffer} */ -export function encode(bitfield, buffer, offset) { - if (!offset) offset = 0 - +export function encode(bitfield) { const bitfieldBuf = Buffer.from( bitfield.buffer, bitfield.byteOffset, @@ -47,7 +44,8 @@ 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 @@ -55,7 +53,7 @@ export function encode(bitfield, buffer, offset) { } /** - * @param {Buffer} bitfield + * @param {Uint8Array} bitfield */ export function encodingLength(bitfield) { var state = new State(bitfield, undefined, 0) @@ -65,7 +63,7 @@ export function encodingLength(bitfield) { decode.bytes = 0 /** - * @param {Buffer} buffer + * @param {Uint8Array} buffer * @param {number} [offset] * @returns {Uint32Array} */ @@ -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 } @@ -105,7 +103,7 @@ export function decode(buffer, offset) { } /** - * @param {Buffer} buffer + * @param {Uint8Array} buffer * @param {number} offset */ export function decodingLength(buffer, offset) { @@ -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 } @@ -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 diff --git a/tests/bitfield-rle.js b/tests/bitfield-rle.js index 01651454f..f853d36ee 100644 --- a/tests/bitfield-rle.js +++ b/tests/bitfield-rle.js @@ -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, ]) @@ -107,7 +107,7 @@ test('not power of two', function (t) { ) }) -/** @param {Bitfield | Buffer | Array} b */ +/** @param {Bitfield | Uint8Array | Array} b */ function toUint32Array(b) { if (Array.isArray(b)) { b = Buffer.from(b) From 1e137f6ab1de4078af35fb58d127132fd8d9586e Mon Sep 17 00:00:00 2001 From: Evan Hahn Date: Mon, 20 May 2024 16:43:27 +0000 Subject: [PATCH 2/2] Redo patch to make it much smaller, and mostly test-only --- src/core-manager/bitfield-rle.js | 35 ++++++++++++++++---------------- tests/bitfield-rle.js | 21 +++++-------------- 2 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/core-manager/bitfield-rle.js b/src/core-manager/bitfield-rle.js index 70eae6401..a775aba69 100644 --- a/src/core-manager/bitfield-rle.js +++ b/src/core-manager/bitfield-rle.js @@ -15,8 +15,8 @@ const n = 4 class State { /** * - * @param {Uint8Array} input - * @param {Uint8Array | undefined} output + * @param {Buffer} input + * @param {Buffer | undefined} output * @param {number} offset */ constructor(input, output, offset) { @@ -31,10 +31,13 @@ class State { encode.bytes = 0 /** - * @param {import('type-fest').TypedArray} bitfield - * @returns {Buffer} + * @param {Uint32Array} bitfield + * @param {Buffer} [buffer] + * @param {number} [offset] */ -export function encode(bitfield) { +export function encode(bitfield, buffer, offset) { + if (!offset) offset = 0 + const bitfieldBuf = Buffer.from( bitfield.buffer, bitfield.byteOffset, @@ -44,8 +47,7 @@ export function encode(bitfield) { // Encoded as little endian if (isBigEndian) bitfieldBuf.swap32() - const buffer = Buffer.allocUnsafe(encodingLength(bitfieldBuf)) - const offset = 0 + if (!buffer) buffer = Buffer.allocUnsafe(encodingLength(bitfieldBuf)) var state = new State(bitfieldBuf, buffer, offset) rle(state) encode.bytes = state.outputOffset - offset @@ -53,9 +55,9 @@ export function encode(bitfield) { } /** - * @param {Uint8Array} bitfield + * @param {Buffer} bitfield */ -export function encodingLength(bitfield) { +function encodingLength(bitfield) { var state = new State(bitfield, undefined, 0) rle(state) return state.outputOffset @@ -63,7 +65,7 @@ export function encodingLength(bitfield) { decode.bytes = 0 /** - * @param {Uint8Array} buffer + * @param {Buffer} buffer * @param {number} [offset] * @returns {Uint32Array} */ @@ -83,7 +85,7 @@ export function decode(buffer, offset) { if (repeat) { bitfieldBuf.fill(next & 2 ? 255 : 0, ptr, ptr + len) } else { - bitfieldBuf.set(buffer.subarray(offset, offset + len), ptr) + buffer.copy(bitfieldBuf, ptr, offset, offset + len) offset += len } @@ -103,7 +105,7 @@ export function decode(buffer, offset) { } /** - * @param {Uint8Array} buffer + * @param {Buffer} buffer * @param {number} offset */ export function decodingLength(buffer, offset) { @@ -164,17 +166,14 @@ function rle(state) { } /** - * @param {State & { output: Uint8Array }} state + * @param {State & { output: Buffer }} 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.output.set( - state.input.subarray(state.inputOffset, end), - state.outputOffset - ) + state.input.copy(state.output, state.outputOffset, state.inputOffset, end) state.outputOffset += headLength } @@ -230,7 +229,7 @@ function encodeUpdate(state, i, len, bit) { /** * * @param {State} state - * @returns {state is State & { output: Uint8Array }} + * @returns {state is State & { output: Buffer }} */ function stateHasOutput(state) { return !!state.output diff --git a/tests/bitfield-rle.js b/tests/bitfield-rle.js index 35ea9b69f..8877c4bce 100644 --- a/tests/bitfield-rle.js +++ b/tests/bitfield-rle.js @@ -15,18 +15,6 @@ test('encodes and decodes', function () { ) }) -test('encodingLength', function () { - var bits = new Bitfield(1024) - var len = rle.encodingLength(bits.buffer) - assert(len < bits.buffer.length, 'is smaller') - var deflated = rle.encode(bits.buffer) - assert.deepEqual( - len, - deflated.length, - 'encoding length is similar to encoded buffers length' - ) -}) - test('encodes and decodes with all bits set', function () { var bits = new Bitfield(1024) @@ -104,12 +92,13 @@ test('encodes empty bitfield', function () { }) test('throws on bad input', function () { - assert.throws(() => { - rle.decode(new Uint8Array([100, 0, 0, 0])) + assert.throws(function () { + rle.decode(Buffer.from([100, 0, 0, 0])) }, 'invalid delta count') - assert.throws(() => { + // t.exception.all also catches RangeErrors, which is what we expect from this + assert.throws(function () { rle.decode( - new Uint8Array([ + Buffer.from([ 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 0, ])