From f29d6a690702e14c0d6c4040a84961b50eb1e14e Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 2 Feb 2024 16:53:08 +0100 Subject: [PATCH] feat: allow use as a singleton This might make things even easier to get started? ```TypeScript import { verifiedFetch } from '@helia/verified-fetch' const response = await verifiedFetch('ipfs://Qmfoo') console.info(await response.json()) ``` It's not in this PR but maybe we'd want to allow some config: ```TypeScript import { verifiedFetch } from '@helia/verified-fetch' const response = await verifiedFetch('ipfs://Qmfoo', { gateways: ['https://...'] }) console.info(await response.json()) ``` Calling this twice with different gateways might result in an error. ```TypeScript import { verifiedFetch } from '@helia/verified-fetch' const response1 = await verifiedFetch('ipfs://Qmfoo', { gateways: ['https://foo...'] }) const response2 = await verifiedFetch('ipfs://Qmfoo', { gateways: ['https://bar...'] }) // Error: Only one set of gateways/routers may be specified, please use `createVerifiedFetch` for more flexibility ``` --- packages/verified-fetch/src/index.ts | 2 ++ packages/verified-fetch/src/singleton.ts | 23 ++++++++++++++++++++++ packages/verified-fetch/test/index.spec.ts | 8 +++++++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 packages/verified-fetch/src/singleton.ts diff --git a/packages/verified-fetch/src/index.ts b/packages/verified-fetch/src/index.ts index c23a77df5..5d7fd3751 100644 --- a/packages/verified-fetch/src/index.ts +++ b/packages/verified-fetch/src/index.ts @@ -300,6 +300,8 @@ export async function createVerifiedFetch (init?: Helia | CreateVerifiedFetchWit 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 && diff --git a/packages/verified-fetch/src/singleton.ts b/packages/verified-fetch/src/singleton.ts new file mode 100644 index 000000000..b5fb767be --- /dev/null +++ b/packages/verified-fetch/src/singleton.ts @@ -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 { + if (singleton._impl == null) { + singleton._impl = await createVerifiedFetch() + } + + return singleton._impl(resource, options) +} +singleton.start = async function () { + await singleton._impl?.stop() +} +singleton.stop = async function () { + await singleton._impl?.stop() +} +const verifiedFetchSingleton: VerifiedFetch = singleton + +export { verifiedFetchSingleton as verifiedFetch } diff --git a/packages/verified-fetch/test/index.spec.ts b/packages/verified-fetch/test/index.spec.ts index 9fcfc8978..e79d04371 100644 --- a/packages/verified-fetch/test/index.spec.ts +++ b/packages/verified-fetch/test/index.spec.ts @@ -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 () => { @@ -45,4 +45,10 @@ describe('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') + }) })