wrangler 2.4.1 → 2.4.3

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.
@@ -1,8 +1,15 @@
1
- import { Awaitable, Dispatcher, Middleware, __facade_invoke__ } from "./common";
2
- export { __facade_register__, __facade_registerInternal__ } from "./common";
3
-
4
- // Miniflare's `EventTarget` follows the spec and doesn't allow exceptions to
5
- // be caught by `dispatchEvent`. Instead it has a custom`ThrowingEventTarget`
1
+ import {
2
+ Awaitable,
3
+ Dispatcher,
4
+ Middleware,
5
+ __facade_invoke__,
6
+ __facade_register__,
7
+ __facade_registerInternal__,
8
+ } from "./common";
9
+ export { __facade_register__, __facade_registerInternal__ };
10
+
11
+ // Miniflare 2's `EventTarget` follows the spec and doesn't allow exceptions to
12
+ // be caught by `dispatchEvent`. Instead it has a custom `ThrowingEventTarget`
6
13
  // class that rethrows errors from event listeners in `dispatchEvent`.
7
14
  // We'd like errors to be propagated to the top-level `addEventListener`, so
8
15
  // we'd like to use `ThrowingEventTarget`. Unfortunately, `ThrowingEventTarget`
@@ -15,45 +22,52 @@ if ((globalThis as any).MINIFLARE) {
15
22
  __FACADE_EVENT_TARGET__ = new EventTarget();
16
23
  }
17
24
 
18
- declare global {
19
- var __facade_addEventListener__: (
20
- type: string,
21
- listener: EventListenerOrEventListenerObject,
22
- options?: EventTargetAddEventListenerOptions | boolean
23
- ) => void;
24
- var __facade_removeEventListener__: (
25
- type: string,
26
- listener: EventListenerOrEventListenerObject,
27
- options?: EventTargetEventListenerOptions | boolean
28
- ) => void;
29
- var __facade_dispatchEvent__: (event: Event) => void;
30
- }
31
-
32
- function __facade_isSpecialEvent__(type: string) {
25
+ function __facade_isSpecialEvent__(
26
+ type: string
27
+ ): type is "fetch" | "scheduled" {
33
28
  return type === "fetch" || type === "scheduled";
34
29
  }
35
- globalThis.__facade_addEventListener__ = function (type, listener, options) {
30
+ const __facade__originalAddEventListener__ = globalThis.addEventListener;
31
+ const __facade__originalRemoveEventListener__ = globalThis.removeEventListener;
32
+ const __facade__originalDispatchEvent__ = globalThis.dispatchEvent;
33
+
34
+ globalThis.addEventListener = function (type, listener, options) {
36
35
  if (__facade_isSpecialEvent__(type)) {
37
- __FACADE_EVENT_TARGET__.addEventListener(type, listener, options);
36
+ __FACADE_EVENT_TARGET__.addEventListener(
37
+ type,
38
+ listener as EventListenerOrEventListenerObject,
39
+ options
40
+ );
38
41
  } else {
39
- globalThis.addEventListener(type as any, listener, options);
42
+ __facade__originalAddEventListener__(type, listener, options);
40
43
  }
41
44
  };
42
- globalThis.__facade_removeEventListener__ = function (type, listener, options) {
45
+ globalThis.removeEventListener = function (type, listener, options) {
43
46
  if (__facade_isSpecialEvent__(type)) {
44
- __FACADE_EVENT_TARGET__.removeEventListener(type, listener, options);
47
+ __FACADE_EVENT_TARGET__.removeEventListener(
48
+ type,
49
+ listener as EventListenerOrEventListenerObject,
50
+ options
51
+ );
45
52
  } else {
46
- globalThis.removeEventListener(type as any, listener, options);
53
+ __facade__originalRemoveEventListener__(type, listener, options);
47
54
  }
48
55
  };
49
- globalThis.__facade_dispatchEvent__ = function (event) {
56
+ globalThis.dispatchEvent = function (event) {
50
57
  if (__facade_isSpecialEvent__(event.type)) {
51
- __FACADE_EVENT_TARGET__.dispatchEvent(event);
58
+ return __FACADE_EVENT_TARGET__.dispatchEvent(event);
52
59
  } else {
53
- globalThis.dispatchEvent(event as any);
60
+ return __facade__originalDispatchEvent__(event);
54
61
  }
55
62
  };
56
63
 
64
+ declare global {
65
+ var addMiddleware: typeof __facade_register__;
66
+ var addMiddlewareInternal: typeof __facade_registerInternal__;
67
+ }
68
+ globalThis.addMiddleware = __facade_register__;
69
+ globalThis.addMiddlewareInternal = __facade_registerInternal__;
70
+
57
71
  const __facade_waitUntil__ = Symbol("__facade_waitUntil__");
58
72
  const __facade_response__ = Symbol("__facade_response__");
59
73
  const __facade_dispatched__ = Symbol("__facade_dispatched__");
@@ -154,7 +168,7 @@ class __Facade_ScheduledEvent__ extends __Facade_ExtendableEvent__ {
154
168
  }
155
169
  }
156
170
 
157
- globalThis.addEventListener("fetch", (event) => {
171
+ __facade__originalAddEventListener__("fetch", (event) => {
158
172
  const ctx: ExecutionContext = {
159
173
  waitUntil: event.waitUntil.bind(event),
160
174
  passThroughOnException: event.passThroughOnException.bind(event),
@@ -201,7 +215,7 @@ globalThis.addEventListener("fetch", (event) => {
201
215
  );
202
216
  });
203
217
 
204
- globalThis.addEventListener("scheduled", (event) => {
218
+ __facade__originalAddEventListener__("scheduled", (event) => {
205
219
  const facadeEvent = new __Facade_ScheduledEvent__("scheduled", {
206
220
  scheduledTime: event.scheduledTime,
207
221
  cron: event.cron,
@@ -0,0 +1,20 @@
1
+ import type { Middleware } from "./common";
2
+
3
+ // See comment in `bundle.ts` for details on why this is needed
4
+ const jsonError: Middleware = async (request, env, _ctx, middlewareCtx) => {
5
+ try {
6
+ return await middlewareCtx.next(request, env);
7
+ } catch (e: any) {
8
+ const error = {
9
+ name: e?.name,
10
+ message: e?.message ?? String(e),
11
+ stack: e?.stack,
12
+ };
13
+ return Response.json(error, {
14
+ status: 500,
15
+ headers: { "MF-Experimental-Error-Stack": "true" },
16
+ });
17
+ }
18
+ };
19
+
20
+ export default jsonError;