├── test ├── browser.js ├── node.js └── test-ping.js ├── src ├── constants.js ├── index.js ├── util.js ├── handler.js └── ping.js ├── ci └── Jenkinsfile ├── .travis.yml ├── .gitignore ├── LICENSE ├── README.md ├── package.json └── CHANGELOG.md /test/browser.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | -------------------------------------------------------------------------------- /test/node.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | require('./test-ping.js') 4 | -------------------------------------------------------------------------------- /src/constants.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = { 4 | PROTOCOL: '/ipfs/ping/1.0.0', 5 | PING_LENGTH: 32 6 | } 7 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const handler = require('./handler') 4 | 5 | exports = module.exports = require('./ping') 6 | exports.mount = handler.mount 7 | exports.unmount = handler.unmount 8 | -------------------------------------------------------------------------------- /ci/Jenkinsfile: -------------------------------------------------------------------------------- 1 | // Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories. 2 | javascript() 3 | -------------------------------------------------------------------------------- /src/util.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const crypto = require('libp2p-crypto') 4 | const constants = require('./constants') 5 | 6 | exports = module.exports 7 | 8 | exports.rnd = (length) => { 9 | if (!length) { 10 | length = constants.PING_LENGTH 11 | } 12 | return crypto.randomBytes(length) 13 | } 14 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | cache: npm 3 | stages: 4 | - check 5 | - test 6 | - cov 7 | 8 | node_js: 9 | - '10' 10 | 11 | os: 12 | - linux 13 | - osx 14 | 15 | script: npx nyc -s npm run test:node -- --bail 16 | after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov 17 | 18 | jobs: 19 | include: 20 | - os: windows 21 | cache: false 22 | 23 | - stage: check 24 | script: 25 | - npx aegir commitlint --travis 26 | - npx aegir dep-check 27 | - npm run lint 28 | 29 | notifications: 30 | email: false 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | docs 2 | **/node_modules/ 3 | **/*.log 4 | test/repo-tests* 5 | **/bundle.js 6 | 7 | # Logs 8 | logs 9 | *.log 10 | 11 | coverage 12 | 13 | # Runtime data 14 | pids 15 | *.pid 16 | *.seed 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | 24 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 25 | .grunt 26 | 27 | # node-waf configuration 28 | .lock-wscript 29 | 30 | build 31 | 32 | # Dependency directory 33 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git 34 | node_modules 35 | 36 | lib 37 | dist 38 | test/test-data/go-ipfs-repo/LOCK 39 | test/test-data/go-ipfs-repo/LOG 40 | test/test-data/go-ipfs-repo/LOG.old 41 | 42 | # while testing npm5 43 | package-lock.json 44 | yarn.lock 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 David Dias 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. 22 | 23 | -------------------------------------------------------------------------------- /src/handler.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const pull = require('pull-stream/pull') 4 | const handshake = require('pull-handshake') 5 | const constants = require('./constants') 6 | const PROTOCOL = constants.PROTOCOL 7 | const PING_LENGTH = constants.PING_LENGTH 8 | 9 | const debug = require('debug') 10 | const log = debug('libp2p-ping') 11 | log.error = debug('libp2p-ping:error') 12 | 13 | function mount (swarm) { 14 | swarm.handle(PROTOCOL, (protocol, conn) => { 15 | const stream = handshake({ timeout: 0 }) 16 | const shake = stream.handshake 17 | 18 | // receive and echo back 19 | function next () { 20 | shake.read(PING_LENGTH, (err, buf) => { 21 | if (err === true) { 22 | // stream closed 23 | return 24 | } 25 | if (err) { 26 | return log.error(err) 27 | } 28 | 29 | shake.write(buf) 30 | return next() 31 | }) 32 | } 33 | 34 | pull( 35 | conn, 36 | stream, 37 | conn 38 | ) 39 | 40 | next() 41 | }) 42 | } 43 | 44 | function unmount (swarm) { 45 | swarm.unhandle(PROTOCOL) 46 | } 47 | 48 | exports = module.exports 49 | exports.mount = mount 50 | exports.unmount = unmount 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ⛔️ DEPRECATED: libp2p-ping is now included in [js-libp2p](https://github.com/libp2p/js-libp2p) 2 | ===== 3 | 4 | libp2p-ping JavaScript Implementation 5 | ===================================== 6 | 7 | [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai) 8 | [![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/) 9 | [![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p) 10 | [![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io) 11 | [![Coverage Status](https://coveralls.io/repos/github/libp2p/js-libp2p-ping/badge.svg?branch=master)](https://coveralls.io/github/libp2p/js-libp2p-ping?branch=master) 12 | [![Dependency Status](https://david-dm.org/libp2p/js-libp2p-ping.svg?style=flat-square)](https://david-dm.org/libp2p/js-libp2p-ping) 13 | [![Travis CI](https://travis-ci.org/libp2p/js-libp2p-ping.svg?branch=master)](https://travis-ci.org/libp2p/js-libp2p-ping) 14 | [![Circle CI](https://circleci.com/gh/libp2p/js-libp2p-ping.svg?style=svg)](https://circleci.com/gh/libp2p/js-libp2p-ping) 15 | 16 | > IPFS ping protocol JavaScript implementation 17 | 18 | ## Lead Maintainer 19 | 20 | [Jacob Heun](https://github.com/jacobheun/) 21 | 22 | ## Usage 23 | 24 | ```javascript 25 | var Ping = require('libp2p-ping') 26 | 27 | Ping.mount(swarm) // Enable this peer to echo Ping requests 28 | 29 | var p = new Ping(swarm, peerDst) // Ping peerDst, peerDst must be a peer-info object 30 | 31 | p.on('ping', function (time) { 32 | console.log(time + 'ms') 33 | p.stop() // stop sending pings 34 | }) 35 | 36 | p.start() 37 | ``` 38 | -------------------------------------------------------------------------------- /src/ping.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const EventEmitter = require('events').EventEmitter 4 | const pull = require('pull-stream/pull') 5 | const empty = require('pull-stream/sources/empty') 6 | const handshake = require('pull-handshake') 7 | const constants = require('./constants') 8 | const util = require('./util') 9 | const rnd = util.rnd 10 | const debug = require('debug') 11 | const log = debug('libp2p-ping') 12 | log.error = debug('libp2p-ping:error') 13 | 14 | const PROTOCOL = constants.PROTOCOL 15 | const PING_LENGTH = constants.PING_LENGTH 16 | 17 | class Ping extends EventEmitter { 18 | constructor (swarm, peer) { 19 | super() 20 | 21 | this._stopped = false 22 | this.peer = peer 23 | this.swarm = swarm 24 | } 25 | 26 | start () { 27 | log('dialing %s to %s', PROTOCOL, this.peer.id.toB58String()) 28 | 29 | this.swarm.dial(this.peer, PROTOCOL, (err, conn) => { 30 | if (err) { 31 | return this.emit('error', err) 32 | } 33 | 34 | const stream = handshake({ timeout: 0 }) 35 | this.shake = stream.handshake 36 | 37 | pull( 38 | stream, 39 | conn, 40 | stream 41 | ) 42 | 43 | // write and wait to see ping back 44 | const self = this 45 | function next () { 46 | let start = new Date() 47 | let buf = rnd(PING_LENGTH) 48 | self.shake.write(buf) 49 | self.shake.read(PING_LENGTH, (err, bufBack) => { 50 | let end = new Date() 51 | if (err || !buf.equals(bufBack)) { 52 | const err = new Error('Received wrong ping ack') 53 | return self.emit('error', err) 54 | } 55 | 56 | self.emit('ping', end - start) 57 | 58 | if (self._stopped) { 59 | return 60 | } 61 | next() 62 | }) 63 | } 64 | 65 | next() 66 | }) 67 | } 68 | 69 | stop () { 70 | if (this._stopped || !this.shake) { 71 | return 72 | } 73 | 74 | this._stopped = true 75 | 76 | pull( 77 | empty(), 78 | this.shake.rest() 79 | ) 80 | } 81 | } 82 | 83 | module.exports = Ping 84 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "libp2p-ping", 3 | "version": "0.8.5", 4 | "description": "libp2p Ping protocol implementation", 5 | "leadMaintainer": "Jacob Heun ", 6 | "main": "src/index.js", 7 | "files": [ 8 | "dist", 9 | "src" 10 | ], 11 | "pre-push": [ 12 | "lint" 13 | ], 14 | "scripts": { 15 | "test": "aegir test -t node", 16 | "test:node": "aegir test -t node", 17 | "build": "aegir build", 18 | "lint": "aegir lint", 19 | "release": "aegir release -t node", 20 | "release-minor": "aegir release --type minor -t node", 21 | "release-major": "aegir release --type major -t node", 22 | "coverage": "aegir coverage", 23 | "coverage-publish": "aegir coverage publish" 24 | }, 25 | "repository": { 26 | "type": "git", 27 | "url": "https://github.com/libp2p/js-libp2p-ping.git" 28 | }, 29 | "keywords": [ 30 | "IPFS" 31 | ], 32 | "license": "MIT", 33 | "bugs": { 34 | "url": "https://github.com/libp2p/js-libp2p-ping/issues" 35 | }, 36 | "homepage": "https://github.com/libp2p/js-libp2p-ping", 37 | "devDependencies": { 38 | "aegir": "^18.2.2", 39 | "async": "^2.6.2", 40 | "chai": "^4.2.0", 41 | "debug": "^4.1.1", 42 | "dirty-chai": "^2.0.1", 43 | "libp2p-switch": "~0.42.9", 44 | "libp2p-tcp": "~0.13.0", 45 | "multiaddr": "^6.0.6", 46 | "peer-book": "~0.9.1", 47 | "peer-id": "~0.12.2", 48 | "peer-info": "~0.15.1", 49 | "run-parallel": "^1.1.9" 50 | }, 51 | "dependencies": { 52 | "libp2p-crypto": "~0.16.1", 53 | "pull-handshake": "^1.1.4", 54 | "pull-stream": "^3.6.9" 55 | }, 56 | "contributors": [ 57 | "David Dias ", 58 | "Francisco Baio Dias ", 59 | "Friedel Ziegelmayer ", 60 | "Hugo Dias ", 61 | "Jacob Heun ", 62 | "João Antunes ", 63 | "Richard Littauer ", 64 | "Vasco Santos ", 65 | "Vasco Santos ", 66 | "greenkeeperio-bot ", 67 | "ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ " 68 | ] 69 | } 70 | -------------------------------------------------------------------------------- /test/test-ping.js: -------------------------------------------------------------------------------- 1 | /* eslint-env mocha */ 2 | 'use strict' 3 | 4 | const chai = require('chai') 5 | const dirtyChai = require('dirty-chai') 6 | const expect = chai.expect 7 | chai.use(dirtyChai) 8 | const PeerInfo = require('peer-info') 9 | const PeerBook = require('peer-book') 10 | 11 | const Swarm = require('libp2p-switch') 12 | const TCP = require('libp2p-tcp') 13 | const series = require('async/series') 14 | const parallel = require('async/parallel') 15 | 16 | const Ping = require('./../src') 17 | 18 | describe('libp2p ping', () => { 19 | let swarmA 20 | let swarmB 21 | let peerA 22 | let peerB 23 | 24 | before(function (done) { 25 | this.timeout(20 * 1000) 26 | series([ 27 | (cb) => PeerInfo.create((err, peerInfo) => { 28 | expect(err).to.not.exist() 29 | peerA = peerInfo 30 | peerA.multiaddrs.add('/ip4/127.0.0.1/tcp/0') 31 | cb() 32 | }), 33 | (cb) => PeerInfo.create((err, peerInfo) => { 34 | expect(err).to.not.exist() 35 | peerB = peerInfo 36 | peerB.multiaddrs.add('/ip4/127.0.0.1/tcp/0') 37 | cb() 38 | }), 39 | (cb) => { 40 | swarmA = new Swarm(peerA, new PeerBook()) 41 | swarmB = new Swarm(peerB, new PeerBook()) 42 | swarmA.transport.add('tcp', new TCP()) 43 | swarmB.transport.add('tcp', new TCP()) 44 | cb() 45 | }, 46 | (cb) => swarmA.start(cb), 47 | (cb) => swarmB.start(cb), 48 | (cb) => { 49 | Ping.mount(swarmA) 50 | Ping.mount(swarmB) 51 | cb() 52 | } 53 | ], done) 54 | }) 55 | 56 | after((done) => { 57 | parallel([ 58 | (cb) => swarmA.stop(cb), 59 | (cb) => swarmB.stop(cb) 60 | ], done) 61 | }) 62 | 63 | it('ping once from peerA to peerB', (done) => { 64 | const p = new Ping(swarmA, peerB) 65 | 66 | p.on('error', (err) => { 67 | expect(err).to.not.exist() 68 | }) 69 | 70 | p.on('ping', (time) => { 71 | expect(time).to.be.a('Number') 72 | p.stop() 73 | done() 74 | }) 75 | 76 | p.start() 77 | }) 78 | 79 | it('ping 5 times from peerB to peerA', (done) => { 80 | const p = new Ping(swarmB, peerA) 81 | 82 | p.on('error', (err) => { 83 | expect(err).to.not.exist() 84 | }) 85 | 86 | let counter = 0 87 | 88 | p.on('ping', (time) => { 89 | expect(time).to.be.a('Number') 90 | if (++counter === 5) { 91 | p.stop() 92 | done() 93 | } 94 | }) 95 | 96 | p.start() 97 | }) 98 | 99 | it('cannot ping itself', (done) => { 100 | const p = new Ping(swarmA, peerA) 101 | 102 | p.on('error', (err) => { 103 | expect(err).to.exist() 104 | done() 105 | }) 106 | 107 | p.on('ping', () => { 108 | expect.fail('should not be called') 109 | }) 110 | 111 | p.start() 112 | }) 113 | 114 | it('unmount PING protocol', () => { 115 | Ping.unmount(swarmA) 116 | Ping.unmount(swarmB) 117 | }) 118 | }) 119 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | ## [0.8.5](https://github.com/libp2p/js-libp2p-ping/compare/v0.8.4...v0.8.5) (2019-01-10) 3 | 4 | 5 | ### Bug Fixes 6 | 7 | * bundle size ([#73](https://github.com/libp2p/js-libp2p-ping/issues/73)) ([c0ae7e9](https://github.com/libp2p/js-libp2p-ping/commit/c0ae7e9)) 8 | 9 | 10 | 11 | 12 | ## [0.8.4](https://github.com/libp2p/js-libp2p-ping/compare/v0.8.3...v0.8.4) (2019-01-04) 13 | 14 | 15 | 16 | 17 | ## [0.8.3](https://github.com/libp2p/js-libp2p-ping/compare/v0.8.2...v0.8.3) (2018-11-09) 18 | 19 | 20 | 21 | 22 | ## [0.8.2](https://github.com/libp2p/js-libp2p-ping/compare/v0.8.1...v0.8.2) (2018-10-31) 23 | 24 | 25 | 26 | 27 | ## [0.8.1](https://github.com/libp2p/js-libp2p-ping/compare/v0.8.0...v0.8.1) (2018-10-31) 28 | 29 | 30 | 31 | 32 | # [0.8.0](https://github.com/libp2p/js-libp2p-ping/compare/v0.7.0...v0.8.0) (2018-04-23) 33 | 34 | 35 | ### Features 36 | 37 | * add start/stop method ([#69](https://github.com/libp2p/js-libp2p-ping/issues/69)) ([c0a6a46](https://github.com/libp2p/js-libp2p-ping/commit/c0a6a46)) 38 | 39 | 40 | 41 | 42 | # [0.7.0](https://github.com/libp2p/js-libp2p-ping/compare/v0.6.1...v0.7.0) (2018-04-05) 43 | 44 | 45 | 46 | 47 | ## [0.6.1](https://github.com/libp2p/js-libp2p-ping/compare/v0.6.0...v0.6.1) (2018-02-12) 48 | 49 | 50 | 51 | 52 | # [0.6.0](https://github.com/libp2p/js-libp2p-ping/compare/v0.5.0...v0.6.0) (2017-09-03) 53 | 54 | 55 | ### Features 56 | 57 | * p2p addrs situation ([#67](https://github.com/libp2p/js-libp2p-ping/issues/67)) ([c4b68c6](https://github.com/libp2p/js-libp2p-ping/commit/c4b68c6)) 58 | 59 | 60 | 61 | 62 | # [0.5.0](https://github.com/libp2p/js-libp2p-ping/compare/v0.4.0...v0.5.0) (2017-07-22) 63 | 64 | 65 | 66 | 67 | # [0.4.0](https://github.com/libp2p/js-libp2p-ping/compare/v0.3.2...v0.4.0) (2017-07-08) 68 | 69 | 70 | ### Features 71 | 72 | * update to latest deps and apis ([75646ac](https://github.com/libp2p/js-libp2p-ping/commit/75646ac)) 73 | 74 | 75 | 76 | 77 | ## [0.3.2](https://github.com/libp2p/js-libp2p-ping/compare/v0.3.1...v0.3.2) (2017-03-21) 78 | 79 | 80 | 81 | 82 | ## [0.3.1](https://github.com/libp2p/js-libp2p-ping/compare/v0.3.0...v0.3.1) (2017-02-09) 83 | 84 | 85 | 86 | 87 | # [0.3.0](https://github.com/libp2p/js-libp2p-ping/compare/v0.2.0...v0.3.0) (2016-12-01) 88 | 89 | 90 | ### Bug Fixes 91 | 92 | * refactor ([e892036](https://github.com/libp2p/js-libp2p-ping/commit/e892036)) 93 | * stop using lack of default memset as random number generator ([9ee10ad](https://github.com/libp2p/js-libp2p-ping/commit/9ee10ad)) 94 | * update ci configs ([d4174ad](https://github.com/libp2p/js-libp2p-ping/commit/d4174ad)) 95 | 96 | 97 | ### Features 98 | 99 | * moar tests ([a10bb7c](https://github.com/libp2p/js-libp2p-ping/commit/a10bb7c)) 100 | * supports multiple pings ([be4c9b0](https://github.com/libp2p/js-libp2p-ping/commit/be4c9b0)) 101 | * update Ping to latests libp2p versions, fix tests, add more tests, update API ([fc1be3a](https://github.com/libp2p/js-libp2p-ping/commit/fc1be3a)) 102 | * use libp2p-crypto instead ([efba7fa](https://github.com/libp2p/js-libp2p-ping/commit/efba7fa)) 103 | * use random ping value generator ([bca4a21](https://github.com/libp2p/js-libp2p-ping/commit/bca4a21)) 104 | 105 | 106 | 107 | 108 | # [0.2.0](https://github.com/libp2p/js-libp2p-ping/compare/v0.1.3...v0.2.0) (2016-09-07) 109 | 110 | 111 | ### Features 112 | 113 | * **deps:** update deps ([2913fd4](https://github.com/libp2p/js-libp2p-ping/commit/2913fd4)) 114 | * **package.json:** add release scripts ([7e9940f](https://github.com/libp2p/js-libp2p-ping/commit/7e9940f)) 115 | 116 | 117 | 118 | 119 | ## [0.1.3](https://github.com/libp2p/js-libp2p-ping/compare/v0.1.1...v0.1.3) (2016-06-29) 120 | 121 | 122 | 123 | 124 | ## [0.1.1](https://github.com/libp2p/js-libp2p-ping/compare/v0.1.0...v0.1.1) (2015-10-29) 125 | 126 | 127 | 128 | 129 | # 0.1.0 (2015-07-27) 130 | 131 | 132 | 133 | --------------------------------------------------------------------------------