mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-13 04:30:16 +08:00
74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
/* The served-chain shapes (portless serves leaf+CA concatenated, its
|
|
* SNI contexts include the CA): an untrusting client refuses with
|
|
* Node's "self-signed certificate in certificate chain"; trusting the
|
|
* right CA connects. */
|
|
import { readFileSync } from "node:fs";
|
|
import { createServer } from "node:net";
|
|
import { request } from "node:https";
|
|
import type { IncomingMessage } from "node:http";
|
|
|
|
const ca = readFileSync("tests/fixtures/server/certs/ca.pem");
|
|
|
|
let port = 0;
|
|
let attempts = 0;
|
|
|
|
function onRes(res: IncomingMessage, path: string, mode: string, next: () => void): void {
|
|
const sc = res.statusCode;
|
|
let body = "";
|
|
res.on("data", (chunk: Buffer) => {
|
|
body += chunk.toString("utf8");
|
|
});
|
|
res.on("end", () => {
|
|
console.log(`${mode} ${path} status=${sc !== undefined ? sc : -1} body=${body}`);
|
|
next();
|
|
});
|
|
}
|
|
|
|
function tryGet(path: string, mode: string, next: () => void): void {
|
|
const req = mode === "default"
|
|
? request({ hostname: "localhost", port, path, method: "GET" }, (res) => onRes(res, path, mode, next))
|
|
: mode === "right-ca"
|
|
? request({ hostname: "localhost", port, path, method: "GET", ca }, (res) => onRes(res, path, mode, next))
|
|
: request({ hostname: "localhost", port, path, method: "GET", rejectUnauthorized: false }, (res) => onRes(res, path, mode, next));
|
|
req.on("error", (err) => {
|
|
console.log(`${mode} ${path} error ${err.message}`);
|
|
next();
|
|
});
|
|
req.end();
|
|
}
|
|
|
|
function start(): void {
|
|
attempts++;
|
|
const req = request(
|
|
{ hostname: "localhost", port, path: "/ready", method: "GET", rejectUnauthorized: false },
|
|
(res) => {
|
|
res.on("end", () => {
|
|
console.log("driver up");
|
|
tryGet("/text", "default", () => {
|
|
tryGet("/text", "right-ca", () => {
|
|
tryGet("/quit", "right-ca", () => console.log("done"));
|
|
});
|
|
});
|
|
});
|
|
res.on("data", () => {});
|
|
},
|
|
);
|
|
req.on("error", () => {
|
|
if (attempts < 400) {
|
|
setTimeout(start, 25);
|
|
} else {
|
|
console.log("driver never came up");
|
|
}
|
|
});
|
|
req.end();
|
|
}
|
|
|
|
const probe = createServer();
|
|
probe.listen(0, () => {
|
|
port = probe.address().port;
|
|
probe.close(() => {
|
|
process.stderr.write(`PORT ${port}\n`);
|
|
start();
|
|
});
|
|
});
|