🆕 Add new script wait-port-ready.ts and update metadata file with its details
This commit is contained in:
@@ -392,6 +392,15 @@
|
||||
"publish_time": 1775409332538,
|
||||
"update_time": 1775409332538
|
||||
},
|
||||
"wait-port-ready.ts": {
|
||||
"script_name": "wait-port-ready.ts",
|
||||
"script_length": 2237,
|
||||
"script_sha256": "d9e523a45667982700e7cad6010d4f8109ccd4e7d03881417f83bc2ce4dd717d",
|
||||
"script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/wait-port-ready.ts",
|
||||
"single_script_file": true,
|
||||
"publish_time": 1776514630023,
|
||||
"update_time": 1776514630023
|
||||
},
|
||||
"wget.ts": {
|
||||
"script_name": "wget.ts",
|
||||
"script_length": 5289,
|
||||
|
||||
82
single-scripts/wait-port-ready.ts
Executable file
82
single-scripts/wait-port-ready.ts
Executable file
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env runts -- --allow-all
|
||||
|
||||
import {parseArgs} from "jsr:@std/cli/parse-args";
|
||||
import {exit, log} from "../libraries/deno-commons-mod.ts";
|
||||
import net from "node:net";
|
||||
|
||||
function parseFlags(): any {
|
||||
const flags = parseArgs(Deno.args, {
|
||||
boolean: ["help"],
|
||||
string: ["port", "timeout"],
|
||||
});
|
||||
const helpMessage = `wait-port-ready.ts - wait assigned port ready
|
||||
|
||||
wait-port-ready.ts --port PORT [--timeout TIMEOUT] wait for PORT with TIMEOUT(default 120 seconds)
|
||||
`;
|
||||
if (flags.help) {
|
||||
console.log(helpMessage);
|
||||
exit(0);
|
||||
}
|
||||
return flags;
|
||||
}
|
||||
|
||||
async function tryConnection(port, host = "127.0.0.1") {
|
||||
return new Promise((resolve, reject) => {
|
||||
const socket = new net.Socket();
|
||||
socket.setTimeout(1000);
|
||||
socket.connect(port, host, () => {
|
||||
socket.destroy();
|
||||
resolve(true);
|
||||
});
|
||||
socket.on("error", (err) => {
|
||||
socket.destroy();
|
||||
reject(err);
|
||||
});
|
||||
socket.on("timeout", () => {
|
||||
socket.destroy();
|
||||
reject(new Error("Connect timeout"));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const flags = parseFlags();
|
||||
if (!flags["port"]) {
|
||||
log.error("--port is required");
|
||||
exit(1);
|
||||
}
|
||||
const port = parseInt(flags.port, 10);
|
||||
if (isNaN(port) || port < 1 || port > 65535) {
|
||||
log.error("invalid port");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
const timeout = parseInt(flags.timeout ?? "120", 10);
|
||||
if (isNaN(timeout) || timeout < 0) {
|
||||
log.error("invalid timeout");
|
||||
exit(1);
|
||||
}
|
||||
const timeoutMs = timeout * 1000;
|
||||
const now = Date.now();
|
||||
|
||||
do {
|
||||
try {
|
||||
await tryConnection(port);
|
||||
log.success("wait port ready successfully");
|
||||
exit(0);
|
||||
} catch (e) {
|
||||
log.debug("try connection failed", e);
|
||||
}
|
||||
} while (Date.now() - now < timeoutMs);
|
||||
|
||||
log.error("wait port ready timeout");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
log.error(err);
|
||||
exit(1);
|
||||
}).then(() => exit(0));
|
||||
|
||||
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260418T201602+08:00.MEUCIQDpTub0iUDo7BL+2OOu
|
||||
// h9QWno5DgrxAQHPORcan1tiWGQIgbvQTCm/IoLMt6GE1p2X/jt/tph02Uz2cem0Q9MRmvZo=
|
||||
Reference in New Issue
Block a user