wrangler 0.0.0-e8df68ee → 0.0.0-e9106dfa5

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 (40) hide show
  1. package/README.md +33 -41
  2. package/bin/wrangler.js +6 -35
  3. package/config-schema.json +2277 -0
  4. package/package.json +182 -195
  5. package/templates/checked-fetch.js +10 -3
  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 +3 -3
  10. package/templates/middleware/loader-modules.ts +92 -85
  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-mock-analytics-engine.d.ts +3 -0
  14. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  15. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  16. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  17. package/templates/middleware/middleware-scheduled.ts +15 -1
  18. package/templates/modules-watch-stub.js +4 -0
  19. package/templates/pages-dev-pipeline.ts +5 -3
  20. package/templates/pages-template-plugin.ts +2 -2
  21. package/templates/pages-template-worker.ts +1 -1
  22. package/templates/startDevWorker/InspectorProxyWorker.ts +679 -0
  23. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  24. package/templates/tsconfig.init.json +4 -87
  25. package/templates/tsconfig.json +8 -1
  26. package/templates/tsconfig.tsbuildinfo +1 -0
  27. package/wrangler-dist/InspectorProxyWorker.js +471 -0
  28. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  29. package/wrangler-dist/ProxyWorker.js +238 -0
  30. package/wrangler-dist/ProxyWorker.js.map +6 -0
  31. package/wrangler-dist/cli.d.ts +25339 -372
  32. package/wrangler-dist/cli.js +150132 -144938
  33. package/Cloudflare_CA.pem +0 -18
  34. package/templates/middleware/middleware-d1-beta.d.ts +0 -3
  35. package/templates/middleware/middleware-d1-beta.ts +0 -244
  36. package/templates/middleware/middleware-multiworker-dev.d.ts +0 -4
  37. package/templates/middleware/middleware-multiworker-dev.ts +0 -59
  38. package/templates/middleware/middleware-serve-static-assets.d.ts +0 -6
  39. package/templates/middleware/middleware-serve-static-assets.ts +0 -56
  40. package/wrangler-dist/wasm-sync.wasm +0 -0
@@ -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,61 +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" | "fetch"
66
- >;
67
-
68
- const facade: ExportedHandler<unknown> & MissingExportHandlers = {
69
- ...(worker.tail && {
70
- tail: maskHandlerEnv(worker.tail),
71
- }),
72
- ...(worker.trace && {
73
- trace: maskHandlerEnv(worker.trace),
74
- }),
75
- ...(worker.scheduled && {
76
- scheduled: maskHandlerEnv(worker.scheduled),
77
- }),
78
- ...(worker.queue && {
79
- queue: maskHandlerEnv(worker.queue),
80
- }),
81
- ...(worker.test && {
82
- test: maskHandlerEnv(worker.test),
83
- }),
84
45
 
85
- fetch(request, rawEnv, ctx) {
86
- const env = getMaskedEnv(rawEnv);
87
- // Get the chain of middleware from the worker object
88
- if (worker.middleware && worker.middleware.length > 0) {
89
- for (const middleware of worker.middleware) {
90
- __facade_register__(middleware);
91
- }
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
+ };
92
56
 
93
- const __facade_modules_dispatch__: Dispatcher = function (type, init) {
57
+ return {
58
+ ...worker,
59
+ fetch(request, env, ctx) {
60
+ const dispatcher: Dispatcher = function (type, init) {
94
61
  if (type === "scheduled" && worker.scheduled !== undefined) {
95
62
  const controller = new __Facade_ScheduledController__(
96
63
  Date.now(),
@@ -100,28 +67,68 @@ const facade: ExportedHandler<unknown> & MissingExportHandlers = {
100
67
  return worker.scheduled(controller, env, ctx);
101
68
  }
102
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
+ }
103
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>) {
104
117
  return __facade_invoke__(
105
118
  request,
106
- env,
107
- ctx,
108
- __facade_modules_dispatch__,
109
- __facade_modules_fetch__
119
+ this.env,
120
+ this.ctx,
121
+ this.#dispatcher,
122
+ this.#fetchDispatcher
110
123
  );
111
- } else {
112
- // We didn't have any middleware so we can skip the invocation chain,
113
- // and just call the fetch handler directly
114
-
115
- // We "don't care" if this is undefined as we want to have the same behavior
116
- // as if the worker completely bypassed middleware.
117
- return __facade_modules_fetch__(request, env, ctx);
118
124
  }
119
- },
120
- };
121
-
122
- type HandlerFn<D, R> = (data: D, env: unknown, ctx: ExecutionContext) => R;
123
- function maskHandlerEnv<D, R>(handler: HandlerFn<D, R>): HandlerFn<D, R> {
124
- return (data, env, ctx) => handler(data, getMaskedEnv(env), ctx);
125
+ };
125
126
  }
126
127
 
127
- 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;
@@ -0,0 +1,3 @@
1
+ declare module "config:middleware/mock-analytics-engine" {
2
+ export const bindings: string[];
3
+ }
@@ -0,0 +1,30 @@
1
+ /// <reference path="middleware-mock-analytics-engine.d.ts"/>
2
+
3
+ import { bindings } from "config:middleware/mock-analytics-engine";
4
+ import type { Middleware } from "./common";
5
+
6
+ const bindingsEnv = Object.fromEntries(
7
+ bindings.map((binding) => [
8
+ binding,
9
+ {
10
+ writeDataPoint() {
11
+ // no op in dev
12
+ },
13
+ },
14
+ ])
15
+ ) satisfies Record<string, AnalyticsEngineDataset>;
16
+
17
+ const analyticsEngine: Middleware = async (
18
+ request,
19
+ env,
20
+ _ctx,
21
+ middlewareCtx
22
+ ) => {
23
+ // we're going to directly modify env so it maintains referential equality
24
+ for (const binding of bindings) {
25
+ env[binding] ??= bindingsEnv[binding];
26
+ }
27
+ return await middlewareCtx.next(request, env);
28
+ };
29
+
30
+ export default analyticsEngine;
@@ -0,0 +1,3 @@
1
+ declare module "config:middleware/patch-console-prefix" {
2
+ export const prefix: string;
3
+ }
@@ -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
- return middlewareCtx.next(request, env);
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;
@@ -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";
@@ -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__;
@@ -19,10 +20,11 @@ export default <ExportedHandler<{ ASSETS: Fetcher }>>{
19
20
 
20
21
  for (const include of routes.include) {
21
22
  if (isRoutingRuleMatch(pathname, include)) {
22
- if (worker.fetch === undefined) {
23
+ const workerAsHandler = worker as ExportedHandler;
24
+ if (workerAsHandler.fetch === undefined) {
23
25
  throw new TypeError("Entry point missing `fetch` handler");
24
26
  }
25
- return worker.fetch(request, env, context);
27
+ return workerAsHandler.fetch(request, env, context);
26
28
  }
27
29
  }
28
30
 
@@ -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