@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.
- package/LICENSE +4 -0
- package/README.md +275 -0
- package/dist/index.min.js +140 -0
- package/dist/src/index.d.ts +271 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +263 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/singleton.d.ts +3 -0
- package/dist/src/singleton.d.ts.map +1 -0
- package/dist/src/singleton.js +15 -0
- package/dist/src/singleton.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 +175 -0
- package/src/index.ts +323 -0
- package/src/singleton.ts +20 -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,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* `@helia/verified-fetch` provides a [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)-like API for retrieving content from the [IPFS](https://ipfs.tech/) network.
|
|
5
|
+
*
|
|
6
|
+
* All content is retrieved in a [trustless manner](https://www.techopedia.com/definition/trustless), and the integrity of all bytes are verified by comparing hashes of the data.
|
|
7
|
+
*
|
|
8
|
+
* This is a marked improvement over `fetch` which offers no such protections and is vulnerable to all sorts of attacks like [Content Spoofing](https://owasp.org/www-community/attacks/Content_Spoofing), [DNS Hijacking](https://en.wikipedia.org/wiki/DNS_hijacking), etc.
|
|
9
|
+
*
|
|
10
|
+
* A `verifiedFetch` function is exported to get up and running quickly, and a `createVerifiedFetch` function is also available that allows customizing the underlying [Helia](https://helia.io/) node for complete control over how content is retrieved.
|
|
11
|
+
*
|
|
12
|
+
* Browser-cache-friendly [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects are returned which should be instantly familiar to web developers.
|
|
13
|
+
*
|
|
14
|
+
* You may use any supported resource argument to fetch content:
|
|
15
|
+
*
|
|
16
|
+
* - [CID](https://multiformats.github.io/js-multiformats/classes/cid.CID.html) instance
|
|
17
|
+
* - IPFS URL
|
|
18
|
+
* - IPNS URL
|
|
19
|
+
*
|
|
20
|
+
* @example Getting started
|
|
21
|
+
*
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
24
|
+
*
|
|
25
|
+
* const resp = await verifiedFetch('ipfs://bafy...')
|
|
26
|
+
*
|
|
27
|
+
* const json = await resp.json()
|
|
28
|
+
*```
|
|
29
|
+
*
|
|
30
|
+
* @example Using a CID instance to fetch JSON
|
|
31
|
+
*
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
34
|
+
* import { CID } from 'multiformats/cid'
|
|
35
|
+
*
|
|
36
|
+
* const cid = CID.parse('bafyFoo') // some image file
|
|
37
|
+
* const response = await verifiedFetch(cid)
|
|
38
|
+
* const json = await response.json()
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @example Using IPFS protocol to fetch an image
|
|
42
|
+
*
|
|
43
|
+
* ```typescript
|
|
44
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
45
|
+
*
|
|
46
|
+
* const response = await verifiedFetch('ipfs://bafyFoo') // CID for some image file
|
|
47
|
+
* const blob = await response.blob()
|
|
48
|
+
* const image = document.createElement('img')
|
|
49
|
+
* image.src = URL.createObjectURL(blob)
|
|
50
|
+
* document.body.appendChild(image)
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example Using IPNS protocol to stream a big file
|
|
54
|
+
*
|
|
55
|
+
* ```typescript
|
|
56
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
57
|
+
*
|
|
58
|
+
* const response = await verifiedFetch('ipns://mydomain.com/path/to/very-long-file.log')
|
|
59
|
+
* const bigFileStreamReader = await response.body.getReader()
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* ## Configuration
|
|
63
|
+
*
|
|
64
|
+
* ### Custom HTTP gateways and routers
|
|
65
|
+
*
|
|
66
|
+
* Out of the box `@helia/verified-fetch` uses a default set of [trustless gateways](https://specs.ipfs.tech/http-gateways/trustless-gateway/) for fetching blocks and [HTTP delegated routers](https://specs.ipfs.tech/routing/http-routing-v1/) for performing routing tasks - looking up peers, resolving/publishing [IPNS](https://docs.ipfs.tech/concepts/ipns/) names, etc.
|
|
67
|
+
*
|
|
68
|
+
* It's possible to override these by passing `gateways` and `routers` keys to the `createVerifiedFetch` function:
|
|
69
|
+
*
|
|
70
|
+
* @example Configuring gateways and routers
|
|
71
|
+
*
|
|
72
|
+
* ```typescript
|
|
73
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
74
|
+
*
|
|
75
|
+
* const fetch = await createVerifiedFetch({
|
|
76
|
+
* gateways: ['https://trustless-gateway.link'],
|
|
77
|
+
* routers: ['http://delegated-ipfs.dev']
|
|
78
|
+
*})
|
|
79
|
+
*
|
|
80
|
+
* const resp = await fetch('ipfs://bafy...')
|
|
81
|
+
*
|
|
82
|
+
* const json = await resp.json()
|
|
83
|
+
*```
|
|
84
|
+
*
|
|
85
|
+
* ### Usage with customized Helia
|
|
86
|
+
*
|
|
87
|
+
* For full control of how `@helia/verified-fetch` fetches content from the distributed web you can pass a preconfigured Helia node to `createVerifiedFetch`.
|
|
88
|
+
*
|
|
89
|
+
* The [helia](https://www.npmjs.com/package/helia) module is configured with a libp2p node that is suited for decentralized applications, alternatively [@helia/http](https://www.npmjs.com/package/@helia/http) is available which uses HTTP gateways for all network operations.
|
|
90
|
+
*
|
|
91
|
+
* You can see variations of Helia and js-libp2p configuration options at https://helia.io/interfaces/helia.index.HeliaInit.html.
|
|
92
|
+
*
|
|
93
|
+
* ```typescript
|
|
94
|
+
* import { trustlessGateway } from '@helia/block-brokers'
|
|
95
|
+
* import { createHeliaHTTP } from '@helia/http'
|
|
96
|
+
* import { delegatedHTTPRouting } from '@helia/routers'
|
|
97
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
98
|
+
*
|
|
99
|
+
* const fetch = await createVerifiedFetch(
|
|
100
|
+
* await createHeliaHTTP({
|
|
101
|
+
* blockBrokers: [
|
|
102
|
+
* trustlessGateway({
|
|
103
|
+
* gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
|
|
104
|
+
* })
|
|
105
|
+
* ],
|
|
106
|
+
* routers: ['http://delegated-ipfs.dev'].map((routerUrl) => delegatedHTTPRouting(routerUrl))
|
|
107
|
+
* })
|
|
108
|
+
* )
|
|
109
|
+
*
|
|
110
|
+
* const resp = await fetch('ipfs://bafy...')
|
|
111
|
+
*
|
|
112
|
+
* const json = await resp.json()
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* ## Comparison to fetch
|
|
116
|
+
*
|
|
117
|
+
* This module attempts to act as similarly to the `fetch()` API as possible.
|
|
118
|
+
*
|
|
119
|
+
* [The `fetch()` API](https://developer.mozilla.org/en-US/docs/Web/API/fetch) takes two parameters:
|
|
120
|
+
*
|
|
121
|
+
* 1. A [resource](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource)
|
|
122
|
+
* 2. An [options object](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
|
|
123
|
+
*
|
|
124
|
+
* ### Resource argument
|
|
125
|
+
*
|
|
126
|
+
* This library supports the following methods of fetching web3 content from IPFS:
|
|
127
|
+
*
|
|
128
|
+
* 1. IPFS protocol: `ipfs://<cidv0>` & `ipfs://<cidv0>`
|
|
129
|
+
* 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
|
|
130
|
+
* 3. CID instances: An actual CID instance `CID.parse('bafy...')`
|
|
131
|
+
*
|
|
132
|
+
* 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).
|
|
133
|
+
*
|
|
134
|
+
* 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.
|
|
135
|
+
*
|
|
136
|
+
* ### Options argument
|
|
137
|
+
*
|
|
138
|
+
* 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.
|
|
139
|
+
*
|
|
140
|
+
* Some of those header specifications are:
|
|
141
|
+
*
|
|
142
|
+
* 1. https://specs.ipfs.tech/http-gateways/path-gateway/#request-headers
|
|
143
|
+
* 2. https://specs.ipfs.tech/http-gateways/trustless-gateway/#request-headers
|
|
144
|
+
* 3. https://specs.ipfs.tech/http-gateways/subdomain-gateway/#request-headers
|
|
145
|
+
*
|
|
146
|
+
* 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.
|
|
147
|
+
*
|
|
148
|
+
* Known Fetch API options that will be supported:
|
|
149
|
+
*
|
|
150
|
+
* 1. `signal` - An AbortSignal that a user can use to abort the request.
|
|
151
|
+
* 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.
|
|
152
|
+
* 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.
|
|
153
|
+
* - `accept` - A string that specifies the accept header. Relevant values:
|
|
154
|
+
* - [`vnd.ipld.raw`](https://www.iana.org/assignments/media-types/application/vnd.ipld.raw). (default)
|
|
155
|
+
* - [`vnd.ipld.car`](https://www.iana.org/assignments/media-types/application/vnd.ipld.car)
|
|
156
|
+
* - [`vnd.ipfs.ipns-record`](https://www.iana.org/assignments/media-types/application/vnd.ipfs.ipns-record)
|
|
157
|
+
* 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.
|
|
158
|
+
* 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.
|
|
159
|
+
* 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.
|
|
160
|
+
*
|
|
161
|
+
* Non-Fetch API options that will be supported:
|
|
162
|
+
*
|
|
163
|
+
* 1. `onProgress` - Similar to Helia `onProgress` options, this will be a function that will be called with a progress event. Supported progress events are:
|
|
164
|
+
* - `helia:verified-fetch:error` - An error occurred during the request.
|
|
165
|
+
* - `helia:verified-fetch:request:start` - The request has been sent
|
|
166
|
+
* - `helia:verified-fetch:request:complete` - The request has been sent
|
|
167
|
+
* - `helia:verified-fetch:request:error` - An error occurred during the request.
|
|
168
|
+
* - `helia:verified-fetch:request:abort` - The request was aborted prior to completion.
|
|
169
|
+
* - `helia:verified-fetch:response:start` - The initial HTTP Response headers have been set, and response stream is started.
|
|
170
|
+
* - `helia:verified-fetch:response:complete` - The response stream has completed.
|
|
171
|
+
* - `helia:verified-fetch:response:error` - An error occurred while building the response.
|
|
172
|
+
*
|
|
173
|
+
* 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:
|
|
174
|
+
*
|
|
175
|
+
* 1. [IPIP-0412: Signaling Block Order in CARs on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0412/)
|
|
176
|
+
* 2. [IPIP-0402: Partial CAR Support on Trustless Gateways](https://specs.ipfs.tech/ipips/ipip-0402/)
|
|
177
|
+
* 3. [IPIP-0386: Subdomain Gateway Interop with _redirects](https://specs.ipfs.tech/ipips/ipip-0386/)
|
|
178
|
+
* 4. [IPIP-0328: JSON and CBOR Response Formats on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0328/)
|
|
179
|
+
* 5. [IPIP-0288: TAR Response Format on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0288/)
|
|
180
|
+
*
|
|
181
|
+
* ### Response types
|
|
182
|
+
*
|
|
183
|
+
* 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.
|
|
184
|
+
*
|
|
185
|
+
* 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.
|
|
186
|
+
*
|
|
187
|
+
* 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.
|
|
188
|
+
*
|
|
189
|
+
* #### Handling response types
|
|
190
|
+
*
|
|
191
|
+
* For handling responses we want to follow conventions/abstractions from Fetch API where possible:
|
|
192
|
+
*
|
|
193
|
+
* - 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.
|
|
194
|
+
* - For images (or other web-relevant asset) you want to add to the DOM, use `.blob()` or `.arrayBuffer()` to get the raw bytes.
|
|
195
|
+
* - For plain text in utf-8, you would call `.text()`
|
|
196
|
+
* - 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).
|
|
197
|
+
*
|
|
198
|
+
* #### Unsupported response types
|
|
199
|
+
*
|
|
200
|
+
* * 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.
|
|
201
|
+
* * Others? Open an issue or PR!
|
|
202
|
+
*
|
|
203
|
+
* ### Response headers
|
|
204
|
+
*
|
|
205
|
+
* 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/).
|
|
206
|
+
*
|
|
207
|
+
* Some known header specifications:
|
|
208
|
+
*
|
|
209
|
+
* * https://specs.ipfs.tech/http-gateways/path-gateway/#response-headers
|
|
210
|
+
* * https://specs.ipfs.tech/http-gateways/trustless-gateway/#response-headers
|
|
211
|
+
* * https://specs.ipfs.tech/http-gateways/subdomain-gateway/#response-headers
|
|
212
|
+
*
|
|
213
|
+
* ### Possible Scenarios that could cause confusion
|
|
214
|
+
*
|
|
215
|
+
* #### Attempting to fetch the CID for content that does not make sense
|
|
216
|
+
*
|
|
217
|
+
* If you request `bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze`, which points to the root of the en.wikipedia.org mirror, a response object does not make sense.
|
|
218
|
+
*
|
|
219
|
+
* ### Errors
|
|
220
|
+
*
|
|
221
|
+
* Known Errors that can be thrown:
|
|
222
|
+
*
|
|
223
|
+
* 1. `TypeError` - If the resource argument is not a string, CID, or CID string.
|
|
224
|
+
* 2. `TypeError` - If the options argument is passed and not an object.
|
|
225
|
+
* 3. `TypeError` - If the options argument is passed and is malformed.
|
|
226
|
+
* 4. `AbortError` - If the content request is aborted due to user aborting provided AbortSignal.
|
|
227
|
+
*/
|
|
228
|
+
import type { Helia } from '@helia/interface';
|
|
229
|
+
import type { IPNSRoutingEvents, ResolveDnsLinkProgressEvents, ResolveProgressEvents } from '@helia/ipns';
|
|
230
|
+
import type { GetEvents } from '@helia/unixfs';
|
|
231
|
+
import type { CID } from 'multiformats/cid';
|
|
232
|
+
import type { ProgressEvent, ProgressOptions } from 'progress-events';
|
|
233
|
+
/**
|
|
234
|
+
* The types for the first argument of the `verifiedFetch` function.
|
|
235
|
+
*/
|
|
236
|
+
export type Resource = string | CID;
|
|
237
|
+
export interface CIDDetail {
|
|
238
|
+
cid: string;
|
|
239
|
+
path: string;
|
|
240
|
+
}
|
|
241
|
+
export interface CIDDetailError extends CIDDetail {
|
|
242
|
+
error: Error;
|
|
243
|
+
}
|
|
244
|
+
export interface VerifiedFetch {
|
|
245
|
+
(resource: Resource, options?: VerifiedFetchInit): Promise<Response>;
|
|
246
|
+
start(): Promise<void>;
|
|
247
|
+
stop(): Promise<void>;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Instead of passing a Helia instance, you can pass a list of gateways and routers, and a HeliaHTTP instance will be created for you.
|
|
251
|
+
*/
|
|
252
|
+
export interface CreateVerifiedFetchWithOptions {
|
|
253
|
+
gateways: string[];
|
|
254
|
+
routers?: string[];
|
|
255
|
+
}
|
|
256
|
+
export type BubbledProgressEvents = GetEvents | ResolveProgressEvents | ResolveDnsLinkProgressEvents | IPNSRoutingEvents;
|
|
257
|
+
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>;
|
|
258
|
+
/**
|
|
259
|
+
* Options for the `fetch` function returned by `createVerifiedFetch`.
|
|
260
|
+
*
|
|
261
|
+
* This method accepts all the same options as the `fetch` function in the browser, plus an `onProgress` option to
|
|
262
|
+
* listen for progress events.
|
|
263
|
+
*/
|
|
264
|
+
export interface VerifiedFetchInit extends RequestInit, ProgressOptions<BubbledProgressEvents | VerifiedFetchProgressEvents> {
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Create and return a Helia node
|
|
268
|
+
*/
|
|
269
|
+
export declare function createVerifiedFetch(init?: Helia | CreateVerifiedFetchWithOptions): Promise<VerifiedFetch>;
|
|
270
|
+
export { verifiedFetch } from './singleton.js';
|
|
271
|
+
//# source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,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,CAAC,EAAE,KAAK,GAAG,8BAA8B,GAAG,OAAO,CAAC,aAAa,CAAC,CAoBhH;AAED,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA"}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
*
|
|
4
|
+
* `@helia/verified-fetch` provides a [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)-like API for retrieving content from the [IPFS](https://ipfs.tech/) network.
|
|
5
|
+
*
|
|
6
|
+
* All content is retrieved in a [trustless manner](https://www.techopedia.com/definition/trustless), and the integrity of all bytes are verified by comparing hashes of the data.
|
|
7
|
+
*
|
|
8
|
+
* This is a marked improvement over `fetch` which offers no such protections and is vulnerable to all sorts of attacks like [Content Spoofing](https://owasp.org/www-community/attacks/Content_Spoofing), [DNS Hijacking](https://en.wikipedia.org/wiki/DNS_hijacking), etc.
|
|
9
|
+
*
|
|
10
|
+
* A `verifiedFetch` function is exported to get up and running quickly, and a `createVerifiedFetch` function is also available that allows customizing the underlying [Helia](https://helia.io/) node for complete control over how content is retrieved.
|
|
11
|
+
*
|
|
12
|
+
* Browser-cache-friendly [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) objects are returned which should be instantly familiar to web developers.
|
|
13
|
+
*
|
|
14
|
+
* You may use any supported resource argument to fetch content:
|
|
15
|
+
*
|
|
16
|
+
* - [CID](https://multiformats.github.io/js-multiformats/classes/cid.CID.html) instance
|
|
17
|
+
* - IPFS URL
|
|
18
|
+
* - IPNS URL
|
|
19
|
+
*
|
|
20
|
+
* @example Getting started
|
|
21
|
+
*
|
|
22
|
+
* ```typescript
|
|
23
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
24
|
+
*
|
|
25
|
+
* const resp = await verifiedFetch('ipfs://bafy...')
|
|
26
|
+
*
|
|
27
|
+
* const json = await resp.json()
|
|
28
|
+
*```
|
|
29
|
+
*
|
|
30
|
+
* @example Using a CID instance to fetch JSON
|
|
31
|
+
*
|
|
32
|
+
* ```typescript
|
|
33
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
34
|
+
* import { CID } from 'multiformats/cid'
|
|
35
|
+
*
|
|
36
|
+
* const cid = CID.parse('bafyFoo') // some image file
|
|
37
|
+
* const response = await verifiedFetch(cid)
|
|
38
|
+
* const json = await response.json()
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @example Using IPFS protocol to fetch an image
|
|
42
|
+
*
|
|
43
|
+
* ```typescript
|
|
44
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
45
|
+
*
|
|
46
|
+
* const response = await verifiedFetch('ipfs://bafyFoo') // CID for some image file
|
|
47
|
+
* const blob = await response.blob()
|
|
48
|
+
* const image = document.createElement('img')
|
|
49
|
+
* image.src = URL.createObjectURL(blob)
|
|
50
|
+
* document.body.appendChild(image)
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example Using IPNS protocol to stream a big file
|
|
54
|
+
*
|
|
55
|
+
* ```typescript
|
|
56
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
57
|
+
*
|
|
58
|
+
* const response = await verifiedFetch('ipns://mydomain.com/path/to/very-long-file.log')
|
|
59
|
+
* const bigFileStreamReader = await response.body.getReader()
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* ## Configuration
|
|
63
|
+
*
|
|
64
|
+
* ### Custom HTTP gateways and routers
|
|
65
|
+
*
|
|
66
|
+
* Out of the box `@helia/verified-fetch` uses a default set of [trustless gateways](https://specs.ipfs.tech/http-gateways/trustless-gateway/) for fetching blocks and [HTTP delegated routers](https://specs.ipfs.tech/routing/http-routing-v1/) for performing routing tasks - looking up peers, resolving/publishing [IPNS](https://docs.ipfs.tech/concepts/ipns/) names, etc.
|
|
67
|
+
*
|
|
68
|
+
* It's possible to override these by passing `gateways` and `routers` keys to the `createVerifiedFetch` function:
|
|
69
|
+
*
|
|
70
|
+
* @example Configuring gateways and routers
|
|
71
|
+
*
|
|
72
|
+
* ```typescript
|
|
73
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
74
|
+
*
|
|
75
|
+
* const fetch = await createVerifiedFetch({
|
|
76
|
+
* gateways: ['https://trustless-gateway.link'],
|
|
77
|
+
* routers: ['http://delegated-ipfs.dev']
|
|
78
|
+
*})
|
|
79
|
+
*
|
|
80
|
+
* const resp = await fetch('ipfs://bafy...')
|
|
81
|
+
*
|
|
82
|
+
* const json = await resp.json()
|
|
83
|
+
*```
|
|
84
|
+
*
|
|
85
|
+
* ### Usage with customized Helia
|
|
86
|
+
*
|
|
87
|
+
* For full control of how `@helia/verified-fetch` fetches content from the distributed web you can pass a preconfigured Helia node to `createVerifiedFetch`.
|
|
88
|
+
*
|
|
89
|
+
* The [helia](https://www.npmjs.com/package/helia) module is configured with a libp2p node that is suited for decentralized applications, alternatively [@helia/http](https://www.npmjs.com/package/@helia/http) is available which uses HTTP gateways for all network operations.
|
|
90
|
+
*
|
|
91
|
+
* You can see variations of Helia and js-libp2p configuration options at https://helia.io/interfaces/helia.index.HeliaInit.html.
|
|
92
|
+
*
|
|
93
|
+
* ```typescript
|
|
94
|
+
* import { trustlessGateway } from '@helia/block-brokers'
|
|
95
|
+
* import { createHeliaHTTP } from '@helia/http'
|
|
96
|
+
* import { delegatedHTTPRouting } from '@helia/routers'
|
|
97
|
+
* import { createVerifiedFetch } from '@helia/verified-fetch'
|
|
98
|
+
*
|
|
99
|
+
* const fetch = await createVerifiedFetch(
|
|
100
|
+
* await createHeliaHTTP({
|
|
101
|
+
* blockBrokers: [
|
|
102
|
+
* trustlessGateway({
|
|
103
|
+
* gateways: ['https://mygateway.example.net', 'https://trustless-gateway.link']
|
|
104
|
+
* })
|
|
105
|
+
* ],
|
|
106
|
+
* routers: ['http://delegated-ipfs.dev'].map((routerUrl) => delegatedHTTPRouting(routerUrl))
|
|
107
|
+
* })
|
|
108
|
+
* )
|
|
109
|
+
*
|
|
110
|
+
* const resp = await fetch('ipfs://bafy...')
|
|
111
|
+
*
|
|
112
|
+
* const json = await resp.json()
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* ## Comparison to fetch
|
|
116
|
+
*
|
|
117
|
+
* This module attempts to act as similarly to the `fetch()` API as possible.
|
|
118
|
+
*
|
|
119
|
+
* [The `fetch()` API](https://developer.mozilla.org/en-US/docs/Web/API/fetch) takes two parameters:
|
|
120
|
+
*
|
|
121
|
+
* 1. A [resource](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource)
|
|
122
|
+
* 2. An [options object](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options)
|
|
123
|
+
*
|
|
124
|
+
* ### Resource argument
|
|
125
|
+
*
|
|
126
|
+
* This library supports the following methods of fetching web3 content from IPFS:
|
|
127
|
+
*
|
|
128
|
+
* 1. IPFS protocol: `ipfs://<cidv0>` & `ipfs://<cidv0>`
|
|
129
|
+
* 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
|
|
130
|
+
* 3. CID instances: An actual CID instance `CID.parse('bafy...')`
|
|
131
|
+
*
|
|
132
|
+
* 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).
|
|
133
|
+
*
|
|
134
|
+
* 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.
|
|
135
|
+
*
|
|
136
|
+
* ### Options argument
|
|
137
|
+
*
|
|
138
|
+
* 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.
|
|
139
|
+
*
|
|
140
|
+
* Some of those header specifications are:
|
|
141
|
+
*
|
|
142
|
+
* 1. https://specs.ipfs.tech/http-gateways/path-gateway/#request-headers
|
|
143
|
+
* 2. https://specs.ipfs.tech/http-gateways/trustless-gateway/#request-headers
|
|
144
|
+
* 3. https://specs.ipfs.tech/http-gateways/subdomain-gateway/#request-headers
|
|
145
|
+
*
|
|
146
|
+
* 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.
|
|
147
|
+
*
|
|
148
|
+
* Known Fetch API options that will be supported:
|
|
149
|
+
*
|
|
150
|
+
* 1. `signal` - An AbortSignal that a user can use to abort the request.
|
|
151
|
+
* 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.
|
|
152
|
+
* 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.
|
|
153
|
+
* - `accept` - A string that specifies the accept header. Relevant values:
|
|
154
|
+
* - [`vnd.ipld.raw`](https://www.iana.org/assignments/media-types/application/vnd.ipld.raw). (default)
|
|
155
|
+
* - [`vnd.ipld.car`](https://www.iana.org/assignments/media-types/application/vnd.ipld.car)
|
|
156
|
+
* - [`vnd.ipfs.ipns-record`](https://www.iana.org/assignments/media-types/application/vnd.ipfs.ipns-record)
|
|
157
|
+
* 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.
|
|
158
|
+
* 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.
|
|
159
|
+
* 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.
|
|
160
|
+
*
|
|
161
|
+
* Non-Fetch API options that will be supported:
|
|
162
|
+
*
|
|
163
|
+
* 1. `onProgress` - Similar to Helia `onProgress` options, this will be a function that will be called with a progress event. Supported progress events are:
|
|
164
|
+
* - `helia:verified-fetch:error` - An error occurred during the request.
|
|
165
|
+
* - `helia:verified-fetch:request:start` - The request has been sent
|
|
166
|
+
* - `helia:verified-fetch:request:complete` - The request has been sent
|
|
167
|
+
* - `helia:verified-fetch:request:error` - An error occurred during the request.
|
|
168
|
+
* - `helia:verified-fetch:request:abort` - The request was aborted prior to completion.
|
|
169
|
+
* - `helia:verified-fetch:response:start` - The initial HTTP Response headers have been set, and response stream is started.
|
|
170
|
+
* - `helia:verified-fetch:response:complete` - The response stream has completed.
|
|
171
|
+
* - `helia:verified-fetch:response:error` - An error occurred while building the response.
|
|
172
|
+
*
|
|
173
|
+
* 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:
|
|
174
|
+
*
|
|
175
|
+
* 1. [IPIP-0412: Signaling Block Order in CARs on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0412/)
|
|
176
|
+
* 2. [IPIP-0402: Partial CAR Support on Trustless Gateways](https://specs.ipfs.tech/ipips/ipip-0402/)
|
|
177
|
+
* 3. [IPIP-0386: Subdomain Gateway Interop with _redirects](https://specs.ipfs.tech/ipips/ipip-0386/)
|
|
178
|
+
* 4. [IPIP-0328: JSON and CBOR Response Formats on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0328/)
|
|
179
|
+
* 5. [IPIP-0288: TAR Response Format on HTTP Gateways](https://specs.ipfs.tech/ipips/ipip-0288/)
|
|
180
|
+
*
|
|
181
|
+
* ### Response types
|
|
182
|
+
*
|
|
183
|
+
* 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.
|
|
184
|
+
*
|
|
185
|
+
* 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.
|
|
186
|
+
*
|
|
187
|
+
* 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.
|
|
188
|
+
*
|
|
189
|
+
* #### Handling response types
|
|
190
|
+
*
|
|
191
|
+
* For handling responses we want to follow conventions/abstractions from Fetch API where possible:
|
|
192
|
+
*
|
|
193
|
+
* - 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.
|
|
194
|
+
* - For images (or other web-relevant asset) you want to add to the DOM, use `.blob()` or `.arrayBuffer()` to get the raw bytes.
|
|
195
|
+
* - For plain text in utf-8, you would call `.text()`
|
|
196
|
+
* - 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).
|
|
197
|
+
*
|
|
198
|
+
* #### Unsupported response types
|
|
199
|
+
*
|
|
200
|
+
* * 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.
|
|
201
|
+
* * Others? Open an issue or PR!
|
|
202
|
+
*
|
|
203
|
+
* ### Response headers
|
|
204
|
+
*
|
|
205
|
+
* 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/).
|
|
206
|
+
*
|
|
207
|
+
* Some known header specifications:
|
|
208
|
+
*
|
|
209
|
+
* * https://specs.ipfs.tech/http-gateways/path-gateway/#response-headers
|
|
210
|
+
* * https://specs.ipfs.tech/http-gateways/trustless-gateway/#response-headers
|
|
211
|
+
* * https://specs.ipfs.tech/http-gateways/subdomain-gateway/#response-headers
|
|
212
|
+
*
|
|
213
|
+
* ### Possible Scenarios that could cause confusion
|
|
214
|
+
*
|
|
215
|
+
* #### Attempting to fetch the CID for content that does not make sense
|
|
216
|
+
*
|
|
217
|
+
* If you request `bafybeiaysi4s6lnjev27ln5icwm6tueaw2vdykrtjkwiphwekaywqhcjze`, which points to the root of the en.wikipedia.org mirror, a response object does not make sense.
|
|
218
|
+
*
|
|
219
|
+
* ### Errors
|
|
220
|
+
*
|
|
221
|
+
* Known Errors that can be thrown:
|
|
222
|
+
*
|
|
223
|
+
* 1. `TypeError` - If the resource argument is not a string, CID, or CID string.
|
|
224
|
+
* 2. `TypeError` - If the options argument is passed and not an object.
|
|
225
|
+
* 3. `TypeError` - If the options argument is passed and is malformed.
|
|
226
|
+
* 4. `AbortError` - If the content request is aborted due to user aborting provided AbortSignal.
|
|
227
|
+
*/
|
|
228
|
+
import { trustlessGateway } from '@helia/block-brokers';
|
|
229
|
+
import { createHeliaHTTP } from '@helia/http';
|
|
230
|
+
import { delegatedHTTPRouting } from '@helia/routers';
|
|
231
|
+
import { VerifiedFetch as VerifiedFetchClass } from './verified-fetch.js';
|
|
232
|
+
/**
|
|
233
|
+
* Create and return a Helia node
|
|
234
|
+
*/
|
|
235
|
+
export async function createVerifiedFetch(init) {
|
|
236
|
+
if (!isHelia(init)) {
|
|
237
|
+
init = await createHeliaHTTP({
|
|
238
|
+
blockBrokers: [
|
|
239
|
+
trustlessGateway({
|
|
240
|
+
gateways: init?.gateways
|
|
241
|
+
})
|
|
242
|
+
],
|
|
243
|
+
routers: (init?.routers ?? ['https://delegated-ipfs.dev']).map((routerUrl) => delegatedHTTPRouting(routerUrl))
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
const verifiedFetchInstance = new VerifiedFetchClass({ helia: init });
|
|
247
|
+
async function verifiedFetch(resource, options) {
|
|
248
|
+
return verifiedFetchInstance.fetch(resource, options);
|
|
249
|
+
}
|
|
250
|
+
verifiedFetch.stop = verifiedFetchInstance.stop.bind(verifiedFetchInstance);
|
|
251
|
+
verifiedFetch.start = verifiedFetchInstance.start.bind(verifiedFetchInstance);
|
|
252
|
+
return verifiedFetch;
|
|
253
|
+
}
|
|
254
|
+
export { verifiedFetch } from './singleton.js';
|
|
255
|
+
function isHelia(obj) {
|
|
256
|
+
// test for the presence of known Helia properties, return a boolean value
|
|
257
|
+
return obj?.blockstore != null &&
|
|
258
|
+
obj?.datastore != null &&
|
|
259
|
+
obj?.gc != null &&
|
|
260
|
+
obj?.stop != null &&
|
|
261
|
+
obj?.start != null;
|
|
262
|
+
}
|
|
263
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @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,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,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAE9C,SAAS,OAAO,CAAE,GAAQ;IACxB,0EAA0E;IAC1E,OAAO,GAAG,EAAE,UAAU,IAAI,IAAI;QAC5B,GAAG,EAAE,SAAS,IAAI,IAAI;QACtB,GAAG,EAAE,EAAE,IAAI,IAAI;QACf,GAAG,EAAE,IAAI,IAAI,IAAI;QACjB,GAAG,EAAE,KAAK,IAAI,IAAI,CAAA;AACtB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"singleton.d.ts","sourceRoot":"","sources":["../../src/singleton.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAY,aAAa,EAAqB,MAAM,YAAY,CAAA;AAI5E,eAAO,MAAM,aAAa,EAAE,aAM3B,CAAA"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createVerifiedFetch } from './index.js';
|
|
2
|
+
let impl;
|
|
3
|
+
export const verifiedFetch = async function verifiedFetch(resource, options) {
|
|
4
|
+
if (impl == null) {
|
|
5
|
+
impl = await createVerifiedFetch();
|
|
6
|
+
}
|
|
7
|
+
return impl(resource, options);
|
|
8
|
+
};
|
|
9
|
+
verifiedFetch.start = async function () {
|
|
10
|
+
await impl?.start();
|
|
11
|
+
};
|
|
12
|
+
verifiedFetch.stop = async function () {
|
|
13
|
+
await impl?.stop();
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=singleton.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"singleton.js","sourceRoot":"","sources":["../../src/singleton.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAGhD,IAAI,IAA+B,CAAA;AAEnC,MAAM,CAAC,MAAM,aAAa,GAAkB,KAAK,UAAU,aAAa,CAAE,QAAkB,EAAE,OAA2B;IACvH,IAAI,IAAI,IAAI,IAAI,EAAE,CAAC;QACjB,IAAI,GAAG,MAAM,mBAAmB,EAAE,CAAA;IACpC,CAAC;IAED,OAAO,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAA;AAChC,CAAC,CAAA;AAED,aAAa,CAAC,KAAK,GAAG,KAAK;IACzB,MAAM,IAAI,EAAE,KAAK,EAAE,CAAA;AACrB,CAAC,CAAA;AAED,aAAa,CAAC,IAAI,GAAG,KAAK;IACxB,MAAM,IAAI,EAAE,IAAI,EAAE,CAAA;AACpB,CAAC,CAAA"}
|
|
@@ -0,0 +1,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,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