add server-control.ts

This commit is contained in:
2026-02-01 01:04:35 +08:00
parent 3c6fd6eb9e
commit 015a7d1581
2 changed files with 326 additions and 4 deletions

View File

@@ -1,10 +1,10 @@
// Reference:
// - https://docs.deno.com/runtime/fundamentals/testing/
import { assert } from "jsr:@std/assert/assert";
import { assertEquals } from "jsr:@std/assert";
import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64";
import { dirname } from "https://deno.land/std@0.208.0/path/mod.ts";
import {assert} from "jsr:@std/assert/assert";
import {assertEquals} from "jsr:@std/assert";
import {decodeBase64, encodeBase64} from "jsr:@std/encoding/base64";
import {dirname} from "https://deno.land/std@0.208.0/path/mod.ts";
// reference: https://docs.deno.com/examples/hex_base64_encoding/
// import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64";
@@ -569,6 +569,26 @@ export class ProcessBar {
}
}
export async function fetchWithTimeout(
input: URL | Request | string,
timeout?: number,
initCallback?: (init: RequestInit) => RequestInit,
): Promise<Response> {
const fetchTimeout = timeout || 10000;
const abortController = new AbortController();
const timeoutHandler = setTimeout(() => {
abortController.abort(`Timeout ${fetchTimeout} ms`);
}, fetchTimeout);
let init: RequestInit = {};
init.signal = abortController.signal;
if (initCallback) {
init = initCallback(init);
}
const response = await fetch(input, init);
clearTimeout(timeoutHandler);
return response;
}
Deno.test("isOn", () => {
assertEquals(false, isOn(undefined));
assertEquals(false, isOn(""));