MCPcopy
hub / github.com/claude-code-best/claude-code / waitForInit

Function waitForInit

src/ssh/createSSHSession.ts:358–427  ·  view source on GitHub ↗
(
  proc: Subprocess,
  fallbackCwd?: string,
)

Source from the content-addressed store, hash-verified

356}
357
358async function waitForInit(
359 proc: Subprocess,
360 fallbackCwd?: string,
361): Promise<string> {
362 const stdout = proc.stdout
363 if (!stdout) {
364 throw new SSHSessionError('Child process stdout is not readable')
365 }
366
367 const reader = (stdout as ReadableStream<Uint8Array>).getReader()
368 const decoder = new TextDecoder()
369 let buffer = ''
370 const deadline = Date.now() + INIT_TIMEOUT_MS
371
372 try {
373 while (Date.now() < deadline) {
374 const remaining = deadline - Date.now()
375 const result = await Promise.race([
376 reader.read(),
377 new Promise<{ done: true; value: undefined }>((_, reject) =>
378 setTimeout(
379 () =>
380 reject(
381 new SSHSessionError(
382 'Remote CLI did not initialize within 30 seconds. Check SSH connectivity and remote binary.',
383 ),
384 ),
385 remaining,
386 ),
387 ),
388 ])
389
390 if (result.done) {
391 throw new SSHSessionError(
392 'Child process exited before sending init message',
393 )
394 }
395
396 buffer += decoder.decode(result.value, { stream: true })
397 const lines = buffer.split('\n')
398 buffer = lines.pop() ?? ''
399
400 for (const line of lines) {
401 const trimmed = line.trim()
402 if (!trimmed) continue
403 try {
404 const msg = jsonParse(trimmed) as Record<string, unknown>
405 if (msg.type === 'system' && msg.subtype === 'init') {
406 reader.releaseLock()
407 return (msg.cwd as string) || fallbackCwd || process.cwd()
408 }
409 } catch {
410 // not valid JSON — skip
411 }
412 }
413 }
414 } catch (err) {
415 reader.releaseLock()

Callers 3

createSSHSessionFunction · 0.85
reconnectFunction · 0.85
createLocalSSHSessionFunction · 0.85

Calls 3

jsonParseFunction · 0.85
nowMethod · 0.80
readMethod · 0.45

Tested by

no test coverage detected