@helia/verified-fetch 0.0.0-8a5bc6f → 0.0.0-f58d467

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.
Files changed (31) hide show
  1. package/README.md +80 -43
  2. package/dist/index.min.js +4 -29
  3. package/dist/src/index.d.ts +110 -51
  4. package/dist/src/index.d.ts.map +1 -1
  5. package/dist/src/index.js +86 -47
  6. package/dist/src/index.js.map +1 -1
  7. package/dist/src/singleton.d.ts +3 -0
  8. package/dist/src/singleton.d.ts.map +1 -0
  9. package/dist/src/singleton.js +15 -0
  10. package/dist/src/singleton.js.map +1 -0
  11. package/dist/src/utils/get-stream-from-async-iterable.d.ts +10 -0
  12. package/dist/src/utils/get-stream-from-async-iterable.d.ts.map +1 -0
  13. package/dist/src/utils/{get-stream-and-content-type.js → get-stream-from-async-iterable.js} +10 -9
  14. package/dist/src/utils/get-stream-from-async-iterable.js.map +1 -0
  15. package/dist/src/verified-fetch.d.ts +4 -1
  16. package/dist/src/verified-fetch.d.ts.map +1 -1
  17. package/dist/src/verified-fetch.js +34 -6
  18. package/dist/src/verified-fetch.js.map +1 -1
  19. package/package.json +19 -19
  20. package/src/index.ts +117 -52
  21. package/src/singleton.ts +20 -0
  22. package/src/utils/{get-stream-and-content-type.ts → get-stream-from-async-iterable.ts} +9 -8
  23. package/src/verified-fetch.ts +41 -10
  24. package/dist/src/utils/get-content-type.d.ts +0 -11
  25. package/dist/src/utils/get-content-type.d.ts.map +0 -1
  26. package/dist/src/utils/get-content-type.js +0 -43
  27. package/dist/src/utils/get-content-type.js.map +0 -1
  28. package/dist/src/utils/get-stream-and-content-type.d.ts +0 -10
  29. package/dist/src/utils/get-stream-and-content-type.d.ts.map +0 -1
  30. package/dist/src/utils/get-stream-and-content-type.js.map +0 -1
  31. package/src/utils/get-content-type.ts +0 -55
@@ -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
56
+ * import { verifiedFetch } from '@helia/verified-fetch'
57
+ *
58
+ * const response = await verifiedFetch('ipns://mydomain.com/path/to/very-long-file.log')
59
+ * const bigFileStreamReader = await response.body.getReader()
60
+ * ```
61
+ *
62
+ * ## Configuration
63
+ *
64
+ * ### Custom HTTP gateways and routers
65
+ *
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.
67
+ *
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
64
73
  * import { createVerifiedFetch } from '@helia/verified-fetch'
65
74
  *
66
75
  * const fetch = await createVerifiedFetch({
67
- * gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
76
+ * gateways: ['https://trustless-gateway.link'],
77
+ * routers: ['http://delegated-ipfs.dev']
68
78
  * })
69
- * const response = await fetch('ipns://mydomain.com/path/to/very-long-file.log')
70
- * const bigFileStreamReader = await response.body.getReader()
71
- * ```
72
79
  *
73
- * ### Configuration
80
+ * const resp = await fetch('ipfs://bafy...')
81
+ *
82
+ * const json = await resp.json()
83
+ *```
74
84
  *
75
- * #### Usage with customized Helia
85
+ * ### Usage with customized Helia
76
86
  *
77
- * You can see variations of Helia and js-libp2p configuration options at https://helia.io/interfaces/helia.index.HeliaInit.html.
87
+ * For full control of how `@helia/verified-fetch` fetches content from the distributed web you can pass a preconfigured Helia node to `createVerifiedFetch`.
78
88
  *
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.
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,53 @@
100
112
  * const json = await resp.json()
101
113
  * ```
102
114
  *
103
- * ### Comparison to fetch
115
+ * ### Custom content-type parsing
116
+ *
117
+ * By default, `@helia/verified-fetch` sets the `Content-Type` header as `application/octet-stream` - this is because the `.json()`, `.text()`, `.blob()`, and `.arrayBuffer()` methods will usually work as expected without a detailed content type.
118
+ *
119
+ * If you require an accurate content-type you can provide a `contentTypeParser` function as an option to `createVerifiedFetch` to handle parsing the content type.
120
+ *
121
+ * The function you provide will be called with the first chunk of bytes from the file and should return a string or a promise of a string.
122
+ *
123
+ * @example Customizing content-type parsing
124
+ *
125
+ * ```typescript
126
+ * import { createVerifiedFetch } from '@helia/verified-fetch'
127
+ * import { fileTypeFromBuffer } from '@sgtpooki/file-type'
128
+ *
129
+ * const fetch = await createVerifiedFetch({
130
+ * gateways: ['https://trustless-gateway.link'],
131
+ * routers: ['http://delegated-ipfs.dev'],
132
+ * contentTypeParser: async (bytes) => {
133
+ * // call to some magic-byte recognition library like magic-bytes, file-type, or your own custom byte recognition
134
+ * const result = await fileTypeFromBuffer(bytes)
135
+ * return result?.mime
136
+ * }
137
+ * })
138
+ * ```
139
+ *
140
+ * ## Comparison to fetch
104
141
  *
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.
142
+ * This module attempts to act as similarly to the `fetch()` API as possible.
106
143
  *
107
144
  * [The `fetch()` API](https://developer.mozilla.org/en-US/docs/Web/API/fetch) takes two parameters:
108
145
  *
109
146
  * 1. A [resource](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource)
110
147
  * 2. An [options object](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
111
148
  *
112
- * #### Resource argument
149
+ * ### Resource argument
113
150
  *
114
- * This library intends to support the following methods of fetching web3 content from IPFS:
151
+ * This library supports the following methods of fetching web3 content from IPFS:
115
152
  *
116
153
  * 1. IPFS protocol: `ipfs://<cidv0>` & `ipfs://<cidv0>`
117
154
  * 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
118
155
  * 3. CID instances: An actual CID instance `CID.parse('bafy...')`
119
156
  *
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).
157
+ * 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
158
  *
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.
159
+ * 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
160
  *
124
- * #### Options argument
161
+ * ### Options argument
125
162
  *
126
163
  * 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
164
  *
@@ -146,7 +183,6 @@
146
183
  * 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
184
  * 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
185
  *
149
- *
150
186
  * Non-Fetch API options that will be supported:
151
187
  *
152
188
  * 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 +203,7 @@
167
203
  * 4. [IPIP-0328: JSON and CBOR Response Formats on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0328/)
168
204
  * 5. [IPIP-0288: TAR Response Format on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0288/)
169
205
  *
170
- * #### Response types
206
+ * ### Response types
171
207
  *
172
208
  * 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
209
  *
@@ -175,7 +211,7 @@
175
211
  *
176
212
  * 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
213
  *
178
- * ##### Handling response types
214
+ * #### Handling response types
179
215
  *
180
216
  * For handling responses we want to follow conventions/abstractions from Fetch API where possible:
181
217
  *
@@ -184,12 +220,12 @@
184
220
  * - For plain text in utf-8, you would call `.text()`
185
221
  * - 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
222
  *
187
- * ##### Unsupported response types
223
+ * #### Unsupported response types
188
224
  *
189
225
  * * 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
226
  * * Others? Open an issue or PR!
191
227
  *
192
- * #### Response headers
228
+ * ### Response headers
193
229
  *
194
230
  * 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
231
  *
@@ -199,13 +235,13 @@
199
235
  * * https://specs.ipfs.tech/http-gateways/trustless-gateway/#response-headers
200
236
  * * https://specs.ipfs.tech/http-gateways/subdomain-gateway/#response-headers
201
237
  *
202
- * #### Possible Scenarios that could cause confusion
238
+ * ### Possible Scenarios that could cause confusion
203
239
  *
204
- * ##### Attempting to fetch the CID for content that does not make sense
240
+ * #### Attempting to fetch the CID for content that does not make sense
205
241
  *
206
242
  * If you request `bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze`, which points to the root of the en.wikipedia.org mirror, a response object does not make sense.
207
243
  *
208
- * #### Errors
244
+ * ### Errors
209
245
  *
210
246
  * Known Errors that can be thrown:
211
247
  *
@@ -236,24 +272,47 @@ export interface VerifiedFetch {
236
272
  stop(): Promise<void>;
237
273
  }
238
274
  /**
239
- * Instead of passing a Helia instance, you can pass a list of gateways and routers, and a HeliaHTTP instance will be created for you.
275
+ * Instead of passing a Helia instance, you can pass a list of gateways and
276
+ * routers, and a HeliaHTTP instance will be created for you.
240
277
  */
241
- export interface CreateVerifiedFetchWithOptions {
278
+ export interface CreateVerifiedFetchOptions {
242
279
  gateways: string[];
243
280
  routers?: string[];
281
+ /**
282
+ * A function to handle parsing content type from bytes. The function you
283
+ * provide will be passed the first set of bytes we receive from the network,
284
+ * and should return a string that will be used as the value for the
285
+ * `Content-Type` header in the response.
286
+ */
287
+ contentTypeParser?: ContentTypeParser;
288
+ }
289
+ /**
290
+ * A ContentTypeParser attempts to return the mime type of a given file. It
291
+ * receives the first chunk of the file data and the file name, if it is
292
+ * available. The function can be sync or async and if it returns/resolves to
293
+ * `undefined`, `application/octet-stream` will be used.
294
+ */
295
+ export interface ContentTypeParser {
296
+ /**
297
+ * Attempt to determine a mime type, either via of the passed bytes or the
298
+ * filename if it is available.
299
+ */
300
+ (bytes: Uint8Array, fileName?: string): Promise<string | undefined> | string | undefined;
244
301
  }
245
302
  export type BubbledProgressEvents = GetEvents | ResolveProgressEvents | ResolveDnsLinkProgressEvents | IPNSRoutingEvents;
246
303
  export type VerifiedFetchProgressEvents = ProgressEvent<'verified-fetch:request:start', CIDDetail> | ProgressEvent<'verified-fetch:request:info', string> | ProgressEvent<'verified-fetch:request:progress:chunk', CIDDetail> | ProgressEvent<'verified-fetch:request:end', CIDDetail> | ProgressEvent<'verified-fetch:request:error', CIDDetailError>;
247
304
  /**
248
305
  * Options for the `fetch` function returned by `createVerifiedFetch`.
249
306
  *
250
- * This method accepts all the same options as the `fetch` function in the browser, plus an `onProgress` option to
251
- * listen for progress events.
307
+ * This interface contains all the same fields as the [options object](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
308
+ * passed to `fetch` in browsers, plus an `onProgress` option to listen for
309
+ * progress events.
252
310
  */
253
311
  export interface VerifiedFetchInit extends RequestInit, ProgressOptions<BubbledProgressEvents | VerifiedFetchProgressEvents> {
254
312
  }
255
313
  /**
256
314
  * Create and return a Helia node
257
315
  */
258
- export declare function createVerifiedFetch(init?: Helia | CreateVerifiedFetchWithOptions): Promise<VerifiedFetch>;
316
+ export declare function createVerifiedFetch(init?: Helia | CreateVerifiedFetchOptions): Promise<VerifiedFetch>;
317
+ export { verifiedFetch } from './singleton.js';
259
318
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuNG;AAMH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,KAAK,EAAE,iBAAiB,EAAE,4BAA4B,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AACzG,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAA;AAC9C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAC3C,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAErE;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,GAAG,CAAA;AAEnC,MAAM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;;OAGG;IACH,CAAC,KAAK,EAAE,UAAU,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,GAAG,MAAM,GAAG,SAAS,CAAA;CACzF;AAED,MAAM,MAAM,qBAAqB,GAE/B,SAAS,GAET,qBAAqB,GAAG,4BAA4B,GAAG,iBAAiB,CAAA;AAE1E,MAAM,MAAM,2BAA2B,GACrC,aAAa,CAAC,8BAA8B,EAAE,SAAS,CAAC,GACxD,aAAa,CAAC,6BAA6B,EAAE,MAAM,CAAC,GACpD,aAAa,CAAC,uCAAuC,EAAE,SAAS,CAAC,GACjE,aAAa,CAAC,4BAA4B,EAAE,SAAS,CAAC,GACtD,aAAa,CAAC,8BAA8B,EAAE,cAAc,CAAC,CAAA;AAE/D;;;;;;GAMG;AACH,MAAM,WAAW,iBAAkB,SAAQ,WAAW,EAAE,eAAe,CAAC,qBAAqB,GAAG,2BAA2B,CAAC;CAC3H;AAED;;GAEG;AACH,wBAAsB,mBAAmB,CAAE,IAAI,CAAC,EAAE,KAAK,GAAG,0BAA0B,GAAG,OAAO,CAAC,aAAa,CAAC,CAuB5G;AAED,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA"}
package/dist/src/index.js 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
56
+ * import { verifiedFetch } from '@helia/verified-fetch'
57
+ *
58
+ * const response = await verifiedFetch('ipns://mydomain.com/path/to/very-long-file.log')
59
+ * const bigFileStreamReader = await response.body.getReader()
60
+ * ```
61
+ *
62
+ * ## Configuration
63
+ *
64
+ * ### Custom HTTP gateways and routers
65
+ *
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.
67
+ *
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
64
73
  * import { createVerifiedFetch } from '@helia/verified-fetch'
65
74
  *
66
75
  * const fetch = await createVerifiedFetch({
67
- * gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
76
+ * gateways: ['https://trustless-gateway.link'],
77
+ * routers: ['http://delegated-ipfs.dev']
68
78
  * })
69
- * const response = await fetch('ipns://mydomain.com/path/to/very-long-file.log')
70
- * const bigFileStreamReader = await response.body.getReader()
71
- * ```
72
79
  *
73
- * ### Configuration
80
+ * const resp = await fetch('ipfs://bafy...')
81
+ *
82
+ * const json = await resp.json()
83
+ *```
84
+ *
85
+ * ### Usage with customized Helia
74
86
  *
75
- * #### Usage with customized Helia
87
+ * For full control of how `@helia/verified-fetch` fetches content from the distributed web you can pass a preconfigured Helia node to `createVerifiedFetch`.
76
88
  *
77
- * You can see variations of Helia and js-libp2p configuration options at https://helia.io/interfaces/helia.index.HeliaInit.html.
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.
78
90
  *
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.
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,53 @@
100
112
  * const json = await resp.json()
101
113
  * ```
102
114
  *
103
- * ### Comparison to fetch
115
+ * ### Custom content-type parsing
116
+ *
117
+ * By default, `@helia/verified-fetch` sets the `Content-Type` header as `application/octet-stream` - this is because the `.json()`, `.text()`, `.blob()`, and `.arrayBuffer()` methods will usually work as expected without a detailed content type.
118
+ *
119
+ * If you require an accurate content-type you can provide a `contentTypeParser` function as an option to `createVerifiedFetch` to handle parsing the content type.
104
120
  *
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.
121
+ * The function you provide will be called with the first chunk of bytes from the file and should return a string or a promise of a string.
122
+ *
123
+ * @example Customizing content-type parsing
124
+ *
125
+ * ```typescript
126
+ * import { createVerifiedFetch } from '@helia/verified-fetch'
127
+ * import { fileTypeFromBuffer } from '@sgtpooki/file-type'
128
+ *
129
+ * const fetch = await createVerifiedFetch({
130
+ * gateways: ['https://trustless-gateway.link'],
131
+ * routers: ['http://delegated-ipfs.dev'],
132
+ * contentTypeParser: async (bytes) => {
133
+ * // call to some magic-byte recognition library like magic-bytes, file-type, or your own custom byte recognition
134
+ * const result = await fileTypeFromBuffer(bytes)
135
+ * return result?.mime
136
+ * }
137
+ * })
138
+ * ```
139
+ *
140
+ * ## Comparison to fetch
141
+ *
142
+ * This module attempts to act as similarly to the `fetch()` API as possible.
106
143
  *
107
144
  * [The `fetch()` API](https://developer.mozilla.org/en-US/docs/Web/API/fetch) takes two parameters:
108
145
  *
109
146
  * 1. A [resource](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource)
110
147
  * 2. An [options object](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
111
148
  *
112
- * #### Resource argument
149
+ * ### Resource argument
113
150
  *
114
- * This library intends to support the following methods of fetching web3 content from IPFS:
151
+ * This library supports the following methods of fetching web3 content from IPFS:
115
152
  *
116
153
  * 1. IPFS protocol: `ipfs://<cidv0>` & `ipfs://<cidv0>`
117
154
  * 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
118
155
  * 3. CID instances: An actual CID instance `CID.parse('bafy...')`
119
156
  *
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).
157
+ * 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
158
  *
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.
159
+ * 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
160
  *
124
- * #### Options argument
161
+ * ### Options argument
125
162
  *
126
163
  * 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
164
  *
@@ -146,7 +183,6 @@
146
183
  * 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
184
  * 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
185
  *
149
- *
150
186
  * Non-Fetch API options that will be supported:
151
187
  *
152
188
  * 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 +203,7 @@
167
203
  * 4. [IPIP-0328: JSON and CBOR Response Formats on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0328/)
168
204
  * 5. [IPIP-0288: TAR Response Format on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0288/)
169
205
  *
170
- * #### Response types
206
+ * ### Response types
171
207
  *
172
208
  * 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
209
  *
@@ -175,7 +211,7 @@
175
211
  *
176
212
  * 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
213
  *
178
- * ##### Handling response types
214
+ * #### Handling response types
179
215
  *
180
216
  * For handling responses we want to follow conventions/abstractions from Fetch API where possible:
181
217
  *
@@ -184,12 +220,12 @@
184
220
  * - For plain text in utf-8, you would call `.text()`
185
221
  * - 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
222
  *
187
- * ##### Unsupported response types
223
+ * #### Unsupported response types
188
224
  *
189
225
  * * 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
226
  * * Others? Open an issue or PR!
191
227
  *
192
- * #### Response headers
228
+ * ### Response headers
193
229
  *
194
230
  * 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
231
  *
@@ -199,13 +235,13 @@
199
235
  * * https://specs.ipfs.tech/http-gateways/trustless-gateway/#response-headers
200
236
  * * https://specs.ipfs.tech/http-gateways/subdomain-gateway/#response-headers
201
237
  *
202
- * #### Possible Scenarios that could cause confusion
238
+ * ### Possible Scenarios that could cause confusion
203
239
  *
204
- * ##### Attempting to fetch the CID for content that does not make sense
240
+ * #### Attempting to fetch the CID for content that does not make sense
205
241
  *
206
242
  * If you request `bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze`, which points to the root of the en.wikipedia.org mirror, a response object does not make sense.
207
243
  *
208
- * #### Errors
244
+ * ### Errors
209
245
  *
210
246
  * Known Errors that can be thrown:
211
247
  *
@@ -222,7 +258,9 @@ import { VerifiedFetch as VerifiedFetchClass } from './verified-fetch.js';
222
258
  * Create and return a Helia node
223
259
  */
224
260
  export async function createVerifiedFetch(init) {
261
+ let contentTypeParser;
225
262
  if (!isHelia(init)) {
263
+ contentTypeParser = init?.contentTypeParser;
226
264
  init = await createHeliaHTTP({
227
265
  blockBrokers: [
228
266
  trustlessGateway({
@@ -232,7 +270,7 @@ export async function createVerifiedFetch(init) {
232
270
  routers: (init?.routers ?? ['https://delegated-ipfs.dev']).map((routerUrl) => delegatedHTTPRouting(routerUrl))
233
271
  });
234
272
  }
235
- const verifiedFetchInstance = new VerifiedFetchClass({ helia: init });
273
+ const verifiedFetchInstance = new VerifiedFetchClass({ helia: init }, { contentTypeParser });
236
274
  async function verifiedFetch(resource, options) {
237
275
  return verifiedFetchInstance.fetch(resource, options);
238
276
  }
@@ -240,6 +278,7 @@ export async function createVerifiedFetch(init) {
240
278
  verifiedFetch.start = verifiedFetchInstance.start.bind(verifiedFetchInstance);
241
279
  return verifiedFetch;
242
280
  }
281
+ export { verifiedFetch } from './singleton.js';
243
282
  function isHelia(obj) {
244
283
  // test for the presence of known Helia properties, return a boolean value
245
284
  return obj?.blockstore != null &&
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuNG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AACrD,OAAO,EAAE,aAAa,IAAI,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AAyDzE;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAE,IAA6C;IACtF,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACnB,IAAI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,KAAK,GAAG,qBAAqB,CAAC,KAAK,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAA;IAE7E,OAAO,aAAa,CAAA;AACtB,CAAC;AAED,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAE9C,SAAS,OAAO,CAAE,GAAQ;IACxB,0EAA0E;IAC1E,OAAO,GAAG,EAAE,UAAU,IAAI,IAAI;QAC5B,GAAG,EAAE,SAAS,IAAI,IAAI;QACtB,GAAG,EAAE,EAAE,IAAI,IAAI;QACf,GAAG,EAAE,IAAI,IAAI,IAAI;QACjB,GAAG,EAAE,KAAK,IAAI,IAAI,CAAA;AACtB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { VerifiedFetch } from './index.js';
2
+ export declare const verifiedFetch: VerifiedFetch;
3
+ //# sourceMappingURL=singleton.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"singleton.d.ts","sourceRoot":"","sources":["../../src/singleton.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAY,aAAa,EAAqB,MAAM,YAAY,CAAA;AAI5E,eAAO,MAAM,aAAa,EAAE,aAM3B,CAAA"}
@@ -0,0 +1,15 @@
1
+ import { createVerifiedFetch } from './index.js';
2
+ let impl;
3
+ export const verifiedFetch = async function verifiedFetch(resource, options) {
4
+ if (impl == null) {
5
+ impl = await createVerifiedFetch();
6
+ }
7
+ return impl(resource, options);
8
+ };
9
+ verifiedFetch.start = async function () {
10
+ await impl?.start();
11
+ };
12
+ verifiedFetch.stop = async function () {
13
+ await impl?.stop();
14
+ };
15
+ //# sourceMappingURL=singleton.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"singleton.js","sourceRoot":"","sources":["../../src/singleton.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAGhD,IAAI,IAA+B,CAAA;AAEnC,MAAM,CAAC,MAAM,aAAa,GAAkB,KAAK,UAAU,aAAa,CAAE,QAAkB,EAAE,OAA2B;IACvH,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;QACjB,IAAI,GAAG,MAAM,mBAAmB,EAAE,CAAA;IACpC,CAAC;IAED,OAAO,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;AAChC,CAAC,CAAA;AAED,aAAa,CAAC,KAAK,GAAG,KAAK;IACzB,MAAM,IAAI,EAAE,KAAK,EAAE,CAAA;AACrB,CAAC,CAAA;AAED,aAAa,CAAC,IAAI,GAAG,KAAK;IACxB,MAAM,IAAI,EAAE,IAAI,EAAE,CAAA;AACpB,CAAC,CAAA"}
@@ -0,0 +1,10 @@
1
+ import type { VerifiedFetchInit } from '../index.js';
2
+ import type { ComponentLogger } from '@libp2p/interface';
3
+ /**
4
+ * Converts an async iterator of Uint8Array bytes to a stream and returns the first chunk of bytes
5
+ */
6
+ export declare function getStreamFromAsyncIterable(iterator: AsyncIterable<Uint8Array>, path: string, logger: ComponentLogger, options?: Pick<VerifiedFetchInit, 'onProgress'>): Promise<{
7
+ stream: ReadableStream<Uint8Array>;
8
+ firstChunk: Uint8Array;
9
+ }>;
10
+ //# sourceMappingURL=get-stream-from-async-iterable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-stream-from-async-iterable.d.ts","sourceRoot":"","sources":["../../../src/utils/get-stream-from-async-iterable.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACpD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAExD;;GAEG;AACH,wBAAsB,0BAA0B,CAAE,QAAQ,EAAE,aAAa,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,iBAAiB,EAAE,YAAY,CAAC,GAAG,OAAO,CAAC;IAAE,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,CAAC;IAAC,UAAU,EAAE,UAAU,CAAA;CAAE,CAAC,CAqCtP"}