wrangler 0.0.0-e178d6fb → 0.0.0-e1c2f5b9

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 (239) hide show
  1. package/README.md +16 -2
  2. package/bin/wrangler.js +31 -11
  3. package/miniflare-dist/index.mjs +1340 -330
  4. package/package.json +40 -13
  5. package/src/__tests__/access.test.ts +25 -0
  6. package/src/__tests__/api-dev.test.ts +238 -0
  7. package/src/__tests__/api-devregistry.test.js +64 -0
  8. package/src/__tests__/api.test.ts +80 -0
  9. package/src/__tests__/configuration.test.ts +683 -470
  10. package/src/__tests__/d1.test.ts +123 -0
  11. package/src/__tests__/delete.test.ts +263 -0
  12. package/src/__tests__/deployments.test.ts +105 -0
  13. package/src/__tests__/dev.test.tsx +453 -70
  14. package/src/__tests__/dialogs.test.tsx +40 -0
  15. package/src/__tests__/generate.test.ts +235 -0
  16. package/src/__tests__/get-host-from-url.test.ts +16 -0
  17. package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
  18. package/src/__tests__/helpers/mock-cfetch.ts +92 -26
  19. package/src/__tests__/helpers/mock-console.ts +11 -2
  20. package/src/__tests__/helpers/mock-dialogs.ts +1 -1
  21. package/src/__tests__/helpers/mock-get-zone-from-host.ts +8 -0
  22. package/src/__tests__/helpers/mock-known-routes.ts +7 -0
  23. package/src/__tests__/helpers/mock-kv.ts +1 -1
  24. package/src/__tests__/helpers/mock-oauth-flow.ts +74 -53
  25. package/src/__tests__/helpers/mock-process.ts +8 -13
  26. package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
  27. package/src/__tests__/helpers/msw/handlers/deployments.ts +59 -0
  28. package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
  29. package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
  30. package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
  31. package/src/__tests__/helpers/msw/handlers/script.ts +75 -0
  32. package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
  33. package/src/__tests__/helpers/msw/handlers/zones.ts +22 -0
  34. package/src/__tests__/helpers/msw/index.ts +21 -0
  35. package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
  36. package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
  37. package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
  38. package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
  39. package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
  40. package/src/__tests__/index.test.ts +103 -97
  41. package/src/__tests__/init.test.ts +1484 -382
  42. package/src/__tests__/jest.setup.ts +88 -5
  43. package/src/__tests__/kv.test.ts +655 -540
  44. package/src/__tests__/logout.test.ts +47 -0
  45. package/src/__tests__/metrics.test.ts +95 -45
  46. package/src/__tests__/middleware.scheduled.test.ts +145 -0
  47. package/src/__tests__/middleware.test.ts +750 -0
  48. package/src/__tests__/pages-deployment-tail.test.ts +884 -0
  49. package/src/__tests__/pages.test.ts +1301 -119
  50. package/src/__tests__/paths.test.ts +39 -0
  51. package/src/__tests__/publish.test.ts +2266 -1180
  52. package/src/__tests__/pubsub.test.ts +211 -88
  53. package/src/__tests__/queues.test.ts +522 -0
  54. package/src/__tests__/r2.test.ts +363 -203
  55. package/src/__tests__/secret.test.ts +261 -59
  56. package/src/__tests__/tail.test.ts +352 -151
  57. package/src/__tests__/test-old-node-version.js +3 -3
  58. package/src/__tests__/tsconfig-sanity.ts +12 -0
  59. package/src/__tests__/tsconfig.json +8 -0
  60. package/src/__tests__/tsconfig.tsbuildinfo +1 -0
  61. package/src/__tests__/type-generation.test.ts +233 -0
  62. package/src/__tests__/user.test.ts +59 -73
  63. package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
  64. package/src/__tests__/validate-dev-props.test.ts +56 -0
  65. package/src/__tests__/whoami.test.tsx +114 -66
  66. package/src/__tests__/worker-namespace.test.ts +154 -155
  67. package/src/api/dev.ts +170 -38
  68. package/src/api/index.ts +1 -0
  69. package/src/bundle-reporter.tsx +41 -2
  70. package/src/bundle.ts +668 -81
  71. package/src/cfetch/index.ts +40 -3
  72. package/src/cfetch/internal.ts +144 -32
  73. package/src/cli.ts +4 -3
  74. package/src/config/config.ts +27 -3
  75. package/src/config/environment.ts +131 -7
  76. package/src/config/index.ts +128 -8
  77. package/src/config/validation-helpers.ts +19 -6
  78. package/src/config/validation.ts +411 -14
  79. package/src/config-cache.ts +3 -2
  80. package/src/create-worker-preview.ts +31 -6
  81. package/src/create-worker-upload-form.ts +84 -22
  82. package/src/d1/backups.tsx +223 -0
  83. package/src/d1/constants.ts +2 -0
  84. package/src/d1/create.tsx +63 -0
  85. package/src/d1/delete.tsx +56 -0
  86. package/src/d1/execute.tsx +352 -0
  87. package/src/d1/formatTimeAgo.ts +14 -0
  88. package/src/d1/index.ts +101 -0
  89. package/src/d1/list.tsx +48 -0
  90. package/src/d1/migrations/apply.tsx +196 -0
  91. package/src/d1/migrations/create.tsx +77 -0
  92. package/src/d1/migrations/helpers.ts +147 -0
  93. package/src/d1/migrations/index.tsx +3 -0
  94. package/src/d1/migrations/list.tsx +79 -0
  95. package/src/d1/migrations/options.ts +18 -0
  96. package/src/d1/options.ts +22 -0
  97. package/src/d1/types.tsx +24 -0
  98. package/src/d1/utils.ts +48 -0
  99. package/src/delete.ts +108 -0
  100. package/src/deployments.ts +83 -0
  101. package/src/deprecated/index.ts +145 -0
  102. package/src/dev/dev-vars.ts +11 -8
  103. package/src/dev/dev.tsx +220 -58
  104. package/src/dev/get-local-persistence-path.tsx +31 -0
  105. package/src/dev/local.tsx +740 -181
  106. package/src/dev/remote.tsx +340 -123
  107. package/src/dev/start-server.ts +543 -0
  108. package/src/dev/use-esbuild.ts +61 -7
  109. package/src/dev/validate-dev-props.ts +31 -0
  110. package/src/dev-registry.tsx +201 -0
  111. package/src/dev.tsx +614 -258
  112. package/src/dialogs.tsx +41 -8
  113. package/src/{worker-namespace.ts → dispatch-namespace.ts} +23 -22
  114. package/src/docs/index.ts +97 -0
  115. package/src/entry.ts +3 -2
  116. package/src/environment-variables/factory.ts +88 -0
  117. package/src/environment-variables/misc-variables.ts +30 -0
  118. package/src/generate/index.ts +300 -0
  119. package/src/git-client.ts +100 -7
  120. package/src/index.tsx +249 -1560
  121. package/src/init.ts +626 -150
  122. package/src/inspect.ts +137 -32
  123. package/src/{metrics/is-ci.ts → is-ci.ts} +0 -0
  124. package/src/is-interactive.ts +4 -0
  125. package/src/jest.d.ts +4 -0
  126. package/src/{kv.ts → kv/helpers.ts} +3 -3
  127. package/src/kv/index.ts +596 -0
  128. package/src/logger.ts +27 -8
  129. package/src/metrics/index.ts +1 -0
  130. package/src/metrics/metrics-config.ts +4 -4
  131. package/src/metrics/metrics-dispatcher.ts +1 -0
  132. package/src/metrics/metrics-usage-headers.ts +24 -0
  133. package/src/metrics/send-event.ts +13 -8
  134. package/src/miniflare-cli/assets.ts +172 -465
  135. package/src/miniflare-cli/index.ts +149 -24
  136. package/src/miniflare-cli/tsconfig.json +9 -0
  137. package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
  138. package/src/miniflare-cli/types.ts +11 -0
  139. package/src/module-collection.ts +12 -3
  140. package/src/pages/build.tsx +108 -42
  141. package/src/pages/constants.ts +11 -0
  142. package/src/pages/deployment-tails.tsx +284 -0
  143. package/src/pages/deployments.tsx +9 -31
  144. package/src/pages/dev.tsx +417 -108
  145. package/src/pages/errors.ts +66 -0
  146. package/src/pages/functions/buildPlugin.ts +86 -84
  147. package/src/pages/functions/buildWorker.ts +121 -106
  148. package/src/pages/functions/routes-consolidation.test.ts +250 -0
  149. package/src/pages/functions/routes-consolidation.ts +73 -0
  150. package/src/pages/functions/routes-transformation.test.ts +282 -0
  151. package/src/pages/functions/routes-transformation.ts +115 -0
  152. package/src/pages/functions/routes-validation.test.ts +403 -0
  153. package/src/pages/functions/routes-validation.ts +202 -0
  154. package/src/pages/functions/tsconfig.json +8 -0
  155. package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
  156. package/src/pages/functions.tsx +82 -0
  157. package/src/pages/hash.tsx +13 -0
  158. package/src/pages/index.tsx +73 -55
  159. package/src/pages/projects.tsx +9 -3
  160. package/src/pages/prompt-select-project.tsx +31 -0
  161. package/src/pages/publish.tsx +183 -60
  162. package/src/pages/types.ts +1 -0
  163. package/src/pages/upload.tsx +107 -56
  164. package/src/pages/utils.ts +13 -2
  165. package/src/paths.ts +49 -4
  166. package/src/proxy.ts +375 -157
  167. package/src/publish/index.ts +270 -0
  168. package/src/{publish.ts → publish/publish.ts} +431 -188
  169. package/src/pubsub/pubsub-commands.tsx +4 -3
  170. package/src/queues/cli/commands/consumer/add.ts +71 -0
  171. package/src/queues/cli/commands/consumer/index.ts +22 -0
  172. package/src/queues/cli/commands/consumer/remove.ts +38 -0
  173. package/src/queues/cli/commands/create.ts +25 -0
  174. package/src/queues/cli/commands/delete.ts +26 -0
  175. package/src/queues/cli/commands/index.ts +36 -0
  176. package/src/queues/cli/commands/list.ts +25 -0
  177. package/src/queues/client.ts +135 -0
  178. package/src/queues/utils.ts +18 -0
  179. package/src/r2/constants.ts +4 -0
  180. package/src/r2/helpers.ts +131 -0
  181. package/src/r2/index.ts +289 -0
  182. package/src/routes.ts +140 -0
  183. package/src/secret/index.ts +379 -0
  184. package/src/sites.tsx +3 -3
  185. package/src/tail/createTail.ts +379 -0
  186. package/src/tail/filters.ts +4 -6
  187. package/src/tail/index.ts +199 -284
  188. package/src/tail/printing.ts +43 -3
  189. package/src/tsconfig-sanity.ts +16 -0
  190. package/src/type-generation.ts +181 -0
  191. package/src/user/access.ts +68 -0
  192. package/src/user/auth-variables.ts +113 -0
  193. package/src/user/choose-account.tsx +1 -1
  194. package/src/user/index.ts +0 -1
  195. package/src/user/user.tsx +116 -49
  196. package/src/utils/collectKeyValues.ts +14 -0
  197. package/src/whoami.tsx +80 -2
  198. package/src/worker.ts +69 -9
  199. package/src/yargs-types.ts +24 -0
  200. package/src/zones.ts +124 -9
  201. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  202. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  203. package/templates/__tests__/tsconfig.json +8 -0
  204. package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
  205. package/templates/d1-beta-facade.js +205 -0
  206. package/templates/facade.d.ts +14 -0
  207. package/templates/first-party-worker-module-facade.ts +19 -0
  208. package/templates/format-dev-errors.ts +33 -0
  209. package/templates/init-tests/test-jest-new-worker.js +23 -0
  210. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  211. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  212. package/templates/middleware/common.ts +62 -0
  213. package/templates/middleware/loader-modules.ts +82 -0
  214. package/templates/middleware/loader-sw.ts +233 -0
  215. package/templates/middleware/middleware-miniflare3-json-error.ts +20 -0
  216. package/templates/middleware/middleware-pretty-error.ts +40 -0
  217. package/templates/middleware/middleware-scheduled.ts +15 -0
  218. package/templates/new-worker.js +1 -1
  219. package/templates/pages-dev-pipeline.ts +31 -0
  220. package/templates/pages-dev-util.ts +52 -0
  221. package/templates/pages-shim.ts +4 -1
  222. package/templates/pages-template-plugin.ts +21 -5
  223. package/templates/pages-template-worker.ts +37 -9
  224. package/templates/{static-asset-facade.js → serve-static-assets.ts} +23 -7
  225. package/templates/service-bindings-module-facade.js +54 -0
  226. package/templates/service-bindings-sw-facade.js +42 -0
  227. package/templates/tsconfig-sanity.ts +11 -0
  228. package/templates/tsconfig.init.json +106 -0
  229. package/templates/tsconfig.json +5 -102
  230. package/templates/tsconfig.tsbuildinfo +1 -0
  231. package/wrangler-dist/cli.d.ts +621 -42
  232. package/wrangler-dist/cli.js +144441 -102606
  233. package/src/environment-variables.ts +0 -35
  234. package/src/faye-websocket.d.ts +0 -6
  235. package/src/generate.ts +0 -33
  236. package/src/miniflare-cli/enum-keys.ts +0 -17
  237. package/src/preview.ts +0 -31
  238. package/src/r2.ts +0 -50
  239. package/src/user/env-vars.ts +0 -46
package/src/bundle.ts CHANGED
@@ -5,44 +5,85 @@ import * as path from "node:path";
5
5
  import NodeGlobalsPolyfills from "@esbuild-plugins/node-globals-polyfill";
6
6
  import NodeModulesPolyfills from "@esbuild-plugins/node-modules-polyfill";
7
7
  import * as esbuild from "esbuild";
8
+ import tmp from "tmp-promise";
8
9
  import createModuleCollector from "./module-collection";
10
+ import { getBasePath, toUrlPath } from "./paths";
9
11
  import type { Config } from "./config";
12
+ import type { WorkerRegistry } from "./dev-registry";
10
13
  import type { Entry } from "./entry";
11
14
  import type { CfModule } from "./worker";
12
15
 
13
- type BundleResult = {
16
+ export type BundleResult = {
14
17
  modules: CfModule[];
18
+ dependencies: esbuild.Metafile["outputs"][string]["inputs"];
15
19
  resolvedEntryPointPath: string;
16
20
  bundleType: "esm" | "commonjs";
17
21
  stop: (() => void) | undefined;
22
+ sourceMapPath?: string | undefined;
18
23
  };
19
24
 
25
+ type StaticAssetsConfig =
26
+ | (Config["assets"] & {
27
+ bypassCache: boolean | undefined;
28
+ })
29
+ | undefined;
30
+
20
31
  /**
21
- * Searches for any uses of node's builtin modules, and throws an error if it
22
- * finds anything. This plugin is only used when nodeCompat is not enabled.
23
- * Supports both regular node builtins, and the new "node:<MODULE>" format.
32
+ * When applying the middleware facade for service workers, we need to inject
33
+ * some code at the top of the final output bundle. Applying an inject too early
34
+ * will allow esbuild to reorder the code. Additionally, we need to make sure
35
+ * user code is bundled in the final esbuild step with `watch` correctly
36
+ * configured, so code changes are detected.
37
+ *
38
+ * This type is used as the return type for the `MiddlewareFn` type representing
39
+ * a facade-applying function. Returned injects should be injected with the
40
+ * final esbuild step.
24
41
  */
25
- const checkForNodeBuiltinsPlugin = {
26
- name: "checkForNodeBuiltins",
27
- setup(build: esbuild.PluginBuild) {
28
- build.onResolve(
29
- {
30
- filter: new RegExp(
31
- "^(" +
32
- builtinModules.join("|") +
33
- "|" +
34
- builtinModules.map((module) => "node:" + module).join("|") +
35
- ")$"
36
- ),
37
- },
38
- () => {
39
- throw new Error(
40
- `Detected a Node builtin module import while Node compatibility is disabled.\nAdd node_compat = true to your wrangler.toml file to enable Node compatibility.`
41
- );
42
- }
43
- );
44
- },
45
- };
42
+ type EntryWithInject = Entry & { inject?: string[] };
43
+
44
+ /**
45
+ * RegExp matching against esbuild's error text when it is unable to resolve
46
+ * a Node built-in module. If we detect this when node_compat is disabled,
47
+ * we'll rewrite the error to suggest enabling it.
48
+ */
49
+ const nodeBuiltinResolveErrorText = new RegExp(
50
+ '^Could not resolve "(' +
51
+ builtinModules.join("|") +
52
+ "|" +
53
+ builtinModules.map((module) => "node:" + module).join("|") +
54
+ ')"$'
55
+ );
56
+
57
+ /**
58
+ * Returns true if the passed value looks like an esbuild BuildFailure object
59
+ */
60
+ export function isBuildFailure(err: unknown): err is esbuild.BuildFailure {
61
+ return (
62
+ typeof err === "object" &&
63
+ err !== null &&
64
+ "errors" in err &&
65
+ "warnings" in err
66
+ );
67
+ }
68
+
69
+ /**
70
+ * Rewrites esbuild BuildFailures for failing to resolve Node built-in modules
71
+ * to suggest enabling Node compat as opposed to `platform: "node"`.
72
+ */
73
+ export function rewriteNodeCompatBuildFailure(err: esbuild.BuildFailure) {
74
+ for (const error of err.errors) {
75
+ const match = nodeBuiltinResolveErrorText.exec(error.text);
76
+ if (match !== null) {
77
+ error.notes = [
78
+ {
79
+ location: null,
80
+ text: `The package "${match[1]}" wasn't found on the file system but is built into node.
81
+ Add "node_compat = true" to your wrangler.toml file to enable Node compatibility.`,
82
+ },
83
+ ];
84
+ }
85
+ }
86
+ }
46
87
 
47
88
  /**
48
89
  * Generate a bundle for the worker identified by the arguments passed in.
@@ -52,19 +93,36 @@ export async function bundleWorker(
52
93
  destination: string,
53
94
  options: {
54
95
  serveAssetsFromWorker: boolean;
55
- jsxFactory: string | undefined;
56
- jsxFragment: string | undefined;
96
+ assets?: StaticAssetsConfig;
97
+ betaD1Shims?: string[];
98
+ jsxFactory?: string;
99
+ jsxFragment?: string;
57
100
  rules: Config["rules"];
58
- watch?: esbuild.WatchMode;
59
- tsconfig: string | undefined;
60
- minify: boolean | undefined;
61
- nodeCompat: boolean | undefined;
101
+ watch?: esbuild.WatchMode | boolean;
102
+ tsconfig?: string;
103
+ minify?: boolean;
104
+ nodeCompat?: boolean;
62
105
  define: Config["define"];
63
106
  checkFetch: boolean;
107
+ services?: Config["services"];
108
+ workerDefinitions?: WorkerRegistry;
109
+ firstPartyWorkerDevFacade?: boolean;
110
+ targetConsumer: "dev" | "publish";
111
+ local: boolean;
112
+ testScheduled?: boolean;
113
+ experimentalLocal?: boolean;
114
+ inject?: string[];
115
+ loader?: Record<string, string>;
116
+ sourcemap?: esbuild.CommonOptions["sourcemap"];
117
+ plugins?: esbuild.Plugin[];
118
+ // TODO: Rip these out https://github.com/cloudflare/wrangler2/issues/2153
119
+ disableModuleCollection?: boolean;
120
+ isOutfile?: boolean;
64
121
  }
65
122
  ): Promise<BundleResult> {
66
123
  const {
67
124
  serveAssetsFromWorker,
125
+ betaD1Shims,
68
126
  jsxFactory,
69
127
  jsxFragment,
70
128
  rules,
@@ -73,9 +131,29 @@ export async function bundleWorker(
73
131
  minify,
74
132
  nodeCompat,
75
133
  checkFetch,
134
+ local,
135
+ assets,
136
+ workerDefinitions,
137
+ services,
138
+ firstPartyWorkerDevFacade,
139
+ targetConsumer,
140
+ testScheduled,
141
+ experimentalLocal,
142
+ inject: injectOption,
143
+ loader,
144
+ sourcemap,
145
+ plugins,
146
+ disableModuleCollection,
147
+ isOutfile,
76
148
  } = options;
149
+
150
+ // We create a temporary directory for any oneoff files we
151
+ // need to create. This is separate from the main build
152
+ // directory (`destination`).
153
+ const tmpDir = await tmp.dir({ unsafeCleanup: true });
154
+
77
155
  const entryDirectory = path.dirname(entry.file);
78
- const moduleCollector = createModuleCollector({
156
+ let moduleCollector = createModuleCollector({
79
157
  wrangler1xlegacyModuleReferences: {
80
158
  rootDirectory: entryDirectory,
81
159
  fileNames: new Set(
@@ -91,41 +169,167 @@ export async function bundleWorker(
91
169
  format: entry.format,
92
170
  rules,
93
171
  });
172
+ if (disableModuleCollection) {
173
+ moduleCollector = {
174
+ modules: [],
175
+ plugin: {
176
+ name: moduleCollector.plugin.name,
177
+ setup: () => {},
178
+ },
179
+ };
180
+ }
94
181
 
95
182
  // In dev, we want to patch `fetch()` with a special version that looks
96
183
  // for bad usages and can warn the user about them; so we inject
97
184
  // `checked-fetch.js` to do so. However, with yarn 3 style pnp,
98
185
  // we need to extract that file to an accessible place before injecting
99
186
  // it in, hence this code here.
100
- const checkedFetchFileToInject = path.join(
101
- destination,
102
- "--temp-wrangler-files--",
103
- "checked-fetch.js"
104
- );
105
187
 
106
- if (checkFetch) {
107
- fs.mkdirSync(path.join(destination, "--temp-wrangler-files--"), {
188
+ const checkedFetchFileToInject = path.join(tmpDir.path, "checked-fetch.js");
189
+
190
+ if (checkFetch && !fs.existsSync(checkedFetchFileToInject)) {
191
+ fs.mkdirSync(tmpDir.path, {
108
192
  recursive: true,
109
193
  });
110
194
  fs.writeFileSync(
111
195
  checkedFetchFileToInject,
112
- fs.readFileSync(path.resolve(__dirname, "../templates/checked-fetch.js"))
196
+ fs.readFileSync(path.resolve(getBasePath(), "templates/checked-fetch.js"))
113
197
  );
114
198
  }
115
- // TODO: we need to have similar logic like above for other files
116
- // like the static asset facade, and other middleware that we
117
- // plan on injecting/referencing.
118
199
 
119
- const result = await esbuild.build({
120
- ...getEntryPoint(entry.file, serveAssetsFromWorker),
200
+ // At this point, we take the opportunity to "wrap" any input workers
201
+ // with any extra functionality we may want to add. This is done by
202
+ // passing the entry point through a pipeline of functions that return
203
+ // a new entry point, that we call "middleware" or "facades".
204
+ // Look at implementations of these functions to learn more.
205
+
206
+ // We also have middleware that uses a more "traditional" middleware stack,
207
+ // which is all loaded as one in a stack.
208
+ const middlewareToLoad: MiddlewareLoader[] = [];
209
+
210
+ if (testScheduled) {
211
+ middlewareToLoad.push({
212
+ path: "templates/middleware/middleware-scheduled.ts",
213
+ });
214
+ }
215
+ if (experimentalLocal) {
216
+ // In Miniflare 3, we bind the user's worker as a service binding in a
217
+ // special entry worker that handles things like injecting `Request.cf`,
218
+ // live-reload, and the pretty-error page.
219
+ //
220
+ // Unfortunately, due to a bug in `workerd`, errors thrown asynchronously by
221
+ // native APIs don't have `stack`s. This means Miniflare can't extract the
222
+ // `stack` trace from dispatching to the user worker service binding by
223
+ // `try/catch`.
224
+ //
225
+ // As a stop-gap solution, if the `MF-Experimental-Error-Stack` header is
226
+ // truthy on responses, the body will be interpreted as a JSON-error of the
227
+ // form `{ message?: string, name?: string, stack?: string }`.
228
+ //
229
+ // This middleware wraps the user's worker in a `try/catch`, and rewrites
230
+ // errors in this format so a pretty-error page can be shown.
231
+ middlewareToLoad.push({
232
+ path: "templates/middleware/middleware-miniflare3-json-error.ts",
233
+ dev: true,
234
+ });
235
+ }
236
+
237
+ type MiddlewareFn = (currentEntry: Entry) => Promise<EntryWithInject>;
238
+ const middleware: (false | undefined | MiddlewareFn)[] = [
239
+ // serve static assets
240
+ serveAssetsFromWorker &&
241
+ ((currentEntry: Entry) => {
242
+ return applyStaticAssetFacade(currentEntry, tmpDir.path, assets);
243
+ }),
244
+ // format errors nicely
245
+ // We use an env var here because we don't actually
246
+ // want to expose this to the user. It's only used internally to
247
+ // experiment with middleware as a teaching exercise.
248
+ process.env.FORMAT_WRANGLER_ERRORS === "true" &&
249
+ ((currentEntry: Entry) => {
250
+ return applyFormatDevErrorsFacade(currentEntry, tmpDir.path);
251
+ }),
252
+ // bind to other dev instances/service bindings
253
+ workerDefinitions &&
254
+ Object.keys(workerDefinitions).length > 0 &&
255
+ services &&
256
+ services.length > 0 &&
257
+ ((currentEntry: Entry) => {
258
+ return applyMultiWorkerDevFacade(
259
+ currentEntry,
260
+ tmpDir.path,
261
+ services,
262
+ workerDefinitions
263
+ );
264
+ }),
265
+ // Simulate internal environment when using first party workers in dev
266
+ firstPartyWorkerDevFacade === true &&
267
+ ((currentEntry: Entry) => {
268
+ return applyFirstPartyWorkerDevFacade(currentEntry, tmpDir.path);
269
+ }),
270
+
271
+ Array.isArray(betaD1Shims) &&
272
+ betaD1Shims.length > 0 &&
273
+ ((currentEntry: Entry) => {
274
+ return applyD1BetaFacade(currentEntry, tmpDir.path, betaD1Shims, local);
275
+ }),
276
+
277
+ // Middleware loader: to add middleware, we add the path to the middleware
278
+ // Currently for demonstration purposes we have two example middlewares
279
+ // Middlewares are togglable by changing the `publish` (default=false) and `dev` (default=true) options
280
+ // As we are not yet supporting user created middlewares yet, if no wrangler applied middleware
281
+ // are found, we will not load any middleware. We also need to check if there are middlewares compatible with
282
+ // the target consumer (dev / publish).
283
+ (middlewareToLoad.filter(
284
+ (m) =>
285
+ (m.publish && targetConsumer === "publish") ||
286
+ (m.dev !== false && targetConsumer === "dev")
287
+ ).length > 0 ||
288
+ process.env.EXPERIMENTAL_MIDDLEWARE === "true") &&
289
+ ((currentEntry: Entry) => {
290
+ return applyMiddlewareLoaderFacade(
291
+ currentEntry,
292
+ tmpDir.path,
293
+ middlewareToLoad.filter(
294
+ // We dynamically filter the middleware depending on where we are bundling for
295
+ (m) =>
296
+ (targetConsumer === "dev" && m.dev !== false) ||
297
+ (m.publish && targetConsumer === "publish")
298
+ )
299
+ );
300
+ }),
301
+ ].filter(Boolean);
302
+
303
+ const inject: string[] = injectOption ?? [];
304
+ if (checkFetch) inject.push(checkedFetchFileToInject);
305
+
306
+ let inputEntry: EntryWithInject = entry;
307
+ for (const middlewareFn of middleware as MiddlewareFn[]) {
308
+ inputEntry = await middlewareFn(inputEntry);
309
+ if (inputEntry.inject !== undefined) inject.push(...inputEntry.inject);
310
+ }
311
+
312
+ // At this point, inputEntry points to the entry point we want to build.
313
+
314
+ const buildOptions: esbuild.BuildOptions & { metafile: true } = {
315
+ entryPoints: [inputEntry.file],
121
316
  bundle: true,
122
317
  absWorkingDir: entry.directory,
123
318
  outdir: destination,
124
- inject: checkFetch ? [checkedFetchFileToInject] : [],
319
+ ...(isOutfile
320
+ ? {
321
+ outdir: undefined,
322
+ outfile: destination,
323
+ }
324
+ : {}),
325
+ inject,
125
326
  external: ["__STATIC_CONTENT_MANIFEST"],
126
327
  format: entry.format === "modules" ? "esm" : "iife",
127
328
  target: "es2020",
128
- sourcemap: true,
329
+ sourcemap: sourcemap ?? true, // this needs to use ?? to accept false
330
+ // Include a reference to the output folder in the sourcemap.
331
+ // This is omitted by default, but we need it to properly resolve source paths in error output.
332
+ sourceRoot: destination,
129
333
  minify,
130
334
  metafile: true,
131
335
  conditions: ["worker", "browser"],
@@ -143,20 +347,31 @@ export async function bundleWorker(
143
347
  ".js": "jsx",
144
348
  ".mjs": "jsx",
145
349
  ".cjs": "jsx",
350
+ ...(loader || {}),
146
351
  },
147
352
  plugins: [
148
353
  moduleCollector.plugin,
149
354
  ...(nodeCompat
150
355
  ? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
151
- : // we use checkForNodeBuiltinsPlugin to throw a nicer error
152
- // if we find node builtins when nodeCompat isn't turned on
153
- [checkForNodeBuiltinsPlugin]),
356
+ : []),
357
+ ...(plugins || []),
154
358
  ],
155
359
  ...(jsxFactory && { jsxFactory }),
156
360
  ...(jsxFragment && { jsxFragment }),
157
361
  ...(tsconfig && { tsconfig }),
158
362
  watch,
159
- });
363
+ // The default logLevel is "warning". So that we can rewrite errors before
364
+ // logging, we disable esbuild's default logging, and log build failures
365
+ // ourselves.
366
+ logLevel: "silent",
367
+ };
368
+ let result;
369
+ try {
370
+ result = await esbuild.build(buildOptions);
371
+ } catch (e) {
372
+ if (!nodeCompat && isBuildFailure(e)) rewriteNodeCompatBuildFailure(e);
373
+ throw e;
374
+ }
160
375
 
161
376
  const entryPointOutputs = Object.entries(result.metafile.outputs).filter(
162
377
  ([_path, output]) => output.entryPoint !== undefined
@@ -172,56 +387,428 @@ export async function bundleWorker(
172
387
  listEntryPoints(entryPointOutputs)
173
388
  );
174
389
 
175
- const entryPointExports = entryPointOutputs[0][1].exports;
390
+ const { exports: entryPointExports, inputs: dependencies } =
391
+ entryPointOutputs[0][1];
176
392
  const bundleType = entryPointExports.length > 0 ? "esm" : "commonjs";
177
393
 
394
+ const sourceMapPath = Object.keys(result.metafile.outputs).filter((_path) =>
395
+ _path.includes(".map")
396
+ )[0];
397
+
178
398
  return {
179
399
  modules: moduleCollector.modules,
400
+ dependencies,
180
401
  resolvedEntryPointPath: path.resolve(
181
402
  entry.directory,
182
403
  entryPointOutputs[0][0]
183
404
  ),
184
405
  bundleType,
185
406
  stop: result.stop,
407
+ sourceMapPath,
186
408
  };
187
409
  }
188
410
 
189
- type EntryPoint = { stdin: esbuild.StdinOptions } | { entryPoints: string[] };
411
+ /**
412
+ * A simple plugin to alias modules and mark them as external
413
+ */
414
+ export function esbuildAliasExternalPlugin(
415
+ aliases: Record<string, string>
416
+ ): esbuild.Plugin {
417
+ return {
418
+ name: "alias",
419
+ setup(build) {
420
+ build.onResolve({ filter: /.*/g }, (args) => {
421
+ // If it's the entrypoint, let it be as is
422
+ if (args.kind === "entry-point") {
423
+ return {
424
+ path: args.path,
425
+ };
426
+ }
427
+ // If it's not a recognised alias, then throw an error
428
+ if (!Object.keys(aliases).includes(args.path)) {
429
+ throw new Error("unrecognized module: " + args.path);
430
+ }
431
+
432
+ // Otherwise, return the alias
433
+ return {
434
+ path: aliases[args.path as keyof typeof aliases],
435
+ external: true,
436
+ };
437
+ });
438
+ },
439
+ };
440
+ }
190
441
 
191
442
  /**
192
- * Create an object that describes the entry point for esbuild.
193
- *
194
- * If we are using the experimental asset handling, then the entry point is
195
- * actually a shim worker that will either return an asset from a KV store,
196
- * or delegate to the actual worker.
443
+ * A middleware that catches any thrown errors, and instead formats
444
+ * them to be rendered in a browser. This middleware is for demonstration
445
+ * purposes only, and is not intended to be used in production (or even dev!)
197
446
  */
198
- function getEntryPoint(
199
- entryFile: string,
200
- serveAssetsFromWorker: boolean
201
- ): EntryPoint {
202
- if (serveAssetsFromWorker) {
203
- return {
204
- stdin: {
205
- contents: fs
206
- .readFileSync(
207
- path.join(__dirname, "../templates/static-asset-facade.js"),
208
- "utf8"
209
- )
210
- // on windows, escape backslashes in the path (`\`)
211
- .replace("__ENTRY_POINT__", entryFile.replaceAll("\\", "\\\\"))
212
- .replace(
213
- "__KV_ASSET_HANDLER__",
214
- path
215
- .join(__dirname, "../kv-asset-handler.js")
216
- .replaceAll("\\", "\\\\")
447
+ async function applyFormatDevErrorsFacade(
448
+ entry: Entry,
449
+ tmpDirPath: string
450
+ ): Promise<Entry> {
451
+ const targetPath = path.join(tmpDirPath, "format-dev-errors.entry.js");
452
+ await esbuild.build({
453
+ entryPoints: [
454
+ path.resolve(getBasePath(), "templates/format-dev-errors.ts"),
455
+ ],
456
+ bundle: true,
457
+ sourcemap: true,
458
+ format: "esm",
459
+ plugins: [
460
+ esbuildAliasExternalPlugin({
461
+ __ENTRY_POINT__: entry.file,
462
+ }),
463
+ ],
464
+ outfile: targetPath,
465
+ });
466
+
467
+ return {
468
+ ...entry,
469
+ file: targetPath,
470
+ };
471
+ }
472
+
473
+ /**
474
+ * A facade that acts as a "middleware loader".
475
+ * Instead of needing to apply a facade for each individual middleware, this allows
476
+ * middleware to be written in a more traditional manner and then be applied all
477
+ * at once, requiring just two esbuild steps, rather than 1 per middleware.
478
+ */
479
+
480
+ interface MiddlewareLoader {
481
+ path: string;
482
+ // By default all middleware will run on dev, but will not be run when published
483
+ publish?: boolean;
484
+ dev?: boolean;
485
+ }
486
+
487
+ async function applyMiddlewareLoaderFacade(
488
+ entry: Entry,
489
+ tmpDirPath: string,
490
+ middleware: MiddlewareLoader[] // a list of paths to middleware files
491
+ ): Promise<EntryWithInject> {
492
+ // Firstly we need to insert the middleware array into the project,
493
+ // and then we load the middleware - this insertion and loading is
494
+ // different for each format.
495
+
496
+ // Make sure we resolve all files relative to the actual temporary directory,
497
+ // otherwise we'll have issues with source maps
498
+ tmpDirPath = fs.realpathSync(tmpDirPath);
499
+
500
+ const targetPathInsertion = path.join(
501
+ tmpDirPath,
502
+ "middleware-insertion.entry.js"
503
+ );
504
+
505
+ // We need to import each of the middlewares, so we need to generate a
506
+ // random, unique identifier that we can use for the import.
507
+ // Middlewares are required to be default exports so we can import to any name.
508
+ const middlewareIdentifiers = middleware.map(
509
+ (_, index) => `__MIDDLEWARE_${index}__`
510
+ );
511
+
512
+ const dynamicFacadePath = path.join(
513
+ tmpDirPath,
514
+ "middleware-insertion-facade.js"
515
+ );
516
+
517
+ if (entry.format === "modules") {
518
+ // We use a facade to expose the required middleware alongside any user defined
519
+ // middleware on the worker object
520
+
521
+ const imports = middlewareIdentifiers
522
+ .map((m) => `import ${m} from "${m}";`)
523
+ .join("\n");
524
+
525
+ // write a file with all of the imports required
526
+ fs.writeFileSync(
527
+ dynamicFacadePath,
528
+ `import worker from "__ENTRY_POINT__";
529
+ ${imports}
530
+ const facade = {
531
+ ...worker,
532
+ middleware: [
533
+ ${middlewareIdentifiers.join(",")}${middlewareIdentifiers.length > 0 ? "," : ""}
534
+ ...(worker.middleware ? worker.middleware : []),
535
+ ]
536
+ }
537
+ export * from "__ENTRY_POINT__";
538
+ export default facade;`
539
+ );
540
+
541
+ await esbuild.build({
542
+ entryPoints: [dynamicFacadePath],
543
+ bundle: true,
544
+ sourcemap: true,
545
+ format: "esm",
546
+ plugins: [
547
+ esbuildAliasExternalPlugin({
548
+ __ENTRY_POINT__: entry.file,
549
+ ...Object.fromEntries(
550
+ middleware.map((val, index) => [
551
+ middlewareIdentifiers[index],
552
+ toUrlPath(path.resolve(getBasePath(), val.path)),
553
+ ])
217
554
  ),
218
- sourcefile: "static-asset-facade.js",
219
- resolveDir: path.dirname(entryFile),
220
- },
555
+ }),
556
+ ],
557
+ outfile: targetPathInsertion,
558
+ });
559
+
560
+ let targetPathLoader = path.join(tmpDirPath, path.basename(entry.file));
561
+ if (path.extname(entry.file) === "") targetPathLoader += ".js";
562
+ const loaderPath = path.resolve(
563
+ getBasePath(),
564
+ "templates/middleware/loader-modules.ts"
565
+ );
566
+ await esbuild.build({
567
+ entryPoints: [loaderPath],
568
+ bundle: true,
569
+ sourcemap: true,
570
+ format: "esm",
571
+ plugins: [
572
+ esbuildAliasExternalPlugin({
573
+ __ENTRY_POINT__: targetPathInsertion,
574
+ "./common": path.resolve(
575
+ getBasePath(),
576
+ "templates/middleware/common.ts"
577
+ ),
578
+ }),
579
+ ],
580
+ outfile: targetPathLoader,
581
+ });
582
+ return {
583
+ ...entry,
584
+ file: targetPathLoader,
221
585
  };
222
586
  } else {
223
- return { entryPoints: [entryFile] };
587
+ const imports = middlewareIdentifiers
588
+ .map((m) => `import ${m} from "${m}";`)
589
+ .join("\n");
590
+ const contents = `import { __facade_registerInternal__ } from "__LOADER__";
591
+ ${imports}
592
+ __facade_registerInternal__([${middlewareIdentifiers.join(",")}]);`;
593
+ fs.writeFileSync(dynamicFacadePath, contents);
594
+
595
+ await esbuild.build({
596
+ entryPoints: [dynamicFacadePath],
597
+ bundle: true,
598
+ sourcemap: true,
599
+ format: "iife",
600
+ plugins: [
601
+ {
602
+ name: "dynamic-facade-imports",
603
+ setup(build) {
604
+ build.onResolve({ filter: /^__LOADER__$/ }, () => {
605
+ const loaderPath = path.resolve(
606
+ getBasePath(),
607
+ "templates/middleware/loader-sw.ts"
608
+ );
609
+ return { path: loaderPath };
610
+ });
611
+ const middlewareFilter = /^__MIDDLEWARE_(\d+)__$/;
612
+ build.onResolve({ filter: middlewareFilter }, (args) => {
613
+ const match = middlewareFilter.exec(args.path);
614
+ assert(match !== null);
615
+ const middlewareIndex = parseInt(match[1]);
616
+ return {
617
+ path: path.resolve(
618
+ getBasePath(),
619
+ middleware[middlewareIndex].path
620
+ ),
621
+ };
622
+ });
623
+ },
624
+ },
625
+ ],
626
+ outfile: targetPathInsertion,
627
+ });
628
+ return {
629
+ ...entry,
630
+ inject: [targetPathInsertion],
631
+ };
632
+ }
633
+ }
634
+
635
+ /**
636
+ * A middleware that serves static assets from a worker.
637
+ * This powers --assets / config.assets
638
+ */
639
+
640
+ async function applyStaticAssetFacade(
641
+ entry: Entry,
642
+ tmpDirPath: string,
643
+ assets: StaticAssetsConfig
644
+ ): Promise<Entry> {
645
+ const targetPath = path.join(tmpDirPath, "serve-static-assets.entry.js");
646
+
647
+ await esbuild.build({
648
+ entryPoints: [
649
+ path.resolve(getBasePath(), "templates/serve-static-assets.ts"),
650
+ ],
651
+ bundle: true,
652
+ format: "esm",
653
+ sourcemap: true,
654
+ plugins: [
655
+ esbuildAliasExternalPlugin({
656
+ __ENTRY_POINT__: entry.file,
657
+ __KV_ASSET_HANDLER__: path.join(getBasePath(), "kv-asset-handler.js"),
658
+ __STATIC_CONTENT_MANIFEST: "__STATIC_CONTENT_MANIFEST",
659
+ }),
660
+ ],
661
+ define: {
662
+ __CACHE_CONTROL_OPTIONS__: JSON.stringify(
663
+ typeof assets === "object"
664
+ ? {
665
+ browserTTL:
666
+ assets.browser_TTL || 172800 /* 2 days: 2* 60 * 60 * 24 */,
667
+ bypassCache: assets.bypassCache,
668
+ }
669
+ : {}
670
+ ),
671
+ __SERVE_SINGLE_PAGE_APP__: JSON.stringify(
672
+ typeof assets === "object" ? assets.serve_single_page_app : false
673
+ ),
674
+ },
675
+ outfile: targetPath,
676
+ });
677
+
678
+ return {
679
+ ...entry,
680
+ file: targetPath,
681
+ };
682
+ }
683
+
684
+ /**
685
+ * A middleware that enables service bindings to be used in dev,
686
+ * binding to other love wrangler dev instances
687
+ */
688
+
689
+ async function applyMultiWorkerDevFacade(
690
+ entry: Entry,
691
+ tmpDirPath: string,
692
+ services: Config["services"],
693
+ workerDefinitions: WorkerRegistry
694
+ ) {
695
+ const targetPath = path.join(tmpDirPath, "multiworker-dev-facade.entry.js");
696
+ const serviceMap = Object.fromEntries(
697
+ (services || []).map((serviceBinding) => [
698
+ serviceBinding.binding,
699
+ workerDefinitions[serviceBinding.service] || null,
700
+ ])
701
+ );
702
+
703
+ await esbuild.build({
704
+ entryPoints: [
705
+ path.join(
706
+ getBasePath(),
707
+ entry.format === "modules"
708
+ ? "templates/service-bindings-module-facade.js"
709
+ : "templates/service-bindings-sw-facade.js"
710
+ ),
711
+ ],
712
+ bundle: true,
713
+ sourcemap: true,
714
+ format: "esm",
715
+ plugins: [
716
+ esbuildAliasExternalPlugin({
717
+ __ENTRY_POINT__: entry.file,
718
+ }),
719
+ ],
720
+ define: {
721
+ __WORKERS__: JSON.stringify(serviceMap),
722
+ },
723
+ outfile: targetPath,
724
+ });
725
+
726
+ return {
727
+ ...entry,
728
+ file: targetPath,
729
+ };
730
+ }
731
+
732
+ /**
733
+ * A middleware that makes first party workers "work" in
734
+ * our dev environments. Is applied during wrangler dev
735
+ * when config.first_party_worker is true
736
+ */
737
+ async function applyFirstPartyWorkerDevFacade(
738
+ entry: Entry,
739
+ tmpDirPath: string
740
+ ) {
741
+ if (entry.format !== "modules") {
742
+ throw new Error(
743
+ "First party workers must be in the modules format. See https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/"
744
+ );
224
745
  }
746
+
747
+ const targetPath = path.join(
748
+ tmpDirPath,
749
+ "first-party-worker-module-facade.entry.js"
750
+ );
751
+
752
+ await esbuild.build({
753
+ entryPoints: [
754
+ path.resolve(
755
+ getBasePath(),
756
+ "templates/first-party-worker-module-facade.ts"
757
+ ),
758
+ ],
759
+ bundle: true,
760
+ format: "esm",
761
+ sourcemap: true,
762
+ plugins: [
763
+ esbuildAliasExternalPlugin({
764
+ __ENTRY_POINT__: entry.file,
765
+ }),
766
+ ],
767
+ outfile: targetPath,
768
+ });
769
+
770
+ return {
771
+ ...entry,
772
+ file: targetPath,
773
+ };
774
+ }
775
+
776
+ /**
777
+ * A middleware that injects the beta D1 API in JS.
778
+ *
779
+ * This code be removed from here when the API is in Workers core,
780
+ * but moved inside Miniflare for simulating D1.
781
+ */
782
+
783
+ async function applyD1BetaFacade(
784
+ entry: Entry,
785
+ tmpDirPath: string,
786
+ betaD1Shims: string[],
787
+ local: boolean
788
+ ): Promise<Entry> {
789
+ const targetPath = path.join(tmpDirPath, "d1-beta-facade.entry.js");
790
+
791
+ await esbuild.build({
792
+ entryPoints: [path.resolve(getBasePath(), "templates/d1-beta-facade.js")],
793
+ bundle: true,
794
+ format: "esm",
795
+ sourcemap: true,
796
+ plugins: [
797
+ esbuildAliasExternalPlugin({
798
+ __ENTRY_POINT__: entry.file,
799
+ }),
800
+ ],
801
+ define: {
802
+ __D1_IMPORTS__: JSON.stringify(betaD1Shims),
803
+ __LOCAL_MODE__: JSON.stringify(local),
804
+ },
805
+ outfile: targetPath,
806
+ });
807
+
808
+ return {
809
+ ...entry,
810
+ file: targetPath,
811
+ };
225
812
  }
226
813
 
227
814
  /**