@theia/plugin-dev 1.45.1 → 1.46.0-next.72

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.
Files changed (54) hide show
  1. package/README.md +31 -31
  2. package/lib/browser/hosted-plugin-controller.d.ts +74 -74
  3. package/lib/browser/hosted-plugin-controller.js +352 -352
  4. package/lib/browser/hosted-plugin-frontend-contribution.d.ts +6 -6
  5. package/lib/browser/hosted-plugin-frontend-contribution.js +56 -56
  6. package/lib/browser/hosted-plugin-informer.d.ts +25 -25
  7. package/lib/browser/hosted-plugin-informer.js +102 -102
  8. package/lib/browser/hosted-plugin-log-viewer.d.ts +14 -14
  9. package/lib/browser/hosted-plugin-log-viewer.js +69 -69
  10. package/lib/browser/hosted-plugin-manager-client.d.ts +80 -80
  11. package/lib/browser/hosted-plugin-manager-client.js +410 -410
  12. package/lib/browser/hosted-plugin-preferences.d.ts +13 -13
  13. package/lib/browser/hosted-plugin-preferences.js +60 -60
  14. package/lib/browser/plugin-dev-frontend-module.d.ts +3 -3
  15. package/lib/browser/plugin-dev-frontend-module.js +42 -42
  16. package/lib/common/index.d.ts +2 -2
  17. package/lib/common/index.js +31 -31
  18. package/lib/common/plugin-dev-protocol.d.ts +24 -24
  19. package/lib/common/plugin-dev-protocol.js +20 -20
  20. package/lib/node/hosted-instance-manager.d.ts +104 -104
  21. package/lib/node/hosted-instance-manager.js +320 -320
  22. package/lib/node/hosted-instance-manager.js.map +1 -1
  23. package/lib/node/hosted-plugin-reader.d.ts +11 -11
  24. package/lib/node/hosted-plugin-reader.js +68 -68
  25. package/lib/node/hosted-plugin-reader.js.map +1 -1
  26. package/lib/node/hosted-plugin-uri-postprocessor.d.ts +6 -6
  27. package/lib/node/hosted-plugin-uri-postprocessor.js +27 -27
  28. package/lib/node/hosted-plugins-manager.d.ts +41 -41
  29. package/lib/node/hosted-plugins-manager.js +119 -119
  30. package/lib/node/plugin-dev-backend-module.d.ts +4 -4
  31. package/lib/node/plugin-dev-backend-module.js +53 -53
  32. package/lib/node/plugin-dev-service.d.ts +25 -25
  33. package/lib/node/plugin-dev-service.js +108 -108
  34. package/lib/node-electron/plugin-dev-electron-backend-module.d.ts +3 -3
  35. package/lib/node-electron/plugin-dev-electron-backend-module.js +28 -28
  36. package/lib/package.spec.js +25 -25
  37. package/package.json +9 -9
  38. package/src/browser/hosted-plugin-controller.ts +356 -356
  39. package/src/browser/hosted-plugin-frontend-contribution.ts +45 -45
  40. package/src/browser/hosted-plugin-informer.ts +93 -93
  41. package/src/browser/hosted-plugin-log-viewer.ts +52 -52
  42. package/src/browser/hosted-plugin-manager-client.ts +430 -430
  43. package/src/browser/hosted-plugin-preferences.ts +71 -71
  44. package/src/browser/plugin-dev-frontend-module.ts +45 -45
  45. package/src/common/index.ts +21 -21
  46. package/src/common/plugin-dev-protocol.ts +45 -45
  47. package/src/node/hosted-instance-manager.ts +382 -382
  48. package/src/node/hosted-plugin-reader.ts +58 -58
  49. package/src/node/hosted-plugin-uri-postprocessor.ts +32 -32
  50. package/src/node/hosted-plugins-manager.ts +146 -146
  51. package/src/node/plugin-dev-backend-module.ts +54 -54
  52. package/src/node/plugin-dev-service.ts +107 -107
  53. package/src/node-electron/plugin-dev-electron-backend-module.ts +29 -29
  54. package/src/package.spec.ts +28 -28
@@ -1,382 +1,382 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2018 Red Hat, Inc. and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import { RequestOptions, RequestService } from '@theia/core/shared/@theia/request';
18
- import { inject, injectable, named } from '@theia/core/shared/inversify';
19
- import * as cp from 'child_process';
20
- import * as fs from '@theia/core/shared/fs-extra';
21
- import * as net from 'net';
22
- import * as path from 'path';
23
- import URI from '@theia/core/lib/common/uri';
24
- import { ContributionProvider } from '@theia/core/lib/common/contribution-provider';
25
- import { HostedPluginUriPostProcessor, HostedPluginUriPostProcessorSymbolName } from './hosted-plugin-uri-postprocessor';
26
- import { environment, isWindows } from '@theia/core';
27
- import { FileUri } from '@theia/core/lib/node/file-uri';
28
- import { LogType } from '@theia/plugin-ext/lib/common/types';
29
- import { HostedPluginSupport } from '@theia/plugin-ext/lib/hosted/node/hosted-plugin';
30
- import { MetadataScanner } from '@theia/plugin-ext/lib/hosted/node/metadata-scanner';
31
- import { PluginDebugConfiguration } from '../common/plugin-dev-protocol';
32
- import { HostedPluginProcess } from '@theia/plugin-ext/lib/hosted/node/hosted-plugin-process';
33
- import { isENOENT } from '@theia/plugin-ext/lib/common/errors';
34
-
35
- const DEFAULT_HOSTED_PLUGIN_PORT = 3030;
36
-
37
- export const HostedInstanceManager = Symbol('HostedInstanceManager');
38
-
39
- /**
40
- * Is responsible for running and handling separate Theia instance with given plugin.
41
- */
42
- export interface HostedInstanceManager {
43
- /**
44
- * Checks whether hosted instance is run.
45
- */
46
- isRunning(): boolean;
47
-
48
- /**
49
- * Runs specified by the given uri plugin in separate Theia instance.
50
- *
51
- * @param pluginUri uri to the plugin source location
52
- * @param port port on which new instance of Theia should be run. Optional.
53
- * @returns uri where new Theia instance is run
54
- */
55
- run(pluginUri: URI, port?: number): Promise<URI>;
56
-
57
- /**
58
- * Runs specified by the given uri plugin with debug in separate Theia instance.
59
- * @param pluginUri uri to the plugin source location
60
- * @param debugConfig debug configuration
61
- * @returns uri where new Theia instance is run
62
- */
63
- debug(pluginUri: URI, debugConfig: PluginDebugConfiguration): Promise<URI>;
64
-
65
- /**
66
- * Terminates hosted plugin instance.
67
- * Throws error if instance is not running.
68
- */
69
- terminate(): void;
70
-
71
- /**
72
- * Returns uri where hosted instance is run.
73
- * Throws error if instance is not running.
74
- */
75
- getInstanceURI(): URI;
76
-
77
- /**
78
- * Returns uri where plugin loaded into hosted instance is located.
79
- * Throws error if instance is not running.
80
- */
81
- getPluginURI(): URI;
82
-
83
- /**
84
- * Checks whether given uri points to a valid plugin.
85
- *
86
- * @param uri uri to the plugin source location
87
- */
88
- isPluginValid(uri: URI): Promise<boolean>;
89
- }
90
-
91
- const HOSTED_INSTANCE_START_TIMEOUT_MS = 30000;
92
- const THEIA_INSTANCE_REGEX = /.*Theia app listening on (.*).*\./;
93
- const PROCESS_OPTIONS = {
94
- cwd: process.cwd(),
95
- env: { ...process.env }
96
- };
97
-
98
- @injectable()
99
- export abstract class AbstractHostedInstanceManager implements HostedInstanceManager {
100
- protected hostedInstanceProcess: cp.ChildProcess;
101
- protected isPluginRunning: boolean = false;
102
- protected instanceUri: URI;
103
- protected pluginUri: URI;
104
- protected instanceOptions: Omit<RequestOptions, 'url'>;
105
-
106
- @inject(HostedPluginSupport)
107
- protected readonly hostedPluginSupport: HostedPluginSupport;
108
-
109
- @inject(MetadataScanner)
110
- protected readonly metadata: MetadataScanner;
111
-
112
- @inject(HostedPluginProcess)
113
- protected readonly hostedPluginProcess: HostedPluginProcess;
114
-
115
- @inject(RequestService)
116
- protected readonly request: RequestService;
117
-
118
- isRunning(): boolean {
119
- return this.isPluginRunning;
120
- }
121
-
122
- async run(pluginUri: URI, port?: number): Promise<URI> {
123
- return this.doRun(pluginUri, port);
124
- }
125
-
126
- async debug(pluginUri: URI, debugConfig: PluginDebugConfiguration): Promise<URI> {
127
- return this.doRun(pluginUri, undefined, debugConfig);
128
- }
129
-
130
- private async doRun(pluginUri: URI, port?: number, debugConfig?: PluginDebugConfiguration): Promise<URI> {
131
- if (this.isPluginRunning) {
132
- this.hostedPluginSupport.sendLog({ data: 'Hosted plugin instance is already running.', type: LogType.Info });
133
- throw new Error('Hosted instance is already running.');
134
- }
135
-
136
- let command: string[];
137
- let processOptions: cp.SpawnOptions;
138
- if (pluginUri.scheme === 'file') {
139
- processOptions = { ...PROCESS_OPTIONS };
140
- // get filesystem path that work cross operating systems
141
- processOptions.env!.HOSTED_PLUGIN = FileUri.fsPath(pluginUri.toString());
142
-
143
- // Disable all the other plugins on this instance
144
- processOptions.env!.THEIA_PLUGINS = '';
145
- command = await this.getStartCommand(port, debugConfig);
146
- } else {
147
- throw new Error('Not supported plugin location: ' + pluginUri.toString());
148
- }
149
-
150
- this.instanceUri = await this.postProcessInstanceUri(await this.runHostedPluginTheiaInstance(command, processOptions));
151
- this.pluginUri = pluginUri;
152
- // disable redirect to grab the release
153
- this.instanceOptions = {
154
- followRedirects: 0
155
- };
156
- this.instanceOptions = await this.postProcessInstanceOptions(this.instanceOptions);
157
- await this.checkInstanceUriReady();
158
-
159
- return this.instanceUri;
160
- }
161
-
162
- terminate(): void {
163
- if (this.isPluginRunning && !!this.hostedInstanceProcess.pid) {
164
- this.hostedPluginProcess.killProcessTree(this.hostedInstanceProcess.pid);
165
- this.hostedPluginSupport.sendLog({ data: 'Hosted instance has been terminated', type: LogType.Info });
166
- this.isPluginRunning = false;
167
- } else {
168
- throw new Error('Hosted plugin instance is not running.');
169
- }
170
- }
171
-
172
- getInstanceURI(): URI {
173
- if (this.isPluginRunning) {
174
- return this.instanceUri;
175
- }
176
- throw new Error('Hosted plugin instance is not running.');
177
- }
178
-
179
- getPluginURI(): URI {
180
- if (this.isPluginRunning) {
181
- return this.pluginUri;
182
- }
183
- throw new Error('Hosted plugin instance is not running.');
184
- }
185
-
186
- /**
187
- * Checks that the `instanceUri` is responding before exiting method
188
- */
189
- public async checkInstanceUriReady(): Promise<void> {
190
- return new Promise<void>((resolve, reject) => this.pingLoop(60, resolve, reject));
191
- }
192
-
193
- /**
194
- * Start a loop to ping, if ping is OK return immediately, else start a new ping after 1second. We iterate for the given amount of loops provided in remainingCount
195
- * @param remainingCount the number of occurrence to check
196
- * @param resolve resolve function if ok
197
- * @param reject reject function if error
198
- */
199
- private async pingLoop(remainingCount: number,
200
- resolve: (value?: void | PromiseLike<void> | undefined | Error) => void,
201
- reject: (value?: void | PromiseLike<void> | undefined | Error) => void): Promise<void> {
202
- const isOK = await this.ping();
203
- if (isOK) {
204
- resolve();
205
- } else {
206
- if (remainingCount > 0) {
207
- setTimeout(() => this.pingLoop(--remainingCount, resolve, reject), 1000);
208
- } else {
209
- reject(new Error('Unable to ping the remote server'));
210
- }
211
- }
212
- }
213
-
214
- /**
215
- * Ping the plugin URI (checking status of the head)
216
- */
217
- private async ping(): Promise<boolean> {
218
- try {
219
- const url = this.instanceUri.toString();
220
- // Wait that the status is OK
221
- const response = await this.request.request({ url, type: 'HEAD', ...this.instanceOptions });
222
- return response.res.statusCode === 200;
223
- } catch {
224
- return false;
225
- }
226
- }
227
-
228
- async isPluginValid(uri: URI): Promise<boolean> {
229
- const pckPath = path.join(FileUri.fsPath(uri), 'package.json');
230
- try {
231
- const pck = await fs.readJSON(pckPath);
232
- this.metadata.getScanner(pck);
233
- return true;
234
- } catch (err) {
235
- if (!isENOENT(err)) {
236
- console.error(err);
237
- }
238
- return false;
239
- }
240
- }
241
-
242
- protected async getStartCommand(port?: number, debugConfig?: PluginDebugConfiguration): Promise<string[]> {
243
-
244
- const processArguments = process.argv;
245
- let command: string[];
246
- if (environment.electron.is()) {
247
- command = ['yarn', 'theia', 'start'];
248
- } else {
249
- command = processArguments.filter((arg, index, args) => {
250
- // remove --port=X and --port X arguments if set
251
- // remove --plugins arguments
252
- if (arg.startsWith('--port') || args[index - 1] === '--port') {
253
- return;
254
- } else {
255
- return arg;
256
- }
257
-
258
- });
259
- }
260
- if (process.env.HOSTED_PLUGIN_HOSTNAME) {
261
- command.push('--hostname=' + process.env.HOSTED_PLUGIN_HOSTNAME);
262
- }
263
- if (port) {
264
- await this.validatePort(port);
265
- command.push('--port=' + port);
266
- }
267
-
268
- if (debugConfig) {
269
- command.push(`--hosted-plugin-${debugConfig.debugMode || 'inspect'}=0.0.0.0${debugConfig.debugPort ? ':' + debugConfig.debugPort : ''}`);
270
- }
271
- return command;
272
- }
273
-
274
- protected async postProcessInstanceUri(uri: URI): Promise<URI> {
275
- return uri;
276
- }
277
-
278
- protected async postProcessInstanceOptions(options: Omit<RequestOptions, 'url'>): Promise<Omit<RequestOptions, 'url'>> {
279
- return options;
280
- }
281
-
282
- protected runHostedPluginTheiaInstance(command: string[], options: cp.SpawnOptions): Promise<URI> {
283
- this.isPluginRunning = true;
284
- return new Promise((resolve, reject) => {
285
- let started = false;
286
- const outputListener = (data: string | Buffer) => {
287
- const line = data.toString();
288
- const match = THEIA_INSTANCE_REGEX.exec(line);
289
- if (match) {
290
- this.hostedInstanceProcess.stdout!.removeListener('data', outputListener);
291
- started = true;
292
- resolve(new URI(match[1]));
293
- }
294
- };
295
-
296
- if (isWindows) {
297
- // Has to be set for running on windows (electron).
298
- // See also: https://github.com/nodejs/node/issues/3675
299
- options.shell = true;
300
- }
301
-
302
- this.hostedInstanceProcess = cp.spawn(command.shift()!, command, options);
303
- this.hostedInstanceProcess.on('error', () => { this.isPluginRunning = false; });
304
- this.hostedInstanceProcess.on('exit', () => { this.isPluginRunning = false; });
305
- this.hostedInstanceProcess.stdout!.addListener('data', outputListener);
306
-
307
- this.hostedInstanceProcess.stdout!.addListener('data', data => {
308
- this.hostedPluginSupport.sendLog({ data: data.toString(), type: LogType.Info });
309
- });
310
- this.hostedInstanceProcess.stderr!.addListener('data', data => {
311
- this.hostedPluginSupport.sendLog({ data: data.toString(), type: LogType.Error });
312
- });
313
-
314
- setTimeout(() => {
315
- if (!started) {
316
- this.terminate();
317
- this.isPluginRunning = false;
318
- reject(new Error('Timeout.'));
319
- }
320
- }, HOSTED_INSTANCE_START_TIMEOUT_MS);
321
- });
322
- }
323
-
324
- protected async validatePort(port: number): Promise<void> {
325
- if (port < 1 || port > 65535) {
326
- throw new Error('Port value is incorrect.');
327
- }
328
-
329
- if (! await this.isPortFree(port)) {
330
- throw new Error('Port ' + port + ' is already in use.');
331
- }
332
- }
333
-
334
- protected isPortFree(port: number): Promise<boolean> {
335
- return new Promise(resolve => {
336
- const server = net.createServer();
337
- server.listen(port, '0.0.0.0');
338
- server.on('error', () => {
339
- resolve(false);
340
- });
341
- server.on('listening', () => {
342
- server.close();
343
- resolve(true);
344
- });
345
- });
346
- }
347
-
348
- }
349
-
350
- @injectable()
351
- export class NodeHostedPluginRunner extends AbstractHostedInstanceManager {
352
- @inject(ContributionProvider) @named(Symbol.for(HostedPluginUriPostProcessorSymbolName))
353
- protected readonly uriPostProcessors: ContributionProvider<HostedPluginUriPostProcessor>;
354
-
355
- protected override async postProcessInstanceUri(uri: URI): Promise<URI> {
356
- for (const uriPostProcessor of this.uriPostProcessors.getContributions()) {
357
- uri = await uriPostProcessor.processUri(uri);
358
- }
359
- return uri;
360
- }
361
-
362
- protected override async postProcessInstanceOptions(options: object): Promise<object> {
363
- for (const uriPostProcessor of this.uriPostProcessors.getContributions()) {
364
- options = await uriPostProcessor.processOptions(options);
365
- }
366
- return options;
367
- }
368
-
369
- protected override async getStartCommand(port?: number, debugConfig?: PluginDebugConfiguration): Promise<string[]> {
370
- if (!port) {
371
- port = process.env.HOSTED_PLUGIN_PORT ?
372
- Number(process.env.HOSTED_PLUGIN_PORT) :
373
- (debugConfig?.debugPort ? Number(debugConfig.debugPort) : DEFAULT_HOSTED_PLUGIN_PORT);
374
- }
375
- return super.getStartCommand(port, debugConfig);
376
- }
377
- }
378
-
379
- @injectable()
380
- export class ElectronNodeHostedPluginRunner extends AbstractHostedInstanceManager {
381
-
382
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2018 Red Hat, Inc. and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { RequestOptions, RequestService } from '@theia/core/shared/@theia/request';
18
+ import { inject, injectable, named } from '@theia/core/shared/inversify';
19
+ import * as cp from 'child_process';
20
+ import * as fs from '@theia/core/shared/fs-extra';
21
+ import * as net from 'net';
22
+ import * as path from 'path';
23
+ import URI from '@theia/core/lib/common/uri';
24
+ import { ContributionProvider } from '@theia/core/lib/common/contribution-provider';
25
+ import { HostedPluginUriPostProcessor, HostedPluginUriPostProcessorSymbolName } from './hosted-plugin-uri-postprocessor';
26
+ import { environment, isWindows } from '@theia/core';
27
+ import { FileUri } from '@theia/core/lib/common/file-uri';
28
+ import { LogType } from '@theia/plugin-ext/lib/common/types';
29
+ import { HostedPluginSupport } from '@theia/plugin-ext/lib/hosted/node/hosted-plugin';
30
+ import { MetadataScanner } from '@theia/plugin-ext/lib/hosted/node/metadata-scanner';
31
+ import { PluginDebugConfiguration } from '../common/plugin-dev-protocol';
32
+ import { HostedPluginProcess } from '@theia/plugin-ext/lib/hosted/node/hosted-plugin-process';
33
+ import { isENOENT } from '@theia/plugin-ext/lib/common/errors';
34
+
35
+ const DEFAULT_HOSTED_PLUGIN_PORT = 3030;
36
+
37
+ export const HostedInstanceManager = Symbol('HostedInstanceManager');
38
+
39
+ /**
40
+ * Is responsible for running and handling separate Theia instance with given plugin.
41
+ */
42
+ export interface HostedInstanceManager {
43
+ /**
44
+ * Checks whether hosted instance is run.
45
+ */
46
+ isRunning(): boolean;
47
+
48
+ /**
49
+ * Runs specified by the given uri plugin in separate Theia instance.
50
+ *
51
+ * @param pluginUri uri to the plugin source location
52
+ * @param port port on which new instance of Theia should be run. Optional.
53
+ * @returns uri where new Theia instance is run
54
+ */
55
+ run(pluginUri: URI, port?: number): Promise<URI>;
56
+
57
+ /**
58
+ * Runs specified by the given uri plugin with debug in separate Theia instance.
59
+ * @param pluginUri uri to the plugin source location
60
+ * @param debugConfig debug configuration
61
+ * @returns uri where new Theia instance is run
62
+ */
63
+ debug(pluginUri: URI, debugConfig: PluginDebugConfiguration): Promise<URI>;
64
+
65
+ /**
66
+ * Terminates hosted plugin instance.
67
+ * Throws error if instance is not running.
68
+ */
69
+ terminate(): void;
70
+
71
+ /**
72
+ * Returns uri where hosted instance is run.
73
+ * Throws error if instance is not running.
74
+ */
75
+ getInstanceURI(): URI;
76
+
77
+ /**
78
+ * Returns uri where plugin loaded into hosted instance is located.
79
+ * Throws error if instance is not running.
80
+ */
81
+ getPluginURI(): URI;
82
+
83
+ /**
84
+ * Checks whether given uri points to a valid plugin.
85
+ *
86
+ * @param uri uri to the plugin source location
87
+ */
88
+ isPluginValid(uri: URI): Promise<boolean>;
89
+ }
90
+
91
+ const HOSTED_INSTANCE_START_TIMEOUT_MS = 30000;
92
+ const THEIA_INSTANCE_REGEX = /.*Theia app listening on (.*).*\./;
93
+ const PROCESS_OPTIONS = {
94
+ cwd: process.cwd(),
95
+ env: { ...process.env }
96
+ };
97
+
98
+ @injectable()
99
+ export abstract class AbstractHostedInstanceManager implements HostedInstanceManager {
100
+ protected hostedInstanceProcess: cp.ChildProcess;
101
+ protected isPluginRunning: boolean = false;
102
+ protected instanceUri: URI;
103
+ protected pluginUri: URI;
104
+ protected instanceOptions: Omit<RequestOptions, 'url'>;
105
+
106
+ @inject(HostedPluginSupport)
107
+ protected readonly hostedPluginSupport: HostedPluginSupport;
108
+
109
+ @inject(MetadataScanner)
110
+ protected readonly metadata: MetadataScanner;
111
+
112
+ @inject(HostedPluginProcess)
113
+ protected readonly hostedPluginProcess: HostedPluginProcess;
114
+
115
+ @inject(RequestService)
116
+ protected readonly request: RequestService;
117
+
118
+ isRunning(): boolean {
119
+ return this.isPluginRunning;
120
+ }
121
+
122
+ async run(pluginUri: URI, port?: number): Promise<URI> {
123
+ return this.doRun(pluginUri, port);
124
+ }
125
+
126
+ async debug(pluginUri: URI, debugConfig: PluginDebugConfiguration): Promise<URI> {
127
+ return this.doRun(pluginUri, undefined, debugConfig);
128
+ }
129
+
130
+ private async doRun(pluginUri: URI, port?: number, debugConfig?: PluginDebugConfiguration): Promise<URI> {
131
+ if (this.isPluginRunning) {
132
+ this.hostedPluginSupport.sendLog({ data: 'Hosted plugin instance is already running.', type: LogType.Info });
133
+ throw new Error('Hosted instance is already running.');
134
+ }
135
+
136
+ let command: string[];
137
+ let processOptions: cp.SpawnOptions;
138
+ if (pluginUri.scheme === 'file') {
139
+ processOptions = { ...PROCESS_OPTIONS };
140
+ // get filesystem path that work cross operating systems
141
+ processOptions.env!.HOSTED_PLUGIN = FileUri.fsPath(pluginUri.toString());
142
+
143
+ // Disable all the other plugins on this instance
144
+ processOptions.env!.THEIA_PLUGINS = '';
145
+ command = await this.getStartCommand(port, debugConfig);
146
+ } else {
147
+ throw new Error('Not supported plugin location: ' + pluginUri.toString());
148
+ }
149
+
150
+ this.instanceUri = await this.postProcessInstanceUri(await this.runHostedPluginTheiaInstance(command, processOptions));
151
+ this.pluginUri = pluginUri;
152
+ // disable redirect to grab the release
153
+ this.instanceOptions = {
154
+ followRedirects: 0
155
+ };
156
+ this.instanceOptions = await this.postProcessInstanceOptions(this.instanceOptions);
157
+ await this.checkInstanceUriReady();
158
+
159
+ return this.instanceUri;
160
+ }
161
+
162
+ terminate(): void {
163
+ if (this.isPluginRunning && !!this.hostedInstanceProcess.pid) {
164
+ this.hostedPluginProcess.killProcessTree(this.hostedInstanceProcess.pid);
165
+ this.hostedPluginSupport.sendLog({ data: 'Hosted instance has been terminated', type: LogType.Info });
166
+ this.isPluginRunning = false;
167
+ } else {
168
+ throw new Error('Hosted plugin instance is not running.');
169
+ }
170
+ }
171
+
172
+ getInstanceURI(): URI {
173
+ if (this.isPluginRunning) {
174
+ return this.instanceUri;
175
+ }
176
+ throw new Error('Hosted plugin instance is not running.');
177
+ }
178
+
179
+ getPluginURI(): URI {
180
+ if (this.isPluginRunning) {
181
+ return this.pluginUri;
182
+ }
183
+ throw new Error('Hosted plugin instance is not running.');
184
+ }
185
+
186
+ /**
187
+ * Checks that the `instanceUri` is responding before exiting method
188
+ */
189
+ public async checkInstanceUriReady(): Promise<void> {
190
+ return new Promise<void>((resolve, reject) => this.pingLoop(60, resolve, reject));
191
+ }
192
+
193
+ /**
194
+ * Start a loop to ping, if ping is OK return immediately, else start a new ping after 1second. We iterate for the given amount of loops provided in remainingCount
195
+ * @param remainingCount the number of occurrence to check
196
+ * @param resolve resolve function if ok
197
+ * @param reject reject function if error
198
+ */
199
+ private async pingLoop(remainingCount: number,
200
+ resolve: (value?: void | PromiseLike<void> | undefined | Error) => void,
201
+ reject: (value?: void | PromiseLike<void> | undefined | Error) => void): Promise<void> {
202
+ const isOK = await this.ping();
203
+ if (isOK) {
204
+ resolve();
205
+ } else {
206
+ if (remainingCount > 0) {
207
+ setTimeout(() => this.pingLoop(--remainingCount, resolve, reject), 1000);
208
+ } else {
209
+ reject(new Error('Unable to ping the remote server'));
210
+ }
211
+ }
212
+ }
213
+
214
+ /**
215
+ * Ping the plugin URI (checking status of the head)
216
+ */
217
+ private async ping(): Promise<boolean> {
218
+ try {
219
+ const url = this.instanceUri.toString();
220
+ // Wait that the status is OK
221
+ const response = await this.request.request({ url, type: 'HEAD', ...this.instanceOptions });
222
+ return response.res.statusCode === 200;
223
+ } catch {
224
+ return false;
225
+ }
226
+ }
227
+
228
+ async isPluginValid(uri: URI): Promise<boolean> {
229
+ const pckPath = path.join(FileUri.fsPath(uri), 'package.json');
230
+ try {
231
+ const pck = await fs.readJSON(pckPath);
232
+ this.metadata.getScanner(pck);
233
+ return true;
234
+ } catch (err) {
235
+ if (!isENOENT(err)) {
236
+ console.error(err);
237
+ }
238
+ return false;
239
+ }
240
+ }
241
+
242
+ protected async getStartCommand(port?: number, debugConfig?: PluginDebugConfiguration): Promise<string[]> {
243
+
244
+ const processArguments = process.argv;
245
+ let command: string[];
246
+ if (environment.electron.is()) {
247
+ command = ['yarn', 'theia', 'start'];
248
+ } else {
249
+ command = processArguments.filter((arg, index, args) => {
250
+ // remove --port=X and --port X arguments if set
251
+ // remove --plugins arguments
252
+ if (arg.startsWith('--port') || args[index - 1] === '--port') {
253
+ return;
254
+ } else {
255
+ return arg;
256
+ }
257
+
258
+ });
259
+ }
260
+ if (process.env.HOSTED_PLUGIN_HOSTNAME) {
261
+ command.push('--hostname=' + process.env.HOSTED_PLUGIN_HOSTNAME);
262
+ }
263
+ if (port) {
264
+ await this.validatePort(port);
265
+ command.push('--port=' + port);
266
+ }
267
+
268
+ if (debugConfig) {
269
+ command.push(`--hosted-plugin-${debugConfig.debugMode || 'inspect'}=0.0.0.0${debugConfig.debugPort ? ':' + debugConfig.debugPort : ''}`);
270
+ }
271
+ return command;
272
+ }
273
+
274
+ protected async postProcessInstanceUri(uri: URI): Promise<URI> {
275
+ return uri;
276
+ }
277
+
278
+ protected async postProcessInstanceOptions(options: Omit<RequestOptions, 'url'>): Promise<Omit<RequestOptions, 'url'>> {
279
+ return options;
280
+ }
281
+
282
+ protected runHostedPluginTheiaInstance(command: string[], options: cp.SpawnOptions): Promise<URI> {
283
+ this.isPluginRunning = true;
284
+ return new Promise((resolve, reject) => {
285
+ let started = false;
286
+ const outputListener = (data: string | Buffer) => {
287
+ const line = data.toString();
288
+ const match = THEIA_INSTANCE_REGEX.exec(line);
289
+ if (match) {
290
+ this.hostedInstanceProcess.stdout!.removeListener('data', outputListener);
291
+ started = true;
292
+ resolve(new URI(match[1]));
293
+ }
294
+ };
295
+
296
+ if (isWindows) {
297
+ // Has to be set for running on windows (electron).
298
+ // See also: https://github.com/nodejs/node/issues/3675
299
+ options.shell = true;
300
+ }
301
+
302
+ this.hostedInstanceProcess = cp.spawn(command.shift()!, command, options);
303
+ this.hostedInstanceProcess.on('error', () => { this.isPluginRunning = false; });
304
+ this.hostedInstanceProcess.on('exit', () => { this.isPluginRunning = false; });
305
+ this.hostedInstanceProcess.stdout!.addListener('data', outputListener);
306
+
307
+ this.hostedInstanceProcess.stdout!.addListener('data', data => {
308
+ this.hostedPluginSupport.sendLog({ data: data.toString(), type: LogType.Info });
309
+ });
310
+ this.hostedInstanceProcess.stderr!.addListener('data', data => {
311
+ this.hostedPluginSupport.sendLog({ data: data.toString(), type: LogType.Error });
312
+ });
313
+
314
+ setTimeout(() => {
315
+ if (!started) {
316
+ this.terminate();
317
+ this.isPluginRunning = false;
318
+ reject(new Error('Timeout.'));
319
+ }
320
+ }, HOSTED_INSTANCE_START_TIMEOUT_MS);
321
+ });
322
+ }
323
+
324
+ protected async validatePort(port: number): Promise<void> {
325
+ if (port < 1 || port > 65535) {
326
+ throw new Error('Port value is incorrect.');
327
+ }
328
+
329
+ if (! await this.isPortFree(port)) {
330
+ throw new Error('Port ' + port + ' is already in use.');
331
+ }
332
+ }
333
+
334
+ protected isPortFree(port: number): Promise<boolean> {
335
+ return new Promise(resolve => {
336
+ const server = net.createServer();
337
+ server.listen(port, '0.0.0.0');
338
+ server.on('error', () => {
339
+ resolve(false);
340
+ });
341
+ server.on('listening', () => {
342
+ server.close();
343
+ resolve(true);
344
+ });
345
+ });
346
+ }
347
+
348
+ }
349
+
350
+ @injectable()
351
+ export class NodeHostedPluginRunner extends AbstractHostedInstanceManager {
352
+ @inject(ContributionProvider) @named(Symbol.for(HostedPluginUriPostProcessorSymbolName))
353
+ protected readonly uriPostProcessors: ContributionProvider<HostedPluginUriPostProcessor>;
354
+
355
+ protected override async postProcessInstanceUri(uri: URI): Promise<URI> {
356
+ for (const uriPostProcessor of this.uriPostProcessors.getContributions()) {
357
+ uri = await uriPostProcessor.processUri(uri);
358
+ }
359
+ return uri;
360
+ }
361
+
362
+ protected override async postProcessInstanceOptions(options: object): Promise<object> {
363
+ for (const uriPostProcessor of this.uriPostProcessors.getContributions()) {
364
+ options = await uriPostProcessor.processOptions(options);
365
+ }
366
+ return options;
367
+ }
368
+
369
+ protected override async getStartCommand(port?: number, debugConfig?: PluginDebugConfiguration): Promise<string[]> {
370
+ if (!port) {
371
+ port = process.env.HOSTED_PLUGIN_PORT ?
372
+ Number(process.env.HOSTED_PLUGIN_PORT) :
373
+ (debugConfig?.debugPort ? Number(debugConfig.debugPort) : DEFAULT_HOSTED_PLUGIN_PORT);
374
+ }
375
+ return super.getStartCommand(port, debugConfig);
376
+ }
377
+ }
378
+
379
+ @injectable()
380
+ export class ElectronNodeHostedPluginRunner extends AbstractHostedInstanceManager {
381
+
382
+ }