@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.
- package/LICENSE +4 -0
- package/README.md +262 -0
- package/dist/index.min.js +140 -0
- package/dist/src/index.d.ts +259 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +251 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/utils/get-content-type.d.ts +11 -0
- package/dist/src/utils/get-content-type.d.ts.map +1 -0
- package/dist/src/utils/get-content-type.js +43 -0
- package/dist/src/utils/get-content-type.js.map +1 -0
- package/dist/src/utils/get-stream-and-content-type.d.ts +10 -0
- package/dist/src/utils/get-stream-and-content-type.d.ts.map +1 -0
- package/dist/src/utils/get-stream-and-content-type.js +37 -0
- package/dist/src/utils/get-stream-and-content-type.js.map +1 -0
- package/dist/src/utils/parse-resource.d.ts +18 -0
- package/dist/src/utils/parse-resource.d.ts.map +1 -0
- package/dist/src/utils/parse-resource.js +24 -0
- package/dist/src/utils/parse-resource.js.map +1 -0
- package/dist/src/utils/parse-url-string.d.ts +26 -0
- package/dist/src/utils/parse-url-string.d.ts.map +1 -0
- package/dist/src/utils/parse-url-string.js +109 -0
- package/dist/src/utils/parse-url-string.js.map +1 -0
- package/dist/src/utils/tlru.d.ts +15 -0
- package/dist/src/utils/tlru.d.ts.map +1 -0
- package/dist/src/utils/tlru.js +40 -0
- package/dist/src/utils/tlru.js.map +1 -0
- package/dist/src/utils/walk-path.d.ts +13 -0
- package/dist/src/utils/walk-path.d.ts.map +1 -0
- package/dist/src/utils/walk-path.js +17 -0
- package/dist/src/utils/walk-path.js.map +1 -0
- package/dist/src/verified-fetch.d.ts +64 -0
- package/dist/src/verified-fetch.d.ts.map +1 -0
- package/dist/src/verified-fetch.js +261 -0
- package/dist/src/verified-fetch.js.map +1 -0
- package/package.json +176 -0
- package/src/index.ts +310 -0
- package/src/utils/get-content-type.ts +55 -0
- package/src/utils/get-stream-and-content-type.ts +44 -0
- package/src/utils/parse-resource.ts +40 -0
- package/src/utils/parse-url-string.ts +139 -0
- package/src/utils/tlru.ts +52 -0
- package/src/utils/walk-path.ts +34 -0
- package/src/verified-fetch.ts +323 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* `@helia/verified-fetch` is a library that provides a fetch-like API for fetching trustless content from IPFS and verifying it.
|
|
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.
|
|
7
|
+
*
|
|
8
|
+
* Exports a `createVerifiedFetch` function that returns a `fetch()` like API method {@link Helia} for fetching IPFS content.
|
|
9
|
+
*
|
|
10
|
+
* You may use any supported resource argument to fetch content:
|
|
11
|
+
*
|
|
12
|
+
* - CID instance
|
|
13
|
+
* - IPFS URL
|
|
14
|
+
* - IPNS URL
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
*
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
20
|
+
*
|
|
21
|
+
* const fetch = await createVerifiedFetch({
|
|
22
|
+
* gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
|
|
23
|
+
*})
|
|
24
|
+
*
|
|
25
|
+
* const resp = await fetch('ipfs://bafy...')
|
|
26
|
+
*
|
|
27
|
+
* const json = await resp.json()
|
|
28
|
+
*```
|
|
29
|
+
*
|
|
30
|
+
*
|
|
31
|
+
* @example Using a CID instance to fetch JSON
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
35
|
+
* import { CID } from 'multiformats/cid'
|
|
36
|
+
*
|
|
37
|
+
* const fetch = await createVerifiedFetch({
|
|
38
|
+
* gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
|
|
39
|
+
* })
|
|
40
|
+
*
|
|
41
|
+
* const cid = CID.parse('bafyFoo') // some image file
|
|
42
|
+
* const response = await fetch(cid)
|
|
43
|
+
* const json = await response.json()
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example Using IPFS protocol to fetch an image
|
|
47
|
+
*
|
|
48
|
+
* ```typescript
|
|
49
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
50
|
+
*
|
|
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
|
|
55
|
+
* const blob = await response.blob()
|
|
56
|
+
* const image = document.createElement('img')
|
|
57
|
+
* image.src = URL.createObjectURL(blob)
|
|
58
|
+
* document.body.appendChild(image)
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example Using IPNS protocol to stream a big file
|
|
62
|
+
*
|
|
63
|
+
* ```typescript
|
|
64
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
65
|
+
*
|
|
66
|
+
* const fetch = await createVerifiedFetch({
|
|
67
|
+
* gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
|
|
68
|
+
* })
|
|
69
|
+
* const response = await fetch('ipns://mydomain.com/path/to/very-long-file.log')
|
|
70
|
+
* const bigFileStreamReader = await response.body.getReader()
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* ### Configuration
|
|
74
|
+
*
|
|
75
|
+
* #### Usage with customized Helia
|
|
76
|
+
*
|
|
77
|
+
* You can see variations of Helia and js-libp2p configuration options at https://helia.io/interfaces/helia.index.HeliaInit.html.
|
|
78
|
+
*
|
|
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.
|
|
80
|
+
*
|
|
81
|
+
* ```typescript
|
|
82
|
+
* import { trustlessGateway } from '@helia/block-brokers'
|
|
83
|
+
* import { createHeliaHTTP } from '@helia/http'
|
|
84
|
+
* import { delegatedHTTPRouting } from '@helia/routers'
|
|
85
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
86
|
+
*
|
|
87
|
+
* const fetch = await createVerifiedFetch(
|
|
88
|
+
* await createHeliaHTTP({
|
|
89
|
+
* blockBrokers: [
|
|
90
|
+
* trustlessGateway({
|
|
91
|
+
* gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
|
|
92
|
+
* })
|
|
93
|
+
* ],
|
|
94
|
+
* routers: ['http://delegated-ipfs.dev'].map((routerUrl) => delegatedHTTPRouting(routerUrl))
|
|
95
|
+
* })
|
|
96
|
+
* )
|
|
97
|
+
*
|
|
98
|
+
* const resp = await fetch('ipfs://bafy...')
|
|
99
|
+
*
|
|
100
|
+
* const json = await resp.json()
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* ### Comparison to fetch
|
|
104
|
+
*
|
|
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.
|
|
106
|
+
*
|
|
107
|
+
* [The `fetch()` API](https://developer.mozilla.org/en-US/docs/Web/API/fetch) takes two parameters:
|
|
108
|
+
*
|
|
109
|
+
* 1. A [resource](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource)
|
|
110
|
+
* 2. An [options object](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
|
|
111
|
+
*
|
|
112
|
+
* #### Resource argument
|
|
113
|
+
*
|
|
114
|
+
* This library intends to support the following methods of fetching web3 content from IPFS:
|
|
115
|
+
*
|
|
116
|
+
* 1. IPFS protocol: `ipfs://<cidv0>` & `ipfs://<cidv0>`
|
|
117
|
+
* 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
|
|
118
|
+
* 3. CID instances: An actual CID instance `CID.parse('bafy...')`
|
|
119
|
+
*
|
|
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).
|
|
121
|
+
*
|
|
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.
|
|
123
|
+
*
|
|
124
|
+
* #### Options argument
|
|
125
|
+
*
|
|
126
|
+
* 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
|
+
*
|
|
128
|
+
* Some of those header specifications are:
|
|
129
|
+
*
|
|
130
|
+
* 1. https://specs.ipfs.tech/http-gateways/path-gateway/#request-headers
|
|
131
|
+
* 2. https://specs.ipfs.tech/http-gateways/trustless-gateway/#request-headers
|
|
132
|
+
* 3. https://specs.ipfs.tech/http-gateways/subdomain-gateway/#request-headers
|
|
133
|
+
*
|
|
134
|
+
* 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.
|
|
135
|
+
*
|
|
136
|
+
* Known Fetch API options that will be supported:
|
|
137
|
+
*
|
|
138
|
+
* 1. `signal` - An AbortSignal that a user can use to abort the request.
|
|
139
|
+
* 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.
|
|
140
|
+
* 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.
|
|
141
|
+
* - `accept` - A string that specifies the accept header. Relevant values:
|
|
142
|
+
* - [`vnd.ipld.raw`](https://www.iana.org/assignments/media-types/application/vnd.ipld.raw). (default)
|
|
143
|
+
* - [`vnd.ipld.car`](https://www.iana.org/assignments/media-types/application/vnd.ipld.car)
|
|
144
|
+
* - [`vnd.ipfs.ipns-record`](https://www.iana.org/assignments/media-types/application/vnd.ipfs.ipns-record)
|
|
145
|
+
* 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.
|
|
146
|
+
* 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
|
+
* 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
|
+
*
|
|
149
|
+
*
|
|
150
|
+
* Non-Fetch API options that will be supported:
|
|
151
|
+
*
|
|
152
|
+
* 1. `onProgress` - Similar to Helia `onProgress` options, this will be a function that will be called with a progress event. Supported progress events are:
|
|
153
|
+
* - `helia:verified-fetch:error` - An error occurred during the request.
|
|
154
|
+
* - `helia:verified-fetch:request:start` - The request has been sent
|
|
155
|
+
* - `helia:verified-fetch:request:complete` - The request has been sent
|
|
156
|
+
* - `helia:verified-fetch:request:error` - An error occurred during the request.
|
|
157
|
+
* - `helia:verified-fetch:request:abort` - The request was aborted prior to completion.
|
|
158
|
+
* - `helia:verified-fetch:response:start` - The initial HTTP Response headers have been set, and response stream is started.
|
|
159
|
+
* - `helia:verified-fetch:response:complete` - The response stream has completed.
|
|
160
|
+
* - `helia:verified-fetch:response:error` - An error occurred while building the response.
|
|
161
|
+
*
|
|
162
|
+
* 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:
|
|
163
|
+
*
|
|
164
|
+
* 1. [IPIP-0412: Signaling Block Order in CARs on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0412/)
|
|
165
|
+
* 2. [IPIP-0402: Partial CAR Support on Trustless Gateways](https://specs.ipfs.tech/ipips/ipip-0402/)
|
|
166
|
+
* 3. [IPIP-0386: Subdomain Gateway Interop with _redirects](https://specs.ipfs.tech/ipips/ipip-0386/)
|
|
167
|
+
* 4. [IPIP-0328: JSON and CBOR Response Formats on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0328/)
|
|
168
|
+
* 5. [IPIP-0288: TAR Response Format on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0288/)
|
|
169
|
+
*
|
|
170
|
+
* #### Response types
|
|
171
|
+
*
|
|
172
|
+
* 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
|
+
*
|
|
174
|
+
* 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.
|
|
175
|
+
*
|
|
176
|
+
* 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
|
+
*
|
|
178
|
+
* ##### Handling response types
|
|
179
|
+
*
|
|
180
|
+
* For handling responses we want to follow conventions/abstractions from Fetch API where possible:
|
|
181
|
+
*
|
|
182
|
+
* - 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.
|
|
183
|
+
* - For images (or other web-relevant asset) you want to add to the DOM, use `.blob()` or `.arrayBuffer()` to get the raw bytes.
|
|
184
|
+
* - For plain text in utf-8, you would call `.text()`
|
|
185
|
+
* - 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
|
+
*
|
|
187
|
+
* ##### Unsupported response types
|
|
188
|
+
*
|
|
189
|
+
* * 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
|
+
* * Others? Open an issue or PR!
|
|
191
|
+
*
|
|
192
|
+
* #### Response headers
|
|
193
|
+
*
|
|
194
|
+
* 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
|
+
*
|
|
196
|
+
* Some known header specifications:
|
|
197
|
+
*
|
|
198
|
+
* * https://specs.ipfs.tech/http-gateways/path-gateway/#response-headers
|
|
199
|
+
* * https://specs.ipfs.tech/http-gateways/trustless-gateway/#response-headers
|
|
200
|
+
* * https://specs.ipfs.tech/http-gateways/subdomain-gateway/#response-headers
|
|
201
|
+
*
|
|
202
|
+
* #### Possible Scenarios that could cause confusion
|
|
203
|
+
*
|
|
204
|
+
* ##### Attempting to fetch the CID for content that does not make sense
|
|
205
|
+
*
|
|
206
|
+
* If you request `bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze`, which points to the root of the en.wikipedia.org mirror, a response object does not make sense.
|
|
207
|
+
*
|
|
208
|
+
* #### Errors
|
|
209
|
+
*
|
|
210
|
+
* Known Errors that can be thrown:
|
|
211
|
+
*
|
|
212
|
+
* 1. `TypeError` - If the resource argument is not a string, CID, or CID string.
|
|
213
|
+
* 2. `TypeError` - If the options argument is passed and not an object.
|
|
214
|
+
* 3. `TypeError` - If the options argument is passed and is malformed.
|
|
215
|
+
* 4. `AbortError` - If the content request is aborted due to user aborting provided AbortSignal.
|
|
216
|
+
*/
|
|
217
|
+
import type { Helia } from '@helia/interface';
|
|
218
|
+
import type { IPNSRoutingEvents, ResolveDnsLinkProgressEvents, ResolveProgressEvents } from '@helia/ipns';
|
|
219
|
+
import type { GetEvents } from '@helia/unixfs';
|
|
220
|
+
import type { CID } from 'multiformats/cid';
|
|
221
|
+
import type { ProgressEvent, ProgressOptions } from 'progress-events';
|
|
222
|
+
/**
|
|
223
|
+
* The types for the first argument of the `verifiedFetch` function.
|
|
224
|
+
*/
|
|
225
|
+
export type Resource = string | CID;
|
|
226
|
+
export interface CIDDetail {
|
|
227
|
+
cid: string;
|
|
228
|
+
path: string;
|
|
229
|
+
}
|
|
230
|
+
export interface CIDDetailError extends CIDDetail {
|
|
231
|
+
error: Error;
|
|
232
|
+
}
|
|
233
|
+
export interface VerifiedFetch {
|
|
234
|
+
(resource: Resource, options?: VerifiedFetchInit): Promise<Response>;
|
|
235
|
+
start(): Promise<void>;
|
|
236
|
+
stop(): Promise<void>;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
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.
|
|
240
|
+
*/
|
|
241
|
+
export interface CreateVerifiedFetchWithOptions {
|
|
242
|
+
gateways: string[];
|
|
243
|
+
routers?: string[];
|
|
244
|
+
}
|
|
245
|
+
export type BubbledProgressEvents = GetEvents | ResolveProgressEvents | ResolveDnsLinkProgressEvents | IPNSRoutingEvents;
|
|
246
|
+
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
|
+
/**
|
|
248
|
+
* Options for the `fetch` function returned by `createVerifiedFetch`.
|
|
249
|
+
*
|
|
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.
|
|
252
|
+
*/
|
|
253
|
+
export interface VerifiedFetchInit extends RequestInit, ProgressOptions<BubbledProgressEvents | VerifiedFetchProgressEvents> {
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Create and return a Helia node
|
|
257
|
+
*/
|
|
258
|
+
export declare function createVerifiedFetch(init: Helia | CreateVerifiedFetchWithOptions): Promise<VerifiedFetch>;
|
|
259
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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,WAAW,SAAS;IACxB,GAAG,EAAE,MAAM,CAAA;IACX,IAAI,EAAE,MAAM,CAAA;CACb;AAED,MAAM,WAAW,cAAe,SAAQ,SAAS;IAC/C,KAAK,EAAE,KAAK,CAAA;CACb;AAED,MAAM,WAAW,aAAa;IAC5B,CAAC,QAAQ,EAAE,QAAQ,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAA;IACpE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;IACtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,8BAA8B;IAC7C,QAAQ,EAAE,MAAM,EAAE,CAAA;IAClB,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;CACnB;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;;;;;GAKG;AACH,MAAM,WAAW,iBAAkB,SAAQ,WAAW,EAAE,eAAe,CAAC,qBAAqB,GAAG,2BAA2B,CAAC;CAC3H;AAED;;GAEG;AACH,wBAAsB,mBAAmB,CAAE,IAAI,EAAE,KAAK,GAAG,8BAA8B,GAAG,OAAO,CAAC,aAAa,CAAC,CAoB/G"}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* `@helia/verified-fetch` is a library that provides a fetch-like API for fetching trustless content from IPFS and verifying it.
|
|
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.
|
|
7
|
+
*
|
|
8
|
+
* Exports a `createVerifiedFetch` function that returns a `fetch()` like API method {@link Helia} for fetching IPFS content.
|
|
9
|
+
*
|
|
10
|
+
* You may use any supported resource argument to fetch content:
|
|
11
|
+
*
|
|
12
|
+
* - CID instance
|
|
13
|
+
* - IPFS URL
|
|
14
|
+
* - IPNS URL
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
*
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
20
|
+
*
|
|
21
|
+
* const fetch = await createVerifiedFetch({
|
|
22
|
+
* gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
|
|
23
|
+
*})
|
|
24
|
+
*
|
|
25
|
+
* const resp = await fetch('ipfs://bafy...')
|
|
26
|
+
*
|
|
27
|
+
* const json = await resp.json()
|
|
28
|
+
*```
|
|
29
|
+
*
|
|
30
|
+
*
|
|
31
|
+
* @example Using a CID instance to fetch JSON
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
35
|
+
* import { CID } from 'multiformats/cid'
|
|
36
|
+
*
|
|
37
|
+
* const fetch = await createVerifiedFetch({
|
|
38
|
+
* gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
|
|
39
|
+
* })
|
|
40
|
+
*
|
|
41
|
+
* const cid = CID.parse('bafyFoo') // some image file
|
|
42
|
+
* const response = await fetch(cid)
|
|
43
|
+
* const json = await response.json()
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example Using IPFS protocol to fetch an image
|
|
47
|
+
*
|
|
48
|
+
* ```typescript
|
|
49
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
50
|
+
*
|
|
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
|
|
55
|
+
* const blob = await response.blob()
|
|
56
|
+
* const image = document.createElement('img')
|
|
57
|
+
* image.src = URL.createObjectURL(blob)
|
|
58
|
+
* document.body.appendChild(image)
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* @example Using IPNS protocol to stream a big file
|
|
62
|
+
*
|
|
63
|
+
* ```typescript
|
|
64
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
65
|
+
*
|
|
66
|
+
* const fetch = await createVerifiedFetch({
|
|
67
|
+
* gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
|
|
68
|
+
* })
|
|
69
|
+
* const response = await fetch('ipns://mydomain.com/path/to/very-long-file.log')
|
|
70
|
+
* const bigFileStreamReader = await response.body.getReader()
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* ### Configuration
|
|
74
|
+
*
|
|
75
|
+
* #### Usage with customized Helia
|
|
76
|
+
*
|
|
77
|
+
* You can see variations of Helia and js-libp2p configuration options at https://helia.io/interfaces/helia.index.HeliaInit.html.
|
|
78
|
+
*
|
|
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.
|
|
80
|
+
*
|
|
81
|
+
* ```typescript
|
|
82
|
+
* import { trustlessGateway } from '@helia/block-brokers'
|
|
83
|
+
* import { createHeliaHTTP } from '@helia/http'
|
|
84
|
+
* import { delegatedHTTPRouting } from '@helia/routers'
|
|
85
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
86
|
+
*
|
|
87
|
+
* const fetch = await createVerifiedFetch(
|
|
88
|
+
* await createHeliaHTTP({
|
|
89
|
+
* blockBrokers: [
|
|
90
|
+
* trustlessGateway({
|
|
91
|
+
* gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
|
|
92
|
+
* })
|
|
93
|
+
* ],
|
|
94
|
+
* routers: ['http://delegated-ipfs.dev'].map((routerUrl) => delegatedHTTPRouting(routerUrl))
|
|
95
|
+
* })
|
|
96
|
+
* )
|
|
97
|
+
*
|
|
98
|
+
* const resp = await fetch('ipfs://bafy...')
|
|
99
|
+
*
|
|
100
|
+
* const json = await resp.json()
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* ### Comparison to fetch
|
|
104
|
+
*
|
|
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.
|
|
106
|
+
*
|
|
107
|
+
* [The `fetch()` API](https://developer.mozilla.org/en-US/docs/Web/API/fetch) takes two parameters:
|
|
108
|
+
*
|
|
109
|
+
* 1. A [resource](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource)
|
|
110
|
+
* 2. An [options object](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
|
|
111
|
+
*
|
|
112
|
+
* #### Resource argument
|
|
113
|
+
*
|
|
114
|
+
* This library intends to support the following methods of fetching web3 content from IPFS:
|
|
115
|
+
*
|
|
116
|
+
* 1. IPFS protocol: `ipfs://<cidv0>` & `ipfs://<cidv0>`
|
|
117
|
+
* 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
|
|
118
|
+
* 3. CID instances: An actual CID instance `CID.parse('bafy...')`
|
|
119
|
+
*
|
|
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).
|
|
121
|
+
*
|
|
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.
|
|
123
|
+
*
|
|
124
|
+
* #### Options argument
|
|
125
|
+
*
|
|
126
|
+
* 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
|
+
*
|
|
128
|
+
* Some of those header specifications are:
|
|
129
|
+
*
|
|
130
|
+
* 1. https://specs.ipfs.tech/http-gateways/path-gateway/#request-headers
|
|
131
|
+
* 2. https://specs.ipfs.tech/http-gateways/trustless-gateway/#request-headers
|
|
132
|
+
* 3. https://specs.ipfs.tech/http-gateways/subdomain-gateway/#request-headers
|
|
133
|
+
*
|
|
134
|
+
* 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.
|
|
135
|
+
*
|
|
136
|
+
* Known Fetch API options that will be supported:
|
|
137
|
+
*
|
|
138
|
+
* 1. `signal` - An AbortSignal that a user can use to abort the request.
|
|
139
|
+
* 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.
|
|
140
|
+
* 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.
|
|
141
|
+
* - `accept` - A string that specifies the accept header. Relevant values:
|
|
142
|
+
* - [`vnd.ipld.raw`](https://www.iana.org/assignments/media-types/application/vnd.ipld.raw). (default)
|
|
143
|
+
* - [`vnd.ipld.car`](https://www.iana.org/assignments/media-types/application/vnd.ipld.car)
|
|
144
|
+
* - [`vnd.ipfs.ipns-record`](https://www.iana.org/assignments/media-types/application/vnd.ipfs.ipns-record)
|
|
145
|
+
* 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.
|
|
146
|
+
* 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
|
+
* 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
|
+
*
|
|
149
|
+
*
|
|
150
|
+
* Non-Fetch API options that will be supported:
|
|
151
|
+
*
|
|
152
|
+
* 1. `onProgress` - Similar to Helia `onProgress` options, this will be a function that will be called with a progress event. Supported progress events are:
|
|
153
|
+
* - `helia:verified-fetch:error` - An error occurred during the request.
|
|
154
|
+
* - `helia:verified-fetch:request:start` - The request has been sent
|
|
155
|
+
* - `helia:verified-fetch:request:complete` - The request has been sent
|
|
156
|
+
* - `helia:verified-fetch:request:error` - An error occurred during the request.
|
|
157
|
+
* - `helia:verified-fetch:request:abort` - The request was aborted prior to completion.
|
|
158
|
+
* - `helia:verified-fetch:response:start` - The initial HTTP Response headers have been set, and response stream is started.
|
|
159
|
+
* - `helia:verified-fetch:response:complete` - The response stream has completed.
|
|
160
|
+
* - `helia:verified-fetch:response:error` - An error occurred while building the response.
|
|
161
|
+
*
|
|
162
|
+
* 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:
|
|
163
|
+
*
|
|
164
|
+
* 1. [IPIP-0412: Signaling Block Order in CARs on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0412/)
|
|
165
|
+
* 2. [IPIP-0402: Partial CAR Support on Trustless Gateways](https://specs.ipfs.tech/ipips/ipip-0402/)
|
|
166
|
+
* 3. [IPIP-0386: Subdomain Gateway Interop with _redirects](https://specs.ipfs.tech/ipips/ipip-0386/)
|
|
167
|
+
* 4. [IPIP-0328: JSON and CBOR Response Formats on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0328/)
|
|
168
|
+
* 5. [IPIP-0288: TAR Response Format on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0288/)
|
|
169
|
+
*
|
|
170
|
+
* #### Response types
|
|
171
|
+
*
|
|
172
|
+
* 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
|
+
*
|
|
174
|
+
* 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.
|
|
175
|
+
*
|
|
176
|
+
* 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
|
+
*
|
|
178
|
+
* ##### Handling response types
|
|
179
|
+
*
|
|
180
|
+
* For handling responses we want to follow conventions/abstractions from Fetch API where possible:
|
|
181
|
+
*
|
|
182
|
+
* - 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.
|
|
183
|
+
* - For images (or other web-relevant asset) you want to add to the DOM, use `.blob()` or `.arrayBuffer()` to get the raw bytes.
|
|
184
|
+
* - For plain text in utf-8, you would call `.text()`
|
|
185
|
+
* - 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
|
+
*
|
|
187
|
+
* ##### Unsupported response types
|
|
188
|
+
*
|
|
189
|
+
* * 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
|
+
* * Others? Open an issue or PR!
|
|
191
|
+
*
|
|
192
|
+
* #### Response headers
|
|
193
|
+
*
|
|
194
|
+
* 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
|
+
*
|
|
196
|
+
* Some known header specifications:
|
|
197
|
+
*
|
|
198
|
+
* * https://specs.ipfs.tech/http-gateways/path-gateway/#response-headers
|
|
199
|
+
* * https://specs.ipfs.tech/http-gateways/trustless-gateway/#response-headers
|
|
200
|
+
* * https://specs.ipfs.tech/http-gateways/subdomain-gateway/#response-headers
|
|
201
|
+
*
|
|
202
|
+
* #### Possible Scenarios that could cause confusion
|
|
203
|
+
*
|
|
204
|
+
* ##### Attempting to fetch the CID for content that does not make sense
|
|
205
|
+
*
|
|
206
|
+
* If you request `bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze`, which points to the root of the en.wikipedia.org mirror, a response object does not make sense.
|
|
207
|
+
*
|
|
208
|
+
* #### Errors
|
|
209
|
+
*
|
|
210
|
+
* Known Errors that can be thrown:
|
|
211
|
+
*
|
|
212
|
+
* 1. `TypeError` - If the resource argument is not a string, CID, or CID string.
|
|
213
|
+
* 2. `TypeError` - If the options argument is passed and not an object.
|
|
214
|
+
* 3. `TypeError` - If the options argument is passed and is malformed.
|
|
215
|
+
* 4. `AbortError` - If the content request is aborted due to user aborting provided AbortSignal.
|
|
216
|
+
*/
|
|
217
|
+
import { trustlessGateway } from '@helia/block-brokers';
|
|
218
|
+
import { createHeliaHTTP } from '@helia/http';
|
|
219
|
+
import { delegatedHTTPRouting } from '@helia/routers';
|
|
220
|
+
import { VerifiedFetch as VerifiedFetchClass } from './verified-fetch.js';
|
|
221
|
+
/**
|
|
222
|
+
* Create and return a Helia node
|
|
223
|
+
*/
|
|
224
|
+
export async function createVerifiedFetch(init) {
|
|
225
|
+
if (!isHelia(init)) {
|
|
226
|
+
init = await createHeliaHTTP({
|
|
227
|
+
blockBrokers: [
|
|
228
|
+
trustlessGateway({
|
|
229
|
+
gateways: init.gateways
|
|
230
|
+
})
|
|
231
|
+
],
|
|
232
|
+
routers: init.routers?.map((routerUrl) => delegatedHTTPRouting(routerUrl))
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
const verifiedFetchInstance = new VerifiedFetchClass({ helia: init });
|
|
236
|
+
async function verifiedFetch(resource, options) {
|
|
237
|
+
return verifiedFetchInstance.fetch(resource, options);
|
|
238
|
+
}
|
|
239
|
+
verifiedFetch.stop = verifiedFetchInstance.stop.bind(verifiedFetchInstance);
|
|
240
|
+
verifiedFetch.start = verifiedFetchInstance.start.bind(verifiedFetchInstance);
|
|
241
|
+
return verifiedFetch;
|
|
242
|
+
}
|
|
243
|
+
function isHelia(obj) {
|
|
244
|
+
// test for the presence of known Helia properties, return a boolean value
|
|
245
|
+
return obj?.blockstore != null &&
|
|
246
|
+
obj?.datastore != null &&
|
|
247
|
+
obj?.gc != null &&
|
|
248
|
+
obj?.stop != null &&
|
|
249
|
+
obj?.start != null;
|
|
250
|
+
}
|
|
251
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,IAA4C;IACrF,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACnB,IAAI,GAAG,MAAM,eAAe,CAAC;YAC3B,YAAY,EAAE;gBACZ,gBAAgB,CAAC;oBACf,QAAQ,EAAE,IAAI,CAAC,QAAQ;iBACxB,CAAC;aACH;YACD,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,oBAAoB,CAAC,SAAS,CAAC,CAAC;SAC3E,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,qBAAqB,GAAG,IAAI,kBAAkB,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;IACrE,KAAK,UAAU,aAAa,CAAE,QAAkB,EAAE,OAA2B;QAC3E,OAAO,qBAAqB,CAAC,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;IACvD,CAAC;IACD,aAAa,CAAC,IAAI,GAAG,qBAAqB,CAAC,IAAI,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAA;IAC3E,aAAa,CAAC,KAAK,GAAG,qBAAqB,CAAC,KAAK,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAA;IAE7E,OAAO,aAAa,CAAA;AACtB,CAAC;AAED,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,11 @@
|
|
|
1
|
+
interface TestInput {
|
|
2
|
+
bytes: Uint8Array;
|
|
3
|
+
path: string;
|
|
4
|
+
}
|
|
5
|
+
export declare const DEFAULT_MIME_TYPE = "application/octet-stream";
|
|
6
|
+
/**
|
|
7
|
+
* Get the content type from the input based on the tests.
|
|
8
|
+
*/
|
|
9
|
+
export declare function getContentType(input: TestInput): Promise<string>;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=get-content-type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-content-type.d.ts","sourceRoot":"","sources":["../../../src/utils/get-content-type.ts"],"names":[],"mappings":"AAEA,UAAU,SAAS;IACjB,KAAK,EAAE,UAAU,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;CACb;AAID,eAAO,MAAM,iBAAiB,6BAA6B,CAAA;AAkC3D;;GAEG;AACH,wBAAsB,cAAc,CAAE,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,CAQvE"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import mime from 'mime-types';
|
|
2
|
+
export const DEFAULT_MIME_TYPE = 'application/octet-stream';
|
|
3
|
+
const xmlRegex = /^(<\?xml[^>]+>)?[^<^\w]+<svg/ig;
|
|
4
|
+
/**
|
|
5
|
+
* Tests to determine the content type of the input.
|
|
6
|
+
* The order is important on this one.
|
|
7
|
+
*/
|
|
8
|
+
const tests = [
|
|
9
|
+
// svg
|
|
10
|
+
async ({ bytes }) => xmlRegex.test(new TextDecoder().decode(bytes.slice(0, 64)))
|
|
11
|
+
? 'image/svg+xml'
|
|
12
|
+
: undefined,
|
|
13
|
+
// testing file-type from path
|
|
14
|
+
async ({ path }) => {
|
|
15
|
+
const mimeType = mime.lookup(path);
|
|
16
|
+
if (mimeType !== false) {
|
|
17
|
+
return mimeType;
|
|
18
|
+
}
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
];
|
|
22
|
+
const overrides = {
|
|
23
|
+
'video/quicktime': 'video/mp4'
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Override the content type based on overrides.
|
|
27
|
+
*/
|
|
28
|
+
function overrideContentType(type) {
|
|
29
|
+
return overrides[type] ?? type;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Get the content type from the input based on the tests.
|
|
33
|
+
*/
|
|
34
|
+
export async function getContentType(input) {
|
|
35
|
+
for (const test of tests) {
|
|
36
|
+
const type = await test(input);
|
|
37
|
+
if (type !== undefined) {
|
|
38
|
+
return overrideContentType(type);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return DEFAULT_MIME_TYPE;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=get-content-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-content-type.js","sourceRoot":"","sources":["../../../src/utils/get-content-type.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,YAAY,CAAA;AAS7B,MAAM,CAAC,MAAM,iBAAiB,GAAG,0BAA0B,CAAA;AAE3D,MAAM,QAAQ,GAAG,gCAAgC,CAAA;AAEjD;;;GAGG;AACH,MAAM,KAAK,GAA4C;IACrD,MAAM;IACN,KAAK,EAAE,EAAE,KAAK,EAAE,EAAc,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;QAC1F,CAAC,CAAC,eAAe;QACjB,CAAC,CAAC,SAAS;IACb,8BAA8B;IAC9B,KAAK,EAAE,EAAE,IAAI,EAAE,EAAc,EAAE;QAC7B,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAClC,IAAI,QAAQ,KAAK,KAAK,EAAE,CAAC;YACvB,OAAO,QAAQ,CAAA;QACjB,CAAC;QACD,OAAO,SAAS,CAAA;IAClB,CAAC;CACF,CAAA;AAED,MAAM,SAAS,GAA2B;IACxC,iBAAiB,EAAE,WAAW;CAC/B,CAAA;AAED;;GAEG;AACH,SAAS,mBAAmB,CAAE,IAAY;IACxC,OAAO,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,CAAA;AAChC,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAE,KAAgB;IACpD,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,CAAA;QAC9B,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,OAAO,mBAAmB,CAAC,IAAI,CAAC,CAAA;QAClC,CAAC;IACH,CAAC;IACD,OAAO,iBAAiB,CAAA;AAC1B,CAAC"}
|
|
@@ -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 attempts to determine the content type of those bytes.
|
|
5
|
+
*/
|
|
6
|
+
export declare function getStreamAndContentType(iterator: AsyncIterable<Uint8Array>, path: string, logger: ComponentLogger, options?: Pick<VerifiedFetchInit, 'onProgress'>): Promise<{
|
|
7
|
+
contentType: string;
|
|
8
|
+
stream: ReadableStream<Uint8Array>;
|
|
9
|
+
}>;
|
|
10
|
+
//# sourceMappingURL=get-stream-and-content-type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-stream-and-content-type.d.ts","sourceRoot":"","sources":["../../../src/utils/get-stream-and-content-type.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AACpD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAExD;;GAEG;AACH,wBAAsB,uBAAuB,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,WAAW,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,CAAA;CAAE,CAAC,CAmChP"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { CustomProgressEvent } from 'progress-events';
|
|
2
|
+
import { getContentType } from './get-content-type.js';
|
|
3
|
+
/**
|
|
4
|
+
* Converts an async iterator of Uint8Array bytes to a stream and attempts to determine the content type of those bytes.
|
|
5
|
+
*/
|
|
6
|
+
export async function getStreamAndContentType(iterator, path, logger, options) {
|
|
7
|
+
const log = logger.forComponent('helia:verified-fetch:get-stream-and-content-type');
|
|
8
|
+
const reader = iterator[Symbol.asyncIterator]();
|
|
9
|
+
const { value, done } = await reader.next();
|
|
10
|
+
if (done === true) {
|
|
11
|
+
log.error('No content found for path', path);
|
|
12
|
+
throw new Error('No content found');
|
|
13
|
+
}
|
|
14
|
+
const contentType = await getContentType({ bytes: value, path });
|
|
15
|
+
const stream = new ReadableStream({
|
|
16
|
+
async start(controller) {
|
|
17
|
+
// the initial value is already available
|
|
18
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:progress:chunk'));
|
|
19
|
+
controller.enqueue(value);
|
|
20
|
+
},
|
|
21
|
+
async pull(controller) {
|
|
22
|
+
const { value, done } = await reader.next();
|
|
23
|
+
if (done === true) {
|
|
24
|
+
if (value != null) {
|
|
25
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:progress:chunk'));
|
|
26
|
+
controller.enqueue(value);
|
|
27
|
+
}
|
|
28
|
+
controller.close();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
options?.onProgress?.(new CustomProgressEvent('verified-fetch:request:progress:chunk'));
|
|
32
|
+
controller.enqueue(value);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
return { contentType, stream };
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=get-stream-and-content-type.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-stream-and-content-type.js","sourceRoot":"","sources":["../../../src/utils/get-stream-and-content-type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AAItD;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAAE,QAAmC,EAAE,IAAY,EAAE,MAAuB,EAAE,OAA+C;IACxK,MAAM,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,kDAAkD,CAAC,CAAA;IACnF,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,CAAA;IAC/C,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;IAE3C,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;QAClB,GAAG,CAAC,KAAK,CAAC,2BAA2B,EAAE,IAAI,CAAC,CAAA;QAC5C,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAA;IACrC,CAAC;IAED,MAAM,WAAW,GAAG,MAAM,cAAc,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;IAChE,MAAM,MAAM,GAAG,IAAI,cAAc,CAAC;QAChC,KAAK,CAAC,KAAK,CAAE,UAAU;YACrB,yCAAyC;YACzC,OAAO,EAAE,UAAU,EAAE,CAAC,IAAI,mBAAmB,CAAO,uCAAuC,CAAC,CAAC,CAAA;YAC7F,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAC3B,CAAC;QACD,KAAK,CAAC,IAAI,CAAE,UAAU;YACpB,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;YAE3C,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;gBAClB,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;oBAClB,OAAO,EAAE,UAAU,EAAE,CAAC,IAAI,mBAAmB,CAAO,uCAAuC,CAAC,CAAC,CAAA;oBAC7F,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;gBAC3B,CAAC;gBACD,UAAU,CAAC,KAAK,EAAE,CAAA;gBAClB,OAAM;YACR,CAAC;YAED,OAAO,EAAE,UAAU,EAAE,CAAC,IAAI,mBAAmB,CAAO,uCAAuC,CAAC,CAAC,CAAA;YAC7F,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;QAC3B,CAAC;KACF,CAAC,CAAA;IAEF,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,CAAA;AAChC,CAAC"}
|