@helia/verified-fetch 0.0.0-3851fe2

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 (49) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +275 -0
  3. package/dist/index.min.js +140 -0
  4. package/dist/src/index.d.ts +271 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/index.js +263 -0
  7. package/dist/src/index.js.map +1 -0
  8. package/dist/src/singleton.d.ts +3 -0
  9. package/dist/src/singleton.d.ts.map +1 -0
  10. package/dist/src/singleton.js +15 -0
  11. package/dist/src/singleton.js.map +1 -0
  12. package/dist/src/utils/get-content-type.d.ts +11 -0
  13. package/dist/src/utils/get-content-type.d.ts.map +1 -0
  14. package/dist/src/utils/get-content-type.js +43 -0
  15. package/dist/src/utils/get-content-type.js.map +1 -0
  16. package/dist/src/utils/get-stream-and-content-type.d.ts +10 -0
  17. package/dist/src/utils/get-stream-and-content-type.d.ts.map +1 -0
  18. package/dist/src/utils/get-stream-and-content-type.js +37 -0
  19. package/dist/src/utils/get-stream-and-content-type.js.map +1 -0
  20. package/dist/src/utils/parse-resource.d.ts +18 -0
  21. package/dist/src/utils/parse-resource.d.ts.map +1 -0
  22. package/dist/src/utils/parse-resource.js +24 -0
  23. package/dist/src/utils/parse-resource.js.map +1 -0
  24. package/dist/src/utils/parse-url-string.d.ts +26 -0
  25. package/dist/src/utils/parse-url-string.d.ts.map +1 -0
  26. package/dist/src/utils/parse-url-string.js +109 -0
  27. package/dist/src/utils/parse-url-string.js.map +1 -0
  28. package/dist/src/utils/tlru.d.ts +15 -0
  29. package/dist/src/utils/tlru.d.ts.map +1 -0
  30. package/dist/src/utils/tlru.js +40 -0
  31. package/dist/src/utils/tlru.js.map +1 -0
  32. package/dist/src/utils/walk-path.d.ts +13 -0
  33. package/dist/src/utils/walk-path.d.ts.map +1 -0
  34. package/dist/src/utils/walk-path.js +17 -0
  35. package/dist/src/utils/walk-path.js.map +1 -0
  36. package/dist/src/verified-fetch.d.ts +64 -0
  37. package/dist/src/verified-fetch.d.ts.map +1 -0
  38. package/dist/src/verified-fetch.js +261 -0
  39. package/dist/src/verified-fetch.js.map +1 -0
  40. package/package.json +175 -0
  41. package/src/index.ts +323 -0
  42. package/src/singleton.ts +20 -0
  43. package/src/utils/get-content-type.ts +55 -0
  44. package/src/utils/get-stream-and-content-type.ts +44 -0
  45. package/src/utils/parse-resource.ts +40 -0
  46. package/src/utils/parse-url-string.ts +139 -0
  47. package/src/utils/tlru.ts +52 -0
  48. package/src/utils/walk-path.ts +34 -0
  49. 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,275 @@
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` 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.
17
+
18
+ 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.
19
+
20
+ 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.
21
+
22
+ 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.
23
+
24
+ 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.
25
+
26
+ You may use any supported resource argument to fetch content:
27
+
28
+ - [CID](https://multiformats.github.io/js-multiformats/classes/cid.CID.html) instance
29
+ - IPFS URL
30
+ - IPNS URL
31
+
32
+ ## Example - Getting started
33
+
34
+ ```typescript
35
+ import { verifiedFetch } from '@helia/verified-fetch'
36
+
37
+ const resp = await verifiedFetch('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 { verifiedFetch } from '@helia/verified-fetch'
46
+ import { CID } from 'multiformats/cid'
47
+
48
+ const cid = CID.parse('bafyFoo') // some image file
49
+ const response = await verifiedFetch(cid)
50
+ const json = await response.json()
51
+ ```
52
+
53
+ ## Example - Using IPFS protocol to fetch an image
54
+
55
+ ```typescript
56
+ import { verifiedFetch } from '@helia/verified-fetch'
57
+
58
+ const response = await verifiedFetch('ipfs://bafyFoo') // CID for some image file
59
+ const blob = await response.blob()
60
+ const image = document.createElement('img')
61
+ image.src = URL.createObjectURL(blob)
62
+ document.body.appendChild(image)
63
+ ```
64
+
65
+ ## Example - Using IPNS protocol to stream a big file
66
+
67
+ ```typescript
68
+ import { verifiedFetch } from '@helia/verified-fetch'
69
+
70
+ const response = await verifiedFetch('ipns://mydomain.com/path/to/very-long-file.log')
71
+ const bigFileStreamReader = await response.body.getReader()
72
+ ```
73
+
74
+ ## Configuration
75
+
76
+ ### Custom HTTP gateways and routers
77
+
78
+ 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.
79
+
80
+ It's possible to override these by passing `gateways` and `routers` keys to the `createVerifiedFetch` function:
81
+
82
+ ## Example - Configuring gateways and routers
83
+
84
+ ```typescript
85
+ import { createVerifiedFetch } from '@helia/verified-fetch'
86
+
87
+ const fetch = await createVerifiedFetch({
88
+ gateways: ['https://trustless-gateway.link'],
89
+ routers: ['http://delegated-ipfs.dev']
90
+ })
91
+
92
+ const resp = await fetch('ipfs://bafy...')
93
+
94
+ const json = await resp.json()
95
+ ```
96
+
97
+ ### Usage with customized Helia
98
+
99
+ For full control of how `@helia/verified-fetch` fetches content from the distributed web you can pass a preconfigured Helia node to `createVerifiedFetch`.
100
+
101
+ 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.
102
+
103
+ You can see variations of Helia and js-libp2p configuration options at <https://helia.io/interfaces/helia.index.HeliaInit.html>.
104
+
105
+ ```typescript
106
+ import { trustlessGateway } from '@helia/block-brokers'
107
+ import { createHeliaHTTP } from '@helia/http'
108
+ import { delegatedHTTPRouting } from '@helia/routers'
109
+ import { createVerifiedFetch } from '@helia/verified-fetch'
110
+
111
+ const fetch = await createVerifiedFetch(
112
+ await createHeliaHTTP({
113
+ blockBrokers: [
114
+ trustlessGateway({
115
+ gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
116
+ })
117
+ ],
118
+ routers: ['http://delegated-ipfs.dev'].map((routerUrl) => delegatedHTTPRouting(routerUrl))
119
+ })
120
+ )
121
+
122
+ const resp = await fetch('ipfs://bafy...')
123
+
124
+ const json = await resp.json()
125
+ ```
126
+
127
+ ## Comparison to fetch
128
+
129
+ This module attempts to act as similarly to the `fetch()` API as possible.
130
+
131
+ [The `fetch()` API](https://developer.mozilla.org/en-US/docs/Web/API/fetch) takes two parameters:
132
+
133
+ 1. A [resource](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource)
134
+ 2. An [options object](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
135
+
136
+ ### Resource argument
137
+
138
+ This library supports the following methods of fetching web3 content from IPFS:
139
+
140
+ 1. IPFS protocol: `ipfs://<cidv0>` & `ipfs://<cidv0>`
141
+ 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
142
+ 3. CID instances: An actual CID instance `CID.parse('bafy...')`
143
+
144
+ 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).
145
+
146
+ 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.
147
+
148
+ ### Options argument
149
+
150
+ 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.
151
+
152
+ Some of those header specifications are:
153
+
154
+ 1. <https://specs.ipfs.tech/http-gateways/path-gateway/#request-headers>
155
+ 2. <https://specs.ipfs.tech/http-gateways/trustless-gateway/#request-headers>
156
+ 3. <https://specs.ipfs.tech/http-gateways/subdomain-gateway/#request-headers>
157
+
158
+ 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.
159
+
160
+ Known Fetch API options that will be supported:
161
+
162
+ 1. `signal` - An AbortSignal that a user can use to abort the request.
163
+ 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.
164
+ 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.
165
+ - `accept` - A string that specifies the accept header. Relevant values:
166
+ - [`vnd.ipld.raw`](https://www.iana.org/assignments/media-types/application/vnd.ipld.raw). (default)
167
+ - [`vnd.ipld.car`](https://www.iana.org/assignments/media-types/application/vnd.ipld.car)
168
+ - [`vnd.ipfs.ipns-record`](https://www.iana.org/assignments/media-types/application/vnd.ipfs.ipns-record)
169
+ 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.
170
+ 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.
171
+ 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.
172
+
173
+ Non-Fetch API options that will be supported:
174
+
175
+ 1. `onProgress` - Similar to Helia `onProgress` options, this will be a function that will be called with a progress event. Supported progress events are:
176
+ - `helia:verified-fetch:error` - An error occurred during the request.
177
+ - `helia:verified-fetch:request:start` - The request has been sent
178
+ - `helia:verified-fetch:request:complete` - The request has been sent
179
+ - `helia:verified-fetch:request:error` - An error occurred during the request.
180
+ - `helia:verified-fetch:request:abort` - The request was aborted prior to completion.
181
+ - `helia:verified-fetch:response:start` - The initial HTTP Response headers have been set, and response stream is started.
182
+ - `helia:verified-fetch:response:complete` - The response stream has completed.
183
+ - `helia:verified-fetch:response:error` - An error occurred while building the response.
184
+
185
+ 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:
186
+
187
+ 1. [IPIP-0412: Signaling Block Order in CARs on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0412/)
188
+ 2. [IPIP-0402: Partial CAR Support on Trustless Gateways](https://specs.ipfs.tech/ipips/ipip-0402/)
189
+ 3. [IPIP-0386: Subdomain Gateway Interop with \_redirects](https://specs.ipfs.tech/ipips/ipip-0386/)
190
+ 4. [IPIP-0328: JSON and CBOR Response Formats on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0328/)
191
+ 5. [IPIP-0288: TAR Response Format on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0288/)
192
+
193
+ ### Response types
194
+
195
+ 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.
196
+
197
+ 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.
198
+
199
+ 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.
200
+
201
+ #### Handling response types
202
+
203
+ For handling responses we want to follow conventions/abstractions from Fetch API where possible:
204
+
205
+ - 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.
206
+ - For images (or other web-relevant asset) you want to add to the DOM, use `.blob()` or `.arrayBuffer()` to get the raw bytes.
207
+ - For plain text in utf-8, you would call `.text()`
208
+ - 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).
209
+
210
+ #### Unsupported response types
211
+
212
+ - 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.
213
+ - Others? Open an issue or PR!
214
+
215
+ ### Response headers
216
+
217
+ 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/).
218
+
219
+ Some known header specifications:
220
+
221
+ - <https://specs.ipfs.tech/http-gateways/path-gateway/#response-headers>
222
+ - <https://specs.ipfs.tech/http-gateways/trustless-gateway/#response-headers>
223
+ - <https://specs.ipfs.tech/http-gateways/subdomain-gateway/#response-headers>
224
+
225
+ ### Possible Scenarios that could cause confusion
226
+
227
+ #### Attempting to fetch the CID for content that does not make sense
228
+
229
+ If you request `bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze`, which points to the root of the en.wikipedia.org mirror, a response object does not make sense.
230
+
231
+ ### Errors
232
+
233
+ Known Errors that can be thrown:
234
+
235
+ 1. `TypeError` - If the resource argument is not a string, CID, or CID string.
236
+ 2. `TypeError` - If the options argument is passed and not an object.
237
+ 3. `TypeError` - If the options argument is passed and is malformed.
238
+ 4. `AbortError` - If the content request is aborted due to user aborting provided AbortSignal.
239
+
240
+ # Install
241
+
242
+ ```console
243
+ $ npm i @helia/verified-fetch
244
+ ```
245
+
246
+ ## Browser `<script>` tag
247
+
248
+ Loading this module through a script tag will make it's exports available as `HeliaVerifiedFetch` in the global namespace.
249
+
250
+ ```html
251
+ <script src="https://unpkg.com/@helia/verified-fetch/dist/index.min.js"></script>
252
+ ```
253
+
254
+ # API Docs
255
+
256
+ - <https://ipfs.github.io/helia/modules/_helia_verified_fetch.html>
257
+
258
+ # License
259
+
260
+ Licensed under either of
261
+
262
+ - Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
263
+ - MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
264
+
265
+ # Contribute
266
+
267
+ Contributions welcome! Please check out [the issues](https://github.com/ipfs/helia/issues).
268
+
269
+ 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.
270
+
271
+ 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).
272
+
273
+ 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.
274
+
275
+ [![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md)