mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-07-04 11:32:30 +00:00
fix: Pin a block.
This commit is contained in:
parent
16686c6a82
commit
61f15fd556
@ -7,6 +7,7 @@
|
|||||||
import { CID } from 'multiformats/cid'
|
import { CID } from 'multiformats/cid'
|
||||||
import { base58btc } from 'multiformats/bases/base58'
|
import { base58btc } from 'multiformats/bases/base58'
|
||||||
import { TimeoutController } from 'timeout-abort-controller'
|
import { TimeoutController } from 'timeout-abort-controller'
|
||||||
|
import drain from 'it-drain'
|
||||||
|
|
||||||
const DefaultTimeout = 30000 // 30 seconds
|
const DefaultTimeout = 30000 // 30 seconds
|
||||||
|
|
||||||
@ -41,7 +42,7 @@ const IPFSBlockStorage = async ({ ipfs, pin, timeout } = {}) => {
|
|||||||
await ipfs.blockstore.put(cid, data, { signal })
|
await ipfs.blockstore.put(cid, data, { signal })
|
||||||
|
|
||||||
if (pin && !(await ipfs.pins.isPinned(cid))) {
|
if (pin && !(await ipfs.pins.isPinned(cid))) {
|
||||||
await ipfs.pins.add(cid)
|
await drain(ipfs.pins.add(cid))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,15 +26,29 @@ describe('IPFSBlockStorage', function () {
|
|||||||
|
|
||||||
it('gets a block', async () => {
|
it('gets a block', async () => {
|
||||||
const expected = 'hello world'
|
const expected = 'hello world'
|
||||||
const block = await Block.encode({ value: expected, codec, hasher })
|
const { cid, bytes } = await Block.encode({ value: expected, codec, hasher })
|
||||||
const cid = block.cid.toString(base58btc)
|
|
||||||
|
|
||||||
await storage.put(cid, 'hello world')
|
const hash = cid.toString(base58btc)
|
||||||
const actual = await storage.get(cid)
|
|
||||||
|
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)
|
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, block.bytes)
|
||||||
|
|
||||||
|
strictEqual(await ipfs.pins.isPinned(block.cid), true)
|
||||||
|
})
|
||||||
|
|
||||||
it('throws an error if a block does not exist', async () => {
|
it('throws an error if a block does not exist', async () => {
|
||||||
const value = 'i don\'t exist'
|
const value = 'i don\'t exist'
|
||||||
const block = await Block.encode({ value, codec, hasher })
|
const block = await Block.encode({ value, codec, hasher })
|
||||||
|
Loading…
x
Reference in New Issue
Block a user