diff --git a/.changeset/funny-dingos-guess.md b/.changeset/funny-dingos-guess.md deleted file mode 100644 index aaed92cb88dd..000000000000 --- a/.changeset/funny-dingos-guess.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@cloudflare/workers-shared": minor ---- - -Option to invoke user worker ahead of assets diff --git a/packages/workers-shared/asset-worker/wrangler.toml b/packages/workers-shared/asset-worker/wrangler.toml index b5bc77b47f80..20e850f2c74c 100644 --- a/packages/workers-shared/asset-worker/wrangler.toml +++ b/packages/workers-shared/asset-worker/wrangler.toml @@ -12,7 +12,6 @@ account_id = "0f1b8aa119a907021f659042f95ea9ba" workers_dev = false main = "src/index.ts" compatibility_date = "2024-07-31" -# nodejs_compat required when using @cloudflare/vitest-pool-workers compatibility_flags = ["nodejs_compat"] [[unsafe.bindings]] diff --git a/packages/workers-shared/package.json b/packages/workers-shared/package.json index 0644c25fe7ea..281512040e2a 100644 --- a/packages/workers-shared/package.json +++ b/packages/workers-shared/package.json @@ -30,16 +30,14 @@ "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 && tsc -p ./router-worker/tests/tsconfig.json", + "check:type:tests": "tsc -p ./asset-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": "concurrently --group -n router-worker,asset-worker \"pnpm run test:router-worker\" \"pnpm run test:asset-worker\"", - "test:asset-worker": "vitest -c asset-worker/vitest.config.mts --dir asset-worker", - "test:ci": "pnpm run test", - "test:router-worker": "vitest -c router-worker/vitest.config.mts --dir router-worker", + "test": "vitest", + "test:ci": "vitest run", "types:emit": "tsc index.ts --declaration --emitDeclarationOnly --declarationDir ./dist" }, "dependencies": { @@ -48,7 +46,6 @@ }, "devDependencies": { "@cloudflare/eslint-config-worker": "workspace:*", - "@cloudflare/vitest-pool-workers": "latest", "@cloudflare/workers-tsconfig": "workspace:*", "@cloudflare/workers-types": "^4.20241106.0", "@types/mime": "^3.0.4", diff --git a/packages/workers-shared/router-worker/src/index.ts b/packages/workers-shared/router-worker/src/index.ts index ccb412adbb0a..9528a7dc7c7e 100644 --- a/packages/workers-shared/router-worker/src/index.ts +++ b/packages/workers-shared/router-worker/src/index.ts @@ -56,19 +56,6 @@ export default { } const maybeSecondRequest = request.clone(); - - // User's configuration indicates they want user-Worker to run ahead of any - // assets. Do not provide any fallback logic. - if (env.CONFIG.invoke_user_worker_ahead_of_assets) { - if (!env.CONFIG.has_user_worker) { - throw new Error( - "Fetch for user worker without having a user worker binding" - ); - } - return env.USER_WORKER.fetch(maybeSecondRequest); - } - - // Otherwise, we try to first fetch assets, falling back to user-Worker. if (env.CONFIG.has_user_worker) { if (await env.ASSET_WORKER.unstable_canFetch(request)) { analytics.setData({ dispatchtype: DISPATCH_TYPE.ASSETS }); diff --git a/packages/workers-shared/router-worker/tests/index.test.ts b/packages/workers-shared/router-worker/tests/index.test.ts deleted file mode 100644 index fbd7f78abdcf..000000000000 --- a/packages/workers-shared/router-worker/tests/index.test.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { createExecutionContext } from "cloudflare:test"; -import { describe, expect, it } from "vitest"; -import { default as worker } from "../src/index"; - -describe("unit tests", async () => { - it("fails if specify running user worker ahead of assets, without user worker", async () => { - const request = new Request("https://example.com"); - const ctx = createExecutionContext(); - - const env = { - CONFIG: { - invoke_user_worker_ahead_of_assets: true, - has_user_worker: false, - }, - } as typeof env; - - void expect( - async () => await worker.fetch(request, env, ctx) - ).rejects.toThrowError( - "Fetch for user worker without having a user worker binding" - ); - }); - - it("it returns fetch from user worker when invoke_user_worker_ahead_of_assets true", async () => { - const request = new Request("https://example.com"); - const ctx = createExecutionContext(); - - const env = { - CONFIG: { - invoke_user_worker_ahead_of_assets: true, - has_user_worker: true, - }, - USER_WORKER: { - async fetch(_: Request): Promise { - return new Response("hello from user worker"); - }, - }, - ASSET_WORKER: { - async fetch(_: Request): Promise { - return new Response("hello from asset worker"); - }, - async unstable_canFetch(_: Request): Promise { - return true; - }, - }, - } as typeof env; - - const response = await worker.fetch(request, env, ctx); - expect(await response.text()).toEqual("hello from user worker"); - }); - - it("it returns fetch from asset worker when matching existing asset path", async () => { - const request = new Request("https://example.com"); - const ctx = createExecutionContext(); - - const env = { - CONFIG: { - invoke_user_worker_ahead_of_assets: false, - has_user_worker: false, - }, - ASSET_WORKER: { - async fetch(_: Request): Promise { - return new Response("hello from asset worker"); - }, - async unstable_canFetch(_: Request): Promise { - return true; - }, - }, - } as typeof env; - - const response = await worker.fetch(request, env, ctx); - expect(await response.text()).toEqual("hello from asset worker"); - }); -}); diff --git a/packages/workers-shared/router-worker/tests/tsconfig.json b/packages/workers-shared/router-worker/tests/tsconfig.json deleted file mode 100644 index 48f509607b68..000000000000 --- a/packages/workers-shared/router-worker/tests/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/packages/workers-shared/router-worker/vitest.config.mts b/packages/workers-shared/router-worker/vitest.config.mts deleted file mode 100644 index 4f94cd4886ed..000000000000 --- a/packages/workers-shared/router-worker/vitest.config.mts +++ /dev/null @@ -1,13 +0,0 @@ -import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config"; - -export default defineWorkersConfig({ - test: { - poolOptions: { - workers: { - wrangler: { - configPath: "./wrangler.toml", - }, - }, - }, - }, -}); diff --git a/packages/workers-shared/router-worker/wrangler.toml b/packages/workers-shared/router-worker/wrangler.toml index cbdc31ae418b..d96e0418f1e0 100644 --- a/packages/workers-shared/router-worker/wrangler.toml +++ b/packages/workers-shared/router-worker/wrangler.toml @@ -12,8 +12,6 @@ account_id = "0f1b8aa119a907021f659042f95ea9ba" workers_dev = false main = "src/index.ts" compatibility_date = "2024-07-31" -# nodejs_compat required when using @cloudflare/vitest-pool-workers -compatibility_flags = ["nodejs_compat", "no_nodejs_compat_v2"] [version_metadata] binding = "VERSION_METADATA" diff --git a/packages/workers-shared/tsconfig.json b/packages/workers-shared/tsconfig.json index 9ee2befe1743..e112be387120 100644 --- a/packages/workers-shared/tsconfig.json +++ b/packages/workers-shared/tsconfig.json @@ -5,7 +5,7 @@ "sourceMap": true, "forceConsistentCasingInFileNames": true, "useUnknownInCatchVariables": false, - "types": ["@cloudflare/workers-types/experimental", "@types/node"] + "types": ["@cloudflare/workers-types/experimental"] }, "include": ["**/*.ts", "vitest.config.mts"], "exclude": ["node_modules", "dist", "**/tests", "**/*.test.ts"] diff --git a/packages/workers-shared/utils/types.ts b/packages/workers-shared/utils/types.ts index eab59ca25767..569cad5dbcab 100644 --- a/packages/workers-shared/utils/types.ts +++ b/packages/workers-shared/utils/types.ts @@ -2,7 +2,6 @@ import { z } from "zod"; export const RoutingConfigSchema = z.object({ has_user_worker: z.boolean().optional(), - invoke_user_worker_ahead_of_assets: z.boolean().optional(), }); export const AssetConfigSchema = z.object({ diff --git a/packages/workers-shared/asset-worker/vitest.config.mts b/packages/workers-shared/vitest.config.mts similarity index 60% rename from packages/workers-shared/asset-worker/vitest.config.mts rename to packages/workers-shared/vitest.config.mts index 19d3d456b85a..363c185f0a91 100644 --- a/packages/workers-shared/asset-worker/vitest.config.mts +++ b/packages/workers-shared/vitest.config.mts @@ -1,11 +1,11 @@ import { defineProject, mergeConfig } from "vitest/config"; -import configShared from "../../../vitest.shared.js"; +import configShared from "../../vitest.shared"; export default mergeConfig( configShared, defineProject({ test: { - include: ["tests/**.{test,spec}.{ts,js}"], + include: ["asset-worker/tests/**.{test,spec}.{ts,js}"], globals: true, }, }) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4b4d655d7ea5..36e5c6715343 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1588,9 +1588,6 @@ importers: '@cloudflare/eslint-config-worker': specifier: workspace:* version: link:../eslint-config-worker - '@cloudflare/vitest-pool-workers': - specifier: latest - 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 @@ -2429,10 +2426,6 @@ 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: @@ -2472,13 +2465,6 @@ 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'} @@ -2509,10 +2495,6 @@ 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==} @@ -6334,11 +6316,6 @@ 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} @@ -8352,16 +8329,6 @@ 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'} @@ -9229,10 +9196,6 @@ 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) @@ -9297,25 +9260,6 @@ 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 @@ -9331,11 +9275,6 @@ 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': @@ -13483,25 +13422,6 @@ 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 @@ -15623,35 +15543,6 @@ 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