diff --git a/.github/workflows/pm-console-edge-deploy.yml b/.github/workflows/pm-console-edge-deploy.yml index 39d64f606..16fb2a4a5 100644 --- a/.github/workflows/pm-console-edge-deploy.yml +++ b/.github/workflows/pm-console-edge-deploy.yml @@ -89,6 +89,12 @@ jobs: - uses: actions/setup-node@v7 with: node-version: 24 + - name: Install the console's type-check tools + working-directory: pm-stack/console/edge + run: npm ci --no-audit --no-fund + - name: Type-check the console + working-directory: pm-stack/console/edge + run: npx tsc --noEmit -p ../tsconfig.json - name: Test the console and its edge working-directory: pm-stack/console run: bun test diff --git a/.github/workflows/pm-stack-tests.yml b/.github/workflows/pm-stack-tests.yml index 0c352006c..31bfb4c6c 100644 --- a/.github/workflows/pm-stack-tests.yml +++ b/.github/workflows/pm-stack-tests.yml @@ -71,6 +71,18 @@ jobs: fi bun test + - uses: actions/setup-node@v7 + with: + node-version: 24 + + - name: Install the console's type-check tools + working-directory: pm-stack/console/edge + run: npm ci --no-audit --no-fund + + - name: Type-check the console + working-directory: pm-stack/console/edge + run: npx tsc --noEmit -p ../tsconfig.json + - name: Test console working-directory: pm-stack/console run: | diff --git a/pm-stack/console/edge/package-lock.json b/pm-stack/console/edge/package-lock.json index ebb601742..f9137398b 100644 --- a/pm-stack/console/edge/package-lock.json +++ b/pm-stack/console/edge/package-lock.json @@ -8,6 +8,9 @@ "name": "pm-console-edge", "version": "0.1.0", "devDependencies": { + "@cloudflare/workers-types": "^5.20261123.1", + "@types/bun": "^1.4.5", + "typescript": "^7.0.2", "wrangler": "4.166.0" } }, @@ -122,6 +125,13 @@ "node": ">=16" } }, + "node_modules/@cloudflare/workers-types": { + "version": "5.20261123.1", + "resolved": "https://registry.npmmirror.com/@cloudflare/workers-types/-/workers-types-5.20261123.1.tgz", + "integrity": "sha512-T5ZYVByysoCFgJytSwsGBLS0ebNhxFYLSbsERdVZ04YErfmpBGlvYErvUmKT+mjNR/xAl4fxCedpjqLgYIyWSg==", + "dev": true, + "license": "MIT OR Apache-2.0" + }, "node_modules/@cloudflare/workers-utils": { "version": "0.56.0", "resolved": "https://registry.npmjs.org/@cloudflare/workers-utils/-/workers-utils-0.56.0.tgz", @@ -1261,6 +1271,16 @@ "dev": true, "license": "CC0-1.0" }, + "node_modules/@types/bun": { + "version": "1.4.5", + "resolved": "https://registry.npmmirror.com/@types/bun/-/bun-1.4.5.tgz", + "integrity": "sha512-DpgwNI9z0L1S0gtb45QLCWgwCUcMLnsHYMJHQvpNj7erGiCGBvpggltAIeARrtobNMKMuy0UP7i56tKKgwFs/Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "bun-types": "1.4.5" + } + }, "node_modules/@types/node": { "version": "18.19.130", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.130.tgz", @@ -1299,6 +1319,346 @@ "dev": true, "license": "MIT" }, + "node_modules/@typescript/typescript-aix-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-aix-ppc64/-/typescript-aix-ppc64-7.0.2.tgz", + "integrity": "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-darwin-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-darwin-arm64/-/typescript-darwin-arm64-7.0.2.tgz", + "integrity": "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-darwin-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-darwin-x64/-/typescript-darwin-x64-7.0.2.tgz", + "integrity": "sha512-SZ9xZInqApNlNGc9s0W1VSsktYSOe9cFqNOIqmN1Gs8SmkjKZYFt017G4VwPxASInODuAdbTW7sXiFUf893RgA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-freebsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-freebsd-arm64/-/typescript-freebsd-arm64-7.0.2.tgz", + "integrity": "sha512-W5NH4y/J0plIIS5b2xvTEkU7JFxyqdMAOgf+Ilhl0vHQXKO5dZoxd+C/jEtq56c4F3wk71RB4BMRQ2XdI+bwYQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-freebsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-freebsd-x64/-/typescript-freebsd-x64-7.0.2.tgz", + "integrity": "sha512-UMGDx5sTpzNw3WiPebH7l90IWfJggEd+egHt/q6p7/Cm3zqoV7VxkGXt+3DxPIw8CcmvAB0j3sVVfbhX+M4Tpw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-arm": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-linux-arm/-/typescript-linux-arm-7.0.2.tgz", + "integrity": "sha512-gffT3xPz9sR7j/YJExkyPntrI0P2EP9XbOyWzth2/Gs0RstK+90RBcO0ncXoXy/beYll1SXw846Nf2zdnEz0QQ==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-linux-arm64/-/typescript-linux-arm64-7.0.2.tgz", + "integrity": "sha512-Qh4eU4/y3yDjnfjjyPYihMj5/ODIlmt+Bzu17OI+fiSRDW57QmU5SiN63exPRNJPKUzcc1INa1NXdrJ+MqHjUQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-loong64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-linux-loong64/-/typescript-linux-loong64-7.0.2.tgz", + "integrity": "sha512-uEHck9i8hoAzXPiYRib1O7miOnz23SxIeVl6F4LXox+qov1K35jHcEW6VHKvZI+pyvl7fZEP4MCU5LYvIq1GuQ==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-mips64el": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-linux-mips64el/-/typescript-linux-mips64el-7.0.2.tgz", + "integrity": "sha512-R4KvAMnE43W5Qeqb0Ly56O3mWMWIAgsMyz36DCaycd5nbg/9kzm0liw3JocfRqyJY0KPmzFjbswozXyW0DnIYA==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-ppc64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-linux-ppc64/-/typescript-linux-ppc64-7.0.2.tgz", + "integrity": "sha512-DORx5b3sd/4S7eayxm4FQv+A7CrkUIGRaHiwI8oiHTAI1fAPWhF4J0vAlkC8biAlHSVVwxMQ3tjZ2/DVbnQiiA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-riscv64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-linux-riscv64/-/typescript-linux-riscv64-7.0.2.tgz", + "integrity": "sha512-wf0jqEDOjrPRnKwYRyyJDRo11KMbvMFrU+q4zqKyChODBzvlkbhNQfKvLxQCcwTpdDaXSHZTVuh0JoCrKCUMHQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-s390x": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-linux-s390x/-/typescript-linux-s390x-7.0.2.tgz", + "integrity": "sha512-IkwJc3L7yhytWd/ewjyxNDfOmswCm9GWMJT/ue/dU4aZNbwZeYAetq42VyLmsmSjvoX7z74X6ZaYCtzAr0EuGw==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-linux-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-linux-x64/-/typescript-linux-x64-7.0.2.tgz", + "integrity": "sha512-EYdf2cNg7rgCWJnxCdJ+F3V39O8ihb37eHAu1LK8oAFizgTQbPOK7zHHXbPt8rX24COqODXeI3sIf0fCXG7H/A==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-netbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-netbsd-arm64/-/typescript-netbsd-arm64-7.0.2.tgz", + "integrity": "sha512-+polYF4MF04aPpO5FTkHran9yUQDSXqy5GiSDKpsll5jy3l3+g9QLhpf39T+ePtefhXLOGrLl0QIjkQP6VnelA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-netbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-netbsd-x64/-/typescript-netbsd-x64-7.0.2.tgz", + "integrity": "sha512-8YIT0EHM/3dq10ZOVF/A7pc/YSMtbcecct4rWtexrnSCHOPcpC2KTLXfTCR6vDpnSiY12heNb1GiN/wu+T/FyA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-openbsd-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-openbsd-arm64/-/typescript-openbsd-arm64-7.0.2.tgz", + "integrity": "sha512-APT8+ClYnuYm1u9+kgGXoMj2VzWzcymwh2gNSQVySHfkRDGOTVkoWLjCmOQSaO+PoqQ57B0flRp9SA+7GnnkzQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-openbsd-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-openbsd-x64/-/typescript-openbsd-x64-7.0.2.tgz", + "integrity": "sha512-yX7s+Q0Dln0Dt9tEzZsAjXXR/+ytBM7AlglaqyeMPxQszJ1JhlJdZ6jLA+IzldHtflX81em7lDao1xXu+aRRkg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-sunos-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-sunos-x64/-/typescript-sunos-x64-7.0.2.tgz", + "integrity": "sha512-dLJDGaLZ1D4HPQn62u1n8mBDkJREwMsAkCdkwd4Ieqw+x3TUyTsqY0YiBCtE6H6OzzgGk3iuZ3vFWRS+E8/d1g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-win32-arm64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-win32-arm64/-/typescript-win32-arm64-7.0.2.tgz", + "integrity": "sha512-Gyl1Vy6OsWesLzmq+EP0Fb7b4Nid5232AvcA2SFcdYreldpNtYFFofPjnt62y9hQy7VTaZp65ICJjuAQRaVcIQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16.20.0" + } + }, + "node_modules/@typescript/typescript-win32-x64": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/@typescript/typescript-win32-x64/-/typescript-win32-x64-7.0.2.tgz", + "integrity": "sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16.20.0" + } + }, "node_modules/abort-controller": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", @@ -1332,6 +1692,17 @@ "dev": true, "license": "MIT" }, + "node_modules/bun-types": { + "version": "1.4.5", + "resolved": "https://registry.npmmirror.com/bun-types/-/bun-types-1.4.5.tgz", + "integrity": "sha512-ECHn+9dhL3wv0G6u0+7dQvrnaimXiRDxxerYBmhTaLyJEEuvTPWk2Fye3XIf5Y++JIIiHKfQw4Z7V5D8KuiXbg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*", + "undici-types": "*" + } + }, "node_modules/call-bind-apply-helpers": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", @@ -1958,6 +2329,41 @@ "license": "0BSD", "optional": true }, + "node_modules/typescript": { + "version": "7.0.2", + "resolved": "https://registry.npmmirror.com/typescript/-/typescript-7.0.2.tgz", + "integrity": "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc" + }, + "engines": { + "node": ">=16.20.0" + }, + "optionalDependencies": { + "@typescript/typescript-aix-ppc64": "7.0.2", + "@typescript/typescript-darwin-arm64": "7.0.2", + "@typescript/typescript-darwin-x64": "7.0.2", + "@typescript/typescript-freebsd-arm64": "7.0.2", + "@typescript/typescript-freebsd-x64": "7.0.2", + "@typescript/typescript-linux-arm": "7.0.2", + "@typescript/typescript-linux-arm64": "7.0.2", + "@typescript/typescript-linux-loong64": "7.0.2", + "@typescript/typescript-linux-mips64el": "7.0.2", + "@typescript/typescript-linux-ppc64": "7.0.2", + "@typescript/typescript-linux-riscv64": "7.0.2", + "@typescript/typescript-linux-s390x": "7.0.2", + "@typescript/typescript-linux-x64": "7.0.2", + "@typescript/typescript-netbsd-arm64": "7.0.2", + "@typescript/typescript-netbsd-x64": "7.0.2", + "@typescript/typescript-openbsd-arm64": "7.0.2", + "@typescript/typescript-openbsd-x64": "7.0.2", + "@typescript/typescript-sunos-x64": "7.0.2", + "@typescript/typescript-win32-arm64": "7.0.2", + "@typescript/typescript-win32-x64": "7.0.2" + } + }, "node_modules/undici": { "version": "7.30.2", "resolved": "https://registry.npmjs.org/undici/-/undici-7.30.2.tgz", diff --git a/pm-stack/console/edge/package.json b/pm-stack/console/edge/package.json index ab0dd1815..fd9c14794 100644 --- a/pm-stack/console/edge/package.json +++ b/pm-stack/console/edge/package.json @@ -10,6 +10,9 @@ "dev": "node build.mjs && wrangler dev --port 8799" }, "devDependencies": { + "@cloudflare/workers-types": "^5.20261123.1", + "@types/bun": "^1.4.5", + "typescript": "^7.0.2", "wrangler": "4.166.0" } } diff --git a/pm-stack/console/edge/src/edge.ts b/pm-stack/console/edge/src/edge.ts index 76ff657be..e31493164 100644 --- a/pm-stack/console/edge/src/edge.ts +++ b/pm-stack/console/edge/src/edge.ts @@ -332,7 +332,7 @@ export class Edge { constructor(options: EdgeOptions) { this.kv = options.kv; this.now = options.now ?? Date.now; - this.fetchImpl = plain(options.fetchImpl ?? fetch); + this.fetchImpl = plain(options.fetchImpl ?? fetch) as typeof fetch; this.direct = options.direct; const setAlarm = options.setAlarm ?? (async () => {}); this.setAlarm = async (at) => { diff --git a/pm-stack/console/edge/src/entry.ts b/pm-stack/console/edge/src/entry.ts index ae5252a29..d79e526c6 100644 --- a/pm-stack/console/edge/src/entry.ts +++ b/pm-stack/console/edge/src/entry.ts @@ -100,7 +100,7 @@ export async function route(request: Request, io: Io): Promise { }); } - const forHost = request.clone(); + const forHost = request.clone() as Request; let res = await io.state(request); let what = res.headers.get("x-pm-next"); diff --git a/pm-stack/console/src/auth.ts b/pm-stack/console/src/auth.ts index cd1be519b..d8d0af6eb 100644 --- a/pm-stack/console/src/auth.ts +++ b/pm-stack/console/src/auth.ts @@ -79,7 +79,7 @@ async function jwks(uri: string, fetchImpl: FetchLike, now: number, force = fals } } -function b64url(text: string): Uint8Array { +function b64url(text: string): Uint8Array { if (!/^[A-Za-z0-9_-]*$/.test(text)) throw new Error("not base64url"); const pad = text.length % 4 === 0 ? "" : "=".repeat(4 - (text.length % 4)); const bin = atob(text.replace(/-/g, "+").replace(/_/g, "/") + pad); diff --git a/pm-stack/console/src/edge-proto.ts b/pm-stack/console/src/edge-proto.ts index b493b8cd3..497e17c28 100644 --- a/pm-stack/console/src/edge-proto.ts +++ b/pm-stack/console/src/edge-proto.ts @@ -322,7 +322,7 @@ const b64u = (bytes: ArrayBuffer | Uint8Array): string => { for (const b of new Uint8Array(bytes as ArrayBuffer)) bin += String.fromCharCode(b); return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); }; -const unb64u = (text: string): Uint8Array => { +const unb64u = (text: string): Uint8Array => { if (!/^[A-Za-z0-9_-]*$/.test(text)) throw new Error("not base64url"); const bin = atob(text.replace(/-/g, "+").replace(/_/g, "/")); return Uint8Array.from(bin, (c) => c.charCodeAt(0)); @@ -359,11 +359,11 @@ export async function newKeyPair(): Promise<{ publicJwk: PublicKey; privateJwk: export async function sign(privateJwk: JsonWebKey, body: Uint8Array): Promise { const key = await crypto.subtle.importKey("jwk", privateJwk, CURVE, false, ["sign"]); - return b64u(await crypto.subtle.sign(SIGN, key, body)); + return b64u(await crypto.subtle.sign(SIGN, key, body as BufferSource)); } export async function verify(key: PublicKey, body: Uint8Array, signature: string): Promise { - let sig: Uint8Array; + let sig: Uint8Array; try { sig = unb64u(signature); } catch { @@ -371,7 +371,7 @@ export async function verify(key: PublicKey, body: Uint8Array, signature: string } if (sig.length !== 64) return false; const imported = await crypto.subtle.importKey("jwk", key, CURVE, false, ["verify"]); - return crypto.subtle.verify(SIGN, imported, sig, body); + return crypto.subtle.verify(SIGN, imported, sig, body as BufferSource); } const strings = (value: unknown): value is string[] => diff --git a/pm-stack/console/src/push.ts b/pm-stack/console/src/push.ts index b0a176920..605e16bb4 100644 --- a/pm-stack/console/src/push.ts +++ b/pm-stack/console/src/push.ts @@ -104,7 +104,12 @@ export function validSubscription(value: unknown): Subscription | null { return { endpoint: url.href, keys: { p256dh: v.keys.p256dh, auth: v.keys.auth } }; } -async function hkdf(salt: Uint8Array, ikm: Uint8Array, info: Uint8Array, length: number): Promise { +async function hkdf( + salt: Uint8Array, + ikm: Uint8Array, + info: Uint8Array, + length: number, +): Promise> { const key = await crypto.subtle.importKey("raw", ikm, "HKDF", false, ["deriveBits"]); return new Uint8Array(await crypto.subtle.deriveBits({ name: "HKDF", hash: "SHA-256", salt, info }, key, length * 8)); } diff --git a/pm-stack/console/src/server.ts b/pm-stack/console/src/server.ts index 913182dfa..d1a575fc2 100644 --- a/pm-stack/console/src/server.ts +++ b/pm-stack/console/src/server.ts @@ -91,11 +91,11 @@ const IDLE_TIMEOUT_SECONDS = 120; // The page's four files, read once: nothing on the request path touches // the disk synchronously after the first hit. -const fileCache = new Map(); +const fileCache = new Map; etag: string }>(); function file(req: Request, name: string, type: string): Response { let kept = fileCache.get(name); if (!kept) { - const bytes = readFileSync(join(PUBLIC_DIR, name)); + const bytes = new Uint8Array(readFileSync(join(PUBLIC_DIR, name))); kept = { bytes, etag: `"${createHash("sha256").update(bytes).digest("base64url").slice(0, 22)}"` }; fileCache.set(name, kept); } @@ -164,7 +164,7 @@ function gzipped(req: Request, bytes: Uint8Array, headers: Headers): Response | if (!wantsGzip(req) || bytes.byteLength < COMPRESS_FROM_BYTES) return null; headers.set("content-encoding", "gzip"); headers.append("vary", "accept-encoding"); - return new Response(Bun.gzipSync(bytes), { headers }); + return new Response(Bun.gzipSync(bytes as Uint8Array), { headers }); } /** Pass one request to the dashboard and its answer back. `open` is for diff --git a/pm-stack/console/src/sync.ts b/pm-stack/console/src/sync.ts index f54196d83..2af4f8b5e 100644 --- a/pm-stack/console/src/sync.ts +++ b/pm-stack/console/src/sync.ts @@ -97,7 +97,7 @@ export class EdgeLink { constructor(private o: LinkOptions) { this.now = o.now ?? Date.now; - this.fetchImpl = plain(o.fetchImpl ?? fetch); + this.fetchImpl = plain(o.fetchImpl ?? fetch) as typeof fetch; this.log = o.log ?? ((line) => console.error(`pm-console: edge: ${line}`)); this.named = o.named ?? new Named(o.done); } diff --git a/pm-stack/console/src/work.ts b/pm-stack/console/src/work.ts index 5dbe42271..1e0f14f36 100644 --- a/pm-stack/console/src/work.ts +++ b/pm-stack/console/src/work.ts @@ -141,7 +141,7 @@ export class Work { return this.ops.cfg.repo; } - private task = (r: Row) => ({ + private task = (r: Row): Row & { url: string | null } => ({ ...r, url: r.issue ? `https://github.com/${this.repo}/issues/${r.issue}` : null, }); diff --git a/pm-stack/console/test/app.test.ts b/pm-stack/console/test/app.test.ts index 116bbb3a4..3a48feba9 100644 --- a/pm-stack/console/test/app.test.ts +++ b/pm-stack/console/test/app.test.ts @@ -318,7 +318,7 @@ test("a card's title is not announced as a skipped heading level", () => { const attrs: Record = {}; return { tagName, attrs, setAttribute: (k: string, v: string) => void (attrs[k] = v) }; }; - const levels = (tags: string[]) => { + const levels = (tags: string[]): (string | null)[] => { const els = tags.map(heading); levelHeadings(els); return els.map((e) => e.attrs["aria-level"] ?? null); @@ -395,11 +395,11 @@ test("what a form on a plan screen sends is the owner's words and only the choic }); test("a change form names a phase only when moving, and a name only when adding under", () => { - expect(changeAsked({ action: "move", to: "now", itemTitle: "left over from before" })).toEqual({ + expect(changeAsked({ action: "move", to: "now", itemTitle: "left over from before" }) as { action: string; to?: string; title?: string }).toEqual({ action: "move", to: "now", }); - expect(changeAsked({ action: "add-under", to: "now", itemTitle: "Tiles" })).toEqual({ + expect(changeAsked({ action: "add-under", to: "now", itemTitle: "Tiles" }) as { action: string; to?: string; title?: string }).toEqual({ action: "add-under", title: "Tiles", }); diff --git a/pm-stack/console/test/console.test.ts b/pm-stack/console/test/console.test.ts index f3034156f..1be6fa028 100644 --- a/pm-stack/console/test/console.test.ts +++ b/pm-stack/console/test/console.test.ts @@ -314,7 +314,7 @@ describe("who is the owner", () => { { host: "127.0.0.1:4872", "cf-connecting-ip": "203.0.113.9" }, { host: "127.0.0.1:4872", "x-forwarded-for": "203.0.113.9" }, { host: "127.0.0.1:4872", "cdn-loop": "cloudflare" }, - ]) { + ] as Record[]) { await expect(identify(local(headers), cfg, here)).rejects.toThrow(/sign-in required/); } }); @@ -564,7 +564,7 @@ describe("operations", () => { const r = recorder(() => "secret-token\n"); const sent: { url: string; body: { requests: { stmt?: { sql: string; args: unknown[] } }[] }; auth: string }[] = []; const ops = new Ops(CFG, r.run, async (url, init) => { - sent.push({ url, body: JSON.parse(init.body), auth: init.headers.Authorization }); + sent.push({ url, body: JSON.parse(init.body as string), auth: init.headers.Authorization }); return { ok: true, status: 200, @@ -663,14 +663,14 @@ describe("operations", () => { : JSON.stringify({ labels: [{ name: "now" }] }), ); await expect(new Ops(CFG, failing.run).setRank("12279", "next")).rejects.toThrow(/502/); - expect(failing.calls.map((c) => c.argv[4])).toEqual([undefined, "POST", "DELETE"]); + expect(failing.calls.map((c): string | undefined => c.argv[4])).toEqual([undefined, "POST", "DELETE"]); // A rank the issue already has is not added again; "none" only removes. const same = recorder(() => JSON.stringify({ labels: [{ name: "now" }, { name: "next" }] })); await new Ops(CFG, same.run).setRank("12279", "now"); - expect(same.calls.map((c) => c.argv[4])).toEqual([undefined, "DELETE"]); + expect(same.calls.map((c): string | undefined => c.argv[4])).toEqual([undefined, "DELETE"]); const none = recorder(() => JSON.stringify({ labels: [{ name: "now" }] })); await new Ops(CFG, none.run).setRank("12279", "none"); - expect(none.calls.map((c) => c.argv[4])).toEqual([undefined, "DELETE"]); + expect(none.calls.map((c): string | undefined => c.argv[4])).toEqual([undefined, "DELETE"]); await expect(ops.setRank("12279", "urgent")).rejects.toThrow(/rank/); await expect(ops.setRank("12279/labels", "now")).rejects.toThrow(OpError); }); @@ -1365,7 +1365,7 @@ describe("HTTP surface", () => { { origin: "https://evil.example" }, { origin: "null" }, { origin: "https://pm.security.cab", "sec-fetch-site": "cross-site" }, - ]) { + ] as Record[]) { expect((await handle(post("/api/cards/7/move", { to: "done" }, headers), d)).status).toBe(403); } expect(d.sent.length).toBe(before + 1); @@ -1387,10 +1387,10 @@ describe("HTTP surface", () => { expect(readConfig({ HOME: "/h" }).androidApk).toBe("/h/.local/state/pm-console/android/pm-console.apk"); const owner = { ...deps("owner"), cfg }; // No build yet: the page is told so, and the link answers 404. - expect((await (await handle(get("/console/api/me"), owner)).json()).androidApp).toBe(false); + expect(((await (await handle(get("/console/api/me"), owner)).json()) as { androidApp: boolean }).androidApp).toBe(false); expect((await handle(get("/console/pm-console.apk"), owner)).status).toBe(404); writeFileSync(join(dir, "pm-console.apk"), "PK\u0003\u0004 signed apk bytes"); - expect((await (await handle(get("/console/api/me"), owner)).json()).androidApp).toBe(true); + expect(((await (await handle(get("/console/api/me"), owner)).json()) as { androidApp: boolean }).androidApp).toBe(true); const res = await handle(get("/console/pm-console.apk"), owner); expect(res.status).toBe(200); expect(res.headers.get("content-type")).toBe("application/vnd.android.package-archive"); @@ -1427,7 +1427,7 @@ describe("HTTP surface", () => { }; const refused = await handle(post("/console/api/prs/12240/merge", {}), d); expect(refused.status).toBe(502); - expect((await refused.json()).error).toContain("not taken"); + expect(((await refused.json()) as { error: string }).error).toContain("not taken"); expect(r.calls.length).toBe(1); // The record of the outcome cannot be written: the merge happened, and diff --git a/pm-stack/console/test/edge.test.ts b/pm-stack/console/test/edge.test.ts index a8becb4d7..b987b2f74 100644 --- a/pm-stack/console/test/edge.test.ts +++ b/pm-stack/console/test/edge.test.ts @@ -76,7 +76,7 @@ async function setup(options: { direct?: boolean } = {}) { /** The release GitHub holds at the app's fixed address: its * description and its file, either of which may be missing; whether * GitHub can be reached at all; and every file asked for, in order. */ - const github: { version: unknown; apk: Uint8Array | null; down: boolean; asked: string[] } = { + const github: { version: unknown; apk: Uint8Array | null; down: boolean; asked: string[] } = { version: null, apk: null, down: false, @@ -803,7 +803,7 @@ async function setup(options: { direct?: boolean } = {}) { /** Each report the host sent, as it read. */ const reports: { sections: { name: string }[]; have: Record }[] = []; /** The signed Android build on the host, if any, and how often it was read to be sent. */ - const build: { bytes: Uint8Array | null; at: number; reads: number; corrupt: boolean; unreadable: boolean } = { + const build: { bytes: Uint8Array | null; at: number; reads: number; corrupt: boolean; unreadable: boolean } = { bytes: null, at: 0, reads: 0, @@ -1123,7 +1123,7 @@ describe("before the host's first report", () => { expect(answer).toMatchObject({ ok: true, watching: false, notify: false, want: [], commands: [] }); // The key came from the host itself, through the tunnel. expect(w.hostSaw).toEqual(["GET /console/edge/key"]); - const kept = w.stored.get("key") as { kid: string; jwk: Record }; + const kept = w.stored.get("key") as { kid: string; jwk: { kty: string; crv: string; x: string; y: string } }; expect(kept.jwk).toEqual(await w.link.now.publicKey()); expect(JSON.stringify([...w.stored.entries()])).not.toContain('"d":'); // never a private key const health = (await (await w.browse("/console/health", { token: null })).json()) as Record; @@ -2164,7 +2164,7 @@ describe("the Android build", () => { }); /** A release as android/release.sh describes it, for these bytes. */ - const described = async (bytes: Uint8Array, name: string, code: number) => { + const described = async (bytes: Uint8Array, name: string, code: number) => { const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes)); return { name, @@ -2480,7 +2480,7 @@ describe("the host's report is the host's", () => { w.now.t += 100; const unread = await report(w, () => {}, hosts, undefined, flooder); expect(unread.status).toBe(429); - expect((await unread.json()).error).toMatch(/too many reports from this address/); + expect(((await unread.json()) as { error: string }).error).toMatch(/too many reports from this address/); // "Before anything is read" is meant literally: a body that fails // the moment it is touched is never touched. const untouchable = (headers: Record) => @@ -3738,7 +3738,7 @@ describe("the edge does the core itself", () => { about: { issue: 301 }, rank: "expedite", }); - expect((await note.json()).result).toMatchObject({ issue: 301, commented: true, queued: true }); + expect(((await note.json()) as { result: Record }).result).toMatchObject({ issue: 301, commented: true, queued: true }); expect((w.ghComments[301] ?? []).some((c) => c.body.startsWith("do this first\n\n---\n"))).toBe(true); expect(w.ghLabels[301]).toEqual(["owner/intake", "expedite"]); // As a session: the owner's prompt, with the routing labels added. @@ -3748,7 +3748,7 @@ describe("the edge does the core itself", () => { text: "pick these up first", mode: "session", }); - expect((await session.json()).result).toMatchObject({ issue: 501, commented: false, queued: true }); + expect(((await session.json()) as { result: Record }).result).toMatchObject({ issue: 501, commented: false, queued: true }); expect(w.ghLabels[501]).toEqual(["owner/prompt", "expedite", "source/human", "owner/intake", "app/fleet"]); // Three tasks wait for a machine, each as the action that builds it there. const at = new Date(w.now.t).toISOString(); diff --git a/pm-stack/console/test/plan.test.ts b/pm-stack/console/test/plan.test.ts index 4d6af3309..7af087cf1 100644 --- a/pm-stack/console/test/plan.test.ts +++ b/pm-stack/console/test/plan.test.ts @@ -934,7 +934,7 @@ describe("over HTTP", () => { const { d, w } = served((c) => c.path.includes("/issues?labels=") ? [] : c.path.includes("/contents/") ? { content: "" } : { items: [] }, ); - const caps = await (await handle(get("/console/api/roadmaps"), d)).json(); + const caps = (await (await handle(get("/console/api/roadmaps"), d)).json()) as { id: string; title: string; file: string | null }[]; expect(caps.slice(0, 4)).toEqual([ { id: "id", title: "id", file: "planning/roadmaps/id.md" }, { id: "fleet", title: "Fleet", file: null }, @@ -956,7 +956,7 @@ describe("over HTTP", () => { expect((await handle(get("/console/api/roadmap?area=nonesuch"), d)).status).toBe(400); expect((await handle(get("/console/api/roadmap"), d)).status).toBe(400); expect(await (await handle(get("/console/api/bugs"), d)).json()).toMatchObject({ open: [], fixed: [] }); - expect((await (await handle(get("/console/api/intake"), d)).json()).items).toEqual([]); + expect(((await (await handle(get("/console/api/intake"), d)).json()) as { items: unknown[] }).items).toEqual([]); const before = w.calls.length; await handle(get("/console/api/bugs?fresh=1"), d); await handle(get("/console/api/intake?fresh=1"), d); @@ -983,7 +983,7 @@ describe("over HTTP", () => { d, ); expect(res.status).toBe(200); - expect((await res.json()).result).toEqual({ + expect(((await res.json()) as { result: Record }).result).toEqual({ issue: 13020, url: "https://github.com/x/y/issues/13020", commented: false, diff --git a/pm-stack/console/test/platform.test.ts b/pm-stack/console/test/platform.test.ts index 38f256ec0..90c31e32e 100644 --- a/pm-stack/console/test/platform.test.ts +++ b/pm-stack/console/test/platform.test.ts @@ -18,7 +18,7 @@ import { Ops, type Run } from "../src/ops.ts"; import { DEPLOYED, GROUPS, STATES, type WorkerRow, workerRow, workersList } from "../src/platform.ts"; import { letters, pick, rng } from "./support.ts"; -const good = { +const good: Omit = { name: "message-bridge", title: "Message bridge", group: "fleet", diff --git a/pm-stack/console/test/push.test.ts b/pm-stack/console/test/push.test.ts index 5b7a5bc43..8a0eddab2 100644 --- a/pm-stack/console/test/push.test.ts +++ b/pm-stack/console/test/push.test.ts @@ -94,7 +94,7 @@ describe("the message a phone receives", () => { // It still holds both devices, each known by its mark and not by its address. const marks = [await endpointMark(a.subscription.endpoint), await endpointMark(m.subscription.endpoint)]; // And what came of the send above, which a restart does not lose. - const last = { at: new Date(NOW).toISOString(), class: "replies", sent: 2, gone: 0, failed: 0, refused: [] }; + const last = { at: new Date(NOW).toISOString(), class: "replies" as const, sent: 2, gone: 0, failed: 0, refused: [] }; expect(await again.status()).toEqual({ ...status, devices: 2, known: marks, last }); expect(status.known).toEqual([]); expect(marks[0]).toMatch(/^[A-Za-z0-9_-]{16}$/); @@ -112,7 +112,7 @@ describe("the message a phone receives", () => { try { const a = await browser(); const b = await browser("web.push.apple.com"); - let answer = (url: string) => (url === a.subscription.endpoint ? 410 : 201); + let answer: (url: string) => number = (url: string) => (url === a.subscription.endpoint ? 410 : 201); let posts = 0; const push = new Push( fileStore(join(dir, "push.json")), diff --git a/pm-stack/console/test/support.ts b/pm-stack/console/test/support.ts index 356d47aca..c39981826 100644 --- a/pm-stack/console/test/support.ts +++ b/pm-stack/console/test/support.ts @@ -27,8 +27,8 @@ export async function browser(host = "fcm.googleapis.com") { const hkdf = async (salt: Uint8Array, ikm: Uint8Array, info: Uint8Array, n: number) => new Uint8Array( await crypto.subtle.deriveBits( - { name: "HKDF", hash: "SHA-256", salt, info }, - await crypto.subtle.importKey("raw", ikm, "HKDF", false, ["deriveBits"]), + { name: "HKDF", hash: "SHA-256", salt: salt as BufferSource, info: info as BufferSource }, + await crypto.subtle.importKey("raw", ikm as BufferSource, "HKDF", false, ["deriveBits"]), n * 8, ), ); diff --git a/pm-stack/console/test/work.test.ts b/pm-stack/console/test/work.test.ts index a2dea8555..35a4d1960 100644 --- a/pm-stack/console/test/work.test.ts +++ b/pm-stack/console/test/work.test.ts @@ -991,7 +991,7 @@ describe("the interface's endpoints", () => { const { d } = deps(); const res = await handle(get("/console/api/doc/questions"), d); expect(res.status).toBe(200); - const body = await res.json(); + const body = (await res.json()) as { questions: { total: number } }; expect(body.questions.total).toBe(4); expect(JSON.stringify(body)).not.toContain(TOKEN); const etag = res.headers.get("etag") ?? ""; @@ -1026,7 +1026,7 @@ describe("the interface's endpoints", () => { expect(await second.json()).toEqual({ ok: true, result: { task: 21016, priority: 10 }, again: true }); expect(moves()).toBe(1); // Its record of what was done carries the name, once. - const core = await (await handle(get("/console/api/doc/core"), d)).json(); + const core = (await (await handle(get("/console/api/doc/core"), d)).json()) as { acts: { id: string; status: string }[] }; expect(core.acts.map((a: { id: string; status: string }) => [a.id, a.status])).toEqual([[id, "done"]]); // With nowhere to write names down, an action is done as often as it is asked: as it always was. const bare = deps(); @@ -1232,7 +1232,7 @@ describe("the interface's endpoints", () => { ["need.comment", "done", { ref: `${REPO}#5` }], ]); expect(JSON.stringify(audited)).not.toContain("private words"); - const core = await (await handle(get("/console/api/doc/core"), d)).json(); + const core = (await (await handle(get("/console/api/doc/core"), d)).json()) as { acts: { kind: string; status: string }[] }; expect(core.acts.map((a: { kind: string; status: string }) => [a.kind, a.status])).toEqual([ ["need.comment", "done"], ["task.move", "done"], @@ -1250,7 +1250,7 @@ describe("the interface's endpoints", () => { expect((await handle(post("/console/api/act", body), d)).status).toBe(400); } expect(w.calls.length).toBe(before); - const failed = (await (await handle(get("/console/api/doc/core"), d)).json()).acts[0]; + const failed = ((await (await handle(get("/console/api/doc/core"), d)).json()) as { acts: { kind: string; status: string }[] }).acts[0]; expect([failed.kind, failed.status]).toEqual(["task.move", "failed"]); await expect(perform(actions(w.ops, new Work(w.ops)), deps().docs, "__proto__", {})).rejects.toThrow( /not an action/, diff --git a/pm-stack/console/tsconfig.json b/pm-stack/console/tsconfig.json new file mode 100644 index 000000000..40baff115 --- /dev/null +++ b/pm-stack/console/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "strict": true, + "noEmit": true, + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "skipLibCheck": true, + "allowJs": true, + "types": ["bun", "@cloudflare/workers-types"], + "typeRoots": ["./edge/node_modules/@types", "./edge/node_modules"] + }, + "include": ["src", "edge/src", "test", "scripts"] +}