Skip to content

Commit

Permalink
chore: use subpath imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ukstv committed Feb 26, 2024
1 parent dc81e75 commit cf396f2
Show file tree
Hide file tree
Showing 23 changed files with 153 additions and 48 deletions.
2 changes: 1 addition & 1 deletion benchmarks/alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -63,17 +66,51 @@
},
"./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": {
"types": "./dist/src/xor.d.ts",
"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": {
Expand Down
18 changes: 18 additions & 0 deletions src/alloc.node.ts
Original file line number Diff line number Diff line change
@@ -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))
}
10 changes: 0 additions & 10 deletions src/alloc.ts
Original file line number Diff line number Diff line change
@@ -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)
}

Expand All @@ -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)
}
6 changes: 6 additions & 0 deletions src/compare.node.ts
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 0 additions & 4 deletions src/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/concat.node.ts
Original file line number Diff line number Diff line change
@@ -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))
}
8 changes: 2 additions & 6 deletions src/concat.ts
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
26 changes: 26 additions & 0 deletions src/from-string.node.ts
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 0 additions & 5 deletions src/from-string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { asUint8Array } from './util/as-uint8array.js'
import bases, { type SupportedEncodings } from './util/bases.js'

export type { SupportedEncodings }
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 25 additions & 0 deletions src/to-string.node.ts
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 0 additions & 3 deletions src/to-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/util/as-uint8array.node.ts
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 0 additions & 4 deletions src/util/as-uint8array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/util/bases.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
Expand Down
4 changes: 2 additions & 2 deletions src/xor.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/alloc.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/compare.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/concat.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/from-string.spec.ts
Original file line number Diff line number Diff line change
@@ -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[]
Expand Down
2 changes: 1 addition & 1 deletion test/to-string.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
10 changes: 9 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit cf396f2

Please sign in to comment.