MCPcopy Create free account
hub / github.com/Snapchat/Valdi / createClient

Function createClient

npm_modules/cli/test/helpers/TypeScriptClient.ts:12–74  ·  view source on GitHub ↗
(workingDir: string)

Source from the content-addressed store, hash-verified

10}
11
12export function createClient(workingDir: string): TSServerClient {
13 const reserveSequence = createNonceService();
14 const read = createMessageBuffer();
15
16 // eslint-disable-next-line unicorn/prefer-event-target
17 const queue = new EventEmitter<{
18 response: [Record<string, unknown>];
19 message: [Record<string, unknown>];
20 }>();
21
22 const service = run('npx', ['tsserver'], {cwd: workingDir});
23
24 service.process.stdout?.on('data', (chunk: Buffer) => {
25 for (const [message] of read(chunk)) {
26 if ('type' in message && message['type'] === 'response') {
27 queue.emit('response', message);
28 }
29 queue.emit('message', message);
30 }
31 });
32
33 service.process.stderr?.pipe(process.stderr)
34
35 const send = (
36 request: Record<string, unknown>,
37 { timeout = 300000 }: { timeout?: number } = {},
38 ): Promise<Record<string, unknown>> => {
39 const seq = reserveSequence();
40 const body = JSON.stringify({
41 seq,
42 ...request,
43 });
44 const payload = body + '\r\n';
45 service.process.stdin.write(payload);
46
47 return withTimeout(
48 new Promise(resolve => {
49 const listener = (message: Record<string, unknown>) => {
50 if ('request_seq' in message && message['request_seq'] !== seq) {
51 return;
52 }
53 queue.removeListener('response', listener);
54 resolve(message);
55 };
56 queue.addListener('response', listener);
57 }),
58 timeout,
59 );
60 };
61
62 return {
63 command: (command: string, args?: Record<string, unknown>) =>
64 send({
65 command,
66 ...(args ? { arguments: args } : null),
67 }),
68 disconnect: () => {
69 service.process.stdin.end();

Callers 1

ValdiSmokeTest.tsFile · 0.90

Calls 9

runFunction · 0.90
createNonceServiceFunction · 0.85
createMessageBufferFunction · 0.85
pipeMethod · 0.80
sendFunction · 0.70
onMethod · 0.65
endMethod · 0.65
killMethod · 0.65
waitMethod · 0.45

Tested by

no test coverage detected