openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
97 lines (96 loc) • 2.16 kB
JavaScript
import { t as escapeRegExp } from "./regexp-dj3rRPbj.mjs";
import "./text-utility-runtime-Ds8jsG0J.mjs";
import { a as normalizeBrowserRequestPath, t as registerBrowserRoutes } from "./routes-BC1rdkyd.mjs";
//#region extensions/browser/src/browser/routes/dispatcher.ts
/**
* Browser route dispatcher.
*
* Provides an in-process request/response adapter so Gateway nodes can invoke
* the same route handlers without opening an HTTP socket.
*/
function compileRoute(e) {
const t = [];
const n = e.split("/").map((e) => {
if (e.startsWith(":")) {
const n = e.slice(1);
t.push(n);
return "([^/]+)";
}
return escapeRegExp(e);
});
return {
regex: new RegExp(`^${n.join("/")}$`),
paramNames: t
};
}
function createBrowserRouteDispatcher(e) {
const t = [];
const register = (e) => (n, r) => {
const { regex: i, paramNames: a } = compileRoute(n);
t.push({
method: e,
regex: i,
paramNames: a,
handler: r
});
};
registerBrowserRoutes({
get: register("GET"),
post: register("POST"),
delete: register("DELETE")
}, e);
return { dispatch: async (e) => {
const n = normalizeBrowserRequestPath(e.path) || "/";
const r = t.find((t) => t.method === e.method && t.regex.test(n));
if (!r) return {
status: 404,
body: { error: "Not Found" }
};
const i = r.regex.exec(n);
const a = {};
if (i) for (const [e, t] of r.paramNames.entries()) {
const n = i[e + 1];
if (typeof n === "string") try {
a[t] = decodeURIComponent(n);
} catch {
return {
status: 400,
body: { error: `invalid path parameter encoding: ${t}` }
};
}
}
let o = 200;
let s = void 0;
const c = {
status(e) {
o = e;
return c;
},
json(e) {
s = e;
}
};
try {
await r.handler({
params: a,
query: e.query ?? {},
body: e.body,
signal: e.signal,
requester: e.requester,
assertCurrent: e.assertCurrent,
screencastAuthority: e.screencastAuthority
}, c);
} catch (e) {
return {
status: 500,
body: { error: String(e) }
};
}
return {
status: o,
body: s
};
} };
}
//#endregion
export { createBrowserRouteDispatcher as t };