Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow use as a singleton #413

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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)
}

Check warning on line 14 in packages/verified-fetch/src/singleton.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/src/singleton.ts#L9-L14

Added lines #L9 - L14 were not covered by tests
singleton.start = async function () {
await singleton._impl?.stop()
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
}

Check warning on line 17 in packages/verified-fetch/src/singleton.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/src/singleton.ts#L16-L17

Added lines #L16 - L17 were not covered by tests
singleton.stop = async function () {
await singleton._impl?.stop()
}

Check warning on line 20 in packages/verified-fetch/src/singleton.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/src/singleton.ts#L19-L20

Added lines #L19 - L20 were not covered by tests
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')
})
})
Loading