Skip to content

Commit

Permalink
Enable strict type checking in TS config (#585)
Browse files Browse the repository at this point in the history
* Enable strict type checking in TS config

* Fix tests, add test script to package.json
  • Loading branch information
klejejs authored Sep 24, 2024
1 parent affd744 commit 8341ea3
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 23 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

- uses: actions/[email protected]
with:
node-version-file: '.nvmrc'
node-version-file: ".nvmrc"

- name: Set wrangler version
id: wrangler
Expand All @@ -42,10 +42,10 @@ jobs:

- uses: actions/[email protected]
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
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"scripts": {
"test": "jest"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240919.0",
"@types/jest": "^27.5.0",
Expand Down
4 changes: 2 additions & 2 deletions src/data/currency.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// From http://country.io/currency.json

export const countryCurrency = {
export const countryCurrency: Record<Iso3166Alpha2Code | "T1", string> = {
BD: "BDT",
BE: "EUR",
BF: "XOF",
Expand Down Expand Up @@ -146,7 +146,6 @@ export const countryCurrency = {
NR: "AUD",
NU: "NZD",
CK: "NZD",
XK: "EUR",
CI: "XOF",
CH: "CHF",
CO: "COP",
Expand Down Expand Up @@ -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
};
6 changes: 4 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ export async function handleRequestWrapper(
): Promise<Response> {
try {
return await serviceHandler(requestUrl, event, sentry);
} catch (err) {
} catch (err: any) {
if (!(err instanceof ServiceError)) {
err = new ServiceError(err.message);
}
sentry.addBreadcrumb({ message: err.message });
const captureId = sentry.captureException(err);

let returnBody: string;
const headers = { "Access-Control-Allow-Origin": "*" };
const headers: Record<string, string> = {
"Access-Control-Allow-Origin": "*",
};

if ((event.request.headers.get("accept") || "").includes("json")) {
returnBody = JSON.stringify({ error: err.errorType });
Expand Down
12 changes: 7 additions & 5 deletions src/services/assist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ const createResponse = (options: {

const handleUploadAudioFile = async (event: WorkerEvent): Promise<Response> => {
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);
Expand All @@ -49,15 +51,15 @@ const handleUploadAudioFile = async (event: WorkerEvent): Promise<Response> => {
});
}

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}`,
},
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}]`,
Expand Down Expand Up @@ -105,7 +107,7 @@ const handleUploadAudioFile = async (event: WorkerEvent): Promise<Response> => {
export async function assistHandler(
requestUrl: URL,
event: WorkerEvent,
sentry: Toucan
_sentry: Toucan
): Promise<Response> {
if (event.request.method === "OPTIONS") {
// CORS preflight request
Expand Down
8 changes: 6 additions & 2 deletions src/services/whoami.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export async function whoamiHandler(
const httpResponse: Map<string, any> = 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),
})
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/assist.handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe("Assist handler", function () {
Object.entries({
"CF-Connecting-IP": "1.2.3.4",
"content-type": contentType,
"content-length": 150 * 1024,
})
);

Expand Down
10 changes: 8 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -13,4 +19,4 @@
"./node_modules/@cloudflare/workers-types/index.d.ts"
],
"exclude": ["node_modules/", "dist/"]
}
}
14 changes: 7 additions & 7 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
]
vars = { WORKER_ENV = "dev" }
r2_buckets = [
{ binding = "WAKEWORD_TRAINING_BUCKET", bucket_name = "assist-wakeword-training-data", preview_bucket_name = "assist-wakeword-training-data-test" },
]

0 comments on commit 8341ea3

Please sign in to comment.