@helia/verified-fetch 0.0.0-5c0c39c → 0.0.0-6c88ee1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -1,57 +1,49 @@
1
1
  /**
2
2
  * @packageDocumentation
3
3
  *
4
- * `@helia/verified-fetch` is a library that provides a fetch-like API for fetching trustless content from IPFS and verifying it.
4
+ * `@helia/verified-fetch` provides a [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)-like API for retrieving content from the [IPFS](https://ipfs.tech/) network.
5
5
  *
6
- * This library should act as a replacement for the `fetch()` API for fetching content from IPFS, and will return a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) object that can be used in a similar manner to the `fetch()` API. This means browser and HTTP caching inside browser main threads, web-workers, and service workers, as well as other features of the `fetch()` API should work in a way familiar to developers.
6
+ * All content is retrieved in a [trustless manner](https://www.techopedia.com/definition/trustless), and the integrity of all bytes are verified by comparing hashes of the data.
7
7
  *
8
- * Exports a `createVerifiedFetch` function that returns a `fetch()` like API method {@link Helia} for fetching IPFS content.
8
+ * This is a marked improvement over `fetch` which offers no such protections and is vulnerable to all sorts of attacks like [Content Spoofing](https://owasp.org/www-community/attacks/Content_Spoofing), [DNS Hijacking](https://en.wikipedia.org/wiki/DNS_hijacking), etc.
9
+ *
10
+ * A `verifiedFetch` function is exported to get up and running quickly, and a `createVerifiedFetch` function is also available that allows customizing the underlying [Helia](https://helia.io/) node for complete control over how content is retrieved.
11
+ *
12
+ * Browser-cache-friendly [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects are returned which should be instantly familiar to web developers.
9
13
  *
10
14
  * You may use any supported resource argument to fetch content:
11
15
  *
12
- * - CID instance
16
+ * - [CID](https://multiformats.github.io/js-multiformats/classes/cid.CID.html) instance
13
17
  * - IPFS URL
14
18
  * - IPNS URL
15
19
  *
16
- * @example
20
+ * @example Getting started
17
21
  *
18
22
  * ```typescript
19
- * import { createVerifiedFetch } from '@helia/verified-fetch'
23
+ * import { verifiedFetch } from '@helia/verified-fetch'
20
24
  *
21
- * const fetch = await createVerifiedFetch({
22
- * gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
23
- *})
24
- *
25
- * const resp = await fetch('ipfs://bafy...')
25
+ * const resp = await verifiedFetch('ipfs://bafy...')
26
26
  *
27
27
  * const json = await resp.json()
28
28
  *```
29
29
  *
30
- *
31
30
  * @example Using a CID instance to fetch JSON
32
31
  *
33
32
  * ```typescript
34
- * import { createVerifiedFetch } from '@helia/verified-fetch'
33
+ * import { verifiedFetch } from '@helia/verified-fetch'
35
34
  * import { CID } from 'multiformats/cid'
36
35
  *
37
- * const fetch = await createVerifiedFetch({
38
- * gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
39
- * })
40
- *
41
36
  * const cid = CID.parse('bafyFoo') // some image file
42
- * const response = await fetch(cid)
37
+ * const response = await verifiedFetch(cid)
43
38
  * const json = await response.json()
44
39
  * ```
45
40
  *
46
41
  * @example Using IPFS protocol to fetch an image
47
42
  *
48
43
  * ```typescript
49
- * import { createVerifiedFetch } from '@helia/verified-fetch'
44
+ * import { verifiedFetch } from '@helia/verified-fetch'
50
45
  *
51
- * const fetch = await createVerifiedFetch({
52
- * gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
53
- * })
54
- * const response = await fetch('ipfs://bafyFoo') // CID for some image file
46
+ * const response = await verifiedFetch('ipfs://bafyFoo') // CID for some image file
55
47
  * const blob = await response.blob()
56
48
  * const image = document.createElement('img')
57
49
  * image.src = URL.createObjectURL(blob)
@@ -61,22 +53,42 @@
61
53
  * @example Using IPNS protocol to stream a big file
62
54
  *
63
55
  * ```typescript
64
- * import { createVerifiedFetch } from '@helia/verified-fetch'
56
+ * import { verifiedFetch } from '@helia/verified-fetch'
65
57
  *
66
- * const fetch = await createVerifiedFetch({
67
- * gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
68
- * })
69
- * const response = await fetch('ipns://mydomain.com/path/to/very-long-file.log')
58
+ * const response = await verifiedFetch('ipns://mydomain.com/path/to/very-long-file.log')
70
59
  * const bigFileStreamReader = await response.body.getReader()
71
60
  * ```
72
61
  *
73
- * ### Configuration
62
+ * ## Configuration
74
63
  *
75
- * #### Usage with customized Helia
64
+ * ### Custom HTTP gateways and routers
76
65
  *
77
- * You can see variations of Helia and js-libp2p configuration options at https://helia.io/interfaces/helia.index.HeliaInit.html.
66
+ * Out of the box `@helia/verified-fetch` uses a default set of [trustless gateways](https://specs.ipfs.tech/http-gateways/trustless-gateway/) for fetching blocks and [HTTP delegated routers](https://specs.ipfs.tech/routing/http-routing-v1/) for performing routing tasks - looking up peers, resolving/publishing [IPNS](https://docs.ipfs.tech/concepts/ipns/) names, etc.
78
67
  *
79
- * The `@helia/http` module is currently in-progress, but the init options should be a subset of the `helia` module's init options. See https://github.com/ipfs/helia/issues/289 for more information.
68
+ * It's possible to override these by passing `gateways` and `routers` keys to the `createVerifiedFetch` function:
69
+ *
70
+ * @example Configuring gateways and routers
71
+ *
72
+ * ```typescript
73
+ * import { createVerifiedFetch } from '@helia/verified-fetch'
74
+ *
75
+ * const fetch = await createVerifiedFetch({
76
+ * gateways: ['https://trustless-gateway.link'],
77
+ * routers: ['http://delegated-ipfs.dev']
78
+ *})
79
+ *
80
+ * const resp = await fetch('ipfs://bafy...')
81
+ *
82
+ * const json = await resp.json()
83
+ *```
84
+ *
85
+ * ### Usage with customized Helia
86
+ *
87
+ * For full control of how `@helia/verified-fetch` fetches content from the distributed web you can pass a preconfigured Helia node to `createVerifiedFetch`.
88
+ *
89
+ * The [helia](https://www.npmjs.com/package/helia) module is configured with a libp2p node that is suited for decentralized applications, alternatively [@helia/http](https://www.npmjs.com/package/@helia/http) is available which uses HTTP gateways for all network operations.
90
+ *
91
+ * You can see variations of Helia and js-libp2p configuration options at https://helia.io/interfaces/helia.index.HeliaInit.html.
80
92
  *
81
93
  * ```typescript
82
94
  * import { trustlessGateway } from '@helia/block-brokers'
@@ -100,28 +112,28 @@
100
112
  * const json = await resp.json()
101
113
  * ```
102
114
  *
103
- * ### Comparison to fetch
115
+ * ## Comparison to fetch
104
116
  *
105
- * First, this library will require instantiation in order to configure the gateways and delegated routers, or potentially a custom Helia instance. Secondly, once your verified-fetch method is created, it will act as similar to the `fetch()` API as possible.
117
+ * This module attempts to act as similarly to the `fetch()` API as possible.
106
118
  *
107
119
  * [The `fetch()` API](https://developer.mozilla.org/en-US/docs/Web/API/fetch) takes two parameters:
108
120
  *
109
121
  * 1. A [resource](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource)
110
122
  * 2. An [options object](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
111
123
  *
112
- * #### Resource argument
124
+ * ### Resource argument
113
125
  *
114
- * This library intends to support the following methods of fetching web3 content from IPFS:
126
+ * This library supports the following methods of fetching web3 content from IPFS:
115
127
  *
116
128
  * 1. IPFS protocol: `ipfs://<cidv0>` & `ipfs://<cidv0>`
117
129
  * 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
118
130
  * 3. CID instances: An actual CID instance `CID.parse('bafy...')`
119
131
  *
120
- * As well as support for pathing & params for item 1&2 above according to [IPFS - Path Gateway Specification](https://specs.ipfs.tech/http-gateways/path-gateway) & [IPFS - Trustless Gateway Specification](https://specs.ipfs.tech/http-gateways/trustless-gateway/). Further refinement of those specifications specifically for web-based scenarios can be found in the [Web Pathing Specification IPIP](https://github.com/ipfs/specs/pull/453).
132
+ * As well as support for pathing & params for item 1 & 2 above according to [IPFS - Path Gateway Specification](https://specs.ipfs.tech/http-gateways/path-gateway) & [IPFS - Trustless Gateway Specification](https://specs.ipfs.tech/http-gateways/trustless-gateway/). Further refinement of those specifications specifically for web-based scenarios can be found in the [Web Pathing Specification IPIP](https://github.com/ipfs/specs/pull/453).
121
133
  *
122
- * If you pass a CID instance, we assume you want the content for that specific CID only, and do not support pathing or params for that CID.
134
+ * If you pass a CID instance, it assumes you want the content for that specific CID only, and does not support pathing or params for that CID.
123
135
  *
124
- * #### Options argument
136
+ * ### Options argument
125
137
  *
126
138
  * This library does not plan to support the exact Fetch API options object, as some of the arguments don't make sense. Instead, it will only support options necessary to meet [IPFS specs](https://specs.ipfs.tech/) related to specifying the resultant shape of desired content.
127
139
  *
@@ -146,7 +158,6 @@
146
158
  * 5. `body` - An object that specifies the body of the request. Best effort to adhere to the [Fetch API body](https://developer.mozilla.org/en-US/docs/Web/API/fetch#body) parameter.
147
159
  * 6. `cache` - Will basically act as `force-cache` for the request. Best effort to adhere to the [Fetch API cache](https://developer.mozilla.org/en-US/docs/Web/API/fetch#cache) parameter.
148
160
  *
149
- *
150
161
  * Non-Fetch API options that will be supported:
151
162
  *
152
163
  * 1. `onProgress` - Similar to Helia `onProgress` options, this will be a function that will be called with a progress event. Supported progress events are:
@@ -167,7 +178,7 @@
167
178
  * 4. [IPIP-0328: JSON and CBOR Response Formats on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0328/)
168
179
  * 5. [IPIP-0288: TAR Response Format on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0288/)
169
180
  *
170
- * #### Response types
181
+ * ### Response types
171
182
  *
172
183
  * This library's purpose is to return reasonably representable content from IPFS. In other words, fetching content is intended for leaf-node content -- such as images/videos/audio & other assets, or other IPLD content (with link) -- that can be represented by https://developer.mozilla.org/en-US/docs/Web/API/Response#instance_methods. The content type you receive back will depend upon the CID you request as well as the `Accept` header value you provide.
173
184
  *
@@ -175,7 +186,7 @@
175
186
  *
176
187
  * If your content doesn't have a mime-type or an [IPFS spec](https://specs.ipfs.tech), this library will not support it, but you can use the [`helia`](https://github.com/ipfs/helia) library directly for those use cases. See [Unsupported response types](#unsupported-response-types) for more information.
177
188
  *
178
- * ##### Handling response types
189
+ * #### Handling response types
179
190
  *
180
191
  * For handling responses we want to follow conventions/abstractions from Fetch API where possible:
181
192
  *
@@ -184,12 +195,12 @@
184
195
  * - For plain text in utf-8, you would call `.text()`
185
196
  * - For streaming response data, use something like `response.body.getReader()` to get a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams#consuming_a_fetch_as_a_stream).
186
197
  *
187
- * ##### Unsupported response types
198
+ * #### Unsupported response types
188
199
  *
189
200
  * * Returning IPLD nodes or DAGs as JS objects is not supported, as there is no currently well-defined structure for representing this data in an [HTTP Response](https://developer.mozilla.org/en-US/docs/Web/API/Response). Instead, users should request `aplication/vnd.ipld.car` or use the [`helia`](https://github.com/ipfs/helia) library directly for this use case.
190
201
  * * Others? Open an issue or PR!
191
202
  *
192
- * #### Response headers
203
+ * ### Response headers
193
204
  *
194
205
  * This library will set the [HTTP Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) headers to the appropriate values for the content type according to the appropriate [IPFS Specifications](https://specs.ipfs.tech/).
195
206
  *
@@ -199,13 +210,13 @@
199
210
  * * https://specs.ipfs.tech/http-gateways/trustless-gateway/#response-headers
200
211
  * * https://specs.ipfs.tech/http-gateways/subdomain-gateway/#response-headers
201
212
  *
202
- * #### Possible Scenarios that could cause confusion
213
+ * ### Possible Scenarios that could cause confusion
203
214
  *
204
- * ##### Attempting to fetch the CID for content that does not make sense
215
+ * #### Attempting to fetch the CID for content that does not make sense
205
216
  *
206
217
  * If you request `bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze`, which points to the root of the en.wikipedia.org mirror, a response object does not make sense.
207
218
  *
208
- * #### Errors
219
+ * ### Errors
209
220
  *
210
221
  * Known Errors that can be thrown:
211
222
  *
@@ -300,6 +311,8 @@ export async function createVerifiedFetch (init?: Helia | CreateVerifiedFetchWit
300
311
  return verifiedFetch
301
312
  }
302
313
 
314
+ export { verifiedFetch } from './singleton.js'
315
+
303
316
  function isHelia (obj: any): obj is Helia {
304
317
  // test for the presence of known Helia properties, return a boolean value
305
318
  return obj?.blockstore != null &&
@@ -0,0 +1,20 @@
1
+ import { createVerifiedFetch } from './index.js'
2
+ import type { Resource, VerifiedFetch, VerifiedFetchInit } from './index.js'
3
+
4
+ let impl: VerifiedFetch | undefined
5
+
6
+ export const verifiedFetch: VerifiedFetch = async function verifiedFetch (resource: Resource, options?: VerifiedFetchInit): Promise<Response> {
7
+ if (impl == null) {
8
+ impl = await createVerifiedFetch()
9
+ }
10
+
11
+ return impl(resource, options)
12
+ }
13
+
14
+ verifiedFetch.start = async function () {
15
+ await impl?.start()
16
+ }
17
+
18
+ verifiedFetch.stop = async function () {
19
+ await impl?.stop()
20
+ }