1+ import { buildTimeoutAbortSignal } from "openclaw/plugin-sdk/extension-shared" ;
12import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http" ;
23import WebSocket from "ws" ;
34import { sha256Hex , signDeviceRequest , utf8 } from "../protocol/index.js" ;
@@ -21,6 +22,9 @@ const REEF_INBOX_LIVE_BUFFER_MAX_ENTRIES = 256;
2122// Stalled TCP peers that never complete the HTTP upgrade would otherwise hang
2223// forever — ws defaults to no handshakeTimeout. Match sibling channel WS budgets.
2324const REEF_WS_HANDSHAKE_MS = 30_000 ;
25+ // Cover headers and body consumption. A relay that accepts the request but
26+ // stops producing bytes must not pin inbox recovery forever.
27+ const REEF_RELAY_REQUEST_TIMEOUT_MS = 15_000 ;
2428
2529export class ReefRelayError extends Error {
2630 constructor (
@@ -58,6 +62,7 @@ export class ReefTransportClient {
5862 readonly keys : ReefKeys ,
5963 readonly fetcher : FetchLike = fetch ,
6064 readonly clock : ( ) => number = ( ) => Math . floor ( Date . now ( ) / 1000 ) ,
65+ readonly requestTimeoutMs : number = REEF_RELAY_REQUEST_TIMEOUT_MS ,
6166 ) { }
6267
6368 async authStart ( email : string ) : Promise < { status : string ; magicLink ?: string } > {
@@ -181,35 +186,49 @@ export class ReefTransportClient {
181186 headers : Record < string , string > ,
182187 signal ?: AbortSignal ,
183188 ) : Promise < T > {
184- const response = await this . fetcher ( new URL ( path , this . relayUrl ) , {
185- method,
186- headers : { ...headers , ...( bytes . length ? { "content-type" : "application/json" } : { } ) } ,
187- ...( bytes . length ? { body : bytes as BodyInit } : { } ) ,
188- ...( signal ? { signal } : { } ) ,
189+ const url = new URL ( path , this . relayUrl ) . toString ( ) ;
190+ const timeout = buildTimeoutAbortSignal ( {
191+ timeoutMs : this . requestTimeoutMs ,
192+ signal,
193+ operation : "reef.relay" ,
194+ url,
189195 } ) ;
190- if ( ! response . ok ) {
191- let message = `relay HTTP ${ response . status } ` ;
192- try {
193- const parsed = await readProviderJsonResponse < { error ?: string } > (
194- response ,
195- "reef.relay.error" ,
196- { maxBytes : REEF_RELAY_ERROR_JSON_MAX_BYTES } ,
197- ) ;
198- if ( typeof parsed . error === "string" && parsed . error ) {
199- message = parsed . error ;
196+ try {
197+ const response = await this . fetcher ( url , {
198+ method,
199+ headers : { ...headers , ...( bytes . length ? { "content-type" : "application/json" } : { } ) } ,
200+ ...( bytes . length ? { body : bytes as BodyInit } : { } ) ,
201+ signal : timeout . signal ,
202+ } ) ;
203+ if ( ! response . ok ) {
204+ let message = `relay HTTP ${ response . status } ` ;
205+ try {
206+ const parsed = await readProviderJsonResponse < { error ?: string } > (
207+ response ,
208+ "reef.relay.error" ,
209+ { maxBytes : REEF_RELAY_ERROR_JSON_MAX_BYTES } ,
210+ ) ;
211+ if ( typeof parsed . error === "string" && parsed . error ) {
212+ message = parsed . error ;
213+ }
214+ } catch {
215+ if ( timeout . signal ?. aborted ) {
216+ throw timeout . signal . reason ;
217+ }
218+ // Keep the status fallback when the error body is missing, malformed,
219+ // or oversized; callers still get a typed ReefRelayError.
200220 }
201- } catch {
202- // Keep the status fallback when the error body is missing, malformed,
203- // or oversized; callers still get a typed ReefRelayError.
221+ throw new ReefRelayError ( response . status , message ) ;
204222 }
205- throw new ReefRelayError ( response . status , message ) ;
206- }
207- if ( response . status === 204 ) {
208- return undefined as T ;
223+ if ( response . status === 204 ) {
224+ return undefined as T ;
225+ }
226+ return await readProviderJsonResponse < T > ( response , "reef.relay" , {
227+ maxBytes : REEF_RELAY_JSON_MAX_BYTES ,
228+ } ) ;
229+ } finally {
230+ timeout . cleanup ( ) ;
209231 }
210- return await readProviderJsonResponse < T > ( response , "reef.relay" , {
211- maxBytes : REEF_RELAY_JSON_MAX_BYTES ,
212- } ) ;
213232 }
214233}
215234
0 commit comments