@depup/wrangler 4.75.0-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +33 -0
  2. package/bin/wrangler.js +93 -0
  3. package/changes.json +18 -0
  4. package/config-schema.json +3222 -0
  5. package/kv-asset-handler.js +1 -0
  6. package/package.json +221 -0
  7. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  8. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  9. package/templates/__tests__/tsconfig.json +8 -0
  10. package/templates/checked-fetch.js +28 -0
  11. package/templates/facade.d.ts +19 -0
  12. package/templates/middleware/common.ts +67 -0
  13. package/templates/middleware/loader-modules.ts +134 -0
  14. package/templates/middleware/loader-sw.ts +229 -0
  15. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  16. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  17. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  18. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  19. package/templates/middleware/middleware-pretty-error.ts +40 -0
  20. package/templates/middleware/middleware-scheduled.ts +29 -0
  21. package/templates/modules-watch-stub.js +4 -0
  22. package/templates/new-worker-scheduled.js +17 -0
  23. package/templates/new-worker-scheduled.ts +32 -0
  24. package/templates/new-worker.js +15 -0
  25. package/templates/new-worker.ts +33 -0
  26. package/templates/no-op-worker.js +10 -0
  27. package/templates/pages-dev-pipeline.ts +33 -0
  28. package/templates/pages-dev-util.ts +55 -0
  29. package/templates/pages-shim.ts +9 -0
  30. package/templates/pages-template-plugin.ts +190 -0
  31. package/templates/pages-template-worker.ts +198 -0
  32. package/templates/remoteBindings/ProxyServerWorker.ts +143 -0
  33. package/templates/remoteBindings/wrangler.jsonc +4 -0
  34. package/templates/startDevWorker/InspectorProxyWorker.ts +699 -0
  35. package/templates/startDevWorker/ProxyWorker.ts +340 -0
  36. package/templates/tsconfig-sanity.ts +11 -0
  37. package/templates/tsconfig.init.json +22 -0
  38. package/templates/tsconfig.json +14 -0
  39. package/wrangler-dist/InspectorProxyWorker.js +486 -0
  40. package/wrangler-dist/ProxyServerWorker.js +3314 -0
  41. package/wrangler-dist/ProxyWorker.js +238 -0
  42. package/wrangler-dist/cli.d.ts +3154 -0
  43. package/wrangler-dist/cli.js +303399 -0
  44. package/wrangler-dist/metafile-cjs.json +1 -0
@@ -0,0 +1 @@
1
+ export * from "@cloudflare/kv-asset-handler";
package/package.json ADDED
@@ -0,0 +1,221 @@
1
+ {
2
+ "name": "@depup/wrangler",
3
+ "version": "4.75.0-depup.0",
4
+ "description": "[DepUp] Command-line interface for all things Cloudflare Workers",
5
+ "keywords": [
6
+ "depup",
7
+ "dependency-bumped",
8
+ "updated-deps",
9
+ "wrangler",
10
+ "cloudflare",
11
+ "workers",
12
+ "cloudflare workers",
13
+ "edge",
14
+ "compute",
15
+ "serverless",
16
+ "serverless application",
17
+ "serverless module",
18
+ "wasm",
19
+ "web",
20
+ "assembly",
21
+ "webassembly",
22
+ "rust",
23
+ "emscripten",
24
+ "typescript",
25
+ "graphql",
26
+ "router",
27
+ "http",
28
+ "cli"
29
+ ],
30
+ "homepage": "https://github.com/cloudflare/workers-sdk#readme",
31
+ "bugs": {
32
+ "url": "https://github.com/cloudflare/workers-sdk/issues"
33
+ },
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/cloudflare/workers-sdk.git",
37
+ "directory": "packages/wrangler"
38
+ },
39
+ "license": "MIT OR Apache-2.0",
40
+ "author": "wrangler@cloudflare.com",
41
+ "main": "wrangler-dist/cli.js",
42
+ "types": "wrangler-dist/cli.d.ts",
43
+ "bin": {
44
+ "wrangler": "./bin/wrangler.js",
45
+ "wrangler2": "./bin/wrangler.js"
46
+ },
47
+ "files": [
48
+ "bin",
49
+ "miniflare-dist",
50
+ "wrangler-dist",
51
+ "templates",
52
+ "kv-asset-handler.js",
53
+ "config-schema.json",
54
+ "changes.json",
55
+ "README.md"
56
+ ],
57
+ "dependencies": {
58
+ "blake3-wasm": "^3.0.0",
59
+ "esbuild": "^0.27.4",
60
+ "path-to-regexp": "^8.3.0",
61
+ "unenv": "2.0.0-rc.24",
62
+ "workerd": "1.20260317.1",
63
+ "@cloudflare/kv-asset-handler": "0.4.2",
64
+ "@cloudflare/unenv-preset": "2.15.0",
65
+ "miniflare": "4.20260317.0"
66
+ },
67
+ "devDependencies": {
68
+ "@aws-sdk/client-s3": "^3.721.0",
69
+ "@bomb.sh/tab": "^0.0.12",
70
+ "@cloudflare/types": "6.18.4",
71
+ "@cloudflare/workers-types": "^4.20260317.1",
72
+ "@cspotcode/source-map-support": "0.8.1",
73
+ "@netlify/build-info": "^10.2.0",
74
+ "@sentry/node": "^7.86.0",
75
+ "@sentry/types": "^7.86.0",
76
+ "@sentry/utils": "^7.86.0",
77
+ "@types/command-exists": "^1.2.0",
78
+ "@types/cross-spawn": "^6.0.2",
79
+ "@types/esprima": "^4.0.3",
80
+ "@types/glob-to-regexp": "^0.4.1",
81
+ "@types/javascript-time-ago": "^2.0.3",
82
+ "@types/json-diff": "^1.0.3",
83
+ "@types/mime": "^3.0.4",
84
+ "@types/minimatch": "^5.1.2",
85
+ "@types/node": "^20.19.9",
86
+ "@types/node-forge": "^1.3.11",
87
+ "@types/prompts": "^2.0.14",
88
+ "@types/resolve": "^1.20.6",
89
+ "@types/shell-quote": "^1.7.2",
90
+ "@types/signal-exit": "^3.0.1",
91
+ "@types/supports-color": "^8.1.1",
92
+ "@types/ws": "^8.5.7",
93
+ "@types/yargs": "^17.0.22",
94
+ "@vitest/ui": "3.2.4",
95
+ "@webcontainer/env": "^1.1.0",
96
+ "am-i-vibing": "^0.1.0",
97
+ "capnweb": "^0.5.0",
98
+ "chalk": "^5.2.0",
99
+ "chokidar": "^4.0.1",
100
+ "ci-info": "^4.4.0",
101
+ "cli-table3": "^0.6.3",
102
+ "cloudflare": "^5.2.0",
103
+ "cmd-shim": "^4.1.0",
104
+ "command-exists": "^1.2.9",
105
+ "concurrently": "^8.2.2",
106
+ "cross-spawn": "^7.0.3",
107
+ "date-fns": "^4.1.0",
108
+ "devtools-protocol": "^0.0.1182435",
109
+ "dotenv": "^16.3.1",
110
+ "dotenv-expand": "^12.0.2",
111
+ "empathic": "^2.0.0",
112
+ "eslint": "^9.39.1",
113
+ "esprima": "4.0.1",
114
+ "execa": "^6.1.0",
115
+ "get-port": "^7.0.0",
116
+ "glob-to-regexp": "^0.4.1",
117
+ "https-proxy-agent": "7.0.2",
118
+ "itty-time": "^1.0.6",
119
+ "javascript-time-ago": "^2.5.4",
120
+ "json-diff": "^1.0.6",
121
+ "jsonc-parser": "^3.2.0",
122
+ "md5-file": "5.0.0",
123
+ "mime": "^3.0.0",
124
+ "minimatch": "^5.1.0",
125
+ "mock-socket": "^9.3.1",
126
+ "msw": "2.12.4",
127
+ "node-forge": "^1.3.2",
128
+ "open": "^8.4.0",
129
+ "p-queue": "^9.0.0",
130
+ "patch-console": "^1.0.0",
131
+ "pretty-bytes": "^6.0.0",
132
+ "prompts": "^2.4.2",
133
+ "recast": "0.23.11",
134
+ "resolve": "^1.22.8",
135
+ "semiver": "^1.1.0",
136
+ "shell-quote": "^1.8.1",
137
+ "signal-exit": "^3.0.7",
138
+ "smol-toml": "^1.5.2",
139
+ "source-map": "^0.6.1",
140
+ "supports-color": "^9.2.2",
141
+ "timeago.js": "^4.0.2",
142
+ "tree-kill": "^1.2.2",
143
+ "ts-dedent": "^2.2.0",
144
+ "ts-json-schema-generator": "^1.5.0",
145
+ "tsup": "8.3.0",
146
+ "typescript": "~5.8.3",
147
+ "undici": "7.24.4",
148
+ "update-check": "^1.5.4",
149
+ "vitest": "3.2.4",
150
+ "vitest-websocket-mock": "^0.4.0",
151
+ "ws": "8.18.0",
152
+ "xxhash-wasm": "^1.0.1",
153
+ "yaml": "^2.8.1",
154
+ "yargs": "^17.7.2",
155
+ "@cloudflare/cli": "1.2.1",
156
+ "@cloudflare/containers-shared": "0.12.0",
157
+ "@cloudflare/eslint-config-shared": "1.2.1",
158
+ "@cloudflare/workers-shared": "0.19.1",
159
+ "@cloudflare/pages-shared": "^0.13.116",
160
+ "@cloudflare/workers-tsconfig": "0.0.0",
161
+ "@cloudflare/workers-utils": "0.12.0",
162
+ "@cloudflare/workflows-shared": "0.6.0"
163
+ },
164
+ "peerDependencies": {
165
+ "@cloudflare/workers-types": "^4.20260317.1"
166
+ },
167
+ "peerDependenciesMeta": {
168
+ "@cloudflare/workers-types": {
169
+ "optional": true
170
+ }
171
+ },
172
+ "optionalDependencies": {
173
+ "fsevents": "~2.3.2"
174
+ },
175
+ "engines": {
176
+ "node": ">=20.0.0"
177
+ },
178
+ "volta": {
179
+ "extends": "../../package.json"
180
+ },
181
+ "workers-sdk": {
182
+ "prerelease": true
183
+ },
184
+ "scripts": {
185
+ "assert-git-version": "node -r esbuild-register scripts/assert-git-version.ts",
186
+ "build": "pnpm run clean && pnpm tsup && pnpm run generate-json-schema",
187
+ "check:lint": "eslint . --max-warnings=0 --cache",
188
+ "check:type": "tsc -p ./tsconfig.json && tsc -p ./templates/tsconfig.json",
189
+ "clean": "node -r esbuild-register ../../tools/clean/clean.ts wrangler-dist miniflare-dist emitted-types",
190
+ "dev": "pnpm run clean && concurrently -c black,blue --kill-others-on-fail false \"pnpm tsup --watch src --watch ../containers-shared/src --watch ../cli\" \"pnpm run check:type --watch --preserveWatchOutput\"",
191
+ "generate-json-schema": "node -r esbuild-register scripts/generate-json-schema.ts",
192
+ "start": "pnpm run build && cross-env NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
193
+ "test": "dotenv -- pnpm run assert-git-version && cross-env NODE_OPTIONS=\"$(node scripts/node-options-for-tests.js)\" dotenv -- vitest",
194
+ "test:ci": "cross-env NODE_OPTIONS=\"$(node scripts/node-options-for-tests.js)\" pnpm run test run",
195
+ "test:debug": "pnpm run test --silent=false --verbose=true",
196
+ "test:e2e": "dotenv -- vitest run -c ./e2e/vitest.config.mts",
197
+ "test:watch": "pnpm run test --testTimeout=50000 --watch",
198
+ "type:tests": "tsc -p ./src/__tests__/tsconfig.json && tsc -p ./e2e/tsconfig.json"
199
+ },
200
+ "depup": {
201
+ "changes": {
202
+ "blake3-wasm": {
203
+ "from": "2.1.5",
204
+ "to": "^3.0.0"
205
+ },
206
+ "esbuild": {
207
+ "from": "0.27.3",
208
+ "to": "^0.27.4"
209
+ },
210
+ "path-to-regexp": {
211
+ "from": "6.3.0",
212
+ "to": "^8.3.0"
213
+ }
214
+ },
215
+ "depsUpdated": 3,
216
+ "originalPackage": "wrangler",
217
+ "originalVersion": "4.75.0",
218
+ "processedAt": "2026-03-17T19:31:03.349Z",
219
+ "smokeTest": "failed"
220
+ }
221
+ }
@@ -0,0 +1,128 @@
1
+ import { isRoutingRuleMatch } from "../pages-dev-util";
2
+
3
+ describe("isRoutingRuleMatch", () => {
4
+ it("should match rules referencing root level correctly", () => {
5
+ const routingRule = "/";
6
+
7
+ expect(isRoutingRuleMatch("/", routingRule)).toBeTruthy();
8
+ expect(isRoutingRuleMatch("/foo", routingRule)).toBeFalsy();
9
+ expect(isRoutingRuleMatch("/foo/", routingRule)).toBeFalsy();
10
+ expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeFalsy();
11
+ });
12
+
13
+ it("should match include-all rules correctly", () => {
14
+ const routingRule = "/*";
15
+
16
+ expect(isRoutingRuleMatch("/", routingRule)).toBeTruthy();
17
+ expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
18
+ expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
19
+ expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
20
+ expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
21
+ expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
22
+ expect(isRoutingRuleMatch("/foo/bar/baz/", routingRule)).toBeTruthy();
23
+ });
24
+
25
+ it("should match `/*` suffix-ed rules correctly", () => {
26
+ let routingRule = "/foo/*";
27
+
28
+ expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
29
+ expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
30
+ expect(isRoutingRuleMatch("/foobar", routingRule)).toBeFalsy();
31
+ expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
32
+ expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
33
+ expect(isRoutingRuleMatch("/bar/foo", routingRule)).toBeFalsy();
34
+ expect(isRoutingRuleMatch("/bar/foo/baz", routingRule)).toBeFalsy();
35
+
36
+ routingRule = "/foo/bar/*";
37
+
38
+ expect(isRoutingRuleMatch("/foo", routingRule)).toBeFalsy();
39
+ expect(isRoutingRuleMatch("/foo/", routingRule)).toBeFalsy();
40
+ expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
41
+ expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
42
+ expect(isRoutingRuleMatch("/foo/barfoo", routingRule)).toBeFalsy();
43
+ expect(isRoutingRuleMatch("baz/foo/bar", routingRule)).toBeFalsy();
44
+ expect(isRoutingRuleMatch("baz/foo/bar/", routingRule)).toBeFalsy();
45
+ });
46
+
47
+ it("should match `/` suffix-ed rules correctly", () => {
48
+ let routingRule = "/foo/";
49
+ expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
50
+ expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
51
+
52
+ routingRule = "/foo/bar/";
53
+ expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
54
+ expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
55
+ });
56
+
57
+ it("should match `*` suffix-ed rules correctly", () => {
58
+ let routingRule = "/foo*";
59
+ expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
60
+ expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
61
+ expect(isRoutingRuleMatch("/foobar", routingRule)).toBeTruthy();
62
+ expect(isRoutingRuleMatch("/barfoo", routingRule)).toBeFalsy();
63
+ expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
64
+ expect(isRoutingRuleMatch("/bar/foo", routingRule)).toBeFalsy();
65
+ expect(isRoutingRuleMatch("/bar/foobar", routingRule)).toBeFalsy();
66
+ expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
67
+ expect(isRoutingRuleMatch("/bar/foo/baz", routingRule)).toBeFalsy();
68
+
69
+ routingRule = "/foo/bar*";
70
+ expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
71
+ expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
72
+ expect(isRoutingRuleMatch("/foo/barfoo", routingRule)).toBeTruthy();
73
+ expect(isRoutingRuleMatch("/bar/foo/barfoo", routingRule)).toBeFalsy();
74
+ expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeTruthy();
75
+ expect(isRoutingRuleMatch("/bar/foo/bar/baz", routingRule)).toBeFalsy();
76
+ });
77
+
78
+ it("should match rules without wildcards correctly", () => {
79
+ let routingRule = "/foo";
80
+
81
+ expect(isRoutingRuleMatch("/foo", routingRule)).toBeTruthy();
82
+ expect(isRoutingRuleMatch("/foo/", routingRule)).toBeTruthy();
83
+ expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeFalsy();
84
+ expect(isRoutingRuleMatch("/bar/foo", routingRule)).toBeFalsy();
85
+
86
+ routingRule = "/foo/bar";
87
+ expect(isRoutingRuleMatch("/foo/bar", routingRule)).toBeTruthy();
88
+ expect(isRoutingRuleMatch("/foo/bar/", routingRule)).toBeTruthy();
89
+ expect(isRoutingRuleMatch("/foo/bar/baz", routingRule)).toBeFalsy();
90
+ expect(isRoutingRuleMatch("/baz/foo/bar", routingRule)).toBeFalsy();
91
+ });
92
+
93
+ it("should throw an error if pathname or routing rule params are missing", () => {
94
+ // MISSING PATHNAME
95
+ expect(() =>
96
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
97
+ // @ts-ignore: sanity check
98
+ isRoutingRuleMatch(undefined, "/*")
99
+ ).toThrow("Pathname is undefined.");
100
+
101
+ expect(() =>
102
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
103
+ // @ts-ignore: sanity check
104
+ isRoutingRuleMatch(null, "/*")
105
+ ).toThrow("Pathname is undefined.");
106
+
107
+ expect(() => isRoutingRuleMatch("", "/*")).toThrow(
108
+ "Pathname is undefined."
109
+ );
110
+
111
+ // MISSING ROUTING RULE
112
+ expect(() =>
113
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
114
+ // @ts-ignore: sanity check
115
+ isRoutingRuleMatch("/foo", undefined)
116
+ ).toThrow("Routing rule is undefined.");
117
+
118
+ expect(() =>
119
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
120
+ // @ts-ignore: sanity check
121
+ isRoutingRuleMatch("/foo", null)
122
+ ).toThrow("Routing rule is undefined.");
123
+
124
+ expect(() => isRoutingRuleMatch("/foo", "")).toThrow(
125
+ "Routing rule is undefined."
126
+ );
127
+ });
128
+ });
@@ -0,0 +1,12 @@
1
+ // `@types/node` should be included
2
+ Buffer.from("test");
3
+
4
+ // `@types/jest` should be included
5
+ test("test");
6
+
7
+ // @ts-expect-error `@cloudflare/workers-types` should NOT be included
8
+ const _handler: ExportedHandler = {};
9
+ // @ts-expect-error `@cloudflare/workers-types` should NOT be included
10
+ new HTMLRewriter();
11
+
12
+ export {};
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "@cloudflare/workers-tsconfig/tsconfig.json",
3
+ "compilerOptions": {
4
+ "types": ["node", "jest"]
5
+ },
6
+ "include": ["**/*.ts"],
7
+ "exclude": []
8
+ }
@@ -0,0 +1,28 @@
1
+ const urls = new Set();
2
+
3
+ function checkURL(request, init) {
4
+ const url =
5
+ request instanceof URL
6
+ ? request
7
+ : new URL(
8
+ (typeof request === "string" ? new Request(request, init) : request)
9
+ .url
10
+ );
11
+ if (url.port && url.port !== "443" && url.protocol === "https:") {
12
+ if (!urls.has(url.toString())) {
13
+ urls.add(url.toString());
14
+ console.warn(
15
+ `WARNING: known issue with \`fetch()\` requests to custom HTTPS ports in published Workers:\n` +
16
+ ` - ${url.toString()} - the custom port will be ignored when the Worker is published using the \`wrangler deploy\` command.\n`
17
+ );
18
+ }
19
+ }
20
+ }
21
+
22
+ globalThis.fetch = new Proxy(globalThis.fetch, {
23
+ apply(target, thisArg, argArray) {
24
+ const [request, init] = argArray;
25
+ checkURL(request, init);
26
+ return Reflect.apply(target, thisArg, argArray);
27
+ },
28
+ });
@@ -0,0 +1,19 @@
1
+ declare module "__ENTRY_POINT__" {
2
+ import { Middleware } from "./middleware/common";
3
+ import { WorkerEntrypoint } from "cloudflare:workers";
4
+
5
+ export type WorkerEntrypointConstructor = typeof WorkerEntrypoint;
6
+
7
+ const worker: ExportedHandler | WorkerEntrypointConstructor;
8
+ export default worker;
9
+ export const __INTERNAL_WRANGLER_MIDDLEWARE__: Middleware[];
10
+ }
11
+
12
+ declare module "__KV_ASSET_HANDLER__" {
13
+ export * from "@cloudflare/kv-asset-handler";
14
+ }
15
+
16
+ declare module "__STATIC_CONTENT_MANIFEST" {
17
+ const manifest: string;
18
+ export default manifest;
19
+ }
@@ -0,0 +1,67 @@
1
+ export type Awaitable<T> = T | Promise<T>;
2
+ // TODO: allow dispatching more events?
3
+ export type Dispatcher = (
4
+ type: "scheduled",
5
+ init: { cron?: string }
6
+ ) => Awaitable<void>;
7
+
8
+ export type IncomingRequest = Request<
9
+ unknown,
10
+ IncomingRequestCfProperties<unknown>
11
+ >;
12
+
13
+ export interface MiddlewareContext {
14
+ dispatch: Dispatcher;
15
+ next(request: IncomingRequest, env: any): Awaitable<Response>;
16
+ }
17
+
18
+ export type Middleware = (
19
+ request: IncomingRequest,
20
+ env: any,
21
+ ctx: ExecutionContext,
22
+ middlewareCtx: MiddlewareContext
23
+ ) => Awaitable<Response>;
24
+
25
+ const __facade_middleware__: Middleware[] = [];
26
+
27
+ // The register functions allow for the insertion of one or many middleware,
28
+ // We register internal middleware first in the stack, but have no way of controlling
29
+ // the order that addMiddleware is run in service workers so need an internal function.
30
+ export function __facade_register__(...args: (Middleware | Middleware[])[]) {
31
+ __facade_middleware__.push(...args.flat());
32
+ }
33
+ export function __facade_registerInternal__(
34
+ ...args: (Middleware | Middleware[])[]
35
+ ) {
36
+ __facade_middleware__.unshift(...args.flat());
37
+ }
38
+
39
+ function __facade_invokeChain__(
40
+ request: IncomingRequest,
41
+ env: any,
42
+ ctx: ExecutionContext,
43
+ dispatch: Dispatcher,
44
+ middlewareChain: Middleware[]
45
+ ): Awaitable<Response> {
46
+ const [head, ...tail] = middlewareChain;
47
+ const middlewareCtx: MiddlewareContext = {
48
+ dispatch,
49
+ next(newRequest, newEnv) {
50
+ return __facade_invokeChain__(newRequest, newEnv, ctx, dispatch, tail);
51
+ },
52
+ };
53
+ return head(request, env, ctx, middlewareCtx);
54
+ }
55
+
56
+ export function __facade_invoke__(
57
+ request: IncomingRequest,
58
+ env: any,
59
+ ctx: ExecutionContext,
60
+ dispatch: Dispatcher,
61
+ finalMiddleware: Middleware
62
+ ): Awaitable<Response> {
63
+ return __facade_invokeChain__(request, env, ctx, dispatch, [
64
+ ...__facade_middleware__,
65
+ finalMiddleware,
66
+ ]);
67
+ }
@@ -0,0 +1,134 @@
1
+ // This loads all middlewares exposed on the middleware object and then starts
2
+ // the invocation chain. The big idea is that we can add these to the middleware
3
+ // export dynamically through wrangler, or we can potentially let users directly
4
+ // add them as a sort of "plugin" system.
5
+
6
+ import ENTRY, { __INTERNAL_WRANGLER_MIDDLEWARE__ } from "__ENTRY_POINT__";
7
+ import { __facade_invoke__, __facade_register__, Dispatcher } from "./common";
8
+ import type { WorkerEntrypointConstructor } from "__ENTRY_POINT__";
9
+
10
+ // Preserve all the exports from the worker
11
+ export * from "__ENTRY_POINT__";
12
+
13
+ class __Facade_ScheduledController__ implements ScheduledController {
14
+ readonly #noRetry: ScheduledController["noRetry"];
15
+
16
+ constructor(
17
+ readonly scheduledTime: number,
18
+ readonly cron: string,
19
+ noRetry: ScheduledController["noRetry"]
20
+ ) {
21
+ this.#noRetry = noRetry;
22
+ }
23
+
24
+ noRetry() {
25
+ if (!(this instanceof __Facade_ScheduledController__)) {
26
+ throw new TypeError("Illegal invocation");
27
+ }
28
+ // Need to call native method immediately in case uncaught error thrown
29
+ this.#noRetry();
30
+ }
31
+ }
32
+
33
+ function wrapExportedHandler(worker: ExportedHandler): ExportedHandler {
34
+ // If we don't have any middleware defined, just return the handler as is
35
+ if (
36
+ __INTERNAL_WRANGLER_MIDDLEWARE__ === undefined ||
37
+ __INTERNAL_WRANGLER_MIDDLEWARE__.length === 0
38
+ ) {
39
+ return worker;
40
+ }
41
+ // Otherwise, register all middleware once
42
+ for (const middleware of __INTERNAL_WRANGLER_MIDDLEWARE__) {
43
+ __facade_register__(middleware);
44
+ }
45
+
46
+ const fetchDispatcher: ExportedHandlerFetchHandler = function (
47
+ request,
48
+ env,
49
+ ctx
50
+ ) {
51
+ if (worker.fetch === undefined) {
52
+ throw new Error("Handler does not export a fetch() function.");
53
+ }
54
+ return worker.fetch(request, env, ctx);
55
+ };
56
+
57
+ return {
58
+ ...worker,
59
+ fetch(request, env, ctx) {
60
+ const dispatcher: Dispatcher = function (type, init) {
61
+ if (type === "scheduled" && worker.scheduled !== undefined) {
62
+ const controller = new __Facade_ScheduledController__(
63
+ Date.now(),
64
+ init.cron ?? "",
65
+ () => {}
66
+ );
67
+ return worker.scheduled(controller, env, ctx);
68
+ }
69
+ };
70
+ return __facade_invoke__(request, env, ctx, dispatcher, fetchDispatcher);
71
+ },
72
+ };
73
+ }
74
+
75
+ function wrapWorkerEntrypoint(
76
+ klass: WorkerEntrypointConstructor
77
+ ): WorkerEntrypointConstructor {
78
+ // If we don't have any middleware defined, just return the handler as is
79
+ if (
80
+ __INTERNAL_WRANGLER_MIDDLEWARE__ === undefined ||
81
+ __INTERNAL_WRANGLER_MIDDLEWARE__.length === 0
82
+ ) {
83
+ return klass;
84
+ }
85
+ // Otherwise, register all middleware once
86
+ for (const middleware of __INTERNAL_WRANGLER_MIDDLEWARE__) {
87
+ __facade_register__(middleware);
88
+ }
89
+
90
+ // `extend`ing `klass` here so other RPC methods remain callable
91
+ return class extends klass {
92
+ #fetchDispatcher: ExportedHandlerFetchHandler<Record<string, unknown>> = (
93
+ request,
94
+ env,
95
+ ctx
96
+ ) => {
97
+ this.env = env;
98
+ this.ctx = ctx;
99
+ if (super.fetch === undefined) {
100
+ throw new Error("Entrypoint class does not define a fetch() function.");
101
+ }
102
+ return super.fetch(request);
103
+ };
104
+
105
+ #dispatcher: Dispatcher = (type, init) => {
106
+ if (type === "scheduled" && super.scheduled !== undefined) {
107
+ const controller = new __Facade_ScheduledController__(
108
+ Date.now(),
109
+ init.cron ?? "",
110
+ () => {}
111
+ );
112
+ return super.scheduled(controller);
113
+ }
114
+ };
115
+
116
+ fetch(request: Request<unknown, IncomingRequestCfProperties>) {
117
+ return __facade_invoke__(
118
+ request,
119
+ this.env,
120
+ this.ctx,
121
+ this.#dispatcher,
122
+ this.#fetchDispatcher
123
+ );
124
+ }
125
+ };
126
+ }
127
+
128
+ let WRAPPED_ENTRY: ExportedHandler | WorkerEntrypointConstructor | undefined;
129
+ if (typeof ENTRY === "object") {
130
+ WRAPPED_ENTRY = wrapExportedHandler(ENTRY);
131
+ } else if (typeof ENTRY === "function") {
132
+ WRAPPED_ENTRY = wrapWorkerEntrypoint(ENTRY);
133
+ }
134
+ export default WRAPPED_ENTRY;