@@ -8,7 +8,7 @@ import { getNodesTheme, runNodesCommand } from "./cli-utils.js";
88import { formatPermissions , parseNodeList , parsePairingList } from "./format.js" ;
99import { renderPendingPairingRequestsTable } from "./pairing-render.js" ;
1010import { callGatewayCli , nodesCallOpts , resolveNodeId } from "./rpc.js" ;
11- import type { NodesRpcOpts } from "./types.js" ;
11+ import type { NodeListNode , NodesRpcOpts , PairedNode } from "./types.js" ;
1212
1313function formatVersionLabel ( raw : string ) {
1414 const trimmed = raw . trim ( ) ;
@@ -97,6 +97,110 @@ function parseSinceMs(raw: unknown, label: string): number | undefined {
9797 }
9898}
9999
100+ function messageFromError ( error : unknown ) : string {
101+ if ( error instanceof Error ) {
102+ return error . message ;
103+ }
104+ if ( typeof error === "string" ) {
105+ return error ;
106+ }
107+ if (
108+ typeof error === "object" &&
109+ error !== null &&
110+ "message" in error &&
111+ typeof ( error as { message ?: unknown } ) . message === "string"
112+ ) {
113+ return ( error as { message : string } ) . message ;
114+ }
115+ if ( typeof error === "object" && error !== null ) {
116+ try {
117+ return JSON . stringify ( error ) ;
118+ } catch {
119+ return "" ;
120+ }
121+ }
122+ return "" ;
123+ }
124+
125+ function shouldFallbackToPairList ( error : unknown ) : boolean {
126+ const message = messageFromError ( error ) . toLowerCase ( ) ;
127+ return (
128+ message . includes ( "unknown method" ) ||
129+ message . includes ( "method not found" ) ||
130+ message . includes ( "invalid request" ) ||
131+ message . includes ( "missing scope: operator.read" ) ||
132+ message . includes ( "not implemented" ) ||
133+ message . includes ( "unsupported" )
134+ ) ;
135+ }
136+
137+ type NodesListEntry = NodeListNode & PairedNode ;
138+
139+ function mergePairedNodeSources ( params : {
140+ liveNodes : NodeListNode [ ] | null ;
141+ pairedNodes : PairedNode [ ] ;
142+ } ) : NodesListEntry [ ] {
143+ const merged = new Map < string , NodesListEntry > ( ) ;
144+
145+ for ( const paired of params . pairedNodes ) {
146+ merged . set ( paired . nodeId , {
147+ ...paired ,
148+ paired : true ,
149+ connected : false ,
150+ } ) ;
151+ }
152+
153+ for ( const live of params . liveNodes ?? [ ] ) {
154+ const previous = merged . get ( live . nodeId ) ;
155+ if ( ! live . paired && ! previous ?. paired ) {
156+ continue ;
157+ }
158+ merged . set ( live . nodeId , {
159+ ...previous ,
160+ ...live ,
161+ displayName : live . displayName ?? previous ?. displayName ,
162+ platform : live . platform ?? previous ?. platform ,
163+ version : live . version ?? previous ?. version ,
164+ coreVersion : live . coreVersion ?? previous ?. coreVersion ,
165+ uiVersion : live . uiVersion ?? previous ?. uiVersion ,
166+ remoteIp : live . remoteIp ?? previous ?. remoteIp ,
167+ permissions : live . permissions ?? previous ?. permissions ,
168+ paired : true ,
169+ connected : live . connected ?? previous ?. connected ,
170+ connectedAtMs : live . connectedAtMs ?? previous ?. connectedAtMs ,
171+ lastConnectedAtMs : previous ?. lastConnectedAtMs ,
172+ } ) ;
173+ }
174+
175+ return [ ...merged . values ( ) ] ;
176+ }
177+
178+ async function loadPairedNodesForList ( opts : NodesRpcOpts ) : Promise < {
179+ pending : ReturnType < typeof parsePairingList > [ "pending" ] ;
180+ paired : NodesListEntry [ ] ;
181+ usedFallback : boolean ;
182+ } > {
183+ const pairingResult = await callGatewayCli ( "node.pair.list" , opts , { } ) ;
184+ const { pending, paired } = parsePairingList ( pairingResult ) ;
185+ try {
186+ const liveNodes = parseNodeList ( await callGatewayCli ( "node.list" , opts , { } ) ) ;
187+ return {
188+ pending,
189+ paired : mergePairedNodeSources ( { liveNodes, pairedNodes : paired } ) ,
190+ usedFallback : false ,
191+ } ;
192+ } catch ( error ) {
193+ if ( ! shouldFallbackToPairList ( error ) ) {
194+ throw error ;
195+ }
196+ return {
197+ pending,
198+ paired : mergePairedNodeSources ( { liveNodes : null , pairedNodes : paired } ) ,
199+ usedFallback : true ,
200+ } ;
201+ }
202+ }
203+
100204export function registerNodesStatusCommands ( nodes : Command ) {
101205 nodesCallOpts (
102206 nodes
@@ -304,35 +408,29 @@ export function registerNodesStatusCommands(nodes: Command) {
304408 await runNodesCommand ( "list" , async ( ) => {
305409 const connectedOnly = Boolean ( opts . connected ) ;
306410 const sinceMs = parseSinceMs ( opts . lastConnected , "Invalid --last-connected" ) ;
307- const result = await callGatewayCli ( "node.pair.list" , opts , { } ) ;
308- const { pending, paired } = parsePairingList ( result ) ;
411+ const { pending, paired, usedFallback } = await loadPairedNodesForList ( opts ) ;
309412 const { heading, muted, warn } = getNodesTheme ( ) ;
310413 const tableWidth = getTerminalTableWidth ( ) ;
311414 const now = Date . now ( ) ;
312415 const hasFilters = connectedOnly || sinceMs !== undefined ;
416+ if ( usedFallback && hasFilters ) {
417+ throw new Error (
418+ "node.list is unavailable on this gateway; --connected and --last-connected require live node data" ,
419+ ) ;
420+ }
313421 const pendingRows = hasFilters ? [ ] : pending ;
314- const connectedById = hasFilters
315- ? new Map (
316- parseNodeList ( await callGatewayCli ( "node.list" , opts , { } ) ) . map ( ( node ) => [
317- node . nodeId ,
318- node ,
319- ] ) ,
320- )
321- : null ;
322422 const filteredPaired = paired . filter ( ( node ) => {
323423 if ( connectedOnly ) {
324- const live = connectedById ?. get ( node . nodeId ) ;
325- if ( ! live ?. connected ) {
424+ if ( ! node . connected ) {
326425 return false ;
327426 }
328427 }
329428 if ( sinceMs !== undefined ) {
330- const live = connectedById ?. get ( node . nodeId ) ;
331429 const lastConnectedAtMs =
332- typeof node . lastConnectedAtMs === "number"
333- ? node . lastConnectedAtMs
334- : typeof live ?. connectedAtMs === "number"
335- ? live . connectedAtMs
430+ typeof node . connectedAtMs === "number"
431+ ? node . connectedAtMs
432+ : typeof node . lastConnectedAtMs === "number"
433+ ? node . lastConnectedAtMs
336434 : undefined ;
337435 if ( typeof lastConnectedAtMs !== "number" ) {
338436 return false ;
@@ -368,12 +466,11 @@ export function registerNodesStatusCommands(nodes: Command) {
368466
369467 if ( filteredPaired . length > 0 ) {
370468 const pairedRows = filteredPaired . map ( ( n ) => {
371- const live = connectedById ?. get ( n . nodeId ) ;
372469 const lastConnectedAtMs =
373- typeof n . lastConnectedAtMs === "number"
374- ? n . lastConnectedAtMs
375- : typeof live ?. connectedAtMs === "number"
376- ? live . connectedAtMs
470+ typeof n . connectedAtMs === "number"
471+ ? n . connectedAtMs
472+ : typeof n . lastConnectedAtMs === "number"
473+ ? n . lastConnectedAtMs
377474 : undefined ;
378475 return {
379476 Node : n . displayName ?. trim ( ) ? n . displayName . trim ( ) : n . nodeId ,
0 commit comments