Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
feat(!): convert to typescript (#20)
Browse files Browse the repository at this point in the history
Converts this module to typescript

Track: libp2p/js-libp2p#1021

BREAKING CHANGE: No more default exports, no more CJS
  • Loading branch information
achingbrain authored Jan 5, 2022
1 parent 3730ffb commit bd0080c
Show file tree
Hide file tree
Showing 25 changed files with 372 additions and 320 deletions.
99 changes: 75 additions & 24 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ci
name: test & maybe release
on:
push:
branches:
Expand All @@ -8,67 +8,118 @@ on:
- master

jobs:

check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx aegir lint
- run: npx aegir ts -p check
- run: npx aegir build
- run: npx aegir dep-check
- uses: ipfs/aegir/actions/bundle-size@master
- uses: actions/setup-node@v2
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
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: [14, 15]
node: [16]
fail-fast: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run --if-present test:node
- uses: codecov/codecov-action@v1

test-chrome:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx aegir test -t browser -t webworker --bail
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run --if-present test:chrome

test-chrome-webworker:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run --if-present test:chrome-webworker

test-firefox:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless
test-webkit:
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run --if-present test: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 browser -t webworker --bail -- --browser webkit
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npm run --if-present test:firefox-webworker

test-electron-main:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe aegir test -t electron-main --bail
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npx xvfb-maybe npm run --if-present test:electron-main

test-electron-renderer:
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: npm install
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- run: npx xvfb-maybe npm run --if-present test: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'
steps:
- uses: actions/checkout@v2.4.0
with:
fetch-depth: 0
- uses: actions/setup-node@v2
with:
node-version: lts/*
- uses: ipfs/aegir/actions/cache-node-modules@master
- uses: ipfs/aegir/actions/docker-login@master
with:
docker-token: ${{ secrets.DOCKER_USERNAME }}
docker-username: ${{ secrets.DOCKER_USERNAME }}
- run: npm run --if-present release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
23 changes: 2 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
The MIT License (MIT)

Copyright (c) IPFS

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.
MIT: https://www.opensource.org/licenses/mit
Apache-2.0: https://www.apache.org/licenses/license-2.0
5 changes: 5 additions & 0 deletions LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
> This package serves as a central repository for shared logic and dependencies for all libp2p packages, using `libp2p-utils` helps to easily re-use small scoped blocks of logic across all libp2p modules and also as a dependency proxy (think `aegir` for domain logic dependencies).

The libp2p ecosystem has lots of repos with it comes several problems like:
The libp2p ecosystem has lots of repos with it comes several problems like:
- Domain logic dedupe - all modules shared a lot of logic like validation, streams handling, etc.
- Dependencies management - it's really easy with so many repos for dependencies to go out of control, they become outdated, different repos use different modules to do the same thing (like merging defaults options), browser bundles ends up with multiple versions of the same package, bumping versions is cumbersome to do because we need to go through several repos, etc.

Expand All @@ -26,14 +26,14 @@ These problems are the motivation for this package, having shared logic in this


```bash
$ npm install --save libp2p-utils
$ npm install --save @libp2p/utils
```

## Usage
Each function should be imported directly.

```js
const ipAndPortToMultiaddr = require('libp2p-utils/src/ip-port-to-multiaddr')
import ipAndPortToMultiaddr from '@libp2p/utils/ip-port-to-multiaddr'

const ma = ipAndPortToMultiaddr('127.0.0.1', 9000)
```
Expand Down
67 changes: 45 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
{
"name": "libp2p-utils",
"name": "@libp2p/utils",
"version": "0.4.1",
"description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
"leadMaintainer": "Vasco Santos <santos.vasco10@gmail.com>",
"main": "src/index.js",
"types": "dist/src/index.d.ts",
"type": "module",
"exports": {
"multiaddr/is-loopback": {
"import": "./dist/src/multiaddr/is-loopback.js",
"types": "./dist/src/multiaddr/is-loopback.d.ts"
},
"multiaddr/is-private": {
"import": "./dist/src/multiaddr/is-private.js",
"types": "./dist/src/multiaddr/is-private.d.ts"
},
"address-sort": {
"import": "./dist/src/address-sort.js",
"types": "./dist/src/address-sort.d.ts"
},
"array-equals": {
"import": "./dist/src/array-equals.js",
"types": "./dist/src/array-equals.d.ts"
},
"ip-port-to-multiaddr": {
"import": "./dist/src/ip-port-to-multiaddr.js",
"types": "./dist/src/ip-port-to-multiaddr.d.ts"
},
"stream-to-ma-conn": {
"import": "./dist/src/stream-to-ma-connr.js",
"types": "./dist/src/stream-to-ma-connr.d.ts"
}
},
"typesVersions": {
"*": {
"src/*": [
Expand All @@ -15,49 +40,47 @@
},
"files": [
"src",
"dist"
"dist/src"
],
"scripts": {
"prepare": "aegir build --no-bundle",
"test": "aegir test",
"test:browser": "aegir test -t browser",
"test:node": "aegir test -t node",
"test:electron": "aegir test -t electron-main",
"test:electron-renderer": "aegir test -t electron-renderer",
"lint": "aegir lint",
"release": "aegir release --docs",
"release-minor": "aegir release --target node --type minor --docs",
"release-major": "aegir release --type major --docs",
"build": "aegir build"
"dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js",
"build": "tsc",
"pretest": "npm run build",
"test": "aegir test -f ./dist/test/**/*.js",
"test:chrome": "npm run test -- -t browser",
"test:chrome-webworker": "npm run test -- -t webworker",
"test:firefox": "npm run test -- -t browser -- --browser firefox",
"test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox",
"test:node": "npm run test -- -t node --cov",
"test:electron-main": "npm run test -- -t electron-main",
"release": "semantic-release"
},
"pre-push": [
"lint"
],
"repository": {
"type": "git",
"url": "git+https://github.com/libp2p/js-libp2p-utils.git"
},
"license": "MIT",
"license": "(Apache-2.0 OR MIT)",
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-utils/issues"
},
"homepage": "https://github.com/libp2p/js-libp2p-utils#readme",
"devDependencies": {
"@libp2p/interfaces": "^0.2.0",
"@types/debug": "^4.1.5",
"aegir": "^33.0.0",
"aegir": "^36.1.2",
"it-pair": "^1.0.0",
"it-pipe": "^1.1.0",
"libp2p-interfaces": "^1.0.0",
"streaming-iterables": "^6.0.0",
"util": "^0.12.3"
},
"dependencies": {
"@achingbrain/ip-address": "^8.1.0",
"@multiformats/multiaddr": "^10.1.1",
"abortable-iterator": "^3.0.0",
"debug": "^4.3.0",
"err-code": "^3.0.1",
"ip-address": "^8.0.0",
"is-loopback-addr": "^1.0.0",
"multiaddr": "^10.0.0",
"private-ip": "^2.1.1"
},
"contributors": [
Expand Down
21 changes: 8 additions & 13 deletions src/address-sort.js → src/address-sort.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'
import type { Multiaddr } from '@multiformats/multiaddr'
import { isPrivate } from './multiaddr/is-private.js'

const isPrivate = require('./multiaddr/is-private')

/**
* @typedef {import('multiaddr').Multiaddr} Multiaddr
*/
interface Address {
multiaddr: Multiaddr
isCertified: boolean
}

/**
* @typedef {Object} Address
Expand All @@ -21,7 +21,7 @@ const isPrivate = require('./multiaddr/is-private')
* @param {Address} b
* @returns {number}
*/
function addressesPublicFirstCompareFunction (a, b) {
function addressesPublicFirstCompareFunction (a: Address, b: Address) {
const isAPrivate = isPrivate(a.multiaddr)
const isBPrivate = isPrivate(b.multiaddr)

Expand All @@ -43,12 +43,7 @@ function addressesPublicFirstCompareFunction (a, b) {
/**
* Sort given addresses by putting public addresses first.
* In case of equality, a certified address will come first.
*
* @param {Array<Address>} addresses
* @returns {Array<Address>}
*/
function publicAddressesFirst (addresses) {
export function publicAddressesFirst (addresses: Address[]) {
return [...addresses].sort(addressesPublicFirstCompareFunction)
}

module.exports.publicAddressesFirst = publicAddressesFirst
15 changes: 0 additions & 15 deletions src/array-equals.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/array-equals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Verify if two arrays of non primitive types with the "equals" function are equal.
* Compatible with multiaddr, peer-id and others.
*/
export function arrayEquals (a: any[], b: any[]) {
const sort = (a: any, b: any) => a.toString().localeCompare(b.toString())
return a.length === b.length && b.sort(sort) && a.sort(sort).every((item, index) => b[index].equals(item))
}
Empty file removed src/index.js
Empty file.
Loading

0 comments on commit bd0080c

Please sign in to comment.