Skip to content

Commit

Permalink
fix: better typescript types (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Barry authored Oct 23, 2023
1 parent 607dd42 commit f01059b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
"src/*"
],
"devDependencies": {
"prettier": "^2.8.8"
"prettier": "^2.8.8",
"typescript": "^5.2.2"
},
"bugs": {
"url": "https://github.com/netlify/plugin-csp-nonce/issues"
},
"homepage": "https://github.com/netlify/plugin-csp-nonce#readme",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"build": "tsc src/*.ts --noEmit --strict --lib es2018,dom"
}
}
13 changes: 7 additions & 6 deletions src/__csp-nonce.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable */
// @ts-expect-error
// @ts-ignore
import type { Config, Context } from "netlify:edge";
// @ts-expect-error
// @ts-ignore
import { randomBytes } from "node:crypto";
// @ts-expect-error
// @ts-ignore
import { HTMLRewriter } from "https://ghuc.cc/worker-tools/html-rewriter@0.1.0-pre.17/index.ts";

// @ts-ignore
import inputs from "./__csp-nonce-inputs.json" assert { type: "json" };

type Params = {
Expand Down Expand Up @@ -43,7 +44,7 @@ const handler = async (request: Request, context: Context) => {

// CSP_NONCE_DISTRIBUTION is a number from 0 to 1,
// but 0 to 100 is also supported, along with a trailing %
// @ts-expect-error
// @ts-ignore
const distribution = Netlify.env.get("CSP_NONCE_DISTRIBUTION");
if (!!distribution) {
const threshold =
Expand Down Expand Up @@ -82,7 +83,7 @@ const handler = async (request: Request, context: Context) => {
params.reportUri || "/.netlify/functions/__csp-violations"
}`;

const csp = response.headers.get(header);
const csp = response.headers.get(header) as string;
if (csp) {
const directives = csp
.split(";")
Expand Down Expand Up @@ -113,7 +114,7 @@ const handler = async (request: Request, context: Context) => {

return new HTMLRewriter()
.on("script", {
element(element) {
element(element: HTMLElement) {
element.setAttribute("nonce", nonce);
},
})
Expand Down
1 change: 1 addition & 0 deletions src/__csp-violations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable */
// @ts-ignore
const handler = async (event) => {
try {
const { "csp-report": cspReport } = JSON.parse(event.body);
Expand Down

0 comments on commit f01059b

Please sign in to comment.