Skip to content

Commit

Permalink
fix: revert use of buffer.concat on node.js (#75)
Browse files Browse the repository at this point in the history
4.0.9 had a breaking change - the input type of `concat` stopped
accepting plain arrays, requiring an array of `Uint8Array`s.
  • Loading branch information
achingbrain authored Dec 7, 2023
1 parent 3d5f87d commit bf6d760
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArrayLike<number>>, length?: number): Uint8Array {
if (length == null) {
length = arrays.reduce((acc, curr) => acc + curr.length, 0)
}
Expand Down
16 changes: 16 additions & 0 deletions test/concat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bf6d760

Please sign in to comment.