@helia/verified-fetch 0.0.0-7a7c0c1 → 0.0.0-7c3ce21
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 +238 -6
- package/dist/index.min.js +4 -4
- package/dist/src/index.d.ts +222 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +219 -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-content-disposition-filename.d.ts +6 -0
- package/dist/src/utils/get-content-disposition-filename.d.ts.map +1 -0
- package/dist/src/utils/get-content-disposition-filename.js +16 -0
- package/dist/src/utils/get-content-disposition-filename.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 +7 -1
- package/dist/src/utils/parse-url-string.d.ts.map +1 -1
- package/dist/src/utils/parse-url-string.js +6 -0
- package/dist/src/utils/parse-url-string.js.map +1 -1
- package/dist/src/utils/responses.d.ts +4 -0
- package/dist/src/utils/responses.d.ts.map +1 -0
- package/dist/src/utils/responses.js +21 -0
- package/dist/src/utils/responses.js.map +1 -0
- package/dist/src/utils/select-output-type.d.ts +12 -0
- package/dist/src/utils/select-output-type.d.ts.map +1 -0
- package/dist/src/utils/select-output-type.js +147 -0
- package/dist/src/utils/select-output-type.js.map +1 -0
- package/dist/src/verified-fetch.d.ts +17 -25
- package/dist/src/verified-fetch.d.ts.map +1 -1
- package/dist/src/verified-fetch.js +220 -143
- package/dist/src/verified-fetch.js.map +1 -1
- package/package.json +25 -15
- package/src/index.ts +223 -4
- package/src/types.ts +1 -0
- package/src/utils/dag-cbor-to-safe-json.ts +44 -0
- package/src/utils/get-content-disposition-filename.ts +18 -0
- package/src/utils/get-e-tag.ts +36 -0
- package/src/utils/parse-url-string.ts +17 -2
- package/src/utils/responses.ts +22 -0
- package/src/utils/select-output-type.ts +166 -0
- package/src/verified-fetch.ts +251 -153
package/README.md
CHANGED
|
@@ -13,9 +13,24 @@
|
|
|
13
13
|
|
|
14
14
|
# About
|
|
15
15
|
|
|
16
|
+
<!--
|
|
17
|
+
|
|
18
|
+
!IMPORTANT!
|
|
19
|
+
|
|
20
|
+
Everything in this README between "# About" and "# Install" is automatically
|
|
21
|
+
generated and will be overwritten the next time the doc generator is run.
|
|
22
|
+
|
|
23
|
+
To make changes to this section, please update the @packageDocumentation section
|
|
24
|
+
of src/index.js or src/index.ts
|
|
25
|
+
|
|
26
|
+
To experiment with formatting, please run "npm run docs" from the root of this
|
|
27
|
+
repo and examine the changes made.
|
|
28
|
+
|
|
29
|
+
-->
|
|
30
|
+
|
|
16
31
|
`@helia/verified-fetch` provides a [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)-like API for retrieving content from the [IPFS](https://ipfs.tech/) network.
|
|
17
32
|
|
|
18
|
-
All content is retrieved in a [trustless manner](https://www.techopedia.com/definition/trustless), and the integrity of all bytes are verified by comparing hashes of the data.
|
|
33
|
+
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/).
|
|
19
34
|
|
|
20
35
|
This is a marked improvement over `fetch` which offers no such protections and is vulnerable to all sorts of attacks like [Content Spoofing](https://owasp.org/www-community/attacks/Content_Spoofing), [DNS Hijacking](https://en.wikipedia.org/wiki/DNS_hijacking), etc.
|
|
21
36
|
|
|
@@ -45,7 +60,7 @@ const json = await resp.json()
|
|
|
45
60
|
import { verifiedFetch } from '@helia/verified-fetch'
|
|
46
61
|
import { CID } from 'multiformats/cid'
|
|
47
62
|
|
|
48
|
-
const cid = CID.parse('bafyFoo') // some
|
|
63
|
+
const cid = CID.parse('bafyFoo') // some json file
|
|
49
64
|
const response = await verifiedFetch(cid)
|
|
50
65
|
const json = await response.json()
|
|
51
66
|
```
|
|
@@ -126,7 +141,7 @@ const json = await resp.json()
|
|
|
126
141
|
|
|
127
142
|
### Custom content-type parsing
|
|
128
143
|
|
|
129
|
-
By default, `@helia/verified-fetch` sets the `Content-Type` header as `application/octet-stream` - this is because the `.json()`, `.text()`, `.blob()`, and `.arrayBuffer()` methods will usually work as expected without a detailed content type.
|
|
144
|
+
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.
|
|
130
145
|
|
|
131
146
|
If you require an accurate content-type you can provide a `contentTypeParser` function as an option to `createVerifiedFetch` to handle parsing the content type.
|
|
132
147
|
|
|
@@ -140,14 +155,231 @@ import { fileTypeFromBuffer } from '@sgtpooki/file-type'
|
|
|
140
155
|
|
|
141
156
|
const fetch = await createVerifiedFetch({
|
|
142
157
|
gateways: ['https://trustless-gateway.link'],
|
|
143
|
-
routers: ['http://delegated-ipfs.dev']
|
|
158
|
+
routers: ['http://delegated-ipfs.dev']
|
|
159
|
+
}, {
|
|
144
160
|
contentTypeParser: async (bytes) => {
|
|
145
161
|
// call to some magic-byte recognition library like magic-bytes, file-type, or your own custom byte recognition
|
|
146
|
-
|
|
162
|
+
const result = await fileTypeFromBuffer(bytes)
|
|
163
|
+
return result?.mime
|
|
147
164
|
}
|
|
148
165
|
})
|
|
149
166
|
```
|
|
150
167
|
|
|
168
|
+
### IPLD codec handling
|
|
169
|
+
|
|
170
|
+
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.
|
|
171
|
+
|
|
172
|
+
#### DAG-PB
|
|
173
|
+
|
|
174
|
+
[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.
|
|
175
|
+
|
|
176
|
+
##### Using the DAG-PB codec as a Blob
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
180
|
+
|
|
181
|
+
const res = await verifiedFetch('ipfs://Qmfoo')
|
|
182
|
+
const blob = await res.blob()
|
|
183
|
+
|
|
184
|
+
console.info(blob) // Blob { size: x, type: 'application/octet-stream' }
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
##### Using the DAG-PB codec as an ArrayBuffer
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
191
|
+
|
|
192
|
+
const res = await verifiedFetch('ipfs://Qmfoo')
|
|
193
|
+
const buf = await res.arrayBuffer()
|
|
194
|
+
|
|
195
|
+
console.info(buf) // ArrayBuffer { [Uint8Contents]: < ... >, byteLength: x }
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
##### Using the DAG-PB codec as a stream
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
202
|
+
|
|
203
|
+
const res = await verifiedFetch('ipfs://Qmfoo')
|
|
204
|
+
const reader = res.body?.getReader()
|
|
205
|
+
|
|
206
|
+
while (true) {
|
|
207
|
+
const next = await reader.read()
|
|
208
|
+
|
|
209
|
+
if (next?.done === true) {
|
|
210
|
+
break
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (next?.value != null) {
|
|
214
|
+
console.info(next.value) // Uint8Array(x) [ ... ]
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
##### Content-Type
|
|
220
|
+
|
|
221
|
+
When fetching `DAG-PB` data, the content type will be set to `application/octet-stream` unless a custom content-type parser is configured.
|
|
222
|
+
|
|
223
|
+
#### JSON
|
|
224
|
+
|
|
225
|
+
The JSON codec is a very simple codec, a block parseable with this codec is a JSON string encoded into a `Uint8Array`.
|
|
226
|
+
|
|
227
|
+
##### Using the JSON codec
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
import * as json from 'multiformats/codecs/json'
|
|
231
|
+
|
|
232
|
+
const block = new TextEncoder().encode('{ "hello": "world" }')
|
|
233
|
+
const obj = json.decode(block)
|
|
234
|
+
|
|
235
|
+
console.info(obj) // { hello: 'world' }
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
##### Content-Type
|
|
239
|
+
|
|
240
|
+
When the `JSON` codec is encountered, the `Content-Type` header of the response will be set to `application/json`.
|
|
241
|
+
|
|
242
|
+
### DAG-JSON
|
|
243
|
+
|
|
244
|
+
[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.
|
|
245
|
+
|
|
246
|
+
`CID`s and byte arrays are represented using special object structures with a single `"/"` property.
|
|
247
|
+
|
|
248
|
+
Using `DAG-JSON` has two important caveats:
|
|
249
|
+
|
|
250
|
+
1. Your `JSON` structure cannot contain an object with only a `"/"` property, as it will be interpreted as a special type.
|
|
251
|
+
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.
|
|
252
|
+
|
|
253
|
+
Otherwise this codec follows the same rules as the `JSON` codec.
|
|
254
|
+
|
|
255
|
+
##### Using the DAG-JSON codec
|
|
256
|
+
|
|
257
|
+
```typescript
|
|
258
|
+
import * as dagJson from '@ipld/dag-json'
|
|
259
|
+
|
|
260
|
+
const block = new TextEncoder().encode(`{
|
|
261
|
+
"hello": "world",
|
|
262
|
+
"cid": {
|
|
263
|
+
"/": "baeaaac3imvwgy3zao5xxe3de"
|
|
264
|
+
},
|
|
265
|
+
"buf": {
|
|
266
|
+
"/": {
|
|
267
|
+
"bytes": "AAECAwQ"
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}`)
|
|
271
|
+
|
|
272
|
+
const obj = dagJson.decode(block)
|
|
273
|
+
|
|
274
|
+
console.info(obj)
|
|
275
|
+
// {
|
|
276
|
+
// hello: 'world',
|
|
277
|
+
// cid: CID(baeaaac3imvwgy3zao5xxe3de),
|
|
278
|
+
// buf: Uint8Array(5) [ 0, 1, 2, 3, 4 ]
|
|
279
|
+
// }
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
##### Content-Type
|
|
283
|
+
|
|
284
|
+
When the `DAG-JSON` codec is encountered in the requested CID, the `Content-Type` header of the response will be set to `application/json`.
|
|
285
|
+
|
|
286
|
+
`DAG-JSON` data can be parsed from the response by using the `.json()` function, which will return `CID`s/byte arrays as plain `{ "/": ... }` objects:
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
290
|
+
import * as dagJson from '@ipld/dag-json'
|
|
291
|
+
|
|
292
|
+
const res = await verifiedFetch('ipfs://bafyDAGJSON')
|
|
293
|
+
|
|
294
|
+
// either:
|
|
295
|
+
const obj = await res.json()
|
|
296
|
+
console.info(obj.cid) // { "/": "baeaaac3imvwgy3zao5xxe3de" }
|
|
297
|
+
console.info(obj.buf) // { "/": { "bytes": "AAECAwQ" } }
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
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:
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
304
|
+
import * as dagJson from '@ipld/dag-json'
|
|
305
|
+
|
|
306
|
+
const res = await verifiedFetch('ipfs://bafyDAGJSON')
|
|
307
|
+
|
|
308
|
+
// or:
|
|
309
|
+
const obj = dagJson.decode(await res.arrayBuffer())
|
|
310
|
+
console.info(obj.cid) // CID(baeaaac3imvwgy3zao5xxe3de)
|
|
311
|
+
console.info(obj.buf) // Uint8Array(5) [ 0, 1, 2, 3, 4 ]
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
#### DAG-CBOR
|
|
315
|
+
|
|
316
|
+
[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.
|
|
317
|
+
|
|
318
|
+
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.
|
|
319
|
+
|
|
320
|
+
##### Content-Type
|
|
321
|
+
|
|
322
|
+
Not all data types supported by `DAG-CBOR` can be successfully turned into JSON and back into the same binary form.
|
|
323
|
+
|
|
324
|
+
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.
|
|
325
|
+
|
|
326
|
+
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()`.
|
|
327
|
+
|
|
328
|
+
##### Detecting JSON-safe DAG-CBOR
|
|
329
|
+
|
|
330
|
+
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.
|
|
331
|
+
|
|
332
|
+
```typescript
|
|
333
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
334
|
+
import * as dagCbor from '@ipld/dag-cbor'
|
|
335
|
+
|
|
336
|
+
const res = await verifiedFetch('ipfs://bafyDagCborCID')
|
|
337
|
+
let obj
|
|
338
|
+
|
|
339
|
+
if (res.headers.get('Content-Type') === 'application/json') {
|
|
340
|
+
// DAG-CBOR data can be safely decoded as JSON
|
|
341
|
+
obj = await res.json()
|
|
342
|
+
} else {
|
|
343
|
+
// response contains non-JSON friendly data types
|
|
344
|
+
obj = dagCbor.decode(await res.arrayBuffer())
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
console.info(obj) // ...
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## The `Accept` header
|
|
351
|
+
|
|
352
|
+
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.
|
|
353
|
+
|
|
354
|
+
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 Acceptible](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/406) response will be returned:
|
|
355
|
+
|
|
356
|
+
```typescript
|
|
357
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
358
|
+
|
|
359
|
+
const res = await verifiedFetch('ipfs://bafyJPEGImageCID', {
|
|
360
|
+
headers: {
|
|
361
|
+
accept: 'image/png'
|
|
362
|
+
}
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
console.info(res.status) // 406 - the image was a JPEG but we specified PNG as the accept header
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
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:
|
|
369
|
+
|
|
370
|
+
```typescript
|
|
371
|
+
import { verifiedFetch } from '@helia/verified-fetch'
|
|
372
|
+
|
|
373
|
+
const res = await verifiedFetch('ipfs://bafyDAGCBORCID', {
|
|
374
|
+
headers: {
|
|
375
|
+
accept: 'application/octet-stream'
|
|
376
|
+
}
|
|
377
|
+
})
|
|
378
|
+
|
|
379
|
+
console.info(res.headers.get('accept')) // application/octet-stream
|
|
380
|
+
const buf = await res.arrayBuffer() // raw bytes, not processed as JSON
|
|
381
|
+
```
|
|
382
|
+
|
|
151
383
|
## Comparison to fetch
|
|
152
384
|
|
|
153
385
|
This module attempts to act as similarly to the `fetch()` API as possible.
|
|
@@ -165,7 +397,7 @@ This library supports the following methods of fetching web3 content from IPFS:
|
|
|
165
397
|
2. IPNS protocol: `ipns://<peerId>` & `ipns://<publicKey>` & `ipns://<hostUri_Supporting_DnsLink_TxtRecords>`
|
|
166
398
|
3. CID instances: An actual CID instance `CID.parse('bafy...')`
|
|
167
399
|
|
|
168
|
-
As well as support for pathing & params for
|
|
400
|
+
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).
|
|
169
401
|
|
|
170
402
|
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.
|
|
171
403
|
|