From e4c9627da195b419ee9f8c45d45931f537c5dee1 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Wed, 12 Oct 2022 11:59:23 +0100 Subject: [PATCH] feat!: convert to typescript and release as ESM-only (#28) Updates multiformats to ESM-only so since this module can no longer be used from CJS code, convert this module to typescript and publish as ESM-only as well. BREAKING CHANGE: this module is now ESM-only --- .aegir.cjs | 11 -- .aegir.js | 8 + .github/dependabot.yml | 11 ++ .github/workflows/automerge.yml | 8 + .github/workflows/js-test-and-release.yml | 145 ++++++++++++++ .github/workflows/main.yml | 103 ---------- LICENSE | 4 + LICENSE-APACHE | 5 + LICENSE-MIT | 19 ++ README.md | 54 ++++- benchmarks/alloc.js | 2 +- benchmarks/concat.js | 10 +- benchmarks/from-string.js | 6 +- benchmarks/to-string.js | 4 +- package.json | 187 ++++++++++++++---- src/{alloc.js => alloc.ts} | 14 +- src/{compare.js => compare.ts} | 5 +- src/{concat.js => concat.ts} | 7 +- src/{equals.js => equals.ts} | 5 +- src/{from-string.js => from-string.ts} | 16 +- src/{index.js => index.ts} | 0 src/{to-string.js => to-string.ts} | 10 +- .../{as-uint8array.js => as-uint8array.ts} | 5 +- src/util/{bases.js => bases.ts} | 23 +-- src/{xor.js => xor.ts} | 5 +- test/{alloc.spec.js => alloc.spec.ts} | 2 +- test/{compare.spec.js => compare.spec.ts} | 2 +- test/{concat.spec.js => concat.spec.ts} | 2 +- test/{equals.spec.js => equals.spec.ts} | 2 +- ...rom-string.spec.js => from-string.spec.ts} | 8 +- test/{to-string.spec.js => to-string.spec.ts} | 2 +- test/{xor.spec.js => xor.spec.ts} | 2 +- tsconfig.json | 2 +- 33 files changed, 437 insertions(+), 252 deletions(-) delete mode 100644 .aegir.cjs create mode 100644 .aegir.js create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/automerge.yml create mode 100644 .github/workflows/js-test-and-release.yml delete mode 100644 .github/workflows/main.yml create mode 100644 LICENSE create mode 100644 LICENSE-APACHE create mode 100644 LICENSE-MIT rename src/{alloc.js => alloc.ts} (63%) rename src/{compare.js => compare.ts} (79%) rename src/{concat.js => concat.ts} (75%) rename src/{equals.js => equals.ts} (77%) rename src/{from-string.js => from-string.ts} (59%) rename src/{index.js => index.ts} (100%) rename src/{to-string.js => to-string.ts} (72%) rename src/util/{as-uint8array.js => as-uint8array.ts} (70%) rename src/util/{bases.js => bases.ts} (64%) rename src/{xor.js => xor.ts} (82%) rename test/{alloc.spec.js => alloc.spec.ts} (95%) rename test/{compare.spec.js => compare.spec.ts} (95%) rename test/{concat.spec.js => concat.spec.ts} (96%) rename test/{equals.spec.js => equals.spec.ts} (93%) rename test/{from-string.spec.js => from-string.spec.ts} (87%) rename test/{to-string.spec.js => to-string.spec.ts} (97%) rename test/{xor.spec.js => xor.spec.ts} (95%) diff --git a/.aegir.cjs b/.aegir.cjs deleted file mode 100644 index 7c35036..0000000 --- a/.aegir.cjs +++ /dev/null @@ -1,11 +0,0 @@ -'use strict' - -/** @type {import('aegir').PartialOptions} */ -module.exports = { - build: { - bundlesizeMax: '7KB', - config: { - entryPoints: ['src/index.js'] - } - } -} diff --git a/.aegir.js b/.aegir.js new file mode 100644 index 0000000..05e0f47 --- /dev/null +++ b/.aegir.js @@ -0,0 +1,8 @@ +/** @type {import('aegir').PartialOptions} */ +const config = { + build: { + bundlesizeMax: '7KB' + } +} + +export default config diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..0bc3b42 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,11 @@ +version: 2 +updates: +- package-ecosystem: npm + directory: "/" + schedule: + interval: daily + time: "10:00" + open-pull-requests-limit: 10 + commit-message: + prefix: "deps" + prefix-development: "deps(dev)" diff --git a/.github/workflows/automerge.yml b/.github/workflows/automerge.yml new file mode 100644 index 0000000..d57c2a0 --- /dev/null +++ b/.github/workflows/automerge.yml @@ -0,0 +1,8 @@ +name: Automerge +on: [ pull_request ] + +jobs: + automerge: + uses: protocol/.github/.github/workflows/automerge.yml@master + with: + job: 'automerge' diff --git a/.github/workflows/js-test-and-release.yml b/.github/workflows/js-test-and-release.yml new file mode 100644 index 0000000..d155996 --- /dev/null +++ b/.github/workflows/js-test-and-release.yml @@ -0,0 +1,145 @@ +name: test & maybe release +on: + push: + branches: + - master # with #262 - ${{{ github.default_branch }}} + pull_request: + branches: + - master # with #262 - ${{{ github.default_branch }}} + +jobs: + + check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present lint + - run: npm run --if-present dep-check + + test-node: + needs: check + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [windows-latest, ubuntu-latest, macos-latest] + node: [16] + fail-fast: true + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node }} + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:node + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + flags: node + + test-chrome: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:chrome + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + flags: chrome + + test-chrome-webworker: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:chrome-webworker + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + flags: chrome-webworker + + test-firefox: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:firefox + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + flags: firefox + + test-firefox-webworker: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npm run --if-present test:firefox-webworker + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + flags: firefox-webworker + + test-electron-main: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npx xvfb-maybe npm run --if-present test:electron-main + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + flags: electron-main + + test-electron-renderer: + needs: check + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - run: npx xvfb-maybe npm run --if-present test:electron-renderer + - uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 + with: + flags: electron-renderer + + release: + needs: [test-node, test-chrome, test-chrome-webworker, test-firefox, test-firefox-webworker, test-electron-main, test-electron-renderer] + runs-on: ubuntu-latest + if: github.event_name == 'push' && github.ref == 'refs/heads/master' # with #262 - 'refs/heads/${{{ github.default_branch }}}' + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-node@v3 + with: + node-version: lts/* + - uses: ipfs/aegir/actions/cache-node-modules@master + - uses: ipfs/aegir/actions/docker-login@master + with: + docker-token: ${{ secrets.DOCKER_TOKEN }} + docker-username: ${{ secrets.DOCKER_USERNAME }} + - run: npm run --if-present release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index b984eda..0000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,103 +0,0 @@ -name: ci -on: - push: - branches: - - master - pull_request: - branches: - - master - -jobs: - check: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: npx aegir lint - - uses: gozala/typescript-error-reporter-action@v1.0.8 - - run: npx aegir build - - run: npx aegir dep-check - - uses: ipfs/aegir/actions/bundle-size@master - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - test-node: - needs: check - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: [windows-latest, ubuntu-latest, macos-latest] - node: [12, 14, 16] - fail-fast: true - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: ${{ matrix.node }} - - run: npm install - - run: npx aegir test -t node --bail --cov - - uses: codecov/codecov-action@v1 - test-chrome: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: microsoft/playwright-github-action@v1 - - run: npm install - - run: npx aegir test -t browser --bail --cov - - uses: codecov/codecov-action@v1 - test-chrome-webworker: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: microsoft/playwright-github-action@v1 - - run: npm install - - run: npx aegir test -t webworker --bail - test-firefox: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: microsoft/playwright-github-action@v1 - - run: npm install - - run: npx aegir test -t browser --bail -- --browser firefox - test-firefox-webworker: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: microsoft/playwright-github-action@v1 - - run: npm install - - run: npx aegir test -t webworker --bail -- --browser firefox - test-webkit: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: microsoft/playwright-github-action@v1 - - run: npm install - - run: npx aegir test -t browser --bail -- --browser webkit - test-webkit-webworker: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: microsoft/playwright-github-action@v1 - - run: npm install - - run: npx aegir test -t webworker --bail -- --browser webkit - test-electron-main: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: npm run build -- --esm-tests # build tests with esm - - run: npx xvfb-maybe aegir test -t electron-main -f "dist/cjs/node-test/*js" --bail - test-electron-renderer: - needs: check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: npm install - - run: npm run build -- --esm-tests # build tests with esm - - run: npx xvfb-maybe aegir test -t electron-renderer -f "dist/cjs/node-test/*js" --bail diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..20ce483 --- /dev/null +++ b/LICENSE @@ -0,0 +1,4 @@ +This project is dual licensed under MIT and Apache-2.0. + +MIT: https://www.opensource.org/licenses/mit +Apache-2.0: https://www.apache.org/licenses/license-2.0 diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..14478a3 --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,5 @@ +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..72dc60d --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,19 @@ +The MIT License (MIT) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md index 4bc4398..bd3739c 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,13 @@ -# Uint8Arrays +# uint8arrays -Some utility functions to make dealing with `Uint8Array`s more pleasant. +[![codecov](https://img.shields.io/codecov/c/github/achingbrain/uint8arrays.svg?style=flat-square)](https://codecov.io/gh/achingbrain/uint8arrays) +[![CI](https://img.shields.io/github/workflow/status/achingbrain/uint8arrays/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/achingbrain/uint8arrays/actions/workflows/js-test-and-release.yml) +> Utility functions to make dealing with Uint8Arrays easier + +## Table of contents + +- [Install](#install) - [API](#api) - [alloc(size)](#allocsize) - [Example](#example) @@ -9,7 +15,7 @@ Some utility functions to make dealing with `Uint8Array`s more pleasant. - [Example](#example-1) - [compare(a, b)](#comparea-b) - [Example](#example-2) - - [concat(arrays, [length])](#concatarrays-length) + - [concat(arrays, \[length])](#concatarrays-length) - [Example](#example-3) - [equals(a, b)](#equalsa-b) - [Example](#example-4) @@ -19,6 +25,35 @@ Some utility functions to make dealing with `Uint8Array`s more pleasant. - [Example](#example-6) - [xor(a, b)](#xora-b) - [Example](#example-7) +- [License](#license) +- [Contribute](#contribute) + +## Install + +```console +$ npm i uint8arrays +``` + +- [Install](#install) +- [API](#api) + - [alloc(size)](#allocsize) + - [Example](#example) + - [allocUnsafe(size)](#allocunsafesize) + - [Example](#example-1) + - [compare(a, b)](#comparea-b) + - [Example](#example-2) + - [concat(arrays, \[length])](#concatarrays-length) + - [Example](#example-3) + - [equals(a, b)](#equalsa-b) + - [Example](#example-4) + - [fromString(string, encoding = 'utf8')](#fromstringstring-encoding--utf8) + - [Example](#example-5) + - [toString(array, encoding = 'utf8')](#tostringarray-encoding--utf8) + - [Example](#example-6) + - [xor(a, b)](#xora-b) + - [Example](#example-7) +- [License](#license) +- [Contribute](#contribute) ## API @@ -71,7 +106,7 @@ console.info(sorted) // ] ``` -### concat(arrays, [length]) +### concat(arrays, \[length]) Concatenate one or more array-likes and return a `Uint8Array` with their contents. @@ -156,3 +191,14 @@ import { xor } from 'uint8arrays/xor' console.info(xor(Uint8Array.from([1, 0]), Uint8Array.from([0, 1]))) // Uint8Array[1, 1] ``` + +## License + +Licensed under either of + +- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / ) +- MIT ([LICENSE-MIT](LICENSE-MIT) / ) + +## Contribute + +Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. diff --git a/benchmarks/alloc.js b/benchmarks/alloc.js index ee2692b..200ca1c 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/alloc.js' +import { alloc, allocUnsafe } from '../src/dist/alloc.js' const LENGTH = 1024 diff --git a/benchmarks/concat.js b/benchmarks/concat.js index 78a28ac..1fb9205 100644 --- a/benchmarks/concat.js +++ b/benchmarks/concat.js @@ -1,14 +1,14 @@ /* eslint-disable no-console */ /* -$ node benchmarks/to-string.js -$ npx playwright-test benchmarks/to-string.js --runner benchmark +$ node benchmarks/concat.js +$ npx playwright-test benchmarks/concat.js --runner benchmark */ import Benchmark from 'benchmark' -import { fromString } from '../src/from-string.js' -import { concat } from '../src/concat.js' -import { allocUnsafe } from '../src/alloc.js' +import { fromString } from '../dist/src/from-string.js' +import { concat } from '../dist/src/concat.js' +import { allocUnsafe } from '../dist/src/alloc.js' 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 67d58d9..7851c86 100644 --- a/benchmarks/from-string.js +++ b/benchmarks/from-string.js @@ -1,12 +1,12 @@ /* eslint-disable no-console */ /* -$ node benchmarks/to-string.js -$ npx playwright-test benchmarks/to-string.js --runner benchmark +$ node benchmarks/from-string.js +$ npx playwright-test benchmarks/from-string.js --runner benchmark */ import Benchmark from 'benchmark' -import { fromString } from '../src/from-string.js' +import { fromString } from '../dist/src/from-string.js' 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 7e59dbe..e98a186 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 '../src/from-string.js' -import { toString } from '../src/to-string.js' +import { fromString } from '../dist/src/from-string.js' +import { toString } from '../dist/src/to-string.js' const string = 'Hello world, this is a Uint8Array created from a string' const DATA = fromString(string) diff --git a/package.json b/package.json index 46e6784..2469801 100644 --- a/package.json +++ b/package.json @@ -2,84 +2,187 @@ "name": "uint8arrays", "version": "3.1.1", "description": "Utility functions to make dealing with Uint8Arrays easier", - "main": "src/index.js", "author": "Alex Potsides ", - "homepage": "https://github.com/achingbrain/uint8arrays", - "bugs": "https://github.com/achingbrain/uint8arrays/issues", - "type": "module", - "types": "types/src/index.d.ts", + "license": "Apache-2.0 OR MIT", + "homepage": "https://github.com/achingbrain/uint8arrays#readme", "repository": { "type": "git", - "url": "https://github.com/achingbrain/uint8arrays.git" + "url": "git+https://github.com/achingbrain/uint8arrays.git" }, - "scripts": { - "test": "aegir test", - "lint": "aegir ts -p check && aegir lint", - "release": "aegir release", - "release-minor": "aegir release --type minor", - "release-major": "aegir release --type major", - "build": "aegir build" + "bugs": { + "url": "https://github.com/achingbrain/uint8arrays/issues" }, - "license": "MIT", - "dependencies": { - "multiformats": "^9.4.2" - }, - "devDependencies": { - "@types/benchmark": "^2.1.1", - "aegir": "^35.0.0", - "benchmark": "^2.1.4", - "util": "^0.12.4" - }, - "eslintConfig": { - "extends": "ipfs", - "parserOptions": { - "sourceType": "module" - }, - "ignorePatterns": [ - "!.aegir.js" - ] + "engines": { + "node": ">=16.0.0", + "npm": ">=7.0.0" }, + "type": "module", + "types": "./dist/src/index.d.ts", "typesVersions": { "*": { "*": [ - "types/src", - "types/src/*" + "*", + "dist/*", + "dist/src/*", + "dist/src/*/index" + ], + "src/*": [ + "*", + "dist/*", + "dist/src/*", + "dist/src/*/index" ] } }, + "files": [ + "src", + "dist/src", + "!dist/test", + "!**/*.tsbuildinfo" + ], "exports": { ".": { + "types": "./dist/src/index.d.ts", "import": "./src/index.js" }, "./alloc": { + "types": "./src/alloc.d.ts", "import": "./src/alloc.js" }, "./compare": { + "types": "./src/compare.d.ts", "import": "./src/compare.js" }, "./concat": { + "types": "./src/concat.d.ts", "import": "./src/concat.js" }, "./equals": { + "types": "./src/equals.d.ts", "import": "./src/equals.js" }, "./from-string": { + "types": "./src/from-string.d.ts", "import": "./src/from-string.js" }, "./to-string": { + "types": "./src/to-string.d.ts", "import": "./src/to-string.js" }, "./xor": { + "types": "./src/xor.d.ts", "import": "./src/xor.js" } }, - "contributors": [ - "achingbrain ", - "Cayman ", - "Irakli Gozalishvili ", - "Hugo Dias ", - "Mircea Nistor ", - "Rafael Ramalho ", - "Vasco Santos " - ] + "eslintConfig": { + "extends": "ipfs", + "parserOptions": { + "sourceType": "module" + } + }, + "release": { + "branches": [ + "master" + ], + "plugins": [ + [ + "@semantic-release/commit-analyzer", + { + "preset": "conventionalcommits", + "releaseRules": [ + { + "breaking": true, + "release": "major" + }, + { + "revert": true, + "release": "patch" + }, + { + "type": "feat", + "release": "minor" + }, + { + "type": "fix", + "release": "patch" + }, + { + "type": "docs", + "release": "patch" + }, + { + "type": "test", + "release": "patch" + }, + { + "type": "deps", + "release": "patch" + }, + { + "scope": "no-release", + "release": false + } + ] + } + ], + [ + "@semantic-release/release-notes-generator", + { + "preset": "conventionalcommits", + "presetConfig": { + "types": [ + { + "type": "feat", + "section": "Features" + }, + { + "type": "fix", + "section": "Bug Fixes" + }, + { + "type": "chore", + "section": "Trivial Changes" + }, + { + "type": "docs", + "section": "Documentation" + }, + { + "type": "deps", + "section": "Dependencies" + }, + { + "type": "test", + "section": "Tests" + } + ] + } + } + ], + "@semantic-release/changelog", + "@semantic-release/npm", + "@semantic-release/github", + "@semantic-release/git" + ] + }, + "scripts": { + "clean": "aegir clean", + "test": "aegir test", + "test:node": "aegir test -t node --cov", + "test:chrome": "aegir test -t browser --cov", + "test:chrome-webworker": "aegir test -t webworker", + "test:firefox": "aegir test -t browser -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -- --browser firefox", + "lint": "aegir lint", + "release": "aegir release", + "build": "aegir build" + }, + "dependencies": { + "multiformats": "^10.0.0" + }, + "devDependencies": { + "@types/benchmark": "^2.1.1", + "aegir": "^37.5.5", + "benchmark": "^2.1.4" + } } diff --git a/src/alloc.js b/src/alloc.ts similarity index 63% rename from src/alloc.js rename to src/alloc.ts index 1cf0b06..ed3d5f9 100644 --- a/src/alloc.js +++ b/src/alloc.ts @@ -3,12 +3,9 @@ import { asUint8Array } from './util/as-uint8array.js' /** * Returns a `Uint8Array` of the requested size. Referenced memory will * be initialized to 0. - * - * @param {number} [size] - * @returns {Uint8Array} */ -export function alloc (size = 0) { - if (globalThis.Buffer != null && globalThis.Buffer.alloc != null) { +export function alloc (size: number = 0): Uint8Array { + if (globalThis.Buffer?.alloc != null) { return asUint8Array(globalThis.Buffer.alloc(size)) } @@ -19,12 +16,9 @@ export function alloc (size = 0) { * 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`. - * - * @param {number} [size] - * @returns {Uint8Array} */ -export function allocUnsafe (size = 0) { - if (globalThis.Buffer != null && globalThis.Buffer.allocUnsafe != null) { +export function allocUnsafe (size: number = 0): Uint8Array { + if (globalThis.Buffer?.allocUnsafe != null) { return asUint8Array(globalThis.Buffer.allocUnsafe(size)) } diff --git a/src/compare.js b/src/compare.ts similarity index 79% rename from src/compare.js rename to src/compare.ts index d71d1f2..1a361c5 100644 --- a/src/compare.js +++ b/src/compare.ts @@ -1,10 +1,7 @@ /** * Can be used with Array.sort to sort and array with Uint8Array entries - * - * @param {Uint8Array} a - * @param {Uint8Array} b */ -export function compare (a, b) { +export function compare (a: Uint8Array, b: Uint8Array): number { for (let i = 0; i < a.byteLength; i++) { if (a[i] < b[i]) { return -1 diff --git a/src/concat.js b/src/concat.ts similarity index 75% rename from src/concat.js rename to src/concat.ts index 2db94d9..698be06 100644 --- a/src/concat.js +++ b/src/concat.ts @@ -3,12 +3,9 @@ import { asUint8Array } from './util/as-uint8array.js' /** * Returns a new Uint8Array created by concatenating the passed ArrayLikes - * - * @param {Array>} arrays - * @param {number} [length] */ -export function concat (arrays, length) { - if (!length) { +export function concat (arrays: Array>, length?: number): Uint8Array { + if (length == null) { length = arrays.reduce((acc, curr) => acc + curr.length, 0) } diff --git a/src/equals.js b/src/equals.ts similarity index 77% rename from src/equals.js rename to src/equals.ts index 3d770b8..e2e82a3 100644 --- a/src/equals.js +++ b/src/equals.ts @@ -1,10 +1,7 @@ /** * Returns true if the two passed Uint8Arrays have the same content - * - * @param {Uint8Array} a - * @param {Uint8Array} b */ -export function equals (a, b) { +export function equals (a: Uint8Array, b: Uint8Array): boolean { if (a === b) { return true } diff --git a/src/from-string.js b/src/from-string.ts similarity index 59% rename from src/from-string.js rename to src/from-string.ts index 7f88286..26a373f 100644 --- a/src/from-string.js +++ b/src/from-string.ts @@ -1,25 +1,17 @@ -import bases from './util/bases.js' +import bases, { SupportedEncodings } from './util/bases.js' import { asUint8Array } from './util/as-uint8array.js' -/** - * @typedef {import('./util/bases').SupportedEncodings} 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. - * - * @param {string} string - * @param {SupportedEncodings} [encoding=utf8] - utf8, base16, base64, base64urlpad, etc - * @returns {Uint8Array} */ -export function fromString (string, encoding = 'utf8') { +export function fromString (string: string, encoding: SupportedEncodings = 'utf8'): Uint8Array { const base = bases[encoding] - if (!base) { + if (base == null) { throw new Error(`Unsupported encoding "${encoding}"`) } @@ -28,5 +20,5 @@ export function fromString (string, encoding = 'utf8') { } // add multibase prefix - return base.decoder.decode(`${base.prefix}${string}`) + return base.decoder.decode(`${base.prefix}${string}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions } diff --git a/src/index.js b/src/index.ts similarity index 100% rename from src/index.js rename to src/index.ts diff --git a/src/to-string.js b/src/to-string.ts similarity index 72% rename from src/to-string.js rename to src/to-string.ts index 95d20eb..4de30f6 100644 --- a/src/to-string.js +++ b/src/to-string.ts @@ -1,4 +1,4 @@ -import bases from './util/bases.js' +import bases, { SupportedEncodings } from './util/bases.js' /** * @typedef {import('./util/bases').SupportedEncodings} SupportedEncodings @@ -10,15 +10,11 @@ import bases from './util/bases.js' * Supports `utf8`, `utf-8` and any encoding supported by the multibase module. * * Also `ascii` which is similar to node's 'binary' encoding. - * - * @param {Uint8Array} array - The array to turn into a string - * @param {SupportedEncodings} [encoding=utf8] - The encoding to use - * @returns {string} */ -export function toString (array, encoding = 'utf8') { +export function toString (array: Uint8Array, encoding: SupportedEncodings = 'utf8'): string { const base = bases[encoding] - if (!base) { + if (base == null) { throw new Error(`Unsupported encoding "${encoding}"`) } diff --git a/src/util/as-uint8array.js b/src/util/as-uint8array.ts similarity index 70% rename from src/util/as-uint8array.js rename to src/util/as-uint8array.ts index 46ce632..c0218ed 100644 --- a/src/util/as-uint8array.js +++ b/src/util/as-uint8array.ts @@ -2,11 +2,8 @@ /** * To guarantee Uint8Array semantics, convert nodejs Buffers * into vanilla Uint8Arrays - * - * @param {Uint8Array} buf - * @returns {Uint8Array} */ -export function asUint8Array (buf) { +export function asUint8Array (buf: Uint8Array): Uint8Array { if (globalThis.Buffer != null) { return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength) } diff --git a/src/util/bases.js b/src/util/bases.ts similarity index 64% rename from src/util/bases.js rename to src/util/bases.ts index 7d2505f..efd897f 100644 --- a/src/util/bases.js +++ b/src/util/bases.ts @@ -1,18 +1,8 @@ +import type { MultibaseCodec } from 'multiformats' import { bases } from 'multiformats/basics' import { allocUnsafe } from '../alloc.js' -/** - * @typedef {import('multiformats/bases/interface').MultibaseCodec} MultibaseCodec - */ - -/** - * @param {string} name - * @param {string} prefix - * @param {(buf: Uint8Array) => string} encode - * @param {(str: string) => Uint8Array} decode - * @returns {MultibaseCodec} - */ -function createCodec (name, prefix, encode, decode) { +function createCodec (name: string, prefix: string, encode: (buf: Uint8Array) => string, decode: (str: string) => Uint8Array): MultibaseCodec { return { name, prefix, @@ -53,14 +43,9 @@ const ascii = createCodec('ascii', 'a', (buf) => { return buf }) -/** - * @typedef {'utf8' | 'utf-8' | 'hex' | 'latin1' | 'ascii' | 'binary' | keyof bases } SupportedEncodings - */ +export type SupportedEncodings = 'utf8' | 'utf-8' | 'hex' | 'latin1' | 'ascii' | 'binary' | keyof typeof bases -/** - * @type {Record} - */ -const BASES = { +const BASES: Record> = { utf8: string, 'utf-8': string, hex: bases.base16, diff --git a/src/xor.js b/src/xor.ts similarity index 82% rename from src/xor.js rename to src/xor.ts index cb9fb50..e53435d 100644 --- a/src/xor.js +++ b/src/xor.ts @@ -3,11 +3,8 @@ import { asUint8Array } from './util/as-uint8array.js' /** * Returns the xor distance between two arrays - * - * @param {Uint8Array} a - * @param {Uint8Array} b */ -export function xor (a, b) { +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/alloc.spec.js b/test/alloc.spec.ts similarity index 95% rename from test/alloc.spec.js rename to test/alloc.spec.ts index 55932f6..719d4bf 100644 --- a/test/alloc.spec.js +++ b/test/alloc.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { alloc, allocUnsafe } from '../src/alloc.js' describe('Uint8Array alloc', () => { diff --git a/test/compare.spec.js b/test/compare.spec.ts similarity index 95% rename from test/compare.spec.js rename to test/compare.spec.ts index 7176fd7..6f31c3c 100644 --- a/test/compare.spec.js +++ b/test/compare.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { compare } from '../src/compare.js' describe('Uint8Array compare', () => { diff --git a/test/concat.spec.js b/test/concat.spec.ts similarity index 96% rename from test/concat.spec.js rename to test/concat.spec.ts index 8675dd4..799827d 100644 --- a/test/concat.spec.js +++ b/test/concat.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { concat } from '../src/concat.js' describe('Uint8Array concat', () => { diff --git a/test/equals.spec.js b/test/equals.spec.ts similarity index 93% rename from test/equals.spec.js rename to test/equals.spec.ts index 87efd87..3098270 100644 --- a/test/equals.spec.js +++ b/test/equals.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { equals } from '../src/equals.js' describe('Uint8Array equals', () => { diff --git a/test/from-string.spec.js b/test/from-string.spec.ts similarity index 87% rename from test/from-string.spec.js rename to test/from-string.spec.ts index 23f8523..2abe53c 100644 --- a/test/from-string.spec.js +++ b/test/from-string.spec.ts @@ -1,13 +1,11 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { fromString } from '../src/from-string.js' import { toString } from '../src/to-string.js' -import bases from '../src/util/bases.js' +import bases, { SupportedEncodings } from '../src/util/bases.js' -/** @type {import('../src/util/bases').SupportedEncodings[]} */ -// @ts-ignore Object.keys returns a string[] -const supportedBases = Object.keys(bases) +const supportedBases = Object.keys(bases) as SupportedEncodings[] describe('Uint8Array fromString', () => { it('creates a Uint8Array from a string', () => { diff --git a/test/to-string.spec.js b/test/to-string.spec.ts similarity index 97% rename from test/to-string.spec.js rename to test/to-string.spec.ts index c610447..9783de2 100644 --- a/test/to-string.spec.js +++ b/test/to-string.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { toString } from '../src/to-string.js' describe('Uint8Array toString', () => { diff --git a/test/xor.spec.js b/test/xor.spec.ts similarity index 95% rename from test/xor.spec.js rename to test/xor.spec.ts index 1f70b3d..5684dc0 100644 --- a/test/xor.spec.js +++ b/test/xor.spec.ts @@ -1,6 +1,6 @@ /* eslint-env mocha */ -import { expect } from 'aegir/utils/chai.js' +import { expect } from 'aegir/chai' import { xor } from '../src/xor.js' describe('Uint8Array xor', () => { diff --git a/tsconfig.json b/tsconfig.json index ecde80b..36ca282 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "./types" + "outDir": "./dist" }, "include": [ "src",