-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathverified-fetch.ts
610 lines (518 loc) · 22.8 KB
/
verified-fetch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
import { car } from '@helia/car'
import { ipns as heliaIpns, type IPNS } from '@helia/ipns'
import { unixfs as heliaUnixFs, type UnixFS as HeliaUnixFs } from '@helia/unixfs'
import * as ipldDagCbor from '@ipld/dag-cbor'
import * as ipldDagJson from '@ipld/dag-json'
import { code as dagPbCode } from '@ipld/dag-pb'
import { type AbortOptions, type Logger, type PeerId } from '@libp2p/interface'
import { Record as DHTRecord } from '@libp2p/kad-dht'
import { peerIdFromString } from '@libp2p/peer-id'
import { Key } from 'interface-datastore'
import toBrowserReadableStream from 'it-to-browser-readablestream'
import { code as jsonCode } from 'multiformats/codecs/json'
import { code as rawCode } from 'multiformats/codecs/raw'
import { identity } from 'multiformats/hashes/identity'
import { CustomProgressEvent } from 'progress-events'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { ByteRangeContext } from './utils/byte-range-context.js'
import { dagCborToSafeJSON } from './utils/dag-cbor-to-safe-json.js'
import { getContentDispositionFilename } from './utils/get-content-disposition-filename.js'
import { getETag } from './utils/get-e-tag.js'
import { getResolvedAcceptHeader } from './utils/get-resolved-accept-header.js'
import { getStreamFromAsyncIterable } from './utils/get-stream-from-async-iterable.js'
import { tarStream } from './utils/get-tar-stream.js'
import { parseResource } from './utils/parse-resource.js'
import { setCacheControlHeader } from './utils/response-headers.js'
import { badRequestResponse, movedPermanentlyResponse, notAcceptableResponse, notSupportedResponse, okResponse, badRangeResponse, okRangeResponse, badGatewayResponse, notFoundResponse } from './utils/responses.js'
import { selectOutputType } from './utils/select-output-type.js'
import { isObjectNode, walkPath } from './utils/walk-path.js'
import type { CIDDetail, ContentTypeParser, Resource, VerifiedFetchInit as VerifiedFetchOptions } from './index.js'
import type { RequestFormatShorthand } from './types.js'
import type { ParsedUrlStringResults } from './utils/parse-url-string'
import type { Helia } from '@helia/interface'
import type { DNSResolver } from '@multiformats/dns/resolvers'
import type { ObjectNode, UnixFSEntry } from 'ipfs-unixfs-exporter'
import type { CID } from 'multiformats/cid'
interface VerifiedFetchComponents {
helia: Helia
ipns?: IPNS
unixfs?: HeliaUnixFs
}
/**
* Potential future options for the VerifiedFetch constructor.
*/
interface VerifiedFetchInit {
contentTypeParser?: ContentTypeParser
dnsResolvers?: DNSResolver[]
}
interface FetchHandlerFunctionArg {
cid: CID
path: string
options?: Omit<VerifiedFetchOptions, 'signal'> & AbortOptions
/**
* If present, the user has sent an accept header with this value - if the
* content cannot be represented in this format a 406 should be returned
*/
accept?: string
/**
* The originally requested resource
*/
resource: string
}
interface FetchHandlerFunction {
(options: FetchHandlerFunctionArg): Promise<Response>
}
function convertOptions (options?: VerifiedFetchOptions): (Omit<VerifiedFetchOptions, 'signal'> & AbortOptions) | undefined {
if (options == null) {
return undefined
}
let signal: AbortSignal | undefined
if (options?.signal === null) {
signal = undefined
} else {
signal = options?.signal
}
return {
...options,
signal
}
}
/**
* These are Accept header values that will cause content type sniffing to be
* skipped and set to these values.
*/
const RAW_HEADERS = [
'application/vnd.ipld.dag-json',
'application/vnd.ipld.raw',
'application/octet-stream'
]
/**
* if the user has specified an `Accept` header, and it's in our list of
* allowable "raw" format headers, use that instead of detecting the content
* type. This avoids the user from receiving something different when they
* signal that they want to `Accept` a specific mime type.
*/
function getOverridenRawContentType ({ headers, accept }: { headers?: HeadersInit, accept?: string }): string | undefined {
// accept has already been resolved by getResolvedAcceptHeader, if we have it, use it.
const acceptHeader = accept ?? new Headers(headers).get('accept') ?? ''
// e.g. "Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8"
const acceptHeaders = acceptHeader.split(',')
.map(s => s.split(';')[0])
.map(s => s.trim())
for (const mimeType of acceptHeaders) {
if (mimeType === '*/*') {
return
}
if (RAW_HEADERS.includes(mimeType ?? '')) {
return mimeType
}
}
}
export class VerifiedFetch {
private readonly helia: Helia
private readonly ipns: IPNS
private readonly unixfs: HeliaUnixFs
private readonly log: Logger
private readonly contentTypeParser: ContentTypeParser | undefined
constructor ({ helia, ipns, unixfs }: VerifiedFetchComponents, init?: VerifiedFetchInit) {
this.helia = helia
this.log = helia.logger.forComponent('helia:verified-fetch')
this.ipns = ipns ?? heliaIpns(helia)
this.unixfs = unixfs ?? heliaUnixFs(helia)
this.contentTypeParser = init?.contentTypeParser
this.log.trace('created VerifiedFetch instance')
}
/**
* Accepts an `ipns://...` URL as a string and returns a `Response` containing
* a raw IPNS record.
*/
private async handleIPNSRecord ({ resource, cid, path, options }: FetchHandlerFunctionArg): Promise<Response> {
if (path !== '' || !resource.startsWith('ipns://')) {
return badRequestResponse('Invalid IPNS name')
}
let peerId: PeerId
try {
peerId = peerIdFromString(resource.replace('ipns://', ''))
} catch (err) {
this.log.error('could not parse peer id from IPNS url %s', resource)
return badRequestResponse('Invalid IPNS name')
}
// since this call happens after parseResource, we've already resolved the
// IPNS name so a local copy should be in the helia datastore, so we can
// just read it out..
const routingKey = uint8ArrayConcat([
uint8ArrayFromString('/ipns/'),
peerId.toBytes()
])
const datastoreKey = new Key('/dht/record/' + uint8ArrayToString(routingKey, 'base32'), false)
const buf = await this.helia.datastore.get(datastoreKey, options)
const record = DHTRecord.deserialize(buf)
const response = okResponse(resource, record.value)
response.headers.set('content-type', 'application/vnd.ipfs.ipns-record')
return response
}
/**
* Accepts a `CID` and returns a `Response` with a body stream that is a CAR
* of the `DAG` referenced by the `CID`.
*/
private async handleCar ({ resource, cid, options }: FetchHandlerFunctionArg): Promise<Response> {
const c = car(this.helia)
const stream = toBrowserReadableStream(c.stream(cid, options))
const response = okResponse(resource, stream)
response.headers.set('content-type', 'application/vnd.ipld.car; version=1')
return response
}
/**
* Accepts a UnixFS `CID` and returns a `.tar` file containing the file or
* directory structure referenced by the `CID`.
*/
private async handleTar ({ resource, cid, path, options }: FetchHandlerFunctionArg): Promise<Response> {
if (cid.code !== dagPbCode && cid.code !== rawCode) {
return notAcceptableResponse('only UnixFS data can be returned in a TAR file')
}
const stream = toBrowserReadableStream<Uint8Array>(tarStream(`/ipfs/${cid}/${path}`, this.helia.blockstore, options))
const response = okResponse(resource, stream)
response.headers.set('content-type', 'application/x-tar')
return response
}
private async handleJson ({ resource, cid, path, accept, options }: FetchHandlerFunctionArg): Promise<Response> {
this.log.trace('fetching %c/%s', cid, path)
const block = await this.helia.blockstore.get(cid, options)
let body: string | Uint8Array
if (accept === 'application/vnd.ipld.dag-cbor' || accept === 'application/cbor') {
try {
// if vnd.ipld.dag-cbor has been specified, convert to the format - note
// that this supports more data types than regular JSON, the content-type
// response header is set so the user knows to process it differently
const obj = ipldDagJson.decode(block)
body = ipldDagCbor.encode(obj)
} catch (err) {
this.log.error('could not transform %c to application/vnd.ipld.dag-cbor', err)
return notAcceptableResponse(resource)
}
} else {
// skip decoding
body = block
}
const response = okResponse(resource, body)
response.headers.set('content-type', accept ?? 'application/json')
return response
}
private async handleDagCbor ({ resource, cid, path, accept, options }: FetchHandlerFunctionArg): Promise<Response> {
this.log.trace('fetching %c/%s', cid, path)
let terminalElement: ObjectNode | undefined
let ipfsRoots: CID[] | undefined
// need to walk path, if it exists, to get the terminal element
try {
const pathDetails = await walkPath(this.helia.blockstore, `${cid.toString()}/${path}`, options)
ipfsRoots = pathDetails.ipfsRoots
const potentialTerminalElement = pathDetails.terminalElement
if (potentialTerminalElement == null) {
return notFoundResponse(resource)
}
if (isObjectNode(potentialTerminalElement)) {
terminalElement = potentialTerminalElement
}
} catch (err: any) {
options?.signal?.throwIfAborted()
if (['ERR_NO_PROP', 'ERR_NO_TERMINAL_ELEMENT'].includes(err.code)) {
return notFoundResponse(resource)
}
this.log.error('error walking path %s', path, err)
return badGatewayResponse(resource, 'Error walking path')
}
const block = terminalElement?.node ?? await this.helia.blockstore.get(cid, options)
let body: string | Uint8Array
if (accept === 'application/octet-stream' || accept === 'application/vnd.ipld.dag-cbor' || accept === 'application/cbor') {
// skip decoding
body = block
} else if (accept === 'application/vnd.ipld.dag-json') {
try {
// if vnd.ipld.dag-json has been specified, convert to the format - note
// that this supports more data types than regular JSON, the content-type
// response header is set so the user knows to process it differently
const obj = ipldDagCbor.decode(block)
body = ipldDagJson.encode(obj)
} catch (err) {
this.log.error('could not transform %c to application/vnd.ipld.dag-json', err)
return notAcceptableResponse(resource)
}
} else {
try {
body = dagCborToSafeJSON(block)
} catch (err) {
if (accept === 'application/json') {
this.log('could not decode DAG-CBOR as JSON-safe, but the client sent "Accept: application/json"', err)
return notAcceptableResponse(resource)
}
this.log('could not decode DAG-CBOR as JSON-safe, falling back to `application/octet-stream`', err)
body = block
}
}
const response = okResponse(resource, body)
if (accept == null) {
accept = body instanceof Uint8Array ? 'application/octet-stream' : 'application/json'
}
response.headers.set('content-type', accept)
if (ipfsRoots != null) {
response.headers.set('X-Ipfs-Roots', ipfsRoots.map(cid => cid.toV1().toString()).join(',')) // https://specs.ipfs.tech/http-gateways/path-gateway/#x-ipfs-roots-response-header
}
return response
}
private async handleDagPb ({ cid, path, resource, options }: FetchHandlerFunctionArg): Promise<Response> {
let terminalElement: UnixFSEntry | undefined
let ipfsRoots: CID[] | undefined
let redirected = false
const byteRangeContext = new ByteRangeContext(this.helia.logger, options?.headers)
try {
const pathDetails = await walkPath(this.helia.blockstore, `${cid.toString()}/${path}`, options)
ipfsRoots = pathDetails.ipfsRoots
terminalElement = pathDetails.terminalElement
} catch (err: any) {
options?.signal?.throwIfAborted()
if (['ERR_NO_PROP', 'ERR_NO_TERMINAL_ELEMENT'].includes(err.code)) {
return notFoundResponse(resource.toString())
}
this.log.error('error walking path %s', path, err)
return badGatewayResponse(resource.toString(), 'Error walking path')
}
let resolvedCID = terminalElement?.cid ?? cid
if (terminalElement?.type === 'directory') {
const dirCid = terminalElement.cid
const redirectCheckNeeded = path === '' ? !resource.toString().endsWith('/') : !path.endsWith('/')
// https://specs.ipfs.tech/http-gateways/path-gateway/#use-in-directory-url-normalization
if (redirectCheckNeeded) {
if (options?.redirect === 'error') {
this.log('could not redirect to %s/ as redirect option was set to "error"', resource)
throw new TypeError('Failed to fetch')
} else if (options?.redirect === 'manual') {
this.log('returning 301 permanent redirect to %s/', resource)
return movedPermanentlyResponse(resource, `${resource}/`)
}
// fall-through simulates following the redirect?
resource = `${resource}/`
redirected = true
}
const rootFilePath = 'index.html'
try {
this.log.trace('found directory at %c/%s, looking for index.html', cid, path)
const stat = await this.unixfs.stat(dirCid, {
path: rootFilePath,
signal: options?.signal,
onProgress: options?.onProgress
})
this.log.trace('found root file at %c/%s with cid %c', dirCid, rootFilePath, stat.cid)
path = rootFilePath
resolvedCID = stat.cid
} catch (err: any) {
options?.signal?.throwIfAborted()
this.log('error loading path %c/%s', dirCid, rootFilePath, err)
return notSupportedResponse('Unable to find index.html for directory at given path. Support for directories with implicit root is not implemented')
} finally {
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:end', { cid: dirCid, path: rootFilePath }))
}
}
// we have a validRangeRequest & terminalElement is a file, we know the size and should set it
if (byteRangeContext.isRangeRequest && byteRangeContext.isValidRangeRequest && terminalElement.type === 'file') {
byteRangeContext.setFileSize(terminalElement.unixfs.fileSize())
this.log.trace('fileSize for rangeRequest %d', byteRangeContext.getFileSize())
}
const offset = byteRangeContext.offset
const length = byteRangeContext.length
this.log.trace('calling unixfs.cat for %c/%s with offset=%o & length=%o', resolvedCID, path, offset, length)
const asyncIter = this.unixfs.cat(resolvedCID, {
signal: options?.signal,
onProgress: options?.onProgress,
offset,
length
})
this.log('got async iterator for %c/%s', cid, path)
try {
const { stream, firstChunk } = await getStreamFromAsyncIterable(asyncIter, path ?? '', this.helia.logger, {
onProgress: options?.onProgress,
signal: options?.signal
})
byteRangeContext.setBody(stream)
// if not a valid range request, okRangeRequest will call okResponse
const response = okRangeResponse(resource, byteRangeContext.getBody(), { byteRangeContext, log: this.log }, {
redirected
})
await this.setContentType(firstChunk, path, response)
if (ipfsRoots != null) {
response.headers.set('X-Ipfs-Roots', ipfsRoots.map(cid => cid.toV1().toString()).join(',')) // https://specs.ipfs.tech/http-gateways/path-gateway/#x-ipfs-roots-response-header
}
return response
} catch (err: any) {
options?.signal?.throwIfAborted()
this.log.error('error streaming %c/%s', cid, path, err)
if (byteRangeContext.isRangeRequest && err.code === 'ERR_INVALID_PARAMS') {
return badRangeResponse(resource)
}
return badGatewayResponse(resource.toString(), 'Unable to stream content')
}
}
private async handleRaw ({ resource, cid, path, options, accept }: FetchHandlerFunctionArg): Promise<Response> {
const byteRangeContext = new ByteRangeContext(this.helia.logger, options?.headers)
const result = await this.helia.blockstore.get(cid, options)
byteRangeContext.setBody(result)
const response = okRangeResponse(resource, byteRangeContext.getBody(), { byteRangeContext, log: this.log }, {
redirected: false
})
// if the user has specified an `Accept` header that corresponds to a raw
// type, honour that header, so for example they don't request
// `application/vnd.ipld.raw` but get `application/octet-stream`
const overriddenContentType = getOverridenRawContentType({ headers: options?.headers, accept })
if (overriddenContentType != null) {
response.headers.set('content-type', overriddenContentType)
} else {
await this.setContentType(result, path, response)
}
return response
}
private async setContentType (bytes: Uint8Array, path: string, response: Response): Promise<void> {
let contentType = 'application/octet-stream'
if (this.contentTypeParser != null) {
try {
let fileName = path.split('/').pop()?.trim()
fileName = fileName === '' ? undefined : fileName
const parsed = this.contentTypeParser(bytes, fileName)
if (isPromise(parsed)) {
const result = await parsed
if (result != null) {
contentType = result
}
} else if (parsed != null) {
contentType = parsed
}
} catch (err) {
this.log.error('error parsing content type', err)
}
}
this.log.trace('setting content type to "%s"', contentType)
response.headers.set('content-type', contentType)
}
/**
* If the user has not specified an Accept header or format query string arg,
* use the CID codec to choose an appropriate handler for the block data.
*/
private readonly codecHandlers: Record<number, FetchHandlerFunction> = {
[dagPbCode]: this.handleDagPb,
[ipldDagJson.code]: this.handleJson,
[jsonCode]: this.handleJson,
[ipldDagCbor.code]: this.handleDagCbor,
[rawCode]: this.handleRaw,
[identity.code]: this.handleRaw
}
/**
* We're starting to get to the point where we need a queue or pipeline of
* operations to perform and a single place to handle errors.
*
* TODO: move operations called by fetch to a queue of operations where we can
* always exit early (and cleanly) if a given signal is aborted
*/
async fetch (resource: Resource, opts?: VerifiedFetchOptions): Promise<Response> {
this.log('fetch %s', resource)
const options = convertOptions(opts)
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:start', { resource }))
// resolve the CID/path from the requested resource
let cid: ParsedUrlStringResults['cid']
let path: ParsedUrlStringResults['path']
let query: ParsedUrlStringResults['query']
let ttl: ParsedUrlStringResults['ttl']
let protocol: ParsedUrlStringResults['protocol']
let ipfsPath: string
try {
const result = await parseResource(resource, { ipns: this.ipns, logger: this.helia.logger }, options)
cid = result.cid
path = result.path
query = result.query
ttl = result.ttl
protocol = result.protocol
ipfsPath = result.ipfsPath
} catch (err: any) {
options?.signal?.throwIfAborted()
this.log.error('error parsing resource %s', resource, err)
return badRequestResponse(resource.toString(), err)
}
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:resolve', { cid, path }))
const acceptHeader = getResolvedAcceptHeader({ query, headers: options?.headers, logger: this.helia.logger })
const accept = selectOutputType(cid, acceptHeader)
this.log('output type %s', accept)
if (acceptHeader != null && accept == null) {
return notAcceptableResponse(resource.toString())
}
let response: Response
let reqFormat: RequestFormatShorthand | undefined
const handlerArgs: FetchHandlerFunctionArg = { resource: resource.toString(), cid, path, accept, options }
if (accept === 'application/vnd.ipfs.ipns-record') {
// the user requested a raw IPNS record
reqFormat = 'ipns-record'
response = await this.handleIPNSRecord(handlerArgs)
} else if (accept === 'application/vnd.ipld.car') {
// the user requested a CAR file
reqFormat = 'car'
query.download = true
query.filename = query.filename ?? `${cid.toString()}.car`
response = await this.handleCar(handlerArgs)
} else if (accept === 'application/vnd.ipld.raw') {
// the user requested a raw block
reqFormat = 'raw'
query.download = true
query.filename = query.filename ?? `${cid.toString()}.bin`
response = await this.handleRaw(handlerArgs)
} else if (accept === 'application/x-tar') {
// the user requested a TAR file
reqFormat = 'tar'
query.download = true
query.filename = query.filename ?? `${cid.toString()}.tar`
response = await this.handleTar(handlerArgs)
} else {
this.log.trace('finding handler for cid code "%s" and output type "%s"', cid.code, accept)
// derive the handler from the CID type
const codecHandler = this.codecHandlers[cid.code]
if (codecHandler == null) {
return notSupportedResponse(`Support for codec with code ${cid.code} is not yet implemented. Please open an issue at https://github.com/ipfs/helia-verified-fetch/issues/new`)
}
this.log.trace('calling handler "%s"', codecHandler.name)
response = await codecHandler.call(this, handlerArgs)
}
response.headers.set('etag', getETag({ cid, reqFormat, weak: false }))
setCacheControlHeader({ response, ttl, protocol })
response.headers.set('X-Ipfs-Path', ipfsPath)
// set Content-Disposition header
let contentDisposition: string | undefined
// force download if requested
if (query.download === true) {
contentDisposition = 'attachment'
}
// override filename if requested
if (query.filename != null) {
if (contentDisposition == null) {
contentDisposition = 'inline'
}
contentDisposition = `${contentDisposition}; ${getContentDispositionFilename(query.filename)}`
}
if (contentDisposition != null) {
response.headers.set('Content-Disposition', contentDisposition)
}
options?.onProgress?.(new CustomProgressEvent<CIDDetail>('verified-fetch:request:end', { cid, path }))
return response
}
/**
* Start the Helia instance
*/
async start (): Promise<void> {
await this.helia.start()
}
/**
* Shut down the Helia instance
*/
async stop (): Promise<void> {
await this.helia.stop()
}
}
function isPromise <T> (p?: any): p is Promise<T> {
return p?.then != null
}