-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Deno platform cache to codegen service
- Loading branch information
Showing
2 changed files
with
57 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Cache } from "https://deno.land/x/httpcache@0.1.2/mod.ts"; | ||
|
||
// https://docs.deno.com/deploy/manual/edge-cache | ||
|
||
export async function platformCache( | ||
name = 'http-policy-cache', | ||
): Promise<Cache> { | ||
|
||
const nativeCache = await globalThis.caches.open(name); | ||
|
||
return new Cache({ | ||
|
||
async get(url) { | ||
const cached = await nativeCache.match(url); | ||
if (!cached) return undefined; | ||
|
||
const policyHeader = cached.headers.get('cache-policy'); | ||
if (!policyHeader) { | ||
console.warn('WARN: platform cache lacked policy', cached); | ||
return undefined; | ||
} | ||
|
||
const BodyBuffer = new Uint8Array(await cached.arrayBuffer()); | ||
|
||
// const idx = BodyBuffer.indexOf(10); | ||
return { | ||
// policy: JSON.parse(new TextDecoder().decode(BodyBuffer.slice(0, idx))), | ||
policy: JSON.parse(policyHeader), | ||
// body: BodyBuffer.slice(idx+1), | ||
body: BodyBuffer, | ||
}; | ||
}, | ||
|
||
async set(url, resp) { | ||
await nativeCache.put(url, new Response(resp.body, { | ||
headers: { | ||
'cache-policy': JSON.stringify(resp.policy), | ||
}, | ||
})); | ||
}, | ||
|
||
async delete(url) { | ||
await nativeCache.delete(url); | ||
}, | ||
|
||
close() {}, | ||
}); | ||
} |
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
b1915c3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed to deploy: