@helia/verified-fetch 0.0.0-f243de2

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 (44) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +262 -0
  3. package/dist/index.min.js +140 -0
  4. package/dist/src/index.d.ts +259 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/index.js +251 -0
  7. package/dist/src/index.js.map +1 -0
  8. package/dist/src/utils/get-content-type.d.ts +11 -0
  9. package/dist/src/utils/get-content-type.d.ts.map +1 -0
  10. package/dist/src/utils/get-content-type.js +43 -0
  11. package/dist/src/utils/get-content-type.js.map +1 -0
  12. package/dist/src/utils/get-stream-and-content-type.d.ts +10 -0
  13. package/dist/src/utils/get-stream-and-content-type.d.ts.map +1 -0
  14. package/dist/src/utils/get-stream-and-content-type.js +37 -0
  15. package/dist/src/utils/get-stream-and-content-type.js.map +1 -0
  16. package/dist/src/utils/parse-resource.d.ts +18 -0
  17. package/dist/src/utils/parse-resource.d.ts.map +1 -0
  18. package/dist/src/utils/parse-resource.js +24 -0
  19. package/dist/src/utils/parse-resource.js.map +1 -0
  20. package/dist/src/utils/parse-url-string.d.ts +26 -0
  21. package/dist/src/utils/parse-url-string.d.ts.map +1 -0
  22. package/dist/src/utils/parse-url-string.js +109 -0
  23. package/dist/src/utils/parse-url-string.js.map +1 -0
  24. package/dist/src/utils/tlru.d.ts +15 -0
  25. package/dist/src/utils/tlru.d.ts.map +1 -0
  26. package/dist/src/utils/tlru.js +40 -0
  27. package/dist/src/utils/tlru.js.map +1 -0
  28. package/dist/src/utils/walk-path.d.ts +13 -0
  29. package/dist/src/utils/walk-path.d.ts.map +1 -0
  30. package/dist/src/utils/walk-path.js +17 -0
  31. package/dist/src/utils/walk-path.js.map +1 -0
  32. package/dist/src/verified-fetch.d.ts +64 -0
  33. package/dist/src/verified-fetch.d.ts.map +1 -0
  34. package/dist/src/verified-fetch.js +261 -0
  35. package/dist/src/verified-fetch.js.map +1 -0
  36. package/package.json +176 -0
  37. package/src/index.ts +310 -0
  38. package/src/utils/get-content-type.ts +55 -0
  39. package/src/utils/get-stream-and-content-type.ts +44 -0
  40. package/src/utils/parse-resource.ts +40 -0
  41. package/src/utils/parse-url-string.ts +139 -0
  42. package/src/utils/tlru.ts +52 -0
  43. package/src/utils/walk-path.ts +34 -0
  44. package/src/verified-fetch.ts +323 -0
package/LICENSE ADDED
@@ -0,0 +1,4 @@
1
+ This project is dual licensed under MIT and Apache-2.0.
2
+
3
+ MIT: https://www.opensource.org/licenses/mit
4
+ Apache-2.0: https://www.apache.org/licenses/license-2.0
package/README.md ADDED
@@ -0,0 +1,262 @@
1
+ <p align="center">
2
+ <a href="https://github.com/ipfs/helia" title="Helia">
3
+ <img src="https://raw.githubusercontent.com/ipfs/helia/main/assets/helia.png" alt="Helia logo" width="300" />
4
+ </a>
5
+ </p>
6
+
7
+ [![ipfs.tech](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](https://ipfs.tech)
8
+ [![Discuss](https://img.shields.io/discourse/https/discuss.ipfs.tech/posts.svg?style=flat-square)](https://discuss.ipfs.tech)
9
+ [![codecov](https://img.shields.io/codecov/c/github/ipfs/helia.svg?style=flat-square)](https://codecov.io/gh/ipfs/helia)
10
+ [![CI](https://img.shields.io/github/actions/workflow/status/ipfs/helia/main.yml?branch=main\&style=flat-square)](https://github.com/ipfs/helia/actions/workflows/main.yml?query=branch%3Amain)
11
+
12
+ > A fetch-like API for obtaining verified & trustless IPFS content on the web.
13
+
14
+ # About
15
+
16
+ `@helia/verified-fetch` is a library that provides a fetch-like API for fetching trustless content from IPFS and verifying it.
17
+
18
+ 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.
19
+
20
+ Exports a `createVerifiedFetch` function that returns a `fetch()` like API method Helia for fetching IPFS content.
21
+
22
+ You may use any supported resource argument to fetch content:
23
+
24
+ - CID instance
25
+ - IPFS URL
26
+ - IPNS URL
27
+
28
+ ## Example
29
+
30
+ ```typescript
31
+ import { createVerifiedFetch } from '@helia/verified-fetch'
32
+
33
+ const fetch = await createVerifiedFetch({
34
+ gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
35
+ })
36
+
37
+ const resp = await fetch('ipfs://bafy...')
38
+
39
+ const json = await resp.json()
40
+ ```
41
+
42
+ ## Example - Using a CID instance to fetch JSON
43
+
44
+ ```typescript
45
+ import { createVerifiedFetch } from '@helia/verified-fetch'
46
+ import { CID } from 'multiformats/cid'
47
+
48
+ const fetch = await createVerifiedFetch({
49
+ gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
50
+ })
51
+
52
+ const cid = CID.parse('bafyFoo') // some image file
53
+ const response = await fetch(cid)
54
+ const json = await response.json()
55
+ ```
56
+
57
+ ## Example - Using IPFS protocol to fetch an image
58
+
59
+ ```typescript
60
+ import { createVerifiedFetch } from '@helia/verified-fetch'
61
+
62
+ const fetch = await createVerifiedFetch({
63
+ gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
64
+ })
65
+ const response = await fetch('ipfs://bafyFoo') // CID for some image file
66
+ const blob = await response.blob()
67
+ const image = document.createElement('img')
68
+ image.src = URL.createObjectURL(blob)
69
+ document.body.appendChild(image)
70
+ ```
71
+
72
+ ## Example - Using IPNS protocol to stream a big file
73
+
74
+ ```typescript
75
+ import { createVerifiedFetch } from '@helia/verified-fetch'
76
+
77
+ const fetch = await createVerifiedFetch({
78
+ gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
79
+ })
80
+ const response = await fetch('ipns://mydomain.com/path/to/very-long-file.log')
81
+ const bigFileStreamReader = await response.body.getReader()
82
+ ```
83
+
84
+ ### Configuration
85
+
86
+ #### Usage with customized Helia
87
+
88
+ You can see variations of Helia and js-libp2p configuration options at <https://helia.io/interfaces/helia.index.HeliaInit.html>.
89
+
90
+ 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
+
92
+ ```typescript
93
+ import { trustlessGateway } from '@helia/block-brokers'
94
+ import { createHeliaHTTP } from '@helia/http'
95
+ import { delegatedHTTPRouting } from '@helia/routers'
96
+ import { createVerifiedFetch } from '@helia/verified-fetch'
97
+
98
+ const fetch = await createVerifiedFetch(
99
+ await createHeliaHTTP({
100
+ blockBrokers: [
101
+ trustlessGateway({
102
+ gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
103
+ })
104
+ ],
105
+ routers: ['http://delegated-ipfs.dev'].map((routerUrl) => delegatedHTTPRouting(routerUrl))
106
+ })
107
+ )
108
+
109
+ const resp = await fetch('ipfs://bafy...')
110
+
111
+ const json = await resp.json()
112
+ ```
113
+
114
+ ### Comparison to fetch
115
+
116
+ 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
+
118
+ [The `fetch()` API](https://developer.mozilla.org/en-US/docs/Web/API/fetch) takes two parameters:
119
+
120
+ 1. A [resource](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource)
121
+ 2. An [options object](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
122
+
123
+ #### Resource argument
124
+
125
+ This library intends to support the following methods of fetching web3 content from IPFS:
126
+
127
+ 1. IPFS protocol: `ipfs://<cidv0>` & `ipfs://<cidv0>`
128
+ 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
129
+ 3. CID instances: An actual CID instance `CID.parse('bafy...')`
130
+
131
+ 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
+
133
+ 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
+
135
+ #### Options argument
136
+
137
+ 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.
138
+
139
+ Some of those header specifications are:
140
+
141
+ 1. <https://specs.ipfs.tech/http-gateways/path-gateway/#request-headers>
142
+ 2. <https://specs.ipfs.tech/http-gateways/trustless-gateway/#request-headers>
143
+ 3. <https://specs.ipfs.tech/http-gateways/subdomain-gateway/#request-headers>
144
+
145
+ Where possible, options and Helia internals will be automatically configured to the appropriate codec & content type based on the `verified-fetch` configuration and `options` argument passed.
146
+
147
+ Known Fetch API options that will be supported:
148
+
149
+ 1. `signal` - An AbortSignal that a user can use to abort the request.
150
+ 2. `redirect` - A string that specifies the redirect type. One of `follow`, `error`, or `manual`. Defaults to `follow`. Best effort to adhere to the [Fetch API redirect](https://developer.mozilla.org/en-US/docs/Web/API/fetch#redirect) parameter.
151
+ 3. `headers` - An object of headers to be sent with the request. Best effort to adhere to the [Fetch API headers](https://developer.mozilla.org/en-US/docs/Web/API/fetch#headers) parameter.
152
+ - `accept` - A string that specifies the accept header. Relevant values:
153
+ - [`vnd.ipld.raw`](https://www.iana.org/assignments/media-types/application/vnd.ipld.raw). (default)
154
+ - [`vnd.ipld.car`](https://www.iana.org/assignments/media-types/application/vnd.ipld.car)
155
+ - [`vnd.ipfs.ipns-record`](https://www.iana.org/assignments/media-types/application/vnd.ipfs.ipns-record)
156
+ 4. `method` - A string that specifies the HTTP method to use for the request. Defaults to `GET`. Best effort to adhere to the [Fetch API method](https://developer.mozilla.org/en-US/docs/Web/API/fetch#method) parameter.
157
+ 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.
158
+ 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.
159
+
160
+ Non-Fetch API options that will be supported:
161
+
162
+ 1. `onProgress` - Similar to Helia `onProgress` options, this will be a function that will be called with a progress event. Supported progress events are:
163
+ - `helia:verified-fetch:error` - An error occurred during the request.
164
+ - `helia:verified-fetch:request:start` - The request has been sent
165
+ - `helia:verified-fetch:request:complete` - The request has been sent
166
+ - `helia:verified-fetch:request:error` - An error occurred during the request.
167
+ - `helia:verified-fetch:request:abort` - The request was aborted prior to completion.
168
+ - `helia:verified-fetch:response:start` - The initial HTTP Response headers have been set, and response stream is started.
169
+ - `helia:verified-fetch:response:complete` - The response stream has completed.
170
+ - `helia:verified-fetch:response:error` - An error occurred while building the response.
171
+
172
+ Some in-flight specs (IPIPs) that will affect the options object this library supports in the future can be seen at <https://specs.ipfs.tech/ipips>, a subset are:
173
+
174
+ 1. [IPIP-0412: Signaling Block Order in CARs on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0412/)
175
+ 2. [IPIP-0402: Partial CAR Support on Trustless Gateways](https://specs.ipfs.tech/ipips/ipip-0402/)
176
+ 3. [IPIP-0386: Subdomain Gateway Interop with \_redirects](https://specs.ipfs.tech/ipips/ipip-0386/)
177
+ 4. [IPIP-0328: JSON and CBOR Response Formats on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0328/)
178
+ 5. [IPIP-0288: TAR Response Format on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0288/)
179
+
180
+ #### Response types
181
+
182
+ 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.
183
+
184
+ All content we retrieve from the IPFS network is obtained via an AsyncIterable, and will be set as the [body of the HTTP Response](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response#body) via a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams#consuming_a_fetch_as_a_stream) or other efficient method that avoids loading the entire response into memory or getting the entire response from the network before returning a response to the user.
185
+
186
+ 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.
187
+
188
+ ##### Handling response types
189
+
190
+ For handling responses we want to follow conventions/abstractions from Fetch API where possible:
191
+
192
+ - For JSON, assuming you abstract any differences between dag-json/dag-cbor/json/and json-file-on-unixfs, you would call `.json()` to get a JSON object.
193
+ - For images (or other web-relevant asset) you want to add to the DOM, use `.blob()` or `.arrayBuffer()` to get the raw bytes.
194
+ - For plain text in utf-8, you would call `.text()`
195
+ - 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).
196
+
197
+ ##### Unsupported response types
198
+
199
+ - 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.
200
+ - Others? Open an issue or PR!
201
+
202
+ #### Response headers
203
+
204
+ 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/).
205
+
206
+ Some known header specifications:
207
+
208
+ - <https://specs.ipfs.tech/http-gateways/path-gateway/#response-headers>
209
+ - <https://specs.ipfs.tech/http-gateways/trustless-gateway/#response-headers>
210
+ - <https://specs.ipfs.tech/http-gateways/subdomain-gateway/#response-headers>
211
+
212
+ #### Possible Scenarios that could cause confusion
213
+
214
+ ##### Attempting to fetch the CID for content that does not make sense
215
+
216
+ If you request `bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze`, which points to the root of the en.wikipedia.org mirror, a response object does not make sense.
217
+
218
+ #### Errors
219
+
220
+ Known Errors that can be thrown:
221
+
222
+ 1. `TypeError` - If the resource argument is not a string, CID, or CID string.
223
+ 2. `TypeError` - If the options argument is passed and not an object.
224
+ 3. `TypeError` - If the options argument is passed and is malformed.
225
+ 4. `AbortError` - If the content request is aborted due to user aborting provided AbortSignal.
226
+
227
+ # Install
228
+
229
+ ```console
230
+ $ npm i @helia/verified-fetch
231
+ ```
232
+
233
+ ## Browser `<script>` tag
234
+
235
+ Loading this module through a script tag will make it's exports available as `HeliaVerifiedFetch` in the global namespace.
236
+
237
+ ```html
238
+ <script src="https://unpkg.com/@helia/verified-fetch/dist/index.min.js"></script>
239
+ ```
240
+
241
+ # API Docs
242
+
243
+ - <https://ipfs.github.io/helia/modules/_helia_verified_fetch.html>
244
+
245
+ # License
246
+
247
+ Licensed under either of
248
+
249
+ - Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
250
+ - MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
251
+
252
+ # Contribute
253
+
254
+ Contributions welcome! Please check out [the issues](https://github.com/ipfs/helia/issues).
255
+
256
+ Also see our [contributing document](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md) for more information on how we work, and about contributing in general.
257
+
258
+ Please be aware that all interactions related to this repo are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
259
+
260
+ Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
261
+
262
+ [![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md)