Skip to content

Commit

Permalink
chore: replace protons with protobufjs (#319)
Browse files Browse the repository at this point in the history
Replaces protons with the official protobufjs module.

Co-authored-by: Vasco Santos <vasco.santos@moxy.studio>
  • Loading branch information
achingbrain and vasco-santos authored Apr 14, 2021
1 parent 47fdb2a commit de83324
Show file tree
Hide file tree
Showing 15 changed files with 1,525 additions and 176 deletions.
2 changes: 1 addition & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = {
}
},
build: {
bundlesizeMax: '61kB',
bundlesizeMax: '63KB',
config: esbuild
}
}
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
"src"
],
"scripts": {
"prepare": "aegir build --no-bundle",
"build": "npm run build:proto && npm run build:proto-types && npm run build:bundle",
"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",
"build:proto-types": "pbts -o src/types/message/message.d.ts src/types/message/message.js",
"build:bundle": "aegir build",
"test": "aegir test",
"test:browser": "aegir test -t browser -t webworker",
"test:node": "aegir test -t node",
Expand Down Expand Up @@ -63,13 +66,13 @@
"devDependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
"@types/debug": "^4.1.5",
"aegir": "^31.0.4",
"aegir": "^33.1.0",
"assert": "^2.0.0",
"benchmark": "^2.1.4",
"delay": "^5.0.0",
"ipfs-repo": "^9.0.0",
"ipfs-utils": "^6.0.1",
"iso-random-stream": "^1.1.1",
"iso-random-stream": "^2.0.0",
"it-all": "^1.0.5",
"it-drain": "^1.0.4",
"libp2p": "^0.30.9",
Expand All @@ -87,7 +90,7 @@
"peer-id": "^0.14.3",
"promisify-es6": "^1.0.3",
"rimraf": "^3.0.2",
"sinon": "^9.2.4",
"sinon": "^10.0.0",
"stats-lite": "^2.2.0",
"uuid": "^8.3.2"
},
Expand All @@ -99,7 +102,7 @@
"debug": "^4.2.0",
"ipfs-core-types": "^0.3.1",
"ipld-block": "^0.11.0",
"it-length-prefixed": "^3.1.0",
"it-length-prefixed": "^5.0.2",
"it-pipe": "^1.1.0",
"just-debounce-it": "^1.1.0",
"libp2p-interfaces": "^0.8.3",
Expand All @@ -108,7 +111,7 @@
"multihashing-async": "^2.1.2",
"native-abort-controller": "^1.0.3",
"process": "^0.11.10",
"protons": "^2.0.0",
"protobufjs": "^6.10.2",
"readable-stream": "^3.6.0",
"streaming-iterables": "^5.0.4",
"uint8arrays": "^2.1.3",
Expand Down
2 changes: 1 addition & 1 deletion src/decision-engine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ class DecisionEngine {

/**
* @private
* @param {import('../types/message/message.proto').WantType} wantType
* @param {import('../types/message/message').Message.Wantlist.WantType} wantType
* @param {number} blockSize
*/
_sendAsBlock (wantType, blockSize) {
Expand Down
2 changes: 1 addition & 1 deletion src/decision-engine/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Ledger {
*
* @param {CID} cid
* @param {number} priority
* @param {import('../types/message/message.proto').WantType} wantType
* @param {import('../types/message/message').Message.Wantlist.WantType} wantType
* @returns {void}
*/
wants (cid, priority, wantType) {
Expand Down
2 changes: 1 addition & 1 deletion src/types/message/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = class BitswapMessageEntry {
/**
* @param {import('cids')} cid
* @param {number} priority
* @param {import('./message.proto').WantType} wantType
* @param {import('./message').Message.Wantlist.WantType} wantType
* @param {boolean} [cancel]
* @param {boolean} [sendDontHave]
*/
Expand Down
61 changes: 30 additions & 31 deletions src/types/message/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { getName } = require('multicodec')
const vd = require('varint-decoder')
const multihashing = require('multihashing-async')
const { isMapEqual } = require('../../utils')
const { Message } = require('./message.proto')
const { Message } = require('./message')
const Entry = require('./entry')

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

/** @type {Map<string, import('./message.proto').BlockPresenceType>} */
/** @type {Map<string, import('./message').Message.BlockPresenceType>} */
this.blockPresences = new Map()
this.pendingBytes = 0
}
Expand All @@ -37,7 +37,7 @@ class BitswapMessage {
*
* @param {CID} cid
* @param {number} priority
* @param {import('./message.proto').WantType} [wantType]
* @param {import('./message').Message.Wantlist.WantType | null} [wantType]
* @param {boolean} [cancel]
* @param {boolean} [sendDontHave]
* @returns {void}
Expand Down Expand Up @@ -123,7 +123,6 @@ class BitswapMessage {
* @returns {Uint8Array}
*/
serializeToBitswap100 () {
/** @type {import('./message.proto').Message100} */
const msg = {
wantlist: {
entries: Array.from(this.wantlist.values()).map((entry) => {
Expand All @@ -132,17 +131,14 @@ class BitswapMessage {
priority: Number(entry.priority),
cancel: Boolean(entry.cancel)
}
})
}),
full: this.full ? true : undefined
},
blocks: Array.from(this.blocks.values())
.map((block) => block.data)
}

if (this.full) {
msg.wantlist.full = true
}

return Message.encode(msg)
return Message.encode(msg).finish()
}

/**
Expand All @@ -152,7 +148,6 @@ class BitswapMessage {
* @returns {Uint8Array}
*/
serializeToBitswap110 () {
/** @type {import('./message.proto').Message110} */
const msg = {
wantlist: {
entries: Array.from(this.wantlist.values()).map((entry) => {
Expand All @@ -163,36 +158,38 @@ class BitswapMessage {
cancel: Boolean(entry.cancel),
sendDontHave: Boolean(entry.sendDontHave)
}
})
}),
full: this.full ? true : undefined
},
/** @type {import('./message').Message.BlockPresence[]} */
blockPresences: [],

/** @type {{ prefix: Uint8Array, data: Uint8Array }[]} */
payload: [],
pendingBytes: this.pendingBytes
}

if (this.full) {
msg.wantlist.full = true
}

this.blocks.forEach((block) => {
msg.payload.push({
prefix: block.cid.prefix,
data: block.data
})
msg.payload.push(
new Message.Block({
prefix: block.cid.prefix,
data: block.data
})
)
})

for (const [cidStr, bpType] of this.blockPresences) {
msg.blockPresences.push({
msg.blockPresences.push(new Message.BlockPresence({
cid: new CID(cidStr).bytes,
type: bpType
})
}))
}

if (this.pendingBytes > 0) {
msg.pendingBytes = this.pendingBytes
}

return Message.encode(msg)
return Message.encode(msg).finish()
}

/**
Expand Down Expand Up @@ -222,27 +219,33 @@ class BitswapMessage {
}

/**
*
* @param {Uint8Array} raw
* @returns {Promise<BitswapMessage>}
*/
BitswapMessage.deserialize = async (raw) => {
const decoded = Message.decode(raw)

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

if (decoded.wantlist) {
if (decoded.wantlist && decoded.wantlist.entries) {
decoded.wantlist.entries.forEach((entry) => {
if (!entry.block) {
return
}
// note: entry.block is the CID here
const cid = new CID(entry.block)
msg.addEntry(cid, entry.priority, entry.wantType, entry.cancel, entry.sendDontHave)
msg.addEntry(cid, entry.priority || 0, entry.wantType, Boolean(entry.cancel), Boolean(entry.sendDontHave))
})
}

if (decoded.blockPresences) {
decoded.blockPresences.forEach((blockPresence) => {
if (!blockPresence.cid) {
return
}

const cid = new CID(blockPresence.cid)

if (blockPresence.type === BitswapMessage.BlockPresenceType.Have) {
msg.addHave(cid)
} else {
Expand Down Expand Up @@ -297,15 +300,11 @@ BitswapMessage.blockPresenceSize = (cid) => {

BitswapMessage.Entry = Entry
BitswapMessage.WantType = {
/** @type {import('./message.proto').WantBlock} */
Block: Message.Wantlist.WantType.Block,
/** @type {import('./message.proto').HaveBlock} */
Have: Message.Wantlist.WantType.Have
}
BitswapMessage.BlockPresenceType = {
/** @type {import('./message.proto').Have} */
Have: Message.BlockPresenceType.Have,
/** @type {import('./message.proto').DontHave} */
DontHave: Message.BlockPresenceType.DontHave
}
module.exports = BitswapMessage
Loading

0 comments on commit de83324

Please sign in to comment.