mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-03-30 15:08:28 +00:00
refactor: Move start/stop OrbitDB convenience functions to own repo.
This commit is contained in:
parent
c844e54fe9
commit
eb5104ead6
@ -1,62 +0,0 @@
|
||||
import { identify } from '@libp2p/identify'
|
||||
import { webSockets } from '@libp2p/websockets'
|
||||
import { webRTC } from '@libp2p/webrtc'
|
||||
import { all } from '@libp2p/websockets/filters'
|
||||
import { noise } from '@chainsafe/libp2p-noise'
|
||||
import { yamux } from '@chainsafe/libp2p-yamux'
|
||||
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
|
||||
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2'
|
||||
|
||||
/**
|
||||
* A basic Libp2p configuration for Node.js nodes.
|
||||
*/
|
||||
export const DefaultLibp2pOptions = {
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0/ws']
|
||||
},
|
||||
transports: [
|
||||
webSockets({
|
||||
filter: all
|
||||
}),
|
||||
webRTC(),
|
||||
circuitRelayTransport({
|
||||
discoverRelays: 1
|
||||
})
|
||||
],
|
||||
connectionEncryption: [noise()],
|
||||
streamMuxers: [yamux()],
|
||||
connectionGater: {
|
||||
denyDialMultiaddr: () => false
|
||||
},
|
||||
services: {
|
||||
identify: identify(),
|
||||
pubsub: gossipsub({ allowPublishToZeroPeers: true })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A basic Libp2p configuration for browser nodes.
|
||||
*/
|
||||
export const DefaultLibp2pBrowserOptions = {
|
||||
addresses: {
|
||||
listen: ['/webrtc']
|
||||
},
|
||||
transports: [
|
||||
webSockets({
|
||||
filter: all
|
||||
}),
|
||||
webRTC(),
|
||||
circuitRelayTransport({
|
||||
discoverRelays: 1
|
||||
})
|
||||
],
|
||||
connectionEncryption: [noise()],
|
||||
streamMuxers: [yamux()],
|
||||
connectionGater: {
|
||||
denyDialMultiaddr: () => false
|
||||
},
|
||||
services: {
|
||||
identify: identify(),
|
||||
pubsub: gossipsub({ allowPublishToZeroPeers: true })
|
||||
}
|
||||
}
|
@ -41,10 +41,3 @@ export {
|
||||
MemoryStorage,
|
||||
ComposedStorage
|
||||
} from './storage/index.js'
|
||||
|
||||
export {
|
||||
DefaultLibp2pOptions,
|
||||
DefaultLibp2pBrowserOptions
|
||||
} from './config/libp2p/index.js'
|
||||
|
||||
export { startOrbitDB } from './utils/index.js'
|
||||
|
@ -1,6 +1,5 @@
|
||||
import createId from './create-id.js'
|
||||
import startOrbitDB from './start.js'
|
||||
|
||||
export {
|
||||
createId,
|
||||
startOrbitDB
|
||||
createId
|
||||
}
|
||||
|
@ -1,30 +0,0 @@
|
||||
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 })
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
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')
|
||||
})
|
||||
})
|
@ -3,12 +3,70 @@ import { bitswap } from '@helia/block-brokers'
|
||||
import { createLibp2p } from 'libp2p'
|
||||
import { MemoryBlockstore } from 'blockstore-core'
|
||||
import { LevelBlockstore } from 'blockstore-level'
|
||||
import { DefaultLibp2pOptions, DefaultLibp2pBrowserOptions } from '../../src/index.js'
|
||||
import { identify } from '@libp2p/identify'
|
||||
import { webSockets } from '@libp2p/websockets'
|
||||
import { webRTC } from '@libp2p/webrtc'
|
||||
import { all } from '@libp2p/websockets/filters'
|
||||
import { noise } from '@chainsafe/libp2p-noise'
|
||||
import { yamux } from '@chainsafe/libp2p-yamux'
|
||||
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
|
||||
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2'
|
||||
|
||||
const isBrowser = () => typeof window !== 'undefined'
|
||||
|
||||
const Libp2pOptions = {
|
||||
addresses: {
|
||||
listen: ['/ip4/0.0.0.0/tcp/0/ws']
|
||||
},
|
||||
transports: [
|
||||
webSockets({
|
||||
filter: all
|
||||
}),
|
||||
webRTC(),
|
||||
circuitRelayTransport({
|
||||
discoverRelays: 1
|
||||
})
|
||||
],
|
||||
connectionEncryption: [noise()],
|
||||
streamMuxers: [yamux()],
|
||||
connectionGater: {
|
||||
denyDialMultiaddr: () => false
|
||||
},
|
||||
services: {
|
||||
identify: identify(),
|
||||
pubsub: gossipsub({ allowPublishToZeroPeers: true })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A basic Libp2p configuration for browser nodes.
|
||||
*/
|
||||
const Libp2pBrowserOptions = {
|
||||
addresses: {
|
||||
listen: ['/webrtc']
|
||||
},
|
||||
transports: [
|
||||
webSockets({
|
||||
filter: all
|
||||
}),
|
||||
webRTC(),
|
||||
circuitRelayTransport({
|
||||
discoverRelays: 1
|
||||
})
|
||||
],
|
||||
connectionEncryption: [noise()],
|
||||
streamMuxers: [yamux()],
|
||||
connectionGater: {
|
||||
denyDialMultiaddr: () => false
|
||||
},
|
||||
services: {
|
||||
identify: identify(),
|
||||
pubsub: gossipsub({ allowPublishToZeroPeers: true })
|
||||
}
|
||||
}
|
||||
|
||||
export default async ({ directory } = {}) => {
|
||||
const options = isBrowser() ? DefaultLibp2pBrowserOptions : DefaultLibp2pOptions
|
||||
const options = isBrowser() ? Libp2pBrowserOptions : Libp2pOptions
|
||||
|
||||
const libp2p = await createLibp2p({ ...options })
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user