@helia/verified-fetch 0.0.0-5c0c39c → 0.0.0-7a7c0c1

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 +114 -52
  4. package/dist/src/index.d.ts.map +1 -1
  5. package/dist/src/index.js +87 -48
  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 +46 -18
  18. package/dist/src/verified-fetch.js.map +1 -1
  19. package/package.json +15 -14
  20. package/src/index.ts +120 -53
  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 +53 -22
  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
package/README.md CHANGED
@@ -13,28 +13,28 @@
13
13
 
14
14
  # About
15
15
 
16
- `@helia/verified-fetch` is a library that provides a fetch-like API for fetching trustless content from IPFS and verifying it.
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
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.
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
19
 
20
- Exports a `createVerifiedFetch` function that returns a `fetch()` like API method Helia for fetching IPFS content.
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.
21
25
 
22
26
  You may use any supported resource argument to fetch content:
23
27
 
24
- - CID instance
28
+ - [CID](https://multiformats.github.io/js-multiformats/classes/cid.CID.html) instance
25
29
  - IPFS URL
26
30
  - IPNS URL
27
31
 
28
- ## Example
32
+ ## Example - Getting started
29
33
 
30
34
  ```typescript
31
- import { createVerifiedFetch } from '@helia/verified-fetch'
35
+ import { verifiedFetch } from '@helia/verified-fetch'
32
36
 
33
- const fetch = await createVerifiedFetch({
34
- gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
35
- })
36
-
37
- const resp = await fetch('ipfs://bafy...')
37
+ const resp = await verifiedFetch('ipfs://bafy...')
38
38
 
39
39
  const json = await resp.json()
40
40
  ```
@@ -42,27 +42,20 @@ const json = await resp.json()
42
42
  ## Example - Using a CID instance to fetch JSON
43
43
 
44
44
  ```typescript
45
- import { createVerifiedFetch } from '@helia/verified-fetch'
45
+ import { verifiedFetch } from '@helia/verified-fetch'
46
46
  import { CID } from 'multiformats/cid'
47
47
 
48
- const fetch = await createVerifiedFetch({
49
- gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
50
- })
51
-
52
48
  const cid = CID.parse('bafyFoo') // some image file
53
- const response = await fetch(cid)
49
+ const response = await verifiedFetch(cid)
54
50
  const json = await response.json()
55
51
  ```
56
52
 
57
53
  ## Example - Using IPFS protocol to fetch an image
58
54
 
59
55
  ```typescript
60
- import { createVerifiedFetch } from '@helia/verified-fetch'
56
+ import { verifiedFetch } from '@helia/verified-fetch'
61
57
 
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
58
+ const response = await verifiedFetch('ipfs://bafyFoo') // CID for some image file
66
59
  const blob = await response.blob()
67
60
  const image = document.createElement('img')
68
61
  image.src = URL.createObjectURL(blob)
@@ -71,23 +64,43 @@ document.body.appendChild(image)
71
64
 
72
65
  ## Example - Using IPNS protocol to stream a big file
73
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
+
74
84
  ```typescript
75
85
  import { createVerifiedFetch } from '@helia/verified-fetch'
76
86
 
77
87
  const fetch = await createVerifiedFetch({
78
- gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
88
+ gateways: ['https://trustless-gateway.link'],
89
+ routers: ['http://delegated-ipfs.dev']
79
90
  })
80
- const response = await fetch('ipns://mydomain.com/path/to/very-long-file.log')
81
- const bigFileStreamReader = await response.body.getReader()
91
+
92
+ const resp = await fetch('ipfs://bafy...')
93
+
94
+ const json = await resp.json()
82
95
  ```
83
96
 
84
- ### Configuration
97
+ ### Usage with customized Helia
85
98
 
86
- #### Usage with customized Helia
99
+ For full control of how `@helia/verified-fetch` fetches content from the distributed web you can pass a preconfigured Helia node to `createVerifiedFetch`.
87
100
 
88
- You can see variations of Helia and js-libp2p configuration options at <https://helia.io/interfaces/helia.index.HeliaInit.html>.
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.
89
102
 
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.
103
+ You can see variations of Helia and js-libp2p configuration options at <https://helia.io/interfaces/helia.index.HeliaInit.html>.
91
104
 
92
105
  ```typescript
93
106
  import { trustlessGateway } from '@helia/block-brokers'
@@ -111,28 +124,52 @@ const resp = await fetch('ipfs://bafy...')
111
124
  const json = await resp.json()
112
125
  ```
113
126
 
114
- ### Comparison to fetch
127
+ ### Custom content-type parsing
128
+
129
+ 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.
130
+
131
+ If you require an accurate content-type you can provide a `contentTypeParser` function as an option to `createVerifiedFetch` to handle parsing the content type.
132
+
133
+ 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.
134
+
135
+ ## Example - Customizing content-type parsing
136
+
137
+ ```typescript
138
+ import { createVerifiedFetch } from '@helia/verified-fetch'
139
+ import { fileTypeFromBuffer } from '@sgtpooki/file-type'
140
+
141
+ const fetch = await createVerifiedFetch({
142
+ gateways: ['https://trustless-gateway.link'],
143
+ routers: ['http://delegated-ipfs.dev'],
144
+ contentTypeParser: async (bytes) => {
145
+ // call to some magic-byte recognition library like magic-bytes, file-type, or your own custom byte recognition
146
+ return fileTypeFromBuffer(bytes)?.mime
147
+ }
148
+ })
149
+ ```
150
+
151
+ ## Comparison to fetch
115
152
 
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.
153
+ This module attempts to act as similarly to the `fetch()` API as possible.
117
154
 
118
155
  [The `fetch()` API](https://developer.mozilla.org/en-US/docs/Web/API/fetch) takes two parameters:
119
156
 
120
157
  1. A [resource](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource)
121
158
  2. An [options object](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
122
159
 
123
- #### Resource argument
160
+ ### Resource argument
124
161
 
125
- This library intends to support the following methods of fetching web3 content from IPFS:
162
+ This library supports the following methods of fetching web3 content from IPFS:
126
163
 
127
164
  1. IPFS protocol: `ipfs://<cidv0>` & `ipfs://<cidv0>`
128
165
  2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
129
166
  3. CID instances: An actual CID instance `CID.parse('bafy...')`
130
167
 
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).
168
+ 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
169
 
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.
170
+ 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.
134
171
 
135
- #### Options argument
172
+ ### Options argument
136
173
 
137
174
  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
175
 
@@ -177,7 +214,7 @@ Some in-flight specs (IPIPs) that will affect the options object this library su
177
214
  4. [IPIP-0328: JSON and CBOR Response Formats on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0328/)
178
215
  5. [IPIP-0288: TAR Response Format on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0288/)
179
216
 
180
- #### Response types
217
+ ### Response types
181
218
 
182
219
  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
220
 
@@ -185,7 +222,7 @@ All content we retrieve from the IPFS network is obtained via an AsyncIterable,
185
222
 
186
223
  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
224
 
188
- ##### Handling response types
225
+ #### Handling response types
189
226
 
190
227
  For handling responses we want to follow conventions/abstractions from Fetch API where possible:
191
228
 
@@ -194,12 +231,12 @@ For handling responses we want to follow conventions/abstractions from Fetch API
194
231
  - For plain text in utf-8, you would call `.text()`
195
232
  - 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
233
 
197
- ##### Unsupported response types
234
+ #### Unsupported response types
198
235
 
199
236
  - 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
237
  - Others? Open an issue or PR!
201
238
 
202
- #### Response headers
239
+ ### Response headers
203
240
 
204
241
  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
242
 
@@ -209,13 +246,13 @@ Some known header specifications:
209
246
  - <https://specs.ipfs.tech/http-gateways/trustless-gateway/#response-headers>
210
247
  - <https://specs.ipfs.tech/http-gateways/subdomain-gateway/#response-headers>
211
248
 
212
- #### Possible Scenarios that could cause confusion
249
+ ### Possible Scenarios that could cause confusion
213
250
 
214
- ##### Attempting to fetch the CID for content that does not make sense
251
+ #### Attempting to fetch the CID for content that does not make sense
215
252
 
216
253
  If you request `bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze`, which points to the root of the en.wikipedia.org mirror, a response object does not make sense.
217
254
 
218
- #### Errors
255
+ ### Errors
219
256
 
220
257
  Known Errors that can be thrown:
221
258