@google-cloud/cloud-run-mcp 1.5.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.c8rc.json +9 -0
- package/.github/workflows/lint-checker.yml +17 -0
- package/.github/workflows/npm-publish.yml +1 -1
- package/.kokoro/kokoro_build.sh +22 -0
- package/.kokoro/presubmit.cfg +21 -0
- package/.kokoro/run_tests.sh +31 -0
- package/CONTRIBUTING.md +24 -9
- package/README.md +52 -11
- package/REVIEWING.md +20 -0
- package/gemini-extension/GEMINI.md +1 -1
- package/lib/cloud-api/billing.js +183 -0
- package/lib/cloud-api/build.js +200 -0
- package/lib/{gcp-api-helpers.js → cloud-api/helpers.js} +81 -35
- package/lib/{gcp-projects.js → cloud-api/projects.js} +58 -28
- package/lib/cloud-api/registry.js +98 -0
- package/lib/{cloud-run-services.js → cloud-api/run.js} +103 -21
- package/lib/cloud-api/storage.js +123 -0
- package/lib/deployment/constants.js +54 -0
- package/lib/deployment/deployer.js +621 -0
- package/lib/deployment/helpers.js +194 -0
- package/lib/deployment/source-processor.js +124 -0
- package/lib/util/archive.js +104 -0
- package/lib/util/helpers.js +42 -0
- package/mcp-server.js +21 -10
- package/package.json +8 -5
- package/test/local/cloud-api/build.test.js +436 -0
- package/test/local/cloud-api/projects.test.js +18 -0
- package/test/local/deployment-helpers.test.js +231 -0
- package/test/local/mcp-server-stdio.test.js +59 -0
- package/test/local/mcp-server-streamable-http.test.js +61 -0
- package/test/local/notifications.test.js +9 -6
- package/test/local/source-processor.test.js +290 -0
- package/test/local/test-utils.js +51 -0
- package/test/local/tools.test.js +114 -70
- package/test/need-gcp/cloud-run-services.test.js +44 -44
- package/test/need-gcp/{cloud-run-deploy.test.js → deployer.test.js} +3 -56
- package/test/need-gcp/gcp-auth-check.test.js +1 -1
- package/test/need-gcp/gcp-projects.test.js +3 -15
- package/test/need-gcp/test-helpers.js +175 -0
- package/test/need-gcp/workflows/deployment-workflows.test.js +100 -0
- package/tools/register-tools.js +106 -97
- package/tools/tools.js +2 -9
- package/lib/cloud-run-deploy.js +0 -937
- package/lib/gcp-billing-accounts.js +0 -98
- /package/lib/{gcp-auth-check.js → cloud-api/auth.js} +0 -0
- /package/lib/{gcp-metadata.js → cloud-api/metadata.js} +0 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { test, describe, before, after } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { spawn } from 'child_process';
|
|
4
|
+
import { waitForString } from './test-utils.js';
|
|
5
|
+
|
|
6
|
+
describe('MCP Server stdio startup', () => {
|
|
7
|
+
let serverProcess;
|
|
8
|
+
let stderr = '';
|
|
9
|
+
const stdioMsg = 'Cloud Run MCP server stdio transport connected';
|
|
10
|
+
|
|
11
|
+
describe('when GCP_STDIO=true', () => {
|
|
12
|
+
before(async () => {
|
|
13
|
+
stderr = '';
|
|
14
|
+
serverProcess = spawn('node', ['mcp-server.js'], {
|
|
15
|
+
cwd: process.cwd(),
|
|
16
|
+
env: { ...process.env, GCP_STDIO: 'true' },
|
|
17
|
+
});
|
|
18
|
+
stderr = await waitForString(serverProcess.stderr, stdioMsg);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
after(() => {
|
|
22
|
+
if (serverProcess) {
|
|
23
|
+
serverProcess.kill();
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('should start in stdio mode', () => {
|
|
28
|
+
assert.ok(stderr.includes(stdioMsg));
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('when GCP_STDIO=false', () => {
|
|
33
|
+
before(async () => {
|
|
34
|
+
stderr = '';
|
|
35
|
+
const env = { ...process.env };
|
|
36
|
+
env.GCP_STDIO = 'false';
|
|
37
|
+
serverProcess = spawn('node', ['mcp-server.js'], {
|
|
38
|
+
cwd: process.cwd(),
|
|
39
|
+
env: env,
|
|
40
|
+
});
|
|
41
|
+
const stderrChunks = [];
|
|
42
|
+
serverProcess.stderr.on('data', (chunk) => {
|
|
43
|
+
stderrChunks.push(chunk);
|
|
44
|
+
});
|
|
45
|
+
await new Promise((resolve) => setTimeout(resolve, 2000));
|
|
46
|
+
stderr = Buffer.concat(stderrChunks).toString();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
after(() => {
|
|
50
|
+
if (serverProcess) {
|
|
51
|
+
serverProcess.kill();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('should not start in stdio mode', () => {
|
|
56
|
+
assert.ok(!stderr.includes(stdioMsg));
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
2
|
+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
3
|
+
import { test, describe, before, after } from 'node:test';
|
|
4
|
+
import assert from 'node:assert/strict';
|
|
5
|
+
import { spawn } from 'child_process';
|
|
6
|
+
import { waitForString } from './test-utils.js';
|
|
7
|
+
|
|
8
|
+
class MCPClient {
|
|
9
|
+
client = null;
|
|
10
|
+
transport = null;
|
|
11
|
+
|
|
12
|
+
constructor(serverName) {
|
|
13
|
+
this.client = new Client({
|
|
14
|
+
name: `mcp-client-for-${serverName}`,
|
|
15
|
+
version: '1.0.0',
|
|
16
|
+
url: `http://localhost:3000/mcp`,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async connectToServer(serverUrl) {
|
|
21
|
+
this.transport = new StreamableHTTPClientTransport(serverUrl);
|
|
22
|
+
await this.client.connect(this.transport);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async cleanup() {
|
|
26
|
+
await this.client.close();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe('MCP Server in Streamble HTTP mode', () => {
|
|
31
|
+
let client;
|
|
32
|
+
let serverProcess;
|
|
33
|
+
let stdout = '';
|
|
34
|
+
const httpMsg = 'Cloud Run MCP server listening on port 3000';
|
|
35
|
+
|
|
36
|
+
describe('when GCP_STDIO=false', () => {
|
|
37
|
+
before(async () => {
|
|
38
|
+
stdout = '';
|
|
39
|
+
// Start MCP server as a child process
|
|
40
|
+
serverProcess = spawn('node', ['mcp-server.js'], {
|
|
41
|
+
cwd: process.cwd(),
|
|
42
|
+
env: { ...process.env, GCP_STDIO: 'false' },
|
|
43
|
+
});
|
|
44
|
+
stdout = await waitForString(serverProcess.stdout, httpMsg);
|
|
45
|
+
|
|
46
|
+
client = new MCPClient('http-server');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
after(async () => {
|
|
50
|
+
await client.cleanup();
|
|
51
|
+
if (serverProcess) {
|
|
52
|
+
serverProcess.kill();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('should start an HTTP server', async () => {
|
|
57
|
+
await client.connectToServer('http://localhost:3000/mcp');
|
|
58
|
+
assert.ok(stdout.includes(httpMsg));
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -8,12 +8,15 @@ describe('Tool Notifications', () => {
|
|
|
8
8
|
registerTool: mock.fn(),
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
'../../
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
const { registerTools } = await esmock(
|
|
12
|
+
'../../tools/tools.js',
|
|
13
|
+
{},
|
|
14
|
+
{
|
|
15
|
+
'../../lib/deployment/deployer.js': {
|
|
16
|
+
deploy: () => Promise.resolve({ uri: 'my-uri' }),
|
|
17
|
+
},
|
|
18
|
+
}
|
|
19
|
+
);
|
|
17
20
|
|
|
18
21
|
registerTools(server, { gcpCredentialsAvailable: true });
|
|
19
22
|
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 Google LLC
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { test, describe, mock } from 'node:test';
|
|
18
|
+
import assert from 'node:assert/strict';
|
|
19
|
+
import esmock from 'esmock';
|
|
20
|
+
|
|
21
|
+
describe('Source Processor', () => {
|
|
22
|
+
const fspMock = {
|
|
23
|
+
mkdir: mock.fn(),
|
|
24
|
+
stat: mock.fn(),
|
|
25
|
+
copyFile: mock.fn(),
|
|
26
|
+
rm: mock.fn(),
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const childProcessMock = {
|
|
30
|
+
exec: mock.fn(),
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
test('prepareSourceDirectory creates temp dir and copies files', async () => {
|
|
34
|
+
const sourceProcessor = await esmock(
|
|
35
|
+
'../../lib/deployment/source-processor.js',
|
|
36
|
+
{
|
|
37
|
+
'node:fs/promises': fspMock,
|
|
38
|
+
'node:child_process': childProcessMock,
|
|
39
|
+
}
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const files = ['/path/to/source/file1.js', '/path/to/source/dir1'];
|
|
43
|
+
|
|
44
|
+
fspMock.stat.mock.mockImplementation(async (filePath) => {
|
|
45
|
+
if (filePath.endsWith('dir1')) return { isDirectory: () => true };
|
|
46
|
+
return { isDirectory: () => false };
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// Mock exec for directory copy (cp -R)
|
|
50
|
+
childProcessMock.exec.mock.mockImplementation((cmd, callback) => {
|
|
51
|
+
// Handle sync callback for util.promisify
|
|
52
|
+
if (callback) callback(null, 'ok', '');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const progressCallback = mock.fn();
|
|
56
|
+
|
|
57
|
+
const tempDir = await sourceProcessor.prepareSourceDirectory(
|
|
58
|
+
files,
|
|
59
|
+
progressCallback
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
assert.ok(
|
|
63
|
+
tempDir.includes('file1.js-'),
|
|
64
|
+
'Temp dir should have expected prefix (based on first filename)'
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
assert.equal(fspMock.mkdir.mock.callCount(), 1);
|
|
68
|
+
assert.equal(fspMock.copyFile.mock.callCount(), 1); // for file1.js
|
|
69
|
+
|
|
70
|
+
// Check if cp -R was called for dir1
|
|
71
|
+
const execCalls = childProcessMock.exec.mock.calls;
|
|
72
|
+
const cpCall = execCalls.find((call) =>
|
|
73
|
+
call.arguments[0].startsWith('cp -R')
|
|
74
|
+
);
|
|
75
|
+
assert.ok(cpCall, 'Should call cp -R for directories');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('installDependencies runs npm install for Node.js', async () => {
|
|
79
|
+
// We need to mock fs.existsSync as well
|
|
80
|
+
const fsMock = {
|
|
81
|
+
existsSync: mock.fn(),
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const sourceProcessor = await esmock(
|
|
85
|
+
'../../lib/deployment/source-processor.js',
|
|
86
|
+
{
|
|
87
|
+
'node:fs': fsMock,
|
|
88
|
+
'node:fs/promises': fspMock,
|
|
89
|
+
'node:child_process': childProcessMock,
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const targetDir = '/temp/dir';
|
|
94
|
+
const deploymentAttrs = {
|
|
95
|
+
runtime: 'nodejs',
|
|
96
|
+
cmd: ['node', 'server.js'],
|
|
97
|
+
args: ['server.js'],
|
|
98
|
+
baseImage: 'node:22',
|
|
99
|
+
};
|
|
100
|
+
const progressCallback = mock.fn();
|
|
101
|
+
|
|
102
|
+
// Mock existsSync
|
|
103
|
+
fsMock.existsSync.mock.mockImplementation((p) => {
|
|
104
|
+
if (p.includes('node_modules')) return false; // not installed yet
|
|
105
|
+
if (p.includes('package-lock.json')) return false; // no lock file
|
|
106
|
+
return false;
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// Mock exec for npm install
|
|
110
|
+
childProcessMock.exec.mock.mockImplementation((cmd, opts, callback) => {
|
|
111
|
+
if (typeof opts === 'function') callback = opts;
|
|
112
|
+
if (callback) callback(null, 'ok', '');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
await sourceProcessor.installDependencies(
|
|
116
|
+
targetDir,
|
|
117
|
+
deploymentAttrs,
|
|
118
|
+
progressCallback
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const calls = childProcessMock.exec.mock.calls;
|
|
122
|
+
const npmCall = calls.find((call) =>
|
|
123
|
+
call.arguments[0].includes('npm install')
|
|
124
|
+
);
|
|
125
|
+
assert.ok(npmCall, 'Should run npm install');
|
|
126
|
+
assert.strictEqual(npmCall.arguments[1].cwd, targetDir);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('installDependencies uses npm ci if lockfile exists', async () => {
|
|
130
|
+
const fsMock = {
|
|
131
|
+
existsSync: mock.fn(),
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const sourceProcessor = await esmock(
|
|
135
|
+
'../../lib/deployment/source-processor.js',
|
|
136
|
+
{
|
|
137
|
+
'node:fs': fsMock,
|
|
138
|
+
'node:child_process': childProcessMock,
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
const targetDir = '/temp/dir';
|
|
143
|
+
const deploymentAttrs = {
|
|
144
|
+
runtime: 'nodejs',
|
|
145
|
+
cmd: ['node', 'server.js'],
|
|
146
|
+
args: ['server.js'],
|
|
147
|
+
baseImage: 'node:22',
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
// Mock existsSync
|
|
151
|
+
fsMock.existsSync.mock.mockImplementation((p) => {
|
|
152
|
+
if (p.includes('node_modules')) return false;
|
|
153
|
+
if (p.includes('package-lock.json')) return true; // has lock file
|
|
154
|
+
return false;
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
childProcessMock.exec.mock.mockImplementation((cmd, opts, callback) => {
|
|
158
|
+
if (typeof opts === 'function') callback = opts;
|
|
159
|
+
if (callback) callback(null, 'ok', '');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
await sourceProcessor.installDependencies(
|
|
163
|
+
targetDir,
|
|
164
|
+
deploymentAttrs,
|
|
165
|
+
() => {}
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const calls = childProcessMock.exec.mock.calls;
|
|
169
|
+
const npmCall = calls.find((call) => call.arguments[0].includes('npm ci'));
|
|
170
|
+
assert.ok(npmCall, 'Should run npm ci');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('installDependencies skips if node_modules exists', async () => {
|
|
174
|
+
const fsMock = {
|
|
175
|
+
existsSync: mock.fn(),
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const sourceProcessor = await esmock(
|
|
179
|
+
'../../lib/deployment/source-processor.js',
|
|
180
|
+
{
|
|
181
|
+
'node:fs': fsMock,
|
|
182
|
+
'node:child_process': childProcessMock,
|
|
183
|
+
}
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
fsMock.existsSync.mock.mockImplementation((p) => {
|
|
187
|
+
if (p.includes('node_modules')) return true;
|
|
188
|
+
return false;
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// clear mock calls
|
|
192
|
+
childProcessMock.exec.mock.resetCalls();
|
|
193
|
+
|
|
194
|
+
await sourceProcessor.installDependencies(
|
|
195
|
+
'/dir',
|
|
196
|
+
{
|
|
197
|
+
runtime: 'nodejs',
|
|
198
|
+
cmd: ['node', 'server.js'],
|
|
199
|
+
args: ['server.js'],
|
|
200
|
+
baseImage: 'node:22',
|
|
201
|
+
},
|
|
202
|
+
() => {}
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
assert.equal(childProcessMock.exec.mock.callCount(), 0);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
test('installDependencies logs warn on failure but does not throw', async () => {
|
|
209
|
+
const fsMock = { existsSync: mock.fn(() => false) };
|
|
210
|
+
const sourceProcessor = await esmock(
|
|
211
|
+
'../../lib/deployment/source-processor.js',
|
|
212
|
+
{
|
|
213
|
+
'node:fs': fsMock,
|
|
214
|
+
'node:child_process': childProcessMock,
|
|
215
|
+
}
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
childProcessMock.exec.mock.mockImplementation((cmd, opts, callback) => {
|
|
219
|
+
if (typeof opts === 'function') callback = opts;
|
|
220
|
+
callback(new Error('npm failed'));
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
const consoleLogMock = mock.method(console, 'log');
|
|
224
|
+
|
|
225
|
+
await assert.doesNotReject(async () => {
|
|
226
|
+
await sourceProcessor.installDependencies('/dir', { runtime: 'nodejs' });
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
assert.equal(
|
|
230
|
+
consoleLogMock.mock.callCount(),
|
|
231
|
+
1,
|
|
232
|
+
'console.log should be called once'
|
|
233
|
+
);
|
|
234
|
+
const logArgs = consoleLogMock.mock.calls[0].arguments;
|
|
235
|
+
assert.ok(
|
|
236
|
+
logArgs[0].includes('Dependency installation failed: npm failed'),
|
|
237
|
+
'console.log should contain the error message'
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
consoleLogMock.mock.restore();
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test('cleanupTempDirectory removes directory', async () => {
|
|
244
|
+
const fsMock = {
|
|
245
|
+
existsSync: mock.fn(() => true),
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const sourceProcessor = await esmock(
|
|
249
|
+
'../../lib/deployment/source-processor.js',
|
|
250
|
+
{
|
|
251
|
+
'node:fs': fsMock,
|
|
252
|
+
'node:fs/promises': fspMock,
|
|
253
|
+
}
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
await sourceProcessor.cleanupTempDirectory('/temp/dir');
|
|
257
|
+
|
|
258
|
+
assert.equal(fspMock.rm.mock.callCount(), 1);
|
|
259
|
+
assert.deepEqual(fspMock.rm.mock.calls[0].arguments[0], '/temp/dir');
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test('cleanupTempDirectory logs error on failure but does not throw', async () => {
|
|
263
|
+
const fsMock = { existsSync: mock.fn(() => true) };
|
|
264
|
+
// mock fsp.rm to throw
|
|
265
|
+
const fspMockThrow = {
|
|
266
|
+
rm: mock.fn(async () => {
|
|
267
|
+
throw new Error('rm failed');
|
|
268
|
+
}),
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
const consoleErrorMock = mock.method(console, 'error');
|
|
272
|
+
|
|
273
|
+
const sourceProcessor = await esmock(
|
|
274
|
+
'../../lib/deployment/source-processor.js',
|
|
275
|
+
{
|
|
276
|
+
'node:fs': fsMock,
|
|
277
|
+
'node:fs/promises': fspMockThrow,
|
|
278
|
+
}
|
|
279
|
+
);
|
|
280
|
+
|
|
281
|
+
await sourceProcessor.cleanupTempDirectory('/dir');
|
|
282
|
+
|
|
283
|
+
assert.equal(consoleErrorMock.mock.callCount(), 1);
|
|
284
|
+
assert.ok(
|
|
285
|
+
consoleErrorMock.mock.calls[0].arguments[1].message.includes('rm failed')
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
consoleErrorMock.mock.restore();
|
|
289
|
+
});
|
|
290
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2025 Google LLC
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
https://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Waits for a specific string to appear in a stream's output.
|
|
19
|
+
* This is useful for synchronizing tests with asynchronous child processes,
|
|
20
|
+
* ensuring that a server or process has started and emitted an
|
|
21
|
+
* expected "ready" message before proceeding with test assertions.
|
|
22
|
+
*
|
|
23
|
+
* @param {ReadableStream} stream - The stream to listen to (e.g., process.stdout or process.stderr).
|
|
24
|
+
* @param {string} str - The string to wait for in the stream's output.
|
|
25
|
+
* @param {number} [timeoutMs=7000] - The maximum time in milliseconds to wait before rejecting.
|
|
26
|
+
* @returns {Promise<string>} A promise that resolves with the accumulated data
|
|
27
|
+
* when the string is found, or rejects if the timeout is reached.
|
|
28
|
+
*/
|
|
29
|
+
export async function waitForString(stream, str, timeoutMs = 7000) {
|
|
30
|
+
let accumulatedData = '';
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
const timeout = setTimeout(() => {
|
|
33
|
+
stream.removeListener('data', onData);
|
|
34
|
+
reject(
|
|
35
|
+
new Error(`waitForString timed out after ${timeoutMs}ms waiting for "${str}".
|
|
36
|
+
Saw:
|
|
37
|
+
${accumulatedData}`)
|
|
38
|
+
);
|
|
39
|
+
}, timeoutMs);
|
|
40
|
+
|
|
41
|
+
function onData(data) {
|
|
42
|
+
accumulatedData += data.toString();
|
|
43
|
+
if (accumulatedData.includes(str)) {
|
|
44
|
+
clearTimeout(timeout);
|
|
45
|
+
stream.removeListener('data', onData);
|
|
46
|
+
resolve(accumulatedData);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
stream.on('data', onData);
|
|
50
|
+
});
|
|
51
|
+
}
|