@helia/verified-fetch 0.0.0-75d0a5b → 0.0.0-7c07e11
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/README.md +205 -6
- package/dist/index.min.js +4 -4
- package/dist/src/index.d.ts +186 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +186 -4
- package/dist/src/index.js.map +1 -1
- package/dist/src/types.d.ts +2 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/utils/dag-cbor-to-safe-json.d.ts +7 -0
- package/dist/src/utils/dag-cbor-to-safe-json.d.ts.map +1 -0
- package/dist/src/utils/dag-cbor-to-safe-json.js +37 -0
- package/dist/src/utils/dag-cbor-to-safe-json.js.map +1 -0
- package/dist/src/utils/get-e-tag.d.ts +28 -0
- package/dist/src/utils/get-e-tag.d.ts.map +1 -0
- package/dist/src/utils/get-e-tag.js +18 -0
- package/dist/src/utils/get-e-tag.js.map +1 -0
- package/dist/src/utils/parse-url-string.d.ts +5 -1
- package/dist/src/utils/parse-url-string.d.ts.map +1 -1
- package/dist/src/utils/parse-url-string.js.map +1 -1
- package/dist/src/verified-fetch.d.ts +1 -11
- package/dist/src/verified-fetch.d.ts.map +1 -1
- package/dist/src/verified-fetch.js +25 -38
- package/dist/src/verified-fetch.js.map +1 -1
- package/package.json +18 -16
- package/src/index.ts +186 -4
- package/src/types.ts +1 -0
- package/src/utils/dag-cbor-to-safe-json.ts +44 -0
- package/src/utils/get-e-tag.ts +36 -0
- package/src/utils/parse-url-string.ts +6 -1
- package/src/verified-fetch.ts +31 -44
package/dist/src/index.d.ts
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
|
|
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
|
* ```
|
|
@@ -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
|
*
|
|
@@ -138,6 +138,188 @@
|
|
|
138
138
|
* })
|
|
139
139
|
* ```
|
|
140
140
|
*
|
|
141
|
+
* ### IPLD codec handling
|
|
142
|
+
*
|
|
143
|
+
* 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.
|
|
144
|
+
*
|
|
145
|
+
* #### DAG-PB
|
|
146
|
+
*
|
|
147
|
+
* [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.
|
|
148
|
+
*
|
|
149
|
+
* ##### Using the DAG-PB codec as a Blob
|
|
150
|
+
*
|
|
151
|
+
* ```typescript
|
|
152
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
153
|
+
*
|
|
154
|
+
* const res = await verifiedFetch('ipfs://Qmfoo')
|
|
155
|
+
* const blob = await res.blob()
|
|
156
|
+
*
|
|
157
|
+
* console.info(blob) // Blob { size: x, type: 'application/octet-stream' }
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* ##### Using the DAG-PB codec as an ArrayBuffer
|
|
161
|
+
*
|
|
162
|
+
* ```typescript
|
|
163
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
164
|
+
*
|
|
165
|
+
* const res = await verifiedFetch('ipfs://Qmfoo')
|
|
166
|
+
* const buf = await res.arrayBuffer()
|
|
167
|
+
*
|
|
168
|
+
* console.info(buf) // ArrayBuffer { [Uint8Contents]: < ... >, byteLength: x }
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* ##### Using the DAG-PB codec as a stream
|
|
172
|
+
*
|
|
173
|
+
* ```typescript
|
|
174
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
175
|
+
*
|
|
176
|
+
* const res = await verifiedFetch('ipfs://Qmfoo')
|
|
177
|
+
* const reader = res.body?.getReader()
|
|
178
|
+
*
|
|
179
|
+
* while (true) {
|
|
180
|
+
* const next = await reader.read()
|
|
181
|
+
*
|
|
182
|
+
* if (next?.done === true) {
|
|
183
|
+
* break
|
|
184
|
+
* }
|
|
185
|
+
*
|
|
186
|
+
* if (next?.value != null) {
|
|
187
|
+
* console.info(next.value) // Uint8Array(x) [ ... ]
|
|
188
|
+
* }
|
|
189
|
+
* }
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* ##### Content-Type
|
|
193
|
+
*
|
|
194
|
+
* When fetching `DAG-PB` data, the content type will be set to `application/octet-stream` unless a custom content-type parser is configured.
|
|
195
|
+
*
|
|
196
|
+
* #### JSON
|
|
197
|
+
*
|
|
198
|
+
* The JSON codec is a very simple codec, a block parseable with this codec is a JSON string encoded into a `Uint8Array`.
|
|
199
|
+
*
|
|
200
|
+
* ##### Using the JSON codec
|
|
201
|
+
*
|
|
202
|
+
* ```typescript
|
|
203
|
+
* import * as json from 'multiformats/codecs/json'
|
|
204
|
+
*
|
|
205
|
+
* const block = new TextEncoder().encode('{ "hello": "world" }')
|
|
206
|
+
* const obj = json.decode(block)
|
|
207
|
+
*
|
|
208
|
+
* console.info(obj) // { hello: 'world' }
|
|
209
|
+
* ```
|
|
210
|
+
*
|
|
211
|
+
* ##### Content-Type
|
|
212
|
+
*
|
|
213
|
+
* When the `JSON` codec is encountered, the `Content-Type` header of the response will be set to `application/json`.
|
|
214
|
+
*
|
|
215
|
+
* ### DAG-JSON
|
|
216
|
+
*
|
|
217
|
+
* [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.
|
|
218
|
+
*
|
|
219
|
+
* `CID`s and byte arrays are represented using special object structures with a single `"/"` property.
|
|
220
|
+
*
|
|
221
|
+
* Using `DAG-JSON` has two important caveats:
|
|
222
|
+
*
|
|
223
|
+
* 1. Your `JSON` structure cannot contain an object with only a `"/"` property, as it will be interpreted as a special type.
|
|
224
|
+
* 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.
|
|
225
|
+
*
|
|
226
|
+
* Otherwise this codec follows the same rules as the `JSON` codec.
|
|
227
|
+
*
|
|
228
|
+
* ##### Using the DAG-JSON codec
|
|
229
|
+
*
|
|
230
|
+
* ```typescript
|
|
231
|
+
* import * as dagJson from '@ipld/dag-json'
|
|
232
|
+
*
|
|
233
|
+
* const block = new TextEncoder().encode(`{
|
|
234
|
+
* "hello": "world",
|
|
235
|
+
* "cid": {
|
|
236
|
+
* "/": "baeaaac3imvwgy3zao5xxe3de"
|
|
237
|
+
* },
|
|
238
|
+
* "buf": {
|
|
239
|
+
* "/": {
|
|
240
|
+
* "bytes": "AAECAwQ"
|
|
241
|
+
* }
|
|
242
|
+
* }
|
|
243
|
+
* }`)
|
|
244
|
+
*
|
|
245
|
+
* const obj = dagJson.decode(block)
|
|
246
|
+
*
|
|
247
|
+
* console.info(obj)
|
|
248
|
+
* // {
|
|
249
|
+
* // hello: 'world',
|
|
250
|
+
* // cid: CID(baeaaac3imvwgy3zao5xxe3de),
|
|
251
|
+
* // buf: Uint8Array(5) [ 0, 1, 2, 3, 4 ]
|
|
252
|
+
* // }
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
* ##### Content-Type
|
|
256
|
+
*
|
|
257
|
+
* When the `DAG-JSON` codec is encountered in the requested CID, the `Content-Type` header of the response will be set to `application/json`.
|
|
258
|
+
*
|
|
259
|
+
* `DAG-JSON` data can be parsed from the response by using the `.json()` function, which will return `CID`s/byte arrays as plain `{ "/": ... }` objects:
|
|
260
|
+
*
|
|
261
|
+
* ```typescript
|
|
262
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
263
|
+
* import * as dagJson from '@ipld/dag-json'
|
|
264
|
+
*
|
|
265
|
+
* const res = await verifiedFetch('ipfs://bafyDAGJSON')
|
|
266
|
+
*
|
|
267
|
+
* // either:
|
|
268
|
+
* const obj = await res.json()
|
|
269
|
+
* console.info(obj.cid) // { "/": "baeaaac3imvwgy3zao5xxe3de" }
|
|
270
|
+
* console.info(obj.buf) // { "/": { "bytes": "AAECAwQ" } }
|
|
271
|
+
* ```
|
|
272
|
+
*
|
|
273
|
+
* 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:
|
|
274
|
+
*
|
|
275
|
+
*```typescript
|
|
276
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
277
|
+
* import * as dagJson from '@ipld/dag-json'
|
|
278
|
+
*
|
|
279
|
+
* const res = await verifiedFetch('ipfs://bafyDAGJSON')
|
|
280
|
+
*
|
|
281
|
+
* // or:
|
|
282
|
+
* const obj = dagJson.decode(await res.arrayBuffer())
|
|
283
|
+
* console.info(obj.cid) // CID(baeaaac3imvwgy3zao5xxe3de)
|
|
284
|
+
* console.info(obj.buf) // Uint8Array(5) [ 0, 1, 2, 3, 4 ]
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* #### DAG-CBOR
|
|
288
|
+
*
|
|
289
|
+
* [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.
|
|
290
|
+
*
|
|
291
|
+
* 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.
|
|
292
|
+
*
|
|
293
|
+
* ##### Content-Type
|
|
294
|
+
*
|
|
295
|
+
* Not all data types supported by `DAG-CBOR` can be successfully turned into JSON and back into the same binary form.
|
|
296
|
+
*
|
|
297
|
+
* 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.
|
|
298
|
+
*
|
|
299
|
+
* 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()`.
|
|
300
|
+
*
|
|
301
|
+
* ##### Detecting JSON-safe DAG-CBOR
|
|
302
|
+
*
|
|
303
|
+
* 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.
|
|
304
|
+
*
|
|
305
|
+
* ```typescript
|
|
306
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
307
|
+
* import * as dagCbor from '@ipld/dag-cbor'
|
|
308
|
+
*
|
|
309
|
+
* const res = await verifiedFetch('ipfs://bafyDagCborCID')
|
|
310
|
+
* let obj
|
|
311
|
+
*
|
|
312
|
+
* if (res.headers.get('Content-Type') === 'application/json') {
|
|
313
|
+
* // DAG-CBOR data can be safely decoded as JSON
|
|
314
|
+
* obj = await res.json()
|
|
315
|
+
* } else {
|
|
316
|
+
* // response contains non-JSON friendly data types
|
|
317
|
+
* obj = dagCbor.decode(await res.arrayBuffer())
|
|
318
|
+
* }
|
|
319
|
+
*
|
|
320
|
+
* console.info(obj) // ...
|
|
321
|
+
* ```
|
|
322
|
+
*
|
|
141
323
|
* ## Comparison to fetch
|
|
142
324
|
*
|
|
143
325
|
* This module attempts to act as similarly to the `fetch()` API as possible.
|
|
@@ -155,7 +337,7 @@
|
|
|
155
337
|
* 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
|
|
156
338
|
* 3. CID instances: An actual CID instance `CID.parse('bafy...')`
|
|
157
339
|
*
|
|
158
|
-
* As well as support for pathing & params for
|
|
340
|
+
* 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).
|
|
159
341
|
*
|
|
160
342
|
* 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.
|
|
161
343
|
*
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkbG;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,GAAG,CAAA;IACR,IAAI,EAAE,MAAM,CAAA;CACb;AAED,MAAM,WAAW,cAAe,S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,EAAE,0BAA0B,GAAG,OAAO,CAAC,aAAa,CAAC,CAsB/I;AAED,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA"}
|
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
|
|
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
|
* ```
|
|
@@ -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
|
*
|
|
@@ -138,6 +138,188 @@
|
|
|
138
138
|
* })
|
|
139
139
|
* ```
|
|
140
140
|
*
|
|
141
|
+
* ### IPLD codec handling
|
|
142
|
+
*
|
|
143
|
+
* 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.
|
|
144
|
+
*
|
|
145
|
+
* #### DAG-PB
|
|
146
|
+
*
|
|
147
|
+
* [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.
|
|
148
|
+
*
|
|
149
|
+
* ##### Using the DAG-PB codec as a Blob
|
|
150
|
+
*
|
|
151
|
+
* ```typescript
|
|
152
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
153
|
+
*
|
|
154
|
+
* const res = await verifiedFetch('ipfs://Qmfoo')
|
|
155
|
+
* const blob = await res.blob()
|
|
156
|
+
*
|
|
157
|
+
* console.info(blob) // Blob { size: x, type: 'application/octet-stream' }
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* ##### Using the DAG-PB codec as an ArrayBuffer
|
|
161
|
+
*
|
|
162
|
+
* ```typescript
|
|
163
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
164
|
+
*
|
|
165
|
+
* const res = await verifiedFetch('ipfs://Qmfoo')
|
|
166
|
+
* const buf = await res.arrayBuffer()
|
|
167
|
+
*
|
|
168
|
+
* console.info(buf) // ArrayBuffer { [Uint8Contents]: < ... >, byteLength: x }
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* ##### Using the DAG-PB codec as a stream
|
|
172
|
+
*
|
|
173
|
+
* ```typescript
|
|
174
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
175
|
+
*
|
|
176
|
+
* const res = await verifiedFetch('ipfs://Qmfoo')
|
|
177
|
+
* const reader = res.body?.getReader()
|
|
178
|
+
*
|
|
179
|
+
* while (true) {
|
|
180
|
+
* const next = await reader.read()
|
|
181
|
+
*
|
|
182
|
+
* if (next?.done === true) {
|
|
183
|
+
* break
|
|
184
|
+
* }
|
|
185
|
+
*
|
|
186
|
+
* if (next?.value != null) {
|
|
187
|
+
* console.info(next.value) // Uint8Array(x) [ ... ]
|
|
188
|
+
* }
|
|
189
|
+
* }
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* ##### Content-Type
|
|
193
|
+
*
|
|
194
|
+
* When fetching `DAG-PB` data, the content type will be set to `application/octet-stream` unless a custom content-type parser is configured.
|
|
195
|
+
*
|
|
196
|
+
* #### JSON
|
|
197
|
+
*
|
|
198
|
+
* The JSON codec is a very simple codec, a block parseable with this codec is a JSON string encoded into a `Uint8Array`.
|
|
199
|
+
*
|
|
200
|
+
* ##### Using the JSON codec
|
|
201
|
+
*
|
|
202
|
+
* ```typescript
|
|
203
|
+
* import * as json from 'multiformats/codecs/json'
|
|
204
|
+
*
|
|
205
|
+
* const block = new TextEncoder().encode('{ "hello": "world" }')
|
|
206
|
+
* const obj = json.decode(block)
|
|
207
|
+
*
|
|
208
|
+
* console.info(obj) // { hello: 'world' }
|
|
209
|
+
* ```
|
|
210
|
+
*
|
|
211
|
+
* ##### Content-Type
|
|
212
|
+
*
|
|
213
|
+
* When the `JSON` codec is encountered, the `Content-Type` header of the response will be set to `application/json`.
|
|
214
|
+
*
|
|
215
|
+
* ### DAG-JSON
|
|
216
|
+
*
|
|
217
|
+
* [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.
|
|
218
|
+
*
|
|
219
|
+
* `CID`s and byte arrays are represented using special object structures with a single `"/"` property.
|
|
220
|
+
*
|
|
221
|
+
* Using `DAG-JSON` has two important caveats:
|
|
222
|
+
*
|
|
223
|
+
* 1. Your `JSON` structure cannot contain an object with only a `"/"` property, as it will be interpreted as a special type.
|
|
224
|
+
* 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.
|
|
225
|
+
*
|
|
226
|
+
* Otherwise this codec follows the same rules as the `JSON` codec.
|
|
227
|
+
*
|
|
228
|
+
* ##### Using the DAG-JSON codec
|
|
229
|
+
*
|
|
230
|
+
* ```typescript
|
|
231
|
+
* import * as dagJson from '@ipld/dag-json'
|
|
232
|
+
*
|
|
233
|
+
* const block = new TextEncoder().encode(`{
|
|
234
|
+
* "hello": "world",
|
|
235
|
+
* "cid": {
|
|
236
|
+
* "/": "baeaaac3imvwgy3zao5xxe3de"
|
|
237
|
+
* },
|
|
238
|
+
* "buf": {
|
|
239
|
+
* "/": {
|
|
240
|
+
* "bytes": "AAECAwQ"
|
|
241
|
+
* }
|
|
242
|
+
* }
|
|
243
|
+
* }`)
|
|
244
|
+
*
|
|
245
|
+
* const obj = dagJson.decode(block)
|
|
246
|
+
*
|
|
247
|
+
* console.info(obj)
|
|
248
|
+
* // {
|
|
249
|
+
* // hello: 'world',
|
|
250
|
+
* // cid: CID(baeaaac3imvwgy3zao5xxe3de),
|
|
251
|
+
* // buf: Uint8Array(5) [ 0, 1, 2, 3, 4 ]
|
|
252
|
+
* // }
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
* ##### Content-Type
|
|
256
|
+
*
|
|
257
|
+
* When the `DAG-JSON` codec is encountered in the requested CID, the `Content-Type` header of the response will be set to `application/json`.
|
|
258
|
+
*
|
|
259
|
+
* `DAG-JSON` data can be parsed from the response by using the `.json()` function, which will return `CID`s/byte arrays as plain `{ "/": ... }` objects:
|
|
260
|
+
*
|
|
261
|
+
* ```typescript
|
|
262
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
263
|
+
* import * as dagJson from '@ipld/dag-json'
|
|
264
|
+
*
|
|
265
|
+
* const res = await verifiedFetch('ipfs://bafyDAGJSON')
|
|
266
|
+
*
|
|
267
|
+
* // either:
|
|
268
|
+
* const obj = await res.json()
|
|
269
|
+
* console.info(obj.cid) // { "/": "baeaaac3imvwgy3zao5xxe3de" }
|
|
270
|
+
* console.info(obj.buf) // { "/": { "bytes": "AAECAwQ" } }
|
|
271
|
+
* ```
|
|
272
|
+
*
|
|
273
|
+
* 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:
|
|
274
|
+
*
|
|
275
|
+
*```typescript
|
|
276
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
277
|
+
* import * as dagJson from '@ipld/dag-json'
|
|
278
|
+
*
|
|
279
|
+
* const res = await verifiedFetch('ipfs://bafyDAGJSON')
|
|
280
|
+
*
|
|
281
|
+
* // or:
|
|
282
|
+
* const obj = dagJson.decode(await res.arrayBuffer())
|
|
283
|
+
* console.info(obj.cid) // CID(baeaaac3imvwgy3zao5xxe3de)
|
|
284
|
+
* console.info(obj.buf) // Uint8Array(5) [ 0, 1, 2, 3, 4 ]
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* #### DAG-CBOR
|
|
288
|
+
*
|
|
289
|
+
* [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.
|
|
290
|
+
*
|
|
291
|
+
* 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.
|
|
292
|
+
*
|
|
293
|
+
* ##### Content-Type
|
|
294
|
+
*
|
|
295
|
+
* Not all data types supported by `DAG-CBOR` can be successfully turned into JSON and back into the same binary form.
|
|
296
|
+
*
|
|
297
|
+
* 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.
|
|
298
|
+
*
|
|
299
|
+
* 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()`.
|
|
300
|
+
*
|
|
301
|
+
* ##### Detecting JSON-safe DAG-CBOR
|
|
302
|
+
*
|
|
303
|
+
* 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.
|
|
304
|
+
*
|
|
305
|
+
* ```typescript
|
|
306
|
+
* import { verifiedFetch } from '@helia/verified-fetch'
|
|
307
|
+
* import * as dagCbor from '@ipld/dag-cbor'
|
|
308
|
+
*
|
|
309
|
+
* const res = await verifiedFetch('ipfs://bafyDagCborCID')
|
|
310
|
+
* let obj
|
|
311
|
+
*
|
|
312
|
+
* if (res.headers.get('Content-Type') === 'application/json') {
|
|
313
|
+
* // DAG-CBOR data can be safely decoded as JSON
|
|
314
|
+
* obj = await res.json()
|
|
315
|
+
* } else {
|
|
316
|
+
* // response contains non-JSON friendly data types
|
|
317
|
+
* obj = dagCbor.decode(await res.arrayBuffer())
|
|
318
|
+
* }
|
|
319
|
+
*
|
|
320
|
+
* console.info(obj) // ...
|
|
321
|
+
* ```
|
|
322
|
+
*
|
|
141
323
|
* ## Comparison to fetch
|
|
142
324
|
*
|
|
143
325
|
* This module attempts to act as similarly to the `fetch()` API as possible.
|
|
@@ -155,7 +337,7 @@
|
|
|
155
337
|
* 2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
|
|
156
338
|
* 3. CID instances: An actual CID instance `CID.parse('bafy...')`
|
|
157
339
|
*
|
|
158
|
-
* As well as support for pathing & params for
|
|
340
|
+
* 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).
|
|
159
341
|
*
|
|
160
342
|
* 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.
|
|
161
343
|
*
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkbG;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,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GAAG,EAAE,KAAK,IAAI,IAAI,CAAA;AACtB,CAAC"}
|
|
@@ -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 @@
|
|
|
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,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"}
|
|
@@ -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,14 @@ 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
|
+
}
|
|
12
16
|
export interface ParsedUrlStringResults {
|
|
13
17
|
protocol: string;
|
|
14
18
|
path: string;
|
|
15
19
|
cid: CID;
|
|
16
|
-
query:
|
|
20
|
+
query: ParsedUrlQuery;
|
|
17
21
|
}
|
|
18
22
|
/**
|
|
19
23
|
* 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;AAEtC,OAAO,KAAK,EAAE,IAAI,EAAE,iBAAiB,EAAE,4BAA4B,EAAE,qBAAqB,EAAiB,MAAM,aAAa,CAAA;AAC9H,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACxD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAItD,MAAM,WAAW,mBAAmB;IAClC,SAAS,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,IAAI,CAAA;IACV,MAAM,EAAE,eAAe,CAAA;CACxB;AACD,MAAM,WAAW,qBAAsB,SAAQ,eAAe,CAAC,qBAAqB,G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B,CAAC,CAwGxJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-url-string.js","sourceRoot":"","sources":["../../../src/utils/parse-url-string.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;
|
|
1
|
+
{"version":3,"file":"parse-url-string.js","sourceRoot":"","sources":["../../../src/utils/parse-url-string.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAA;AAMhC,MAAM,SAAS,GAAG,IAAI,IAAI,CAAgB,IAAI,CAAC,CAAA;AAsB/C,MAAM,SAAS,GAAG,oGAAoG,CAAA;AAEtH;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAE,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,EAAuB,EAAE,OAA+B;IACrH,MAAM,GAAG,GAAG,MAAM,CAAC,YAAY,CAAC,uCAAuC,CAAC,CAAA;IACxE,MAAM,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,CAAA;IAExC,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,CAAC,MAAM,IAAI,IAAI,EAAE,CAAC;QAC1C,MAAM,IAAI,SAAS,CAAC,gBAAgB,SAAS,4CAA4C,CAAC,CAAA;IAC5F,CAAC;IAED,MAAM,EAAE,QAAQ,EAAE,oBAAoB,EAAE,IAAI,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,KAAK,CAAC,MAAM,CAAA;IAEnF,IAAI,GAAoB,CAAA;IACxB,IAAI,YAAgC,CAAA;IACpC,MAAM,MAAM,GAAY,EAAE,CAAA;I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