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

Commit bd0080c

Browse files
authored
feat(!): convert to typescript (#20)
Converts this module to typescript Track: libp2p/js-libp2p#1021 BREAKING CHANGE: No more default exports, no more CJS
1 parent 3730ffb commit bd0080c

25 files changed

+372
-320
lines changed

.github/workflows/main.yml

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: ci
1+
name: test & maybe release
22
on:
33
push:
44
branches:
@@ -8,67 +8,118 @@ on:
88
- master
99

1010
jobs:
11+
1112
check:
1213
runs-on: ubuntu-latest
1314
steps:
1415
- uses: actions/checkout@v2
15-
- run: npm install
16-
- run: npx aegir lint
17-
- run: npx aegir ts -p check
18-
- run: npx aegir build
19-
- run: npx aegir dep-check
20-
- uses: ipfs/aegir/actions/bundle-size@master
16+
- uses: actions/setup-node@v2
2117
with:
22-
github_token: ${{ secrets.GITHUB_TOKEN }}
18+
node-version: lts/*
19+
- uses: ipfs/aegir/actions/cache-node-modules@master
20+
- run: npm run --if-present lint
21+
- run: npm run --if-present dep-check
22+
2323
test-node:
2424
needs: check
2525
runs-on: ${{ matrix.os }}
2626
strategy:
2727
matrix:
2828
os: [windows-latest, ubuntu-latest, macos-latest]
29-
node: [14, 15]
29+
node: [16]
3030
fail-fast: true
3131
steps:
3232
- uses: actions/checkout@v2
33-
- uses: actions/setup-node@v1
33+
- uses: actions/setup-node@v2
3434
with:
3535
node-version: ${{ matrix.node }}
36-
- run: npm install
37-
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
36+
- uses: ipfs/aegir/actions/cache-node-modules@master
37+
- run: npm run --if-present test:node
3838
- uses: codecov/codecov-action@v1
39+
3940
test-chrome:
4041
needs: check
4142
runs-on: ubuntu-latest
4243
steps:
4344
- uses: actions/checkout@v2
44-
- run: npm install
45-
- run: npx aegir test -t browser -t webworker --bail
45+
- uses: actions/setup-node@v2
46+
with:
47+
node-version: lts/*
48+
- uses: ipfs/aegir/actions/cache-node-modules@master
49+
- run: npm run --if-present test:chrome
50+
51+
test-chrome-webworker:
52+
needs: check
53+
runs-on: ubuntu-latest
54+
steps:
55+
- uses: actions/checkout@v2
56+
- uses: actions/setup-node@v2
57+
with:
58+
node-version: lts/*
59+
- uses: ipfs/aegir/actions/cache-node-modules@master
60+
- run: npm run --if-present test:chrome-webworker
61+
4662
test-firefox:
4763
needs: check
4864
runs-on: ubuntu-latest
4965
steps:
5066
- uses: actions/checkout@v2
51-
- run: npm install
52-
- run: npx aegir test -t browser -t webworker --bail -- --browsers FirefoxHeadless
53-
test-webkit:
67+
- uses: actions/setup-node@v2
68+
with:
69+
node-version: lts/*
70+
- uses: ipfs/aegir/actions/cache-node-modules@master
71+
- run: npm run --if-present test:firefox
72+
73+
test-firefox-webworker:
5474
needs: check
5575
runs-on: ubuntu-latest
5676
steps:
5777
- uses: actions/checkout@v2
58-
- uses: microsoft/playwright-github-action@v1
59-
- run: npm install
60-
- run: npx aegir test -t browser -t webworker --bail -- --browser webkit
78+
- uses: actions/setup-node@v2
79+
with:
80+
node-version: lts/*
81+
- uses: ipfs/aegir/actions/cache-node-modules@master
82+
- run: npm run --if-present test:firefox-webworker
83+
6184
test-electron-main:
6285
needs: check
6386
runs-on: ubuntu-latest
6487
steps:
6588
- uses: actions/checkout@v2
66-
- run: npm install
67-
- run: npx xvfb-maybe aegir test -t electron-main --bail
89+
- uses: actions/setup-node@v2
90+
with:
91+
node-version: lts/*
92+
- uses: ipfs/aegir/actions/cache-node-modules@master
93+
- run: npx xvfb-maybe npm run --if-present test:electron-main
94+
6895
test-electron-renderer:
6996
needs: check
7097
runs-on: ubuntu-latest
7198
steps:
7299
- uses: actions/checkout@v2
73-
- run: npm install
74-
- run: npx xvfb-maybe aegir test -t electron-renderer --bail
100+
- uses: actions/setup-node@v2
101+
with:
102+
node-version: lts/*
103+
- uses: ipfs/aegir/actions/cache-node-modules@master
104+
- run: npx xvfb-maybe npm run --if-present test:electron-renderer
105+
106+
release:
107+
needs: [test-node, test-chrome, test-chrome-webworker, test-firefox, test-firefox-webworker, test-electron-main, test-electron-renderer]
108+
runs-on: ubuntu-latest
109+
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
110+
steps:
111+
- uses: actions/[email protected]
112+
with:
113+
fetch-depth: 0
114+
- uses: actions/setup-node@v2
115+
with:
116+
node-version: lts/*
117+
- uses: ipfs/aegir/actions/cache-node-modules@master
118+
- uses: ipfs/aegir/actions/docker-login@master
119+
with:
120+
docker-token: ${{ secrets.DOCKER_USERNAME }}
121+
docker-username: ${{ secrets.DOCKER_USERNAME }}
122+
- run: npm run --if-present release
123+
env:
124+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
125+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

LICENSE

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,2 @@
1-
The MIT License (MIT)
2-
3-
Copyright (c) IPFS
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
1+
MIT: https://www.opensource.org/licenses/mit
2+
Apache-2.0: https://www.apache.org/licenses/license-2.0

LICENSE-APACHE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
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
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
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.

LICENSE-MIT

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
> 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).
1313
1414

15-
The libp2p ecosystem has lots of repos with it comes several problems like:
15+
The libp2p ecosystem has lots of repos with it comes several problems like:
1616
- Domain logic dedupe - all modules shared a lot of logic like validation, streams handling, etc.
1717
- 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.
1818

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

2727

2828
```bash
29-
$ npm install --save libp2p-utils
29+
$ npm install --save @libp2p/utils
3030
```
3131

3232
## Usage
3333
Each function should be imported directly.
3434

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

3838
const ma = ipAndPortToMultiaddr('127.0.0.1', 9000)
3939
```

package.json

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
{
2-
"name": "libp2p-utils",
2+
"name": "@libp2p/utils",
33
"version": "0.4.1",
44
"description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem",
55
"leadMaintainer": "Vasco Santos <[email protected]>",
6-
"main": "src/index.js",
7-
"types": "dist/src/index.d.ts",
6+
"type": "module",
7+
"exports": {
8+
"multiaddr/is-loopback": {
9+
"import": "./dist/src/multiaddr/is-loopback.js",
10+
"types": "./dist/src/multiaddr/is-loopback.d.ts"
11+
},
12+
"multiaddr/is-private": {
13+
"import": "./dist/src/multiaddr/is-private.js",
14+
"types": "./dist/src/multiaddr/is-private.d.ts"
15+
},
16+
"address-sort": {
17+
"import": "./dist/src/address-sort.js",
18+
"types": "./dist/src/address-sort.d.ts"
19+
},
20+
"array-equals": {
21+
"import": "./dist/src/array-equals.js",
22+
"types": "./dist/src/array-equals.d.ts"
23+
},
24+
"ip-port-to-multiaddr": {
25+
"import": "./dist/src/ip-port-to-multiaddr.js",
26+
"types": "./dist/src/ip-port-to-multiaddr.d.ts"
27+
},
28+
"stream-to-ma-conn": {
29+
"import": "./dist/src/stream-to-ma-connr.js",
30+
"types": "./dist/src/stream-to-ma-connr.d.ts"
31+
}
32+
},
833
"typesVersions": {
934
"*": {
1035
"src/*": [
@@ -15,49 +40,47 @@
1540
},
1641
"files": [
1742
"src",
18-
"dist"
43+
"dist/src"
1944
],
2045
"scripts": {
21-
"prepare": "aegir build --no-bundle",
22-
"test": "aegir test",
23-
"test:browser": "aegir test -t browser",
24-
"test:node": "aegir test -t node",
25-
"test:electron": "aegir test -t electron-main",
26-
"test:electron-renderer": "aegir test -t electron-renderer",
2746
"lint": "aegir lint",
28-
"release": "aegir release --docs",
29-
"release-minor": "aegir release --target node --type minor --docs",
30-
"release-major": "aegir release --type major --docs",
31-
"build": "aegir build"
47+
"dep-check": "aegir dep-check dist/src/**/*.js dist/test/**/*.js",
48+
"build": "tsc",
49+
"pretest": "npm run build",
50+
"test": "aegir test -f ./dist/test/**/*.js",
51+
"test:chrome": "npm run test -- -t browser",
52+
"test:chrome-webworker": "npm run test -- -t webworker",
53+
"test:firefox": "npm run test -- -t browser -- --browser firefox",
54+
"test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox",
55+
"test:node": "npm run test -- -t node --cov",
56+
"test:electron-main": "npm run test -- -t electron-main",
57+
"release": "semantic-release"
3258
},
33-
"pre-push": [
34-
"lint"
35-
],
3659
"repository": {
3760
"type": "git",
3861
"url": "git+https://github.com/libp2p/js-libp2p-utils.git"
3962
},
40-
"license": "MIT",
63+
"license": "(Apache-2.0 OR MIT)",
4164
"bugs": {
4265
"url": "https://github.com/libp2p/js-libp2p-utils/issues"
4366
},
4467
"homepage": "https://github.com/libp2p/js-libp2p-utils#readme",
4568
"devDependencies": {
69+
"@libp2p/interfaces": "^0.2.0",
4670
"@types/debug": "^4.1.5",
47-
"aegir": "^33.0.0",
71+
"aegir": "^36.1.2",
4872
"it-pair": "^1.0.0",
4973
"it-pipe": "^1.1.0",
50-
"libp2p-interfaces": "^1.0.0",
5174
"streaming-iterables": "^6.0.0",
5275
"util": "^0.12.3"
5376
},
5477
"dependencies": {
78+
"@achingbrain/ip-address": "^8.1.0",
79+
"@multiformats/multiaddr": "^10.1.1",
5580
"abortable-iterator": "^3.0.0",
5681
"debug": "^4.3.0",
5782
"err-code": "^3.0.1",
58-
"ip-address": "^8.0.0",
5983
"is-loopback-addr": "^1.0.0",
60-
"multiaddr": "^10.0.0",
6184
"private-ip": "^2.1.1"
6285
},
6386
"contributors": [

src/address-sort.js renamed to src/address-sort.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
'use strict'
1+
import type { Multiaddr } from '@multiformats/multiaddr'
2+
import { isPrivate } from './multiaddr/is-private.js'
23

3-
const isPrivate = require('./multiaddr/is-private')
4-
5-
/**
6-
* @typedef {import('multiaddr').Multiaddr} Multiaddr
7-
*/
4+
interface Address {
5+
multiaddr: Multiaddr
6+
isCertified: boolean
7+
}
88

99
/**
1010
* @typedef {Object} Address
@@ -21,7 +21,7 @@ const isPrivate = require('./multiaddr/is-private')
2121
* @param {Address} b
2222
* @returns {number}
2323
*/
24-
function addressesPublicFirstCompareFunction (a, b) {
24+
function addressesPublicFirstCompareFunction (a: Address, b: Address) {
2525
const isAPrivate = isPrivate(a.multiaddr)
2626
const isBPrivate = isPrivate(b.multiaddr)
2727

@@ -43,12 +43,7 @@ function addressesPublicFirstCompareFunction (a, b) {
4343
/**
4444
* Sort given addresses by putting public addresses first.
4545
* In case of equality, a certified address will come first.
46-
*
47-
* @param {Array<Address>} addresses
48-
* @returns {Array<Address>}
4946
*/
50-
function publicAddressesFirst (addresses) {
47+
export function publicAddressesFirst (addresses: Address[]) {
5148
return [...addresses].sort(addressesPublicFirstCompareFunction)
5249
}
53-
54-
module.exports.publicAddressesFirst = publicAddressesFirst

src/array-equals.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/array-equals.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Verify if two arrays of non primitive types with the "equals" function are equal.
3+
* Compatible with multiaddr, peer-id and others.
4+
*/
5+
export function arrayEquals (a: any[], b: any[]) {
6+
const sort = (a: any, b: any) => a.toString().localeCompare(b.toString())
7+
return a.length === b.length && b.sort(sort) && a.sort(sort).every((item, index) => b[index].equals(item))
8+
}

src/index.js

Whitespace-only changes.

0 commit comments

Comments
 (0)