-
Notifications
You must be signed in to change notification settings - Fork 198
/
bindings.spec.ts
653 lines (623 loc) 路 20.3 KB
/
bindings.spec.ts
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
import assert from "assert";
import { readFileSync } from "fs";
import fs from "fs/promises";
import path from "path";
import { setImmediate } from "timers/promises";
import { CachePlugin } from "@miniflare/cache";
import {
BindingsPlugin,
Fetcher,
MiniflareCoreError,
Response,
_CoreMount,
} from "@miniflare/core";
import {
Compatibility,
NoOpLog,
PluginContext,
getRequestContext,
} from "@miniflare/shared";
import {
getObjectProperties,
logPluginOptions,
parsePluginArgv,
parsePluginWranglerConfig,
useMiniflare,
useServer,
useTmp,
} from "@miniflare/shared-test";
import test from "ava";
const log = new NoOpLog();
const compat = new Compatibility();
const rootPath = process.cwd();
const ctx: PluginContext = { log, compat, rootPath };
const fixturesPath = path.join(__dirname, "..", "..", "..", "test", "fixtures");
// add.wasm is a WebAssembly module with a single export "add" that adds
// its 2 integer parameters together and returns the result, it is from:
// https://webassembly.github.io/wabt/demo/wat2wasm/
const addModulePath = path.join(fixturesPath, "add.wasm");
// lorem-ipsum.txt is five paragraphs of lorem ipsum nonsense text
const loremIpsumPath = path.join(fixturesPath, "lorem-ipsum.txt");
const loremIpsum = readFileSync(loremIpsumPath, "utf-8");
test("BindingsPlugin: parses options from argv", (t) => {
let options = parsePluginArgv(BindingsPlugin, [
"--env",
".env.test",
"--binding",
"KEY1=value1",
"--binding",
"KEY2=value2",
"--global",
"KEY3=value3",
"--global",
"KEY4=value4",
"--wasm",
"MODULE1=module1.wasm",
"--wasm",
"MODULE2=module2.wasm",
"--text-blob",
"TEXT1=text-blob-1.txt",
"--text-blob",
"TEXT2=text-blob-2.txt",
"--service",
"SERVICE1=service1",
"--service",
"SERVICE2=service2@development",
]);
t.deepEqual(options, {
envPath: ".env.test",
bindings: { KEY1: "value1", KEY2: "value2" },
globals: { KEY3: "value3", KEY4: "value4" },
wasmBindings: { MODULE1: "module1.wasm", MODULE2: "module2.wasm" },
textBlobBindings: { TEXT1: "text-blob-1.txt", TEXT2: "text-blob-2.txt" },
serviceBindings: {
SERVICE1: "service1",
SERVICE2: { service: "service2", environment: "development" },
},
});
options = parsePluginArgv(BindingsPlugin, [
"-e",
".env.test",
"-b",
"KEY1=value1",
"-b",
"KEY2=value2",
"-S",
"SERVICE1=service1",
"-S",
"SERVICE2=service2@development",
]);
t.deepEqual(options, {
envPath: ".env.test",
bindings: { KEY1: "value1", KEY2: "value2" },
serviceBindings: {
SERVICE1: "service1",
SERVICE2: { service: "service2", environment: "development" },
},
});
});
test("BindingsPlugin: parses options from wrangler config", async (t) => {
let options = parsePluginWranglerConfig(BindingsPlugin, {
wasm_modules: {
MODULE1: "module1.wasm",
MODULE2: "module2.wasm",
},
text_blobs: {
TEXT1: "text-blob-1.txt",
TEXT2: "text-blob-2.txt",
},
experimental_services: [
{ name: "SERVICE1", service: "service1", environment: "development" },
{ name: "SERVICE2", service: "service2", environment: "production" },
],
miniflare: {
globals: { KEY5: "value5", KEY6: false, KEY7: 10 },
env_path: ".env.test",
},
});
t.like(options, {
envPath: ".env.test",
globals: { KEY5: "value5", KEY6: false, KEY7: 10 },
wasmBindings: { MODULE1: "module1.wasm", MODULE2: "module2.wasm" },
textBlobBindings: { TEXT1: "text-blob-1.txt", TEXT2: "text-blob-2.txt" },
serviceBindings: {
SERVICE1: { service: "service1", environment: "development" },
SERVICE2: { service: "service2", environment: "production" },
},
});
// Wrangler bindings are stored in the kWranglerBindings symbol, which isn't
// exported, so setup the plugin and check they're included
options = parsePluginWranglerConfig(BindingsPlugin, {
vars: { KEY1: "value1", KEY2: "value2", KEY3: true, KEY4: 42 },
});
const plugin = new BindingsPlugin(ctx, options);
const result = await plugin.setup();
// Wrangler bindings should be stringified
t.deepEqual(result.bindings, {
KEY1: "value1",
KEY2: "value2",
KEY3: "true",
KEY4: "42",
});
});
test("BindingsPlugin: logs options", (t) => {
// wranglerOptions should contain [kWranglerBindings]
const wranglerOptions = parsePluginWranglerConfig(BindingsPlugin, {
vars: { KEY1: "value1", KEY2: "value2" },
});
let logs = logPluginOptions(BindingsPlugin, {
...wranglerOptions,
envPath: ".env.custom",
bindings: { KEY3: "value3", KEY4: "value4" },
globals: { KEY5: "value5", KEY6: "value6" },
wasmBindings: { MODULE1: "module1.wasm", MODULE2: "module2.wasm" },
textBlobBindings: { TEXT1: "text-blob-1.txt", TEXT2: "text-blob-2.txt" },
serviceBindings: {
SERVICE1: "service1",
SERVICE2: { service: "service2", environment: "development" },
},
});
t.deepEqual(logs, [
"Env Path: .env.custom",
"Wrangler Variables: KEY1, KEY2",
"Custom Bindings: KEY3, KEY4",
"Custom Globals: KEY5, KEY6",
"WASM Bindings: MODULE1, MODULE2",
"Text Blob Bindings: TEXT1, TEXT2",
"Service Bindings: SERVICE1, SERVICE2",
]);
logs = logPluginOptions(BindingsPlugin, { envPath: true });
t.deepEqual(logs, ["Env Path: .env"]);
logs = logPluginOptions(BindingsPlugin, { envPath: false });
t.deepEqual(logs, []);
});
test("BindingsPlugin: uses default .env path if envPathDefaultFallback set and envPath is undefined", (t) => {
let plugin = new BindingsPlugin(ctx, { envPathDefaultFallback: true });
t.true(plugin.envPath);
// Check leaves envPath alone if defined
plugin = new BindingsPlugin(ctx, {
envPathDefaultFallback: true,
envPath: false,
});
t.false(plugin.envPath);
plugin = new BindingsPlugin(ctx, {
envPathDefaultFallback: true,
envPath: ".env.custom",
});
t.is(plugin.envPath, ".env.custom");
});
test("BindingsPlugin: setup: loads .env bindings from default location", async (t) => {
const tmp = await useTmp(t);
const defaultEnvPath = path.join(tmp, ".env");
let plugin = new BindingsPlugin(
{ log, compat, rootPath: tmp },
{ envPath: true }
);
// Shouldn't throw if file doesn't exist...
let result = await plugin.setup();
// ...but should still watch file
t.deepEqual(result, {
globals: undefined,
bindings: {},
watch: [defaultEnvPath],
});
// Create file and try setup again
await fs.writeFile(defaultEnvPath, "KEY=value");
result = await plugin.setup();
t.deepEqual(result, {
globals: undefined,
bindings: { KEY: "value" },
watch: [defaultEnvPath],
});
// Check default .env only loaded when envPath set to true
plugin = new BindingsPlugin({ log, compat, rootPath: tmp }, {});
result = await plugin.setup();
t.deepEqual(result, { globals: undefined, bindings: {}, watch: [] });
});
test("BindingsPlugin: setup: loads .env bindings from custom location", async (t) => {
const tmp = await useTmp(t);
const defaultEnvPath = path.join(tmp, ".env");
const customEnvPath = path.join(tmp, ".env.custom");
await fs.writeFile(defaultEnvPath, "KEY=default");
const plugin = new BindingsPlugin(
{ log, compat, rootPath: tmp },
// Should resolve envPath relative to rootPath
{ envPath: ".env.custom" }
);
// Should throw if file doesn't exist
await t.throwsAsync(plugin.setup(), {
code: "ENOENT",
message: /\.env\.custom/,
});
// Create file and try setup again
await fs.writeFile(customEnvPath, "KEY=custom");
const result = await plugin.setup();
t.deepEqual(result, {
globals: undefined,
bindings: { KEY: "custom" },
watch: [customEnvPath],
});
});
test("BindingsPlugin: setup: includes custom bindings", async (t) => {
const obj = { a: 1 };
const plugin = new BindingsPlugin(ctx, { bindings: { obj } });
const result = await plugin.setup();
t.is(result.bindings?.obj, obj);
t.deepEqual(result.watch, []);
});
test("BindingsPlugin: setup: loads WebAssembly bindings", async (t) => {
let plugin = new BindingsPlugin(ctx, {
wasmBindings: { ADD: addModulePath },
});
let result = await plugin.setup();
t.not(result.bindings?.ADD, undefined);
assert(result.bindings?.ADD);
const instance = new WebAssembly.Instance(result.bindings.ADD);
assert(typeof instance.exports.add === "function");
t.is(instance.exports.add(1, 2), 3);
// Check resolves wasmBindings path relative to rootPath
plugin = new BindingsPlugin(
{ log, compat, rootPath: path.dirname(addModulePath) },
{ wasmBindings: { ADD: path.basename(addModulePath) } }
);
result = await plugin.setup();
t.not(result.bindings?.ADD, undefined);
});
test("BindingsPlugin: setup: loads text blob bindings", async (t) => {
let plugin = new BindingsPlugin(ctx, {
textBlobBindings: { LOREM_IPSUM: loremIpsumPath },
});
let result = await plugin.setup();
t.not(result.bindings?.LOREM_IPSUM, undefined);
t.is(result.bindings?.LOREM_IPSUM, loremIpsum);
// Check resolves text blob bindings path relative to rootPath
plugin = new BindingsPlugin(
{ log, compat, rootPath: path.dirname(loremIpsumPath) },
{ textBlobBindings: { LOREM_IPSUM: loremIpsumPath } }
);
result = await plugin.setup();
t.not(result.bindings?.LOREM_IPSUM, undefined);
t.is(result.bindings?.LOREM_IPSUM, loremIpsum);
});
test("BindingsPlugin: setup: loads bindings from all sources", async (t) => {
// Bindings should be loaded in this order, from lowest to highest priority:
// 1) Wrangler [vars]
// 2) .env Variables
// 3) WASM Module Bindings
// 4) Text Blob Bindings
// 5) Service Bindings
// 6) Custom Bindings
// wranglerOptions should contain [kWranglerBindings]
const wranglerOptions = parsePluginWranglerConfig(BindingsPlugin, {
vars: { A: "w", B: "w", C: "w", D: "w", E: "w" },
});
const tmp = await useTmp(t);
const envPath = path.join(tmp, ".env");
await fs.writeFile(envPath, "A=env\nB=env\nC=env\nD=env");
const obj = { ping: "pong" };
const throws = () => {
throw new Error("Should not be called");
};
const plugin = new BindingsPlugin(ctx, {
...wranglerOptions,
wasmBindings: {
A: addModulePath,
B: addModulePath,
C: addModulePath,
},
textBlobBindings: {
D: loremIpsumPath,
},
serviceBindings: { A: throws, B: throws },
bindings: { A: obj },
envPath,
});
const result = await plugin.setup();
assert(result.bindings);
t.is(result.bindings.E, "w");
t.is(result.bindings.D, loremIpsum);
t.true(result.bindings.C instanceof WebAssembly.Module);
t.true(result.bindings.B instanceof Fetcher);
t.is(result.bindings.A, obj);
});
// Service bindings tests
test("Fetcher: hides implementation details", (t) => {
const throws = () => {
throw new Error("Should not be called");
};
const fetcher = new Fetcher(throws, throws);
t.deepEqual(getObjectProperties(fetcher), ["fetch"]);
});
test("BindingsPlugin: dispatches fetch to mounted service", async (t) => {
const mf = useMiniflare(
{ BindingsPlugin },
{
name: "a",
modules: true,
script: `export default {
fetch(request, env) {
const { pathname } = new URL(request.url);
if (pathname === "/ping") {
return new Response("pong");
}
return env.SERVICE_B.fetch("http://localhost/test", { method: "POST" });
}
}`,
serviceBindings: {
SERVICE_B: { service: "b", environment: "production" },
},
mounts: {
b: {
name: "b",
modules: true,
script: `export default {
async fetch(request, env) {
const res = await env.SERVICE_A.fetch("http://localhost/ping");
const text = await res.text();
return new Response(request.method + " " + request.url + ":" + text);
}
}`,
// Implicitly testing service binding shorthand
serviceBindings: { SERVICE_A: "a" },
},
},
}
);
const res = await mf.dispatchFetch("http://localhost/");
t.is(await res.text(), "POST http://localhost/test:pong");
});
test("BindingsPlugin: dispatches fetch to custom service", async (t) => {
const plugin = new BindingsPlugin(ctx, {
serviceBindings: {
async SERVICE(request) {
return new Response(`${request.method} ${request.url}`);
},
},
});
const { bindings } = await plugin.setup();
let res = await bindings!.SERVICE.fetch("http://localhost/", {
method: "POST",
});
t.is(await res.text(), "POST http://localhost/");
// No need to run beforeReload()/reload() hooks here, but just check that
// running them doesn't break anything
plugin.beforeReload();
plugin.reload({}, {}, new Map());
res = await bindings!.SERVICE.fetch("http://localhost/test");
t.is(await res.text(), "GET http://localhost/test");
});
test("BindingsPlugin: waits for services before dispatching", async (t) => {
const plugin = new BindingsPlugin(ctx, {
// Implicitly testing service binding without environment
serviceBindings: { SERVICE: { service: "a" } },
});
const { bindings } = await plugin.setup();
plugin.beforeReload();
// Simulate fetching before reload complete
const res = bindings!.SERVICE.fetch("http://localhost/");
await setImmediate();
const mount: _CoreMount = {
dispatchFetch: async () => new Response("a service"),
};
plugin.reload({}, {}, new Map([["a", mount]]));
t.is(await (await res).text(), "a service");
});
test("BindingsPlugin: reload: throws if service isn't mounted", async (t) => {
let plugin = new BindingsPlugin(ctx, {
serviceBindings: { SERVICE: "a" },
});
await plugin.setup();
plugin.beforeReload();
t.throws(() => plugin.reload({}, {}, new Map()), {
instanceOf: MiniflareCoreError,
code: "ERR_SERVICE_NOT_MOUNTED",
message:
'Service "a" for binding "SERVICE" not found.\nMake sure "a" is mounted so Miniflare knows where to find it.',
});
// Check doesn't throw if using custom fetch function
plugin = new BindingsPlugin(ctx, {
serviceBindings: { SERVICE: () => new Response() },
});
await plugin.setup();
plugin.beforeReload();
plugin.reload({}, {}, new Map());
t.pass();
});
test("BindingsPlugin: reloads service bindings used by mount when another mounted worker reloads", async (t) => {
const mf = useMiniflare(
{ BindingsPlugin },
{
mounts: {
a: {
modules: true,
routes: ["*"],
script: `export default {
async fetch(request, env) {
const res = await env.SERVICE_B.fetch("http://localhost/");
return new Response("a" + await res.text());
}
}`,
serviceBindings: { SERVICE_B: "b" },
},
b: {
modules: true,
script: `export default { fetch: () => new Response("b1") }`,
},
},
}
);
let res = await mf.dispatchFetch("http://localhost/");
t.is(await res.text(), "ab1");
// Update "b" and check new response
const b = await mf.getMount("b");
await b.setOptions({
script: `export default { fetch: () => new Response("b2") }`,
});
res = await mf.dispatchFetch("http://localhost/");
t.is(await res.text(), "ab2");
});
test("BindingsPlugin: passes through when service doesn't respond", async (t) => {
const upstream = (await useServer(t, (req, res) => res.end("upstream"))).http;
const mf = useMiniflare(
{ BindingsPlugin },
{
name: "parent",
script: 'addEventListener("fetch", () => {})',
mounts: {
a: { script: 'addEventListener("fetch", () => {})' },
b: {
modules: true,
script: `export default {
fetch(request, env, ctx) {
ctx.passThroughOnException();
throw new Error("oops");
}
}`,
},
c: {
modules: true,
routes: ["*/*"],
script: `export default {
async fetch(request, env) {
const { pathname } = new URL(request.url);
const name = pathname === "/a" ? "SERVICE_A"
: pathname === "/b" ? "SERVICE_B" : "SERVICE_PARENT";
const service = env[name];
const res = await service.fetch(${JSON.stringify(upstream)});
return new Response(name + ":" + await res.text());
}
}`,
serviceBindings: {
SERVICE_A: "a",
SERVICE_B: "b",
SERVICE_PARENT: "parent",
},
},
},
}
);
// Check with both another mounted service and the parent and when
// passThroughOnException() is called
let res = await mf.dispatchFetch("http://localhost/a");
t.is(await res.text(), "SERVICE_A:upstream");
res = await mf.dispatchFetch("http://localhost/b");
t.is(await res.text(), "SERVICE_B:upstream");
res = await mf.dispatchFetch("http://localhost/parent");
t.is(await res.text(), "SERVICE_PARENT:upstream");
});
test("BindingsPlugin: propagates error if service throws", async (t) => {
const mf = useMiniflare(
{ BindingsPlugin },
{
modules: true,
script: `export default {
async fetch(request, env) {
await env.SERVICE.fetch(request);
return new Response("body");
} ,
}`,
serviceBindings: { SERVICE: "a" },
mounts: {
a: {
modules: true,
script: `export default {
fetch: () => { throw new Error("oops"); },
}`,
},
},
}
);
await t.throwsAsync(mf.dispatchFetch("http://localhost/"), {
message: "oops",
});
});
test("BindingsPlugin: service fetch creates new request context", async (t) => {
// noinspection JSUnusedGlobalSymbols
const bindings = {
assertSubrequests(expected: number) {
t.is(getRequestContext()?.subrequests, expected);
},
};
const mf = useMiniflare(
{ BindingsPlugin, CachePlugin },
{
bindings,
serviceBindings: { SERVICE: "a" },
modules: true,
script: `export default {
async fetch(request, env) {
env.assertSubrequests(0);
await caches.default.match("http://localhost/");
env.assertSubrequests(1);
return await env.SERVICE.fetch(request);
},
}`,
mounts: {
a: {
bindings,
modules: true,
script: `export default {
async fetch(request, env) {
env.assertSubrequests(0);
await caches.default.match("http://localhost/");
env.assertSubrequests(1);
const n = parseInt(new URL(request.url).searchParams.get("n"));
await Promise.all(
Array.from(Array(n)).map(() => caches.default.match("http://localhost/"))
);
return new Response("body");
},
}`,
},
},
}
);
await t.throwsAsync(mf.dispatchFetch("http://localhost/?n=50"), {
instanceOf: Error,
message: /^Too many subrequests/,
});
const res = await mf.dispatchFetch("http://localhost/?n=1");
t.is(await res.text(), "body");
});
test("BindingsPlugin: service fetch increases pipeline depth", async (t) => {
const depths: [request: number, pipeline: number][] = [];
const mf = useMiniflare(
{ BindingsPlugin },
{
bindings: {
recordDepth() {
const ctx = getRequestContext()!;
depths.push([ctx.requestDepth, ctx.pipelineDepth]);
},
},
modules: true,
name: "service",
serviceBindings: { SERVICE: "service" },
script: `export default {
async fetch(request, env) {
env.recordDepth();
const url = new URL(request.url);
const n = parseInt(url.searchParams.get("n") ?? "0");
if (n === 0) return new Response("end");
url.searchParams.set("n", n - 1);
const res = await env.SERVICE.fetch(url);
return new Response(\`\${n},\${await res.text()}\`);
}
}`,
}
);
const res = await mf.dispatchFetch("http://localhost/?n=3");
t.is(await res.text(), "3,2,1,end");
t.deepEqual(depths, [
[1, 1],
[1, 2],
[1, 3],
[1, 4],
]);
await mf.dispatchFetch("http://localhost/?n=31"); // Shouldn't throw
await t.throwsAsync(mf.dispatchFetch("http://localhost/?n=32"), {
instanceOf: Error,
message:
/^Subrequest depth limit exceeded.+\nService bindings can recurse up to 32 times\./,
});
});