mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-10-07 22:57:07 +00:00
* refactor: Functionize identity provider. * docs: Building a custom identity provider. * refactor: Functionize clock.
88 lines
2.1 KiB
JavaScript
88 lines
2.1 KiB
JavaScript
/**
|
|
* @namespace Storage-IPFS
|
|
* @memberof module:Storage
|
|
* @description
|
|
* IPFSBlockStorage uses IPFS to store data as raw blocks.
|
|
*/
|
|
import { CID } from 'multiformats/cid'
|
|
import { base58btc } from 'multiformats/bases/base58'
|
|
|
|
const defaultTimeout = 30000
|
|
|
|
/**
|
|
* Creates an instance of IPFSBlockStorage.
|
|
* @function
|
|
* @param {Object} params One or more parameters for configuring
|
|
* IPFSBlockStorage.
|
|
* @param {IPFS} params.ipfs An IPFS instance.
|
|
* @param {number} [params.timeout=defaultTimeout] A timeout in ms.
|
|
* @param {boolean} [params.pin=false] True, if the block should be pinned,
|
|
* false otherwise.
|
|
* @return {module:Storage.Storage-IPFS} An instance of IPFSBlockStorage.
|
|
* @memberof module:Storage
|
|
* @throw An instance of ipfs is required if params.ipfs is not specified.
|
|
* @instance
|
|
*/
|
|
const IPFSBlockStorage = async ({ ipfs, timeout, pin } = {}) => {
|
|
if (!ipfs) throw new Error('An instance of ipfs is required.')
|
|
|
|
timeout = timeout || defaultTimeout
|
|
|
|
/**
|
|
* Puts data to an IPFS block.
|
|
* @function
|
|
* @param {string} hash The hash of the block to put.
|
|
* @param {*} data The data to store in the IPFS block.
|
|
* @memberof module:Storage.Storage-IPFS
|
|
* @instance
|
|
*/
|
|
const put = async (hash, data) => {
|
|
const cid = CID.parse(hash, base58btc)
|
|
await ipfs.block.put(data, {
|
|
cid: cid.bytes,
|
|
version: cid.version,
|
|
format: 'dag-cbor',
|
|
mhtype: 'sha2-256',
|
|
pin,
|
|
timeout
|
|
})
|
|
}
|
|
|
|
const del = async (hash) => {}
|
|
|
|
/**
|
|
* Gets data from an IPFS block.
|
|
* @function
|
|
* @param {string} hash The hash of the block to get.
|
|
* @memberof module:Storage.Storage-IPFS
|
|
* @instance
|
|
*/
|
|
const get = async (hash) => {
|
|
const cid = CID.parse(hash, base58btc)
|
|
const block = await ipfs.block.get(cid, { timeout })
|
|
if (block) {
|
|
return block
|
|
}
|
|
}
|
|
|
|
const iterator = async function * () {}
|
|
|
|
const merge = async (other) => {}
|
|
|
|
const clear = async () => {}
|
|
|
|
const close = async () => {}
|
|
|
|
return {
|
|
put,
|
|
del,
|
|
get,
|
|
iterator,
|
|
merge,
|
|
clear,
|
|
close
|
|
}
|
|
}
|
|
|
|
export default IPFSBlockStorage
|