-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyn-server.ts
45 lines (40 loc) · 1016 Bytes
/
dyn-server.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
type HasLength = { length: number };
type WithModeRequest<Result> = {
request: Request;
mode: string;
action: (request: Request) => Result;
};
function reallyPerform<Task extends HasLength>(
request: Request,
task: Task,
): number {
const mode = request.headers.get("mode");
console.log(`${mode}: ${task}`);
return task.length;
}
function perform<Task extends HasLength>(
request: Request,
task: Task,
): number {
return reallyPerform(request, task);
}
function withMode<Result>(request: WithModeRequest<Result>): Result {
const next = request.request.clone();
next.headers.set("mode", request.mode);
return request.action(next);
}
export default {
port: 3000,
fetch(request: Request) {
request.headers.set("mode", "safe");
return new Response([
perform(request, "something"),
withMode({
request,
mode: "faster",
action: (request) => perform(request, "reliable"),
}),
perform(request, ["again"]),
].join(" "));
},
};