locadot
Version:
Secure your local development environment with HTTPS and custom domains like dev.localhost.
37 lines (36 loc) • 1.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const https_1 = __importDefault(require("https"));
class Localhost {
static async isLocalhostOpen(domain, port) {
return new Promise((resolve) => {
const req = https_1.default.request({
hostname: "localhost",
port,
method: "HEAD",
rejectUnauthorized: false,
timeout: 1000,
headers: {
Host: domain,
},
}, (res) => {
resolve(res.statusCode >= 200 && res.statusCode < 400);
res.destroy();
});
req.on("error", () => resolve(false));
req.on("timeout", () => {
req.destroy();
resolve(false);
});
req.end();
});
}
static isValidLocalhostDomain(domain) {
const localhostPattern = /^(?:[a-zA-Z0-9-]+\.)*localhost$/;
return localhostPattern.test(domain);
}
}
exports.default = Localhost;