MCPcopy Index your code
hub / github.com/callstack/agent-device / waitForStdoutLine

Function waitForStdoutLine

src/__tests__/daemon-entrypoint.test.ts:23–60  ·  view source on GitHub ↗
(
  stream: NodeJS.ReadableStream | null,
  pattern: RegExp,
  timeoutMs: number,
)

Source from the content-addressed store, hash-verified

21};
22
23function waitForStdoutLine(
24 stream: NodeJS.ReadableStream | null,
25 pattern: RegExp,
26 timeoutMs: number,
27): Promise<string> {
28 if (!stream) {
29 return Promise.reject(new Error('Expected daemon stdout stream.'));
30 }
31 stream.setEncoding('utf8');
32 let buffer = '';
33 return new Promise((resolve, reject) => {
34 const timeout = setTimeout(() => {
35 cleanup();
36 reject(new Error(`Timed out waiting for daemon stdout line matching ${pattern}.`));
37 }, timeoutMs);
38 const onData = (chunk: string) => {
39 buffer += chunk;
40 const line = buffer
41 .split('\n')
42 .map((entry) => entry.trim())
43 .find((entry) => pattern.test(entry));
44 if (!line) return;
45 cleanup();
46 resolve(line);
47 };
48 const onError = (error: unknown) => {
49 cleanup();
50 reject(error instanceof Error ? error : new Error(String(error)));
51 };
52 const cleanup = () => {
53 clearTimeout(timeout);
54 stream.off('data', onData);
55 stream.off('error', onError);
56 };
57 stream.on('data', onData);
58 stream.on('error', onError);
59 });
60}
61
62test('daemon runtime starts HTTP transport in-process and shuts down cleanly', async () => {
63 const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), 'agent-device-daemon-runtime-'));

Callers 1

Calls 1

cleanupFunction · 0.70

Tested by

no test coverage detected