UNPKG

5.07 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox 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// *****************************************************************************
17var __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};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26Object.defineProperty(exports, "__esModule", { value: true });
27exports.IPCConnectionProvider = void 0;
28const cp = require("child_process");
29const inversify_1 = require("inversify");
30const path = require("path");
31const readline_1 = require("readline");
32const common_1 = require("../../common");
33const ipc_channel_1 = require("./ipc-channel");
34const ipc_protocol_1 = require("./ipc-protocol");
35let IPCConnectionProvider = class IPCConnectionProvider {
36 listen(options, acceptor) {
37 return this.doListen(Object.assign({ logger: this.logger, args: [] }, options), acceptor);
38 }
39 doListen(options, acceptor) {
40 const childProcess = this.fork(options);
41 const channel = new ipc_channel_1.IPCChannel(childProcess);
42 const toStop = new common_1.DisposableCollection();
43 const toCancelStop = toStop.push(common_1.Disposable.create(() => childProcess.kill()));
44 const errorHandler = options.errorHandler;
45 if (errorHandler) {
46 let errorCount = 0;
47 channel.onError((err) => {
48 errorCount++;
49 if (errorHandler.shouldStop(err, errorCount)) {
50 toStop.dispose();
51 }
52 });
53 channel.onClose(() => {
54 if (toStop.disposed) {
55 return;
56 }
57 if (errorHandler.shouldRestart()) {
58 toCancelStop.dispose();
59 toStop.push(this.doListen(options, acceptor));
60 }
61 });
62 }
63 acceptor(channel);
64 return toStop;
65 }
66 fork(options) {
67 const forkOptions = {
68 env: (0, ipc_protocol_1.createIpcEnv)(options),
69 execArgv: [],
70 // 5th element MUST be 'overlapped' for it to work properly on Windows.
71 // 'overlapped' works just like 'pipe' on non-Windows platforms.
72 // See: https://nodejs.org/docs/latest-v14.x/api/child_process.html#child_process_options_stdio
73 // Note: For some reason `@types/node` does not know about 'overlapped'.
74 stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'overlapped']
75 };
76 const inspectArgPrefix = `--${options.serverName}-inspect`;
77 const inspectArg = process.argv.find(v => v.startsWith(inspectArgPrefix));
78 if (inspectArg !== undefined) {
79 forkOptions.execArgv = ['--nolazy', `--inspect${inspectArg.substr(inspectArgPrefix.length)}`];
80 }
81 const childProcess = cp.fork(path.join(__dirname, 'ipc-bootstrap'), options.args, forkOptions);
82 (0, readline_1.createInterface)(childProcess.stdout).on('line', line => this.logger.info(`[${options.serverName}: ${childProcess.pid}] ${line}`));
83 (0, readline_1.createInterface)(childProcess.stderr).on('line', line => this.logger.error(`[${options.serverName}: ${childProcess.pid}] ${line}`));
84 this.logger.debug(`[${options.serverName}: ${childProcess.pid}] IPC started`);
85 childProcess.once('exit', () => this.logger.debug(`[${options.serverName}: ${childProcess.pid}] IPC exited`));
86 return childProcess;
87 }
88};
89__decorate([
90 (0, inversify_1.inject)(common_1.ILogger),
91 __metadata("design:type", Object)
92], IPCConnectionProvider.prototype, "logger", void 0);
93IPCConnectionProvider = __decorate([
94 (0, inversify_1.injectable)()
95], IPCConnectionProvider);
96exports.IPCConnectionProvider = IPCConnectionProvider;
97//# sourceMappingURL=ipc-connection-provider.js.map
\No newline at end of file