diff --git a/package.json b/package.json index e767c97..59dab8b 100644 --- a/package.json +++ b/package.json @@ -200,11 +200,11 @@ "docs": "aegir docs" }, "dependencies": { - "multiformats": "^13.0.0" + "multiformats": "^14.0.0" }, "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..06706e5 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) + } + + 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 8712fdd..b30b906 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 + } + + const 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', () => {