From cf396f29d74abe135a1ff410e162c9bccc6b8d2f Mon Sep 17 00:00:00 2001 From: Sergey Ukustov Date: Mon, 26 Feb 2024 12:58:13 +0300 Subject: [PATCH 1/4] chore: use subpath imports --- benchmarks/alloc.js | 2 +- package.json | 37 ++++++++++++++++++++++++++++++++++ src/alloc.node.ts | 18 +++++++++++++++++ src/alloc.ts | 10 --------- src/compare.node.ts | 6 ++++++ src/compare.ts | 4 ---- src/concat.node.ts | 8 ++++++++ src/concat.ts | 8 ++------ src/from-string.node.ts | 26 ++++++++++++++++++++++++ src/from-string.ts | 5 ----- src/index.ts | 8 ++++---- src/to-string.node.ts | 25 +++++++++++++++++++++++ src/to-string.ts | 3 --- src/util/as-uint8array.node.ts | 7 +++++++ src/util/as-uint8array.ts | 4 ---- src/util/bases.ts | 2 +- src/xor.ts | 4 ++-- test/alloc.spec.ts | 2 +- test/compare.spec.ts | 2 +- test/concat.spec.ts | 4 ++-- test/from-string.spec.ts | 4 ++-- test/to-string.spec.ts | 2 +- tsconfig.json | 10 ++++++++- 23 files changed, 153 insertions(+), 48 deletions(-) create mode 100644 src/alloc.node.ts create mode 100644 src/compare.node.ts create mode 100644 src/concat.node.ts create mode 100644 src/from-string.node.ts create mode 100644 src/to-string.node.ts create mode 100644 src/util/as-uint8array.node.ts diff --git a/benchmarks/alloc.js b/benchmarks/alloc.js index 200ca1c..ae35f64 100644 --- a/benchmarks/alloc.js +++ b/benchmarks/alloc.js @@ -6,7 +6,7 @@ $ npx playwright-test benchmarks/alloc.js --runner benchmark */ import Benchmark from 'benchmark' -import { alloc, allocUnsafe } from '../src/dist/alloc.js' +import { alloc, allocUnsafe } from '../dist/src/alloc.js' const LENGTH = 1024 diff --git a/package.json b/package.json index 4c9d38c..e221dfb 100644 --- a/package.json +++ b/package.json @@ -47,14 +47,17 @@ }, "./alloc": { "types": "./dist/src/alloc.d.ts", + "node": "./dist/src/alloc.node.js", "import": "./dist/src/alloc.js" }, "./compare": { "types": "./dist/src/compare.d.ts", + "node": "./dist/src/compare.node.js", "import": "./dist/src/compare.js" }, "./concat": { "types": "./dist/src/concat.d.ts", + "node": "./dist/src/concat.node.js", "import": "./dist/src/concat.js" }, "./equals": { @@ -63,10 +66,12 @@ }, "./from-string": { "types": "./dist/src/from-string.d.ts", + "node": "./dist/src/from-string.node.js", "import": "./dist/src/from-string.js" }, "./to-string": { "types": "./dist/src/to-string.d.ts", + "node": "./dist/src/to-string.node.js", "import": "./dist/src/to-string.js" }, "./xor": { @@ -74,6 +79,38 @@ "import": "./dist/src/xor.js" } }, + "imports": { + "#util/as-uint8array": { + "types": "./dist/src/util/as-uint8array.d.ts", + "node": "./dist/src/util/as-uint8array.node.js", + "import": "./dist/src/util/as-uint8array.js" + }, + "#alloc": { + "types": "./dist/src/alloc.d.ts", + "node": "./dist/src/alloc.node.js", + "import": "./dist/src/alloc.js" + }, + "#compare": { + "types": "./dist/src/compare.d.ts", + "node": "./dist/src/compare.node.js", + "import": "./dist/src/compare.js" + }, + "#concat": { + "types": "./dist/src/concat.d.ts", + "node": "./dist/src/concat.node.js", + "import": "./dist/src/concat.js" + }, + "#from-string": { + "types": "./dist/src/from-string.d.ts", + "node": "./dist/src/from-string.node.js", + "import": "./dist/src/from-string.js" + }, + "#to-string": { + "types": "./dist/src/to-string.d.ts", + "node": "./dist/src/to-string.node.js", + "import": "./dist/src/to-string.js" + } + }, "eslintConfig": { "extends": "ipfs", "parserOptions": { diff --git a/src/alloc.node.ts b/src/alloc.node.ts new file mode 100644 index 0000000..61adc83 --- /dev/null +++ b/src/alloc.node.ts @@ -0,0 +1,18 @@ +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 { + return asUint8Array(globalThis.Buffer.alloc(size)) +} + +/** + * Where possible returns a Uint8Array of the requested size that references + * 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 { + return asUint8Array(globalThis.Buffer.allocUnsafe(size)) +} diff --git a/src/alloc.ts b/src/alloc.ts index ed3d5f9..8cbb77b 100644 --- a/src/alloc.ts +++ b/src/alloc.ts @@ -1,14 +1,8 @@ -import { asUint8Array } from './util/as-uint8array.js' - /** * Returns a `Uint8Array` of the requested size. Referenced memory will * be initialized to 0. */ export function alloc (size: number = 0): Uint8Array { - if (globalThis.Buffer?.alloc != null) { - return asUint8Array(globalThis.Buffer.alloc(size)) - } - return new Uint8Array(size) } @@ -18,9 +12,5 @@ export function alloc (size: number = 0): Uint8Array { * overwrite every value in the returned `Uint8Array`. */ export function allocUnsafe (size: number = 0): Uint8Array { - if (globalThis.Buffer?.allocUnsafe != null) { - return asUint8Array(globalThis.Buffer.allocUnsafe(size)) - } - return new Uint8Array(size) } diff --git a/src/compare.node.ts b/src/compare.node.ts new file mode 100644 index 0000000..5810920 --- /dev/null +++ b/src/compare.node.ts @@ -0,0 +1,6 @@ +/** + * Can be used with Array.sort to sort and array with Uint8Array entries + */ +export function compare (a: Uint8Array, b: Uint8Array): number { + return globalThis.Buffer.compare(a, b) +} diff --git a/src/compare.ts b/src/compare.ts index 633f72a..1a361c5 100644 --- a/src/compare.ts +++ b/src/compare.ts @@ -2,10 +2,6 @@ * Can be used with Array.sort to sort and array with Uint8Array entries */ export function compare (a: Uint8Array, b: Uint8Array): number { - if (globalThis.Buffer != null) { - return globalThis.Buffer.compare(a, b) - } - for (let i = 0; i < a.byteLength; i++) { if (a[i] < b[i]) { return -1 diff --git a/src/concat.node.ts b/src/concat.node.ts new file mode 100644 index 0000000..d59d1cb --- /dev/null +++ b/src/concat.node.ts @@ -0,0 +1,8 @@ +import { asUint8Array } from '#util/as-uint8array' + +/** + * Returns a new Uint8Array created by concatenating the passed Uint8Arrays + */ +export function concat (arrays: Uint8Array[], length?: number): Uint8Array { + return asUint8Array(globalThis.Buffer.concat(arrays, length)) +} diff --git a/src/concat.ts b/src/concat.ts index 71ad369..2ef6de1 100644 --- a/src/concat.ts +++ b/src/concat.ts @@ -1,14 +1,10 @@ -import { allocUnsafe } from './alloc.js' -import { asUint8Array } from './util/as-uint8array.js' +import { allocUnsafe } from '#alloc' +import { asUint8Array } from '#util/as-uint8array' /** * Returns a new Uint8Array created by concatenating the passed Uint8Arrays */ export function concat (arrays: Uint8Array[], length?: number): Uint8Array { - if (globalThis.Buffer != null) { - return asUint8Array(globalThis.Buffer.concat(arrays, length)) - } - 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 new file mode 100644 index 0000000..2091b96 --- /dev/null +++ b/src/from-string.node.ts @@ -0,0 +1,26 @@ +import { asUint8Array } from '#util/as-uint8array' +import bases, { type SupportedEncodings } from './util/bases.js' + +export type { SupportedEncodings } + +/** + * Create a `Uint8Array` from the passed string + * + * Supports `utf8`, `utf-8`, `hex`, and any encoding supported by the multiformats module. + * + * Also `ascii` which is similar to node's 'binary' encoding. + */ +export function fromString (string: string, encoding: SupportedEncodings = 'utf8'): Uint8Array { + const base = bases[encoding] + + if (base == null) { + throw new Error(`Unsupported encoding "${encoding}"`) + } + + if (encoding === 'utf8' || encoding === 'utf-8') { + return asUint8Array(globalThis.Buffer.from(string, 'utf-8')) + } + + // add multibase prefix + return base.decoder.decode(`${base.prefix}${string}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions +} diff --git a/src/from-string.ts b/src/from-string.ts index 550e1b3..dd629ec 100644 --- a/src/from-string.ts +++ b/src/from-string.ts @@ -1,4 +1,3 @@ -import { asUint8Array } from './util/as-uint8array.js' import bases, { type SupportedEncodings } from './util/bases.js' export type { SupportedEncodings } @@ -17,10 +16,6 @@ export function fromString (string: string, encoding: SupportedEncodings = 'utf8 throw new Error(`Unsupported encoding "${encoding}"`) } - if ((encoding === 'utf8' || encoding === 'utf-8') && globalThis.Buffer != null && globalThis.Buffer.from != null) { - return asUint8Array(globalThis.Buffer.from(string, 'utf-8')) - } - // add multibase prefix return base.decoder.decode(`${base.prefix}${string}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions } diff --git a/src/index.ts b/src/index.ts index 5a1b432..c9ea2c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -143,11 +143,11 @@ * ``` */ -import { compare } from './compare.js' -import { concat } from './concat.js' +import { compare } from '#compare' +import { concat } from '#concat' import { equals } from './equals.js' -import { fromString } from './from-string.js' -import { toString } from './to-string.js' +import { fromString } from '#from-string' +import { toString } from '#to-string' import { xor } from './xor.js' export { diff --git a/src/to-string.node.ts b/src/to-string.node.ts new file mode 100644 index 0000000..7877898 --- /dev/null +++ b/src/to-string.node.ts @@ -0,0 +1,25 @@ +import bases, { type SupportedEncodings } from './util/bases.js' + +export type { SupportedEncodings } + +/** + * Turns a `Uint8Array` into a string. + * + * Supports `utf8`, `utf-8` and any encoding supported by the multibase module. + * + * Also `ascii` which is similar to node's 'binary' encoding. + */ +export function toString (array: Uint8Array, encoding: SupportedEncodings = 'utf8'): string { + const base = bases[encoding] + + if (base == null) { + throw new Error(`Unsupported encoding "${encoding}"`) + } + + if (encoding === 'utf8' || encoding === 'utf-8') { + return globalThis.Buffer.from(array.buffer, array.byteOffset, array.byteLength).toString('utf8') + } + + // strip multibase prefix + return base.encoder.encode(array).substring(1) +} diff --git a/src/to-string.ts b/src/to-string.ts index 277486b..514a135 100644 --- a/src/to-string.ts +++ b/src/to-string.ts @@ -16,9 +16,6 @@ export function toString (array: Uint8Array, encoding: SupportedEncodings = 'utf throw new Error(`Unsupported encoding "${encoding}"`) } - if ((encoding === 'utf8' || encoding === 'utf-8') && globalThis.Buffer != null && globalThis.Buffer.from != null) { - return globalThis.Buffer.from(array.buffer, array.byteOffset, array.byteLength).toString('utf8') - } // strip multibase prefix return base.encoder.encode(array).substring(1) diff --git a/src/util/as-uint8array.node.ts b/src/util/as-uint8array.node.ts new file mode 100644 index 0000000..0d39d72 --- /dev/null +++ b/src/util/as-uint8array.node.ts @@ -0,0 +1,7 @@ +/** + * 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) +} diff --git a/src/util/as-uint8array.ts b/src/util/as-uint8array.ts index 9a24af5..8712fdd 100644 --- a/src/util/as-uint8array.ts +++ b/src/util/as-uint8array.ts @@ -3,9 +3,5 @@ * into vanilla Uint8Arrays */ export function asUint8Array (buf: Uint8Array): Uint8Array { - if (globalThis.Buffer != null) { - return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) - } - return buf } diff --git a/src/util/bases.ts b/src/util/bases.ts index f4b1f81..a348c4c 100644 --- a/src/util/bases.ts +++ b/src/util/bases.ts @@ -1,5 +1,5 @@ import { bases } from 'multiformats/basics' -import { allocUnsafe } from '../alloc.js' +import { allocUnsafe } from '#alloc' import type { MultibaseCodec } from 'multiformats' function createCodec (name: string, prefix: string, encode: (buf: Uint8Array) => string, decode: (str: string) => Uint8Array): MultibaseCodec { diff --git a/src/xor.ts b/src/xor.ts index e53435d..d4f9819 100644 --- a/src/xor.ts +++ b/src/xor.ts @@ -1,5 +1,5 @@ -import { allocUnsafe } from './alloc.js' -import { asUint8Array } from './util/as-uint8array.js' +import { allocUnsafe } from '#alloc' +import { asUint8Array } from '#util/as-uint8array' /** * Returns the xor distance between two arrays diff --git a/test/alloc.spec.ts b/test/alloc.spec.ts index 719d4bf..a12dc3c 100644 --- a/test/alloc.spec.ts +++ b/test/alloc.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ import { expect } from 'aegir/chai' -import { alloc, allocUnsafe } from '../src/alloc.js' +import { alloc, allocUnsafe } from '#alloc' describe('Uint8Array alloc', () => { it('can alloc memory', () => { diff --git a/test/compare.spec.ts b/test/compare.spec.ts index 6f31c3c..3e2dd87 100644 --- a/test/compare.spec.ts +++ b/test/compare.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ import { expect } from 'aegir/chai' -import { compare } from '../src/compare.js' +import { compare } from '#compare' describe('Uint8Array compare', () => { it('is stable', () => { diff --git a/test/concat.spec.ts b/test/concat.spec.ts index d2b8720..1b859ce 100644 --- a/test/concat.spec.ts +++ b/test/concat.spec.ts @@ -1,8 +1,8 @@ /* eslint-env mocha */ import { expect } from 'aegir/chai' -import { alloc } from '../src/alloc.js' -import { concat } from '../src/concat.js' +import { alloc } from '#alloc' +import { concat } from '#concat' describe('Uint8Array concat', () => { it('concats two Uint8Arrays', () => { diff --git a/test/from-string.spec.ts b/test/from-string.spec.ts index 3f35140..e17e453 100644 --- a/test/from-string.spec.ts +++ b/test/from-string.spec.ts @@ -1,8 +1,8 @@ /* eslint-env mocha */ import { expect } from 'aegir/chai' -import { fromString, type SupportedEncodings } from '../src/from-string.js' -import { toString } from '../src/to-string.js' +import { fromString, type SupportedEncodings } from '#from-string' +import { toString } from '#to-string' import bases from '../src/util/bases.js' const supportedBases = Object.keys(bases) as SupportedEncodings[] diff --git a/test/to-string.spec.ts b/test/to-string.spec.ts index 9783de2..8ddc0ce 100644 --- a/test/to-string.spec.ts +++ b/test/to-string.spec.ts @@ -1,7 +1,7 @@ /* eslint-env mocha */ import { expect } from 'aegir/chai' -import { toString } from '../src/to-string.js' +import { toString } from '#to-string' describe('Uint8Array toString', () => { it('creates a String from a Uint8Array', () => { diff --git a/tsconfig.json b/tsconfig.json index 36ca282..24e41de 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,15 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "./dist" + "outDir": "./dist", + "paths": { + "#util/as-uint8array": ["./src/util/as-uint8array.ts"], + "#alloc": ["./src/alloc.ts"], + "#compare": ["./src/compare.ts"], + "#concat": ["./src/concat.ts"], + "#from-string": ["./src/from-string.ts"], + "#to-string": ["./src/to-string.ts"] + } }, "include": [ "src", From f18170d269ee0b21362f031fe250271e77493b19 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 7 Mar 2024 12:35:46 +0100 Subject: [PATCH 2/4] chore: fix linting --- src/from-string.node.ts | 2 +- src/index.ts | 4 ++-- src/to-string.ts | 1 - src/util/bases.ts | 2 +- test/from-string.spec.ts | 2 +- 5 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/from-string.node.ts b/src/from-string.node.ts index 2091b96..3e5d880 100644 --- a/src/from-string.node.ts +++ b/src/from-string.node.ts @@ -1,5 +1,5 @@ -import { asUint8Array } from '#util/as-uint8array' import bases, { type SupportedEncodings } from './util/bases.js' +import { asUint8Array } from '#util/as-uint8array' export type { SupportedEncodings } diff --git a/src/index.ts b/src/index.ts index c9ea2c3..f83adc7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -143,12 +143,12 @@ * ``` */ +import { equals } from './equals.js' +import { xor } from './xor.js' import { compare } from '#compare' import { concat } from '#concat' -import { equals } from './equals.js' import { fromString } from '#from-string' import { toString } from '#to-string' -import { xor } from './xor.js' export { compare, diff --git a/src/to-string.ts b/src/to-string.ts index 514a135..5cf264f 100644 --- a/src/to-string.ts +++ b/src/to-string.ts @@ -16,7 +16,6 @@ export function toString (array: Uint8Array, encoding: SupportedEncodings = 'utf throw new Error(`Unsupported encoding "${encoding}"`) } - // strip multibase prefix return base.encoder.encode(array).substring(1) } diff --git a/src/util/bases.ts b/src/util/bases.ts index a348c4c..16f305f 100644 --- a/src/util/bases.ts +++ b/src/util/bases.ts @@ -1,6 +1,6 @@ import { bases } from 'multiformats/basics' -import { allocUnsafe } from '#alloc' import type { MultibaseCodec } from 'multiformats' +import { allocUnsafe } from '#alloc' function createCodec (name: string, prefix: string, encode: (buf: Uint8Array) => string, decode: (str: string) => Uint8Array): MultibaseCodec { return { diff --git a/test/from-string.spec.ts b/test/from-string.spec.ts index e17e453..05b6f28 100644 --- a/test/from-string.spec.ts +++ b/test/from-string.spec.ts @@ -1,9 +1,9 @@ /* eslint-env mocha */ import { expect } from 'aegir/chai' +import bases from '../src/util/bases.js' import { fromString, type SupportedEncodings } from '#from-string' import { toString } from '#to-string' -import bases from '../src/util/bases.js' const supportedBases = Object.keys(bases) as SupportedEncodings[] From f3078ae53590cf1987b0bb92d889c05a29b3ce04 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 7 Mar 2024 13:07:22 +0100 Subject: [PATCH 3/4] chore: import buffer from node:buffer --- README.md | 19 +++++++++++++++++-- benchmarks/alloc.js | 2 +- benchmarks/concat.js | 6 +++--- benchmarks/from-string.js | 2 +- benchmarks/to-string.js | 4 ++-- src/alloc.node.ts | 5 +++-- src/compare.node.ts | 4 +++- src/concat.node.ts | 3 ++- src/from-string.node.ts | 3 ++- src/index.ts | 4 ++-- src/to-string.node.ts | 3 ++- 11 files changed, 38 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f38df54..ce96f84 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,21 @@ # About + + `Uint8Array`s bring memory-efficient(ish) byte handling to browsers - they are similar to Node.js `Buffer`s but lack a lot of the utility methods present on that class. This module exports a number of function that let you do common operations - joining Uint8Arrays together, seeing if they have the same contents etc. @@ -13,7 +28,7 @@ Since Node.js `Buffer`s are also `Uint8Array`s, it falls back to `Buffer` intern ## alloc(size) -Create a new `Uint8Array`. If `globalThis.Buffer` is defined, it will be used in preference to `globalThis.Uint8Array`. +Create a new `Uint8Array`. When running under Node.js, `Buffer` will be used in preference to `Uint8Array`. ### Example @@ -25,7 +40,7 @@ const buf = alloc(100) ## allocUnsafe(size) -Create a new `Uint8Array`. If `globalThis.Buffer` is defined, it will be used in preference to `globalThis.Uint8Array`. +Create a new `Uint8Array`. When running under Node.js, `Buffer` will be used in preference to `Uint8Array`. On platforms that support it, memory referenced by the returned `Uint8Array` will not be initialized. diff --git a/benchmarks/alloc.js b/benchmarks/alloc.js index ae35f64..0b36285 100644 --- a/benchmarks/alloc.js +++ b/benchmarks/alloc.js @@ -6,7 +6,7 @@ $ npx playwright-test benchmarks/alloc.js --runner benchmark */ import Benchmark from 'benchmark' -import { alloc, allocUnsafe } from '../dist/src/alloc.js' +import { alloc, allocUnsafe } from '#alloc' const LENGTH = 1024 diff --git a/benchmarks/concat.js b/benchmarks/concat.js index 4c1fe89..417c1fb 100644 --- a/benchmarks/concat.js +++ b/benchmarks/concat.js @@ -6,9 +6,9 @@ $ npx playwright-test benchmarks/concat.js --runner benchmark */ import Benchmark from 'benchmark' -import { allocUnsafe } from '../dist/src/alloc.js' -import { concat } from '../dist/src/concat.js' -import { fromString } from '../dist/src/from-string.js' +import { allocUnsafe } from '#alloc' +import { concat } from '#concat' +import { fromString } from '#from-string' const string = 'Hello world, this is a Uint8Array created from a string' const DATA1 = fromString(string) diff --git a/benchmarks/from-string.js b/benchmarks/from-string.js index 7851c86..6ba4b45 100644 --- a/benchmarks/from-string.js +++ b/benchmarks/from-string.js @@ -6,7 +6,7 @@ $ npx playwright-test benchmarks/from-string.js --runner benchmark */ import Benchmark from 'benchmark' -import { fromString } from '../dist/src/from-string.js' +import { fromString } from '#from-string' const string = 'Hello world, this is a Uint8Array created from a string' const DATA = fromString(string) diff --git a/benchmarks/to-string.js b/benchmarks/to-string.js index e98a186..b21995b 100644 --- a/benchmarks/to-string.js +++ b/benchmarks/to-string.js @@ -6,8 +6,8 @@ $ npx playwright-test benchmarks/to-string.js --runner benchmark */ import Benchmark from 'benchmark' -import { fromString } from '../dist/src/from-string.js' -import { toString } from '../dist/src/to-string.js' +import { fromString } from '#from-string' +import { toString } from '#to-string' const string = 'Hello world, this is a Uint8Array created from a string' const DATA = fromString(string) diff --git a/src/alloc.node.ts b/src/alloc.node.ts index 61adc83..211b98b 100644 --- a/src/alloc.node.ts +++ b/src/alloc.node.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'node:buffer' import { asUint8Array } from '#util/as-uint8array' /** @@ -5,7 +6,7 @@ import { asUint8Array } from '#util/as-uint8array' * be initialized to 0. */ export function alloc (size: number = 0): Uint8Array { - return asUint8Array(globalThis.Buffer.alloc(size)) + return asUint8Array(Buffer.alloc(size)) } /** @@ -14,5 +15,5 @@ export function alloc (size: number = 0): Uint8Array { * overwrite every value in the returned `Uint8Array`. */ export function allocUnsafe (size: number = 0): Uint8Array { - return asUint8Array(globalThis.Buffer.allocUnsafe(size)) + return asUint8Array(Buffer.allocUnsafe(size)) } diff --git a/src/compare.node.ts b/src/compare.node.ts index 5810920..f95376d 100644 --- a/src/compare.node.ts +++ b/src/compare.node.ts @@ -1,6 +1,8 @@ +import { Buffer } from 'node:buffer' + /** * Can be used with Array.sort to sort and array with Uint8Array entries */ export function compare (a: Uint8Array, b: Uint8Array): number { - return globalThis.Buffer.compare(a, b) + return Buffer.compare(a, b) } diff --git a/src/concat.node.ts b/src/concat.node.ts index d59d1cb..94d2187 100644 --- a/src/concat.node.ts +++ b/src/concat.node.ts @@ -1,8 +1,9 @@ +import { Buffer } from 'node:buffer' import { asUint8Array } from '#util/as-uint8array' /** * Returns a new Uint8Array created by concatenating the passed Uint8Arrays */ export function concat (arrays: Uint8Array[], length?: number): Uint8Array { - return asUint8Array(globalThis.Buffer.concat(arrays, length)) + return asUint8Array(Buffer.concat(arrays, length)) } diff --git a/src/from-string.node.ts b/src/from-string.node.ts index 3e5d880..67496a8 100644 --- a/src/from-string.node.ts +++ b/src/from-string.node.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'node:buffer' import bases, { type SupportedEncodings } from './util/bases.js' import { asUint8Array } from '#util/as-uint8array' @@ -18,7 +19,7 @@ export function fromString (string: string, encoding: SupportedEncodings = 'utf8 } if (encoding === 'utf8' || encoding === 'utf-8') { - return asUint8Array(globalThis.Buffer.from(string, 'utf-8')) + return asUint8Array(Buffer.from(string, 'utf-8')) } // add multibase prefix diff --git a/src/index.ts b/src/index.ts index f83adc7..efdb141 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ * * ## alloc(size) * - * Create a new `Uint8Array`. If `globalThis.Buffer` is defined, it will be used in preference to `globalThis.Uint8Array`. + * Create a new `Uint8Array`. When running under Node.js, `Buffer` will be used in preference to `Uint8Array`. * * ### Example * @@ -21,7 +21,7 @@ * * ## allocUnsafe(size) * - * Create a new `Uint8Array`. If `globalThis.Buffer` is defined, it will be used in preference to `globalThis.Uint8Array`. + * Create a new `Uint8Array`. When running under Node.js, `Buffer` will be used in preference to `Uint8Array`. * * On platforms that support it, memory referenced by the returned `Uint8Array` will not be initialized. * diff --git a/src/to-string.node.ts b/src/to-string.node.ts index 7877898..183c64c 100644 --- a/src/to-string.node.ts +++ b/src/to-string.node.ts @@ -1,3 +1,4 @@ +import { Buffer } from 'node:buffer' import bases, { type SupportedEncodings } from './util/bases.js' export type { SupportedEncodings } @@ -17,7 +18,7 @@ export function toString (array: Uint8Array, encoding: SupportedEncodings = 'utf } if (encoding === 'utf8' || encoding === 'utf-8') { - return globalThis.Buffer.from(array.buffer, array.byteOffset, array.byteLength).toString('utf8') + return Buffer.from(array.buffer, array.byteOffset, array.byteLength).toString('utf8') } // strip multibase prefix From 8dc30b2c00607f4110052afbe7e6961c430deb90 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Thu, 7 Mar 2024 13:49:52 +0100 Subject: [PATCH 4/4] chore: support react-native --- package.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/package.json b/package.json index e221dfb..e2b578b 100644 --- a/package.json +++ b/package.json @@ -224,5 +224,13 @@ "@types/benchmark": "^2.1.1", "aegir": "^42.2.3", "benchmark": "^2.1.4" + }, + "react-native": { + "#util/as-uint8array": "./dist/src/util/as-uint8array.js", + "#alloc": "./dist/src/alloc.js", + "#compare": "./dist/src/compare.js", + "#concat": "./dist/src/concat.js", + "#from-string": "./dist/src/from-string.js", + "#to-string": "./dist/src/to-string.js" } }