@theia/plugin-dev 1.34.1 → 1.34.3

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 (53) hide show
  1. package/LICENSE +641 -641
  2. package/README.md +31 -31
  3. package/lib/browser/hosted-plugin-controller.d.ts +74 -74
  4. package/lib/browser/hosted-plugin-controller.js +352 -352
  5. package/lib/browser/hosted-plugin-frontend-contribution.d.ts +6 -6
  6. package/lib/browser/hosted-plugin-frontend-contribution.js +56 -56
  7. package/lib/browser/hosted-plugin-informer.d.ts +25 -25
  8. package/lib/browser/hosted-plugin-informer.js +102 -102
  9. package/lib/browser/hosted-plugin-log-viewer.d.ts +14 -14
  10. package/lib/browser/hosted-plugin-log-viewer.js +69 -69
  11. package/lib/browser/hosted-plugin-manager-client.d.ts +79 -79
  12. package/lib/browser/hosted-plugin-manager-client.js +407 -407
  13. package/lib/browser/hosted-plugin-preferences.d.ts +13 -13
  14. package/lib/browser/hosted-plugin-preferences.js +60 -60
  15. package/lib/browser/plugin-dev-frontend-module.d.ts +3 -3
  16. package/lib/browser/plugin-dev-frontend-module.js +42 -42
  17. package/lib/common/index.d.ts +2 -2
  18. package/lib/common/index.js +31 -31
  19. package/lib/common/plugin-dev-protocol.d.ts +24 -24
  20. package/lib/common/plugin-dev-protocol.js +20 -20
  21. package/lib/node/hosted-instance-manager.d.ts +102 -102
  22. package/lib/node/hosted-instance-manager.js +316 -316
  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-uri-postprocessor.d.ts +6 -6
  26. package/lib/node/hosted-plugin-uri-postprocessor.js +27 -27
  27. package/lib/node/hosted-plugins-manager.d.ts +41 -41
  28. package/lib/node/hosted-plugins-manager.js +118 -118
  29. package/lib/node/plugin-dev-backend-module.d.ts +4 -4
  30. package/lib/node/plugin-dev-backend-module.js +53 -53
  31. package/lib/node/plugin-dev-service.d.ts +25 -25
  32. package/lib/node/plugin-dev-service.js +108 -108
  33. package/lib/node-electron/plugin-dev-electron-backend-module.d.ts +3 -3
  34. package/lib/node-electron/plugin-dev-electron-backend-module.js +28 -28
  35. package/lib/package.spec.js +25 -25
  36. package/package.json +9 -9
  37. package/src/browser/hosted-plugin-controller.ts +356 -356
  38. package/src/browser/hosted-plugin-frontend-contribution.ts +45 -45
  39. package/src/browser/hosted-plugin-informer.ts +93 -93
  40. package/src/browser/hosted-plugin-log-viewer.ts +52 -52
  41. package/src/browser/hosted-plugin-manager-client.ts +426 -426
  42. package/src/browser/hosted-plugin-preferences.ts +71 -71
  43. package/src/browser/plugin-dev-frontend-module.ts +45 -45
  44. package/src/common/index.ts +21 -21
  45. package/src/common/plugin-dev-protocol.ts +45 -45
  46. package/src/node/hosted-instance-manager.ts +381 -381
  47. package/src/node/hosted-plugin-reader.ts +58 -58
  48. package/src/node/hosted-plugin-uri-postprocessor.ts +32 -32
  49. package/src/node/hosted-plugins-manager.ts +146 -146
  50. package/src/node/plugin-dev-backend-module.ts +54 -54
  51. package/src/node/plugin-dev-service.ts +107 -107
  52. package/src/node-electron/plugin-dev-electron-backend-module.ts +29 -29
  53. package/src/package.spec.ts +28 -28
@@ -1,317 +1,317 @@
1
- "use strict";
2
- // *****************************************************************************
3
- // Copyright (C) 2018 Red Hat, Inc. and others.
4
- //
5
- // This program and the accompanying materials are made available under the
6
- // terms of the Eclipse Public License v. 2.0 which is available at
7
- // http://www.eclipse.org/legal/epl-2.0.
8
- //
9
- // This Source Code may also be made available under the following Secondary
10
- // Licenses when the conditions for such availability set forth in the Eclipse
11
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
- // with the GNU Classpath Exception which is available at
13
- // https://www.gnu.org/software/classpath/license.html.
14
- //
15
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16
- // *****************************************************************************
17
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
- return c > 3 && r && Object.defineProperty(target, key, r), r;
22
- };
23
- var __metadata = (this && this.__metadata) || function (k, v) {
24
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
- };
26
- Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.ElectronNodeHostedPluginRunner = exports.NodeHostedPluginRunner = exports.AbstractHostedInstanceManager = exports.HostedInstanceManager = void 0;
28
- const inversify_1 = require("@theia/core/shared/inversify");
29
- const cp = require("child_process");
30
- const fs = require("@theia/core/shared/fs-extra");
31
- const net = require("net");
32
- const path = require("path");
33
- const request = require("request");
34
- const uri_1 = require("@theia/core/lib/common/uri");
35
- const contribution_provider_1 = require("@theia/core/lib/common/contribution-provider");
36
- const hosted_plugin_uri_postprocessor_1 = require("./hosted-plugin-uri-postprocessor");
37
- const core_1 = require("@theia/core");
38
- const file_uri_1 = require("@theia/core/lib/node/file-uri");
39
- const types_1 = require("@theia/plugin-ext/lib/common/types");
40
- const hosted_plugin_1 = require("@theia/plugin-ext/lib/hosted/node/hosted-plugin");
41
- const metadata_scanner_1 = require("@theia/plugin-ext/lib/hosted/node/metadata-scanner");
42
- const hosted_plugin_process_1 = require("@theia/plugin-ext/lib/hosted/node/hosted-plugin-process");
43
- const DEFAULT_HOSTED_PLUGIN_PORT = 3030;
44
- exports.HostedInstanceManager = Symbol('HostedInstanceManager');
45
- const HOSTED_INSTANCE_START_TIMEOUT_MS = 30000;
46
- const THEIA_INSTANCE_REGEX = /.*Theia app listening on (.*).*\./;
47
- const PROCESS_OPTIONS = {
48
- cwd: process.cwd(),
49
- env: Object.assign({}, process.env)
50
- };
51
- let AbstractHostedInstanceManager = class AbstractHostedInstanceManager {
52
- constructor() {
53
- this.isPluginRunning = false;
54
- }
55
- isRunning() {
56
- return this.isPluginRunning;
57
- }
58
- async run(pluginUri, port) {
59
- return this.doRun(pluginUri, port);
60
- }
61
- async debug(pluginUri, debugConfig) {
62
- return this.doRun(pluginUri, undefined, debugConfig);
63
- }
64
- async doRun(pluginUri, port, debugConfig) {
65
- if (this.isPluginRunning) {
66
- this.hostedPluginSupport.sendLog({ data: 'Hosted plugin instance is already running.', type: types_1.LogType.Info });
67
- throw new Error('Hosted instance is already running.');
68
- }
69
- let command;
70
- let processOptions;
71
- if (pluginUri.scheme === 'file') {
72
- processOptions = Object.assign({}, PROCESS_OPTIONS);
73
- // get filesystem path that work cross operating systems
74
- processOptions.env.HOSTED_PLUGIN = file_uri_1.FileUri.fsPath(pluginUri.toString());
75
- // Disable all the other plugins on this instance
76
- processOptions.env.THEIA_PLUGINS = '';
77
- command = await this.getStartCommand(port, debugConfig);
78
- }
79
- else {
80
- throw new Error('Not supported plugin location: ' + pluginUri.toString());
81
- }
82
- this.instanceUri = await this.postProcessInstanceUri(await this.runHostedPluginTheiaInstance(command, processOptions));
83
- this.pluginUri = pluginUri;
84
- // disable redirect to grab the release
85
- this.instanceOptions = {
86
- followRedirect: false
87
- };
88
- this.instanceOptions = await this.postProcessInstanceOptions(this.instanceOptions);
89
- await this.checkInstanceUriReady();
90
- return this.instanceUri;
91
- }
92
- terminate() {
93
- if (this.isPluginRunning) {
94
- this.hostedPluginProcess.killProcessTree(this.hostedInstanceProcess.pid);
95
- this.hostedPluginSupport.sendLog({ data: 'Hosted instance has been terminated', type: types_1.LogType.Info });
96
- this.isPluginRunning = false;
97
- }
98
- else {
99
- throw new Error('Hosted plugin instance is not running.');
100
- }
101
- }
102
- getInstanceURI() {
103
- if (this.isPluginRunning) {
104
- return this.instanceUri;
105
- }
106
- throw new Error('Hosted plugin instance is not running.');
107
- }
108
- getPluginURI() {
109
- if (this.isPluginRunning) {
110
- return this.pluginUri;
111
- }
112
- throw new Error('Hosted plugin instance is not running.');
113
- }
114
- /**
115
- * Checks that the `instanceUri` is responding before exiting method
116
- */
117
- async checkInstanceUriReady() {
118
- return new Promise((resolve, reject) => this.pingLoop(60, resolve, reject));
119
- }
120
- /**
121
- * 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
122
- * @param remainingCount the number of occurrence to check
123
- * @param resolve resolve function if ok
124
- * @param reject reject function if error
125
- */
126
- async pingLoop(remainingCount, resolve, reject) {
127
- const isOK = await this.ping();
128
- if (isOK) {
129
- resolve();
130
- }
131
- else {
132
- if (remainingCount > 0) {
133
- setTimeout(() => this.pingLoop(--remainingCount, resolve, reject), 1000);
134
- }
135
- else {
136
- reject(new Error('Unable to ping the remote server'));
137
- }
138
- }
139
- }
140
- /**
141
- * Ping the plugin URI (checking status of the head)
142
- */
143
- async ping() {
144
- return new Promise((resolve, reject) => {
145
- const url = this.instanceUri.toString();
146
- request.head(url, this.instanceOptions).on('response', res => {
147
- // Wait that the status is OK
148
- resolve(res.statusCode === 200);
149
- }).on('error', error => {
150
- resolve(false);
151
- });
152
- });
153
- }
154
- isPluginValid(uri) {
155
- const pckPath = path.join(file_uri_1.FileUri.fsPath(uri), 'package.json');
156
- if (fs.existsSync(pckPath)) {
157
- const pck = fs.readJSONSync(pckPath);
158
- try {
159
- this.metadata.getScanner(pck);
160
- return true;
161
- }
162
- catch (e) {
163
- console.error(e);
164
- return false;
165
- }
166
- }
167
- return false;
168
- }
169
- async getStartCommand(port, debugConfig) {
170
- const processArguments = process.argv;
171
- let command;
172
- if (core_1.environment.electron.is()) {
173
- command = ['yarn', 'theia', 'start'];
174
- }
175
- else {
176
- command = processArguments.filter((arg, index, args) => {
177
- // remove --port=X and --port X arguments if set
178
- // remove --plugins arguments
179
- if (arg.startsWith('--port') || args[index - 1] === '--port') {
180
- return;
181
- }
182
- else {
183
- return arg;
184
- }
185
- });
186
- }
187
- if (process.env.HOSTED_PLUGIN_HOSTNAME) {
188
- command.push('--hostname=' + process.env.HOSTED_PLUGIN_HOSTNAME);
189
- }
190
- if (port) {
191
- await this.validatePort(port);
192
- command.push('--port=' + port);
193
- }
194
- if (debugConfig) {
195
- command.push(`--hosted-plugin-${debugConfig.debugMode || 'inspect'}=0.0.0.0${debugConfig.debugPort ? ':' + debugConfig.debugPort : ''}`);
196
- }
197
- return command;
198
- }
199
- async postProcessInstanceUri(uri) {
200
- return uri;
201
- }
202
- async postProcessInstanceOptions(options) {
203
- return options;
204
- }
205
- runHostedPluginTheiaInstance(command, options) {
206
- this.isPluginRunning = true;
207
- return new Promise((resolve, reject) => {
208
- let started = false;
209
- const outputListener = (data) => {
210
- const line = data.toString();
211
- const match = THEIA_INSTANCE_REGEX.exec(line);
212
- if (match) {
213
- this.hostedInstanceProcess.stdout.removeListener('data', outputListener);
214
- started = true;
215
- resolve(new uri_1.default(match[1]));
216
- }
217
- };
218
- if (core_1.isWindows) {
219
- // Has to be set for running on windows (electron).
220
- // See also: https://github.com/nodejs/node/issues/3675
221
- options.shell = true;
222
- }
223
- this.hostedInstanceProcess = cp.spawn(command.shift(), command, options);
224
- this.hostedInstanceProcess.on('error', () => { this.isPluginRunning = false; });
225
- this.hostedInstanceProcess.on('exit', () => { this.isPluginRunning = false; });
226
- this.hostedInstanceProcess.stdout.addListener('data', outputListener);
227
- this.hostedInstanceProcess.stdout.addListener('data', data => {
228
- this.hostedPluginSupport.sendLog({ data: data.toString(), type: types_1.LogType.Info });
229
- });
230
- this.hostedInstanceProcess.stderr.addListener('data', data => {
231
- this.hostedPluginSupport.sendLog({ data: data.toString(), type: types_1.LogType.Error });
232
- });
233
- setTimeout(() => {
234
- if (!started) {
235
- this.terminate();
236
- this.isPluginRunning = false;
237
- reject(new Error('Timeout.'));
238
- }
239
- }, HOSTED_INSTANCE_START_TIMEOUT_MS);
240
- });
241
- }
242
- async validatePort(port) {
243
- if (port < 1 || port > 65535) {
244
- throw new Error('Port value is incorrect.');
245
- }
246
- if (!await this.isPortFree(port)) {
247
- throw new Error('Port ' + port + ' is already in use.');
248
- }
249
- }
250
- isPortFree(port) {
251
- return new Promise(resolve => {
252
- const server = net.createServer();
253
- server.listen(port, '0.0.0.0');
254
- server.on('error', () => {
255
- resolve(false);
256
- });
257
- server.on('listening', () => {
258
- server.close();
259
- resolve(true);
260
- });
261
- });
262
- }
263
- };
264
- __decorate([
265
- (0, inversify_1.inject)(hosted_plugin_1.HostedPluginSupport),
266
- __metadata("design:type", hosted_plugin_1.HostedPluginSupport)
267
- ], AbstractHostedInstanceManager.prototype, "hostedPluginSupport", void 0);
268
- __decorate([
269
- (0, inversify_1.inject)(metadata_scanner_1.MetadataScanner),
270
- __metadata("design:type", metadata_scanner_1.MetadataScanner)
271
- ], AbstractHostedInstanceManager.prototype, "metadata", void 0);
272
- __decorate([
273
- (0, inversify_1.inject)(hosted_plugin_process_1.HostedPluginProcess),
274
- __metadata("design:type", hosted_plugin_process_1.HostedPluginProcess)
275
- ], AbstractHostedInstanceManager.prototype, "hostedPluginProcess", void 0);
276
- AbstractHostedInstanceManager = __decorate([
277
- (0, inversify_1.injectable)()
278
- ], AbstractHostedInstanceManager);
279
- exports.AbstractHostedInstanceManager = AbstractHostedInstanceManager;
280
- let NodeHostedPluginRunner = class NodeHostedPluginRunner extends AbstractHostedInstanceManager {
281
- async postProcessInstanceUri(uri) {
282
- for (const uriPostProcessor of this.uriPostProcessors.getContributions()) {
283
- uri = await uriPostProcessor.processUri(uri);
284
- }
285
- return uri;
286
- }
287
- async postProcessInstanceOptions(options) {
288
- for (const uriPostProcessor of this.uriPostProcessors.getContributions()) {
289
- options = await uriPostProcessor.processOptions(options);
290
- }
291
- return options;
292
- }
293
- async getStartCommand(port, debugConfig) {
294
- if (!port) {
295
- port = process.env.HOSTED_PLUGIN_PORT ?
296
- Number(process.env.HOSTED_PLUGIN_PORT) :
297
- ((debugConfig === null || debugConfig === void 0 ? void 0 : debugConfig.debugPort) ? Number(debugConfig.debugPort) : DEFAULT_HOSTED_PLUGIN_PORT);
298
- }
299
- return super.getStartCommand(port, debugConfig);
300
- }
301
- };
302
- __decorate([
303
- (0, inversify_1.inject)(contribution_provider_1.ContributionProvider),
304
- (0, inversify_1.named)(Symbol.for(hosted_plugin_uri_postprocessor_1.HostedPluginUriPostProcessorSymbolName)),
305
- __metadata("design:type", Object)
306
- ], NodeHostedPluginRunner.prototype, "uriPostProcessors", void 0);
307
- NodeHostedPluginRunner = __decorate([
308
- (0, inversify_1.injectable)()
309
- ], NodeHostedPluginRunner);
310
- exports.NodeHostedPluginRunner = NodeHostedPluginRunner;
311
- let ElectronNodeHostedPluginRunner = class ElectronNodeHostedPluginRunner extends AbstractHostedInstanceManager {
312
- };
313
- ElectronNodeHostedPluginRunner = __decorate([
314
- (0, inversify_1.injectable)()
315
- ], ElectronNodeHostedPluginRunner);
316
- exports.ElectronNodeHostedPluginRunner = ElectronNodeHostedPluginRunner;
1
+ "use strict";
2
+ // *****************************************************************************
3
+ // Copyright (C) 2018 Red Hat, Inc. and others.
4
+ //
5
+ // This program and the accompanying materials are made available under the
6
+ // terms of the Eclipse Public License v. 2.0 which is available at
7
+ // http://www.eclipse.org/legal/epl-2.0.
8
+ //
9
+ // This Source Code may also be made available under the following Secondary
10
+ // Licenses when the conditions for such availability set forth in the Eclipse
11
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
12
+ // with the GNU Classpath Exception which is available at
13
+ // https://www.gnu.org/software/classpath/license.html.
14
+ //
15
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16
+ // *****************************************************************************
17
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
22
+ };
23
+ var __metadata = (this && this.__metadata) || function (k, v) {
24
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25
+ };
26
+ Object.defineProperty(exports, "__esModule", { value: true });
27
+ exports.ElectronNodeHostedPluginRunner = exports.NodeHostedPluginRunner = exports.AbstractHostedInstanceManager = exports.HostedInstanceManager = void 0;
28
+ const inversify_1 = require("@theia/core/shared/inversify");
29
+ const cp = require("child_process");
30
+ const fs = require("@theia/core/shared/fs-extra");
31
+ const net = require("net");
32
+ const path = require("path");
33
+ const request = require("request");
34
+ const uri_1 = require("@theia/core/lib/common/uri");
35
+ const contribution_provider_1 = require("@theia/core/lib/common/contribution-provider");
36
+ const hosted_plugin_uri_postprocessor_1 = require("./hosted-plugin-uri-postprocessor");
37
+ const core_1 = require("@theia/core");
38
+ const file_uri_1 = require("@theia/core/lib/node/file-uri");
39
+ const types_1 = require("@theia/plugin-ext/lib/common/types");
40
+ const hosted_plugin_1 = require("@theia/plugin-ext/lib/hosted/node/hosted-plugin");
41
+ const metadata_scanner_1 = require("@theia/plugin-ext/lib/hosted/node/metadata-scanner");
42
+ const hosted_plugin_process_1 = require("@theia/plugin-ext/lib/hosted/node/hosted-plugin-process");
43
+ const DEFAULT_HOSTED_PLUGIN_PORT = 3030;
44
+ exports.HostedInstanceManager = Symbol('HostedInstanceManager');
45
+ const HOSTED_INSTANCE_START_TIMEOUT_MS = 30000;
46
+ const THEIA_INSTANCE_REGEX = /.*Theia app listening on (.*).*\./;
47
+ const PROCESS_OPTIONS = {
48
+ cwd: process.cwd(),
49
+ env: Object.assign({}, process.env)
50
+ };
51
+ let AbstractHostedInstanceManager = class AbstractHostedInstanceManager {
52
+ constructor() {
53
+ this.isPluginRunning = false;
54
+ }
55
+ isRunning() {
56
+ return this.isPluginRunning;
57
+ }
58
+ async run(pluginUri, port) {
59
+ return this.doRun(pluginUri, port);
60
+ }
61
+ async debug(pluginUri, debugConfig) {
62
+ return this.doRun(pluginUri, undefined, debugConfig);
63
+ }
64
+ async doRun(pluginUri, port, debugConfig) {
65
+ if (this.isPluginRunning) {
66
+ this.hostedPluginSupport.sendLog({ data: 'Hosted plugin instance is already running.', type: types_1.LogType.Info });
67
+ throw new Error('Hosted instance is already running.');
68
+ }
69
+ let command;
70
+ let processOptions;
71
+ if (pluginUri.scheme === 'file') {
72
+ processOptions = Object.assign({}, PROCESS_OPTIONS);
73
+ // get filesystem path that work cross operating systems
74
+ processOptions.env.HOSTED_PLUGIN = file_uri_1.FileUri.fsPath(pluginUri.toString());
75
+ // Disable all the other plugins on this instance
76
+ processOptions.env.THEIA_PLUGINS = '';
77
+ command = await this.getStartCommand(port, debugConfig);
78
+ }
79
+ else {
80
+ throw new Error('Not supported plugin location: ' + pluginUri.toString());
81
+ }
82
+ this.instanceUri = await this.postProcessInstanceUri(await this.runHostedPluginTheiaInstance(command, processOptions));
83
+ this.pluginUri = pluginUri;
84
+ // disable redirect to grab the release
85
+ this.instanceOptions = {
86
+ followRedirect: false
87
+ };
88
+ this.instanceOptions = await this.postProcessInstanceOptions(this.instanceOptions);
89
+ await this.checkInstanceUriReady();
90
+ return this.instanceUri;
91
+ }
92
+ terminate() {
93
+ if (this.isPluginRunning) {
94
+ this.hostedPluginProcess.killProcessTree(this.hostedInstanceProcess.pid);
95
+ this.hostedPluginSupport.sendLog({ data: 'Hosted instance has been terminated', type: types_1.LogType.Info });
96
+ this.isPluginRunning = false;
97
+ }
98
+ else {
99
+ throw new Error('Hosted plugin instance is not running.');
100
+ }
101
+ }
102
+ getInstanceURI() {
103
+ if (this.isPluginRunning) {
104
+ return this.instanceUri;
105
+ }
106
+ throw new Error('Hosted plugin instance is not running.');
107
+ }
108
+ getPluginURI() {
109
+ if (this.isPluginRunning) {
110
+ return this.pluginUri;
111
+ }
112
+ throw new Error('Hosted plugin instance is not running.');
113
+ }
114
+ /**
115
+ * Checks that the `instanceUri` is responding before exiting method
116
+ */
117
+ async checkInstanceUriReady() {
118
+ return new Promise((resolve, reject) => this.pingLoop(60, resolve, reject));
119
+ }
120
+ /**
121
+ * 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
122
+ * @param remainingCount the number of occurrence to check
123
+ * @param resolve resolve function if ok
124
+ * @param reject reject function if error
125
+ */
126
+ async pingLoop(remainingCount, resolve, reject) {
127
+ const isOK = await this.ping();
128
+ if (isOK) {
129
+ resolve();
130
+ }
131
+ else {
132
+ if (remainingCount > 0) {
133
+ setTimeout(() => this.pingLoop(--remainingCount, resolve, reject), 1000);
134
+ }
135
+ else {
136
+ reject(new Error('Unable to ping the remote server'));
137
+ }
138
+ }
139
+ }
140
+ /**
141
+ * Ping the plugin URI (checking status of the head)
142
+ */
143
+ async ping() {
144
+ return new Promise((resolve, reject) => {
145
+ const url = this.instanceUri.toString();
146
+ request.head(url, this.instanceOptions).on('response', res => {
147
+ // Wait that the status is OK
148
+ resolve(res.statusCode === 200);
149
+ }).on('error', error => {
150
+ resolve(false);
151
+ });
152
+ });
153
+ }
154
+ isPluginValid(uri) {
155
+ const pckPath = path.join(file_uri_1.FileUri.fsPath(uri), 'package.json');
156
+ if (fs.existsSync(pckPath)) {
157
+ const pck = fs.readJSONSync(pckPath);
158
+ try {
159
+ this.metadata.getScanner(pck);
160
+ return true;
161
+ }
162
+ catch (e) {
163
+ console.error(e);
164
+ return false;
165
+ }
166
+ }
167
+ return false;
168
+ }
169
+ async getStartCommand(port, debugConfig) {
170
+ const processArguments = process.argv;
171
+ let command;
172
+ if (core_1.environment.electron.is()) {
173
+ command = ['yarn', 'theia', 'start'];
174
+ }
175
+ else {
176
+ command = processArguments.filter((arg, index, args) => {
177
+ // remove --port=X and --port X arguments if set
178
+ // remove --plugins arguments
179
+ if (arg.startsWith('--port') || args[index - 1] === '--port') {
180
+ return;
181
+ }
182
+ else {
183
+ return arg;
184
+ }
185
+ });
186
+ }
187
+ if (process.env.HOSTED_PLUGIN_HOSTNAME) {
188
+ command.push('--hostname=' + process.env.HOSTED_PLUGIN_HOSTNAME);
189
+ }
190
+ if (port) {
191
+ await this.validatePort(port);
192
+ command.push('--port=' + port);
193
+ }
194
+ if (debugConfig) {
195
+ command.push(`--hosted-plugin-${debugConfig.debugMode || 'inspect'}=0.0.0.0${debugConfig.debugPort ? ':' + debugConfig.debugPort : ''}`);
196
+ }
197
+ return command;
198
+ }
199
+ async postProcessInstanceUri(uri) {
200
+ return uri;
201
+ }
202
+ async postProcessInstanceOptions(options) {
203
+ return options;
204
+ }
205
+ runHostedPluginTheiaInstance(command, options) {
206
+ this.isPluginRunning = true;
207
+ return new Promise((resolve, reject) => {
208
+ let started = false;
209
+ const outputListener = (data) => {
210
+ const line = data.toString();
211
+ const match = THEIA_INSTANCE_REGEX.exec(line);
212
+ if (match) {
213
+ this.hostedInstanceProcess.stdout.removeListener('data', outputListener);
214
+ started = true;
215
+ resolve(new uri_1.default(match[1]));
216
+ }
217
+ };
218
+ if (core_1.isWindows) {
219
+ // Has to be set for running on windows (electron).
220
+ // See also: https://github.com/nodejs/node/issues/3675
221
+ options.shell = true;
222
+ }
223
+ this.hostedInstanceProcess = cp.spawn(command.shift(), command, options);
224
+ this.hostedInstanceProcess.on('error', () => { this.isPluginRunning = false; });
225
+ this.hostedInstanceProcess.on('exit', () => { this.isPluginRunning = false; });
226
+ this.hostedInstanceProcess.stdout.addListener('data', outputListener);
227
+ this.hostedInstanceProcess.stdout.addListener('data', data => {
228
+ this.hostedPluginSupport.sendLog({ data: data.toString(), type: types_1.LogType.Info });
229
+ });
230
+ this.hostedInstanceProcess.stderr.addListener('data', data => {
231
+ this.hostedPluginSupport.sendLog({ data: data.toString(), type: types_1.LogType.Error });
232
+ });
233
+ setTimeout(() => {
234
+ if (!started) {
235
+ this.terminate();
236
+ this.isPluginRunning = false;
237
+ reject(new Error('Timeout.'));
238
+ }
239
+ }, HOSTED_INSTANCE_START_TIMEOUT_MS);
240
+ });
241
+ }
242
+ async validatePort(port) {
243
+ if (port < 1 || port > 65535) {
244
+ throw new Error('Port value is incorrect.');
245
+ }
246
+ if (!await this.isPortFree(port)) {
247
+ throw new Error('Port ' + port + ' is already in use.');
248
+ }
249
+ }
250
+ isPortFree(port) {
251
+ return new Promise(resolve => {
252
+ const server = net.createServer();
253
+ server.listen(port, '0.0.0.0');
254
+ server.on('error', () => {
255
+ resolve(false);
256
+ });
257
+ server.on('listening', () => {
258
+ server.close();
259
+ resolve(true);
260
+ });
261
+ });
262
+ }
263
+ };
264
+ __decorate([
265
+ (0, inversify_1.inject)(hosted_plugin_1.HostedPluginSupport),
266
+ __metadata("design:type", hosted_plugin_1.HostedPluginSupport)
267
+ ], AbstractHostedInstanceManager.prototype, "hostedPluginSupport", void 0);
268
+ __decorate([
269
+ (0, inversify_1.inject)(metadata_scanner_1.MetadataScanner),
270
+ __metadata("design:type", metadata_scanner_1.MetadataScanner)
271
+ ], AbstractHostedInstanceManager.prototype, "metadata", void 0);
272
+ __decorate([
273
+ (0, inversify_1.inject)(hosted_plugin_process_1.HostedPluginProcess),
274
+ __metadata("design:type", hosted_plugin_process_1.HostedPluginProcess)
275
+ ], AbstractHostedInstanceManager.prototype, "hostedPluginProcess", void 0);
276
+ AbstractHostedInstanceManager = __decorate([
277
+ (0, inversify_1.injectable)()
278
+ ], AbstractHostedInstanceManager);
279
+ exports.AbstractHostedInstanceManager = AbstractHostedInstanceManager;
280
+ let NodeHostedPluginRunner = class NodeHostedPluginRunner extends AbstractHostedInstanceManager {
281
+ async postProcessInstanceUri(uri) {
282
+ for (const uriPostProcessor of this.uriPostProcessors.getContributions()) {
283
+ uri = await uriPostProcessor.processUri(uri);
284
+ }
285
+ return uri;
286
+ }
287
+ async postProcessInstanceOptions(options) {
288
+ for (const uriPostProcessor of this.uriPostProcessors.getContributions()) {
289
+ options = await uriPostProcessor.processOptions(options);
290
+ }
291
+ return options;
292
+ }
293
+ async getStartCommand(port, debugConfig) {
294
+ if (!port) {
295
+ port = process.env.HOSTED_PLUGIN_PORT ?
296
+ Number(process.env.HOSTED_PLUGIN_PORT) :
297
+ ((debugConfig === null || debugConfig === void 0 ? void 0 : debugConfig.debugPort) ? Number(debugConfig.debugPort) : DEFAULT_HOSTED_PLUGIN_PORT);
298
+ }
299
+ return super.getStartCommand(port, debugConfig);
300
+ }
301
+ };
302
+ __decorate([
303
+ (0, inversify_1.inject)(contribution_provider_1.ContributionProvider),
304
+ (0, inversify_1.named)(Symbol.for(hosted_plugin_uri_postprocessor_1.HostedPluginUriPostProcessorSymbolName)),
305
+ __metadata("design:type", Object)
306
+ ], NodeHostedPluginRunner.prototype, "uriPostProcessors", void 0);
307
+ NodeHostedPluginRunner = __decorate([
308
+ (0, inversify_1.injectable)()
309
+ ], NodeHostedPluginRunner);
310
+ exports.NodeHostedPluginRunner = NodeHostedPluginRunner;
311
+ let ElectronNodeHostedPluginRunner = class ElectronNodeHostedPluginRunner extends AbstractHostedInstanceManager {
312
+ };
313
+ ElectronNodeHostedPluginRunner = __decorate([
314
+ (0, inversify_1.injectable)()
315
+ ], ElectronNodeHostedPluginRunner);
316
+ exports.ElectronNodeHostedPluginRunner = ElectronNodeHostedPluginRunner;
317
317
  //# sourceMappingURL=hosted-instance-manager.js.map