wrangler 0.0.0-e1d50407 → 0.0.0-e1df3d831
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.
- package/README.md +30 -38
- package/bin/wrangler.js +3 -37
- package/config-schema.json +2388 -0
- package/package.json +76 -107
- package/templates/checked-fetch.js +1 -1
- package/templates/facade.d.ts +7 -5
- package/templates/gitignore +2 -4
- package/templates/init-tests/test-vitest-new-worker.js +1 -1
- package/templates/init-tests/test-vitest-new-worker.ts +3 -3
- package/templates/middleware/loader-modules.ts +92 -95
- package/templates/middleware/loader-sw.ts +4 -3
- package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
- package/templates/middleware/middleware-miniflare3-json-error.ts +0 -1
- package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
- package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
- package/templates/middleware/middleware-scheduled.ts +15 -1
- package/templates/mixedMode/proxyServerWorker/index.ts +5 -0
- package/templates/mixedMode/proxyServerWorker/wrangler.jsonc +4 -0
- package/templates/pages-dev-pipeline.ts +5 -3
- package/templates/pages-template-plugin.ts +2 -2
- package/templates/pages-template-worker.ts +1 -1
- package/templates/startDevWorker/InspectorProxyWorker.ts +32 -5
- package/templates/startDevWorker/ProxyWorker.ts +148 -64
- package/templates/tsconfig.init.json +4 -87
- package/templates/tsconfig.json +8 -1
- package/templates/tsconfig.tsbuildinfo +1 -0
- package/wrangler-dist/InspectorProxyWorker.js +19 -8
- package/wrangler-dist/InspectorProxyWorker.js.map +1 -1
- package/wrangler-dist/ProxyWorker.js +102 -50
- package/wrangler-dist/ProxyWorker.js.map +1 -1
- package/wrangler-dist/cli.d.ts +4248 -972
- package/wrangler-dist/cli.js +150038 -154522
- package/wrangler-dist/cli.js.map +7 -0
- package/Cloudflare_CA.pem +0 -18
- package/templates/middleware/middleware-d1-beta.d.ts +0 -3
- package/templates/middleware/middleware-multiworker-dev.d.ts +0 -4
- package/templates/middleware/middleware-multiworker-dev.ts +0 -68
- package/templates/middleware/middleware-serve-static-assets.d.ts +0 -6
- package/templates/middleware/middleware-serve-static-assets.ts +0 -56
- package/wrangler-dist/wasm-sync.wasm +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wrangler",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-e1df3d831",
|
|
4
4
|
"description": "Command-line interface for all things Cloudflare Workers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"wrangler",
|
|
@@ -47,167 +47,136 @@
|
|
|
47
47
|
"wrangler-dist",
|
|
48
48
|
"templates",
|
|
49
49
|
"kv-asset-handler.js",
|
|
50
|
-
"
|
|
50
|
+
"config-schema.json"
|
|
51
51
|
],
|
|
52
|
-
"jest": {
|
|
53
|
-
"coverageReporters": [
|
|
54
|
-
"json",
|
|
55
|
-
"html",
|
|
56
|
-
"text",
|
|
57
|
-
"cobertura"
|
|
58
|
-
],
|
|
59
|
-
"moduleNameMapper": {
|
|
60
|
-
"clipboardy": "<rootDir>/src/__tests__/helpers/clipboardy-mock.js",
|
|
61
|
-
"miniflare/cli": "<rootDir>/../../node_modules/miniflare/dist/src/cli.js"
|
|
62
|
-
},
|
|
63
|
-
"restoreMocks": true,
|
|
64
|
-
"setupFilesAfterEnv": [
|
|
65
|
-
"<rootDir>/src/__tests__/jest.setup.ts"
|
|
66
|
-
],
|
|
67
|
-
"testRegex": "src/__tests__/.*\\.(test|spec)\\.[jt]sx?$",
|
|
68
|
-
"testTimeout": 50000,
|
|
69
|
-
"transform": {
|
|
70
|
-
"^.+\\.c?(t|j)sx?$": [
|
|
71
|
-
"esbuild-jest",
|
|
72
|
-
{
|
|
73
|
-
"sourcemap": true
|
|
74
|
-
}
|
|
75
|
-
]
|
|
76
|
-
},
|
|
77
|
-
"transformIgnorePatterns": [
|
|
78
|
-
"node_modules/.pnpm/(?!find-up|locate-path|p-locate|p-limit|p-timeout|p-queue|yocto-queue|path-exists|execa|strip-final-newline|npm-run-path|path-key|onetime|mimic-fn|human-signals|is-stream|get-port|supports-color|pretty-bytes)"
|
|
79
|
-
],
|
|
80
|
-
"snapshotFormat": {
|
|
81
|
-
"escapeString": true,
|
|
82
|
-
"printBasicPrototype": true
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
52
|
"dependencies": {
|
|
86
|
-
"@cloudflare/
|
|
87
|
-
"
|
|
88
|
-
"
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
"
|
|
92
|
-
"
|
|
93
|
-
"
|
|
94
|
-
"path-to-regexp": "^6.2.0",
|
|
95
|
-
"resolve.exports": "^2.0.2",
|
|
96
|
-
"selfsigned": "^2.0.1",
|
|
97
|
-
"source-map": "0.6.1",
|
|
98
|
-
"xxhash-wasm": "^1.0.1",
|
|
99
|
-
"miniflare": "3.20231030.4"
|
|
53
|
+
"@cloudflare/unenv-preset": "2.3.1",
|
|
54
|
+
"blake3-wasm": "2.1.5",
|
|
55
|
+
"esbuild": "0.25.4",
|
|
56
|
+
"path-to-regexp": "6.3.0",
|
|
57
|
+
"unenv": "2.0.0-rc.15",
|
|
58
|
+
"workerd": "1.20250508.0",
|
|
59
|
+
"@cloudflare/kv-asset-handler": "0.4.0",
|
|
60
|
+
"miniflare": "0.0.0-e1df3d831"
|
|
100
61
|
},
|
|
101
62
|
"devDependencies": {
|
|
102
|
-
"@
|
|
103
|
-
"@cloudflare/
|
|
104
|
-
"@cloudflare/types": "^
|
|
105
|
-
"@
|
|
63
|
+
"@aws-sdk/client-s3": "^3.721.0",
|
|
64
|
+
"@cloudflare/types": "6.18.4",
|
|
65
|
+
"@cloudflare/workers-types": "^4.20250508.0",
|
|
66
|
+
"@cspotcode/source-map-support": "0.8.1",
|
|
106
67
|
"@iarna/toml": "^3.0.0",
|
|
107
|
-
"@microsoft/api-extractor": "^7.
|
|
68
|
+
"@microsoft/api-extractor": "^7.47.0",
|
|
108
69
|
"@sentry/node": "^7.86.0",
|
|
109
70
|
"@sentry/types": "^7.86.0",
|
|
110
71
|
"@sentry/utils": "^7.86.0",
|
|
111
|
-
"@types/body-parser": "^1.19.2",
|
|
112
|
-
"@types/busboy": "^1.5.0",
|
|
113
72
|
"@types/command-exists": "^1.2.0",
|
|
114
|
-
"@types/
|
|
115
|
-
"@types/glob-to-regexp": "0.4.1",
|
|
73
|
+
"@types/glob-to-regexp": "^0.4.1",
|
|
116
74
|
"@types/is-ci": "^3.0.0",
|
|
117
75
|
"@types/javascript-time-ago": "^2.0.3",
|
|
118
|
-
"@types/
|
|
119
|
-
"@types/mime": "^2.0.3",
|
|
76
|
+
"@types/mime": "^3.0.4",
|
|
120
77
|
"@types/minimatch": "^5.1.2",
|
|
78
|
+
"@types/node": "^20.17.32",
|
|
79
|
+
"@types/node-forge": "^1.3.11",
|
|
121
80
|
"@types/prompts": "^2.0.14",
|
|
122
|
-
"@types/
|
|
123
|
-
"@types/serve-static": "^1.13.10",
|
|
81
|
+
"@types/resolve": "^1.20.6",
|
|
124
82
|
"@types/shell-quote": "^1.7.2",
|
|
125
83
|
"@types/signal-exit": "^3.0.1",
|
|
126
|
-
"@types/source-map-support": "^0.5.7",
|
|
127
84
|
"@types/supports-color": "^8.1.1",
|
|
128
|
-
"@types/ws": "^8.5.
|
|
129
|
-
"@types/yargs": "^17.0.
|
|
85
|
+
"@types/ws": "^8.5.7",
|
|
86
|
+
"@types/yargs": "^17.0.22",
|
|
87
|
+
"@vitest/ui": "~3.1.1",
|
|
130
88
|
"@webcontainer/env": "^1.1.0",
|
|
131
|
-
"
|
|
132
|
-
"
|
|
89
|
+
"chalk": "^5.2.0",
|
|
90
|
+
"chokidar": "^4.0.1",
|
|
133
91
|
"cli-table3": "^0.6.3",
|
|
134
|
-
"clipboardy": "^3.0.0",
|
|
135
92
|
"cmd-shim": "^4.1.0",
|
|
136
93
|
"command-exists": "^1.2.9",
|
|
137
|
-
"concurrently": "^
|
|
138
|
-
"
|
|
139
|
-
"
|
|
140
|
-
"
|
|
94
|
+
"concurrently": "^8.2.2",
|
|
95
|
+
"date-fns": "^4.1.0",
|
|
96
|
+
"devtools-protocol": "^0.0.1182435",
|
|
97
|
+
"dotenv": "^16.3.1",
|
|
141
98
|
"execa": "^6.1.0",
|
|
142
|
-
"express": "^4.18.1",
|
|
143
|
-
"finalhandler": "^1.2.0",
|
|
144
99
|
"find-up": "^6.3.0",
|
|
145
|
-
"get-port": "^
|
|
146
|
-
"glob-to-regexp": "0.4.1",
|
|
147
|
-
"http-terminator": "^3.2.0",
|
|
100
|
+
"get-port": "^7.0.0",
|
|
101
|
+
"glob-to-regexp": "^0.4.1",
|
|
148
102
|
"https-proxy-agent": "7.0.2",
|
|
149
|
-
"ignore": "^5.2.0",
|
|
150
|
-
"ink": "^3.2.0",
|
|
151
|
-
"ink-select-input": "^4.2.1",
|
|
152
|
-
"ink-spinner": "^4.0.3",
|
|
153
|
-
"ink-table": "^3.0.0",
|
|
154
|
-
"ink-testing-library": "^2.1.0",
|
|
155
|
-
"ink-text-input": "^4.0.3",
|
|
156
103
|
"is-ci": "^3.0.1",
|
|
104
|
+
"itty-time": "^1.0.6",
|
|
157
105
|
"javascript-time-ago": "^2.5.4",
|
|
158
|
-
"
|
|
159
|
-
"jest-fetch-mock": "^3.0.3",
|
|
160
|
-
"jest-websocket-mock": "^2.5.0",
|
|
106
|
+
"md5-file": "5.0.0",
|
|
161
107
|
"mime": "^3.0.0",
|
|
162
108
|
"minimatch": "^5.1.0",
|
|
163
|
-
"
|
|
109
|
+
"mock-socket": "^9.3.1",
|
|
110
|
+
"msw": "2.4.3",
|
|
111
|
+
"node-forge": "^1.3.1",
|
|
164
112
|
"open": "^8.4.0",
|
|
165
113
|
"p-queue": "^7.2.0",
|
|
166
114
|
"patch-console": "^1.0.0",
|
|
167
115
|
"pretty-bytes": "^6.0.0",
|
|
168
116
|
"prompts": "^2.4.2",
|
|
169
|
-
"
|
|
170
|
-
"
|
|
171
|
-
"
|
|
117
|
+
"resolve": "^1.22.8",
|
|
118
|
+
"rimraf": "^6.0.1",
|
|
119
|
+
"selfsigned": "^2.0.1",
|
|
172
120
|
"semiver": "^1.1.0",
|
|
173
|
-
"serve-static": "^1.15.0",
|
|
174
121
|
"shell-quote": "^1.8.1",
|
|
175
|
-
"shellac": "^0.8.0",
|
|
176
122
|
"signal-exit": "^3.0.7",
|
|
177
|
-
"
|
|
123
|
+
"source-map": "^0.6.1",
|
|
124
|
+
"strip-ansi": "^7.1.0",
|
|
178
125
|
"supports-color": "^9.2.2",
|
|
179
126
|
"timeago.js": "^4.0.2",
|
|
180
127
|
"ts-dedent": "^2.2.0",
|
|
181
|
-
"
|
|
128
|
+
"ts-json-schema-generator": "^1.5.0",
|
|
129
|
+
"typescript": "^5.7.2",
|
|
130
|
+
"undici": "^5.28.5",
|
|
182
131
|
"update-check": "^1.5.4",
|
|
183
|
-
"
|
|
132
|
+
"vitest": "~3.1.1",
|
|
133
|
+
"vitest-websocket-mock": "^0.4.0",
|
|
134
|
+
"ws": "8.18.0",
|
|
184
135
|
"xdg-app-paths": "^8.3.0",
|
|
136
|
+
"xxhash-wasm": "^1.0.1",
|
|
185
137
|
"yargs": "^17.7.2",
|
|
186
|
-
"
|
|
187
|
-
"@cloudflare/
|
|
188
|
-
"@cloudflare/pages-shared": "^0.
|
|
138
|
+
"@cloudflare/cli": "1.1.1",
|
|
139
|
+
"@cloudflare/eslint-config-worker": "1.1.0",
|
|
140
|
+
"@cloudflare/pages-shared": "^0.13.36",
|
|
141
|
+
"@cloudflare/workers-shared": "0.0.0-e1df3d831",
|
|
189
142
|
"@cloudflare/workers-tsconfig": "0.0.0"
|
|
190
143
|
},
|
|
144
|
+
"peerDependencies": {
|
|
145
|
+
"@cloudflare/workers-types": "^4.20250508.0"
|
|
146
|
+
},
|
|
147
|
+
"peerDependenciesMeta": {
|
|
148
|
+
"@cloudflare/workers-types": {
|
|
149
|
+
"optional": true
|
|
150
|
+
}
|
|
151
|
+
},
|
|
191
152
|
"optionalDependencies": {
|
|
192
|
-
"fsevents": "~2.3.2"
|
|
153
|
+
"fsevents": "~2.3.2",
|
|
154
|
+
"sharp": "^0.33.5"
|
|
193
155
|
},
|
|
194
156
|
"engines": {
|
|
195
|
-
"node": ">=
|
|
157
|
+
"node": ">=18.0.0"
|
|
158
|
+
},
|
|
159
|
+
"volta": {
|
|
160
|
+
"extends": "../../package.json"
|
|
161
|
+
},
|
|
162
|
+
"workers-sdk": {
|
|
163
|
+
"prerelease": true
|
|
196
164
|
},
|
|
197
165
|
"scripts": {
|
|
198
166
|
"assert-git-version": "node -r esbuild-register scripts/assert-git-version.ts",
|
|
199
|
-
"build": "pnpm run clean && pnpm run bundle && pnpm run emit-types",
|
|
167
|
+
"build": "pnpm run clean && pnpm run bundle && pnpm run emit-types && pnpm run generate-json-schema",
|
|
200
168
|
"bundle": "node -r esbuild-register scripts/bundle.ts",
|
|
201
|
-
"check:lint": "eslint .",
|
|
202
|
-
"check:type": "tsc",
|
|
169
|
+
"check:lint": "eslint . --max-warnings=0",
|
|
170
|
+
"check:type": "tsc -p ./tsconfig.json && tsc -p ./templates/tsconfig.json",
|
|
203
171
|
"clean": "rimraf wrangler-dist miniflare-dist emitted-types",
|
|
204
172
|
"dev": "pnpm run clean && concurrently -c black,blue --kill-others-on-fail false \"pnpm run bundle --watch\" \"pnpm run check:type --watch --preserveWatchOutput\"",
|
|
205
173
|
"emit-types": "tsc -p tsconfig.emit.json && node -r esbuild-register scripts/emit-types.ts",
|
|
174
|
+
"generate-json-schema": "node -r esbuild-register scripts/generate-json-schema.ts",
|
|
206
175
|
"start": "pnpm run bundle && cross-env NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
|
|
207
|
-
"test": "pnpm run assert-git-version &&
|
|
208
|
-
"test:ci": "pnpm run test
|
|
176
|
+
"test": "dotenv -- pnpm run assert-git-version && dotenv -- vitest",
|
|
177
|
+
"test:ci": "pnpm run test run",
|
|
209
178
|
"test:debug": "pnpm run test --silent=false --verbose=true",
|
|
210
|
-
"test:e2e": "
|
|
179
|
+
"test:e2e": "dotenv -- vitest run -c ./e2e/vitest.config.mts",
|
|
211
180
|
"test:watch": "pnpm run test --testTimeout=50000 --watch",
|
|
212
181
|
"type:tests": "tsc -p ./src/__tests__/tsconfig.json && tsc -p ./e2e/tsconfig.json"
|
|
213
182
|
}
|
package/templates/facade.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
declare module "__ENTRY_POINT__" {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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;
|
|
7
8
|
export default worker;
|
|
9
|
+
export const __INTERNAL_WRANGLER_MIDDLEWARE__: Middleware[];
|
|
8
10
|
}
|
|
9
11
|
|
|
10
12
|
declare module "__KV_ASSET_HANDLER__" {
|
package/templates/gitignore
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
1
2
|
import { unstable_dev } from "wrangler";
|
|
2
|
-
import type {
|
|
3
|
-
import { describe, expect, it, beforeAll, afterAll } from "vitest";
|
|
3
|
+
import type { Unstable_DevWorker } from "wrangler";
|
|
4
4
|
|
|
5
5
|
describe("Worker", () => {
|
|
6
|
-
let worker:
|
|
6
|
+
let worker: Unstable_DevWorker;
|
|
7
7
|
|
|
8
8
|
beforeAll(async () => {
|
|
9
9
|
worker = await unstable_dev("src/index.ts", {
|
|
@@ -1,23 +1,17 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// // of "plugin" system.
|
|
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.
|
|
6
5
|
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
__facade_invoke__,
|
|
11
|
-
__facade_register__,
|
|
12
|
-
} from "./common";
|
|
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__";
|
|
13
9
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
// We need to preserve all of the exports from the worker
|
|
10
|
+
// Preserve all the exports from the worker
|
|
17
11
|
export * from "__ENTRY_POINT__";
|
|
18
12
|
|
|
19
13
|
class __Facade_ScheduledController__ implements ScheduledController {
|
|
20
|
-
#noRetry: ScheduledController["noRetry"];
|
|
14
|
+
readonly #noRetry: ScheduledController["noRetry"];
|
|
21
15
|
|
|
22
16
|
constructor(
|
|
23
17
|
readonly scheduledTime: number,
|
|
@@ -36,71 +30,34 @@ class __Facade_ScheduledController__ implements ScheduledController {
|
|
|
36
30
|
}
|
|
37
31
|
}
|
|
38
32
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
let env = rawEnv as Record<string, unknown>;
|
|
51
|
-
if (worker.envWrappers && worker.envWrappers.length > 0) {
|
|
52
|
-
for (const wrapFn of worker.envWrappers) {
|
|
53
|
-
env = wrapFn(env);
|
|
54
|
-
}
|
|
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);
|
|
55
44
|
}
|
|
56
|
-
return env;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* This type is here to cause a type error if a new export handler is added to
|
|
61
|
-
* `ExportHandler` without it being included in the `facade` below.
|
|
62
|
-
*/
|
|
63
|
-
type MissingExportHandlers = Omit<
|
|
64
|
-
Required<ExportedHandler>,
|
|
65
|
-
"tail" | "trace" | "scheduled" | "queue" | "test" | "email" | "fetch"
|
|
66
|
-
>;
|
|
67
|
-
|
|
68
|
-
let registeredMiddleware = false;
|
|
69
|
-
|
|
70
|
-
const facade: ExportedHandler<unknown> & MissingExportHandlers = {
|
|
71
|
-
...(worker.tail && {
|
|
72
|
-
tail: maskHandlerEnv(worker.tail),
|
|
73
|
-
}),
|
|
74
|
-
...(worker.trace && {
|
|
75
|
-
trace: maskHandlerEnv(worker.trace),
|
|
76
|
-
}),
|
|
77
|
-
...(worker.scheduled && {
|
|
78
|
-
scheduled: maskHandlerEnv(worker.scheduled),
|
|
79
|
-
}),
|
|
80
|
-
...(worker.queue && {
|
|
81
|
-
queue: maskHandlerEnv(worker.queue),
|
|
82
|
-
}),
|
|
83
|
-
...(worker.test && {
|
|
84
|
-
test: maskHandlerEnv(worker.test),
|
|
85
|
-
}),
|
|
86
|
-
...(worker.email && {
|
|
87
|
-
email: maskHandlerEnv(worker.email),
|
|
88
|
-
}),
|
|
89
45
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
}
|
|
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
|
+
};
|
|
102
56
|
|
|
103
|
-
|
|
57
|
+
return {
|
|
58
|
+
...worker,
|
|
59
|
+
fetch(request, env, ctx) {
|
|
60
|
+
const dispatcher: Dispatcher = function (type, init) {
|
|
104
61
|
if (type === "scheduled" && worker.scheduled !== undefined) {
|
|
105
62
|
const controller = new __Facade_ScheduledController__(
|
|
106
63
|
Date.now(),
|
|
@@ -110,28 +67,68 @@ const facade: ExportedHandler<unknown> & MissingExportHandlers = {
|
|
|
110
67
|
return worker.scheduled(controller, env, ctx);
|
|
111
68
|
}
|
|
112
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
|
+
}
|
|
113
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>) {
|
|
114
117
|
return __facade_invoke__(
|
|
115
118
|
request,
|
|
116
|
-
env,
|
|
117
|
-
ctx,
|
|
118
|
-
|
|
119
|
-
|
|
119
|
+
this.env,
|
|
120
|
+
this.ctx,
|
|
121
|
+
this.#dispatcher,
|
|
122
|
+
this.#fetchDispatcher
|
|
120
123
|
);
|
|
121
|
-
} else {
|
|
122
|
-
// We didn't have any middleware so we can skip the invocation chain,
|
|
123
|
-
// and just call the fetch handler directly
|
|
124
|
-
|
|
125
|
-
// We "don't care" if this is undefined as we want to have the same behavior
|
|
126
|
-
// as if the worker completely bypassed middleware.
|
|
127
|
-
return __facade_modules_fetch__(request, env, ctx);
|
|
128
124
|
}
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
type HandlerFn<D, R> = (data: D, env: unknown, ctx: ExecutionContext) => R;
|
|
133
|
-
function maskHandlerEnv<D, R>(handler: HandlerFn<D, R>): HandlerFn<D, R> {
|
|
134
|
-
return (data, env, ctx) => handler(data, getMaskedEnv(env), ctx);
|
|
125
|
+
};
|
|
135
126
|
}
|
|
136
127
|
|
|
137
|
-
|
|
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;
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
|
+
__facade_invoke__,
|
|
3
|
+
__facade_register__,
|
|
4
|
+
__facade_registerInternal__,
|
|
2
5
|
Awaitable,
|
|
3
6
|
Dispatcher,
|
|
4
7
|
IncomingRequest,
|
|
5
8
|
Middleware,
|
|
6
|
-
__facade_invoke__,
|
|
7
|
-
__facade_register__,
|
|
8
|
-
__facade_registerInternal__,
|
|
9
9
|
} from "./common";
|
|
10
|
+
|
|
10
11
|
export { __facade_register__, __facade_registerInternal__ };
|
|
11
12
|
|
|
12
13
|
// Miniflare 2's `EventTarget` follows the spec and doesn't allow exceptions to
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Middleware } from "./common";
|
|
2
|
+
|
|
3
|
+
const drainBody: Middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
4
|
+
try {
|
|
5
|
+
return await middlewareCtx.next(request, env);
|
|
6
|
+
} finally {
|
|
7
|
+
try {
|
|
8
|
+
if (request.body !== null && !request.bodyUsed) {
|
|
9
|
+
const reader = request.body.getReader();
|
|
10
|
+
while (!(await reader.read()).done) {}
|
|
11
|
+
}
|
|
12
|
+
} catch (e) {
|
|
13
|
+
console.error("Failed to drain the unused request body.", e);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default drainBody;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/// <reference path="middleware-patch-console-prefix.d.ts"/>
|
|
2
|
+
|
|
3
|
+
import { prefix } from "config:middleware/patch-console-prefix";
|
|
4
|
+
import type { Middleware } from "./common";
|
|
5
|
+
|
|
6
|
+
// @ts-expect-error globalThis.console _does_ exist
|
|
7
|
+
globalThis.console = new Proxy(globalThis.console, {
|
|
8
|
+
get(target, p, receiver) {
|
|
9
|
+
if (p === "log" || p === "debug" || p === "info") {
|
|
10
|
+
return (...args: unknown[]) =>
|
|
11
|
+
Reflect.get(target, p, receiver)(prefix, ...args);
|
|
12
|
+
}
|
|
13
|
+
return Reflect.get(target, p, receiver);
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const passthrough: Middleware = (request, env, _ctx, middlewareCtx) => {
|
|
18
|
+
return middlewareCtx.next(request, env);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export default passthrough;
|
|
@@ -9,7 +9,21 @@ const scheduled: Middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
|
9
9
|
|
|
10
10
|
return new Response("Ran scheduled event");
|
|
11
11
|
}
|
|
12
|
-
|
|
12
|
+
|
|
13
|
+
const resp = await middlewareCtx.next(request, env);
|
|
14
|
+
|
|
15
|
+
// If you open the `/__scheduled` page in a browser, the browser will automatically make a request to `/favicon.ico`.
|
|
16
|
+
// For scheduled Workers _without_ a fetch handler, this will result in a 500 response that clutters the log with unhelpful error messages.
|
|
17
|
+
// To avoid this, inject a 404 response to favicon.ico loads on the `/__scheduled` page
|
|
18
|
+
if (
|
|
19
|
+
request.headers.get("referer")?.endsWith("/__scheduled") &&
|
|
20
|
+
url.pathname === "/favicon.ico" &&
|
|
21
|
+
resp.status === 500
|
|
22
|
+
) {
|
|
23
|
+
return new Response(null, { status: 404 });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return resp;
|
|
13
27
|
};
|
|
14
28
|
|
|
15
29
|
export default scheduled;
|