From 52fd611e37cd658dc54be7cb64806ce0e28366f8 Mon Sep 17 00:00:00 2001 From: Samuel Macleod Date: Wed, 20 Nov 2024 18:55:05 +0000 Subject: [PATCH 1/5] Cleanup types --- .../asset-worker/tests/handler.test.ts | 2 +- .../asset-worker/tests/kv.test.ts | 1 + .../asset-worker/tests/tsconfig.json | 9 +- .../workers-shared/asset-worker/tsconfig.json | 7 +- .../asset-worker/vitest.config.mts | 49 +++++++ packages/workers-shared/package.json | 6 +- .../router-worker/tsconfig.json | 7 +- packages/workers-shared/tsconfig.json | 19 ++- packages/workers-shared/tsconfig.node.json | 12 ++ packages/workers-shared/utils/tsconfig.json | 15 --- packages/workers-shared/vitest.config.mts | 12 -- pnpm-lock.yaml | 127 ++++++++++++++++-- 12 files changed, 211 insertions(+), 55 deletions(-) create mode 100644 packages/workers-shared/asset-worker/vitest.config.mts create mode 100644 packages/workers-shared/tsconfig.node.json delete mode 100644 packages/workers-shared/utils/tsconfig.json delete mode 100644 packages/workers-shared/vitest.config.mts diff --git a/packages/workers-shared/asset-worker/tests/handler.test.ts b/packages/workers-shared/asset-worker/tests/handler.test.ts index 5f31c39a14b6..455fc4648c01 100644 --- a/packages/workers-shared/asset-worker/tests/handler.test.ts +++ b/packages/workers-shared/asset-worker/tests/handler.test.ts @@ -1,4 +1,4 @@ -import { vi } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import { handleRequest } from "../src/handler"; import type { AssetConfig } from "../../utils/types"; diff --git a/packages/workers-shared/asset-worker/tests/kv.test.ts b/packages/workers-shared/asset-worker/tests/kv.test.ts index 1435b1c390c0..230046309cbe 100644 --- a/packages/workers-shared/asset-worker/tests/kv.test.ts +++ b/packages/workers-shared/asset-worker/tests/kv.test.ts @@ -1,3 +1,4 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { getAssetWithMetadataFromKV } from "../src/utils/kv"; import type { AssetMetadata } from "../src/utils/kv"; import type { MockInstance } from "vitest"; diff --git a/packages/workers-shared/asset-worker/tests/tsconfig.json b/packages/workers-shared/asset-worker/tests/tsconfig.json index ff9b901a0662..0869caf63444 100644 --- a/packages/workers-shared/asset-worker/tests/tsconfig.json +++ b/packages/workers-shared/asset-worker/tests/tsconfig.json @@ -1,7 +1,10 @@ { - "extends": "@cloudflare/workers-tsconfig/tsconfig.json", + "extends": "../tsconfig.json", "compilerOptions": { - "types": ["@cloudflare/workers-types/experimental", "vitest/globals"] + "types": [ + "@cloudflare/workers-types/experimental", + "@cloudflare/vitest-pool-workers" + ] }, - "include": ["**/*.ts"] + "include": ["**/*.test.ts"] } diff --git a/packages/workers-shared/asset-worker/tsconfig.json b/packages/workers-shared/asset-worker/tsconfig.json index 965b1378614d..12efc336697d 100644 --- a/packages/workers-shared/asset-worker/tsconfig.json +++ b/packages/workers-shared/asset-worker/tsconfig.json @@ -2,8 +2,8 @@ "compilerOptions": { "target": "es2021", "lib": ["es2021"], - "module": "NodeNext", - "moduleResolution": "nodenext", + "module": "es2022", + "moduleResolution": "Bundler", "types": ["@cloudflare/workers-types/experimental"], "noEmit": true, "isolatedModules": true, @@ -11,5 +11,6 @@ "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true - } + }, + "include": ["src/**/*.ts"] } diff --git a/packages/workers-shared/asset-worker/vitest.config.mts b/packages/workers-shared/asset-worker/vitest.config.mts new file mode 100644 index 000000000000..192c841cd7a4 --- /dev/null +++ b/packages/workers-shared/asset-worker/vitest.config.mts @@ -0,0 +1,49 @@ +import { defineWorkersProject } from "@cloudflare/vitest-pool-workers/config"; + +class FilteredPushArray extends Array { + constructor(private readonly predicate: (item: T) => boolean) { + super(); + } + + push(...items: T[]) { + return super.push(...items.filter(this.predicate)); + } +} + +export default defineWorkersProject({ + test: { + poolOptions: { + workers: { + singleWorker: true, + wrangler: { + configPath: "./wrangler.toml", + }, + }, + }, + // Configure the `vite-node` server used by Vitest code to import configs, + // custom pools and tests. By default, Vitest effectively applies Vite + // transforms to all files outside `node_modules`. This means by default, + // our custom pool code is transformed by Vite during development, but not + // when published, leading to possible behaviour mismatches. To fix this, + // we ensure file paths containing `packages/vitest-pool-workers/dist` are + // always "externalised", meaning they're imported directly by Node. + server: { + deps: { + // Vitest automatically adds `/^(?!.*node_modules).*\.mjs$/` as an + // `inline` RegExp: https://github.com/vitest-dev/vitest/blob/v2.1.1/packages/vitest/src/constants.ts#L9 + // We'd like `packages/vitest-pool-workers/dist/pool/index.mjs` to be + // externalised though. Unfortunately, `inline`s are checked before + // `external`s, so there's no nice way we can override this. Instead, + // we prevent the extra `inline` being added in the first place. + inline: new FilteredPushArray((item) => { + const str = `${item}`; + return str !== "/^(?!.*node_modules).*\\.mjs$/"; + }), + external: [ + /packages\/vitest-pool-workers\/dist/, + /packages\/wrangler\//, + ], + }, + }, + }, +}); diff --git a/packages/workers-shared/package.json b/packages/workers-shared/package.json index 608104f3eb8f..23431237e587 100644 --- a/packages/workers-shared/package.json +++ b/packages/workers-shared/package.json @@ -29,8 +29,9 @@ "bundle:router-worker": "esbuild router-worker/src/index.ts --format=esm --bundle --outfile=dist/router-worker.mjs --sourcemap=external", "bundle:router-worker:prod": "pnpm run bundle:router-worker --minify", "check:lint": "eslint . --max-warnings=0", - "check:type": "pnpm run check:type:tests && tsc", - "check:type:tests": "tsc -p ./asset-worker/tests/tsconfig.json", + "check:type": "tsc -p ./tsconfig.json && tsc -p ./tsconfig.node.json", + "check:type:asset-worker": "tsc -p ./asset-worker/tsconfig.json && tsc -p ./asset-worker/tests/tsconfig.json", + "check:type:router-worker": "tsc -p ./router-worker/tsconfig.json && tsc -p ./router-worker/tests/tsconfig.json", "clean": "rimraf dist", "deploy": "pnpm run deploy:router-worker && pnpm run deploy:asset-worker", "deploy:asset-worker": "CLOUDFLARE_API_TOKEN=$WORKERS_DEPLOY_AND_CONFIG_CLOUDFLARE_API_TOKEN pnpx wrangler versions upload --experimental-versions -c asset-worker/wrangler.toml", @@ -46,6 +47,7 @@ }, "devDependencies": { "@cloudflare/eslint-config-worker": "workspace:*", + "@cloudflare/vitest-pool-workers": "^0.5.29", "@cloudflare/workers-tsconfig": "workspace:*", "@cloudflare/workers-types": "^4.20241106.0", "@types/mime": "^3.0.4", diff --git a/packages/workers-shared/router-worker/tsconfig.json b/packages/workers-shared/router-worker/tsconfig.json index 965b1378614d..12efc336697d 100644 --- a/packages/workers-shared/router-worker/tsconfig.json +++ b/packages/workers-shared/router-worker/tsconfig.json @@ -2,8 +2,8 @@ "compilerOptions": { "target": "es2021", "lib": ["es2021"], - "module": "NodeNext", - "moduleResolution": "nodenext", + "module": "es2022", + "moduleResolution": "Bundler", "types": ["@cloudflare/workers-types/experimental"], "noEmit": true, "isolatedModules": true, @@ -11,5 +11,6 @@ "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true - } + }, + "include": ["src/**/*.ts"] } diff --git a/packages/workers-shared/tsconfig.json b/packages/workers-shared/tsconfig.json index e112be387120..a6923e31b8f1 100644 --- a/packages/workers-shared/tsconfig.json +++ b/packages/workers-shared/tsconfig.json @@ -1,12 +1,17 @@ { - "extends": "@cloudflare/workers-tsconfig/tsconfig.json", "compilerOptions": { - "module": "esnext", - "sourceMap": true, + "target": "es2021", + "lib": ["es2021"], + "module": "es2022", + "moduleResolution": "Bundler", + "types": ["@cloudflare/workers-types/experimental"], + "noEmit": true, + "isolatedModules": true, + "allowSyntheticDefaultImports": true, "forceConsistentCasingInFileNames": true, - "useUnknownInCatchVariables": false, - "types": ["@cloudflare/workers-types/experimental"] + "strict": true, + "skipLibCheck": true }, - "include": ["**/*.ts", "vitest.config.mts"], - "exclude": ["node_modules", "dist", "**/tests", "**/*.test.ts"] + "include": ["**/*.ts"], + "exclude": ["scripts/**"] } diff --git a/packages/workers-shared/tsconfig.node.json b/packages/workers-shared/tsconfig.node.json new file mode 100644 index 000000000000..143ec4bd3629 --- /dev/null +++ b/packages/workers-shared/tsconfig.node.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "composite": true, + "skipLibCheck": true, + "module": "NodeNext", + "moduleResolution": "NodeNext", + "allowSyntheticDefaultImports": true, + "target": "ES2021", + "lib": ["ES2021"] + }, + "include": ["./**/vitest.config.mts", "./scripts/**/*.ts"] +} diff --git a/packages/workers-shared/utils/tsconfig.json b/packages/workers-shared/utils/tsconfig.json deleted file mode 100644 index 33c65059b33a..000000000000 --- a/packages/workers-shared/utils/tsconfig.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "compilerOptions": { - "target": "es2021", - "lib": ["es2021"], - "module": "NodeNext", - "moduleResolution": "nodenext", - "types": ["@cloudflare/workers-types/experimental", "@types/node"], - "noEmit": true, - "isolatedModules": true, - "allowSyntheticDefaultImports": true, - "forceConsistentCasingInFileNames": true, - "strict": true, - "skipLibCheck": true - } -} diff --git a/packages/workers-shared/vitest.config.mts b/packages/workers-shared/vitest.config.mts deleted file mode 100644 index 363c185f0a91..000000000000 --- a/packages/workers-shared/vitest.config.mts +++ /dev/null @@ -1,12 +0,0 @@ -import { defineProject, mergeConfig } from "vitest/config"; -import configShared from "../../vitest.shared"; - -export default mergeConfig( - configShared, - defineProject({ - test: { - include: ["asset-worker/tests/**.{test,spec}.{ts,js}"], - globals: true, - }, - }) -); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 73acc46b8003..719464e85fad 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -961,7 +961,7 @@ importers: version: 8.57.0 eslint-config-turbo: specifier: latest - version: 2.2.3(eslint@8.57.0) + version: 2.3.0(eslint@8.57.0) eslint-plugin-import: specifier: 2.26.x version: 2.26.0(@typescript-eslint/parser@6.10.0(eslint@8.57.0)(typescript@5.6.3))(eslint@8.57.0) @@ -1576,6 +1576,9 @@ importers: '@cloudflare/eslint-config-worker': specifier: workspace:* version: link:../eslint-config-worker + '@cloudflare/vitest-pool-workers': + specifier: ^0.5.29 + version: 0.5.29(@cloudflare/workers-types@4.20241106.0)(@vitest/runner@2.1.3)(@vitest/snapshot@2.1.3)(vitest@2.1.3) '@cloudflare/workers-tsconfig': specifier: workspace:* version: link:../workers-tsconfig @@ -2008,7 +2011,7 @@ packages: '@azure/core-http@3.0.4': resolution: {integrity: sha512-Fok9VVhMdxAFOtqiiAtg74fL0UJkt0z3D+ouUUxcRLzZNBioPRAMJFVxiWoJljYpXsRi4GDQHzQHDc9AiYaIUQ==} engines: {node: '>=14.0.0'} - deprecated: deprecating as we migrated to core v2 + deprecated: This package is no longer supported. Please migrate to use @azure/core-rest-pipeline '@azure/core-lro@2.5.4': resolution: {integrity: sha512-3GJiMVH7/10bulzOKGrrLeG/uCBH/9VtxqaMcB9lIqAeamI/xYQSHJL/KcsLDuH+yTjYpro/u6D/MuRe4dN70Q==} @@ -2414,6 +2417,10 @@ packages: peerDependencies: react: ^15.0.0-0 || ^16.0.0-0 || ^17.0.0-0 + '@cloudflare/kv-asset-handler@0.3.4': + resolution: {integrity: sha512-YLPHc8yASwjNkmcDMQMY35yiWjoKAKnhUbPRszBRS0YgH+IXtsMp61j+yTcnCE3oO2DgP0U3iejLC8FTtKDC8Q==} + engines: {node: '>=16.13'} + '@cloudflare/style-const@5.7.3': resolution: {integrity: sha512-N9Y8bcFXoO7htm+sSVsBmQOVbjLeEY2hy1CBmvt0AoH1zWvs3izwJrnlL0ee4kJ6DkyjaY6SIAkUGUtTOApF3Q==} peerDependencies: @@ -2453,6 +2460,13 @@ packages: '@cloudflare/util-markdown@1.2.15': resolution: {integrity: sha512-H8q/Msk+9Fga6iqqmff7i4mi+kraBCQWFbMEaKIRq3+HBNN5gkpizk05DSG6iIHVxCG1M3WR1FkN9CQ0ZtK4Cw==} + '@cloudflare/vitest-pool-workers@0.5.29': + resolution: {integrity: sha512-UFRT/pUWj8O7FDrdKowjucaiMukMg6mwaDhbXU9FgZvnwhS958Y6TxjMZoUY/5UMp9vNY6wF1V1WeUfkBiB74Q==} + peerDependencies: + '@vitest/runner': 2.0.x - 2.1.x + '@vitest/snapshot': 2.0.x - 2.1.x + vitest: 2.0.x - 2.1.x + '@cloudflare/workerd-darwin-64@1.20241106.1': resolution: {integrity: sha512-zxvaToi1m0qzAScrxFt7UvFVqU8DxrCO2CinM1yQkv5no7pA1HolpIrwZ0xOhR3ny64Is2s/J6BrRjpO5dM9Zw==} engines: {node: '>=16'} @@ -2483,6 +2497,10 @@ packages: cpu: [x64] os: [win32] + '@cloudflare/workers-shared@0.7.1': + resolution: {integrity: sha512-46cP5FCrl3TrvHeoHLb5SRuiDMKH5kc9Yvo36SAfzt8dqJI/qJRoY1GP3ioHn/gP7v2QIoUOTAzIl7Ml7MnfrA==} + engines: {node: '>=16.7.0'} + '@cloudflare/workers-types@4.20241106.0': resolution: {integrity: sha512-pI4ivacmp+vgNO/siHDsZ6BdITR0LC4Mh/1+yzVLcl9U75pt5DUDCOWOiqIRFXRq6H65DPnJbEPFo3x9UfgofQ==} @@ -4958,8 +4976,8 @@ packages: peerDependencies: eslint: '>=7.0.0' - eslint-config-turbo@2.2.3: - resolution: {integrity: sha512-/zwNU+G2w0HszXzWILdl6/Catt86ejUG7vsFSdpnFzFAAUbbT2TxgoCFvC1fKtm6+SkQsXwkRRe9tFz0aMftpg==} + eslint-config-turbo@2.3.0: + resolution: {integrity: sha512-Nm9WZgNoUIJw4bpYQugGCDjzYy1TlUD4sQ/nGblL+HdNqJWCj5NqXbJ1k+TBfYedhr65dlGoAFPYUOfjUOmKVg==} peerDependencies: eslint: '>6.6.0' @@ -5033,8 +5051,8 @@ packages: peerDependencies: eslint: ^3 || ^4 || ^5 || ^6 || ^7 || ^8 - eslint-plugin-turbo@2.2.3: - resolution: {integrity: sha512-LHt35VwxthdGVO6hQRfvmFb6ee8/exAzAYWCy4o87Bnp7urltP8qg7xMd4dPSLAhtfnI2xSo1WgeVaR3MeItxw==} + eslint-plugin-turbo@2.3.0: + resolution: {integrity: sha512-2iVUoIhrjp6kI8p0J4NewKPpXaKrHvL4K4eRnNXbqZvP/7xsm4Of+33B3b7m7OsS0UgX8HHOjlB9bEjigKMkMA==} peerDependencies: eslint: '>6.6.0' @@ -6304,6 +6322,11 @@ packages: resolution: {integrity: sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==} engines: {node: '>=4'} + miniflare@3.20241106.0: + resolution: {integrity: sha512-PjOoJKjUUofCueQskfhXlGvvHxZj36UAJAp1DnquMK88MFF50zCULblh0KXMSNM+bXeQYA94Gj06a7kfmBGxPw==} + engines: {node: '>=16.13'} + hasBin: true + minimatch@10.0.1: resolution: {integrity: sha512-ethXTt3SGGR+95gudmqJ1eNhRO7eGEGIgYA9vnPatK4/etz2MEVDno5GMCibdMTuBMyElzIlgxMna3K94XDIDQ==} engines: {node: 20 || >=22} @@ -8314,6 +8337,16 @@ packages: engines: {node: '>=16'} hasBin: true + wrangler@3.88.0: + resolution: {integrity: sha512-1yM5cgerjKkIBL9GOzIWhl8MsyEGNTsN4emJCiLLHJFz52TSNjJJlMbd1x0hX351mfy2MsGgUqKcx8iRL51PcQ==} + engines: {node: '>=16.17.0'} + hasBin: true + peerDependencies: + '@cloudflare/workers-types': ^4.20241106.0 + peerDependenciesMeta: + '@cloudflare/workers-types': + optional: true + wrap-ansi@6.2.0: resolution: {integrity: sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==} engines: {node: '>=8'} @@ -9181,6 +9214,10 @@ snapshots: dependencies: react: 18.3.1 + '@cloudflare/kv-asset-handler@0.3.4': + dependencies: + mime: 3.0.0 + '@cloudflare/style-const@5.7.3(react@18.3.1)': dependencies: '@cloudflare/types': 6.23.6(react@18.3.1) @@ -9245,6 +9282,25 @@ snapshots: lodash.memoize: 4.1.2 marked: 0.3.19 + '@cloudflare/vitest-pool-workers@0.5.29(@cloudflare/workers-types@4.20241106.0)(@vitest/runner@2.1.3)(@vitest/snapshot@2.1.3)(vitest@2.1.3)': + dependencies: + '@vitest/runner': 2.1.3 + '@vitest/snapshot': 2.1.3 + birpc: 0.2.14 + cjs-module-lexer: 1.2.3 + devalue: 4.3.2 + esbuild: 0.17.19 + miniflare: 3.20241106.0 + semver: 7.5.4 + vitest: 2.1.3(@types/node@18.19.59)(@vitest/ui@2.1.3)(msw@2.4.3(typescript@5.6.3))(supports-color@9.2.2) + wrangler: 3.88.0(@cloudflare/workers-types@4.20241106.0) + zod: 3.22.3 + transitivePeerDependencies: + - '@cloudflare/workers-types' + - bufferutil + - supports-color + - utf-8-validate + '@cloudflare/workerd-darwin-64@1.20241106.1': optional: true @@ -9260,6 +9316,11 @@ snapshots: '@cloudflare/workerd-windows-64@1.20241106.1': optional: true + '@cloudflare/workers-shared@0.7.1': + dependencies: + mime: 3.0.0 + zod: 3.22.3 + '@cloudflare/workers-types@4.20241106.0': {} '@colors/colors@1.5.0': @@ -11955,10 +12016,10 @@ snapshots: dependencies: eslint: 8.57.0 - eslint-config-turbo@2.2.3(eslint@8.57.0): + eslint-config-turbo@2.3.0(eslint@8.57.0): dependencies: eslint: 8.57.0 - eslint-plugin-turbo: 2.2.3(eslint@8.57.0) + eslint-plugin-turbo: 2.3.0(eslint@8.57.0) eslint-import-resolver-node@0.3.7: dependencies: @@ -12042,7 +12103,7 @@ snapshots: semver: 6.3.1 string.prototype.matchall: 4.0.8 - eslint-plugin-turbo@2.2.3(eslint@8.57.0): + eslint-plugin-turbo@2.3.0(eslint@8.57.0): dependencies: dotenv: 16.0.3 eslint: 8.57.0 @@ -13407,6 +13468,25 @@ snapshots: min-indent@1.0.1: {} + miniflare@3.20241106.0: + dependencies: + '@cspotcode/source-map-support': 0.8.1 + acorn: 8.11.3 + acorn-walk: 8.3.2 + capnp-ts: 0.7.0(patch_hash=l4yimnxyvkiyj6alnps2ec3sii) + exit-hook: 2.2.1 + glob-to-regexp: 0.4.1 + stoppable: 1.1.0 + undici: 5.28.4 + workerd: 1.20241106.1 + ws: 8.18.0 + youch: 3.2.3 + zod: 3.22.3 + transitivePeerDependencies: + - bufferutil + - supports-color + - utf-8-validate + minimatch@10.0.1: dependencies: brace-expansion: 2.0.1 @@ -15521,6 +15601,35 @@ snapshots: '@cloudflare/workerd-linux-arm64': 1.20241106.1 '@cloudflare/workerd-windows-64': 1.20241106.1 + wrangler@3.88.0(@cloudflare/workers-types@4.20241106.0): + dependencies: + '@cloudflare/kv-asset-handler': 0.3.4 + '@cloudflare/workers-shared': 0.7.1 + '@esbuild-plugins/node-globals-polyfill': 0.2.3(esbuild@0.17.19) + '@esbuild-plugins/node-modules-polyfill': 0.2.2(esbuild@0.17.19) + blake3-wasm: 2.1.5 + chokidar: 4.0.1 + date-fns: 4.1.0 + esbuild: 0.17.19 + itty-time: 1.0.6 + miniflare: 3.20241106.0 + nanoid: 3.3.7 + path-to-regexp: 6.3.0 + resolve: 1.22.8 + resolve.exports: 2.0.2 + selfsigned: 2.1.1 + source-map: 0.6.1 + unenv: unenv-nightly@2.0.0-20241111-080453-894aa31 + workerd: 1.20241106.1 + xxhash-wasm: 1.0.1 + optionalDependencies: + '@cloudflare/workers-types': 4.20241106.0 + fsevents: 2.3.3 + transitivePeerDependencies: + - bufferutil + - supports-color + - utf-8-validate + wrap-ansi@6.2.0: dependencies: ansi-styles: 4.3.0 From fe45d5102d7bbb6867f5eef8e58c9caba03fe49e Mon Sep 17 00:00:00 2001 From: Samuel Macleod Date: Wed, 20 Nov 2024 19:03:32 +0000 Subject: [PATCH 2/5] lint --- packages/workers-shared/tsconfig.node.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/workers-shared/tsconfig.node.json b/packages/workers-shared/tsconfig.node.json index 143ec4bd3629..d32e6b26abad 100644 --- a/packages/workers-shared/tsconfig.node.json +++ b/packages/workers-shared/tsconfig.node.json @@ -6,7 +6,8 @@ "moduleResolution": "NodeNext", "allowSyntheticDefaultImports": true, "target": "ES2021", - "lib": ["ES2021"] + "lib": ["ES2021"], + "noEmit": true }, "include": ["./**/vitest.config.mts", "./scripts/**/*.ts"] } From 4d565fadbc38799395f35060ee4438250052512a Mon Sep 17 00:00:00 2001 From: Samuel Macleod Date: Wed, 20 Nov 2024 19:06:51 +0000 Subject: [PATCH 3/5] lint --- packages/workers-shared/.eslintrc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/workers-shared/.eslintrc.js b/packages/workers-shared/.eslintrc.js index a56877269a90..078248316933 100644 --- a/packages/workers-shared/.eslintrc.js +++ b/packages/workers-shared/.eslintrc.js @@ -1,5 +1,5 @@ module.exports = { root: true, extends: ["@cloudflare/eslint-config-worker"], - ignorePatterns: ["dist"], + ignorePatterns: ["dist", "scripts"], }; From 5f0739778a4c74626e3d737d0c1854b86e11ad04 Mon Sep 17 00:00:00 2001 From: Samuel Macleod Date: Wed, 20 Nov 2024 19:16:55 +0000 Subject: [PATCH 4/5] Move tests --- fixtures/asset-config/README.md | 5 -- fixtures/asset-config/env.d.ts | 8 --- fixtures/asset-config/package.json | 24 --------- fixtures/asset-config/tsconfig.json | 15 ------ fixtures/asset-config/vitest.config.ts | 52 ------------------- .../asset-worker/tests}/html-handling.test.ts | 20 +++---- .../tests}/test-cases/encoding-test-cases.ts | 2 +- .../test-cases/html-handling-test-cases.ts | 2 +- .../tests/test-cases/test-case.ts | 7 +++ .../asset-worker/tests/tsconfig.json | 2 +- packages/workers-shared/package.json | 7 +-- packages/workers-shared/tsconfig.json | 4 +- 12 files changed, 22 insertions(+), 126 deletions(-) delete mode 100644 fixtures/asset-config/README.md delete mode 100644 fixtures/asset-config/env.d.ts delete mode 100644 fixtures/asset-config/package.json delete mode 100644 fixtures/asset-config/tsconfig.json delete mode 100644 fixtures/asset-config/vitest.config.ts rename {fixtures/asset-config => packages/workers-shared/asset-worker/tests}/html-handling.test.ts (77%) rename {fixtures/asset-config => packages/workers-shared/asset-worker/tests}/test-cases/encoding-test-cases.ts (99%) rename {fixtures/asset-config => packages/workers-shared/asset-worker/tests}/test-cases/html-handling-test-cases.ts (99%) create mode 100644 packages/workers-shared/asset-worker/tests/test-cases/test-case.ts diff --git a/fixtures/asset-config/README.md b/fixtures/asset-config/README.md deleted file mode 100644 index 65429521ac8c..000000000000 --- a/fixtures/asset-config/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# Asset config fixture - -This is testing the asset worker in the workers-shared package. We cannot use the vitest integration directly in that package because it results in circular dependencies. - -Since it runs tests in a workerd context and doesn't depend on the host OS, this fixture only runs test on Linux to reduce wasted time in CI. diff --git a/fixtures/asset-config/env.d.ts b/fixtures/asset-config/env.d.ts deleted file mode 100644 index ec168ef521c9..000000000000 --- a/fixtures/asset-config/env.d.ts +++ /dev/null @@ -1,8 +0,0 @@ -declare module "cloudflare:test" { - // Controls the type of `import("cloudflare:test").env` - interface ProvidedEnv { - CONFIG: Record; - ASSETS_MANIFEST: ArrayBuffer; - ASSETS_KV_NAMESPACE: KVNamespace; - } -} diff --git a/fixtures/asset-config/package.json b/fixtures/asset-config/package.json deleted file mode 100644 index b38045d674eb..000000000000 --- a/fixtures/asset-config/package.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "name": "workers-assets-config-test", - "private": true, - "scripts": { - "dev": "npx wrangler dev", - "test:ci": "run-script-os", - "test:ci:default": "true", - "test:ci:nix": "vitest run", - "test:watch": "vitest", - "type:tests": "tsc -p ./tsconfig.json" - }, - "devDependencies": { - "@cloudflare/vitest-pool-workers": "workspace:*", - "@cloudflare/workers-tsconfig": "workspace:*", - "@cloudflare/workers-types": "^4.20241106.0", - "run-script-os": "^1.1.6", - "undici": "catalog:default", - "vitest": "catalog:default", - "wrangler": "workspace:*" - }, - "volta": { - "extends": "../../package.json" - } -} diff --git a/fixtures/asset-config/tsconfig.json b/fixtures/asset-config/tsconfig.json deleted file mode 100644 index 48f509607b68..000000000000 --- a/fixtures/asset-config/tsconfig.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "compilerOptions": { - "target": "ES2020", - "module": "ES2020", - "lib": ["ES2020"], - "types": [ - "@cloudflare/workers-types/experimental", - "@cloudflare/vitest-pool-workers" - ], - "moduleResolution": "bundler", - "noEmit": true, - "skipLibCheck": true - }, - "include": ["**/*.ts"] -} diff --git a/fixtures/asset-config/vitest.config.ts b/fixtures/asset-config/vitest.config.ts deleted file mode 100644 index 34e3cac98de2..000000000000 --- a/fixtures/asset-config/vitest.config.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; - -class FilteredPushArray extends Array { - constructor(private readonly predicate: (item: T) => boolean) { - super(); - } - - push(...items: T[]) { - return super.push(...items.filter(this.predicate)); - } -} - -export default defineWorkersConfig({ - test: { - chaiConfig: { - truncateThreshold: 80, - }, - poolOptions: { - workers: { - wrangler: { - configPath: - "../../packages/workers-shared/asset-worker/wrangler.toml", - }, - }, - }, - // Configure the `vite-node` server used by Vitest code to import configs, - // custom pools and tests. By default, Vitest effectively applies Vite - // transforms to all files outside `node_modules`. This means by default, - // our custom pool code is transformed by Vite during development, but not - // when published, leading to possible behaviour mismatches. To fix this, - // we ensure file paths containing `packages/vitest-pool-workers/dist` are - // always "externalised", meaning they're imported directly by Node. - server: { - deps: { - // Vitest automatically adds `/^(?!.*node_modules).*\.mjs$/` as an - // `inline` RegExp: https://github.com/vitest-dev/vitest/blob/v2.1.1/packages/vitest/src/constants.ts#L9 - // We'd like `packages/vitest-pool-workers/dist/pool/index.mjs` to be - // externalised though. Unfortunately, `inline`s are checked before - // `external`s, so there's no nice way we can override this. Instead, - // we prevent the extra `inline` being added in the first place. - inline: new FilteredPushArray((item) => { - const str = `${item}`; - return str !== "/^(?!.*node_modules).*\\.mjs$/"; - }), - external: [ - /packages\/vitest-pool-workers\/dist/, - /packages\/wrangler\//, - ], - }, - }, - }, -}); diff --git a/fixtures/asset-config/html-handling.test.ts b/packages/workers-shared/asset-worker/tests/html-handling.test.ts similarity index 77% rename from fixtures/asset-config/html-handling.test.ts rename to packages/workers-shared/asset-worker/tests/html-handling.test.ts index e024bfd956cc..3ec0b9690476 100644 --- a/fixtures/asset-config/html-handling.test.ts +++ b/packages/workers-shared/asset-worker/tests/html-handling.test.ts @@ -1,16 +1,16 @@ import { SELF } from "cloudflare:test"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { applyConfigurationDefaults } from "../../packages/workers-shared/asset-worker/src/configuration"; -import Worker from "../../packages/workers-shared/asset-worker/src/index"; -import { getAssetWithMetadataFromKV } from "../../packages/workers-shared/asset-worker/src/utils/kv"; +import { applyConfigurationDefaults } from "../src/configuration"; +import Worker from "../src/index"; +import { getAssetWithMetadataFromKV } from "../src/utils/kv"; import { encodingTestCases } from "./test-cases/encoding-test-cases"; import { htmlHandlingTestCases } from "./test-cases/html-handling-test-cases"; -import type { AssetMetadata } from "../../packages/workers-shared/asset-worker/src/utils/kv"; +import type { AssetMetadata } from "../src/utils/kv"; const IncomingRequest = Request; -vi.mock("../../packages/workers-shared/asset-worker/src/utils/kv.ts"); -vi.mock("../../packages/workers-shared/asset-worker/src/configuration"); +vi.mock("../src/utils/kv.ts"); +vi.mock("../src/configuration"); const existsMock = (fileList: Set) => { vi.spyOn(Worker.prototype, "unstable_exists").mockImplementation( async (pathname: string) => { @@ -22,14 +22,6 @@ const existsMock = (fileList: Set) => { }; const BASE_URL = "http://example.com"; -export type TestCase = { - title: string; - files: string[]; - requestPath: string; - matchedFile?: string; - finalPath?: string; -}; - const testSuites = [ { title: "htmlHanding options", diff --git a/fixtures/asset-config/test-cases/encoding-test-cases.ts b/packages/workers-shared/asset-worker/tests/test-cases/encoding-test-cases.ts similarity index 99% rename from fixtures/asset-config/test-cases/encoding-test-cases.ts rename to packages/workers-shared/asset-worker/tests/test-cases/encoding-test-cases.ts index 0da5719f1947..6cf6a23d5053 100644 --- a/fixtures/asset-config/test-cases/encoding-test-cases.ts +++ b/packages/workers-shared/asset-worker/tests/test-cases/encoding-test-cases.ts @@ -1,4 +1,4 @@ -import { TestCase } from "../html-handling.test"; +import { TestCase } from "./test-case"; export const encodingTestCases: { html_handling: diff --git a/fixtures/asset-config/test-cases/html-handling-test-cases.ts b/packages/workers-shared/asset-worker/tests/test-cases/html-handling-test-cases.ts similarity index 99% rename from fixtures/asset-config/test-cases/html-handling-test-cases.ts rename to packages/workers-shared/asset-worker/tests/test-cases/html-handling-test-cases.ts index 8c65ebac26aa..14ec704857a5 100644 --- a/fixtures/asset-config/test-cases/html-handling-test-cases.ts +++ b/packages/workers-shared/asset-worker/tests/test-cases/html-handling-test-cases.ts @@ -1,4 +1,4 @@ -import { TestCase } from "../html-handling.test"; +import { TestCase } from "./test-case"; export const htmlHandlingTestCases: { html_handling: diff --git a/packages/workers-shared/asset-worker/tests/test-cases/test-case.ts b/packages/workers-shared/asset-worker/tests/test-cases/test-case.ts new file mode 100644 index 000000000000..d1db2be99d9a --- /dev/null +++ b/packages/workers-shared/asset-worker/tests/test-cases/test-case.ts @@ -0,0 +1,7 @@ +export type TestCase = { + title: string; + files: string[]; + requestPath: string; + matchedFile?: string; + finalPath?: string; +}; diff --git a/packages/workers-shared/asset-worker/tests/tsconfig.json b/packages/workers-shared/asset-worker/tests/tsconfig.json index 0869caf63444..43ca30566dcd 100644 --- a/packages/workers-shared/asset-worker/tests/tsconfig.json +++ b/packages/workers-shared/asset-worker/tests/tsconfig.json @@ -6,5 +6,5 @@ "@cloudflare/vitest-pool-workers" ] }, - "include": ["**/*.test.ts"] + "include": ["**/*.ts"] } diff --git a/packages/workers-shared/package.json b/packages/workers-shared/package.json index 23431237e587..dafd66c46833 100644 --- a/packages/workers-shared/package.json +++ b/packages/workers-shared/package.json @@ -29,16 +29,17 @@ "bundle:router-worker": "esbuild router-worker/src/index.ts --format=esm --bundle --outfile=dist/router-worker.mjs --sourcemap=external", "bundle:router-worker:prod": "pnpm run bundle:router-worker --minify", "check:lint": "eslint . --max-warnings=0", - "check:type": "tsc -p ./tsconfig.json && tsc -p ./tsconfig.node.json", + "check:type": "pnpm run check:type:package && pnpm run check:type:asset-worker && pnpm run check:type:router-worker", "check:type:asset-worker": "tsc -p ./asset-worker/tsconfig.json && tsc -p ./asset-worker/tests/tsconfig.json", + "check:type:package": "tsc -p ./tsconfig.json && tsc -p ./tsconfig.node.json", "check:type:router-worker": "tsc -p ./router-worker/tsconfig.json && tsc -p ./router-worker/tests/tsconfig.json", "clean": "rimraf dist", "deploy": "pnpm run deploy:router-worker && pnpm run deploy:asset-worker", "deploy:asset-worker": "CLOUDFLARE_API_TOKEN=$WORKERS_DEPLOY_AND_CONFIG_CLOUDFLARE_API_TOKEN pnpx wrangler versions upload --experimental-versions -c asset-worker/wrangler.toml", "deploy:router-worker": "CLOUDFLARE_API_TOKEN=$WORKERS_DEPLOY_AND_CONFIG_CLOUDFLARE_API_TOKEN pnpx wrangler versions upload --experimental-versions -c router-worker/wrangler.toml", "dev": "pnpm run clean && concurrently -n bundle:asset-worker,bundle:router-worker -c blue,magenta \"pnpm run bundle:asset-worker --watch\" \"pnpm run bundle:router-worker --watch\"", - "test": "vitest", - "test:ci": "vitest run", + "test": "vitest -c asset-worker/vitest.config.mts", + "test:ci": "vitest run -c asset-worker/vitest.config.mts", "types:emit": "tsc index.ts --declaration --emitDeclarationOnly --declarationDir ./dist" }, "dependencies": { diff --git a/packages/workers-shared/tsconfig.json b/packages/workers-shared/tsconfig.json index a6923e31b8f1..cd731beefbe6 100644 --- a/packages/workers-shared/tsconfig.json +++ b/packages/workers-shared/tsconfig.json @@ -4,7 +4,7 @@ "lib": ["es2021"], "module": "es2022", "moduleResolution": "Bundler", - "types": ["@cloudflare/workers-types/experimental"], + "types": ["@cloudflare/workers-types/experimental", "@types/node"], "noEmit": true, "isolatedModules": true, "allowSyntheticDefaultImports": true, @@ -12,6 +12,6 @@ "strict": true, "skipLibCheck": true }, - "include": ["**/*.ts"], + "include": ["utils/*.ts"], "exclude": ["scripts/**"] } From ba2b58eddfc7a9bc0ee008142a9e3fa1851f6b9b Mon Sep 17 00:00:00 2001 From: Samuel Macleod Date: Wed, 20 Nov 2024 19:37:02 +0000 Subject: [PATCH 5/5] Fix check --- .../workers-shared/asset-worker/tests/html-handling.test.ts | 5 +++-- .../asset-worker/tests/test-cases/encoding-test-cases.ts | 2 +- .../tests/test-cases/html-handling-test-cases.ts | 2 +- packages/workers-shared/package.json | 2 +- packages/workers-shared/tsconfig.json | 2 +- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/workers-shared/asset-worker/tests/html-handling.test.ts b/packages/workers-shared/asset-worker/tests/html-handling.test.ts index 3ec0b9690476..ef9f6bb995db 100644 --- a/packages/workers-shared/asset-worker/tests/html-handling.test.ts +++ b/packages/workers-shared/asset-worker/tests/html-handling.test.ts @@ -17,6 +17,7 @@ const existsMock = (fileList: Set) => { if (fileList.has(pathname)) { return pathname; } + return null; } ); }; @@ -33,7 +34,7 @@ const testSuites = [ }, ]; -describe.each(testSuites)("$title", ({ title, suite }) => { +describe.each(testSuites)("$title", ({ suite }) => { beforeEach(() => { vi.mocked(getAssetWithMetadataFromKV).mockImplementation( () => @@ -64,7 +65,7 @@ describe.each(testSuites)("$title", ({ title, suite }) => { async ({ files, requestPath, matchedFile, finalPath }) => { existsMock(new Set(files)); const request = new IncomingRequest(BASE_URL + requestPath); - let response = await SELF.fetch(request); + const response = await SELF.fetch(request); if (matchedFile && finalPath) { expect(getAssetWithMetadataFromKV).toBeCalledTimes(1); expect(getAssetWithMetadataFromKV).toBeCalledWith( diff --git a/packages/workers-shared/asset-worker/tests/test-cases/encoding-test-cases.ts b/packages/workers-shared/asset-worker/tests/test-cases/encoding-test-cases.ts index 6cf6a23d5053..34436418b2f0 100644 --- a/packages/workers-shared/asset-worker/tests/test-cases/encoding-test-cases.ts +++ b/packages/workers-shared/asset-worker/tests/test-cases/encoding-test-cases.ts @@ -1,4 +1,4 @@ -import { TestCase } from "./test-case"; +import type { TestCase } from "./test-case"; export const encodingTestCases: { html_handling: diff --git a/packages/workers-shared/asset-worker/tests/test-cases/html-handling-test-cases.ts b/packages/workers-shared/asset-worker/tests/test-cases/html-handling-test-cases.ts index 14ec704857a5..e675d9e5e304 100644 --- a/packages/workers-shared/asset-worker/tests/test-cases/html-handling-test-cases.ts +++ b/packages/workers-shared/asset-worker/tests/test-cases/html-handling-test-cases.ts @@ -1,4 +1,4 @@ -import { TestCase } from "./test-case"; +import type { TestCase } from "./test-case"; export const htmlHandlingTestCases: { html_handling: diff --git a/packages/workers-shared/package.json b/packages/workers-shared/package.json index dafd66c46833..a33971a07130 100644 --- a/packages/workers-shared/package.json +++ b/packages/workers-shared/package.json @@ -32,7 +32,7 @@ "check:type": "pnpm run check:type:package && pnpm run check:type:asset-worker && pnpm run check:type:router-worker", "check:type:asset-worker": "tsc -p ./asset-worker/tsconfig.json && tsc -p ./asset-worker/tests/tsconfig.json", "check:type:package": "tsc -p ./tsconfig.json && tsc -p ./tsconfig.node.json", - "check:type:router-worker": "tsc -p ./router-worker/tsconfig.json && tsc -p ./router-worker/tests/tsconfig.json", + "check:type:router-worker": "tsc -p ./router-worker/tsconfig.json", "clean": "rimraf dist", "deploy": "pnpm run deploy:router-worker && pnpm run deploy:asset-worker", "deploy:asset-worker": "CLOUDFLARE_API_TOKEN=$WORKERS_DEPLOY_AND_CONFIG_CLOUDFLARE_API_TOKEN pnpx wrangler versions upload --experimental-versions -c asset-worker/wrangler.toml", diff --git a/packages/workers-shared/tsconfig.json b/packages/workers-shared/tsconfig.json index cd731beefbe6..609d161c8048 100644 --- a/packages/workers-shared/tsconfig.json +++ b/packages/workers-shared/tsconfig.json @@ -12,6 +12,6 @@ "strict": true, "skipLibCheck": true }, - "include": ["utils/*.ts"], + "include": ["utils/*.ts", "index.ts"], "exclude": ["scripts/**"] }