Skip to content

Commit de83324

Browse files
chore: replace protons with protobufjs (#319)
Replaces protons with the official protobufjs module. Co-authored-by: Vasco Santos <[email protected]>
1 parent 47fdb2a commit de83324

File tree

15 files changed

+1525
-176
lines changed

15 files changed

+1525
-176
lines changed

.aegir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = {
2727
}
2828
},
2929
build: {
30-
bundlesizeMax: '61kB',
30+
bundlesizeMax: '63KB',
3131
config: esbuild
3232
}
3333
}

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
"src"
3232
],
3333
"scripts": {
34-
"prepare": "aegir build --no-bundle",
34+
"build": "npm run build:proto && npm run build:proto-types && npm run build:bundle",
35+
"build:proto": "pbjs -t static-module -w commonjs --force-number --no-verify --no-delimited --no-create --no-beautify --no-defaults --lint eslint-disable -o src/types/message/message.js src/types/message/message.proto",
36+
"build:proto-types": "pbts -o src/types/message/message.d.ts src/types/message/message.js",
37+
"build:bundle": "aegir build",
3538
"test": "aegir test",
3639
"test:browser": "aegir test -t browser -t webworker",
3740
"test:node": "aegir test -t node",
@@ -63,13 +66,13 @@
6366
"devDependencies": {
6467
"@nodeutils/defaults-deep": "^1.1.0",
6568
"@types/debug": "^4.1.5",
66-
"aegir": "^31.0.4",
69+
"aegir": "^33.1.0",
6770
"assert": "^2.0.0",
6871
"benchmark": "^2.1.4",
6972
"delay": "^5.0.0",
7073
"ipfs-repo": "^9.0.0",
7174
"ipfs-utils": "^6.0.1",
72-
"iso-random-stream": "^1.1.1",
75+
"iso-random-stream": "^2.0.0",
7376
"it-all": "^1.0.5",
7477
"it-drain": "^1.0.4",
7578
"libp2p": "^0.30.9",
@@ -87,7 +90,7 @@
8790
"peer-id": "^0.14.3",
8891
"promisify-es6": "^1.0.3",
8992
"rimraf": "^3.0.2",
90-
"sinon": "^9.2.4",
93+
"sinon": "^10.0.0",
9194
"stats-lite": "^2.2.0",
9295
"uuid": "^8.3.2"
9396
},
@@ -99,7 +102,7 @@
99102
"debug": "^4.2.0",
100103
"ipfs-core-types": "^0.3.1",
101104
"ipld-block": "^0.11.0",
102-
"it-length-prefixed": "^3.1.0",
105+
"it-length-prefixed": "^5.0.2",
103106
"it-pipe": "^1.1.0",
104107
"just-debounce-it": "^1.1.0",
105108
"libp2p-interfaces": "^0.8.3",
@@ -108,7 +111,7 @@
108111
"multihashing-async": "^2.1.2",
109112
"native-abort-controller": "^1.0.3",
110113
"process": "^0.11.10",
111-
"protons": "^2.0.0",
114+
"protobufjs": "^6.10.2",
112115
"readable-stream": "^3.6.0",
113116
"streaming-iterables": "^5.0.4",
114117
"uint8arrays": "^2.1.3",

src/decision-engine/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ class DecisionEngine {
379379

380380
/**
381381
* @private
382-
* @param {import('../types/message/message.proto').WantType} wantType
382+
* @param {import('../types/message/message').Message.Wantlist.WantType} wantType
383383
* @param {number} blockSize
384384
*/
385385
_sendAsBlock (wantType, blockSize) {

src/decision-engine/ledger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Ledger {
4545
*
4646
* @param {CID} cid
4747
* @param {number} priority
48-
* @param {import('../types/message/message.proto').WantType} wantType
48+
* @param {import('../types/message/message').Message.Wantlist.WantType} wantType
4949
* @returns {void}
5050
*/
5151
wants (cid, priority, wantType) {

src/types/message/entry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = class BitswapMessageEntry {
66
/**
77
* @param {import('cids')} cid
88
* @param {number} priority
9-
* @param {import('./message.proto').WantType} wantType
9+
* @param {import('./message').Message.Wantlist.WantType} wantType
1010
* @param {boolean} [cancel]
1111
* @param {boolean} [sendDontHave]
1212
*/

src/types/message/index.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { getName } = require('multicodec')
77
const vd = require('varint-decoder')
88
const multihashing = require('multihashing-async')
99
const { isMapEqual } = require('../../utils')
10-
const { Message } = require('./message.proto')
10+
const { Message } = require('./message')
1111
const Entry = require('./entry')
1212

1313
class BitswapMessage {
@@ -22,7 +22,7 @@ class BitswapMessage {
2222
/** @type {Map<string, import('ipfs-core-types/src/block-service').Block>} */
2323
this.blocks = new Map()
2424

25-
/** @type {Map<string, import('./message.proto').BlockPresenceType>} */
25+
/** @type {Map<string, import('./message').Message.BlockPresenceType>} */
2626
this.blockPresences = new Map()
2727
this.pendingBytes = 0
2828
}
@@ -37,7 +37,7 @@ class BitswapMessage {
3737
*
3838
* @param {CID} cid
3939
* @param {number} priority
40-
* @param {import('./message.proto').WantType} [wantType]
40+
* @param {import('./message').Message.Wantlist.WantType | null} [wantType]
4141
* @param {boolean} [cancel]
4242
* @param {boolean} [sendDontHave]
4343
* @returns {void}
@@ -123,7 +123,6 @@ class BitswapMessage {
123123
* @returns {Uint8Array}
124124
*/
125125
serializeToBitswap100 () {
126-
/** @type {import('./message.proto').Message100} */
127126
const msg = {
128127
wantlist: {
129128
entries: Array.from(this.wantlist.values()).map((entry) => {
@@ -132,17 +131,14 @@ class BitswapMessage {
132131
priority: Number(entry.priority),
133132
cancel: Boolean(entry.cancel)
134133
}
135-
})
134+
}),
135+
full: this.full ? true : undefined
136136
},
137137
blocks: Array.from(this.blocks.values())
138138
.map((block) => block.data)
139139
}
140140

141-
if (this.full) {
142-
msg.wantlist.full = true
143-
}
144-
145-
return Message.encode(msg)
141+
return Message.encode(msg).finish()
146142
}
147143

148144
/**
@@ -152,7 +148,6 @@ class BitswapMessage {
152148
* @returns {Uint8Array}
153149
*/
154150
serializeToBitswap110 () {
155-
/** @type {import('./message.proto').Message110} */
156151
const msg = {
157152
wantlist: {
158153
entries: Array.from(this.wantlist.values()).map((entry) => {
@@ -163,36 +158,38 @@ class BitswapMessage {
163158
cancel: Boolean(entry.cancel),
164159
sendDontHave: Boolean(entry.sendDontHave)
165160
}
166-
})
161+
}),
162+
full: this.full ? true : undefined
167163
},
164+
/** @type {import('./message').Message.BlockPresence[]} */
168165
blockPresences: [],
166+
167+
/** @type {{ prefix: Uint8Array, data: Uint8Array }[]} */
169168
payload: [],
170169
pendingBytes: this.pendingBytes
171170
}
172171

173-
if (this.full) {
174-
msg.wantlist.full = true
175-
}
176-
177172
this.blocks.forEach((block) => {
178-
msg.payload.push({
179-
prefix: block.cid.prefix,
180-
data: block.data
181-
})
173+
msg.payload.push(
174+
new Message.Block({
175+
prefix: block.cid.prefix,
176+
data: block.data
177+
})
178+
)
182179
})
183180

184181
for (const [cidStr, bpType] of this.blockPresences) {
185-
msg.blockPresences.push({
182+
msg.blockPresences.push(new Message.BlockPresence({
186183
cid: new CID(cidStr).bytes,
187184
type: bpType
188-
})
185+
}))
189186
}
190187

191188
if (this.pendingBytes > 0) {
192189
msg.pendingBytes = this.pendingBytes
193190
}
194191

195-
return Message.encode(msg)
192+
return Message.encode(msg).finish()
196193
}
197194

198195
/**
@@ -222,27 +219,33 @@ class BitswapMessage {
222219
}
223220

224221
/**
225-
*
226222
* @param {Uint8Array} raw
227-
* @returns {Promise<BitswapMessage>}
228223
*/
229224
BitswapMessage.deserialize = async (raw) => {
230225
const decoded = Message.decode(raw)
231226

232227
const isFull = (decoded.wantlist && decoded.wantlist.full) || false
233228
const msg = new BitswapMessage(isFull)
234229

235-
if (decoded.wantlist) {
230+
if (decoded.wantlist && decoded.wantlist.entries) {
236231
decoded.wantlist.entries.forEach((entry) => {
232+
if (!entry.block) {
233+
return
234+
}
237235
// note: entry.block is the CID here
238236
const cid = new CID(entry.block)
239-
msg.addEntry(cid, entry.priority, entry.wantType, entry.cancel, entry.sendDontHave)
237+
msg.addEntry(cid, entry.priority || 0, entry.wantType, Boolean(entry.cancel), Boolean(entry.sendDontHave))
240238
})
241239
}
242240

243241
if (decoded.blockPresences) {
244242
decoded.blockPresences.forEach((blockPresence) => {
243+
if (!blockPresence.cid) {
244+
return
245+
}
246+
245247
const cid = new CID(blockPresence.cid)
248+
246249
if (blockPresence.type === BitswapMessage.BlockPresenceType.Have) {
247250
msg.addHave(cid)
248251
} else {
@@ -297,15 +300,11 @@ BitswapMessage.blockPresenceSize = (cid) => {
297300

298301
BitswapMessage.Entry = Entry
299302
BitswapMessage.WantType = {
300-
/** @type {import('./message.proto').WantBlock} */
301303
Block: Message.Wantlist.WantType.Block,
302-
/** @type {import('./message.proto').HaveBlock} */
303304
Have: Message.Wantlist.WantType.Have
304305
}
305306
BitswapMessage.BlockPresenceType = {
306-
/** @type {import('./message.proto').Have} */
307307
Have: Message.BlockPresenceType.Have,
308-
/** @type {import('./message.proto').DontHave} */
309308
DontHave: Message.BlockPresenceType.DontHave
310309
}
311310
module.exports = BitswapMessage

0 commit comments

Comments
 (0)