openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
49 lines (48 loc) • 1.94 kB
JavaScript
import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs";
import { r as AUTH_RATE_LIMIT_SCOPE_DEVICE_JOIN } from "./auth-rate-limit-DGfPTrXr.mjs";
import { n as withSerializedRateLimitAttempt } from "./rate-limit-attempt-serialization-B_N04LLR.mjs";
import { t as isDevicePairingJoinCode } from "./join-code-zMzzNXYS.mjs";
import { t as sendJson } from "./http-json-ddGJz5CA.mjs";
import "./http-common-FfhIIwOW.mjs";
import { t as redeemDevicePairingJoinCode } from "./device-pairing-join-code-Bz52UdzO.mjs";
//#region src/gateway/device-pairing-join-http.ts
/** Handle the core-owned /j namespace before hooks, plugins, and the Control UI SPA. */
async function handleDevicePairingJoinHttpRequest(e) {
const t = URL.parse(e.req.url ?? "/", "http://localhost");
e.res.setHeader("Cache-Control", "no-store");
const n = e.req.method === "GET" && !t?.search && isDevicePairingJoinCode(e.shortcode);
let r;
let i;
if (n) try {
r = captureOpenClawStateWorkerContext();
} catch (e) {
i = { error: e };
}
await withSerializedRateLimitAttempt({
ip: e.clientIp,
scope: AUTH_RATE_LIMIT_SCOPE_DEVICE_JOIN,
run: async () => {
const t = e.rateLimiter?.check(e.clientIp, AUTH_RATE_LIMIT_SCOPE_DEVICE_JOIN);
if (t && !t.allowed) {
if (t.retryAfterMs > 0) e.res.setHeader("Retry-After", String(Math.ceil(t.retryAfterMs / 1e3)));
sendJson(e.res, 429, { error: "rate_limited" });
return;
}
if (i) throw i.error;
const a = n ? await redeemDevicePairingJoinCode({
shortcode: e.shortcode,
context: r
}) : null;
if (!a) {
e.rateLimiter?.recordFailure(e.clientIp, AUTH_RATE_LIMIT_SCOPE_DEVICE_JOIN);
sendJson(e.res, 404, { error: "not_found" });
return;
}
e.rateLimiter?.reset(e.clientIp, AUTH_RATE_LIMIT_SCOPE_DEVICE_JOIN);
sendJson(e.res, 200, a);
}
});
return true;
}
//#endregion
export { handleDevicePairingJoinHttpRequest };