11import {
22 buildGatewayConnectAuth ,
3- buildDeviceAuthPayload ,
43 ConnectErrorDetailCodes ,
54 GATEWAY_CLIENT_MODES ,
65 GATEWAY_CLIENT_NAMES ,
@@ -48,10 +47,10 @@ import {
4847 loadDeviceAuthToken ,
4948 storeDeviceAuthToken ,
5049 loadOrCreateDeviceIdentity ,
51- signDevicePayload ,
5250} from "../lib/nodes/index.ts" ;
5351import { generateUUID } from "../lib/uuid.ts" ;
5452import { createBrowserGatewaySocket } from "./gateway-browser-socket.ts" ;
53+ import { buildGatewayConnectDevice } from "./gateway-connect-device.ts" ;
5554import {
5655 enrichProtocolMismatchDetails ,
5756 resolveGatewayErrorDetailCode ,
@@ -229,51 +228,13 @@ async function deriveLegacyV4RecoveryScope(material: string | undefined): Promis
229228 }
230229}
231230
232- async function buildGatewayConnectDevice ( params : {
233- deviceIdentity : Awaited < ReturnType < typeof loadOrCreateDeviceIdentity > > | null ;
234- client : ConnectParams [ "client" ] ;
235- role : string ;
236- scopes : string [ ] ;
237- authToken ?: string ;
238- connectNonce : string | null ;
239- connectChallengeTs : number | null | undefined ;
240- } ) : Promise < NonNullable < ConnectParams [ "device" ] > | undefined > {
241- const { deviceIdentity } = params ;
242- if ( ! deviceIdentity ) {
243- return undefined ;
244- }
245- if ( params . connectChallengeTs === null ) {
246- throw new Error ( "gateway connect challenge timestamp invalid" ) ;
247- }
248- // The Control UI alone supports pre-challenge Gateways; that timeout fallback has no server time.
249- const signedAtMs = params . connectChallengeTs ?? Date . now ( ) ;
250- const nonce = params . connectNonce ?? "" ;
251- const payload = buildDeviceAuthPayload ( {
252- deviceId : deviceIdentity . deviceId ,
253- clientId : params . client . id ,
254- clientMode : params . client . mode ,
255- role : params . role ,
256- scopes : params . scopes ,
257- signedAtMs,
258- token : params . authToken ?? null ,
259- nonce,
260- } ) ;
261- const signature = await signDevicePayload ( deviceIdentity . privateKey , payload ) ;
262- return {
263- id : deviceIdentity . deviceId ,
264- publicKey : deviceIdentity . publicKey ,
265- signature,
266- signedAt : signedAtMs ,
267- nonce,
268- } ;
269- }
270-
271231export class GatewayBrowserClient {
272232 private readonly client : GatewayProtocolClient < ConnectPlan > ;
273233 private maxPayloadBytes : number | undefined ;
274234 private scopeUpgradeRuntime : Promise < GatewayScopeUpgrade > | null = null ;
275235 inboundActivitySeq = 0 ;
276236 private lastInboundActivityAtMs : number | null = null ;
237+ private maxInboundSilenceMs : number | null = null ;
277238 private tickWatchTimer : ReturnType < typeof setInterval > | null = null ;
278239 private pendingDeviceTokenRetry = false ;
279240 private deviceTokenRetryBudgetUsed = false ;
@@ -388,6 +349,15 @@ export class GatewayBrowserClient {
388349 return this . client . connected ;
389350 }
390351
352+ get needsWakeReconnect ( ) {
353+ return (
354+ ! this . client . connected ||
355+ ( this . lastInboundActivityAtMs !== null &&
356+ this . maxInboundSilenceMs !== null &&
357+ Date . now ( ) - this . lastInboundActivityAtMs > this . maxInboundSilenceMs )
358+ ) ;
359+ }
360+
391361 get recoveryScope ( ) {
392362 return this . recovery . value ;
393363 }
@@ -569,12 +539,12 @@ export class GatewayBrowserClient {
569539 : DEFAULT_GATEWAY_TICK_INTERVAL_MS ,
570540 { minMs : MIN_GATEWAY_TICK_WATCH_INTERVAL_MS } ,
571541 ) ;
542+ this . maxInboundSilenceMs = tickIntervalMs * 2 ;
572543 this . lastInboundActivityAtMs = Date . now ( ) ;
573544 this . tickWatchTimer = setInterval ( ( ) => {
574- const lastActivityAtMs = this . lastInboundActivityAtMs ;
575545 // Preserve long-running requests while real Gateway heartbeats arrive;
576546 // only a silent socket should enter the shared reconnect lifecycle.
577- if ( lastActivityAtMs !== null && Date . now ( ) - lastActivityAtMs > tickIntervalMs * 2 ) {
547+ if ( this . needsWakeReconnect ) {
578548 this . forceReconnect ( "tick timeout" ) ;
579549 }
580550 } , tickIntervalMs ) ;
@@ -586,6 +556,7 @@ export class GatewayBrowserClient {
586556 this . tickWatchTimer = null ;
587557 }
588558 this . lastInboundActivityAtMs = null ;
559+ this . maxInboundSilenceMs = null ;
589560 }
590561
591562 private handleConnectFailure ( err : GatewayProtocolRequestError , plan : ConnectPlan ) {
0 commit comments