openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 1.59 kB
JavaScript
import{r as captureOpenClawStateWorkerContext}from"./openclaw-state-worker-context-Dx8YjqO2.mjs";import{a as AUTH_RATE_LIMIT_SCOPE_DEVICE_JOIN}from"./auth-rate-limit-CJ17nIGS.mjs";import{n as withSerializedRateLimitAttempt}from"./rate-limit-attempt-serialization-CLMEob1y.mjs";import{t as sendJson}from"./http-json-g_k96dZ_.mjs";import{t as isDevicePairingJoinCode}from"./join-code-D4rNoELt.mjs";import"./http-common-kI7-c_LU.mjs";import{t as redeemDevicePairingJoinCode}from"./device-pairing-join-code-DW7fYX2E.mjs";
//!src/gateway/device-pairing-join-http.ts
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}export{handleDevicePairingJoinHttpRequest};