|
| 1 | +/* eslint-env mocha */ |
| 2 | + |
| 3 | +import { car } from '@helia/car' |
| 4 | +import { dagCbor } from '@helia/dag-cbor' |
| 5 | +import { dagJson } from '@helia/dag-json' |
| 6 | +import { mfs } from '@helia/mfs' |
| 7 | +import { strings } from '@helia/strings' |
| 8 | +import { unixfs } from '@helia/unixfs' |
| 9 | +import { peerIdFromString } from '@libp2p/peer-id' |
| 10 | +import { createScalableCuckooFilter } from '@libp2p/utils/filters' |
| 11 | +import { expect } from 'aegir/chai' |
| 12 | +import toBuffer from 'it-to-buffer' |
| 13 | +import { multiaddr } from 'kubo-rpc-client' |
| 14 | +import { CID } from 'multiformats/cid' |
| 15 | +import { createHeliaNode } from './fixtures/create-helia.js' |
| 16 | +import { createKuboNode } from './fixtures/create-kubo.js' |
| 17 | +import type { PeerId } from '@libp2p/interface' |
| 18 | +import type { Helia } from 'helia' |
| 19 | +import type { FileCandidate } from 'ipfs-unixfs-importer' |
| 20 | +import type { KuboInfo, KuboNode } from 'ipfsd-ctl' |
| 21 | + |
| 22 | +describe('providers', () => { |
| 23 | + let helia: Helia |
| 24 | + let kubo: KuboNode |
| 25 | + let cid: CID |
| 26 | + let kuboInfo: KuboInfo |
| 27 | + let input: Uint8Array[] |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + // helia and kubo are not connected together before the test |
| 31 | + helia = await createHeliaNode() |
| 32 | + kubo = await createKuboNode() |
| 33 | + |
| 34 | + const chunkSize = 1024 * 1024 |
| 35 | + const size = chunkSize * 10 |
| 36 | + input = [] |
| 37 | + |
| 38 | + const candidate: FileCandidate = { |
| 39 | + content: (async function * () { |
| 40 | + for (let i = 0; i < size; i += chunkSize) { |
| 41 | + const buf = new Uint8Array(chunkSize) |
| 42 | + input.push(buf) |
| 43 | + |
| 44 | + yield buf |
| 45 | + } |
| 46 | + }()) |
| 47 | + } |
| 48 | + |
| 49 | + const importResult = await kubo.api.add(candidate.content) |
| 50 | + cid = CID.parse(importResult.cid.toString()) |
| 51 | + kuboInfo = await kubo.info() |
| 52 | + }) |
| 53 | + |
| 54 | + afterEach(async () => { |
| 55 | + if (helia != null) { |
| 56 | + await helia.stop() |
| 57 | + } |
| 58 | + |
| 59 | + if (kubo != null) { |
| 60 | + await kubo.stop() |
| 61 | + } |
| 62 | + }) |
| 63 | + |
| 64 | + it('should fail to fetch without using a provider', async () => { |
| 65 | + await expect(helia.blockstore.get(cid, { |
| 66 | + signal: AbortSignal.timeout(100) |
| 67 | + })).to.eventually.be.rejected() |
| 68 | + .with.nested.property('errors[0].name', 'AbortError') |
| 69 | + }) |
| 70 | + |
| 71 | + it('should fetch raw using a provider', async () => { |
| 72 | + let sender: PeerId | undefined |
| 73 | + |
| 74 | + const buf = await helia.blockstore.get(cid, { |
| 75 | + providers: [ |
| 76 | + kuboInfo.multiaddrs.map(ma => multiaddr(ma)) |
| 77 | + ], |
| 78 | + onProgress (evt) { |
| 79 | + // @ts-expect-error cannot derive config-based progress event types |
| 80 | + if (evt.type === 'bitswap:want-block:received') { |
| 81 | + // @ts-expect-error cannot derive config-based progress event types |
| 82 | + sender = evt.detail.sender |
| 83 | + } |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + expect(buf).to.have.lengthOf(1930) |
| 88 | + expect(sender).to.deep.equal(peerIdFromString(kuboInfo.peerId?.toString() ?? '')) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should fetch dag-cbor using a provider', async () => { |
| 92 | + let sender: PeerId | undefined |
| 93 | + const obj = { hello: 'world' } |
| 94 | + const cid = await kubo.api.dag.put(obj, { |
| 95 | + storeCodec: 'dag-cbor' |
| 96 | + }) |
| 97 | + |
| 98 | + const d = dagCbor(helia) |
| 99 | + |
| 100 | + await expect(d.get(cid, { |
| 101 | + providers: [ |
| 102 | + kuboInfo.multiaddrs.map(ma => multiaddr(ma)) |
| 103 | + ], |
| 104 | + onProgress (evt) { |
| 105 | + // @ts-expect-error cannot derive config-based progress event types |
| 106 | + if (evt.type === 'bitswap:want-block:received') { |
| 107 | + // @ts-expect-error cannot derive config-based progress event types |
| 108 | + sender = evt.detail.sender |
| 109 | + } |
| 110 | + } |
| 111 | + })).to.eventually.deep.equal(obj) |
| 112 | + expect(sender).to.deep.equal(peerIdFromString(kuboInfo.peerId?.toString() ?? '')) |
| 113 | + }) |
| 114 | + |
| 115 | + it('should fetch dag-json using a provider', async () => { |
| 116 | + let sender: PeerId | undefined |
| 117 | + const obj = { hello: 'world' } |
| 118 | + const cid = await kubo.api.dag.put(obj, { |
| 119 | + storeCodec: 'dag-json' |
| 120 | + }) |
| 121 | + |
| 122 | + const d = dagJson(helia) |
| 123 | + |
| 124 | + await expect(d.get(cid, { |
| 125 | + providers: [ |
| 126 | + kuboInfo.multiaddrs.map(ma => multiaddr(ma)) |
| 127 | + ], |
| 128 | + onProgress (evt) { |
| 129 | + // @ts-expect-error cannot derive config-based progress event types |
| 130 | + if (evt.type === 'bitswap:want-block:received') { |
| 131 | + // @ts-expect-error cannot derive config-based progress event types |
| 132 | + sender = evt.detail.sender |
| 133 | + } |
| 134 | + } |
| 135 | + })).to.eventually.deep.equal(obj) |
| 136 | + expect(sender).to.deep.equal(peerIdFromString(kuboInfo.peerId?.toString() ?? '')) |
| 137 | + }) |
| 138 | + |
| 139 | + it('should fetch string using a provider', async () => { |
| 140 | + let sender: PeerId | undefined |
| 141 | + const obj = 'hello world' |
| 142 | + const cid = await kubo.api.dag.put(obj, { |
| 143 | + storeCodec: 'dag-json' |
| 144 | + }) |
| 145 | + |
| 146 | + const s = strings(helia) |
| 147 | + |
| 148 | + await expect(s.get(cid, { |
| 149 | + providers: [ |
| 150 | + kuboInfo.multiaddrs.map(ma => multiaddr(ma)) |
| 151 | + ], |
| 152 | + onProgress (evt) { |
| 153 | + // @ts-expect-error cannot derive config-based progress event types |
| 154 | + if (evt.type === 'bitswap:want-block:received') { |
| 155 | + // @ts-expect-error cannot derive config-based progress event types |
| 156 | + sender = evt.detail.sender |
| 157 | + } |
| 158 | + } |
| 159 | + })).to.eventually.equal(JSON.stringify(obj)) |
| 160 | + expect(sender).to.deep.equal(peerIdFromString(kuboInfo.peerId?.toString() ?? '')) |
| 161 | + }) |
| 162 | + |
| 163 | + it('should fetch via unixfs using a provider', async () => { |
| 164 | + let sender: PeerId | undefined |
| 165 | + const fs = unixfs(helia) |
| 166 | + |
| 167 | + const bytes = await toBuffer(fs.cat(cid, { |
| 168 | + providers: [ |
| 169 | + kuboInfo.multiaddrs.map(ma => multiaddr(ma)) |
| 170 | + ], |
| 171 | + onProgress (evt) { |
| 172 | + // @ts-expect-error cannot derive config-based progress event types |
| 173 | + if (evt.type === 'bitswap:want-block:received') { |
| 174 | + // @ts-expect-error cannot derive config-based progress event types |
| 175 | + sender = evt.detail.sender |
| 176 | + } |
| 177 | + } |
| 178 | + })) |
| 179 | + |
| 180 | + expect(bytes).to.equalBytes(toBuffer(input)) |
| 181 | + expect(sender).to.deep.equal(peerIdFromString(kuboInfo.peerId?.toString() ?? '')) |
| 182 | + }) |
| 183 | + |
| 184 | + it('should fetch via mfs using a provider', async () => { |
| 185 | + let sender: PeerId | undefined |
| 186 | + const fs = mfs(helia) |
| 187 | + |
| 188 | + await fs.cp(cid, '/file.txt', { |
| 189 | + providers: [ |
| 190 | + kuboInfo.multiaddrs.map(ma => multiaddr(ma)) |
| 191 | + ], |
| 192 | + onProgress (evt) { |
| 193 | + // @ts-expect-error cannot derive config-based progress event types |
| 194 | + if (evt.type === 'bitswap:want-block:received') { |
| 195 | + // @ts-expect-error cannot derive config-based progress event types |
| 196 | + sender = evt.detail.sender |
| 197 | + } |
| 198 | + } |
| 199 | + }) |
| 200 | + |
| 201 | + const bytes = await toBuffer(fs.cat('/file.txt')) |
| 202 | + |
| 203 | + expect(bytes).to.equalBytes(toBuffer(input)) |
| 204 | + expect(sender).to.deep.equal(peerIdFromString(kuboInfo.peerId?.toString() ?? '')) |
| 205 | + }) |
| 206 | + |
| 207 | + it('should fetch via car using a provider', async () => { |
| 208 | + let sender: PeerId | undefined |
| 209 | + const c = car(helia) |
| 210 | + |
| 211 | + expect(await toBuffer( |
| 212 | + c.stream(cid, { |
| 213 | + providers: [ |
| 214 | + kuboInfo.multiaddrs.map(ma => multiaddr(ma)) |
| 215 | + ], |
| 216 | + blockFilter: createScalableCuckooFilter(10), |
| 217 | + onProgress (evt) { |
| 218 | + // @ts-expect-error cannot derive config-based progress event types |
| 219 | + if (evt.type === 'bitswap:want-block:received') { |
| 220 | + // @ts-expect-error cannot derive config-based progress event types |
| 221 | + sender = evt.detail.sender |
| 222 | + } |
| 223 | + } |
| 224 | + })) |
| 225 | + ).to.equalBytes(await toBuffer( |
| 226 | + kubo.api.dag.export(cid) |
| 227 | + )) |
| 228 | + expect(sender).to.deep.equal(peerIdFromString(kuboInfo.peerId?.toString() ?? '')) |
| 229 | + }) |
| 230 | +}) |
0 commit comments