Skip to content

Commit

Permalink
feat: allow use as a singleton
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
achingbrain committed Feb 2, 2024
1 parent 8dcf18e commit f29d6a6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/verified-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
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()
}

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 }
8 changes: 7 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 @@ -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')
})
})

0 comments on commit f29d6a6

Please sign in to comment.