Skip to content

Commit

Permalink
fix: some ipns ttl precision cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Mar 15, 2024
1 parent dbd8863 commit a93b5e4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
13 changes: 9 additions & 4 deletions packages/verified-fetch/src/utils/parse-url-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ export interface ParsedUrlQuery extends Record<string, string | unknown> {
interface ParsedUrlStringResultsBase extends ResolveResult {
protocol: 'ipfs' | 'ipns'
query: ParsedUrlQuery

/**
* milliseconds as a number
* seconds as a number
*/
ttl?: number
}
Expand Down Expand Up @@ -59,9 +60,12 @@ function matchURLString (urlString: string): MatchUrlGroups {
}

/**
* determines the TTL for the resolved resource.
* determines the TTL for the resolved resource that will be used for the `Cache-Control` header's `max-age` directive.
* max-age is in seconds
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#response_directives
*
* If we have ipnsTtlNs, it will be a BigInt representing nanoseconds. We need to convert it back to milliseconds.
* If we have ipnsTtlNs, it will be a BigInt representing "nanoseconds". We need to convert it back to seconds.
*
* For more TTL nuances:
*
Expand All @@ -74,7 +78,8 @@ function calculateTtl (resolveResult?: IPNSResolveResult | DNSLinkResolveResult)
}
const dnsLinkTtl = (resolveResult as DNSLinkResolveResult).answer?.TTL
const ipnsTtlNs = (resolveResult as IPNSResolveResult).record?.ttl
const ipnsTtl = ipnsTtlNs != null ? Number(BigInt(ipnsTtlNs) / BigInt(1e5)) : undefined
// For some reason, ipns "nanoseconds" are 1e-8 of a second, instead of 1e-9.
const ipnsTtl = ipnsTtlNs != null ? Number(ipnsTtlNs / BigInt(1e8)) : undefined
return dnsLinkTtl ?? ipnsTtl
}

Expand Down
5 changes: 5 additions & 0 deletions packages/verified-fetch/src/utils/response-headers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
interface CacheControlHeaderOptions {
/**
* This should be seconds as a number.
*
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#response_directives
*/
ttl?: number
protocol: 'ipfs' | 'ipns'
response: Response
Expand Down
6 changes: 3 additions & 3 deletions packages/verified-fetch/test/cache-control-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('cache-control header', () => {
const c = dagCbor(helia)
const cid = await c.add(obj)

const oneHourInMs = 1000 * 60 * 60
const oneHourInSeconds = 60 * 60
const peerId = await createEd25519PeerId()

/**
Expand All @@ -99,13 +99,13 @@ describe('cache-control header', () => {
* @see https://github.com/ipfs/js-ipns/blob/16e0e10682fa9a663e0bb493a44d3e99a5200944/src/index.ts#L200
* @see https://github.com/ipfs/js-ipns/pull/308
*/
await name.publish(peerId, cid, { lifetime: oneHourInMs })
await name.publish(peerId, cid, { lifetime: oneHourInSeconds * 1000 }) // pass to ipns as milliseconds

const resp = await verifiedFetch.fetch(`ipns://${peerId}`)
expect(resp).to.be.ok()
expect(resp.status).to.equal(200)

expect(resp.headers.get('Cache-Control')).to.equal(`public, max-age=${oneHourInMs}`)
expect(resp.headers.get('Cache-Control')).to.equal(`public, max-age=${oneHourInSeconds}`)
})

it('should not contain immutable in the cache-control header for a DNSLink name', async () => {
Expand Down

0 comments on commit a93b5e4

Please sign in to comment.