Skip to content

Commit

Permalink
Rename salt to syntax_version
Browse files Browse the repository at this point in the history
  • Loading branch information
zephraph committed Oct 7, 2024
1 parent b16b44c commit b45cff8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
15 changes: 7 additions & 8 deletions packages/astro-md/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class SyntaxHighlightRewriter implements HTMLRewriterElementContentHandlers {
private lang: string = "";
private code: string = "";
cache: KVNamespace;
salt: string;
cacheVersion: string;
skip: boolean = false;

constructor(private runtime: Runtime["runtime"]) {
this.cache = runtime.env.KV_HIGHLIGHT;
this.salt = runtime.env.SYNTAX_SALT;
this.cacheVersion = runtime.env.SYNTAX_VERSION;
}

element(element: Element) {
Expand All @@ -44,16 +44,15 @@ class SyntaxHighlightRewriter implements HTMLRewriterElementContentHandlers {
if (text.lastInTextNode) {
const hashBuffer = await crypto.subtle.digest(
"SHA-1",
new TextEncoder().encode(
`${this.salt}-${theme}-${this.lang}-${this.code}`
)
new TextEncoder().encode(`${theme}-${this.lang}-${this.code}`)
);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hash = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
const key = `${this.cacheVersion}:${hash}`;

const cachedResult = await this.cache.get(hash, {
const cachedResult = await this.cache.get(key, {
cacheTtl: syntaxHighlightCacheTtl,
});
if (cachedResult) {
Expand All @@ -63,7 +62,7 @@ class SyntaxHighlightRewriter implements HTMLRewriterElementContentHandlers {
// When setting cacheTtl, even misses are cached. If we've gotten
// to this point the cache is empty, so let's clear the miss so that
// the next request has the actual content (after we write it)
this.runtime.ctx.waitUntil(this.cache.get(hash));
this.runtime.ctx.waitUntil(this.cache.get(key));

const res = await fetch(
`https://just_be-highlight.web.val.run?lang=${this.lang}&theme=${theme}`,
Expand All @@ -76,7 +75,7 @@ class SyntaxHighlightRewriter implements HTMLRewriterElementContentHandlers {
}
);
const data = await res.text();
this.runtime.ctx.waitUntil(this.cache.put(hash, data));
this.runtime.ctx.waitUntil(this.cache.put(key, data));
text.replace(data, { html: true });
} else {
text.remove();
Expand Down
22 changes: 11 additions & 11 deletions worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Generated by Wrangler by running `wrangler types`

interface Env {
KV_MAPPINGS: KVNamespace;
KV_HIGHLIGHT: KVNamespace;
NODE_VERSION: "v20.16.0";
PNPM_VERSION: "v9.7.0";
SITE: string;
PUBLISH_KEY: string;
SENTRY_DSN: string;
SENTRY_AUTH_TOKEN: string;
SYNTAX_SALT: string;
R2_BUCKET: R2Bucket;
R2_ASSETS: R2Bucket;
KV_MAPPINGS: KVNamespace;
KV_HIGHLIGHT: KVNamespace;
NODE_VERSION: "v20.16.0";
PNPM_VERSION: "v9.7.0";
SITE: string;
PUBLISH_KEY: string;
SENTRY_DSN: string;
SENTRY_AUTH_TOKEN: string;
SYNTAX_VERSION: string;
R2_BUCKET: R2Bucket;
R2_ASSETS: R2Bucket;
}

0 comments on commit b45cff8

Please sign in to comment.