wrangler 0.0.0-d8a9995b → 0.0.0-d8b5a01e
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 +6 -8
- package/bin/wrangler.js +5 -0
- package/package.json +209 -191
- package/templates/__tests__/tsconfig.json +1 -1
- package/templates/checked-fetch.js +9 -2
- package/templates/facade.d.ts +4 -1
- package/templates/middleware/loader-modules.ts +64 -11
- package/templates/middleware/middleware-d1-beta.d.ts +3 -0
- package/templates/middleware/middleware-miniflare3-json-error.ts +1 -0
- package/templates/middleware/middleware-multiworker-dev.d.ts +4 -0
- package/templates/middleware/middleware-multiworker-dev.ts +68 -0
- package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
- package/templates/middleware/middleware-serve-static-assets.ts +56 -0
- package/templates/modules-watch-stub.js +4 -0
- package/templates/pages-template-plugin.ts +12 -2
- package/templates/pages-template-worker.ts +11 -2
- package/templates/startDevWorker/InspectorProxyWorker.ts +608 -0
- package/templates/startDevWorker/ProxyWorker.ts +252 -0
- package/templates/tsconfig.json +1 -1
- package/wrangler-dist/InspectorProxyWorker.js +419 -0
- package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
- package/wrangler-dist/ProxyWorker.js +186 -0
- package/wrangler-dist/ProxyWorker.js.map +6 -0
- package/wrangler-dist/cli.d.ts +22003 -225
- package/wrangler-dist/cli.js +83833 -81175
- package/wrangler-dist/wasm-sync.wasm +0 -0
- package/templates/__tests__/tsconfig.tsbuildinfo +0 -1
- package/templates/d1-beta-facade.js +0 -244
- package/templates/first-party-worker-module-facade.ts +0 -19
- package/templates/format-dev-errors.ts +0 -34
- package/templates/serve-static-assets.ts +0 -66
- package/templates/service-bindings-module-facade.js +0 -55
- package/templates/service-bindings-sw-facade.js +0 -42
- package/templates/tsconfig.tsbuildinfo +0 -1
package/README.md
CHANGED
|
@@ -6,8 +6,6 @@
|
|
|
6
6
|
<a href="https://discord.gg/CloudflareDev"><img alt="Discord" src="https://img.shields.io/discord/595317990191398933?color=%23F48120&style=flat-square"></a>
|
|
7
7
|
</section>
|
|
8
8
|
|
|
9
|
-
> This package is for wrangler v2.x, released first in May 2022. If you're looking for v1.x of the `@cloudflare/wrangler` package, visit https://www.npmjs.com/package/@cloudflare/wrangler / https://github.com/cloudflare/wrangler-legacy.
|
|
10
|
-
|
|
11
9
|
`wrangler` is a command line tool for building [Cloudflare Workers](https://workers.cloudflare.com/).
|
|
12
10
|
|
|
13
11
|
## Quick Start
|
|
@@ -26,7 +24,7 @@ npx wrangler deploy index.js --name my-worker
|
|
|
26
24
|
|
|
27
25
|
```bash
|
|
28
26
|
# Generate a new project
|
|
29
|
-
npx wrangler init my-worker
|
|
27
|
+
npx wrangler init my-worker --no-delegate-c3
|
|
30
28
|
# try it out
|
|
31
29
|
cd my-worker && npm run start
|
|
32
30
|
# and then deploy it
|
|
@@ -43,15 +41,15 @@ $ npm install wrangler --save-dev
|
|
|
43
41
|
|
|
44
42
|
Wrangler is configured via a `wrangler.toml` file in the project root. When utilizing the `wrangler init` command, a `wrangler.toml` file will be created for you.
|
|
45
43
|
|
|
46
|
-
|
|
44
|
+
Example:
|
|
47
45
|
|
|
48
46
|
```toml
|
|
49
|
-
main = "./src/index.ts" # init w/ TypeScript
|
|
50
47
|
name = "my-worker"
|
|
51
|
-
|
|
48
|
+
main = "./src/index.ts" # init w/ TypeScript
|
|
49
|
+
compatibility_date = "YYYY-MM-DD"
|
|
52
50
|
```
|
|
53
51
|
|
|
54
|
-
|
|
52
|
+
For more detailed information about configuration, refer to the [documentation](https://developers.cloudflare.com/workers/wrangler/configuration/).
|
|
55
53
|
|
|
56
54
|
## Commands
|
|
57
55
|
|
|
@@ -67,7 +65,7 @@ Start a local development server, with live reloading and devtools.
|
|
|
67
65
|
|
|
68
66
|
Publish the given script to the worldwide Cloudflare network.
|
|
69
67
|
|
|
70
|
-
For more commands and options, refer to the [documentation](https://developers.cloudflare.com/workers/
|
|
68
|
+
For more commands and options, refer to the [documentation](https://developers.cloudflare.com/workers/wrangler/commands/).
|
|
71
69
|
|
|
72
70
|
## Pages
|
|
73
71
|
|
package/bin/wrangler.js
CHANGED
package/package.json
CHANGED
|
@@ -1,192 +1,210 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
2
|
+
"name": "wrangler",
|
|
3
|
+
"version": "0.0.0-d8b5a01e",
|
|
4
|
+
"description": "Command-line interface for all things Cloudflare Workers",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"wrangler",
|
|
7
|
+
"cloudflare",
|
|
8
|
+
"workers",
|
|
9
|
+
"cloudflare workers",
|
|
10
|
+
"edge",
|
|
11
|
+
"compute",
|
|
12
|
+
"serverless",
|
|
13
|
+
"serverless application",
|
|
14
|
+
"serverless module",
|
|
15
|
+
"wasm",
|
|
16
|
+
"web",
|
|
17
|
+
"assembly",
|
|
18
|
+
"webassembly",
|
|
19
|
+
"rust",
|
|
20
|
+
"emscripten",
|
|
21
|
+
"typescript",
|
|
22
|
+
"graphql",
|
|
23
|
+
"router",
|
|
24
|
+
"http",
|
|
25
|
+
"cli"
|
|
26
|
+
],
|
|
27
|
+
"homepage": "https://github.com/cloudflare/workers-sdk#readme",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/cloudflare/workers-sdk/issues"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/cloudflare/workers-sdk.git",
|
|
34
|
+
"directory": "packages/wrangler"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT OR Apache-2.0",
|
|
37
|
+
"author": "wrangler@cloudflare.com",
|
|
38
|
+
"main": "wrangler-dist/cli.js",
|
|
39
|
+
"types": "wrangler-dist/cli.d.ts",
|
|
40
|
+
"bin": {
|
|
41
|
+
"wrangler": "./bin/wrangler.js",
|
|
42
|
+
"wrangler2": "./bin/wrangler.js"
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"bin",
|
|
46
|
+
"miniflare-dist",
|
|
47
|
+
"wrangler-dist",
|
|
48
|
+
"templates",
|
|
49
|
+
"kv-asset-handler.js",
|
|
50
|
+
"Cloudflare_CA.pem"
|
|
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
|
+
"dependencies": {
|
|
86
|
+
"@cloudflare/kv-asset-handler": "^0.2.0",
|
|
87
|
+
"@esbuild-plugins/node-globals-polyfill": "^0.2.3",
|
|
88
|
+
"@esbuild-plugins/node-modules-polyfill": "^0.2.2",
|
|
89
|
+
"blake3-wasm": "^2.1.5",
|
|
90
|
+
"chokidar": "^3.5.3",
|
|
91
|
+
"esbuild": "0.17.19",
|
|
92
|
+
"nanoid": "^3.3.3",
|
|
93
|
+
"path-to-regexp": "^6.2.0",
|
|
94
|
+
"resolve.exports": "^2.0.2",
|
|
95
|
+
"selfsigned": "^2.0.1",
|
|
96
|
+
"source-map": "0.6.1",
|
|
97
|
+
"source-map-support": "0.5.21",
|
|
98
|
+
"xxhash-wasm": "^1.0.1",
|
|
99
|
+
"miniflare": "3.20231030.1"
|
|
100
|
+
},
|
|
101
|
+
"devDependencies": {
|
|
102
|
+
"@cloudflare/eslint-config-worker": "*",
|
|
103
|
+
"@cloudflare/types": "^6.18.4",
|
|
104
|
+
"https-proxy-agent": "7.0.2",
|
|
105
|
+
"@cloudflare/workers-types": "^4.20230914.0",
|
|
106
|
+
"@iarna/toml": "^3.0.0",
|
|
107
|
+
"@microsoft/api-extractor": "^7.28.3",
|
|
108
|
+
"@types/body-parser": "^1.19.2",
|
|
109
|
+
"@types/busboy": "^1.5.0",
|
|
110
|
+
"@types/command-exists": "^1.2.0",
|
|
111
|
+
"@types/express": "^4.17.13",
|
|
112
|
+
"@types/glob-to-regexp": "0.4.1",
|
|
113
|
+
"@types/is-ci": "^3.0.0",
|
|
114
|
+
"@types/javascript-time-ago": "^2.0.3",
|
|
115
|
+
"@types/jest": "^29.5.5",
|
|
116
|
+
"@types/mime": "^2.0.3",
|
|
117
|
+
"@types/minimatch": "^5.1.2",
|
|
118
|
+
"@types/prompts": "^2.0.14",
|
|
119
|
+
"@types/react": "^17.0.37",
|
|
120
|
+
"@types/serve-static": "^1.13.10",
|
|
121
|
+
"@types/shell-quote": "^1.7.2",
|
|
122
|
+
"@types/signal-exit": "^3.0.1",
|
|
123
|
+
"@types/source-map-support": "^0.5.7",
|
|
124
|
+
"@types/supports-color": "^8.1.1",
|
|
125
|
+
"@types/ws": "^8.5.3",
|
|
126
|
+
"@types/yargs": "^17.0.10",
|
|
127
|
+
"@webcontainer/env": "^1.1.0",
|
|
128
|
+
"body-parser": "^1.20.0",
|
|
129
|
+
"chalk": "^2.4.2",
|
|
130
|
+
"cli-table3": "^0.6.3",
|
|
131
|
+
"clipboardy": "^3.0.0",
|
|
132
|
+
"cmd-shim": "^4.1.0",
|
|
133
|
+
"command-exists": "^1.2.9",
|
|
134
|
+
"concurrently": "^7.2.2",
|
|
135
|
+
"devtools-protocol": "^0.0.955664",
|
|
136
|
+
"dotenv": "^16.0.0",
|
|
137
|
+
"esbuild-jest": "0.5.0",
|
|
138
|
+
"execa": "^6.1.0",
|
|
139
|
+
"express": "^4.18.1",
|
|
140
|
+
"finalhandler": "^1.2.0",
|
|
141
|
+
"find-up": "^6.3.0",
|
|
142
|
+
"get-port": "^6.1.2",
|
|
143
|
+
"glob-to-regexp": "0.4.1",
|
|
144
|
+
"http-terminator": "^3.2.0",
|
|
145
|
+
"ignore": "^5.2.0",
|
|
146
|
+
"ink": "^3.2.0",
|
|
147
|
+
"ink-select-input": "^4.2.1",
|
|
148
|
+
"ink-spinner": "^4.0.3",
|
|
149
|
+
"ink-table": "^3.0.0",
|
|
150
|
+
"ink-testing-library": "^2.1.0",
|
|
151
|
+
"ink-text-input": "^4.0.3",
|
|
152
|
+
"is-ci": "^3.0.1",
|
|
153
|
+
"javascript-time-ago": "^2.5.4",
|
|
154
|
+
"jest": "^29.7.0",
|
|
155
|
+
"jest-fetch-mock": "^3.0.3",
|
|
156
|
+
"jest-websocket-mock": "^2.5.0",
|
|
157
|
+
"mime": "^3.0.0",
|
|
158
|
+
"minimatch": "^5.1.0",
|
|
159
|
+
"msw": "^0.49.1",
|
|
160
|
+
"open": "^8.4.0",
|
|
161
|
+
"p-queue": "^7.2.0",
|
|
162
|
+
"patch-console": "^1.0.0",
|
|
163
|
+
"pretty-bytes": "^6.0.0",
|
|
164
|
+
"prompts": "^2.4.2",
|
|
165
|
+
"react": "^17.0.2",
|
|
166
|
+
"react-error-boundary": "^3.1.4",
|
|
167
|
+
"remove-accents-esm": "^0.0.1",
|
|
168
|
+
"semiver": "^1.1.0",
|
|
169
|
+
"serve-static": "^1.15.0",
|
|
170
|
+
"shell-quote": "^1.8.1",
|
|
171
|
+
"shellac": "^0.8.0",
|
|
172
|
+
"signal-exit": "^3.0.7",
|
|
173
|
+
"strip-ansi": "^7.0.1",
|
|
174
|
+
"supports-color": "^9.2.2",
|
|
175
|
+
"timeago.js": "^4.0.2",
|
|
176
|
+
"ts-dedent": "^2.2.0",
|
|
177
|
+
"undici": "5.20.0",
|
|
178
|
+
"update-check": "^1.5.4",
|
|
179
|
+
"ws": "^8.5.0",
|
|
180
|
+
"xdg-app-paths": "^8.3.0",
|
|
181
|
+
"yargs": "^17.7.2",
|
|
182
|
+
"yoga-layout": "file:../../vendor/yoga-layout-2.0.0-beta.1.tgz",
|
|
183
|
+
"@cloudflare/cli": "1.0.0",
|
|
184
|
+
"@cloudflare/pages-shared": "^0.11.1",
|
|
185
|
+
"@cloudflare/workers-tsconfig": "0.0.0"
|
|
186
|
+
},
|
|
187
|
+
"optionalDependencies": {
|
|
188
|
+
"fsevents": "~2.3.2"
|
|
189
|
+
},
|
|
190
|
+
"engines": {
|
|
191
|
+
"node": ">=16.17.0"
|
|
192
|
+
},
|
|
193
|
+
"scripts": {
|
|
194
|
+
"assert-git-version": "node -r esbuild-register scripts/assert-git-version.ts",
|
|
195
|
+
"build": "pnpm run clean && pnpm run bundle && pnpm run emit-types",
|
|
196
|
+
"bundle": "node -r esbuild-register scripts/bundle.ts",
|
|
197
|
+
"check:lint": "eslint .",
|
|
198
|
+
"check:type": "tsc",
|
|
199
|
+
"clean": "rimraf wrangler-dist miniflare-dist emitted-types",
|
|
200
|
+
"dev": "pnpm run clean && concurrently -c black,blue --kill-others-on-fail false \"pnpm run bundle --watch\" \"pnpm run check:type --watch --preserveWatchOutput\"",
|
|
201
|
+
"emit-types": "tsc -p tsconfig.emit.json && node -r esbuild-register scripts/emit-types.ts",
|
|
202
|
+
"start": "pnpm run bundle && cross-env NODE_OPTIONS=--enable-source-maps ./bin/wrangler.js",
|
|
203
|
+
"test": "pnpm run assert-git-version && jest",
|
|
204
|
+
"test:ci": "pnpm run test --coverage",
|
|
205
|
+
"test:debug": "pnpm run test --silent=false --verbose=true",
|
|
206
|
+
"test:e2e": "vitest --test-timeout 240000 --single-thread --dir ./e2e --retry 2 run",
|
|
207
|
+
"test:watch": "pnpm run test --testTimeout=50000 --watch",
|
|
208
|
+
"type:tests": "tsc -p ./src/__tests__/tsconfig.json && tsc -p ./e2e/tsconfig.json"
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const urls = new Set();
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
function checkURL(request, init) {
|
|
4
4
|
const url =
|
|
5
5
|
request instanceof URL
|
|
6
6
|
? request
|
|
@@ -19,5 +19,12 @@ export function checkedFetch(request, init) {
|
|
|
19
19
|
);
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
-
return globalThis.fetch(request, init);
|
|
23
22
|
}
|
|
23
|
+
|
|
24
|
+
globalThis.fetch = new Proxy(globalThis.fetch, {
|
|
25
|
+
apply(target, thisArg, argArray) {
|
|
26
|
+
const [request, init] = argArray;
|
|
27
|
+
checkURL(request, init);
|
|
28
|
+
return Reflect.apply(target, thisArg, argArray);
|
|
29
|
+
},
|
|
30
|
+
});
|
package/templates/facade.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
declare module "__ENTRY_POINT__" {
|
|
2
2
|
import type { Middleware } from "./middleware/common";
|
|
3
|
-
const worker: ExportedHandler & {
|
|
3
|
+
const worker: ExportedHandler & {
|
|
4
|
+
middleware?: Middleware[];
|
|
5
|
+
envWrappers: ((env: Record<string, unknown>) => Record<string, unknown>)[];
|
|
6
|
+
};
|
|
4
7
|
export default worker;
|
|
5
8
|
}
|
|
6
9
|
|
|
@@ -36,17 +36,68 @@ class __Facade_ScheduledController__ implements ScheduledController {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
const __facade_modules_fetch__:
|
|
40
|
-
|
|
39
|
+
const __facade_modules_fetch__: ExportedHandlerFetchHandler = function (
|
|
40
|
+
request,
|
|
41
|
+
env,
|
|
42
|
+
ctx
|
|
43
|
+
) {
|
|
44
|
+
if (worker.fetch === undefined)
|
|
45
|
+
throw new Error("Handler does not export a fetch() function.");
|
|
41
46
|
return worker.fetch(request, env, ctx);
|
|
42
47
|
};
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
|
|
49
|
+
function getMaskedEnv(rawEnv: unknown) {
|
|
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
|
+
}
|
|
55
|
+
}
|
|
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
|
+
|
|
90
|
+
fetch(request, rawEnv, ctx) {
|
|
91
|
+
const env = getMaskedEnv(rawEnv);
|
|
46
92
|
// Get the chain of middleware from the worker object
|
|
47
93
|
if (worker.middleware && worker.middleware.length > 0) {
|
|
48
|
-
|
|
49
|
-
|
|
94
|
+
// Make sure we only register middleware once:
|
|
95
|
+
// https://github.com/cloudflare/workers-sdk/issues/2386#issuecomment-1614715911
|
|
96
|
+
if (!registeredMiddleware) {
|
|
97
|
+
registeredMiddleware = true;
|
|
98
|
+
for (const middleware of worker.middleware) {
|
|
99
|
+
__facade_register__(middleware);
|
|
100
|
+
}
|
|
50
101
|
}
|
|
51
102
|
|
|
52
103
|
const __facade_modules_dispatch__: Dispatcher = function (type, init) {
|
|
@@ -71,14 +122,16 @@ const facade: ExportedHandler<unknown> = {
|
|
|
71
122
|
// We didn't have any middleware so we can skip the invocation chain,
|
|
72
123
|
// and just call the fetch handler directly
|
|
73
124
|
|
|
74
|
-
// We "don't care" if this is undefined as we want to have the same
|
|
125
|
+
// We "don't care" if this is undefined as we want to have the same behavior
|
|
75
126
|
// as if the worker completely bypassed middleware.
|
|
76
|
-
return
|
|
127
|
+
return __facade_modules_fetch__(request, env, ctx);
|
|
77
128
|
}
|
|
78
129
|
},
|
|
79
|
-
scheduled: worker.scheduled,
|
|
80
|
-
queue: worker.queue,
|
|
81
|
-
trace: worker.trace,
|
|
82
130
|
};
|
|
83
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);
|
|
135
|
+
}
|
|
136
|
+
|
|
84
137
|
export default facade;
|