@helia/verified-fetch 0.0.0-f58d467 → 1.0.0

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 (54) hide show
  1. package/README.md +285 -25
  2. package/dist/index.min.js +7 -4
  3. package/dist/src/index.d.ts +285 -25
  4. package/dist/src/index.d.ts.map +1 -1
  5. package/dist/src/index.js +267 -25
  6. package/dist/src/index.js.map +1 -1
  7. package/dist/src/types.d.ts +2 -0
  8. package/dist/src/types.d.ts.map +1 -0
  9. package/dist/src/types.js +2 -0
  10. package/dist/src/types.js.map +1 -0
  11. package/dist/src/utils/dag-cbor-to-safe-json.d.ts +7 -0
  12. package/dist/src/utils/dag-cbor-to-safe-json.d.ts.map +1 -0
  13. package/dist/src/utils/dag-cbor-to-safe-json.js +37 -0
  14. package/dist/src/utils/dag-cbor-to-safe-json.js.map +1 -0
  15. package/dist/src/utils/get-content-disposition-filename.d.ts +6 -0
  16. package/dist/src/utils/get-content-disposition-filename.d.ts.map +1 -0
  17. package/dist/src/utils/get-content-disposition-filename.js +16 -0
  18. package/dist/src/utils/get-content-disposition-filename.js.map +1 -0
  19. package/dist/src/utils/get-e-tag.d.ts +28 -0
  20. package/dist/src/utils/get-e-tag.d.ts.map +1 -0
  21. package/dist/src/utils/get-e-tag.js +18 -0
  22. package/dist/src/utils/get-e-tag.js.map +1 -0
  23. package/dist/src/utils/get-tar-stream.d.ts +4 -0
  24. package/dist/src/utils/get-tar-stream.d.ts.map +1 -0
  25. package/dist/src/utils/get-tar-stream.js +46 -0
  26. package/dist/src/utils/get-tar-stream.js.map +1 -0
  27. package/dist/src/utils/parse-url-string.d.ts +7 -1
  28. package/dist/src/utils/parse-url-string.d.ts.map +1 -1
  29. package/dist/src/utils/parse-url-string.js +6 -0
  30. package/dist/src/utils/parse-url-string.js.map +1 -1
  31. package/dist/src/utils/responses.d.ts +5 -0
  32. package/dist/src/utils/responses.d.ts.map +1 -0
  33. package/dist/src/utils/responses.js +27 -0
  34. package/dist/src/utils/responses.js.map +1 -0
  35. package/dist/src/utils/select-output-type.d.ts +12 -0
  36. package/dist/src/utils/select-output-type.d.ts.map +1 -0
  37. package/dist/src/utils/select-output-type.js +148 -0
  38. package/dist/src/utils/select-output-type.js.map +1 -0
  39. package/dist/src/verified-fetch.d.ts +19 -26
  40. package/dist/src/verified-fetch.d.ts.map +1 -1
  41. package/dist/src/verified-fetch.js +261 -142
  42. package/dist/src/verified-fetch.js.map +1 -1
  43. package/dist/typedoc-urls.json +27 -0
  44. package/package.json +49 -112
  45. package/src/index.ts +290 -29
  46. package/src/types.ts +1 -0
  47. package/src/utils/dag-cbor-to-safe-json.ts +44 -0
  48. package/src/utils/get-content-disposition-filename.ts +18 -0
  49. package/src/utils/get-e-tag.ts +36 -0
  50. package/src/utils/get-tar-stream.ts +68 -0
  51. package/src/utils/parse-url-string.ts +17 -2
  52. package/src/utils/responses.ts +29 -0
  53. package/src/utils/select-output-type.ts +167 -0
  54. package/src/verified-fetch.ts +310 -154
package/dist/src/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  *
4
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
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.
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. By default, CIDs are retrieved over HTTP from [trustless gateways](https://specs.ipfs.tech/http-gateways/trustless-gateway/).
7
7
  *
8
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
9
  *
@@ -33,7 +33,7 @@
33
33
  * import { verifiedFetch } from '@helia/verified-fetch'
34
34
  * import { CID } from 'multiformats/cid'
35
35
  *
36
- * const cid = CID.parse('bafyFoo') // some image file
36
+ * const cid = CID.parse('bafyFoo') // some json file
37
37
  * const response = await verifiedFetch(cid)
38
38
  * const json = await response.json()
39
39
  * ```
@@ -56,7 +56,7 @@
56
56
  * import { verifiedFetch } from '@helia/verified-fetch'
57
57
  *
58
58
  * const response = await verifiedFetch('ipns://mydomain.com/path/to/very-long-file.log')
59
- * const bigFileStreamReader = await response.body.getReader()
59
+ * const bigFileStreamReader = await response.body?.getReader()
60
60
  * ```
61
61
  *
62
62
  * ## Configuration
@@ -73,8 +73,8 @@
73
73
  * import { createVerifiedFetch } from '@helia/verified-fetch'
74
74
  *
75
75
  * const fetch = await createVerifiedFetch({
76
- * gateways: ['https://trustless-gateway.link'],
77
- * routers: ['http://delegated-ipfs.dev']
76
+ * gateways: ['https://trustless-gateway.link'],
77
+ * routers: ['http://delegated-ipfs.dev']
78
78
  * })
79
79
  *
80
80
  * const resp = await fetch('ipfs://bafy...')
@@ -98,13 +98,13 @@
98
98
  *
99
99
  * const fetch = await createVerifiedFetch(
100
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
- * })
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
108
  * )
109
109
  *
110
110
  * const resp = await fetch('ipfs://bafy...')
@@ -114,7 +114,7 @@
114
114
  *
115
115
  * ### Custom content-type parsing
116
116
  *
117
- * 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.
117
+ * By default, if the response can be parsed as JSON, `@helia/verified-fetch` sets the `Content-Type` header as `application/json`, otherwise it sets it as `application/octet-stream` - this is because the `.json()`, `.text()`, `.blob()`, and `.arrayBuffer()` methods will usually work as expected without a detailed content type.
118
118
  *
119
119
  * If you require an accurate content-type you can provide a `contentTypeParser` function as an option to `createVerifiedFetch` to handle parsing the content type.
120
120
  *
@@ -127,16 +127,258 @@
127
127
  * import { fileTypeFromBuffer } from '@sgtpooki/file-type'
128
128
  *
129
129
  * const fetch = await createVerifiedFetch({
130
- * gateways: ['https://trustless-gateway.link'],
131
- * routers: ['http://delegated-ipfs.dev'],
132
- * contentTypeParser: async (bytes) => {
133
- * // call to some magic-byte recognition library like magic-bytes, file-type, or your own custom byte recognition
134
- * const result = await fileTypeFromBuffer(bytes)
135
- * return result?.mime
136
- * }
130
+ * gateways: ['https://trustless-gateway.link'],
131
+ * routers: ['http://delegated-ipfs.dev']
132
+ * }, {
133
+ * contentTypeParser: async (bytes) => {
134
+ * // call to some magic-byte recognition library like magic-bytes, file-type, or your own custom byte recognition
135
+ * const result = await fileTypeFromBuffer(bytes)
136
+ * return result?.mime
137
+ * }
137
138
  * })
138
139
  * ```
139
140
  *
141
+ * ### Custom DNS resolvers
142
+ *
143
+ * If you don't want to leak DNS queries to the default resolvers, you can provide your own list of DNS resolvers to `createVerifiedFetch`.
144
+ *
145
+ * Note that you do not need to provide both a DNS-over-HTTPS and a DNS-over-JSON resolver, and you should prefer `dnsJsonOverHttps` resolvers for usage in the browser for a smaller bundle size. See https://github.com/ipfs/helia/tree/main/packages/ipns#example---using-dns-json-over-https for more information.
146
+ *
147
+ * @example Customizing DNS resolvers
148
+ *
149
+ * ```typescript
150
+ * import { createVerifiedFetch } from '@helia/verified-fetch'
151
+ * import { dnsJsonOverHttps, dnsOverHttps } from '@helia/ipns/dns-resolvers'
152
+ *
153
+ * const fetch = await createVerifiedFetch({
154
+ * gateways: ['https://trustless-gateway.link'],
155
+ * routers: ['http://delegated-ipfs.dev'],
156
+ * dnsResolvers: [
157
+ * dnsJsonOverHttps('https://my-dns-resolver.example.com/dns-json'),
158
+ * dnsOverHttps('https://my-dns-resolver.example.com/dns-query')
159
+ * ]
160
+ * })
161
+ * ```
162
+ *
163
+ * ### IPLD codec handling
164
+ *
165
+ * IPFS supports several data formats (typically referred to as codecs) which are included in the CID. `@helia/verified-fetch` attempts to abstract away some of the details for easier consumption.
166
+ *
167
+ * #### DAG-PB
168
+ *
169
+ * [DAG-PB](https://ipld.io/docs/codecs/known/dag-pb/) is the codec we are most likely to encounter, it is what [UnixFS](https://github.com/ipfs/specs/blob/main/UNIXFS.md) uses under the hood.
170
+ *
171
+ * ##### Using the DAG-PB codec as a Blob
172
+ *
173
+ * ```typescript
174
+ * import { verifiedFetch } from '@helia/verified-fetch'
175
+ *
176
+ * const res = await verifiedFetch('ipfs://Qmfoo')
177
+ * const blob = await res.blob()
178
+ *
179
+ * console.info(blob) // Blob { size: x, type: 'application/octet-stream' }
180
+ * ```
181
+ *
182
+ * ##### Using the DAG-PB codec as an ArrayBuffer
183
+ *
184
+ * ```typescript
185
+ * import { verifiedFetch } from '@helia/verified-fetch'
186
+ *
187
+ * const res = await verifiedFetch('ipfs://Qmfoo')
188
+ * const buf = await res.arrayBuffer()
189
+ *
190
+ * console.info(buf) // ArrayBuffer { [Uint8Contents]: < ... >, byteLength: x }
191
+ * ```
192
+ *
193
+ * ##### Using the DAG-PB codec as a stream
194
+ *
195
+ * ```typescript
196
+ * import { verifiedFetch } from '@helia/verified-fetch'
197
+ *
198
+ * const res = await verifiedFetch('ipfs://Qmfoo')
199
+ * const reader = res.body?.getReader()
200
+ *
201
+ * if (reader == null) {
202
+ * throw new Error('Could not create reader from response body')
203
+ * }
204
+ *
205
+ * while (true) {
206
+ * const next = await reader.read()
207
+ *
208
+ * if (next?.done === true) {
209
+ * break
210
+ * }
211
+ *
212
+ * if (next?.value != null) {
213
+ * console.info(next.value) // Uint8Array(x) [ ... ]
214
+ * }
215
+ * }
216
+ * ```
217
+ *
218
+ * ##### Content-Type
219
+ *
220
+ * When fetching `DAG-PB` data, the content type will be set to `application/octet-stream` unless a custom content-type parser is configured.
221
+ *
222
+ * #### JSON
223
+ *
224
+ * The JSON codec is a very simple codec, a block parseable with this codec is a JSON string encoded into a `Uint8Array`.
225
+ *
226
+ * ##### Using the JSON codec
227
+ *
228
+ * ```typescript
229
+ * import * as json from 'multiformats/codecs/json'
230
+ *
231
+ * const block = new TextEncoder().encode('{ "hello": "world" }')
232
+ * const obj = json.decode(block)
233
+ *
234
+ * console.info(obj) // { hello: 'world' }
235
+ * ```
236
+ *
237
+ * ##### Content-Type
238
+ *
239
+ * When the `JSON` codec is encountered, the `Content-Type` header of the response will be set to `application/json`.
240
+ *
241
+ * ### DAG-JSON
242
+ *
243
+ * [DAG-JSON](https://ipld.io/docs/codecs/known/dag-json/) expands on the `JSON` codec, adding the ability to contain [CID](https://docs.ipfs.tech/concepts/content-addressing/)s which act as links to other blocks, and byte arrays.
244
+ *
245
+ * `CID`s and byte arrays are represented using special object structures with a single `"/"` property.
246
+ *
247
+ * Using `DAG-JSON` has two important caveats:
248
+ *
249
+ * 1. Your `JSON` structure cannot contain an object with only a `"/"` property, as it will be interpreted as a special type.
250
+ * 2. Since `JSON` has no technical limit on number sizes, `DAG-JSON` also allows numbers larger than `Number.MAX_SAFE_INTEGER`. JavaScript requires use of `BigInt`s to represent numbers larger than this, and `JSON.parse` does not support them, so precision will be lost.
251
+ *
252
+ * Otherwise this codec follows the same rules as the `JSON` codec.
253
+ *
254
+ * ##### Using the DAG-JSON codec
255
+ *
256
+ * ```typescript
257
+ * import * as dagJson from '@ipld/dag-json'
258
+ *
259
+ * const block = new TextEncoder().encode(`{
260
+ * "hello": "world",
261
+ * "cid": {
262
+ * "/": "baeaaac3imvwgy3zao5xxe3de"
263
+ * },
264
+ * "buf": {
265
+ * "/": {
266
+ * "bytes": "AAECAwQ"
267
+ * }
268
+ * }
269
+ * }`)
270
+ *
271
+ * const obj = dagJson.decode(block)
272
+ *
273
+ * console.info(obj)
274
+ * // {
275
+ * // hello: 'world',
276
+ * // cid: CID(baeaaac3imvwgy3zao5xxe3de),
277
+ * // buf: Uint8Array(5) [ 0, 1, 2, 3, 4 ]
278
+ * // }
279
+ * ```
280
+ *
281
+ * ##### Content-Type
282
+ *
283
+ * When the `DAG-JSON` codec is encountered in the requested CID, the `Content-Type` header of the response will be set to `application/json`.
284
+ *
285
+ * `DAG-JSON` data can be parsed from the response by using the `.json()` function, which will return `CID`s/byte arrays as plain `{ "/": ... }` objects:
286
+ *
287
+ * ```typescript
288
+ * import { verifiedFetch } from '@helia/verified-fetch'
289
+ * import * as dagJson from '@ipld/dag-json'
290
+ *
291
+ * const res = await verifiedFetch('ipfs://bafyDAGJSON')
292
+ *
293
+ * // either:
294
+ * const obj = await res.json()
295
+ * console.info(obj.cid) // { "/": "baeaaac3imvwgy3zao5xxe3de" }
296
+ * console.info(obj.buf) // { "/": { "bytes": "AAECAwQ" } }
297
+ * ```
298
+ *
299
+ * Alternatively, it can be decoded using the `@ipld/dag-json` module and the `.arrayBuffer()` method, in which case you will get `CID` objects and `Uint8Array`s:
300
+ *
301
+ *```typescript
302
+ * import { verifiedFetch } from '@helia/verified-fetch'
303
+ * import * as dagJson from '@ipld/dag-json'
304
+ *
305
+ * const res = await verifiedFetch('ipfs://bafyDAGJSON')
306
+ *
307
+ * // or:
308
+ * const obj = dagJson.decode<any>(await res.arrayBuffer())
309
+ * console.info(obj.cid) // CID(baeaaac3imvwgy3zao5xxe3de)
310
+ * console.info(obj.buf) // Uint8Array(5) [ 0, 1, 2, 3, 4 ]
311
+ * ```
312
+ *
313
+ * #### DAG-CBOR
314
+ *
315
+ * [DAG-CBOR](https://ipld.io/docs/codecs/known/dag-cbor/) uses the [Concise Binary Object Representation](https://cbor.io/) format for serialization instead of JSON.
316
+ *
317
+ * This supports more datatypes in a safer way than JSON and is smaller on the wire to boot so is usually preferable to JSON or DAG-JSON.
318
+ *
319
+ * ##### Content-Type
320
+ *
321
+ * Not all data types supported by `DAG-CBOR` can be successfully turned into JSON and back into the same binary form.
322
+ *
323
+ * When a decoded block can be round-tripped to JSON, the `Content-Type` will be set to `application/json`. In this case the `.json()` method on the `Response` object can be used to obtain an object representation of the response.
324
+ *
325
+ * When it cannot, the `Content-Type` will be `application/octet-stream` - in this case the `@ipld/dag-json` module must be used to deserialize the return value from `.arrayBuffer()`.
326
+ *
327
+ * ##### Detecting JSON-safe DAG-CBOR
328
+ *
329
+ * If the `Content-Type` header of the response is `application/json`, the `.json()` method may be used to access the response body in object form, otherwise the `.arrayBuffer()` method must be used to decode the raw bytes using the `@ipld/dag-cbor` module.
330
+ *
331
+ * ```typescript
332
+ * import { verifiedFetch } from '@helia/verified-fetch'
333
+ * import * as dagCbor from '@ipld/dag-cbor'
334
+ *
335
+ * const res = await verifiedFetch('ipfs://bafyDagCborCID')
336
+ * let obj
337
+ *
338
+ * if (res.headers.get('Content-Type') === 'application/json') {
339
+ * // DAG-CBOR data can be safely decoded as JSON
340
+ * obj = await res.json()
341
+ * } else {
342
+ * // response contains non-JSON friendly data types
343
+ * obj = dagCbor.decode(await res.arrayBuffer())
344
+ * }
345
+ *
346
+ * console.info(obj) // ...
347
+ * ```
348
+ *
349
+ * ## The `Accept` header
350
+ *
351
+ * The `Accept` header can be passed to override certain response processing, or to ensure that the final `Content-Type` of the response is the one that is expected.
352
+ *
353
+ * If the final `Content-Type` does not match the `Accept` header, or if the content cannot be represented in the format dictated by the `Accept` header, or you have configured a custom content type parser, and that parser returns a value that isn't in the accept header, a [406: Not Acceptable](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406) response will be returned:
354
+ *
355
+ * ```typescript
356
+ * import { verifiedFetch } from '@helia/verified-fetch'
357
+ *
358
+ * const res = await verifiedFetch('ipfs://bafyJPEGImageCID', {
359
+ * headers: {
360
+ * accept: 'image/png'
361
+ * }
362
+ * })
363
+ *
364
+ * console.info(res.status) // 406 - the image was a JPEG but we specified PNG as the accept header
365
+ * ```
366
+ *
367
+ * It can also be used to skip processing the data from some formats such as `DAG-CBOR` if you wish to handle decoding it yourself:
368
+ *
369
+ * ```typescript
370
+ * import { verifiedFetch } from '@helia/verified-fetch'
371
+ *
372
+ * const res = await verifiedFetch('ipfs://bafyDAGCBORCID', {
373
+ * headers: {
374
+ * accept: 'application/octet-stream'
375
+ * }
376
+ * })
377
+ *
378
+ * console.info(res.headers.get('accept')) // application/octet-stream
379
+ * const buf = await res.arrayBuffer() // raw bytes, not processed as JSON
380
+ * ```
381
+ *
140
382
  * ## Comparison to fetch
141
383
  *
142
384
  * This module attempts to act as similarly to the `fetch()` API as possible.
@@ -154,7 +396,7 @@
154
396
  * 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
155
397
  * 3. CID instances: An actual CID instance `CID.parse('bafy...')`
156
398
  *
157
- * 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).
399
+ * As well as support for pathing & params for items 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).
158
400
  *
159
401
  * 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.
160
402
  *
@@ -257,10 +499,10 @@ import { VerifiedFetch as VerifiedFetchClass } from './verified-fetch.js';
257
499
  /**
258
500
  * Create and return a Helia node
259
501
  */
260
- export async function createVerifiedFetch(init) {
261
- let contentTypeParser;
502
+ export async function createVerifiedFetch(init, options) {
503
+ let dnsResolvers;
262
504
  if (!isHelia(init)) {
263
- contentTypeParser = init?.contentTypeParser;
505
+ dnsResolvers = init?.dnsResolvers;
264
506
  init = await createHeliaHTTP({
265
507
  blockBrokers: [
266
508
  trustlessGateway({
@@ -270,7 +512,7 @@ export async function createVerifiedFetch(init) {
270
512
  routers: (init?.routers ?? ['https://delegated-ipfs.dev']).map((routerUrl) => delegatedHTTPRouting(routerUrl))
271
513
  });
272
514
  }
273
- const verifiedFetchInstance = new VerifiedFetchClass({ helia: init }, { contentTypeParser });
515
+ const verifiedFetchInstance = new VerifiedFetchClass({ helia: init }, { dnsResolvers, ...options });
274
516
  async function verifiedFetch(resource, options) {
275
517
  return verifiedFetchInstance.fetch(resource, options);
276
518
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2PG;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;AAiFzE;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAE,IAAyC;IAClF,IAAI,iBAAgD,CAAA;IAEpD,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACnB,i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AE,CAAC,oBAAoB,CAAC,SAAS,CAAC,CAAC;SAC/G,CAAC,CAAA;IACJ,CAAC;IAED,MAAM,qBAAqB,GAAG,IAAI,kBAAkB,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,EAAE,YAAY,EAAE,GAAG,OAAO,EAAE,CAAC,CAAA;IACnG,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,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,2 @@
1
+ export type RequestFormatShorthand = 'raw' | 'car' | 'tar' | 'ipns-record' | 'dag-json' | 'dag-cbor' | 'json' | 'cbor';
2
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,sBAAsB,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,aAAa,GAAG,UAAU,GAAG,UAAU,GAAG,MAAM,GAAG,MAAM,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Take a `DAG-CBOR` encoded `Uint8Array`, deserialize it as an object and
3
+ * re-serialize it in a form that can be passed to `JSON.serialize` and then
4
+ * `JSON.parse` without losing any data.
5
+ */
6
+ export declare function dagCborToSafeJSON(buf: Uint8Array): string;
7
+ //# sourceMappingURL=dag-cbor-to-safe-json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dag-cbor-to-safe-json.d.ts","sourceRoot":"","sources":["../../../src/utils/dag-cbor-to-safe-json.ts"],"names":[],"mappings":"AAgBA;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAE,GAAG,EAAE,UAAU,GAAG,MAAM,CAsB1D"}
@@ -0,0 +1,37 @@
1
+ import { decode } from 'cborg';
2
+ import { encode } from 'cborg/json';
3
+ import { CID } from 'multiformats/cid';
4
+ // https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
5
+ const CID_CBOR_TAG = 0x2A;
6
+ function cidDecoder(bytes) {
7
+ if (bytes[0] !== 0) {
8
+ throw new Error('Invalid CID for CBOR tag 42; expected leading 0x00');
9
+ }
10
+ return CID.decode(bytes.subarray(1)); // ignore leading 0x00
11
+ }
12
+ /**
13
+ * Take a `DAG-CBOR` encoded `Uint8Array`, deserialize it as an object and
14
+ * re-serialize it in a form that can be passed to `JSON.serialize` and then
15
+ * `JSON.parse` without losing any data.
16
+ */
17
+ export function dagCborToSafeJSON(buf) {
18
+ const tags = [];
19
+ tags[CID_CBOR_TAG] = cidDecoder;
20
+ const obj = decode(buf, {
21
+ allowIndefinite: false,
22
+ coerceUndefinedToNull: true,
23
+ allowNaN: false,
24
+ allowInfinity: false,
25
+ strict: true,
26
+ useMaps: false,
27
+ rejectDuplicateMapKeys: true,
28
+ tags,
29
+ // this is different to `DAG-CBOR` - the reason we disallow BigInts is
30
+ // because we are about to re-encode to `JSON` which does not support
31
+ // BigInts. Blocks containing large numbers should be deserialized using a
32
+ // cbor decoder instead
33
+ allowBigInt: false
34
+ });
35
+ return new TextDecoder().decode(encode(obj));
36
+ }
37
+ //# sourceMappingURL=dag-cbor-to-safe-json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dag-cbor-to-safe-json.js","sourceRoot":"","sources":["../../../src/utils/dag-cbor-to-safe-json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACnC,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAGtC,qEAAqE;AACrE,MAAM,YAAY,GAAG,IAAI,CAAA;AAEzB,SAAS,UAAU,CAAE,KAAiB;IACpC,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAA;IACvE,CAAC;IAED,OAAO,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAA,CAAC,sBAAsB;AAC7D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,iBAAiB,CAAE,GAAe;IAChD,MAAM,IAAI,GAAiB,EAAE,CAAA;IAC7B,IAAI,CAAC,YAAY,CAAC,GAAG,UAAU,CAAA;IAE/B,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,EAAE;QACtB,eAAe,EAAE,KAAK;QACtB,qBAAqB,EAAE,IAAI;QAC3B,QAAQ,EAAE,KAAK;QACf,aAAa,EAAE,KAAK;QACpB,MAAM,EAAE,IAAI;QACZ,OAAO,EAAE,KAAK;QACd,sBAAsB,EAAE,IAAI;QAC5B,IAAI;QAEJ,sEAAsE;QACtE,qEAAqE;QACrE,0EAA0E;QAC1E,uBAAuB;QACvB,WAAW,EAAE,KAAK;KACnB,CAAC,CAAA;IAEF,OAAO,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAA;AAC9C,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Takes a filename URL param and returns a string for use in a
3
+ * `Content-Disposition` header
4
+ */
5
+ export declare function getContentDispositionFilename(filename: string): string;
6
+ //# sourceMappingURL=get-content-disposition-filename.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-content-disposition-filename.d.ts","sourceRoot":"","sources":["../../../src/utils/get-content-disposition-filename.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,6BAA6B,CAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAQvE"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Takes a filename URL param and returns a string for use in a
3
+ * `Content-Disposition` header
4
+ */
5
+ export function getContentDispositionFilename(filename) {
6
+ const asciiOnly = replaceNonAsciiCharacters(filename);
7
+ if (asciiOnly === filename) {
8
+ return `filename="${filename}"`;
9
+ }
10
+ return `filename="${asciiOnly}"; filename*=UTF-8''${encodeURIComponent(filename)}`;
11
+ }
12
+ function replaceNonAsciiCharacters(filename) {
13
+ // eslint-disable-next-line no-control-regex
14
+ return filename.replace(/[^\x00-\x7F]/g, '_');
15
+ }
16
+ //# sourceMappingURL=get-content-disposition-filename.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-content-disposition-filename.js","sourceRoot":"","sources":["../../../src/utils/get-content-disposition-filename.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,6BAA6B,CAAE,QAAgB;IAC7D,MAAM,SAAS,GAAG,yBAAyB,CAAC,QAAQ,CAAC,CAAA;IAErD,IAAI,SAAS,KAAK,QAAQ,EAAE,CAAC;QAC3B,OAAO,aAAa,QAAQ,GAAG,CAAA;IACjC,CAAC;IAED,OAAO,aAAa,SAAS,uBAAuB,kBAAkB,CAAC,QAAQ,CAAC,EAAE,CAAA;AACpF,CAAC;AAED,SAAS,yBAAyB,CAAE,QAAgB;IAClD,4CAA4C;IAC5C,OAAO,QAAQ,CAAC,OAAO,CAAC,eAAe,EAAE,GAAG,CAAC,CAAA;AAC/C,CAAC"}
@@ -0,0 +1,28 @@
1
+ import type { RequestFormatShorthand } from '../types.js';
2
+ import type { CID } from 'multiformats/cid';
3
+ interface GetETagArg {
4
+ cid: CID;
5
+ reqFormat?: RequestFormatShorthand;
6
+ rangeStart?: number;
7
+ rangeEnd?: number;
8
+ /**
9
+ * Weak Etag is used when we can't guarantee byte-for-byte-determinism (generated, or mutable content).
10
+ * Some examples:
11
+ * - IPNS requests
12
+ * - CAR streamed with blocks in non-deterministic order
13
+ * - TAR streamed with files in non-deterministic order
14
+ */
15
+ weak?: boolean;
16
+ }
17
+ /**
18
+ * etag
19
+ * you need to wrap cid with ""
20
+ * we use strong Etags for immutable responses and weak one (prefixed with W/ ) for mutable/generated ones (ipns and generated HTML).
21
+ * block and car responses should have different etag than deserialized one, so you can add some prefix like we do in existing gateway
22
+ *
23
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
24
+ * @see https://specs.ipfs.tech/http-gateways/path-gateway/#etag-response-header
25
+ */
26
+ export declare function getETag({ cid, reqFormat, weak, rangeStart, rangeEnd }: GetETagArg): string;
27
+ export {};
28
+ //# sourceMappingURL=get-e-tag.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-e-tag.d.ts","sourceRoot":"","sources":["../../../src/utils/get-e-tag.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AACzD,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAE3C,UAAU,UAAU;IAClB,GAAG,EAAE,GAAG,CAAA;IACR,SAAS,CAAC,EAAE,sBAAsB,CAAA;IAClC,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,OAAO,CAAA;CACf;AAED;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAE,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE,UAAU,GAAG,MAAM,CAQ3F"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * etag
3
+ * you need to wrap cid with ""
4
+ * we use strong Etags for immutable responses and weak one (prefixed with W/ ) for mutable/generated ones (ipns and generated HTML).
5
+ * block and car responses should have different etag than deserialized one, so you can add some prefix like we do in existing gateway
6
+ *
7
+ * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
8
+ * @see https://specs.ipfs.tech/http-gateways/path-gateway/#etag-response-header
9
+ */
10
+ export function getETag({ cid, reqFormat, weak, rangeStart, rangeEnd }) {
11
+ const prefix = weak === true ? 'W/' : '';
12
+ let suffix = reqFormat == null ? '' : `.${reqFormat}`;
13
+ if (rangeStart != null || rangeEnd != null) {
14
+ suffix += `.${rangeStart ?? '0'}-${rangeEnd ?? 'N'}`;
15
+ }
16
+ return `${prefix}"${cid.toString()}${suffix}"`;
17
+ }
18
+ //# sourceMappingURL=get-e-tag.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-e-tag.js","sourceRoot":"","sources":["../../../src/utils/get-e-tag.ts"],"names":[],"mappings":"AAkBA;;;;;;;;GAQG;AACH,MAAM,UAAU,OAAO,CAAE,EAAE,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAc;IACjF,MAAM,MAAM,GAAG,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAA;IACxC,IAAI,MAAM,GAAG,SAAS,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,SAAS,EAAE,CAAA;IACrD,IAAI,UAAU,IAAI,IAAI,IAAI,QAAQ,IAAI,IAAI,EAAE,CAAC;QAC3C,MAAM,IAAI,IAAI,UAAU,IAAI,GAAG,IAAI,QAAQ,IAAI,GAAG,EAAE,CAAA;IACtD,CAAC;IAED,OAAO,GAAG,MAAM,IAAI,GAAG,CAAC,QAAQ,EAAE,GAAG,MAAM,GAAG,CAAA;AAChD,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { AbortOptions } from '@libp2p/interface';
2
+ import type { Blockstore } from 'interface-blockstore';
3
+ export declare function tarStream(ipfsPath: string, blockstore: Blockstore, options?: AbortOptions): AsyncGenerator<Uint8Array>;
4
+ //# sourceMappingURL=get-tar-stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-tar-stream.d.ts","sourceRoot":"","sources":["../../../src/utils/get-tar-stream.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AAsCtD,wBAAwB,SAAS,CAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,cAAc,CAAC,UAAU,CAAC,CAuB/H"}
@@ -0,0 +1,46 @@
1
+ import { CodeError } from '@libp2p/interface';
2
+ import { exporter, recursive } from 'ipfs-unixfs-exporter';
3
+ import map from 'it-map';
4
+ import { pipe } from 'it-pipe';
5
+ import { pack } from 'it-tar';
6
+ const EXPORTABLE = ['file', 'raw', 'directory'];
7
+ function toHeader(file) {
8
+ let mode;
9
+ let mtime;
10
+ if (file.type === 'file' || file.type === 'directory') {
11
+ mode = file.unixfs.mode;
12
+ mtime = file.unixfs.mtime != null ? new Date(Number(file.unixfs.mtime.secs * 1000n)) : undefined;
13
+ }
14
+ return {
15
+ name: file.path,
16
+ mode,
17
+ mtime,
18
+ size: Number(file.size),
19
+ type: file.type === 'directory' ? 'directory' : 'file'
20
+ };
21
+ }
22
+ function toTarImportCandidate(entry) {
23
+ if (!EXPORTABLE.includes(entry.type)) {
24
+ throw new CodeError('Not a UnixFS node', 'ERR_NOT_UNIXFS');
25
+ }
26
+ const candidate = {
27
+ header: toHeader(entry)
28
+ };
29
+ if (entry.type === 'file' || entry.type === 'raw') {
30
+ candidate.body = entry.content();
31
+ }
32
+ return candidate;
33
+ }
34
+ export async function* tarStream(ipfsPath, blockstore, options) {
35
+ const file = await exporter(ipfsPath, blockstore, options);
36
+ if (file.type === 'file' || file.type === 'raw') {
37
+ yield* pipe([toTarImportCandidate(file)], pack());
38
+ return;
39
+ }
40
+ if (file.type === 'directory') {
41
+ yield* pipe(recursive(ipfsPath, blockstore, options), (source) => map(source, (entry) => toTarImportCandidate(entry)), pack());
42
+ return;
43
+ }
44
+ throw new CodeError('Not a UnixFS node', 'ERR_NOT_UNIXFS');
45
+ }
46
+ //# sourceMappingURL=get-tar-stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-tar-stream.js","sourceRoot":"","sources":["../../../src/utils/get-tar-stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAoB,MAAM,sBAAsB,CAAA;AAC5E,OAAO,GAAG,MAAM,QAAQ,CAAA;AACxB,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,IAAI,EAAgD,MAAM,QAAQ,CAAA;AAI3E,MAAM,UAAU,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,WAAW,CAAC,CAAA;AAE/C,SAAS,QAAQ,CAAE,IAAiB;IAClC,IAAI,IAAwB,CAAA;IAC5B,IAAI,KAAuB,CAAA;IAE3B,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAAI,IAAI,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QACtD,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAA;QACvB,KAAK,GAAG,I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AAC,CAAC,EAC5B,IAAI,EAAE,CACP,CAAA;QAED,OAAM;IACR,CAAC;IAED,IAAI,IAAI,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;QAC9B,KAAM,CAAC,CAAC,IAAI,CACV,SAAS,CAAC,QAAQ,EAAE,UAAU,EAAE,OAAO,CAAC,EACxC,CAAC,MAAM,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,oBAAoB,CAAC,KAAK,CAAC,CAAC,EAC/D,IAAI,EAAE,CACP,CAAA;QAED,OAAM;IACR,CAAC;IAED,MAAM,IAAI,SAAS,CAAC,mBAAmB,EAAE,gBAAgB,CAAC,CAAA;AAC5D,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { CID } from 'multiformats/cid';
2
+ import type { RequestFormatShorthand } from '../types.js';
2
3
  import type { IPNS, IPNSRoutingEvents, ResolveDnsLinkProgressEvents, ResolveProgressEvents } from '@helia/ipns';
3
4
  import type { ComponentLogger } from '@libp2p/interface';
4
5
  import type { ProgressOptions } from 'progress-events';
@@ -9,11 +10,16 @@ export interface ParseUrlStringInput {
9
10
  }
10
11
  export interface ParseUrlStringOptions extends ProgressOptions<ResolveProgressEvents | IPNSRoutingEvents | ResolveDnsLinkProgressEvents> {
11
12
  }
13
+ export interface ParsedUrlQuery extends Record<string, string | unknown> {
14
+ format?: RequestFormatShorthand;
15
+ download?: boolean;
16
+ filename?: string;
17
+ }
12
18
  export interface ParsedUrlStringResults {
13
19
  protocol: string;
14
20
  path: string;
15
21
  cid: CID;
16
- query: Record<string, string>;
22
+ query: ParsedUrlQuery;
17
23
  }
18
24
  /**
19
25
  * A function that parses ipfs:// and ipns:// URLs, returning an object with easily recognizable properties.
@@ -1 +1 @@
1
- {"version":3,"file":"parse-url-string.d.ts","sourceRoot":"","sources":["../../../src/utils/parse-url-string.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AAEt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MAAM,CAAA;IACjB,IAAI,EAAE,IAAI,CAAA;IACV,MAAM,EAAE,eAAe,CAAA;CACxB;AACD,MAAM,WAAW,qBAAsB,SAAQ,eAAe,CAAC,qBAAqB,GAAG,iBAAiB,GAAG,4BAA4B,CAAC;CAEvI;AAED,MAAM,WAAW,cAAe,SAAQ,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC;IACtE,MAAM,CAAC,EAAE,sBAAsB,CAAA;IAC/B,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB;AAED,MAAM,WAAW,sBAAsB;IACrC,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,GAAG,CAAA;IACR,KAAK,EAAE,cAAc,CAAA;CACtB;AAID;;;;;;GAMG;AACH,wBAAsB,cAAc,CAAE,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,mBAAmB,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAgHxJ"}
@@ -85,6 +85,12 @@ export async function parseUrlString({ urlString, ipns, logger }, options) {
85
85
  const [key, value] = part.split('=');
86
86
  query[key] = decodeURIComponent(value);
87
87
  }
88
+ if (query.download != null) {
89
+ query.download = query.download === 'true';
90
+ }
91
+ if (query.filename != null) {
92
+ query.filename = query.filename.toString();
93
+ }
88
94
  }
89
95
  /**
90
96
  * join the path from resolve result & given path.