Skip to content

Commit

Permalink
fix: helia init should extend base helia init (#464)
Browse files Browse the repository at this point in the history
Similar to `@helia/http`'s init interface extends that from `@helia/utils`,
reduce the duplciation in `helia`'s init interface.
  • Loading branch information
achingbrain authored Mar 12, 2024
1 parent c69913c commit a64e5de
Showing 1 changed file with 6 additions and 62 deletions.
68 changes: 6 additions & 62 deletions packages/helia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ import { libp2pDefaults } from './utils/libp2p-defaults.js'
import { createLibp2p } from './utils/libp2p.js'
import type { DefaultLibp2pServices } from './utils/libp2p-defaults.js'
import type { Helia } from '@helia/interface'
import type { BlockBroker } from '@helia/interface/blocks'
import type { ComponentLogger, Libp2p } from '@libp2p/interface'
import type { HeliaInit as HeliaClassInit } from '@helia/utils'
import type { Libp2p } from '@libp2p/interface'
import type { KeychainInit } from '@libp2p/keychain'
import type { Blockstore } from 'interface-blockstore'
import type { Datastore } from 'interface-datastore'
import type { Libp2pOptions } from 'libp2p'
import type { CID } from 'multiformats/cid'
import type { MultihashHasher } from 'multiformats/hashes/interface'

// re-export interface types so people don't have to depend on @helia/interface
// if they don't want to
Expand All @@ -55,7 +52,7 @@ export interface DAGWalker {
/**
* Options used to create a Helia node.
*/
export interface HeliaInit<T extends Libp2p = Libp2p> {
export interface HeliaInit<T extends Libp2p = Libp2p> extends HeliaClassInit {
/**
* A libp2p node is required to perform network operations. Either a
* preconfigured node or options to configure a node can be passed
Expand All @@ -70,64 +67,11 @@ export interface HeliaInit<T extends Libp2p = Libp2p> {
*/
libp2p?: T | Omit<Libp2pOptions, 'start'>

/**
* The blockstore is where blocks are stored
*/
blockstore?: Blockstore

/**
* The datastore is where data is stored
*/
datastore?: Datastore

/**
* By default sha256, sha512 and identity hashes are supported for
* bitswap operations. To bitswap blocks with CIDs using other hashes
* pass appropriate MultihashHashers here.
*/
hashers?: MultihashHasher[]

/**
* In order to pin CIDs that correspond to a DAG, it's necessary to know
* how to traverse that DAG. DAGWalkers take a block and yield any CIDs
* encoded within that block.
*/
dagWalkers?: DAGWalker[]

/**
* A list of strategies used to fetch blocks when they are not present in
* the local blockstore
*/
blockBrokers?: Array<(components: any) => BlockBroker>

/**
* Pass `false` to not start the Helia node
*/
start?: boolean

/**
* Garbage collection requires preventing blockstore writes during searches
* for unpinned blocks as DAGs are typically pinned after they've been
* imported - without locking this could lead to the deletion of blocks while
* they are being added to the blockstore.
*
* By default this lock is held on the main process (e.g. node cluster's
* primary process, the renderer thread in browsers) and other processes will
* contact the main process for access (worker processes in node cluster,
* webworkers in the browser).
*
* If Helia is being run wholly in a non-primary process, with no other process
* expected to access the blockstore (e.g. being run in the background in a
* webworker), pass true here to hold the gc lock in this process.
*/
holdGcLock?: boolean

/**
* An optional logging component to pass to libp2p. If not specified the
* default implementation from libp2p will be used.
*/
logger?: ComponentLogger

/**
* By default Helia stores the node's PeerId in an encrypted form in a
* libp2p keystore. These options control how that keystore is configured.
Expand All @@ -142,9 +86,9 @@ export interface HeliaLibp2p<T extends Libp2p = Libp2p<DefaultLibp2pServices>> e
/**
* Create and return a Helia node
*/
export async function createHelia <T extends Libp2p> (init: HeliaInit<T>): Promise<HeliaLibp2p<T>>
export async function createHelia (init?: HeliaInit<Libp2p<DefaultLibp2pServices>>): Promise<HeliaLibp2p<Libp2p<DefaultLibp2pServices>>>
export async function createHelia (init: HeliaInit = {}): Promise<HeliaLibp2p> {
export async function createHelia <T extends Libp2p> (init: Partial<HeliaInit<T>>): Promise<HeliaLibp2p<T>>
export async function createHelia (init?: Partial<HeliaInit<Libp2p<DefaultLibp2pServices>>>): Promise<HeliaLibp2p<Libp2p<DefaultLibp2pServices>>>
export async function createHelia (init: Partial<HeliaInit> = {}): Promise<HeliaLibp2p> {
const datastore = init.datastore ?? new MemoryDatastore()
const blockstore = init.blockstore ?? new MemoryBlockstore()

Expand Down

0 comments on commit a64e5de

Please sign in to comment.