wrangler 0.0.0-e72a5794 → 0.0.0-e75c5812f

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 (33) hide show
  1. package/README.md +1 -1
  2. package/bin/wrangler.js +5 -28
  3. package/config-schema.json +3067 -0
  4. package/package.json +197 -195
  5. package/templates/checked-fetch.js +1 -1
  6. package/templates/facade.d.ts +7 -5
  7. package/templates/gitignore +2 -4
  8. package/templates/init-tests/test-vitest-new-worker.js +1 -1
  9. package/templates/init-tests/test-vitest-new-worker.ts +1 -1
  10. package/templates/middleware/loader-modules.ts +92 -95
  11. package/templates/middleware/loader-sw.ts +4 -3
  12. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  13. package/templates/middleware/middleware-serve-static-assets.ts +5 -5
  14. package/templates/modules-watch-stub.js +4 -0
  15. package/templates/no-op-assets-worker.ts +33 -0
  16. package/templates/pages-dev-pipeline.ts +2 -1
  17. package/templates/pages-template-plugin.ts +2 -2
  18. package/templates/pages-template-worker.ts +1 -1
  19. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  20. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  21. package/templates/tsconfig.init.json +4 -87
  22. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  23. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  24. package/wrangler-dist/ProxyWorker.js +241 -0
  25. package/wrangler-dist/ProxyWorker.js.map +6 -0
  26. package/wrangler-dist/cli.d.ts +24878 -212
  27. package/wrangler-dist/cli.js +156427 -93691
  28. package/wrangler-dist/wasm-sync.wasm +0 -0
  29. package/Cloudflare_CA.pem +0 -18
  30. package/templates/middleware/middleware-d1-beta.d.ts +0 -3
  31. package/templates/middleware/middleware-d1-beta.ts +0 -244
  32. package/templates/middleware/middleware-multiworker-dev.d.ts +0 -4
  33. package/templates/middleware/middleware-multiworker-dev.ts +0 -59
@@ -1,23 +1,17 @@
1
- // // This loads all middlewares exposed on the middleware object
2
- // // and then starts the invocation chain.
3
- // // The big idea is that we can add these to the middleware export dynamically
4
- // // through wrangler, or we can potentially let users directly add them as a sort
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
- Dispatcher,
9
- Middleware,
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
- import worker from "__ENTRY_POINT__";
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
- 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.");
46
- return worker.fetch(request, env, ctx);
47
- };
48
-
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
- }
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
- fetch(request, rawEnv, ctx) {
91
- const env = getMaskedEnv(rawEnv);
92
- // Get the chain of middleware from the worker object
93
- if (worker.middleware && worker.middleware.length > 0) {
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
- }
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
- const __facade_modules_dispatch__: Dispatcher = function (type, init) {
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
- __facade_modules_dispatch__,
119
- __facade_modules_fetch__
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
- export default facade;
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;
@@ -1,18 +1,18 @@
1
1
  /// <reference path="middleware-serve-static-assets.d.ts"/>
2
2
 
3
+ import manifest from "__STATIC_CONTENT_MANIFEST";
3
4
  import {
4
5
  getAssetFromKV,
5
- NotFoundError,
6
6
  MethodNotAllowedError,
7
+ NotFoundError,
7
8
  serveSinglePageApp,
8
9
  } from "@cloudflare/kv-asset-handler";
10
+ import { cacheControl, spaMode } from "config:middleware/serve-static-assets";
11
+ import type { Middleware } from "./common";
9
12
  import type { Options } from "@cloudflare/kv-asset-handler";
10
- import { spaMode, cacheControl } from "config:middleware/serve-static-assets";
11
13
  import type * as kvAssetHandler from "@cloudflare/kv-asset-handler";
12
- import manifest from "__STATIC_CONTENT_MANIFEST";
13
- const ASSET_MANIFEST = JSON.parse(manifest);
14
14
 
15
- import type { Middleware } from "./common";
15
+ const ASSET_MANIFEST = JSON.parse(manifest);
16
16
 
17
17
  const staticAssets: Middleware = async (request, env, _ctx, middlewareCtx) => {
18
18
  let options: Partial<Options> = {
@@ -0,0 +1,4 @@
1
+ // `esbuild` doesn't support returning `watch*` options from `onStart()`
2
+ // plugin callbacks. Instead, we define an empty virtual module that is
3
+ // imported by this injected file. Importing the module registers watchers.
4
+ import "wrangler:modules-watch";
@@ -0,0 +1,33 @@
1
+ /**
2
+ * This Worker is used as a default entry-point for Assets-only
3
+ * Workers. It proxies the request directly on to the Asset Sever
4
+ * Worker service binding.
5
+ *
6
+ * In an Assets-only Workers world, we want to enable users
7
+ * to deploy a Worker with Assets without ever having to provide
8
+ * a User Worker.
9
+ *
10
+ * ```bash
11
+ * wrangler dev --experimental-assets dist
12
+ * wrangler deploy --experimental-assets dist
13
+ * ```
14
+ *
15
+ * ```toml
16
+ * name = "assets-only-worker"
17
+ * compatibility_date = "2024-01-01"
18
+ * ```
19
+ *
20
+ * Without a user-defined Worker, which usually serves as the entry
21
+ * point in the bundling process, wrangler needs to default to some
22
+ * other entry-point Worker for all intents and purposes. This is what
23
+ * this Worker is.
24
+ */
25
+ type Env = {
26
+ ASSET_SERVER: Fetcher;
27
+ };
28
+
29
+ export default {
30
+ async fetch(request: Request, env: Env) {
31
+ return env.ASSET_SERVER.fetch(request);
32
+ },
33
+ };
@@ -1,8 +1,9 @@
1
1
  // @ts-ignore entry point will get replaced
2
2
  import worker from "__ENTRY_POINT__";
3
+ import { isRoutingRuleMatch } from "./pages-dev-util";
4
+
3
5
  // @ts-ignore entry point will get replaced
4
6
  export * from "__ENTRY_POINT__";
5
- import { isRoutingRuleMatch } from "./pages-dev-util";
6
7
 
7
8
  // @ts-ignore routes are injected
8
9
  const routes = __ROUTES__;
@@ -41,14 +41,14 @@ type EventPluginContext<Env, P extends string, Data, PluginArgs> = {
41
41
  declare type PagesFunction<
42
42
  Env = unknown,
43
43
  P extends string = string,
44
- Data extends Record<string, unknown> = Record<string, unknown>
44
+ Data extends Record<string, unknown> = Record<string, unknown>,
45
45
  > = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
46
46
 
47
47
  declare type PagesPluginFunction<
48
48
  Env = unknown,
49
49
  P extends string = string,
50
50
  Data extends Record<string, unknown> = Record<string, unknown>,
51
- PluginArgs = unknown
51
+ PluginArgs = unknown,
52
52
  > = (
53
53
  context: EventPluginContext<Env, P, Data, PluginArgs>
54
54
  ) => Response | Promise<Response>;
@@ -29,7 +29,7 @@ type EventContext<Env, P extends string, Data> = {
29
29
  declare type PagesFunction<
30
30
  Env = unknown,
31
31
  P extends string = string,
32
- Data extends Record<string, unknown> = Record<string, unknown>
32
+ Data extends Record<string, unknown> = Record<string, unknown>,
33
33
  > = (context: EventContext<Env, P, Data>) => Response | Promise<Response>;
34
34
  /* end @cloudflare/workers-types */
35
35