





















@@ -53,26 +53,39 @@ function readPositiveInt(raw, fallback, label) {
5353async function withClickClackFixtureResponse(url, init, consume, options = {}) {
5454const timeoutMs = options.timeoutMs ?? clickClackHttpTimeoutMs();
5555const controller = new AbortController();
56-const timer = setTimeout(() => {
57-controller.abort();
58-}, timeoutMs);
56+const timeoutError = new Error(`${url} timed out after ${timeoutMs}ms`);
57+let timer;
58+const timeoutPromise = new Promise((_resolve, reject) => {
59+timer = setTimeout(() => {
60+controller.abort(timeoutError);
61+reject(timeoutError);
62+}, timeoutMs);
63+});
5964try {
60-const response = await fetch(url, {
61- ...init,
62-signal: controller.signal,
63-});
64-return await consume(response);
65+const response = await Promise.race([
66+fetch(url, {
67+ ...init,
68+signal: controller.signal,
69+}),
70+timeoutPromise,
71+]);
72+return await consume(response, { timeoutPromise });
6573} finally {
6674clearTimeout(timer);
6775}
6876}
697770-async function readBoundedResponseText(response, label, byteLimit = clickClackHttpBodyMaxBytes()) {
71-return await readBoundedResponseTextWithLimit(response, label, byteLimit);
78+async function readBoundedResponseText(
79+response,
80+label,
81+byteLimit = clickClackHttpBodyMaxBytes(),
82+options = {},
83+) {
84+return await readBoundedResponseTextWithLimit(response, label, byteLimit, options.timeoutPromise);
7285}
738674-async function readBoundedResponseJson(response, label) {
75-return JSON.parse(await readBoundedResponseText(response, label));
87+async function readBoundedResponseJson(response, label, options = {}) {
88+return JSON.parse(await readBoundedResponseText(response, label, undefined, options));
7689}
77907891function resolveHomePath(value) {
@@ -278,8 +291,10 @@ async function postClickClackInbound() {
278291headers: { "content-type": "application/json" },
279292body: JSON.stringify({ body }),
280293},
281-async (response) => {
282-const text = response.ok ? "" : await readBoundedResponseText(response, "ClickClack inbound");
294+async (response, options) => {
295+const text = response.ok
296+ ? ""
297+ : await readBoundedResponseText(response, "ClickClack inbound", undefined, options);
283298assert(response.ok, `fixture inbound failed: ${response.status} ${text}`);
284299},
285300);
@@ -298,9 +313,9 @@ async function waitClickClackSocket() {
298313const state = await withClickClackFixtureResponse(
299314`${baseUrl}/fixture/state`,
300315{},
301-async (response) =>
316+async (response, options) =>
302317response.ok
303- ? await readBoundedResponseJson(response, "ClickClack fixture state")
318+ ? await readBoundedResponseJson(response, "ClickClack fixture state", options)
304319 : undefined,
305320{
306321timeoutMs: Math.min(clickClackHttpTimeoutMs(), remainingMs),
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。