@shopify/cli
Version:
A CLI tool to build for the Shopify platform
108 lines (103 loc) • 4.37 kB
JavaScript
import {
init_cjs_shims
} from "./chunk-PKR7KJ6P.js";
// ../../node_modules/.pnpm/@shopify+cli-hydrogen@10.0.0_@graphql-codegen+cli@5.0.4_react-dom@17.0.2_react@17.0.2/node_modules/@shopify/cli-hydrogen/dist/lib/find-port.js
init_cjs_shims();
// ../../node_modules/.pnpm/get-port@7.1.0/node_modules/get-port/index.js
init_cjs_shims();
import net from "node:net";
import os from "node:os";
var Locked = class extends Error {
constructor(port) {
super(`${port} is locked`);
}
}, lockedPorts = {
old: /* @__PURE__ */ new Set(),
young: /* @__PURE__ */ new Set()
}, releaseOldLockedPortsIntervalMs = 1e3 * 15, minPort = 1024, maxPort = 65535, timeout, getLocalHosts = () => {
let interfaces = os.networkInterfaces(), results = /* @__PURE__ */ new Set([void 0, "0.0.0.0"]);
for (let _interface of Object.values(interfaces))
for (let config of _interface)
results.add(config.address);
return results;
}, checkAvailablePort = (options) => new Promise((resolve, reject) => {
let server = net.createServer();
server.unref(), server.on("error", reject), server.listen(options, () => {
let { port } = server.address();
server.close(() => {
resolve(port);
});
});
}), getAvailablePort = async (options, hosts) => {
if (options.host || options.port === 0)
return checkAvailablePort(options);
for (let host of hosts)
try {
await checkAvailablePort({ port: options.port, host });
} catch (error) {
if (!["EADDRNOTAVAIL", "EINVAL"].includes(error.code))
throw error;
}
return options.port;
}, portCheckSequence = function* (ports) {
ports && (yield* ports), yield 0;
};
async function getPorts(options) {
let ports, exclude = /* @__PURE__ */ new Set();
if (options && (options.port && (ports = typeof options.port == "number" ? [options.port] : options.port), options.exclude)) {
let excludeIterable = options.exclude;
if (typeof excludeIterable[Symbol.iterator] != "function")
throw new TypeError("The `exclude` option must be an iterable.");
for (let element of excludeIterable) {
if (typeof element != "number")
throw new TypeError("Each item in the `exclude` option must be a number corresponding to the port you want excluded.");
if (!Number.isSafeInteger(element))
throw new TypeError(`Number ${element} in the exclude option is not a safe integer and can't be used`);
}
exclude = new Set(excludeIterable);
}
timeout === void 0 && (timeout = setTimeout(() => {
timeout = void 0, lockedPorts.old = lockedPorts.young, lockedPorts.young = /* @__PURE__ */ new Set();
}, releaseOldLockedPortsIntervalMs), timeout.unref && timeout.unref());
let hosts = getLocalHosts();
for (let port of portCheckSequence(ports))
try {
if (exclude.has(port))
continue;
let availablePort = await getAvailablePort({ ...options, port }, hosts);
for (; lockedPorts.old.has(availablePort) || lockedPorts.young.has(availablePort); ) {
if (port !== 0)
throw new Locked(port);
availablePort = await getAvailablePort({ ...options, port }, hosts);
}
return lockedPorts.young.add(availablePort), availablePort;
} catch (error) {
if (!["EADDRINUSE", "EACCES"].includes(error.code) && !(error instanceof Locked))
throw error;
}
throw new Error("No available ports found");
}
function portNumbers(from, to) {
if (!Number.isInteger(from) || !Number.isInteger(to))
throw new TypeError("`from` and `to` must be integer numbers");
if (from < minPort || from > maxPort)
throw new RangeError(`'from' must be between ${minPort} and ${maxPort}`);
if (to < minPort || to > maxPort)
throw new RangeError(`'to' must be between ${minPort} and ${maxPort}`);
if (from > to)
throw new RangeError("`to` must be greater than or equal to `from`");
return function* (from2, to2) {
for (let port = from2; port <= to2; port++)
yield port;
}(from, to);
}
// ../../node_modules/.pnpm/@shopify+cli-hydrogen@10.0.0_@graphql-codegen+cli@5.0.4_react-dom@17.0.2_react@17.0.2/node_modules/@shopify/cli-hydrogen/dist/lib/find-port.js
function findPort(portPreference, range = 100) {
return getPorts({
port: portNumbers(portPreference, portPreference + range)
});
}
export {
findPort
};
//# sourceMappingURL=chunk-BV6PURBM.js.map