@gguf/claw
Version:
WhatsApp gateway CLI (Baileys web) with Pi RPC agent
160 lines (156 loc) • 3.93 kB
JavaScript
import { t as registerBrowserRoutes } from "./routes-BSfXf8a5.js";
import { execFile } from "node:child_process";
import os from "node:os";
import { promisify } from "node:util";
import fs from "node:fs/promises";
//#region src/infra/machine-name.ts
const execFileAsync = promisify(execFile);
let cachedPromise = null;
async function tryScutil(key) {
try {
const { stdout } = await execFileAsync("/usr/sbin/scutil", ["--get", key], {
timeout: 1e3,
windowsHide: true
});
const value = String(stdout ?? "").trim();
return value.length > 0 ? value : null;
} catch {
return null;
}
}
function fallbackHostName() {
return os.hostname().replace(/\.local$/i, "").trim() || "openclaw";
}
async function getMachineDisplayName() {
if (cachedPromise) return cachedPromise;
cachedPromise = (async () => {
if (process.env.VITEST || false) return fallbackHostName();
if (process.platform === "darwin") {
const computerName = await tryScutil("ComputerName");
if (computerName) return computerName;
const localHostName = await tryScutil("LocalHostName");
if (localHostName) return localHostName;
}
return fallbackHostName();
})();
return cachedPromise;
}
//#endregion
//#region src/browser/routes/dispatcher.ts
function escapeRegex(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function compileRoute(path) {
const paramNames = [];
const parts = path.split("/").map((part) => {
if (part.startsWith(":")) {
const name = part.slice(1);
paramNames.push(name);
return "([^/]+)";
}
return escapeRegex(part);
});
return {
regex: new RegExp(`^${parts.join("/")}$`),
paramNames
};
}
function createRegistry() {
const routes = [];
const register = (method) => (path, handler) => {
const { regex, paramNames } = compileRoute(path);
routes.push({
method,
path,
regex,
paramNames,
handler
});
};
return {
routes,
router: {
get: register("GET"),
post: register("POST"),
delete: register("DELETE")
}
};
}
function normalizePath(path) {
if (!path) return "/";
return path.startsWith("/") ? path : `/${path}`;
}
function createBrowserRouteDispatcher(ctx) {
const registry = createRegistry();
registerBrowserRoutes(registry.router, ctx);
return { dispatch: async (req) => {
const method = req.method;
const path = normalizePath(req.path);
const query = req.query ?? {};
const body = req.body;
const match = registry.routes.find((route) => {
if (route.method !== method) return false;
return route.regex.test(path);
});
if (!match) return {
status: 404,
body: { error: "Not Found" }
};
const exec = match.regex.exec(path);
const params = {};
if (exec) for (const [idx, name] of match.paramNames.entries()) {
const value = exec[idx + 1];
if (typeof value === "string") params[name] = decodeURIComponent(value);
}
let status = 200;
let payload = void 0;
const res = {
status(code) {
status = code;
return res;
},
json(bodyValue) {
payload = bodyValue;
}
};
try {
await match.handler({
params,
query,
body
}, res);
} catch (err) {
return {
status: 500,
body: { error: String(err) }
};
}
return {
status,
body: payload
};
} };
}
//#endregion
//#region src/infra/wsl.ts
let wslCached = null;
function isWSLEnv() {
if (process.env.WSL_INTEROP || process.env.WSL_DISTRO_NAME || process.env.WSLENV) return true;
return false;
}
async function isWSL() {
if (wslCached !== null) return wslCached;
if (isWSLEnv()) {
wslCached = true;
return wslCached;
}
try {
const release = await fs.readFile("/proc/sys/kernel/osrelease", "utf8");
wslCached = release.toLowerCase().includes("microsoft") || release.toLowerCase().includes("wsl");
} catch {
wslCached = false;
}
return wslCached;
}
//#endregion
export { getMachineDisplayName as i, isWSLEnv as n, createBrowserRouteDispatcher as r, isWSL as t };