This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc5a3d5
commit 426b421
Showing
8 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package-lock.json | ||
yarn.lock | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git | ||
node_modules | ||
|
||
dist | ||
docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
language: node_js | ||
cache: npm | ||
stages: | ||
- check | ||
- test | ||
- cov | ||
|
||
node_js: | ||
- '10' | ||
- '12' | ||
|
||
os: | ||
- linux | ||
- osx | ||
|
||
script: npx nyc -s npm run test:node -- --bail | ||
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov | ||
|
||
jobs: | ||
include: | ||
- os: windows | ||
filter_secrets: false | ||
cache: false | ||
|
||
- stage: check | ||
script: | ||
- npx aegir commitlint --travis | ||
- npx aegir dep-check | ||
- npm run lint | ||
|
||
- stage: test | ||
name: chrome | ||
addons: | ||
chrome: stable | ||
script: npx aegir test -t browser -t webworker | ||
|
||
- stage: test | ||
name: firefox | ||
addons: | ||
firefox: latest | ||
script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless | ||
|
||
- stage: test | ||
name: electron | ||
services: | ||
- xvfb | ||
script: | ||
- npm run test:electron | ||
- npm run test:electron-renderer | ||
|
||
notifications: | ||
email: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,49 @@ | ||
# js-libp2p-utils | ||
|
||
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai) | ||
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/) | ||
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p) | ||
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io) | ||
[![](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-utils.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-utils) | ||
[![](https://img.shields.io/travis/libp2p/js-libp2p-utils.svg?style=flat-square)](https://travis-ci.com/libp2p/js-libp2p-utils) | ||
[![Dependency Status](https://david-dm.org/libp2p/js-libp2p-utils.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-utils) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) | ||
|
||
> 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: | ||
- 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. | ||
|
||
These problems are the motivation for this package, having shared logic in this package avoids creating cyclic dependencies, centralizes common use modules/functions (exactly like aegir does for the tooling), semantic versioning for 3rd party dependencies is handled in one single place (a good example is going from streams 2 to 3) and maintainers should only care about having `libp2p-utils` updated. | ||
|
||
## Lead Maintainer | ||
|
||
[Vasco Santos](https://github.com/vasco-santos) | ||
|
||
## Install | ||
|
||
|
||
```bash | ||
$ npm install --save libp2p-utils | ||
``` | ||
|
||
## Usage | ||
Each function should be imported directly. | ||
|
||
```js | ||
const ipAndPortToMultiaddr = require('libp2p-utils/src/ip-port-to-multiaddr') | ||
|
||
const ma = ipAndPortToMultiaddr('127.0.0.1', 9000) | ||
``` | ||
|
||
## Contribute | ||
|
||
Contributions welcome. Please check out [the issues](https://github.com/libp2p/js-libp2p-utils/issues). | ||
|
||
Check out our [contributing document](https://github.com/ipfs/community/blob/master/contributing.md) for more information on how we work, and about contributing in general. Please be aware that all interactions related to this repo are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md). | ||
|
||
## License | ||
|
||
[MIT](LICENSE) © Protocol Labs Inc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "libp2p-utils", | ||
"version": "0.0.0", | ||
"description": "Package to aggregate shared logic and dependencies for the libp2p ecosystem", | ||
"leadMaintainer": "Vasco Santos <[email protected]>", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"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 --type minor --docs", | ||
"release-major": "aegir release --type major --docs", | ||
"build": "aegir build" | ||
}, | ||
"pre-push": [ | ||
"lint" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/libp2p/js-libp2p-utils.git" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/libp2p/js-libp2p-utils/issues" | ||
}, | ||
"homepage": "https://github.com/libp2p/js-libp2p-utils#readme", | ||
"devDependencies": { | ||
"aegir": "^20.3.1", | ||
"chai": "^4.2.0", | ||
"dirty-chai": "^2.0.1" | ||
}, | ||
"dependencies": { | ||
"ip-address": "^6.1.0", | ||
"multiaddr": "^7.1.0" | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict' | ||
|
||
const multiaddr = require('multiaddr') | ||
const { Address4, Address6 } = require('ip-address') | ||
|
||
module.exports = (ip, port) => { | ||
if (typeof ip !== 'string') { | ||
throw new Error('invalid ip') | ||
} | ||
|
||
port = parseInt(port) | ||
|
||
if (isNaN(port)) { | ||
throw new Error('invalid port') | ||
} | ||
|
||
if (new Address4(ip).isValid()) { | ||
return multiaddr(`/ip4/${ip}/tcp/${port}`) | ||
} | ||
|
||
const ip6 = new Address6(ip) | ||
|
||
if (ip6.isValid()) { | ||
return ip6.is4() | ||
? multiaddr(`/ip4/${ip6.to4().correctForm()}/tcp/${port}`) | ||
: multiaddr(`/ip6/${ip}/tcp/${port}`) | ||
} | ||
|
||
throw new Error('invalid ip') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
const dirtyChai = require('dirty-chai') | ||
const expect = chai.expect | ||
chai.use(dirtyChai) | ||
const toMultiaddr = require('../src/ip-port-to-multiaddr') | ||
|
||
describe('IP and port to Multiaddr', () => { | ||
it('creates multiaddr from valid IPv4 IP and port', () => { | ||
const ip = '127.0.0.1' | ||
const port = '9090' | ||
expect(toMultiaddr(ip, port).toString()).to.equal(`/ip4/${ip}/tcp/${port}`) | ||
}) | ||
|
||
it('creates multiaddr from valid IPv4 IP and numeric port', () => { | ||
const ip = '127.0.0.1' | ||
const port = 9090 | ||
expect(toMultiaddr(ip, port).toString()).to.equal(`/ip4/${ip}/tcp/${port}`) | ||
}) | ||
|
||
it('creates multiaddr from valid IPv4 in IPv6 IP and port', () => { | ||
const ip = '0:0:0:0:0:0:101.45.75.219' | ||
const port = '9090' | ||
expect(toMultiaddr(ip, port).toString()).to.equal(`/ip4/101.45.75.219/tcp/${port}`) | ||
}) | ||
|
||
it('creates multiaddr from valid IPv6 IP and port', () => { | ||
const ip = '::1' | ||
const port = '9090' | ||
expect(toMultiaddr(ip, port).toString()).to.equal(`/ip6/${ip}/tcp/${port}`) | ||
}) | ||
|
||
it('throws for missing IP address', () => { | ||
expect(() => toMultiaddr()).to.throw('invalid ip') | ||
}) | ||
|
||
it('throws for invalid IP address', () => { | ||
const ip = 'aewmrn4awoew' | ||
const port = '234' | ||
expect(() => toMultiaddr(ip, port)).to.throw('invalid ip') | ||
}) | ||
|
||
it('throws for invalid port', () => { | ||
const ip = '127.0.0.1' | ||
const port = 'garbage' | ||
expect(() => toMultiaddr(ip, port)).to.throw('invalid port') | ||
}) | ||
}) |