Skip to content

Commit

Permalink
fix: prefix encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
alanshaw committed Apr 26, 2023
1 parent 2f57f68 commit 928a72a
Show file tree
Hide file tree
Showing 7 changed files with 1,117 additions and 3,898 deletions.
8 changes: 4 additions & 4 deletions index.js
@@ -1,7 +1,7 @@
import { pipe } from 'it-pipe'
import * as lp from 'it-length-prefixed'
import { transform, consume } from 'streaming-iterables'
import { Message, WantType, Block, BlockPresence, BlockPresenceType } from './message.js'
import { Message, WantType, Block, BlockPresence, BlockPresenceType, Entry } from './message.js'

export const BITSWAP_PROTOCOL = '/ipfs/bitswap/1.2.0'
const PROCESS_MESSAGE_CONCURRENCY = 10
Expand All @@ -24,12 +24,12 @@ export class Miniswap {
try {
await pipe(
inStream,
lp.decode(),
lp.decode,
transform(PROCESS_MESSAGE_CONCURRENCY, async data => {
const message = Message.decode(data.subarray())
const outStream = await connection.newStream(BITSWAP_PROTOCOL)
const bs = this._blockstore
await pipe(processWantlist(bs, message.wantlist), lp.encode(), outStream)
await pipe(processWantlist(bs, message.wantlist), lp.encode, outStream)
outStream.close()
}),
consume
Expand All @@ -51,7 +51,7 @@ export class Miniswap {
function processWantlist (blockstore, wantlist) {
return pipe(
wantlist.entries.filter(entry => !entry.cancel),
transform(PROCESS_WANTLIST_CONCURRENCY, async entry => {
transform(PROCESS_WANTLIST_CONCURRENCY, async (/** @type {Entry} */ entry) => {
if (entry.wantType === WantType.Block) {
const raw = await blockstore.get(entry.cid)
if (raw) {
Expand Down
8 changes: 2 additions & 6 deletions message.js
@@ -1,5 +1,6 @@
import { CID } from 'multiformats/cid'
import * as gen from './gen/message.js'
import { encode as encodePrefix } from './prefix.js'

const MAX_PRIORITY = Math.pow(2, 31) - 1
const MAX_MESSAGE_SIZE = 4 * 1024 * 1024 // 4 MB
Expand Down Expand Up @@ -127,12 +128,7 @@ export class Block {
*/
constructor (prefixOrCid, data) {
if (prefixOrCid instanceof CID) {
prefixOrCid = new Uint8Array([
prefixOrCid.version,
prefixOrCid.code,
prefixOrCid.multihash.bytes[0],
prefixOrCid.multihash.bytes[1]
])
prefixOrCid = encodePrefix(prefixOrCid)
}

this.prefix = prefixOrCid
Expand Down

0 comments on commit 928a72a

Please sign in to comment.