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

feat: add types #16

Merged
merged 7 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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
- run: npx aegir ts -p check
- 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: [14, 15]
fail-fast: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}
- run: npm install
- run: npx nyc --reporter=lcov aegir test -t node -- --bail
- 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
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:
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
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
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
52 changes: 0 additions & 52 deletions .travis.yml

This file was deleted.

29 changes: 23 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@
"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",
"typesVersions": {
"*": {
"src/*": [
"dist/src/*",
"dist/src/*/index"
]
}
},
"files": [
"src",
"dist"
],
"scripts": {
"prepare": "aegir build --no-bundle",
"test": "aegir test",
"test:browser": "aegir test -t browser",
"test:node": "aegir test -t node",
Expand All @@ -29,18 +43,21 @@
},
"homepage": "https://github.com/libp2p/js-libp2p-utils#readme",
"devDependencies": {
"aegir": "^27.0.0",
"@types/debug": "^4.1.5",
"aegir": "^32.1.0",
"it-pair": "^1.0.0",
"it-pipe": "^1.1.0",
"streaming-iterables": "^5.0.3"
"libp2p-interfaces": "^0.8.4",
"streaming-iterables": "^5.0.3",
"util": "^0.12.3"
},
"dependencies": {
"abortable-iterator": "^3.0.0",
"debug": "^4.2.0",
"err-code": "^2.0.3",
"ip-address": "^6.1.0",
"debug": "^4.3.0",
"err-code": "^3.0.1",
"ip-address": "^7.1.0",
"is-loopback-addr": "^1.0.0",
"multiaddr": "^8.0.0",
"multiaddr": "^8.1.2",
"private-ip": "^2.1.1"
},
"contributors": [
Expand Down
10 changes: 10 additions & 0 deletions src/address-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

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

/**
* @typedef {import('multiaddr')} Multiaddr
*/

/**
* @typedef {Object} Address
* @property {Multiaddr} multiaddr peer multiaddr.
* @property {boolean} isCertified obtained from a signed peer record.
*/

/**
* Compare function for array.sort().
* This sort aims to move the private adresses to the end of the array.
Expand Down
17 changes: 10 additions & 7 deletions src/ip-port-to-multiaddr.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,28 @@ function ipPortToMultiaddr (ip, port) {
throw errCode(new Error(`invalid ip provided: ${ip}`), errors.ERR_INVALID_IP_PARAMETER)
}

// @ts-ignore parseInt expects only string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to just wrap with an if clause

port = parseInt(port)

if (isNaN(port)) {
throw errCode(new Error(`invalid port provided: ${port}`), errors.ERR_INVALID_PORT_PARAMETER)
}

if (new Address4(ip).isValid()) {
try {
// Test valid IPv4
new Address4(ip) // eslint-disable-line no-new
return multiaddr(`/ip4/${ip}/tcp/${port}`)
}

const ip6 = new Address6(ip)
} catch (_) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're ignoring the error you can use optional catch binding here:

Suggested change
} catch (_) {}
} catch {}


if (ip6.isValid()) {
try {
// Test valid IPv6
const ip6 = new Address6(ip)
return ip6.is4()
? multiaddr(`/ip4/${ip6.to4().correctForm()}/tcp/${port}`)
: multiaddr(`/ip6/${ip}/tcp/${port}`)
} catch (err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this error get logged somewhere?

throw errCode(new Error(`invalid ip:port for creating a multiaddr: ${ip}:${port}`), errors.ERR_INVALID_IP)
}

throw errCode(new Error(`invalid ip:port for creating a multiaddr: ${ip}:${port}`), errors.ERR_INVALID_IP)
}

module.exports = ipPortToMultiaddr
Expand Down
5 changes: 5 additions & 0 deletions src/multiaddr/is-loopback.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict'

// @ts-ignore is-loopback-addr does not publish types
const isLoopbackAddr = require('is-loopback-addr')

/**
* @typedef {import('multiaddr')} Multiaddr
*/

/**
* Check if a given multiaddr is a loopback address.
*
Expand Down
5 changes: 5 additions & 0 deletions src/multiaddr/is-private.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict'

// @ts-ignore private-ip does not publish types
const isIpPrivate = require('private-ip')

/**
* @typedef {import('multiaddr')} Multiaddr
*/

/**
* Check if a given multiaddr has a private address.
*
Expand Down
31 changes: 24 additions & 7 deletions src/stream-to-ma-conn.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
'use strict'

const abortable = require('abortable-iterator')
const log = require('debug')('libp2p:stream:converter')
const debug = require('debug')
const log = debug('libp2p:stream:converter')

/**
* @typedef {import('multiaddr')} Multiaddr
* @typedef {import('libp2p-interfaces/src/transport/types').MultiaddrConnection} MultiaddrConnection
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
*
* @typedef {Object} Timeline
* @property {number} open - connection opening timestamp.
* @property {number} [upgraded] - connection upgraded timestamp.
* @property {number} [close]
*/

/**
* Convert a duplex iterable into a MultiaddrConnection.
* https://github.com/libp2p/interface-transport#multiaddrconnection
*
* @param {object} streamProperties
* @param {DuplexStream} streamProperties.stream
* @param {MuxedStream} streamProperties.stream
* @param {Multiaddr} streamProperties.remoteAddr
* @param {Multiaddr} streamProperties.localAddr
* @param {object} [options]
Expand All @@ -17,8 +29,12 @@ const log = require('debug')('libp2p:stream:converter')
function streamToMaConnection ({ stream, remoteAddr, localAddr }, options = {}) {
const { sink, source } = stream
const maConn = {
/**
* @param {Uint8Array} source
*/
async sink (source) {
if (options.signal) {
// @ts-ignore abortable has no type definitions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I retried it deconstructing the require and it has types indeed. However, types infer that the Uint8Array will be a source with numbers for reasons 🤷🏼 I updated the ignore message to not block on this

source = abortable(source, options.signal)
}

Expand All @@ -35,23 +51,24 @@ function streamToMaConnection ({ stream, remoteAddr, localAddr }, options = {})
}
close()
},

// @ts-ignore abortable has no type definitions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why, I will open an issue in abortable-iterator. It is not exporting correctly the types in the default for some reason. I get abortable is not callable
However, if we do const { source: abortable } = require('abortable-iterator') it will infer the types correctly

source: options.signal ? abortable(source, options.signal) : source,
conn: stream,
localAddr,
remoteAddr,
timeline: { open: Date.now() },

/** @type {Timeline} */
timeline: { open: Date.now(), close: undefined },
close () {
sink([])
close()
sink(new Uint8Array(0))
return close()
}
}

function close () {
if (!maConn.timeline.close) {
maConn.timeline.close = Date.now()
}
return Promise.resolve()
}

return maConn
Expand Down
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./node_modules/aegir/src/config/tsconfig.aegir.json",
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved
"compilerOptions": {
"outDir": "dist"
},
"include": [
"src"
]
}