summaryrefslogtreecommitdiff
path: root/node_modules/wrangler/templates/middleware/loader-sw.ts
blob: 9e465f90d18dad58ed9bfad926a968aa0c64e888 (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import {
	Awaitable,
	Dispatcher,
	IncomingRequest,
	Middleware,
	__facade_invoke__,
	__facade_register__,
	__facade_registerInternal__,
} from "./common";
export { __facade_register__, __facade_registerInternal__ };

// Miniflare 2's `EventTarget` follows the spec and doesn't allow exceptions to
// be caught by `dispatchEvent`. Instead it has a custom `ThrowingEventTarget`
// class that rethrows errors from event listeners in `dispatchEvent`.
// We'd like errors to be propagated to the top-level `addEventListener`, so
// we'd like to use `ThrowingEventTarget`. Unfortunately, `ThrowingEventTarget`
// isn't exposed on the global scope, but `WorkerGlobalScope` (which extends
// `ThrowingEventTarget`) is. Therefore, we get at it in this nasty way.
let __FACADE_EVENT_TARGET__: EventTarget;
if ((globalThis as any).MINIFLARE) {
	__FACADE_EVENT_TARGET__ = new (Object.getPrototypeOf(WorkerGlobalScope))();
} else {
	__FACADE_EVENT_TARGET__ = new EventTarget();
}

function __facade_isSpecialEvent__(
	type: string
): type is "fetch" | "scheduled" {
	return type === "fetch" || type === "scheduled";
}
const __facade__originalAddEventListener__ = globalThis.addEventListener;
const __facade__originalRemoveEventListener__ = globalThis.removeEventListener;
const __facade__originalDispatchEvent__ = globalThis.dispatchEvent;

globalThis.addEventListener = function (type, listener, options) {
	if (__facade_isSpecialEvent__(type)) {
		__FACADE_EVENT_TARGET__.addEventListener(
			type,
			listener as EventListenerOrEventListenerObject,
			options
		);
	} else {
		__facade__originalAddEventListener__(type, listener, options);
	}
};
globalThis.removeEventListener = function (type, listener, options) {
	if (__facade_isSpecialEvent__(type)) {
		__FACADE_EVENT_TARGET__.removeEventListener(
			type,
			listener as EventListenerOrEventListenerObject,
			options
		);
	} else {
		__facade__originalRemoveEventListener__(type, listener, options);
	}
};
globalThis.dispatchEvent = function (event) {
	if (__facade_isSpecialEvent__(event.type)) {
		return __FACADE_EVENT_TARGET__.dispatchEvent(event);
	} else {
		return __facade__originalDispatchEvent__(event);
	}
};

declare global {
	var addMiddleware: typeof __facade_register__;
	var addMiddlewareInternal: typeof __facade_registerInternal__;
}
globalThis.addMiddleware = __facade_register__;
globalThis.addMiddlewareInternal = __facade_registerInternal__;

const __facade_waitUntil__ = Symbol("__facade_waitUntil__");
const __facade_response__ = Symbol("__facade_response__");
const __facade_dispatched__ = Symbol("__facade_dispatched__");

class __Facade_ExtendableEvent__ extends Event {
	[__facade_waitUntil__]: Awaitable<unknown>[] = [];

	waitUntil(promise: Awaitable<any>) {
		if (!(this instanceof __Facade_ExtendableEvent__)) {
			throw new TypeError("Illegal invocation");
		}
		this[__facade_waitUntil__].push(promise);
	}
}

interface FetchEventInit extends EventInit {
	request: Request;
	passThroughOnException: FetchEvent["passThroughOnException"];
}

class __Facade_FetchEvent__ extends __Facade_ExtendableEvent__ {
	#request: Request;
	#passThroughOnException: FetchEvent["passThroughOnException"];
	[__facade_response__]?: Awaitable<Response>;
	[__facade_dispatched__] = false;

	constructor(type: "fetch", init: FetchEventInit) {
		super(type);
		this.#request = init.request;
		this.#passThroughOnException = init.passThroughOnException;
	}

	get request() {
		return this.#request;
	}

	respondWith(response: Awaitable<Response>) {
		if (!(this instanceof __Facade_FetchEvent__)) {
			throw new TypeError("Illegal invocation");
		}
		if (this[__facade_response__] !== undefined) {
			throw new DOMException(
				"FetchEvent.respondWith() has already been called; it can only be called once.",
				"InvalidStateError"
			);
		}
		if (this[__facade_dispatched__]) {
			throw new DOMException(
				"Too late to call FetchEvent.respondWith(). It must be called synchronously in the event handler.",
				"InvalidStateError"
			);
		}
		this.stopImmediatePropagation();
		this[__facade_response__] = response;
	}

	passThroughOnException() {
		if (!(this instanceof __Facade_FetchEvent__)) {
			throw new TypeError("Illegal invocation");
		}
		// Need to call native method immediately in case uncaught error thrown
		this.#passThroughOnException();
	}
}

interface ScheduledEventInit extends EventInit {
	scheduledTime: number;
	cron: string;
	noRetry: ScheduledEvent["noRetry"];
}

class __Facade_ScheduledEvent__ extends __Facade_ExtendableEvent__ {
	#scheduledTime: number;
	#cron: string;
	#noRetry: ScheduledEvent["noRetry"];

	constructor(type: "scheduled", init: ScheduledEventInit) {
		super(type);
		this.#scheduledTime = init.scheduledTime;
		this.#cron = init.cron;
		this.#noRetry = init.noRetry;
	}

	get scheduledTime() {
		return this.#scheduledTime;
	}

	get cron() {
		return this.#cron;
	}

	noRetry() {
		if (!(this instanceof __Facade_ScheduledEvent__)) {
			throw new TypeError("Illegal invocation");
		}
		// Need to call native method immediately in case uncaught error thrown
		this.#noRetry();
	}
}

__facade__originalAddEventListener__("fetch", (event) => {
	const ctx: ExecutionContext = {
		waitUntil: event.waitUntil.bind(event),
		passThroughOnException: event.passThroughOnException.bind(event),
	};

	const __facade_sw_dispatch__: Dispatcher = function (type, init) {
		if (type === "scheduled") {
			const facadeEvent = new __Facade_ScheduledEvent__("scheduled", {
				scheduledTime: Date.now(),
				cron: init.cron ?? "",
				noRetry() {},
			});

			__FACADE_EVENT_TARGET__.dispatchEvent(facadeEvent);
			event.waitUntil(Promise.all(facadeEvent[__facade_waitUntil__]));
		}
	};

	const __facade_sw_fetch__: Middleware = function (request, _env, ctx) {
		const facadeEvent = new __Facade_FetchEvent__("fetch", {
			request,
			passThroughOnException: ctx.passThroughOnException,
		});

		__FACADE_EVENT_TARGET__.dispatchEvent(facadeEvent);
		facadeEvent[__facade_dispatched__] = true;
		event.waitUntil(Promise.all(facadeEvent[__facade_waitUntil__]));

		const response = facadeEvent[__facade_response__];
		if (response === undefined) {
			throw new Error("No response!"); // TODO: proper error message
		}
		return response;
	};

	event.respondWith(
		__facade_invoke__(
			event.request as IncomingRequest,
			globalThis,
			ctx,
			__facade_sw_dispatch__,
			__facade_sw_fetch__
		)
	);
});

__facade__originalAddEventListener__("scheduled", (event) => {
	const facadeEvent = new __Facade_ScheduledEvent__("scheduled", {
		scheduledTime: event.scheduledTime,
		cron: event.cron,
		noRetry: event.noRetry.bind(event),
	});

	__FACADE_EVENT_TARGET__.dispatchEvent(facadeEvent);
	event.waitUntil(Promise.all(facadeEvent[__facade_waitUntil__]));
});