wrangler 2.0.29 → 2.1.2
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.
- package/miniflare-dist/index.mjs +1136 -372
- package/package.json +3 -2
- package/src/__tests__/helpers/mock-cfetch.ts +39 -19
- package/src/__tests__/helpers/mock-console.ts +11 -2
- package/src/__tests__/helpers/msw/handlers/index.ts +13 -0
- package/src/__tests__/helpers/msw/handlers/namespaces.ts +104 -0
- package/src/__tests__/helpers/msw/handlers/oauth.ts +36 -0
- package/src/__tests__/helpers/msw/handlers/r2.ts +80 -0
- package/src/__tests__/helpers/msw/handlers/user.ts +63 -0
- package/src/__tests__/helpers/msw/index.ts +4 -0
- package/src/__tests__/index.test.ts +9 -7
- package/src/__tests__/init.test.ts +356 -5
- package/src/__tests__/jest.setup.ts +16 -0
- package/src/__tests__/middleware.test.ts +768 -0
- package/src/__tests__/pages.test.ts +11 -12
- package/src/__tests__/publish.test.ts +516 -438
- package/src/__tests__/r2.test.ts +128 -93
- package/src/__tests__/secret.test.ts +78 -0
- package/src/__tests__/tail.test.ts +47 -74
- package/src/__tests__/whoami.test.tsx +49 -64
- package/src/api/dev.ts +23 -4
- package/src/bundle.ts +225 -1
- package/src/dev/dev.tsx +3 -1
- package/src/dev/local.tsx +2 -2
- package/src/dev/remote.tsx +6 -3
- package/src/dev/start-server.ts +11 -7
- package/src/dev/use-esbuild.ts +4 -0
- package/src/dev.tsx +6 -16
- package/src/dialogs.tsx +12 -0
- package/src/index.tsx +95 -4
- package/src/init.ts +286 -11
- package/src/miniflare-cli/assets.ts +130 -415
- package/src/miniflare-cli/index.ts +3 -1
- package/src/pages/dev.tsx +5 -1
- package/src/pages/hash.tsx +13 -0
- package/src/pages/upload.tsx +3 -18
- package/src/publish.ts +38 -4
- package/src/tail/filters.ts +1 -5
- package/src/tail/index.ts +6 -3
- package/templates/middleware/common.ts +62 -0
- package/templates/middleware/loader-modules.ts +84 -0
- package/templates/middleware/loader-sw.ts +213 -0
- package/templates/middleware/middleware-pretty-error.ts +40 -0
- package/templates/middleware/middleware-scheduled.ts +14 -0
- package/wrangler-dist/cli.js +65900 -65432
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import { unstable_dev } from "../api";
|
|
3
|
+
import { unsetAllMocks } from "./helpers/mock-cfetch";
|
|
4
|
+
import { useMockIsTTY } from "./helpers/mock-istty";
|
|
5
|
+
import { runInTempDir } from "./helpers/run-in-tmp";
|
|
6
|
+
|
|
7
|
+
jest.unmock("undici");
|
|
8
|
+
|
|
9
|
+
describe("module workers change behaviour with middleware with wrangler dev", () => {
|
|
10
|
+
runInTempDir();
|
|
11
|
+
const { setIsTTY } = useMockIsTTY();
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
setIsTTY(true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
unsetAllMocks();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
process.env.EXPERIMENTAL_MIDDLEWARE = "true";
|
|
22
|
+
|
|
23
|
+
it("should register a middleware and intercept", async () => {
|
|
24
|
+
const scriptContent = `
|
|
25
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
26
|
+
const response = await middlewareCtx.next(request, env);
|
|
27
|
+
const text = await response.text();
|
|
28
|
+
return new Response(text + ' world');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default {
|
|
32
|
+
middleware: [middleware],
|
|
33
|
+
fetch(request, env, ctx) {
|
|
34
|
+
return new Response('Hello');
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
`;
|
|
38
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
39
|
+
|
|
40
|
+
const worker = await unstable_dev(
|
|
41
|
+
"index.js",
|
|
42
|
+
{},
|
|
43
|
+
{ disableExperimentalWarning: true }
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const resp = await worker.fetch();
|
|
47
|
+
let text;
|
|
48
|
+
if (resp) text = await resp.text();
|
|
49
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
50
|
+
await worker.stop();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should be able to access scheduled workers from middleware", async () => {
|
|
54
|
+
const scriptContent = `
|
|
55
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
56
|
+
await middlewareCtx.dispatch("scheduled", { cron: "* * * * *" });
|
|
57
|
+
return new Response("OK");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export default {
|
|
61
|
+
middleware: [middleware],
|
|
62
|
+
scheduled(controller, env, ctx) {
|
|
63
|
+
console.log("Scheduled worker called");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
`;
|
|
67
|
+
|
|
68
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
69
|
+
|
|
70
|
+
const worker = await unstable_dev(
|
|
71
|
+
"index.js",
|
|
72
|
+
{},
|
|
73
|
+
{ disableExperimentalWarning: true }
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const resp = await worker.fetch();
|
|
77
|
+
let text;
|
|
78
|
+
if (resp) text = await resp.text();
|
|
79
|
+
expect(text).toMatchInlineSnapshot(`"OK"`);
|
|
80
|
+
await worker.stop();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("should trigger an error in a scheduled work from middleware", async () => {
|
|
84
|
+
const scriptContent = `
|
|
85
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
86
|
+
try {
|
|
87
|
+
await middlewareCtx.dispatch("scheduled", { cron: "* * * * *" });
|
|
88
|
+
} catch (e) {
|
|
89
|
+
return new Response(e.message);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export default {
|
|
94
|
+
middleware: [middleware],
|
|
95
|
+
scheduled(controller, env, ctx) {
|
|
96
|
+
throw new Error("Error in scheduled worker");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
`;
|
|
100
|
+
|
|
101
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
102
|
+
|
|
103
|
+
const worker = await unstable_dev(
|
|
104
|
+
"index.js",
|
|
105
|
+
{},
|
|
106
|
+
{ disableExperimentalWarning: true }
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const resp = await worker.fetch();
|
|
110
|
+
let text;
|
|
111
|
+
if (resp) text = await resp.text();
|
|
112
|
+
expect(text).toMatchInlineSnapshot(`"Error in scheduled worker"`);
|
|
113
|
+
await worker.stop();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe("service workers change behaviour with middleware with wrangler dev", () => {
|
|
118
|
+
runInTempDir();
|
|
119
|
+
const { setIsTTY } = useMockIsTTY();
|
|
120
|
+
|
|
121
|
+
beforeEach(() => {
|
|
122
|
+
setIsTTY(true);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
afterEach(() => {
|
|
126
|
+
unsetAllMocks();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
process.env.EXPERIMENTAL_MIDDLEWARE = "true";
|
|
130
|
+
|
|
131
|
+
it("should register a middleware and intercept using addMiddleware", async () => {
|
|
132
|
+
const scriptContent = `
|
|
133
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
134
|
+
const response = await middlewareCtx.next(request, env);
|
|
135
|
+
const text = await response.text();
|
|
136
|
+
return new Response(text + ' world');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
addMiddleware(middleware);
|
|
140
|
+
|
|
141
|
+
addEventListener("fetch", (event) => {
|
|
142
|
+
event.respondWith(new Response('Hello'));
|
|
143
|
+
});
|
|
144
|
+
`;
|
|
145
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
146
|
+
|
|
147
|
+
const worker = await unstable_dev(
|
|
148
|
+
"index.js",
|
|
149
|
+
{},
|
|
150
|
+
{ disableExperimentalWarning: true }
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
const resp = await worker.fetch();
|
|
154
|
+
let text;
|
|
155
|
+
if (resp) text = await resp.text();
|
|
156
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
157
|
+
await worker.stop();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("should register a middleware and intercept using addMiddlewareInternal", async () => {
|
|
161
|
+
const scriptContent = `
|
|
162
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
163
|
+
const response = await middlewareCtx.next(request, env);
|
|
164
|
+
const text = await response.text();
|
|
165
|
+
return new Response(text + ' world');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
addMiddlewareInternal(middleware);
|
|
169
|
+
|
|
170
|
+
addEventListener("fetch", (event) => {
|
|
171
|
+
event.respondWith(new Response('Hello'));
|
|
172
|
+
});
|
|
173
|
+
`;
|
|
174
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
175
|
+
|
|
176
|
+
const worker = await unstable_dev(
|
|
177
|
+
"index.js",
|
|
178
|
+
{},
|
|
179
|
+
{ disableExperimentalWarning: true }
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
const resp = await worker.fetch();
|
|
183
|
+
let text;
|
|
184
|
+
if (resp) text = await resp.text();
|
|
185
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
186
|
+
await worker.stop();
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("should be able to access scheduled workers from middleware", async () => {
|
|
190
|
+
const scriptContent = `
|
|
191
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
192
|
+
await middlewareCtx.dispatch("scheduled", { cron: "* * * * *" });
|
|
193
|
+
return new Response("OK");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
addMiddleware(middleware);
|
|
197
|
+
|
|
198
|
+
addEventListener("scheduled", (event) => {
|
|
199
|
+
console.log("Scheduled worker called");
|
|
200
|
+
});
|
|
201
|
+
`;
|
|
202
|
+
|
|
203
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
204
|
+
|
|
205
|
+
const worker = await unstable_dev(
|
|
206
|
+
"index.js",
|
|
207
|
+
{},
|
|
208
|
+
{ disableExperimentalWarning: true }
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
const resp = await worker.fetch();
|
|
212
|
+
let text;
|
|
213
|
+
if (resp) text = await resp.text();
|
|
214
|
+
expect(text).toMatchInlineSnapshot(`"OK"`);
|
|
215
|
+
await worker.stop();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("should trigger an error in a scheduled work from middleware", async () => {
|
|
219
|
+
const scriptContent = `
|
|
220
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
221
|
+
try {
|
|
222
|
+
await middlewareCtx.dispatch("scheduled", { cron: "* * * * *" });
|
|
223
|
+
} catch (e) {
|
|
224
|
+
return new Response(e.message);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
addMiddleware(middleware);
|
|
229
|
+
|
|
230
|
+
addEventListener("scheduled", (event) => {
|
|
231
|
+
throw new Error("Error in scheduled worker");
|
|
232
|
+
});
|
|
233
|
+
`;
|
|
234
|
+
|
|
235
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
236
|
+
|
|
237
|
+
const worker = await unstable_dev(
|
|
238
|
+
"index.js",
|
|
239
|
+
{},
|
|
240
|
+
{ disableExperimentalWarning: true }
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
const resp = await worker.fetch();
|
|
244
|
+
let text;
|
|
245
|
+
if (resp) text = await resp.text();
|
|
246
|
+
expect(text).toMatchInlineSnapshot(`"Error in scheduled worker"`);
|
|
247
|
+
await worker.stop();
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
describe("unchanged functionality when wrapping with middleware (modules)", () => {
|
|
252
|
+
runInTempDir();
|
|
253
|
+
const { setIsTTY } = useMockIsTTY();
|
|
254
|
+
|
|
255
|
+
beforeEach(() => {
|
|
256
|
+
setIsTTY(true);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
afterEach(() => {
|
|
260
|
+
unsetAllMocks();
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
process.env.EXPERIMENTAL_MIDDLEWARE = "true";
|
|
264
|
+
|
|
265
|
+
it("should return Hello World with no middleware export", async () => {
|
|
266
|
+
const scriptContent = `
|
|
267
|
+
export default {
|
|
268
|
+
fetch(request, env, ctx) {
|
|
269
|
+
return new Response("Hello world");
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
`;
|
|
273
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
274
|
+
|
|
275
|
+
const worker = await unstable_dev(
|
|
276
|
+
"index.js",
|
|
277
|
+
{},
|
|
278
|
+
{ disableExperimentalWarning: true }
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
const resp = await worker.fetch();
|
|
282
|
+
if (resp) {
|
|
283
|
+
const text = await resp.text();
|
|
284
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
285
|
+
}
|
|
286
|
+
await worker.stop();
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it("should return hello world with empty middleware array", async () => {
|
|
290
|
+
const scriptContent = `
|
|
291
|
+
export default {
|
|
292
|
+
middleware: [],
|
|
293
|
+
fetch() {
|
|
294
|
+
return new Response("Hello world");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
`;
|
|
298
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
299
|
+
|
|
300
|
+
const worker = await unstable_dev(
|
|
301
|
+
"index.js",
|
|
302
|
+
{},
|
|
303
|
+
{ disableExperimentalWarning: true }
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
const resp = await worker.fetch();
|
|
307
|
+
let text;
|
|
308
|
+
if (resp) text = await resp.text();
|
|
309
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
310
|
+
await worker.stop();
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it("should return hello world passing through middleware", async () => {
|
|
314
|
+
const scriptContent = `
|
|
315
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
316
|
+
return middlewareCtx.next(request, env);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export default {
|
|
320
|
+
middleware: [middleware],
|
|
321
|
+
fetch(request, env, ctx) {
|
|
322
|
+
return new Response("Hello world");
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
`;
|
|
326
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
327
|
+
|
|
328
|
+
const worker = await unstable_dev(
|
|
329
|
+
"index.js",
|
|
330
|
+
{},
|
|
331
|
+
{ disableExperimentalWarning: true }
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
const resp = await worker.fetch();
|
|
335
|
+
if (resp) {
|
|
336
|
+
const text = await resp.text();
|
|
337
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
338
|
+
}
|
|
339
|
+
await worker.stop();
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("should return hello world with multiple middleware in array", async () => {
|
|
343
|
+
const scriptContent = `
|
|
344
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
345
|
+
return middlewareCtx.next(request, env);
|
|
346
|
+
}
|
|
347
|
+
const middleware2 = async (request, env, _ctx, middlewareCtx) => {
|
|
348
|
+
return middlewareCtx.next(request, env);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
export default {
|
|
353
|
+
middleware: [middleware, middleware2],
|
|
354
|
+
fetch() {
|
|
355
|
+
return new Response("Hello world");
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
`;
|
|
359
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
360
|
+
|
|
361
|
+
const worker = await unstable_dev(
|
|
362
|
+
"index.js",
|
|
363
|
+
{},
|
|
364
|
+
{ disableExperimentalWarning: true }
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
const resp = await worker.fetch();
|
|
368
|
+
let text;
|
|
369
|
+
if (resp) text = await resp.text();
|
|
370
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
371
|
+
await worker.stop();
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("should leave response headers unchanged with middleware", async () => {
|
|
375
|
+
const scriptContent = `
|
|
376
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
377
|
+
return middlewareCtx.next(request, env);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export default {
|
|
381
|
+
middleware: [middleware],
|
|
382
|
+
fetch() {
|
|
383
|
+
return new Response("Hello world", { status: 500, headers: { "x-test": "test" } });
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
`;
|
|
387
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
388
|
+
|
|
389
|
+
const worker = await unstable_dev(
|
|
390
|
+
"index.js",
|
|
391
|
+
{},
|
|
392
|
+
{ disableExperimentalWarning: true }
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
const resp = await worker.fetch();
|
|
396
|
+
const status = resp?.status;
|
|
397
|
+
let text;
|
|
398
|
+
if (resp) text = await resp.text();
|
|
399
|
+
const testHeader = resp?.headers.get("x-test");
|
|
400
|
+
expect(status).toEqual(500);
|
|
401
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
402
|
+
expect(testHeader).toEqual("test");
|
|
403
|
+
await worker.stop();
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("waitUntil should not block responses", async () => {
|
|
407
|
+
const scriptContent = `
|
|
408
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
409
|
+
return middlewareCtx.next(request, env);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
export default {
|
|
413
|
+
middleware: [middleware],
|
|
414
|
+
async fetch(request, env, ctx) {
|
|
415
|
+
let count = 0;
|
|
416
|
+
ctx.waitUntil(new Promise(resolve => {
|
|
417
|
+
setTimeout(() => {
|
|
418
|
+
count += 1;
|
|
419
|
+
console.log("waitUntil", count);
|
|
420
|
+
resolve()
|
|
421
|
+
}, 1000);
|
|
422
|
+
}));
|
|
423
|
+
return new Response("Hello world" + String(count));
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
`;
|
|
427
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
428
|
+
|
|
429
|
+
const worker = await unstable_dev(
|
|
430
|
+
"index.js",
|
|
431
|
+
{},
|
|
432
|
+
{ disableExperimentalWarning: true }
|
|
433
|
+
);
|
|
434
|
+
|
|
435
|
+
const resp = await worker.fetch();
|
|
436
|
+
let text;
|
|
437
|
+
if (resp) text = await resp.text();
|
|
438
|
+
expect(text).toMatchInlineSnapshot(`"Hello world0"`);
|
|
439
|
+
await worker.stop();
|
|
440
|
+
});
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
describe("unchanged functionality when wrapping with middlware (service workers)", () => {
|
|
444
|
+
runInTempDir();
|
|
445
|
+
const { setIsTTY } = useMockIsTTY();
|
|
446
|
+
|
|
447
|
+
beforeEach(() => {
|
|
448
|
+
setIsTTY(true);
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
afterEach(() => {
|
|
452
|
+
unsetAllMocks();
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
process.env.EXPERIMENTAL_MIDDLEWARE = "true";
|
|
456
|
+
|
|
457
|
+
it("should return Hello World with no middleware export", async () => {
|
|
458
|
+
const scriptContent = `
|
|
459
|
+
addEventListener("fetch", (event) => {
|
|
460
|
+
event.respondWith(new Response("Hello world"));
|
|
461
|
+
});
|
|
462
|
+
`;
|
|
463
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
464
|
+
|
|
465
|
+
const worker = await unstable_dev(
|
|
466
|
+
"index.js",
|
|
467
|
+
{},
|
|
468
|
+
{ disableExperimentalWarning: true }
|
|
469
|
+
);
|
|
470
|
+
|
|
471
|
+
const resp = await worker.fetch();
|
|
472
|
+
if (resp) {
|
|
473
|
+
const text = await resp.text();
|
|
474
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
475
|
+
}
|
|
476
|
+
await worker.stop();
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
it("should return hello world with empty middleware array", async () => {
|
|
480
|
+
const scriptContent = `
|
|
481
|
+
addMiddleware([]);
|
|
482
|
+
|
|
483
|
+
addEventListener("fetch", (event) => {
|
|
484
|
+
event.respondWith(new Response("Hello world"));
|
|
485
|
+
});
|
|
486
|
+
`;
|
|
487
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
488
|
+
|
|
489
|
+
const worker = await unstable_dev(
|
|
490
|
+
"index.js",
|
|
491
|
+
{},
|
|
492
|
+
{ disableExperimentalWarning: true }
|
|
493
|
+
);
|
|
494
|
+
|
|
495
|
+
const resp = await worker.fetch();
|
|
496
|
+
let text;
|
|
497
|
+
if (resp) text = await resp.text();
|
|
498
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
499
|
+
await worker.stop();
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
it("should return hello world passing through middleware", async () => {
|
|
503
|
+
const scriptContent = `
|
|
504
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
505
|
+
return middlewareCtx.next(request, env);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
addMiddleware(middleware);
|
|
509
|
+
|
|
510
|
+
addEventListener("fetch", (event) => {
|
|
511
|
+
event.respondWith(new Response("Hello world"));
|
|
512
|
+
});
|
|
513
|
+
`;
|
|
514
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
515
|
+
|
|
516
|
+
const worker = await unstable_dev(
|
|
517
|
+
"index.js",
|
|
518
|
+
{},
|
|
519
|
+
{ disableExperimentalWarning: true }
|
|
520
|
+
);
|
|
521
|
+
|
|
522
|
+
const resp = await worker.fetch();
|
|
523
|
+
if (resp) {
|
|
524
|
+
const text = await resp.text();
|
|
525
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
526
|
+
}
|
|
527
|
+
await worker.stop();
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
it("should return hello world with addMiddleware function called multiple times", async () => {
|
|
531
|
+
const scriptContent = `
|
|
532
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
533
|
+
return middlewareCtx.next(request, env);
|
|
534
|
+
}
|
|
535
|
+
const middleware2 = async (request, env, _ctx, middlewareCtx) => {
|
|
536
|
+
return middlewareCtx.next(request, env);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
addMiddleware(middleware);
|
|
540
|
+
addMiddleware(middleware2);
|
|
541
|
+
|
|
542
|
+
addEventListener("fetch", (event) => {
|
|
543
|
+
event.respondWith(new Response("Hello world"));
|
|
544
|
+
});
|
|
545
|
+
`;
|
|
546
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
547
|
+
|
|
548
|
+
const worker = await unstable_dev(
|
|
549
|
+
"index.js",
|
|
550
|
+
{},
|
|
551
|
+
{ disableExperimentalWarning: true }
|
|
552
|
+
);
|
|
553
|
+
|
|
554
|
+
const resp = await worker.fetch();
|
|
555
|
+
let text;
|
|
556
|
+
if (resp) text = await resp.text();
|
|
557
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
558
|
+
await worker.stop();
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
it("should return hello world with addMiddleware function called with array of middleware", async () => {
|
|
562
|
+
const scriptContent = `
|
|
563
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
564
|
+
return middlewareCtx.next(request, env);
|
|
565
|
+
}
|
|
566
|
+
const middleware2 = async (request, env, _ctx, middlewareCtx) => {
|
|
567
|
+
return middlewareCtx.next(request, env);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
addMiddleware(middleware, middleware2);
|
|
571
|
+
|
|
572
|
+
addEventListener("fetch", (event) => {
|
|
573
|
+
event.respondWith(new Response("Hello world"));
|
|
574
|
+
});
|
|
575
|
+
`;
|
|
576
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
577
|
+
|
|
578
|
+
const worker = await unstable_dev(
|
|
579
|
+
"index.js",
|
|
580
|
+
{},
|
|
581
|
+
{ disableExperimentalWarning: true }
|
|
582
|
+
);
|
|
583
|
+
|
|
584
|
+
const resp = await worker.fetch();
|
|
585
|
+
let text;
|
|
586
|
+
if (resp) text = await resp.text();
|
|
587
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
588
|
+
await worker.stop();
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
it("should return hello world with addMiddlewareInternal function called multiple times", async () => {
|
|
592
|
+
const scriptContent = `
|
|
593
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
594
|
+
return middlewareCtx.next(request, env);
|
|
595
|
+
}
|
|
596
|
+
const middleware2 = async (request, env, _ctx, middlewareCtx) => {
|
|
597
|
+
return middlewareCtx.next(request, env);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
addMiddlewareInternal(middleware);
|
|
601
|
+
addMiddlewareInternal(middleware2);
|
|
602
|
+
|
|
603
|
+
addEventListener("fetch", (event) => {
|
|
604
|
+
event.respondWith(new Response("Hello world"));
|
|
605
|
+
});
|
|
606
|
+
`;
|
|
607
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
608
|
+
|
|
609
|
+
const worker = await unstable_dev(
|
|
610
|
+
"index.js",
|
|
611
|
+
{},
|
|
612
|
+
{ disableExperimentalWarning: true }
|
|
613
|
+
);
|
|
614
|
+
|
|
615
|
+
const resp = await worker.fetch();
|
|
616
|
+
let text;
|
|
617
|
+
if (resp) text = await resp.text();
|
|
618
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
619
|
+
await worker.stop();
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
it("should return hello world with addMiddlewareInternal function called with array of middleware", async () => {
|
|
623
|
+
const scriptContent = `
|
|
624
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
625
|
+
return middlewareCtx.next(request, env);
|
|
626
|
+
}
|
|
627
|
+
const middleware2 = async (request, env, _ctx, middlewareCtx) => {
|
|
628
|
+
return middlewareCtx.next(request, env);
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
addMiddlewareInternal(middleware, middleware2);
|
|
632
|
+
|
|
633
|
+
addEventListener("fetch", (event) => {
|
|
634
|
+
event.respondWith(new Response("Hello world"));
|
|
635
|
+
});
|
|
636
|
+
`;
|
|
637
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
638
|
+
|
|
639
|
+
const worker = await unstable_dev(
|
|
640
|
+
"index.js",
|
|
641
|
+
{},
|
|
642
|
+
{ disableExperimentalWarning: true }
|
|
643
|
+
);
|
|
644
|
+
|
|
645
|
+
const resp = await worker.fetch();
|
|
646
|
+
let text;
|
|
647
|
+
if (resp) text = await resp.text();
|
|
648
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
649
|
+
await worker.stop();
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
it("should return hello world with both addMiddleware and addMiddlewareInternal called", async () => {
|
|
653
|
+
const scriptContent = `
|
|
654
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
655
|
+
return middlewareCtx.next(request, env);
|
|
656
|
+
}
|
|
657
|
+
const middleware2 = async (request, env, _ctx, middlewareCtx) => {
|
|
658
|
+
return middlewareCtx.next(request, env);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
addMiddleware(middleware);
|
|
662
|
+
addMiddlewareInternal(middleware2);
|
|
663
|
+
|
|
664
|
+
addEventListener("fetch", (event) => {
|
|
665
|
+
event.respondWith(new Response("Hello world"));
|
|
666
|
+
});
|
|
667
|
+
`;
|
|
668
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
669
|
+
|
|
670
|
+
const worker = await unstable_dev(
|
|
671
|
+
"index.js",
|
|
672
|
+
{},
|
|
673
|
+
{ disableExperimentalWarning: true }
|
|
674
|
+
);
|
|
675
|
+
|
|
676
|
+
const resp = await worker.fetch();
|
|
677
|
+
let text;
|
|
678
|
+
if (resp) text = await resp.text();
|
|
679
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
680
|
+
await worker.stop();
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
it("should leave response headers unchanged with middleware", async () => {
|
|
684
|
+
const scriptContent = `
|
|
685
|
+
const middleware = async (request, env, _ctx, middlewareCtx) => {
|
|
686
|
+
return middlewareCtx.next(request, env);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
addEventListener("fetch", (event) => {
|
|
690
|
+
event.respondWith(new Response("Hello world", { status: 500, headers: { "x-test": "test" } }));
|
|
691
|
+
});
|
|
692
|
+
`;
|
|
693
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
694
|
+
|
|
695
|
+
const worker = await unstable_dev(
|
|
696
|
+
"index.js",
|
|
697
|
+
{},
|
|
698
|
+
{ disableExperimentalWarning: true }
|
|
699
|
+
);
|
|
700
|
+
|
|
701
|
+
const resp = await worker.fetch();
|
|
702
|
+
const status = resp?.status;
|
|
703
|
+
let text;
|
|
704
|
+
if (resp) text = await resp.text();
|
|
705
|
+
const testHeader = resp?.headers.get("x-test");
|
|
706
|
+
expect(status).toEqual(500);
|
|
707
|
+
expect(text).toMatchInlineSnapshot(`"Hello world"`);
|
|
708
|
+
expect(testHeader).toEqual("test");
|
|
709
|
+
await worker.stop();
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
it("should allow multiple addEventListeners for fetch", async () => {
|
|
713
|
+
const scriptContent = `
|
|
714
|
+
let count = 0;
|
|
715
|
+
|
|
716
|
+
addEventListener("fetch", (event) => {
|
|
717
|
+
count += 1;
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
addEventListener("fetch", (event) => {
|
|
721
|
+
event.respondWith(new Response("Hello world" + String(count)));
|
|
722
|
+
});
|
|
723
|
+
`;
|
|
724
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
725
|
+
|
|
726
|
+
const worker = await unstable_dev(
|
|
727
|
+
"index.js",
|
|
728
|
+
{},
|
|
729
|
+
{ disableExperimentalWarning: true }
|
|
730
|
+
);
|
|
731
|
+
|
|
732
|
+
const resp = await worker.fetch();
|
|
733
|
+
let text;
|
|
734
|
+
if (resp) text = await resp.text();
|
|
735
|
+
expect(text).toMatchInlineSnapshot(`"Hello world1"`);
|
|
736
|
+
await worker.stop();
|
|
737
|
+
});
|
|
738
|
+
|
|
739
|
+
it("waitUntil should not block responses", async () => {
|
|
740
|
+
const scriptContent = `
|
|
741
|
+
addEventListener("fetch", (event) => {
|
|
742
|
+
|
|
743
|
+
let count = 0;
|
|
744
|
+
event.waitUntil(new Promise((resolve) => {
|
|
745
|
+
setTimeout(() => {
|
|
746
|
+
count +=1;
|
|
747
|
+
console.log('waitUntil', count);
|
|
748
|
+
resolve();
|
|
749
|
+
}, 1000);
|
|
750
|
+
}));
|
|
751
|
+
event.respondWith(new Response("Hello world" + String(count)));
|
|
752
|
+
});
|
|
753
|
+
`;
|
|
754
|
+
fs.writeFileSync("index.js", scriptContent);
|
|
755
|
+
|
|
756
|
+
const worker = await unstable_dev(
|
|
757
|
+
"index.js",
|
|
758
|
+
{},
|
|
759
|
+
{ disableExperimentalWarning: true }
|
|
760
|
+
);
|
|
761
|
+
|
|
762
|
+
const resp = await worker.fetch();
|
|
763
|
+
let text;
|
|
764
|
+
if (resp) text = await resp.text();
|
|
765
|
+
expect(text).toMatchInlineSnapshot(`"Hello world0"`);
|
|
766
|
+
await worker.stop();
|
|
767
|
+
});
|
|
768
|
+
});
|