Skip to content

Commit

Permalink
let's go
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeChampion committed Nov 21, 2024
1 parent b81bb11 commit 8f84114
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import init from "../pkg/html_rewriter.js";
import { HTMLRewriter } from "./html_rewriter_wrapper.ts";
import { Element } from "./types.d.ts";

export { default as init } from "../pkg/html_rewriter.js";

type Params = {
/**
* When true, uses the `Content-Security-Policy-Report-Only` header instead
Expand Down Expand Up @@ -59,7 +60,7 @@ function uInt8ArrayToBase64String(input: Uint8Array): string {
return btoa(res);
}

export async function csp(originalResponse: Response, params?: Params) {
export function csp(originalResponse: Response, params?: Params) {
const isHTMLResponse = originalResponse.headers.get("content-type")
?.startsWith(
"text/html",
Expand Down Expand Up @@ -146,7 +147,6 @@ export async function csp(originalResponse: Response, params?: Params) {
}

const querySelectors = ["script", 'link[rel="preload"][as="script"]'];
await init();
return new HTMLRewriter()
.on(querySelectors.join(","), {
element(element: Element) {
Expand Down
22 changes: 11 additions & 11 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
assertMatch,
assertStrictEquals,
} from "https://deno.land/std@0.224.0/assert/mod.ts";
import { csp } from "../src/index.ts";
import { csp, init } from "../src/index.ts";

import * as cheerio from "https://cdn.jsdelivr.net/npm/cheerio/+esm";

Expand All @@ -17,7 +17,7 @@ Deno.test({
fn: async () => {
let response = await fetch("https://html.spec.whatwg.org/");
response = new Response(response.body, response);

await init();
const result = await csp(response);
const policy: Map<string, string[]> = parseContentSecurityPolicy(
result.headers.get("content-security-policy")!,
Expand Down Expand Up @@ -46,7 +46,7 @@ Deno.test({
const response = new Response("meow", {
headers: { "content-type": "text/plain" },
});

await init();
const result = await csp(response);
assertStrictEquals(response, result);
assertEquals(result.headers.has("content-security-policy"), false);
Expand Down Expand Up @@ -133,7 +133,7 @@ Deno.test({
},
);
const originBody = await response.clone().arrayBuffer();

await init();
const result = await csp(response);
assertEquals(originBody, await result.arrayBuffer());
},
Expand All @@ -149,7 +149,7 @@ Deno.test({
},
},
);

await init();
const result = await csp(response);
const policy: Map<string, string[]> = parseContentSecurityPolicy(
result.headers.get("content-security-policy")!,
Expand Down Expand Up @@ -181,7 +181,7 @@ Deno.test({
"img-src 'self' blob: data:; script-src 'strict-dynamic' 'sha256-/Cb4VxgL2aVP0MVDvbP0DgEOUv+MeNQmZX4yXHkn/c0='",
},
});

await init();
const result = await csp(response);
const policy: Map<string, string[]> = parseContentSecurityPolicy(
result.headers.get("content-security-policy") || "",
Expand Down Expand Up @@ -209,7 +209,7 @@ Deno.test({
"img-src 'self' blob: data:; script-src 'sha256-/Cb4VxgL2aVP0MVDvbP0DgEOUv+MeNQmZX4yXHkn/c0='",
},
});

await init();
const result = await csp(response, { distribution: "0" });
assertStrictEquals(
result.headers.get("content-security-policy")!,
Expand All @@ -233,7 +233,7 @@ Deno.test({
"img-src 'self' blob: data:; script-src 'sha256-/Cb4VxgL2aVP0MVDvbP0DgEOUv+MeNQmZX4yXHkn/c0='",
},
});

await init();
const result = await csp(response, { distribution: "0%" });
assertStrictEquals(
result.headers.get("content-security-policy")!,
Expand All @@ -255,7 +255,7 @@ Deno.test({
"content-type": "text/html; charset=utf-8",
},
});

await init();
const result = await csp(response, { reportOnly: true });
assertMatch(
result.headers.get("content-security-policy-report-only")!,
Expand All @@ -273,7 +273,7 @@ Deno.test({
"content-type": "text/html; charset=utf-8",
},
});

await init();
const result = await csp(response, { unsafeEval: true });
assertMatch(
result.headers.get("content-security-policy")!,
Expand All @@ -291,7 +291,7 @@ Deno.test({
"content-type": "text/html; charset=utf-8",
},
});

await init();
const result = await csp(response, { reportUri: "https://example.com" });
assertMatch(
result.headers.get("content-security-policy")!,
Expand Down

0 comments on commit 8f84114

Please sign in to comment.