Skip to content

Commit bd10429

Browse files
committed
chore: replace protons with protobufjs
1 parent 8083aae commit bd10429

31 files changed

+3111
-259
lines changed

package.json

+20-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
"scripts": {
2121
"lint": "aegir lint",
2222
"build": "aegir build",
23+
"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",
24+
"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",
25+
"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",
26+
"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",
27+
"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",
28+
"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",
29+
"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",
30+
"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",
31+
"build:proto-types:circuit": "pbts -o src/circuit/protocol/index.d.ts src/circuit/protocol/index.js",
32+
"build:proto-types:plaintext": "pbts -o src/insecure/proto.d.ts src/insecure/proto.js",
33+
"build:proto-types:address-book": "pbts -o src/peer-store/persistent/pb/address-book.d.ts src/peer-store/persistent/pb/address-book.js",
34+
"build:proto-types:proto-book": "pbts -o src/peer-store/persistent/pb/proto-book.d.ts src/peer-store/persistent/pb/proto-book.js",
35+
"build:proto-types:peer-record": "pbts -o src/record/peer-record/peer-record.d.ts src/record/peer-record/peer-record.js",
36+
"build:proto-types:envelope": "pbts -o src/record/envelope/envelope.d.ts src/record/envelope/envelope.js",
2337
"test": "aegir test",
2438
"test:ts": "aegir build --no-bundle && npm run test --prefix test/ts-use",
2539
"test:node": "aegir test -t node -f \"./test/**/*.{node,spec}.js\"",
@@ -48,8 +62,7 @@
4862
"homepage": "https://libp2p.io",
4963
"license": "MIT",
5064
"engines": {
51-
"node": ">=12.0.0",
52-
"npm": ">=6.0.0"
65+
"node": ">=14.0.0"
5366
},
5467
"browser": {
5568
"@motrix/nat-api": false
@@ -82,19 +95,19 @@
8295
"it-filter": "^1.0.1",
8396
"it-first": "^1.0.4",
8497
"it-handshake": "^1.0.2",
85-
"it-length-prefixed": "^3.1.0",
98+
"it-length-prefixed": "^4.0.0",
8699
"it-map": "^1.0.4",
87100
"it-merge": "1.0.0",
88101
"it-pipe": "^1.1.0",
89102
"it-protocol-buffers": "^0.2.0",
90103
"it-take": "1.0.0",
91104
"libp2p-crypto": "^0.19.0",
92105
"libp2p-interfaces": "^0.9.0",
93-
"libp2p-utils": "libp2p/js-libp2p-utils#feat/add-types",
106+
"libp2p-utils": "^0.3.0",
94107
"mafmt": "^8.0.0",
95108
"merge-options": "^3.0.4",
96109
"moving-average": "^1.0.0",
97-
"multiaddr": "^8.1.0",
110+
"multiaddr": "^8.1.2",
98111
"multicodec": "^3.0.1",
99112
"multihashing-async": "^2.1.2",
100113
"multistream-select": "^1.0.0",
@@ -106,6 +119,7 @@
106119
"p-settle": "^4.1.1",
107120
"peer-id": "^0.14.2",
108121
"private-ip": "^2.1.0",
122+
"protobufjs": "^6.10.2",
109123
"protons": "^2.0.0",
110124
"retimer": "^3.0.0",
111125
"sanitize-filename": "^1.6.3",
@@ -121,7 +135,7 @@
121135
"@types/node-forge": "^0.9.7",
122136
"@types/varint": "^6.0.0",
123137
"abortable-iterator": "^3.0.0",
124-
"aegir": "^32.1.0",
138+
"aegir": "^33.0.0",
125139
"chai-bytes": "^0.1.2",
126140
"chai-string": "^1.5.0",
127141
"delay": "^5.0.0",

src/circuit/circuit/hop.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const { stop } = require('./stop')
1818
const multicodec = require('./../multicodec')
1919

2020
/**
21-
* @typedef {import('../../types').CircuitRequest} CircuitRequest
21+
* @typedef {import('../protocol').ICircuitRelay} ICircuitRelay
2222
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
2323
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
2424
* @typedef {import('../transport')} Transport
@@ -27,7 +27,7 @@ const multicodec = require('./../multicodec')
2727
/**
2828
* @typedef {Object} HopRequest
2929
* @property {Connection} connection
30-
* @property {CircuitRequest} request
30+
* @property {ICircuitRelay} request
3131
* @property {StreamHandler} streamHandler
3232
* @property {Transport} circuit
3333
*/
@@ -118,7 +118,7 @@ async function handleHop ({
118118
*
119119
* @param {object} options
120120
* @param {Connection} options.connection - Connection to the relay
121-
* @param {CircuitRequest} options.request
121+
* @param {ICircuitRelay} options.request
122122
* @returns {Promise<MuxedStream>}
123123
*/
124124
async function hop ({

src/circuit/circuit/stop.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { validateAddrs } = require('./utils')
1313
/**
1414
* @typedef {import('libp2p-interfaces/src/connection').Connection} Connection
1515
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
16-
* @typedef {import('../../types').CircuitRequest} CircuitRequest
16+
* @typedef {import('../protocol').ICircuitRelay} ICircuitRelay
1717
*/
1818

1919
/**
@@ -22,7 +22,7 @@ const { validateAddrs } = require('./utils')
2222
* @private
2323
* @param {Object} options
2424
* @param {Connection} options.connection
25-
* @param {CircuitRequest} options.request - The CircuitRelay protobuf request (unencoded)
25+
* @param {ICircuitRelay} options.request - The CircuitRelay protobuf request (unencoded)
2626
* @param {StreamHandler} options.streamHandler
2727
* @returns {Promise<MuxedStream>|void} Resolves a duplex iterable
2828
*/
@@ -53,7 +53,7 @@ module.exports.handleStop = function handleStop ({
5353
* @private
5454
* @param {Object} options
5555
* @param {Connection} options.connection
56-
* @param {CircuitRequest} options.request - The CircuitRelay protobuf request (unencoded)
56+
* @param {ICircuitRelay} options.request - The CircuitRelay protobuf request (unencoded)
5757
* @returns {Promise<MuxedStream|void>} Resolves a duplex iterable
5858
*/
5959
module.exports.stop = async function stop ({

src/circuit/circuit/stream-handler.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ const log = Object.assign(debug('libp2p:circuit:stream-handler'), {
88
const lp = require('it-length-prefixed')
99
// @ts-ignore it-handshake does not export types
1010
const handshake = require('it-handshake')
11-
const { CircuitRelay: CircuitPB } = require('../protocol')
11+
const { CircuitRelay } = require('../protocol')
1212

1313
/**
1414
* @typedef {import('libp2p-interfaces/src/stream-muxer/types').MuxedStream} MuxedStream
15-
* @typedef {import('../../types').CircuitRequest} CircuitRequest
15+
* @typedef {import('../protocol').ICircuitRelay} ICircuitRelay
1616
*/
1717

1818
class StreamHandler {
@@ -35,12 +35,11 @@ class StreamHandler {
3535
* Read and decode message
3636
*
3737
* @async
38-
* @returns {Promise<CircuitRequest|undefined>}
3938
*/
4039
async read () {
4140
const msg = await this.decoder.next()
4241
if (msg.value) {
43-
const value = CircuitPB.decode(msg.value.slice())
42+
const value = CircuitRelay.decode(msg.value.slice())
4443
log('read message type', value.type)
4544
return value
4645
}
@@ -53,13 +52,13 @@ class StreamHandler {
5352
/**
5453
* Encode and write array of buffers
5554
*
56-
* @param {CircuitRequest} msg - An unencoded CircuitRelay protobuf message
55+
* @param {ICircuitRelay} msg - An unencoded CircuitRelay protobuf message
5756
* @returns {void}
5857
*/
5958
write (msg) {
6059
log('write message type %s', msg.type)
6160
// @ts-ignore lp.encode expects type type 'Buffer | BufferList', not 'Uint8Array'
62-
this.shake.write(lp.encode.single(CircuitPB.encode(msg)))
61+
this.shake.write(lp.encode.single(CircuitRelay.encode(msg).finish()))
6362
}
6463

6564
/**
@@ -73,7 +72,7 @@ class StreamHandler {
7372
}
7473

7574
/**
76-
* @param {CircuitRequest} msg - An unencoded CircuitRelay protobuf message
75+
* @param {ICircuitRelay} msg - An unencoded CircuitRelay protobuf message
7776
*/
7877
end (msg) {
7978
this.write(msg)

src/circuit/circuit/utils.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const { CircuitRelay } = require('../protocol')
55

66
/**
77
* @typedef {import('./stream-handler')} StreamHandler
8-
* @typedef {import('../../types').CircuitStatus} CircuitStatus
8+
* @typedef {import('../protocol').ICircuitRelay} ICircuitRelay
99
*/
1010

1111
/**
1212
* Write a response
1313
*
1414
* @param {StreamHandler} streamHandler
15-
* @param {CircuitStatus} status
15+
* @param {import('../protocol').CircuitRelay.Status} status
1616
*/
1717
function writeResponse (streamHandler, status) {
1818
streamHandler.write({
@@ -24,14 +24,16 @@ function writeResponse (streamHandler, status) {
2424
/**
2525
* Validate incomming HOP/STOP message
2626
*
27-
* @param {*} msg - A CircuitRelay unencoded protobuf message
27+
* @param {ICircuitRelay} msg - A CircuitRelay unencoded protobuf message
2828
* @param {StreamHandler} streamHandler
2929
*/
3030
function validateAddrs (msg, streamHandler) {
3131
try {
32-
msg.dstPeer.addrs.forEach((/** @type {string} */ addr) => {
33-
return multiaddr(addr)
34-
})
32+
if (msg.dstPeer && msg.dstPeer.addrs) {
33+
msg.dstPeer.addrs.forEach((addr) => {
34+
return multiaddr(addr)
35+
})
36+
}
3537
} catch (err) {
3638
writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
3739
? CircuitRelay.Status.HOP_DST_MULTIADDR_INVALID
@@ -40,9 +42,11 @@ function validateAddrs (msg, streamHandler) {
4042
}
4143

4244
try {
43-
msg.srcPeer.addrs.forEach((/** @type {string} */ addr) => {
44-
return multiaddr(addr)
45-
})
45+
if (msg.srcPeer && msg.srcPeer.addrs) {
46+
msg.srcPeer.addrs.forEach((addr) => {
47+
return multiaddr(addr)
48+
})
49+
}
4650
} catch (err) {
4751
writeResponse(streamHandler, msg.type === CircuitRelay.Type.HOP
4852
? CircuitRelay.Status.HOP_SRC_MULTIADDR_INVALID

0 commit comments

Comments
 (0)