diff --git a/src/index.js b/src/index.js index c19ab66..da4d111 100644 --- a/src/index.js +++ b/src/index.js @@ -46,3 +46,5 @@ export { DefaultLibp2pOptions, DefaultLibp2pBrowserOptions } from './config/libp2p/index.js' + +export { startOrbitDB } from './utils/index.js' diff --git a/src/utils/index.js b/src/utils/index.js index a6ac16f..67737d6 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -1,5 +1,6 @@ import createId from './create-id.js' - +import startOrbitDB from './start.js' export { - createId + createId, + startOrbitDB } diff --git a/src/utils/start.js b/src/utils/start.js new file mode 100644 index 0000000..948d22c --- /dev/null +++ b/src/utils/start.js @@ -0,0 +1,30 @@ +import { createHelia } from 'helia' +import { createLibp2p } from 'libp2p' +import { LevelBlockstore } from 'blockstore-level' +import { bitswap } from '@helia/block-brokers' +import createOrbitDB from '../orbitdb.js' +import { DefaultLibp2pOptions, DefaultLibp2pBrowserOptions } from '../config/libp2p/index.js' + +const isBrowser = () => typeof window !== 'undefined' + +/** + * Start a new OrbitDB peer with a preconfigured Helia instance. + * @function startOrbitDB + * @param {Object} params One or more parameters for configuring OrbitDB. + * @param {IPFS} params.ipfs An IPFS instance. + * @param {module:Identity|Object} [params.identity] An identity instance or an object containing an Identity Provider instance and any additional params required to create the identity using the specified provider. + * @param {Function} [params.identity.provider] An initialized identity provider. + * @param {module:Identities} [params.identities] An Identities system instance. + * @param {string} [params.directory] A location for storing OrbitDB data. + * @return {module:OrbitDB~OrbitDB} An instance of OrbitDB. + * @throws "IPFS instance is required argument" if no IPFS instance is provided. + * @instance + */ +export default async ({ id, identity, identities, directory } = {}) => { + const options = isBrowser() ? DefaultLibp2pBrowserOptions : DefaultLibp2pOptions + const libp2p = await createLibp2p({ ...options }) + directory = directory || '.' + const blockstore = new LevelBlockstore(`${directory}/ipfs/blocks`) + const ipfs = await createHelia({ libp2p, blockstore, blockBrokers: [bitswap()] }) + return createOrbitDB({ ipfs, id, identity, identities, directory }) +} diff --git a/test/orbitdb-start.test.js b/test/orbitdb-start.test.js new file mode 100644 index 0000000..6abb1be --- /dev/null +++ b/test/orbitdb-start.test.js @@ -0,0 +1,17 @@ +import { startOrbitDB } from '../src/index.js' +import { deepStrictEqual } from 'assert' +import { rimraf } from 'rimraf' + +describe('Starting OrbitDB', function () { + it('starts OrbitDB with a preconfigured Helia instance', async () => { + const orbitdb = await startOrbitDB({ directory: './ipfs' }) + const db1 = await orbitdb.open('db1') + await db1.add('hello world!') + + deepStrictEqual((await db1.all()).map(e => e.value), ['hello world!']) + await orbitdb.stop() + await orbitdb.ipfs.stop() + await rimraf('./orbitdb') + await rimraf('./ipfs') + }) +})