Compare commits

..

8 Commits

Author SHA1 Message Date
Vasco Santos
266f2c3c86 chore: release version v0.32.3 2021-08-16 18:20:41 +02:00
Vasco Santos
7783edb059 chore: update contributors 2021-08-16 18:20:40 +02:00
Vasco Santos
ba2b4d4b28 fix: uint8arrays is a dep (#964) 2021-08-16 17:54:24 +02:00
Vasco Santos
06c3a6d407 chore: release version v0.32.2 2021-08-13 17:02:52 +02:00
Vasco Santos
833f789714 chore: update contributors 2021-08-13 17:02:51 +02:00
greenSnot
ef24fabf02 feat: custom protocol name (#962)
Co-authored-by: mzdws <8580712+mzdws@user.noreply.gitee.com>
2021-08-13 16:21:50 +02:00
Vasco Santos
3b33fb4b73 fix: browser example ci 2021-08-12 15:15:13 +02:00
Vasco Santos
a07fb7960b chore: update action setup node 2021-08-12 15:15:13 +02:00
14 changed files with 207 additions and 80 deletions

View File

@@ -12,16 +12,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js 14
uses: actions/setup-node@v1
with:
node-version: 14
- uses: actions/setup-node@v2
with:
node-version: 14
- run: npm install
- run: npx aegir lint
- uses: gozala/typescript-error-reporter-action@v1.0.8
- run: npx aegir build
- run: npx aegir dep-check
- uses: ipfs/aegir/actions/bundle-size@master
- uses: ipfs/aegir/actions/bundle-size@v32.1.0
name: size
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
@@ -35,7 +34,7 @@ jobs:
fail-fast: true
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
- run: npm install

View File

@@ -1,3 +1,26 @@
## [0.32.3](https://github.com/libp2p/js-libp2p/compare/v0.32.2...v0.32.3) (2021-08-16)
### Bug Fixes
* uint8arrays is a dep ([#964](https://github.com/libp2p/js-libp2p/issues/964)) ([ba2b4d4](https://github.com/libp2p/js-libp2p/commit/ba2b4d4b28f1d9940b457de344aed44537f9eabd))
## [0.32.2](https://github.com/libp2p/js-libp2p/compare/v0.32.1...v0.32.2) (2021-08-13)
### Bug Fixes
* browser example ci ([3b33fb4](https://github.com/libp2p/js-libp2p/commit/3b33fb4b73ba8065e432fb59f758fe138fd23d9e))
### Features
* custom protocol name ([#962](https://github.com/libp2p/js-libp2p/issues/962)) ([ef24fab](https://github.com/libp2p/js-libp2p/commit/ef24fabf0269fd079888e92eedb458e23ef1c733))
## [0.32.1](https://github.com/libp2p/js-libp2p/compare/v0.32.0...v0.32.1) (2021-07-22)

View File

@@ -781,6 +781,26 @@ By default under nodejs libp2p will attempt to use [UPnP](https://en.wikipedia.o
[NAT-PMP](http://miniupnp.free.fr/nat-pmp.html) is a feature of some modern routers which performs a similar job to UPnP. NAT-PMP is disabled by default, if enabled libp2p will try to use NAT-PMP and will fall back to UPnP if it fails.
#### Configuring protocol name
Changing the protocol name prefix can isolate default public network (IPFS) for custom purposes.
```js
const node = await Libp2p.create({
config: {
protocolPrefix: 'ipfs' // default
}
})
/*
protocols: [
"/ipfs/id/1.0.0", // identify service protocol (if we have multiplexers)
"/ipfs/id/push/1.0.0", // identify service push protocol (if we have multiplexers)
"/ipfs/ping/1.0.0", // built-in ping protocol
]
*/
```
## Configuration examples
As libp2p is designed to be a modular networking library, its usage will vary based on individual project needs. We've included links to some existing project configurations for your reference, in case you wish to replicate their configuration:

View File

@@ -16,7 +16,7 @@
<pre id="output"></pre>
</main>
<script src="./index.js"></script>
<script type="module" src="./index.js"></script>
</body>

View File

@@ -2,7 +2,6 @@
"name": "libp2p-in-browser",
"version": "1.0.0",
"description": "A libp2p node running in the browser",
"main": "dist/index.html",
"browserslist": [
"last 2 Chrome versions"
],

View File

@@ -1,6 +1,6 @@
{
"name": "libp2p",
"version": "0.32.1",
"version": "0.32.3",
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js",
@@ -124,6 +124,7 @@
"set-delayed-interval": "^1.0.0",
"streaming-iterables": "^6.0.0",
"timeout-abort-controller": "^1.1.1",
"uint8arrays": "^2.1.3",
"varint": "^6.0.0",
"wherearewe": "^1.0.0",
"xsalsa20": "^1.1.0"
@@ -165,7 +166,6 @@
"p-wait-for": "^3.2.0",
"rimraf": "^3.0.2",
"sinon": "^11.1.1",
"uint8arrays": "^2.1.3",
"util": "^0.12.3"
},
"contributors": [
@@ -182,51 +182,19 @@
"Volker Mische <volker.mische@gmail.com>",
"Chris Dostert <chrisdostert@users.noreply.github.com>",
"dirkmc <dirkmdev@gmail.com>",
"zeim839 <50573884+zeim839@users.noreply.github.com>",
"Richard Littauer <richard.littauer@gmail.com>",
"zeim839 <50573884+zeim839@users.noreply.github.com>",
"Ryan Bell <ryan@piing.net>",
"a1300 <matthias-knopp@gmx.net>",
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>",
"Samlior <samlior@foxmail.com>",
"Franck Royer <franck@royer.one>",
"Thomas Eizinger <thomas@eizinger.io>",
"Giovanni T. Parra <fiatjaf@gmail.com>",
"acolytec3 <17355484+acolytec3@users.noreply.github.com>",
"Elven <mon.samuel@qq.com>",
"Andrew Nesbitt <andrewnez@gmail.com>",
"Didrik Nordström <didrik.nordstrom@gmail.com>",
"RasmusErik Voel Jensen <github@solsort.com>",
"Robert Kiel <robert.kiel@hoprnet.org>",
"Aditya Bose <13054902+adbose@users.noreply.github.com>",
"Smite Chow <xiaopengyou@live.com>",
"Soeren <nikorpoulsen@gmail.com>",
"Sönke Hahn <soenkehahn@gmail.com>",
"TJKoury <TJKoury@gmail.com>",
"Tiago Alves <alvesjtiago@gmail.com>",
"Yusef Napora <yusef@napora.org>",
"Zane Starr <zcstarr@gmail.com>",
"ebinks <elizabethjbinks@gmail.com>",
"isan_rivkin <isanrivkin@gmail.com>",
"mayerwin <mayerwin@users.noreply.github.com>",
"mcclure <andi.m.mcclure@gmail.com>",
"phillmac <phillmac@users.noreply.github.com>",
"robertkiel <robert.kiel@validitylabs.org>",
"shresthagrawal <34920931+shresthagrawal@users.noreply.github.com>",
"swedneck <40505480+swedneck@users.noreply.github.com>",
"Aleksei <vozhdb@gmail.com>",
"Bernd Strehl <bernd.strehl@gmail.com>",
"Chris Bratlien <chrisbratlien@gmail.com>",
"Cindy Wu <ciindy.wu@gmail.com>",
"Daijiro Wachi <daijiro.wachi@gmail.com>",
"Diogo Silva <fsdiogo@gmail.com>",
"Dmitriy Ryajov <dryajov@gmail.com>",
"Ethan Lam <elmemphis2000@gmail.com>",
"Fei Liu <liu.feiwood@gmail.com>",
"Felipe Martins <felipebrasil93@gmail.com>",
"Florian-Merle <florian.david.merle@gmail.com>",
"Francis Gulotta <wizard@roborooter.com>",
"Guy Sviry <32539816+guysv@users.noreply.github.com>",
"Henrique Dias <hacdias@gmail.com>",
"Elven <mon.samuel@qq.com>",
"Franck Royer <franck@royer.one>",
"Giovanni T. Parra <fiatjaf@gmail.com>",
"Samlior <samlior@foxmail.com>",
"Thomas Eizinger <thomas@eizinger.io>",
"acolytec3 <17355484+acolytec3@users.noreply.github.com>",
"Didrik Nordström <didrik@betamos.se>",
"Irakli Gozalishvili <rfobic@gmail.com>",
"Joel Gustafson <joelg@mit.edu>",
"John Rees <johnrees@users.noreply.github.com>",
@@ -235,10 +203,43 @@
"Kevin Kwok <antimatter15@gmail.com>",
"Kevin Lacker <lacker@gmail.com>",
"Lars Gierth <lgierth@users.noreply.github.com>",
"Ethan Lam <elmemphis2000@gmail.com>",
"Marcin Tojek <mtojek@users.noreply.github.com>",
"Michael Burns <5170+mburns@users.noreply.github.com>",
"Miguel Mota <miguelmota2@gmail.com>",
"Nuno Nogueira <nunofmn@gmail.com>",
"Philipp Muens <raute1337@gmx.de>"
"Dmitriy Ryajov <dryajov@gmail.com>",
"Philipp Muens <raute1337@gmx.de>",
"RasmusErik Voel Jensen <github@solsort.com>",
"Diogo Silva <fsdiogo@gmail.com>",
"Robert Kiel <robert.kiel@hoprnet.org>",
"phillmac <phillmac@users.noreply.github.com>",
"robertkiel <robert.kiel@validitylabs.org>",
"Smite Chow <xiaopengyou@live.com>",
"Soeren <nikorpoulsen@gmail.com>",
"Sönke Hahn <soenkehahn@gmail.com>",
"TJKoury <TJKoury@gmail.com>",
"shresthagrawal <34920931+shresthagrawal@users.noreply.github.com>",
"Tiago Alves <alvesjtiago@gmail.com>",
"Daijiro Wachi <daijiro.wachi@gmail.com>",
"Yusef Napora <yusef@napora.org>",
"Zane Starr <zcstarr@gmail.com>",
"swedneck <40505480+swedneck@users.noreply.github.com>",
"Aleksei <vozhdb@gmail.com>",
"Cindy Wu <ciindy.wu@gmail.com>",
"Aditya Bose <13054902+adbose@users.noreply.github.com>",
"Chris Bratlien <chrisbratlien@gmail.com>",
"ebinks <elizabethjbinks@gmail.com>",
"greenSnot <greenSnot@users.noreply.github.com>",
"Bernd Strehl <bernd.strehl@gmail.com>",
"Francis Gulotta <wizard@roborooter.com>",
"isan_rivkin <isanrivkin@gmail.com>",
"Florian-Merle <florian.david.merle@gmail.com>",
"mayerwin <mayerwin@users.noreply.github.com>",
"Guy Sviry <32539816+guysv@users.noreply.github.com>",
"Henrique Dias <hacdias@gmail.com>",
"mcclure <andi.m.mcclure@gmail.com>",
"Felipe Martins <felipebrasil93@gmail.com>",
"Fei Liu <liu.feiwood@gmail.com>"
]
}

View File

@@ -57,6 +57,7 @@ const DefaultConfig = {
}
},
config: {
protocolPrefix: 'ipfs',
dht: {
enabled: false,
kBucketSize: 20,

View File

@@ -3,7 +3,13 @@
// @ts-ignore file not listed within the file list of projects
const libp2pVersion = require('../../package.json').version
module.exports.PROTOCOL_VERSION = 'ipfs/0.1.0'
module.exports.PROTOCOL_VERSION = 'ipfs/0.1.0' // deprecated
module.exports.AGENT_VERSION = `js-libp2p/${libp2pVersion}`
module.exports.MULTICODEC_IDENTIFY = '/ipfs/id/1.0.0'
module.exports.MULTICODEC_IDENTIFY_PUSH = '/ipfs/id/push/1.0.0'
module.exports.MULTICODEC_IDENTIFY = '/ipfs/id/1.0.0' // deprecated
module.exports.MULTICODEC_IDENTIFY_PUSH = '/ipfs/id/push/1.0.0' // deprecated
module.exports.IDENTIFY_PROTOCOL_VERSION = '0.1.0'
module.exports.MULTICODEC_IDENTIFY_PROTOCOL_NAME = 'id'
module.exports.MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME = 'id/push'
module.exports.MULTICODEC_IDENTIFY_PROTOCOL_VERSION = '1.0.0'
module.exports.MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION = '1.0.0'

View File

@@ -23,7 +23,11 @@ const PeerRecord = require('../record/peer-record')
const {
MULTICODEC_IDENTIFY,
MULTICODEC_IDENTIFY_PUSH,
PROTOCOL_VERSION
IDENTIFY_PROTOCOL_VERSION,
MULTICODEC_IDENTIFY_PROTOCOL_NAME,
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME,
MULTICODEC_IDENTIFY_PROTOCOL_VERSION,
MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION
} = require('./consts')
const { codes } = require('../errors')
@@ -39,6 +43,16 @@ const { codes } = require('../errors')
*/
class IdentifyService {
/**
* @param {import('../')} libp2p
*/
static getProtocolStr (libp2p) {
return {
identifyProtocolStr: `/${libp2p._config.protocolPrefix}/${MULTICODEC_IDENTIFY_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PROTOCOL_VERSION}`,
identifyPushProtocolStr: `/${libp2p._config.protocolPrefix}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_NAME}/${MULTICODEC_IDENTIFY_PUSH_PROTOCOL_VERSION}`
}
}
/**
* @class
* @param {Object} options
@@ -53,9 +67,13 @@ class IdentifyService {
this.handleMessage = this.handleMessage.bind(this)
const protocolStr = IdentifyService.getProtocolStr(libp2p)
this.identifyProtocolStr = protocolStr.identifyProtocolStr
this.identifyPushProtocolStr = protocolStr.identifyPushProtocolStr
// Store self host metadata
this._host = {
protocolVersion: PROTOCOL_VERSION,
protocolVersion: `${libp2p._config.protocolPrefix}/${IDENTIFY_PROTOCOL_VERSION}`,
...libp2p._options.host
}
@@ -94,7 +112,7 @@ class IdentifyService {
const pushes = connections.map(async connection => {
try {
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY_PUSH)
const { stream } = await connection.newStream(this.identifyPushProtocolStr)
await pipe(
[Message.Identify.encode({
@@ -129,7 +147,7 @@ class IdentifyService {
const connections = []
let connection
for (const peer of this.peerStore.peers.values()) {
if (peer.protocols.includes(MULTICODEC_IDENTIFY_PUSH) && (connection = this.connectionManager.get(peer.id))) {
if (peer.protocols.includes(this.identifyPushProtocolStr) && (connection = this.connectionManager.get(peer.id))) {
connections.push(connection)
}
}
@@ -147,7 +165,7 @@ class IdentifyService {
* @returns {Promise<void>}
*/
async identify (connection) {
const { stream } = await connection.newStream(MULTICODEC_IDENTIFY)
const { stream } = await connection.newStream(this.identifyProtocolStr)
const [data] = await pipe(
[],
stream,
@@ -224,9 +242,9 @@ class IdentifyService {
*/
handleMessage ({ connection, stream, protocol }) {
switch (protocol) {
case MULTICODEC_IDENTIFY:
case this.identifyProtocolStr:
return this._handleIdentify({ connection, stream })
case MULTICODEC_IDENTIFY_PUSH:
case this.identifyPushProtocolStr:
return this._handlePush({ connection, stream })
default:
log.error('cannot handle unknown protocol %s', protocol)

View File

@@ -31,7 +31,6 @@ const PersistentPeerStore = require('./peer-store/persistent')
const Registrar = require('./registrar')
const ping = require('./ping')
const IdentifyService = require('./identify')
const IDENTIFY_PROTOCOLS = IdentifyService.multicodecs
const NatManager = require('./nat-manager')
const { updateSelfPeerRecord } = require('./record/utils')
@@ -289,7 +288,7 @@ class Libp2p extends EventEmitter {
// Add the identify service since we can multiplex
this.identifyService = new IdentifyService({ libp2p: this })
this.handle(Object.values(IDENTIFY_PROTOCOLS), this.identifyService.handleMessage)
this.handle(Object.values(IdentifyService.getProtocolStr(this)), this.identifyService.handleMessage)
}
// Attach private network protector

View File

@@ -1,6 +1,8 @@
'use strict'
module.exports = {
PROTOCOL: '/ipfs/ping/1.0.0',
PING_LENGTH: 32
PROTOCOL: '/ipfs/ping/1.0.0', // deprecated
PING_LENGTH: 32,
PROTOCOL_VERSION: '1.0.0',
PROTOCOL_NAME: 'ping'
}

View File

@@ -13,7 +13,7 @@ const { toBuffer } = require('it-buffer')
const { collect, take } = require('streaming-iterables')
const equals = require('uint8arrays/equals')
const { PROTOCOL, PING_LENGTH } = require('./constants')
const { PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION } = require('./constants')
/**
* @typedef {import('../')} Libp2p
@@ -30,11 +30,12 @@ const { PROTOCOL, PING_LENGTH } = require('./constants')
* @returns {Promise<number>}
*/
async function ping (node, peer) {
const protocol = `/${node._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
// @ts-ignore multiaddr might not have toB58String
log('dialing %s to %s', PROTOCOL, peer.toB58String ? peer.toB58String() : peer)
log('dialing %s to %s', protocol, peer.toB58String ? peer.toB58String() : peer)
const connection = await node.dial(peer)
const { stream } = await connection.newStream(PROTOCOL)
const { stream } = await connection.newStream(protocol)
const start = Date.now()
const data = crypto.randomBytes(PING_LENGTH)
@@ -61,7 +62,7 @@ async function ping (node, peer) {
* @param {Libp2p} node
*/
function mount (node) {
node.handle(PROTOCOL, ({ stream }) => pipe(stream, stream))
node.handle(`/${node._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`, ({ stream }) => pipe(stream, stream))
}
/**
@@ -70,7 +71,7 @@ function mount (node) {
* @param {Libp2p} node
*/
function unmount (node) {
node.unhandle(PROTOCOL)
node.unhandle(`/${node._config.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`)
}
exports = module.exports = ping

View File

@@ -0,0 +1,47 @@
'use strict'
/* eslint-env mocha */
const { expect } = require('aegir/utils/chai')
const mergeOptions = require('merge-options')
const { create } = require('../../src')
const { baseOptions } = require('./utils')
describe('Protocol prefix is configurable', () => {
let libp2p
it('protocolPrefix is provided', async () => {
const testProtocol = 'test-protocol'
libp2p = await create(mergeOptions(baseOptions, {
config: {
protocolPrefix: testProtocol
}
}))
const protocols = libp2p.peerStore.protoBook.get(libp2p.peerId);
[
'/libp2p/circuit/relay/0.1.0',
`/${testProtocol}/id/1.0.0`,
`/${testProtocol}/id/push/1.0.0`,
`/${testProtocol}/ping/1.0.0`
].forEach((i, idx) => {
expect(protocols[idx]).equals(i)
})
await libp2p.stop()
})
it('protocolPrefix is not provided', async () => {
libp2p = await create(baseOptions)
const protocols = libp2p.peerStore.protoBook.get(libp2p.peerId);
[
'/libp2p/circuit/relay/0.1.0',
'/ipfs/id/1.0.0',
'/ipfs/id/push/1.0.0',
'/ipfs/ping/1.0.0'
].forEach((i, idx) => {
expect(protocols[idx]).equals(i)
})
await libp2p.stop()
})
})

View File

@@ -60,7 +60,8 @@ describe('Identify', () => {
peerStore: localPeerStore,
multiaddrs: listenMaddrs,
isStarted: () => true,
_options: { host: {} }
_options: { host: {} },
_config: { protocolPrefix: 'ipfs' }
}
})
const remoteIdentify = new IdentifyService({
@@ -70,7 +71,8 @@ describe('Identify', () => {
peerStore: remotePeerStore,
multiaddrs: listenMaddrs,
isStarted: () => true,
_options: { host: {} }
_options: { host: {} },
_config: { protocolPrefix: 'ipfs' }
}
})
@@ -119,7 +121,8 @@ describe('Identify', () => {
peerStore: localPeerStore,
multiaddrs: listenMaddrs,
isStarted: () => true,
_options: { host: { agentVersion } }
_options: { host: { agentVersion } },
_config: { protocolPrefix: 'ipfs' }
}
})
@@ -131,7 +134,8 @@ describe('Identify', () => {
peerStore: remotePeerStore,
multiaddrs: listenMaddrs,
isStarted: () => true,
_options: { host: { agentVersion } }
_options: { host: { agentVersion } },
_config: { protocolPrefix: 'ipfs' }
}
})
@@ -180,7 +184,8 @@ describe('Identify', () => {
connectionManager: new EventEmitter(),
peerStore: localPeerStore,
multiaddrs: [],
_options: { host: {} }
_options: { host: {} },
_config: { protocolPrefix: 'ipfs' }
}
})
const remoteIdentify = new IdentifyService({
@@ -189,7 +194,8 @@ describe('Identify', () => {
connectionManager: new EventEmitter(),
peerStore: remotePeerStore,
multiaddrs: [],
_options: { host: {} }
_options: { host: {} },
_config: { protocolPrefix: 'ipfs' }
}
})
@@ -231,7 +237,8 @@ describe('Identify', () => {
host: {
agentVersion
}
}
},
_config: { protocolPrefix: 'ipfs' }
},
protocols
})
@@ -261,7 +268,8 @@ describe('Identify', () => {
peerStore: localPeerStore,
multiaddrs: listenMaddrs,
isStarted: () => true,
_options: { host: {} }
_options: { host: {} },
_config: { protocolPrefix: 'ipfs' }
}
})
@@ -275,7 +283,8 @@ describe('Identify', () => {
peerStore: remotePeerStore,
multiaddrs: [],
isStarted: () => true,
_options: { host: {} }
_options: { host: {} },
_config: { protocolPrefix: 'ipfs' }
}
})
@@ -333,7 +342,8 @@ describe('Identify', () => {
peerStore: localPeerStore,
multiaddrs: listenMaddrs,
isStarted: () => true,
_options: { host: {} }
_options: { host: {} },
_config: { protocolPrefix: 'ipfs' }
}
})
@@ -347,6 +357,7 @@ describe('Identify', () => {
peerStore: new PeerStore({ peerId: remotePeer }),
multiaddrs: [],
_options: { host: {} },
_config: { protocolPrefix: 'ipfs' },
isStarted: () => true
}
})