From 61d2cf8df42cccc8cda7aec0ebc706999a294c0a Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 7 Dec 2023 07:33:03 +0000 Subject: [PATCH] fix: revert use of buffer.concat on node.js 4.0.9 had a breaking change - the input type of `concat` stopped accepting plain arrays, requiring an array of `Uint8Array`s. --- src/concat.ts | 6 +----- test/concat.spec.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/concat.ts b/src/concat.ts index 8e6aa73..698be06 100644 --- a/src/concat.ts +++ b/src/concat.ts @@ -4,11 +4,7 @@ import { asUint8Array } from './util/as-uint8array.js' /** * Returns a new Uint8Array created by concatenating the passed ArrayLikes */ -export function concat (arrays: Uint8Array[], length?: number): Uint8Array { - if (globalThis.Buffer != null) { - return asUint8Array(globalThis.Buffer.concat(arrays, length)) - } - +export function concat (arrays: Array>, length?: number): Uint8Array { if (length == null) { length = arrays.reduce((acc, curr) => acc + curr.length, 0) } diff --git a/test/concat.spec.ts b/test/concat.spec.ts index d2b8720..097122e 100644 --- a/test/concat.spec.ts +++ b/test/concat.spec.ts @@ -21,6 +21,22 @@ describe('Uint8Array concat', () => { expect(concat([a, b], 8)).to.deep.equal(c) }) + it('concats mixed Uint8Arrays and Arrays', () => { + const a = Uint8Array.from([0, 1, 2, 3]) + const b = [4, 5, 6, 7] + const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7]) + + expect(concat([a, b])).to.deep.equal(c) + }) + + it('concats mixed Uint8Arrays and Arrays with a length', () => { + const a = Uint8Array.from([0, 1, 2, 3]) + const b = [4, 5, 6, 7] + const c = Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7]) + + expect(concat([a, b], 8)).to.deep.equal(c) + }) + it('concat returns Uint8Array', () => { const a = Uint8Array.from([0, 1, 2, 3]) const b = alloc(10).fill(1)