@helia/verified-fetch 7.2.14 → 7.2.16
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/dist/index.min.js +57 -57
- package/dist/index.min.js.map +4 -4
- package/dist/src/index.d.ts +9 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/plugins/plugin-handle-ipns-record.d.ts +1 -1
- package/dist/src/plugins/plugin-handle-ipns-record.d.ts.map +1 -1
- package/dist/src/plugins/plugin-handle-ipns-record.js +5 -1
- package/dist/src/plugins/plugin-handle-ipns-record.js.map +1 -1
- package/dist/src/plugins/plugin-handle-unixfs.d.ts.map +1 -1
- package/dist/src/plugins/plugin-handle-unixfs.js +2 -1
- package/dist/src/plugins/plugin-handle-unixfs.js.map +1 -1
- package/dist/src/url-resolver.d.ts.map +1 -1
- package/dist/src/url-resolver.js +8 -2
- package/dist/src/url-resolver.js.map +1 -1
- package/dist/src/utils/response-headers.d.ts +12 -4
- package/dist/src/utils/response-headers.d.ts.map +1 -1
- package/dist/src/utils/response-headers.js +25 -13
- package/dist/src/utils/response-headers.js.map +1 -1
- package/dist/src/verified-fetch.d.ts.map +1 -1
- package/dist/src/verified-fetch.js +6 -4
- package/dist/src/verified-fetch.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +12 -1
- package/src/plugins/plugin-handle-ipns-record.ts +8 -2
- package/src/plugins/plugin-handle-unixfs.ts +3 -1
- package/src/url-resolver.ts +10 -2
- package/src/utils/response-headers.ts +33 -15
- package/src/verified-fetch.ts +7 -4
|
@@ -4,42 +4,60 @@ interface CacheControlHeaderOptions {
|
|
|
4
4
|
/**
|
|
5
5
|
* This should be seconds as a number.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
7
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#response_directives
|
|
8
8
|
*/
|
|
9
9
|
ttl?: number
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* This can represent an IPNS record's EOL validity
|
|
13
|
+
*/
|
|
14
|
+
expires?: Date
|
|
10
15
|
protocol: string
|
|
11
16
|
response: Response
|
|
12
17
|
}
|
|
13
18
|
|
|
14
19
|
/**
|
|
15
|
-
* Implementations may place an upper bound on any TTL received, as noted in
|
|
20
|
+
* Implementations may place an upper bound on any TTL received, as noted in
|
|
21
|
+
* Section 8 of [rfc2181].
|
|
22
|
+
*
|
|
16
23
|
* If TTL value is unknown, implementations should not send a Cache-Control
|
|
17
|
-
*
|
|
24
|
+
*
|
|
25
|
+
* No matter if TTL value is known or not, implementations should always send a
|
|
26
|
+
* Last-Modified header with the timestamp of the record resolution.
|
|
18
27
|
*
|
|
19
28
|
* @see https://specs.ipfs.tech/http-gateways/path-gateway/#cache-control-response-header
|
|
20
29
|
*/
|
|
21
|
-
export function setCacheControlHeader ({ ttl, protocol, response }: CacheControlHeaderOptions): void {
|
|
22
|
-
|
|
23
|
-
// don't set the header if it's already set by a plugin
|
|
24
|
-
return
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
let headerValue: string
|
|
30
|
+
export function setCacheControlHeader ({ ttl, expires, protocol, response }: CacheControlHeaderOptions): void {
|
|
31
|
+
let cacheControl: string
|
|
28
32
|
|
|
29
33
|
if (protocol === 'ipfs:') {
|
|
30
|
-
|
|
34
|
+
cacheControl = 'public, max-age=29030400, immutable'
|
|
31
35
|
} else if (ttl == null) {
|
|
32
36
|
/**
|
|
33
|
-
* default limit for unknown TTL: "use 5 minute as default fallback when it
|
|
37
|
+
* default limit for unknown TTL: "use 5 minute as default fallback when it
|
|
38
|
+
* is not available."
|
|
34
39
|
*
|
|
35
40
|
* @see https://github.com/ipfs/boxo/issues/329#issuecomment-1995236409
|
|
36
41
|
*/
|
|
37
|
-
|
|
42
|
+
cacheControl = 'public, max-age=300'
|
|
38
43
|
} else {
|
|
39
|
-
|
|
44
|
+
cacheControl = `public, max-age=${ttl}`
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (expires != null) {
|
|
48
|
+
const lifetimeRemaining = Math.max(0, Math.round((expires.getTime() - Date.now()) / 1000))
|
|
49
|
+
cacheControl += `, stale-while-revalidate=${lifetimeRemaining}, stale-if-error=${lifetimeRemaining}`
|
|
50
|
+
|
|
51
|
+
// add the expires header if it's not been set by a plugin
|
|
52
|
+
if (!response.headers.has('expires')) {
|
|
53
|
+
response.headers.set('expires', expires.toUTCString())
|
|
54
|
+
}
|
|
40
55
|
}
|
|
41
56
|
|
|
42
|
-
response.headers.
|
|
57
|
+
if (!response.headers.has('cache-control')) {
|
|
58
|
+
// don't set the cache-control header if it's already set by a plugin
|
|
59
|
+
response.headers.set('cache-control', cacheControl)
|
|
60
|
+
}
|
|
43
61
|
}
|
|
44
62
|
|
|
45
63
|
/**
|
package/src/verified-fetch.ts
CHANGED
|
@@ -198,15 +198,17 @@ export class VerifiedFetch {
|
|
|
198
198
|
return notAcceptableResponse(resource, requestedMimeTypes, [])
|
|
199
199
|
}
|
|
200
200
|
|
|
201
|
-
|
|
201
|
+
const context = {
|
|
202
202
|
...options,
|
|
203
203
|
range,
|
|
204
204
|
url,
|
|
205
205
|
resource,
|
|
206
|
-
redirected: false
|
|
207
|
-
}), withServerTiming, {
|
|
206
|
+
redirected: false,
|
|
208
207
|
serverTiming
|
|
209
|
-
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// @ts-expect-error headers type is wrong
|
|
211
|
+
return this.handleFinalResponse(await ipnsRecordPlugin.handle(context), withServerTiming, context)
|
|
210
212
|
}
|
|
211
213
|
|
|
212
214
|
const resolveResult = await this.urlResolver.resolve(url, serverTiming, options)
|
|
@@ -421,6 +423,7 @@ export class VerifiedFetch {
|
|
|
421
423
|
setCacheControlHeader({
|
|
422
424
|
response,
|
|
423
425
|
ttl: context.ttl,
|
|
426
|
+
expires: context.expires,
|
|
424
427
|
protocol: context.url.protocol
|
|
425
428
|
})
|
|
426
429
|
}
|