From 8341ea31376269f9bd204e702f77040d4f151a64 Mon Sep 17 00:00:00 2001 From: Krisjanis Lejejs Date: Tue, 24 Sep 2024 16:46:07 +0300 Subject: [PATCH] Enable strict type checking in TS config (#585) * Enable strict type checking in TS config * Fix tests, add test script to package.json --- .github/workflows/test.yaml | 6 +++--- package.json | 3 +++ src/data/currency.ts | 4 ++-- src/router.ts | 6 ++++-- src/services/assist.ts | 12 +++++++----- src/services/whoami.ts | 8 ++++++-- tests/assist.handler.spec.ts | 1 + tsconfig.json | 10 ++++++++-- wrangler.toml | 14 +++++++------- 9 files changed, 41 insertions(+), 23 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 1b24226..597d0e6 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -19,7 +19,7 @@ jobs: - uses: actions/setup-node@v4.0.4 with: - node-version-file: '.nvmrc' + node-version-file: ".nvmrc" - name: Set wrangler version id: wrangler @@ -42,10 +42,10 @@ jobs: - uses: actions/setup-node@v4.0.4 with: - node-version-file: '.nvmrc' + node-version-file: ".nvmrc" - name: Install packages run: yarn install - name: Test - run: ./node_modules/.bin/jest + run: yarn test diff --git a/package.json b/package.json index fce79c2..8527ced 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,7 @@ { + "scripts": { + "test": "jest" + }, "devDependencies": { "@cloudflare/workers-types": "^4.20240919.0", "@types/jest": "^27.5.0", diff --git a/src/data/currency.ts b/src/data/currency.ts index a263641..2a069d7 100644 --- a/src/data/currency.ts +++ b/src/data/currency.ts @@ -1,6 +1,6 @@ // From http://country.io/currency.json -export const countryCurrency = { +export const countryCurrency: Record = { BD: "BDT", BE: "EUR", BF: "XOF", @@ -146,7 +146,6 @@ export const countryCurrency = { NR: "AUD", NU: "NZD", CK: "NZD", - XK: "EUR", CI: "XOF", CH: "CHF", CO: "COP", @@ -251,4 +250,5 @@ export const countryCurrency = { UA: "UAH", QA: "QAR", MZ: "MZN", + T1: "", // T1 is a special case for Tor requests that Cloudflare cannot determine a country code for }; diff --git a/src/router.ts b/src/router.ts index 3c751c3..8bee5fd 100644 --- a/src/router.ts +++ b/src/router.ts @@ -45,7 +45,7 @@ export async function handleRequestWrapper( ): Promise { try { return await serviceHandler(requestUrl, event, sentry); - } catch (err) { + } catch (err: any) { if (!(err instanceof ServiceError)) { err = new ServiceError(err.message); } @@ -53,7 +53,9 @@ export async function handleRequestWrapper( const captureId = sentry.captureException(err); let returnBody: string; - const headers = { "Access-Control-Allow-Origin": "*" }; + const headers: Record = { + "Access-Control-Allow-Origin": "*", + }; if ((event.request.headers.get("accept") || "").includes("json")) { returnBody = JSON.stringify({ error: err.errorType }); diff --git a/src/services/assist.ts b/src/services/assist.ts index e69695b..4c31276 100644 --- a/src/services/assist.ts +++ b/src/services/assist.ts @@ -31,8 +31,10 @@ const createResponse = (options: { const handleUploadAudioFile = async (event: WorkerEvent): Promise => { const { request } = event; - const contentType = request.headers.get("content-type").split(";")[0]; - const contentLength = parseInt(request.headers.get("content-length"), 10); + const contentType = request.headers.get("content-type")?.split(";")[0]; + const contentLengthHeaderValue = request.headers.get("content-length"); + const contentLength = + contentLengthHeaderValue && parseInt(contentLengthHeaderValue, 10); const cfRay = request.headers.get("cf-ray"); const { searchParams } = new URL(request.url); @@ -49,7 +51,7 @@ const handleUploadAudioFile = async (event: WorkerEvent): Promise => { }); } - if (!WAKE_WORD_ALLOWED_CONTENT_TYPES.includes(contentType)) { + if (!contentType || !WAKE_WORD_ALLOWED_CONTENT_TYPES.includes(contentType)) { return createResponse({ content: { message: `Invalid content-type, received: ${contentType}, allowed: ${WAKE_WORD_ALLOWED_CONTENT_TYPES}`, @@ -57,7 +59,7 @@ const handleUploadAudioFile = async (event: WorkerEvent): Promise => { status: 415, }); } - if (contentLength > WAKE_WORD_MAX_CONTENT_LENGTH) { + if (!contentLength || contentLength > WAKE_WORD_MAX_CONTENT_LENGTH) { return createResponse({ content: { message: `Invalid content-length, received: ${contentLength}, allowed [<${WAKE_WORD_MAX_CONTENT_LENGTH}]`, @@ -105,7 +107,7 @@ const handleUploadAudioFile = async (event: WorkerEvent): Promise => { export async function assistHandler( requestUrl: URL, event: WorkerEvent, - sentry: Toucan + _sentry: Toucan ): Promise { if (event.request.method === "OPTIONS") { // CORS preflight request diff --git a/src/services/whoami.ts b/src/services/whoami.ts index be58621..7d1cbef 100644 --- a/src/services/whoami.ts +++ b/src/services/whoami.ts @@ -40,7 +40,10 @@ export async function whoamiHandler( const httpResponse: Map = new Map( Object.entries({ timezone: - request.cf.timezone || countryTimeZoneFallback.get(request.cf.country), + request.cf.timezone || + (request.cf.country && + countryTimeZoneFallback.get(request.cf.country)) || + undefined, iso_time: date.toISOString(), timestamp: Math.round(date.getTime() / 1000), }) @@ -52,7 +55,8 @@ export async function whoamiHandler( city: request.cf.city, continent: request.cf.continent, country: request.cf.country, - currency: countryCurrency[request.cf.country] || null, + currency: + (request.cf.country && countryCurrency[request.cf.country]) || null, latitude: request.cf.latitude, longitude: request.cf.longitude, postal_code: request.cf.postalCode, diff --git a/tests/assist.handler.spec.ts b/tests/assist.handler.spec.ts index 4654d21..9f601f8 100644 --- a/tests/assist.handler.spec.ts +++ b/tests/assist.handler.spec.ts @@ -72,6 +72,7 @@ describe("Assist handler", function () { Object.entries({ "CF-Connecting-IP": "1.2.3.4", "content-type": contentType, + "content-length": 150 * 1024, }) ); diff --git a/tsconfig.json b/tsconfig.json index 06232d4..b3d51b3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,13 @@ "target": "ES2020", "module": "CommonJS", "lib": ["ES2020", "WebWorker"], - "types": ["@cloudflare/workers-types", "jest", "node"] + "types": ["@cloudflare/workers-types", "jest", "node"], + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true }, "include": [ "./src/*.ts", @@ -13,4 +19,4 @@ "./node_modules/@cloudflare/workers-types/index.d.ts" ], "exclude": ["node_modules/", "dist/"] -} \ No newline at end of file +} diff --git a/wrangler.toml b/wrangler.toml index e27d800..6f4f053 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -8,15 +8,15 @@ send_metrics = false [env.production] zone_id = "0ba583492080d3db28c103574f1d19cf" routes = ["*whoami.home-assistant.io/*", "*services.home-assistant.io/*"] -vars = {WORKER_ENV = "production"} -r2_buckets = [ - { binding = "WAKEWORD_TRAINING_BUCKET", bucket_name = "assist-wakeword-training-data", preview_bucket_name="assist-wakeword-training-data-test" } +vars = { WORKER_ENV = "production" } +r2_buckets = [ + { binding = "WAKEWORD_TRAINING_BUCKET", bucket_name = "assist-wakeword-training-data", preview_bucket_name = "assist-wakeword-training-data-test" }, ] # For dev environment, use '-e dev' [env.dev] workers_dev = true -vars = {WORKER_ENV = "dev"} -r2_buckets = [ - { binding = "WAKEWORD_TRAINING_BUCKET", bucket_name = "assist-wakeword-training-data", preview_bucket_name="assist-wakeword-training-data-test" } -] \ No newline at end of file +vars = { WORKER_ENV = "dev" } +r2_buckets = [ + { binding = "WAKEWORD_TRAINING_BUCKET", bucket_name = "assist-wakeword-training-data", preview_bucket_name = "assist-wakeword-training-data-test" }, +]