Skip to content

Commit

Permalink
Cache signed key
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Apr 11, 2024
1 parent 4d459c5 commit 6b29720
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 4 additions & 0 deletions federation/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ Deno.test("Federation.createContext()", async (t) => {
assertEquals(ctx.url, new URL("https://example.com/"));
assertEquals(ctx.data, 123);
assertEquals(await ctx.getSignedKey(), null);
// Multiple calls should return the same result:
assertEquals(await ctx.getSignedKey(), null);

const signedReq = await sign(
new Request("https://example.com/"),
Expand All @@ -190,6 +192,8 @@ Deno.test("Federation.createContext()", async (t) => {
assertEquals(signedCtx.url, new URL("https://example.com/"));
assertEquals(signedCtx.data, 456);
assertEquals(await signedCtx.getSignedKey(), publicKey2);
// Multiple calls should return the same result:
assertEquals(await signedCtx.getSignedKey(), publicKey2);
});

mf.uninstall();
Expand Down
6 changes: 4 additions & 2 deletions federation/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,14 @@ export class Federation<TContextData> {
},
};
if (request == null) return context;
let signedKey: CryptographicKey | null | undefined = undefined;
const reqCtx: RequestContext<TContextData> = {
...context,
request,
url,
getSignedKey() {
return verify(request, context.documentLoader);
async getSignedKey() {
if (signedKey !== undefined) return signedKey;
return signedKey = await verify(request, context.documentLoader);
},
};
return reqCtx;
Expand Down

0 comments on commit 6b29720

Please sign in to comment.