CH-J Server Managerbainisteoireacht freastalaí SSH
Roghchlár
Foinse foilsithe

CH-J Server Manager

Brabhsáil comhadlanna agus comhaid chun feidhmchlár áirithe a scaoileadh.

Íosluchtaigh foinse ZIP
CH-J / Server Manager 339 Comhaid
core-20261014T184258Z-3fbed0b93fdf Suimigh: Dáta gan taifeadadh · Tógtha:
Comhaid
testHttpsFetch.js Suimigh: Dáta gan taifeadadh 2.9 KB · 76 líne
Íosluchtaigh comhad
1"use strict";
3const https = require("node:https");
4const { Readable } = require("node:stream");
6function normalizeOrigins(values) {
7 return new Set((values || []).map((value) => {
8 const url = new URL(String(value || ""));
9 if (url.protocol !== "https:") throw new Error("Test update transport accepts HTTPS origins only.");
10 return url.origin;
11 }));
14function responseHeaders(source) {
15 const headers = new Headers();
16 for (const [name, value] of Object.entries(source || {})) {
17 if (Array.isArray(value)) {
18 for (const item of value) headers.append(name, item);
19 } else if (value != null) {
20 headers.set(name, String(value));
21 }
22 }
23 return headers;
26function createTestHttpsFetch(options = {}) {
27 const allowedOrigins = normalizeOrigins(options.allowedBaseUrls);
28 const insecureOrigins = normalizeOrigins(options.insecureBaseUrls || []);
29 if (allowedOrigins.size === 0) throw new Error("At least one test HTTPS origin is required.");
30 for (const origin of insecureOrigins) {
31 if (!allowedOrigins.has(origin)) throw new Error("An insecure test origin must also be explicitly allowed.");
32 }
34 return function testHttpsFetch(input, init = {}) {
35 const url = new URL(typeof input === "string" || input instanceof URL ? input : input?.url);
36 if (url.protocol !== "https:" || !allowedOrigins.has(url.origin)) {
37 return Promise.reject(new Error("Test HTTPS request is outside the allowed origins."));
38 }
39 if (init.redirect && init.redirect !== "manual") {
40 return Promise.reject(new Error("Test HTTPS requests must not follow redirects automatically."));
41 }
43 return new Promise((resolve, reject) => {
44 const signal = init.signal;
45 if (signal?.aborted) {
46 reject(signal.reason || new Error("Request aborted."));
47 return;
48 }
50 // Dočasný alfa režim: výjimku lze explicitně zapnout pouze pro jediný
51 // pevně povolený HTTPS origin. Před Stable vydáním se musí odstranit.
52 const request = https.request(url, {
53 method: init.method || "GET",
54 headers: init.headers,
55 rejectUnauthorized: !insecureOrigins.has(url.origin),
56 minVersion: "TLSv1.2",
57 maxVersion: "TLSv1.3"
58 });
59 const abort = () => request.destroy(signal.reason || new Error("Request aborted."));
60 signal?.addEventListener("abort", abort, { once: true });
61 request.once("error", reject);
62 request.once("response", (incoming) => {
63 incoming.once("close", () => signal?.removeEventListener("abort", abort));
64 const bodyForbidden = init.method === "HEAD" || [204, 205, 304].includes(incoming.statusCode || 0);
65 resolve(new Response(bodyForbidden ? null : Readable.toWeb(incoming), {
66 status: incoming.statusCode || 500,
67 statusText: incoming.statusMessage || "",
68 headers: responseHeaders(incoming.headers)
69 }));
70 });
71 request.end();
72 });
73 };
76module.exports = { createTestHttpsFetch };
SHA-256bb84eefca59838805cd5f78336975a5756f5d81e3eb0b8de8cdc0bc25683bb13

CH-J Proprietary Software License 1.14 · Tá Foinse ar fáil faoin CH-J Proprietary Software License 1.14. Ní chuireann a infhaighteacht a athrú ar na téarmaí ceadúnais nó cearta breise a dheonú.

Cartlann SHA-2563df08cf5e899262a6ce92d7ec2caf5c42b683f1f2b57055662b845ff7dde67a8