-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathindex.ts
More file actions
46 lines (44 loc) · 1.52 KB
/
index.ts
File metadata and controls
46 lines (44 loc) · 1.52 KB
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
// Worker: makes one outbound fetch to itself, catches RateLimitError, and
// returns { retryAfterMs } in JSON so the integration test can verify the field.
//
// Requests with "x-skip: 1" are terminal — they return immediately without
// making an outbound call, so the worker does not recurse indefinitely.
Deno.serve(async (req: Request) => {
const serverUrl = req.headers.get("x-test-server-url");
if (!serverUrl) {
return new Response(
JSON.stringify({ msg: "missing x-test-server-url header" }),
{ status: 400, headers: { "Content-Type": "application/json" } },
);
}
// Terminal hop: just acknowledge, no outbound call.
if (req.headers.get("x-skip") === "1") {
return new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
try {
await fetch(`${serverUrl}/rate-limit-retry-after`, {
headers: {
"x-test-server-url": serverUrl,
"x-skip": "1",
},
});
return new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
} catch (e) {
if (e instanceof Deno.errors.RateLimitError) {
return new Response(
JSON.stringify({ name: e.name, retryAfterMs: e.retryAfterMs }),
{ status: 429, headers: { "Content-Type": "application/json" } },
);
}
return new Response(
JSON.stringify({ msg: String(e) }),
{ status: 500, headers: { "Content-Type": "application/json" } },
);
}
});