-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin-process.ts
More file actions
161 lines (136 loc) · 4.51 KB
/
Copy pathplugin-process.ts
File metadata and controls
161 lines (136 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {
ApplyRequestData,
CommandRequestData,
CommandRequestDataSchema,
ImportRequestData,
ImportResponseData,
InitializeResponseData,
IpcMessageSchema,
IpcMessageV2,
MessageCmd,
PlanRequestData,
PlanResponseData,
ValidateRequestData,
ValidateResponseData
} from '@codifycli/schemas';
import Ajv from 'ajv';
import { nanoid } from 'nanoid';
import { ChildProcess, fork } from 'node:child_process';
import path from 'node:path';
import { spawnSafe } from './spawn.js';
import { TestUtils } from './test-utils.js';
const ajv = new Ajv.default({
strict: true
});
const ipcMessageValidator = ajv.compile(IpcMessageSchema);
const commandRequestValidator = ajv.compile(CommandRequestDataSchema);
export class PluginProcess {
childProcess: ChildProcess
/**
* PluginTester is a helper class to integration test plugins. It launches plugins via fork() just like CodifyCLI does.
*
* @param pluginPath A fully qualified path
*/
constructor(pluginPath: string) {
if (!path.isAbsolute(pluginPath)) {
throw new Error('A fully qualified path must be supplied to PluginTester');
}
const debug = (process.env.DEBUG) ? ['--inspect-brk=9221'] : [];
this.childProcess = fork(
pluginPath,
[],
{
// Use default true to test plugins in secure mode (un-able to request sudo directly)
// detached: true,
env: { ...process.env },
execArgv: ['--import', 'tsx/esm', ...debug],
stdio: 'pipe',
},
)
this.childProcess.stderr?.pipe(process.stderr);
this.childProcess.stdout?.pipe(process.stdout);
this.handleIncomingRequests(this.childProcess);
}
async initialize(): Promise<InitializeResponseData> {
return TestUtils.sendMessageAndAwaitResponse(this.childProcess, {
cmd: 'initialize',
data: { verbosityLevel: 3 },
requestId: nanoid(6),
});
}
async validate(data: ValidateRequestData): Promise<ValidateResponseData> {
return TestUtils.sendMessageAndAwaitResponse(this.childProcess, {
cmd: 'validate',
data,
requestId: nanoid(6),
});
}
async plan(data: PlanRequestData): Promise<PlanResponseData> {
return TestUtils.sendMessageAndAwaitResponse(this.childProcess, {
cmd: 'plan',
data,
requestId: nanoid(6),
});
}
async apply(data: ApplyRequestData): Promise<void> {
return TestUtils.sendMessageAndAwaitResponse(this.childProcess, {
cmd: 'apply',
data,
requestId: nanoid(6),
});
}
async import(data: ImportRequestData): Promise<ImportResponseData> {
return TestUtils.sendMessageAndAwaitResponse(this.childProcess, {
cmd: 'import',
data,
requestId: nanoid(6),
});
}
kill() {
this.childProcess.kill();
}
private handleIncomingRequests(cp: ChildProcess) {
// Listen for incoming sudo incoming sudo requests
cp.on('message', async (message) => {
if (!ipcMessageValidator(message)) {
throw new Error(`Invalid message from plugin. ${JSON.stringify(message, null, 2)}`);
}
if (message.cmd === MessageCmd.COMMAND_REQUEST) {
const { data, requestId } = message;
if (!commandRequestValidator(data)) {
throw new Error(`Invalid sudo request from plugin. ${JSON.stringify(commandRequestValidator.errors, null, 2)}`);
}
const { command, options } = data as unknown as CommandRequestData;
const result = await spawnSafe(command, options);
cp.send(<IpcMessageV2>{
cmd: MessageCmd.COMMAND_REQUEST + '_Response',
data: result,
requestId,
})
}
if (message.cmd === MessageCmd.PRESS_KEY_TO_CONTINUE_REQUEST) {
const { data, requestId } = message;
if (!commandRequestValidator(data)) {
throw new Error(`Invalid sudo request from plugin. ${JSON.stringify(commandRequestValidator.errors, null, 2)}`);
}
cp.send(<IpcMessageV2>{
cmd: MessageCmd.PRESS_KEY_TO_CONTINUE_REQUEST + '_Response',
data: {},
requestId,
})
}
if (message.cmd === MessageCmd.CODIFY_CREDENTIALS_REQUEST) {
const { requestId } = message;
const testJwt = process.env.VITE_CODIFY_TEST_JWT;
if (!testJwt) {
throw new Error('Unable to parse login credentials from VITE_CODIFY_TEST_JWT env var. Please set and try again');
}
cp.send(<IpcMessageV2>{
cmd: MessageCmd.CODIFY_CREDENTIALS_REQUEST + '_Response',
data: testJwt,
requestId,
})
}
})
}
}