From e0e77291d5e909bd390ba02d5ff8a31567725b9c Mon Sep 17 00:00:00 2001 From: achingbrain Date: Mon, 4 May 2026 17:17:38 +0300 Subject: [PATCH 1/3] fix: return ArrayBuffer-backed arrays Since https://github.com/microsoft/TypeScript/pull/59417 `Uint8Array`s backed by `ArrayBuffer` are incompatible with those backed with `SharedArrayBuffer` so be explicit about the types returned. --- package.json | 2 +- src/alloc.node.ts | 4 ++-- src/alloc.ts | 4 ++-- src/concat.node.ts | 2 +- src/concat.ts | 2 +- src/from-string.node.ts | 2 +- src/from-string.ts | 2 +- src/util/as-uint8array.node.ts | 10 ++++++++-- src/util/as-uint8array.ts | 14 ++++++++++++-- src/util/bases.ts | 2 +- src/xor.ts | 2 +- test/equals.spec.ts | 2 +- test/from-string.spec.ts | 2 +- test/xor-compare.spec.ts | 2 +- test/xor.spec.ts | 2 +- 15 files changed, 35 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index e767c97..1ee5929 100644 --- a/package.json +++ b/package.json @@ -204,7 +204,7 @@ }, "devDependencies": { "@types/benchmark": "^2.1.1", - "aegir": "^47.1.5", + "aegir": "^48.0.1", "benchmark": "^2.1.4" }, "imports": { diff --git a/src/alloc.node.ts b/src/alloc.node.ts index 211b98b..3204d2d 100644 --- a/src/alloc.node.ts +++ b/src/alloc.node.ts @@ -5,7 +5,7 @@ import { asUint8Array } from '#util/as-uint8array' * Returns a `Uint8Array` of the requested size. Referenced memory will * be initialized to 0. */ -export function alloc (size: number = 0): Uint8Array { +export function alloc (size: number = 0): Uint8Array { return asUint8Array(Buffer.alloc(size)) } @@ -14,6 +14,6 @@ export function alloc (size: number = 0): Uint8Array { * uninitialized memory. Only use if you are certain you will immediately * overwrite every value in the returned `Uint8Array`. */ -export function allocUnsafe (size: number = 0): Uint8Array { +export function allocUnsafe (size: number = 0): Uint8Array { return asUint8Array(Buffer.allocUnsafe(size)) } diff --git a/src/alloc.ts b/src/alloc.ts index 8cbb77b..ea6b45f 100644 --- a/src/alloc.ts +++ b/src/alloc.ts @@ -2,7 +2,7 @@ * Returns a `Uint8Array` of the requested size. Referenced memory will * be initialized to 0. */ -export function alloc (size: number = 0): Uint8Array { +export function alloc (size: number = 0): Uint8Array { return new Uint8Array(size) } @@ -11,6 +11,6 @@ export function alloc (size: number = 0): Uint8Array { * uninitialized memory. Only use if you are certain you will immediately * overwrite every value in the returned `Uint8Array`. */ -export function allocUnsafe (size: number = 0): Uint8Array { +export function allocUnsafe (size: number = 0): Uint8Array { return new Uint8Array(size) } diff --git a/src/concat.node.ts b/src/concat.node.ts index 94d2187..92d00f2 100644 --- a/src/concat.node.ts +++ b/src/concat.node.ts @@ -4,6 +4,6 @@ import { asUint8Array } from '#util/as-uint8array' /** * Returns a new Uint8Array created by concatenating the passed Uint8Arrays */ -export function concat (arrays: Uint8Array[], length?: number): Uint8Array { +export function concat (arrays: Uint8Array[], length?: number): Uint8Array { return asUint8Array(Buffer.concat(arrays, length)) } diff --git a/src/concat.ts b/src/concat.ts index 2ef6de1..cffc5c6 100644 --- a/src/concat.ts +++ b/src/concat.ts @@ -4,7 +4,7 @@ import { asUint8Array } from '#util/as-uint8array' /** * Returns a new Uint8Array created by concatenating the passed Uint8Arrays */ -export function concat (arrays: Uint8Array[], length?: number): Uint8Array { +export function concat (arrays: Uint8Array[], length?: number): Uint8Array { if (length == null) { length = arrays.reduce((acc, curr) => acc + curr.length, 0) } diff --git a/src/from-string.node.ts b/src/from-string.node.ts index 7015147..625beb9 100644 --- a/src/from-string.node.ts +++ b/src/from-string.node.ts @@ -12,7 +12,7 @@ export type { SupportedEncodings } * * Also `ascii` which is similar to node's 'binary' encoding. */ -export function fromString (string: string, encoding: SupportedEncodings = 'utf8'): Uint8Array { +export function fromString (string: string, encoding: SupportedEncodings = 'utf8'): Uint8Array { const base = bases[encoding] if (base == null) { diff --git a/src/from-string.ts b/src/from-string.ts index bd1e8c1..acc2bc5 100644 --- a/src/from-string.ts +++ b/src/from-string.ts @@ -10,7 +10,7 @@ export type { SupportedEncodings } * * Also `ascii` which is similar to node's 'binary' encoding. */ -export function fromString (string: string, encoding: SupportedEncodings = 'utf8'): Uint8Array { +export function fromString (string: string, encoding: SupportedEncodings = 'utf8'): Uint8Array { const base = bases[encoding] if (base == null) { diff --git a/src/util/as-uint8array.node.ts b/src/util/as-uint8array.node.ts index 0d39d72..72ff548 100644 --- a/src/util/as-uint8array.node.ts +++ b/src/util/as-uint8array.node.ts @@ -2,6 +2,12 @@ * To guarantee Uint8Array semantics, convert nodejs Buffers * into vanilla Uint8Arrays */ -export function asUint8Array (buf: Uint8Array): Uint8Array { - return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) +export function asUint8Array (buf: Uint8Array): Uint8Array { + if (buf.buffer instanceof ArrayBuffer) { + return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) + } + + let b = buf.slice() + + return new Uint8Array(b.buffer, 0, b.byteLength) } diff --git a/src/util/as-uint8array.ts b/src/util/as-uint8array.ts index 8712fdd..2a77030 100644 --- a/src/util/as-uint8array.ts +++ b/src/util/as-uint8array.ts @@ -1,7 +1,17 @@ +function isByteArrayWithArrayBuffer (b?: Uint8Array): b is Uint8Array { + return b?.buffer instanceof ArrayBuffer +} + /** * To guarantee Uint8Array semantics, convert nodejs Buffers * into vanilla Uint8Arrays */ -export function asUint8Array (buf: Uint8Array): Uint8Array { - return buf +export function asUint8Array (buf: Uint8Array): Uint8Array { + if (isByteArrayWithArrayBuffer(buf)) { + return buf + } + + let b = buf.slice() + + return new Uint8Array(b.buffer, 0, b.byteLength) } diff --git a/src/util/bases.ts b/src/util/bases.ts index 16f305f..7f89498 100644 --- a/src/util/bases.ts +++ b/src/util/bases.ts @@ -2,7 +2,7 @@ import { bases } from 'multiformats/basics' import type { MultibaseCodec } from 'multiformats' import { allocUnsafe } from '#alloc' -function createCodec (name: string, prefix: string, encode: (buf: Uint8Array) => string, decode: (str: string) => Uint8Array): MultibaseCodec { +function createCodec (name: string, prefix: string, encode: (buf: Uint8Array) => string, decode: (str: string) => Uint8Array): MultibaseCodec { return { name, prefix, diff --git a/src/xor.ts b/src/xor.ts index 9743ebd..52e13b2 100644 --- a/src/xor.ts +++ b/src/xor.ts @@ -4,7 +4,7 @@ import { asUint8Array } from '#util/as-uint8array' /** * Returns the xor distance between two Uint8Arrays */ -export function xor (a: Uint8Array, b: Uint8Array): Uint8Array { +export function xor (a: Uint8Array, b: Uint8Array): Uint8Array { if (a.length !== b.length) { throw new Error('Inputs should have the same length') } diff --git a/test/equals.spec.ts b/test/equals.spec.ts index 6f10910..8aa71ae 100644 --- a/test/equals.spec.ts +++ b/test/equals.spec.ts @@ -1,5 +1,5 @@ import { expect } from 'aegir/chai' -import { equals } from '../src/equals.js' +import { equals } from '../src/equals.ts' describe('Uint8Array equals', () => { it('finds two Uint8Arrays equal', () => { diff --git a/test/from-string.spec.ts b/test/from-string.spec.ts index 1e777a0..447bcf6 100644 --- a/test/from-string.spec.ts +++ b/test/from-string.spec.ts @@ -1,5 +1,5 @@ import { expect } from 'aegir/chai' -import bases from '../src/util/bases.js' +import bases from '../src/util/bases.ts' import type { SupportedEncodings } from '#from-string' import { fromString } from '#from-string' import { toString } from '#to-string' diff --git a/test/xor-compare.spec.ts b/test/xor-compare.spec.ts index a5e7b6d..7976610 100644 --- a/test/xor-compare.spec.ts +++ b/test/xor-compare.spec.ts @@ -1,5 +1,5 @@ import { expect } from 'aegir/chai' -import { xorCompare } from '../src/xor-compare.js' +import { xorCompare } from '../src/xor-compare.ts' describe('xor-compare', () => { it('compare', () => { diff --git a/test/xor.spec.ts b/test/xor.spec.ts index 9e0a960..9a393ca 100644 --- a/test/xor.spec.ts +++ b/test/xor.spec.ts @@ -1,5 +1,5 @@ import { expect } from 'aegir/chai' -import { xor } from '../src/xor.js' +import { xor } from '../src/xor.ts' describe('Uint8Array xor', () => { it('xors 1,0 and 0,1', () => { From 455b08b3015b301896f1849aaad72f04b9581ce1 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 7 May 2026 09:35:50 +0300 Subject: [PATCH 2/3] chore: update multiformats dep --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1ee5929..59dab8b 100644 --- a/package.json +++ b/package.json @@ -200,7 +200,7 @@ "docs": "aegir docs" }, "dependencies": { - "multiformats": "^13.0.0" + "multiformats": "^14.0.0" }, "devDependencies": { "@types/benchmark": "^2.1.1", From ad3f36495f9781c532f634666e4312d811f94f9e Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 7 May 2026 09:59:09 +0300 Subject: [PATCH 3/3] chore: linting --- src/util/as-uint8array.node.ts | 2 +- src/util/as-uint8array.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/as-uint8array.node.ts b/src/util/as-uint8array.node.ts index 72ff548..06706e5 100644 --- a/src/util/as-uint8array.node.ts +++ b/src/util/as-uint8array.node.ts @@ -7,7 +7,7 @@ export function asUint8Array (buf: Uint8Array): Uint8Array { return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) } - let b = buf.slice() + const b = buf.slice() return new Uint8Array(b.buffer, 0, b.byteLength) } diff --git a/src/util/as-uint8array.ts b/src/util/as-uint8array.ts index 2a77030..b30b906 100644 --- a/src/util/as-uint8array.ts +++ b/src/util/as-uint8array.ts @@ -11,7 +11,7 @@ export function asUint8Array (buf: Uint8Array): Uint8Array { return buf } - let b = buf.slice() + const b = buf.slice() return new Uint8Array(b.buffer, 0, b.byteLength) }