Skip to content

Commit

Permalink
Restore URL, salt hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
zephraph committed Oct 7, 2024
1 parent b287ed1 commit eb0b630
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/astro-md/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class SyntaxHighlightRewriter implements HTMLRewriterElementContentHandlers {
private lang: string = "";
private code: string = "";
cache: KVNamespace;
salt: string;

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

element(element: Element) {
Expand All @@ -33,7 +35,9 @@ class SyntaxHighlightRewriter implements HTMLRewriterElementContentHandlers {
if (text.lastInTextNode) {
const hashBuffer = await crypto.subtle.digest(
"SHA-1",
new TextEncoder().encode(`${theme}-${this.lang}-${this.code}`)
new TextEncoder().encode(
`${this.salt}-${theme}-${this.lang}-${this.code}`
)
);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hash = hashArray
Expand All @@ -53,7 +57,7 @@ class SyntaxHighlightRewriter implements HTMLRewriterElementContentHandlers {
this.runtime.ctx.waitUntil(this.cache.get(hash));

const res = await fetch(
`https://just_be-highlight.web.val.run?lang=${this.lang}&theme=${theme}`,
`https://highlight.val.just-be.dev?lang=${this.lang}&theme=${theme}`,
{
method: "POST",
body: this.code,
Expand Down
1 change: 1 addition & 0 deletions worker-configuration.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface Env {
PUBLISH_KEY: string;
SENTRY_DSN: string;
SENTRY_AUTH_TOKEN: string;
SYNTAX_SALT: string;
R2_BUCKET: R2Bucket;
R2_ASSETS: R2Bucket;
}

0 comments on commit eb0b630

Please sign in to comment.