ipfs / helia

An implementation of IPFS in TypeScript
https://helia.io
Other
952 stars 105 forks source link

Pins not working since Helia update from 2.3.0 to Helia 4.2.1 #571

Closed Jollarvia closed 3 months ago

Jollarvia commented 4 months ago

I have a test in my software that broke the moment I moved from 2.3.0 to 4.2.1. So I created a mocha test with pure Helia to illustrate my problem.

describe("pin|unpin|isPinned", function() {
        this.timeout(15000)
        it("should pin successfully", async() => {
            const { createHelia } = await import('helia')
            const helia = await createHelia({})
            const heliaJson = json(helia)
            const obj = {x:1, y:2}
            const cid = await heliaJson.add(obj)
            var pre = await helia.pins.add(cid)
            for (var x in pre){
                console.log(JSON.stringify(pre))
            }

            var result = await helia.pins.isPinned(cid)  
            expect(result).to.be.true   //<--- breaks because its not true
            await helia.stop()
        })
    })

The object is retrievable from the datastore. The generator recovered from the act of pinning ('variable name pre') does not have the passed in cid and ispinned always returns false. I would expect it to be true as it was in 2.3.0.

acul71 commented 3 months ago

The issue here is with how you're using the helia.pins.add method, which has changed in Helia 3.x.x.

Before:

const cid = CID.parse('QmFoo')
await helia.pin.add(cid)

After (with an AsyncGenerator):

const cid = CID.parse('QmFoo')

for await (const pinnedCid of helia.pins.add(cid)) {
  // ...
}

In your test, you're using the helia.pins.add method with a single parameter (cid) and then trying to iterate over the result using a for...in loop. However, since add now returns an AsyncGenerator, you should use a for await...of loop instead.

change this code:

var pre = await helia.pins.add(cid)
for (var x in pre){
    console.log(JSON.stringify(pre))
}

to

// Use the AsyncGenerator to pin the CID
for await (const pinnedCid of helia.pins.add(cid)) {
   console.log(`Pinned CID: ${pinnedCid}`);
}

It should work now!

Jollarvia commented 3 months ago

Thanks @acul71 , that fixed it. Closing the issue.