Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/verified-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,15 @@ export interface VerifiedFetchInit extends RequestInit, ProgressOptions<BubbledP
/**
* Create and return a Helia node
*/
export async function createVerifiedFetch (init: Helia | CreateVerifiedFetchWithOptions): Promise<VerifiedFetch> {
export async function createVerifiedFetch (init?: Helia | CreateVerifiedFetchWithOptions): Promise<VerifiedFetch> {
if (!isHelia(init)) {
init = await createHeliaHTTP({
blockBrokers: [
trustlessGateway({
gateways: init.gateways
gateways: init?.gateways
})
],
routers: init.routers?.map((routerUrl) => delegatedHTTPRouting(routerUrl))
routers: (init?.routers ?? ['https://delegated-ipfs.dev']).map((routerUrl) => delegatedHTTPRouting(routerUrl))
})
}

Expand All @@ -300,6 +300,8 @@ export async function createVerifiedFetch (init: Helia | CreateVerifiedFetchWith
return verifiedFetch
}

export { verifiedFetch } from './singleton.js'

function isHelia (obj: any): obj is Helia {
// test for the presence of known Helia properties, return a boolean value
return obj?.blockstore != null &&
Expand Down
23 changes: 23 additions & 0 deletions packages/verified-fetch/src/singleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createVerifiedFetch } from './index.js'
import type { Resource, VerifiedFetch, VerifiedFetchInit } from './index.js'

interface VerifiedFetchSingleton extends VerifiedFetch {
_impl?: VerifiedFetch
}

const singleton: VerifiedFetchSingleton = async function verifiedFetch (resource: Resource, options?: VerifiedFetchInit): Promise<Response> {
if (singleton._impl == null) {
singleton._impl = await createVerifiedFetch()
}

return singleton._impl(resource, options)
}
singleton.start = async function () {
await singleton._impl?.start()
}
singleton.stop = async function () {
await singleton._impl?.stop()
}
const verifiedFetchSingleton: VerifiedFetch = singleton

export { verifiedFetchSingleton as verifiedFetch }
15 changes: 14 additions & 1 deletion packages/verified-fetch/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { createHeliaHTTP } from '@helia/http'
import { expect } from 'aegir/chai'
import { createHelia } from 'helia'
import { createVerifiedFetch } from '../src/index.js'
import { createVerifiedFetch, verifiedFetch } from '../src/index.js'

describe('createVerifiedFetch', () => {
it('can be constructed with a HeliaHttp instance', async () => {
Expand Down Expand Up @@ -38,4 +38,17 @@ describe('createVerifiedFetch', () => {
expect(verifiedFetch).to.be.ok()
await verifiedFetch.stop()
})

it('can be constructed with no options', async () => {
const verifiedFetch = await createVerifiedFetch()

expect(verifiedFetch).to.be.ok()
await verifiedFetch.stop()
})

it('can be used as a singleton', () => {
expect(verifiedFetch).to.be.a('function')
expect(verifiedFetch.stop).to.be.a('function')
expect(verifiedFetch.start).to.be.a('function')
})
})