-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a configurable session cache that creates sessions based on the base URL of the requested resource. E.g. `https://Qmfoo.ipfs.gateway.com/foo.txt` and`https://Qmfoo.ipfs.gateway.com/bar.txt` will be loaded from the same session. Defaults to 100 sessions maximum with a TTL of one minute. These are arbitrary numbers that will require some tweaking.
- Loading branch information
1 parent
abaaeab
commit 4b05147
Showing
8 changed files
with
255 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/verified-fetch/src/utils/resource-to-cache-key.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { CID } from 'multiformats/cid' | ||
import { matchURLString } from './parse-url-string.js' | ||
|
||
/** | ||
* Takes a resource and returns a session cache key as an IPFS or IPNS path with | ||
* any trailing segments removed. | ||
* | ||
* E.g. | ||
* | ||
* - Qmfoo -> /ipfs/Qmfoo | ||
* - https://Qmfoo.ipfs.gateway.org -> /ipfs/Qmfoo | ||
* - https://gateway.org/ipfs/Qmfoo -> /ipfs/Qmfoo | ||
* - https://gateway.org/ipfs/Qmfoo/bar.txt -> /ipfs/Qmfoo | ||
* - etc | ||
*/ | ||
export function resourceToSessionCacheKey (url: string | CID): string { | ||
const cid = CID.asCID(url) | ||
|
||
if (cid != null) { | ||
return `/ipfs/${cid}` | ||
} | ||
|
||
try { | ||
return `/ipfs/${CID.parse(url.toString())}` | ||
} catch {} | ||
|
||
const { protocol, cidOrPeerIdOrDnsLink } = matchURLString(url.toString()) | ||
|
||
return `/${protocol}/${cidOrPeerIdOrDnsLink}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/verified-fetch/test/utils/resource-to-cache-key.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { expect } from 'aegir/chai' | ||
import { CID } from 'multiformats/cid' | ||
import { resourceToSessionCacheKey } from '../../src/utils/resource-to-cache-key.js' | ||
|
||
describe('resource-to-cache-key', () => { | ||
it('converts url with IPFS path', () => { | ||
expect(resourceToSessionCacheKey('https://localhost:8080/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA')) | ||
.to.equal('/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA') | ||
}) | ||
|
||
it('converts url with IPFS path and resource path', () => { | ||
expect(resourceToSessionCacheKey('https://localhost:8080/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA/foo/bar/baz.txt')) | ||
.to.equal('/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA') | ||
}) | ||
|
||
it('converts url with IPNS path', () => { | ||
expect(resourceToSessionCacheKey('https://localhost:8080/ipns/ipfs.io')) | ||
.to.equal('/ipns/ipfs.io') | ||
}) | ||
|
||
it('converts url with IPNS path and resource path', () => { | ||
expect(resourceToSessionCacheKey('https://localhost:8080/ipns/ipfs.io/foo/bar/baz.txt')) | ||
.to.equal('/ipns/ipfs.io') | ||
}) | ||
|
||
it('converts IPFS subdomain', () => { | ||
expect(resourceToSessionCacheKey('https://QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA.ipfs.localhost:8080')) | ||
.to.equal('/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA') | ||
}) | ||
|
||
it('converts IPFS subdomain with path', () => { | ||
expect(resourceToSessionCacheKey('https://QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA.ipfs.localhost:8080/foo/bar/baz.txt')) | ||
.to.equal('/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA') | ||
}) | ||
|
||
it('converts IPNS subdomain', () => { | ||
expect(resourceToSessionCacheKey('https://ipfs.io.ipns.localhost:8080')) | ||
.to.equal('/ipns/ipfs.io') | ||
}) | ||
|
||
it('converts IPNS subdomain with resource path', () => { | ||
expect(resourceToSessionCacheKey('https://ipfs.io.ipns.localhost:8080/foo/bar/baz.txt')) | ||
.to.equal('/ipns/ipfs.io') | ||
}) | ||
|
||
it('converts CID', () => { | ||
expect(resourceToSessionCacheKey(CID.parse('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA'))) | ||
.to.equal('/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA') | ||
}) | ||
|
||
it('converts CID string', () => { | ||
expect(resourceToSessionCacheKey('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA')) | ||
.to.equal('/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJA') | ||
}) | ||
}) |
Oops, something went wrong.