@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.
@@ -4,42 +4,60 @@ interface CacheControlHeaderOptions {
4
4
  /**
5
5
  * This should be seconds as a number.
6
6
  *
7
- * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#response_directives
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 Section 8 of [rfc2181].
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
- * No matter if TTL value is known or not, implementations should always send a Last-Modified header with the timestamp of the record resolution.
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
- if (response.headers.has('cache-control')) {
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
- headerValue = 'public, max-age=29030400, immutable'
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 is not available."
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
- headerValue = 'public, max-age=300'
42
+ cacheControl = 'public, max-age=300'
38
43
  } else {
39
- headerValue = `public, max-age=${ttl}`
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.set('cache-control', headerValue)
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
  /**
@@ -198,15 +198,17 @@ export class VerifiedFetch {
198
198
  return notAcceptableResponse(resource, requestedMimeTypes, [])
199
199
  }
200
200
 
201
- return this.handleFinalResponse(await ipnsRecordPlugin.handle({
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
  }