Skip to content

Commit

Permalink
chore: update aegir
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Mar 29, 2021
1 parent cec3ce7 commit a3a2f15
Show file tree
Hide file tree
Showing 59 changed files with 590 additions and 267 deletions.
24 changes: 16 additions & 8 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const path = require('path')
const Libp2p = require('./src')
const { MULTIADDRS_WEBSOCKETS } = require('./test/fixtures/browser')
const Peers = require('./test/fixtures/peers')
Expand Down Expand Up @@ -47,16 +48,23 @@ const after = async () => {
await libp2p.stop()
}

/** @type {import('aegir').Options["build"]["config"]} */
const esbuild = {
inject: [path.join(__dirname, './scripts/node-globals.js')]
}

/** @type {import('aegir').PartialOptions} */
module.exports = {
bundlesize: { maxSize: '225kB' },
hooks: {
pre: before,
post: after
build: {
bundlesizeMax: '225kB'
},
webpack: {
node: {
// needed by bcrypto
Buffer: true
test: {
before,
after,
browser: {
config: {
buildConfig: esbuild
}
}
}
}
46 changes: 28 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,25 @@
"browser": {
"@motrix/nat-api": false
},
"eslintConfig": {
"extends": "ipfs",
"ignorePatterns": [
"!.aegir.js"
]
},
"dependencies": {
"@motrix/nat-api": "^0.3.1",
"@types/varint": "^6.0.0",
"abort-controller": "^3.0.0",
"aggregate-error": "^3.1.0",
"any-signal": "^2.1.1",
"bignumber.js": "^9.0.1",
"cids": "^1.1.5",
"class-is": "^1.1.0",
"debug": "^4.3.1",
"err-code": "^2.0.0",
"err-code": "^3.0.0",
"es6-promisify": "^6.1.1",
"events": "^3.2.0",
"events": "^3.3.0",
"hashlru": "^2.3.0",
"interface-datastore": "^3.0.3",
"ipfs-utils": "^6.0.0",
Expand All @@ -83,42 +90,43 @@
"it-take": "1.0.0",
"libp2p-crypto": "^0.19.0",
"libp2p-interfaces": "libp2p/js-libp2p-interfaces#chore/update-types",
"libp2p-utils": "^0.2.2",
"libp2p-utils": "libp2p/js-libp2p-utils#feat/add-types",
"mafmt": "^8.0.0",
"merge-options": "^3.0.4",
"moving-average": "^1.0.0",
"multiaddr": "^8.1.0",
"multicodec": "^2.1.0",
"multihashing-async": "^2.0.1",
"multicodec": "^3.0.1",
"multihashing-async": "^2.1.2",
"multistream-select": "^1.0.0",
"mutable-proxy": "^1.0.0",
"node-forge": "^0.10.0",
"p-any": "^3.0.0",
"p-fifo": "^1.0.0",
"p-retry": "^4.2.0",
"p-settle": "^4.0.1",
"p-retry": "^4.4.0",
"p-settle": "^4.1.1",
"peer-id": "^0.14.2",
"private-ip": "^2.0.0",
"private-ip": "^2.1.0",
"protons": "^2.0.0",
"retimer": "^2.0.0",
"retimer": "^3.0.0",
"sanitize-filename": "^1.6.3",
"set-delayed-interval": "^1.0.0",
"streaming-iterables": "^5.0.2",
"timeout-abort-controller": "^1.1.1",
"varint": "^6.0.0",
"xsalsa20": "^1.0.2"
"xsalsa20": "^1.1.0"
},
"devDependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
"@types/es6-promisify": "^6.0.0",
"@types/node-forge": "^0.9.7",
"abortable-iterator": "^3.0.0",
"aegir": "^29.2.0",
"aegir": "^32.1.0",
"chai-bytes": "^0.1.2",
"chai-string": "^1.5.0",
"delay": "^4.4.0",
"delay": "^5.0.0",
"interop-libp2p": "^0.3.0",
"into-stream": "^6.0.0",
"ipfs-http-client": "^48.2.2",
"ipfs-http-client": "^49.0.4",
"it-concat": "^1.0.0",
"it-pair": "^1.0.0",
"it-pushable": "^1.4.0",
Expand All @@ -128,22 +136,24 @@
"libp2p-delegated-peer-routing": "^0.8.0",
"libp2p-floodsub": "^0.24.0",
"libp2p-gossipsub": "^0.8.0",
"libp2p-kad-dht": "^0.20.5",
"libp2p-kad-dht": "^0.21.0",
"libp2p-mdns": "^0.15.0",
"libp2p-mplex": "^0.10.1",
"libp2p-noise": "^2.0.0",
"libp2p-secio": "^0.13.1",
"libp2p-tcp": "^0.15.1",
"libp2p-webrtc-star": "^0.20.0",
"libp2p-webrtc-star": "^0.21.2",
"libp2p-websockets": "^0.15.0",
"multihashes": "^3.0.1",
"multihashes": "^4.0.2",
"nock": "^13.0.3",
"p-defer": "^3.0.0",
"p-times": "^3.0.0",
"p-wait-for": "^3.2.0",
"rimraf": "^3.0.2",
"sinon": "^9.2.4",
"uint8arrays": "^2.0.5"
"sinon": "^10.0.0",
"uint8arrays": "^2.1.3",
"url": "^0.11.0",
"util": "^0.12.3"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
Expand Down
3 changes: 3 additions & 0 deletions scripts/node-globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @ts-nocheck
export const { Buffer } = require('buffer')
export const url = require('url')
19 changes: 14 additions & 5 deletions src/circuit/circuit/hop.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ const multicodec = require('./../multicodec')
/**
* @typedef {import('../../types').CircuitRequest} CircuitRequest
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
* @typedef {import('./stream-handler')<CircuitRequest>} StreamHandlerT
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
* @typedef {import('../transport')} Transport
*/

/**
* @typedef {Object} HopRequest
* @property {Connection} connection
* @property {CircuitRequest} request
* @property {StreamHandlerT} streamHandler
* @property {StreamHandler} streamHandler
* @property {Transport} circuit
*/

Expand Down Expand Up @@ -58,6 +58,11 @@ async function handleHop ({
return log.error('invalid hop request via peer %s', connection.remotePeer.toB58String(), err)
}

if (!request.dstPeer) {
log('HOP request received but we do not receive a dstPeer')
return
}

// Get the connection to the destination (stop) peer
const destinationPeer = new PeerId(request.dstPeer.id)

Expand Down Expand Up @@ -114,7 +119,7 @@ async function handleHop ({
* @param {object} options
* @param {Connection} options.connection - Connection to the relay
* @param {CircuitRequest} options.request
* @returns {Promise<Connection>}
* @returns {Promise<MuxedStream>}
*/
async function hop ({
connection,
Expand All @@ -128,6 +133,10 @@ async function hop ({

const response = await streamHandler.read()

if (!response) {
throw errCode(new Error('HOP request had no response'), Errors.ERR_HOP_REQUEST_FAILED)
}

if (response.code === CircuitPB.Status.SUCCESS) {
log('hop request was successful')
return streamHandler.rest()
Expand Down Expand Up @@ -159,7 +168,7 @@ async function canHop ({
const response = await streamHandler.read()
await streamHandler.close()

if (response.code !== CircuitPB.Status.SUCCESS) {
if (!response || response.code !== CircuitPB.Status.SUCCESS) {
return false
}

Expand All @@ -171,7 +180,7 @@ async function canHop ({
*
* @param {Object} options
* @param {Connection} options.connection
* @param {StreamHandlerT} options.streamHandler
* @param {StreamHandler} options.streamHandler
* @param {Transport} options.circuit
* @private
*/
Expand Down
6 changes: 5 additions & 1 deletion src/circuit/circuit/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { validateAddrs } = require('./utils')
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
* @typedef {import('../../types').CircuitRequest} CircuitRequest
* @typedef {import('./stream-handler')<CircuitRequest>} StreamHandlerT
* @typedef {import('./stream-handler')} StreamHandlerT
*/

/**
Expand Down Expand Up @@ -68,6 +68,10 @@ module.exports.stop = async function stop ({
streamHandler.write(request)
const response = await streamHandler.read()

if (!response) {
return streamHandler.close()
}

if (response.code === CircuitPB.Status.SUCCESS) {
log('stop request to %s was successful', connection.remotePeer.toB58String())
return streamHandler.rest()
Expand Down
12 changes: 7 additions & 5 deletions src/circuit/circuit/stream-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ const log = Object.assign(debug('libp2p:circuit:stream-handler'), {
})

const lp = require('it-length-prefixed')
// @ts-ignore it-handshake does not export types
const handshake = require('it-handshake')
const { CircuitRelay: CircuitPB } = require('../protocol')

/**
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
* @typedef {import('../../types').CircuitRequest} CircuitRequest
*/

/**
* @template T
*/
class StreamHandler {
/**
* Create a stream handler for connection
Expand All @@ -36,7 +35,7 @@ class StreamHandler {
* Read and decode message
*
* @async
* @returns {Promise<T|undefined>}
* @returns {Promise<CircuitRequest|undefined>}
*/
async read () {
const msg = await this.decoder.next()
Expand All @@ -54,7 +53,7 @@ class StreamHandler {
/**
* Encode and write array of buffers
*
* @param {CircuitPB} msg - An unencoded CircuitRelay protobuf message
* @param {CircuitRequest} msg - An unencoded CircuitRelay protobuf message
* @returns {void}
*/
write (msg) {
Expand All @@ -73,6 +72,9 @@ class StreamHandler {
return this.shake.stream
}

/**
* @param {CircuitRequest} msg - An unencoded CircuitRelay protobuf message
*/
end (msg) {
this.write(msg)
this.close()
Expand Down
4 changes: 2 additions & 2 deletions src/circuit/circuit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function writeResponse (streamHandler, status) {
*/
function validateAddrs (msg, streamHandler) {
try {
msg.dstPeer.addrs.forEach((addr) => {
msg.dstPeer.addrs.forEach((/** @type {string} */ addr) => {
return multiaddr(addr)
})
} catch (err) {
Expand All @@ -40,7 +40,7 @@ function validateAddrs (msg, streamHandler) {
}

try {
msg.srcPeer.addrs.forEach((addr) => {
msg.srcPeer.addrs.forEach((/** @type {string} */ addr) => {
return multiaddr(addr)
})
} catch (err) {
Expand Down
9 changes: 1 addition & 8 deletions src/circuit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ const log = Object.assign(debug('libp2p:relay'), {
const {
setDelayedInterval,
clearDelayedInterval
// @ts-ignore set-delayed-interval does not export types
} = require('set-delayed-interval')

const AutoRelay = require('./auto-relay')
const { namespaceToCid } = require('./utils')
const {
ADVERTISE_BOOT_DELAY,
ADVERTISE_TTL,
RELAY_RENDEZVOUS_NS
} = require('./constants')

Expand Down Expand Up @@ -45,12 +44,6 @@ class Relay {
constructor (libp2p) {
this._libp2p = libp2p
this._options = {
advertise: {
bootDelay: ADVERTISE_BOOT_DELAY,
enabled: true,
ttl: ADVERTISE_TTL,
...libp2p._config.relay.advertise
},
...libp2p._config.relay
}

Expand Down
2 changes: 2 additions & 0 deletions src/circuit/protocol/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict'

// @ts-ignore protons does not have types
const protobuf = require('protons')

/** @type {{CircuitRelay: import('../../types').CircuitMessageProto}} */
Expand Down
4 changes: 3 additions & 1 deletion src/circuit/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Circuit {
* @param {MuxedStream} props.stream
*/
async _onProtocol ({ connection, stream }) {
/** @type {import('./circuit/stream-handler')<CircuitRequest>} */
/** @type {import('./circuit/stream-handler')} */
const streamHandler = new StreamHandler({ stream })
const request = await streamHandler.read()

Expand Down Expand Up @@ -96,7 +96,9 @@ class Circuit {
}

if (virtualConnection) {
// @ts-ignore dst peer will not be undefined
const remoteAddr = multiaddr(request.dstPeer.addrs[0])
// @ts-ignore src peer will not be undefined
const localAddr = multiaddr(request.srcPeer.addrs[0])
const maConn = toConnection({
stream: virtualConnection,
Expand Down
Loading

0 comments on commit a3a2f15

Please sign in to comment.