Skip to content

Commit

Permalink
chore: replace protons with protobufjs
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Apr 8, 2021
1 parent 8083aae commit bd10429
Show file tree
Hide file tree
Showing 31 changed files with 3,111 additions and 259 deletions.
26 changes: 20 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
"build:proto": "npm run build:proto:circuit && npm run build:proto:plaintext && npm run build:proto:address-book && npm run build:proto:proto-book && npm run build:proto:peer-record && npm run build:proto:envelope",
"build:proto:circuit": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/circuit/protocol/index.js ./src/circuit/protocol/index.proto",
"build:proto:plaintext": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/insecure/proto.js ./src/insecure/proto.proto",
"build:proto:address-book": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/peer-store/persistent/pb/address-book.js ./src/peer-store/persistent/pb/address-book.proto",
"build:proto:proto-book": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/peer-store/persistent/pb/proto-book.js ./src/peer-store/persistent/pb/proto-book.proto",
"build:proto:peer-record": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/record/peer-record/peer-record.js ./src/record/peer-record/peer-record.proto",
"build:proto:envelope": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/record/envelope/envelope.js ./src/record/envelope/envelope.proto",
"build:proto-types": "npm run build:proto-types:circuit && npm run build:proto-types:plaintext && npm run build:proto-types:address-book && npm run build:proto-types:proto-book && npm run build:proto-types:peer-record && npm run build:proto-types:envelope",
"build:proto-types:circuit": "pbts -o src/circuit/protocol/index.d.ts src/circuit/protocol/index.js",
"build:proto-types:plaintext": "pbts -o src/insecure/proto.d.ts src/insecure/proto.js",
"build:proto-types:address-book": "pbts -o src/peer-store/persistent/pb/address-book.d.ts src/peer-store/persistent/pb/address-book.js",
"build:proto-types:proto-book": "pbts -o src/peer-store/persistent/pb/proto-book.d.ts src/peer-store/persistent/pb/proto-book.js",
"build:proto-types:peer-record": "pbts -o src/record/peer-record/peer-record.d.ts src/record/peer-record/peer-record.js",
"build:proto-types:envelope": "pbts -o src/record/envelope/envelope.d.ts src/record/envelope/envelope.js",
"test": "aegir test",
"test:ts": "aegir build --no-bundle && npm run test --prefix test/ts-use",
"test:node": "aegir test -t node -f \"./test/**/*.{node,spec}.js\"",
Expand Down Expand Up @@ -48,8 +62,7 @@
"homepage": "https://libp2p.io",
"license": "MIT",
"engines": {
"node": ">=12.0.0",
"npm": ">=6.0.0"
"node": ">=14.0.0"
},
"browser": {
"@motrix/nat-api": false
Expand Down Expand Up @@ -82,19 +95,19 @@
"it-filter": "^1.0.1",
"it-first": "^1.0.4",
"it-handshake": "^1.0.2",
"it-length-prefixed": "^3.1.0",
"it-length-prefixed": "^4.0.0",
"it-map": "^1.0.4",
"it-merge": "1.0.0",
"it-pipe": "^1.1.0",
"it-protocol-buffers": "^0.2.0",
"it-take": "1.0.0",
"libp2p-crypto": "^0.19.0",
"libp2p-interfaces": "^0.9.0",
"libp2p-utils": "libp2p/js-libp2p-utils#feat/add-types",
"libp2p-utils": "^0.3.0",
"mafmt": "^8.0.0",
"merge-options": "^3.0.4",
"moving-average": "^1.0.0",
"multiaddr": "^8.1.0",
"multiaddr": "^8.1.2",
"multicodec": "^3.0.1",
"multihashing-async": "^2.1.2",
"multistream-select": "^1.0.0",
Expand All @@ -106,6 +119,7 @@
"p-settle": "^4.1.1",
"peer-id": "^0.14.2",
"private-ip": "^2.1.0",
"protobufjs": "^6.10.2",
"protons": "^2.0.0",
"retimer": "^3.0.0",
"sanitize-filename": "^1.6.3",
Expand All @@ -121,7 +135,7 @@
"@types/node-forge": "^0.9.7",
"@types/varint": "^6.0.0",
"abortable-iterator": "^3.0.0",
"aegir": "^32.1.0",
"aegir": "^33.0.0",
"chai-bytes": "^0.1.2",
"chai-string": "^1.5.0",
"delay": "^5.0.0",
Expand Down
6 changes: 3 additions & 3 deletions src/circuit/circuit/hop.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const { stop } = require('./stop')
const multicodec = require('./../multicodec')

/**
* @typedef {import('../../types').CircuitRequest} CircuitRequest
* @typedef {import('../protocol').ICircuitRelay} ICircuitRelay
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
* @typedef {import('../transport')} Transport
Expand All @@ -27,7 +27,7 @@ const multicodec = require('./../multicodec')
/**
* @typedef {Object} HopRequest
* @property {Connection} connection
* @property {CircuitRequest} request
* @property {ICircuitRelay} request
* @property {StreamHandler} streamHandler
* @property {Transport} circuit
*/
Expand Down Expand Up @@ -118,7 +118,7 @@ async function handleHop ({
*
* @param {object} options
* @param {Connection} options.connection - Connection to the relay
* @param {CircuitRequest} options.request
* @param {ICircuitRelay} options.request
* @returns {Promise<MuxedStream>}
*/
async function hop ({
Expand Down
6 changes: 3 additions & 3 deletions src/circuit/circuit/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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('../protocol').ICircuitRelay} ICircuitRelay
*/

/**
Expand All @@ -22,7 +22,7 @@ const { validateAddrs } = require('./utils')
* @private
* @param {Object} options
* @param {Connection} options.connection
* @param {CircuitRequest} options.request - The CircuitRelay protobuf request (unencoded)
* @param {ICircuitRelay} options.request - The CircuitRelay protobuf request (unencoded)
* @param {StreamHandler} options.streamHandler
* @returns {Promise<MuxedStream>|void} Resolves a duplex iterable
*/
Expand Down Expand Up @@ -53,7 +53,7 @@ module.exports.handleStop = function handleStop ({
* @private
* @param {Object} options
* @param {Connection} options.connection
* @param {CircuitRequest} options.request - The CircuitRelay protobuf request (unencoded)
* @param {ICircuitRelay} options.request - The CircuitRelay protobuf request (unencoded)
* @returns {Promise<MuxedStream|void>} Resolves a duplex iterable
*/
module.exports.stop = async function stop ({
Expand Down
13 changes: 6 additions & 7 deletions src/circuit/circuit/stream-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ 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')
const { CircuitRelay } = require('../protocol')

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

class StreamHandler {
Expand All @@ -35,12 +35,11 @@ class StreamHandler {
* Read and decode message
*
* @async
* @returns {Promise<CircuitRequest|undefined>}
*/
async read () {
const msg = await this.decoder.next()
if (msg.value) {
const value = CircuitPB.decode(msg.value.slice())
const value = CircuitRelay.decode(msg.value.slice())
log('read message type', value.type)
return value
}
Expand All @@ -53,13 +52,13 @@ class StreamHandler {
/**
* Encode and write array of buffers
*
* @param {CircuitRequest} msg - An unencoded CircuitRelay protobuf message
* @param {ICircuitRelay} msg - An unencoded CircuitRelay protobuf message
* @returns {void}
*/
write (msg) {
log('write message type %s', msg.type)
// @ts-ignore lp.encode expects type type 'Buffer | BufferList', not 'Uint8Array'
this.shake.write(lp.encode.single(CircuitPB.encode(msg)))
this.shake.write(lp.encode.single(CircuitRelay.encode(msg).finish()))
}

/**
Expand All @@ -73,7 +72,7 @@ class StreamHandler {
}

/**
* @param {CircuitRequest} msg - An unencoded CircuitRelay protobuf message
* @param {ICircuitRelay} msg - An unencoded CircuitRelay protobuf message
*/
end (msg) {
this.write(msg)
Expand Down
22 changes: 13 additions & 9 deletions src/circuit/circuit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const { CircuitRelay } = require('../protocol')

/**
* @typedef {import('./stream-handler')} StreamHandler
* @typedef {import('../../types').CircuitStatus} CircuitStatus
* @typedef {import('../protocol').ICircuitRelay} ICircuitRelay
*/

/**
* Write a response
*
* @param {StreamHandler} streamHandler
* @param {CircuitStatus} status
* @param {import('../protocol').CircuitRelay.Status} status
*/
function writeResponse (streamHandler, status) {
streamHandler.write({
Expand All @@ -24,14 +24,16 @@ function writeResponse (streamHandler, status) {
/**
* Validate incomming HOP/STOP message
*
* @param {*} msg - A CircuitRelay unencoded protobuf message
* @param {ICircuitRelay} msg - A CircuitRelay unencoded protobuf message
* @param {StreamHandler} streamHandler
*/
function validateAddrs (msg, streamHandler) {
try {
msg.dstPeer.addrs.forEach((/** @type {string} */ addr) => {
return multiaddr(addr)
})
if (msg.dstPeer && msg.dstPeer.addrs) {
msg.dstPeer.addrs.forEach((addr) => {
return multiaddr(addr)
})
}
} catch (err) {
writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
? CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID
Expand All @@ -40,9 +42,11 @@ function validateAddrs (msg, streamHandler) {
}

try {
msg.srcPeer.addrs.forEach((/** @type {string} */ addr) => {
return multiaddr(addr)
})
if (msg.srcPeer && msg.srcPeer.addrs) {
msg.srcPeer.addrs.forEach((addr) => {
return multiaddr(addr)
})
}
} catch (err) {
writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
? CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID
Expand Down
Loading

0 comments on commit bd10429

Please sign in to comment.