-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from ipfs/feat/multiaddr
This PR introduces utility methods for multiaddr detection: ### `isIPFS.multiaddr(input)` Generic `multiaddr` checks if input is a valid multiaddr. (It does not care about its type, just validity) ### `isIPFS.peerMultiaddr(input)` `peerMultiaddr` provides easy validation of IPFS Peer addresses (direct or encapsulated). It is a thin wrapper on top of generic `isIPFS.multiaddr(input)` check coupled with `mafmt.IPFS.matches(input)` from [mafmt](https://github.com/multiformats/js-mafmt) (low-level multiaddr validation library) Details behind `mafmt.IPFS` can be found at https://github.com/multiformats/js-mafmt#api **Note:** I've added only this check because `is-ipfs` is IPFS-centric utility. If one needs more nuanced multiaddr validation, then [mafmt](https://github.com/multiformats/js-mafmt) should be used directly. ---- Closes #26
- Loading branch information
Showing
7 changed files
with
237 additions
and
23 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 |
---|---|---|
@@ -1,23 +1,42 @@ | ||
|
||
language: node_js | ||
cache: npm | ||
stages: | ||
- check | ||
- test | ||
- cov | ||
|
||
node_js: | ||
- node | ||
- '10' | ||
|
||
os: | ||
- linux | ||
- osx | ||
|
||
script: npx nyc -s npm run test:node -- --bail | ||
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov | ||
|
||
# Make sure we have new NPM. | ||
before_install: | ||
- npm install -g npm | ||
jobs: | ||
include: | ||
- os: windows | ||
cache: false | ||
|
||
script: | ||
- npm run lint | ||
- npm test | ||
- npm run coverage | ||
- stage: check | ||
script: | ||
- npx aegir commitlint --travis | ||
- npx aegir dep-check | ||
- npm run lint | ||
|
||
addons: | ||
firefox: 'latest' | ||
- stage: test | ||
name: chrome | ||
addons: | ||
chrome: stable | ||
script: npx aegir test -t browser -t webworker | ||
|
||
before_script: | ||
- export DISPLAY=:99.0 | ||
- sh -e /etc/init.d/xvfb start | ||
- stage: test | ||
name: firefox | ||
addons: | ||
firefox: latest | ||
script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless | ||
|
||
after_success: | ||
- npm run coverage-publish | ||
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
This file was deleted.
Oops, something went wrong.
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
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
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
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,143 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const expect = require('chai').expect | ||
const Multiaddr = require('multiaddr') | ||
const isIPFS = require('../src/index') | ||
|
||
describe('ipfs multiaddr', () => { | ||
it('isIPFS.multiaddr should match a string with valid ip4 multiaddr', (done) => { | ||
const actual = isIPFS.multiaddr('/ip4/127.0.0.1/udp/1234/http') | ||
expect(actual).to.equal(true) | ||
done() | ||
}) | ||
|
||
it('isIPFS.multiaddr should match a string with valid ip6 multiaddr', (done) => { | ||
const actual = isIPFS.multiaddr('/ip6/::1/udp/1234/http') | ||
expect(actual).to.equal(true) | ||
done() | ||
}) | ||
|
||
it('isIPFS.multiaddr should match a valid Multiaddr instance', (done) => { | ||
const ma = new Multiaddr('/ip6/::1/udp/1234/http') | ||
const actual = isIPFS.multiaddr(ma) | ||
expect(actual).to.equal(true) | ||
done() | ||
}) | ||
|
||
it('isIPFS.multiaddr should match a Buffer with multiaddr', (done) => { | ||
const ma = new Multiaddr('/ip6/::1/udp/1234/http') | ||
const actual = isIPFS.multiaddr(Buffer.from(ma.buffer)) | ||
expect(actual).to.equal(true) | ||
done() | ||
}) | ||
|
||
it('isIPFS.multiaddr should not match random Buffer', (done) => { | ||
const actual = isIPFS.multiaddr(Buffer.from('randombuffer')) | ||
expect(actual).to.equal(false) | ||
done() | ||
}) | ||
|
||
it('isIPFS.multiaddr should not match an invalid multiaddr (no initial slash)', (done) => { | ||
const actual = isIPFS.multiaddr('ip4/127.0.0.1/udp/1234/http') | ||
expect(actual).to.equal(false) | ||
done() | ||
}) | ||
|
||
it('isIPFS.multiaddr should not match an invalid multiaddr (unknown namespace)', (done) => { | ||
const actual = isIPFS.multiaddr('/yoloinvalid/127.0.0.1/udp/1234/http') | ||
expect(actual).to.equal(false) | ||
done() | ||
}) | ||
|
||
it('isIPFS.multiaddr should not match an invalid multiaddr', (done) => { | ||
const actual = isIPFS.multiaddr('noop') | ||
expect(actual).to.equal(false) | ||
done() | ||
}) | ||
|
||
it('isIPFS.multiaddr should not match an invalid multiaddr data type', (done) => { | ||
const actual = isIPFS.multiaddr(4) | ||
expect(actual).to.equal(false) | ||
done() | ||
}) | ||
}) | ||
|
||
describe('ipfs peerMultiaddr', () => { | ||
// https://github.com/multiformats/js-mafmt/blob/v6.0.6/test/index.spec.js#L137 | ||
const goodCircuit = [ | ||
'/p2p-circuit', | ||
'/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj', | ||
'/p2p-circuit/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj', | ||
'/p2p-circuit/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4', | ||
'/p2p-circuit/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4', | ||
'/p2p-circuit/ip4/127.0.0.1/tcp/4002/ipfs/QmddWMcQX6orJGHpETYMyPgXrCXCtYANMFVDCvhKoDwLqA', | ||
'/p2p-circuit/ipfs/QmddWMcQX6orJGHpETYMyPgXrCXCtYANMFVDCvhKoDwLqA', | ||
'/p2p-circuit/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj/' + | ||
'p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj' | ||
] | ||
// https://github.com/multiformats/js-mafmt/blob/v6.0.6/test/index.spec.js#L157 | ||
const validPeerMultiaddrs = [ | ||
'/ip4/127.0.0.1/tcp/20008/ws/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj', | ||
'/ip4/1.2.3.4/tcp/3456/ws/p2p-webrtc-star/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4', | ||
'/ip4/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4', | ||
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit', | ||
'/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4/p2p-circuit/ipfs/QmUjNmr8TgJCn1Ao7DvMy4cjoZU15b9bwSCBLE3vwXiwgj' | ||
].concat(goodCircuit) | ||
|
||
it('isIPFS.peerMultiaddr should match a string with a valid IPFS peer', (done) => { | ||
for (let addr of validPeerMultiaddrs) { | ||
const actual = isIPFS.peerMultiaddr(addr) | ||
expect(actual, `isIPFS.peerMultiaddr(${addr})`).to.equal(true) | ||
} | ||
done() | ||
}) | ||
|
||
it('isIPFS.peerMultiaddr should match a valid Multiaddr instance', (done) => { | ||
for (let addr of validPeerMultiaddrs) { | ||
const ma = new Multiaddr(addr) | ||
const actual = isIPFS.peerMultiaddr(ma) | ||
expect(actual, `isIPFS.peerMultiaddr(${addr})`).to.equal(true) | ||
} | ||
done() | ||
}) | ||
|
||
it('isIPFS.peerMultiaddr should match a Buffer with multiaddr', (done) => { | ||
for (let addr of validPeerMultiaddrs) { | ||
const ma = new Multiaddr(addr) | ||
const actual = isIPFS.peerMultiaddr((Buffer.from(ma.buffer))) | ||
expect(actual, `isIPFS.peerMultiaddr(${addr})`).to.equal(true) | ||
} | ||
done() | ||
}) | ||
|
||
it('isIPFS.peerMultiaddr should not match random Buffer', (done) => { | ||
const actual = isIPFS.peerMultiaddr(Buffer.from('randombuffer')) | ||
expect(actual).to.equal(false) | ||
done() | ||
}) | ||
|
||
it('isIPFS.peerMultiaddr should not match an invalid multiaddr (no initial slash)', (done) => { | ||
const actual = isIPFS.peerMultiaddr('ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4') | ||
expect(actual).to.equal(false) | ||
done() | ||
}) | ||
|
||
it('isIPFS.peerMultiaddr should not match an invalid multiaddr (unknown namespace)', (done) => { | ||
const actual = isIPFS.peerMultiaddr('/yoloinvalid/1.2.3.4/tcp/3456/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooo4') | ||
expect(actual).to.equal(false) | ||
done() | ||
}) | ||
|
||
it('isIPFS.peerMultiaddr should not match an invalid multiaddr', (done) => { | ||
const actual = isIPFS.peerMultiaddr('noop') | ||
expect(actual).to.equal(false) | ||
done() | ||
}) | ||
|
||
it('isIPFS.peerMultiaddr should not match an invalid multiaddr data type', (done) => { | ||
const actual = isIPFS.peerMultiaddr(4) | ||
expect(actual).to.equal(false) | ||
done() | ||
}) | ||
}) |