Merge pull request #1165 from orbitdb/fix/pin-blocks

fix: Pin a block.
This commit is contained in:
Hayden Young 2024-03-05 21:13:15 +00:00 committed by GitHub
commit e02a92ffc5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View File

@ -7,6 +7,7 @@
import { CID } from 'multiformats/cid'
import { base58btc } from 'multiformats/bases/base58'
import { TimeoutController } from 'timeout-abort-controller'
import drain from 'it-drain'
const DefaultTimeout = 30000 // 30 seconds
@ -41,7 +42,7 @@ const IPFSBlockStorage = async ({ ipfs, pin, timeout } = {}) => {
await ipfs.blockstore.put(cid, data, { signal })
if (pin && !(await ipfs.pins.isPinned(cid))) {
await ipfs.pins.add(cid)
await drain(ipfs.pins.add(cid))
}
}

View File

@ -25,14 +25,28 @@ describe('IPFSBlockStorage', function () {
})
it('gets a block', async () => {
const expected = 'hello world'
const { cid, bytes } = await Block.encode({ value: expected, codec, hasher })
const hash = cid.toString(base58btc)
await storage.put(hash, bytes)
const data = await storage.get(hash)
const block = await Block.decode({ bytes: data, codec, hasher })
const actual = block.value
strictEqual(actual, expected)
})
it('checks that a block is pinned', async () => {
const expected = 'hello world'
const block = await Block.encode({ value: expected, codec, hasher })
const cid = block.cid.toString(base58btc)
await storage.put(cid, 'hello world')
const actual = await storage.get(cid)
await storage.put(cid, block.bytes)
strictEqual(actual, expected)
strictEqual(await ipfs.pins.isPinned(block.cid), true)
})
it('throws an error if a block does not exist', async () => {