massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-json / node_modules / vscode-languageserver / lib / node / main.js
1 "use strict";
2 /* --------------------------------------------------------------------------------------------
3  * Copyright (c) Microsoft Corporation. All rights reserved.
4  * Licensed under the MIT License. See License.txt in the project root for license information.
5  * ------------------------------------------------------------------------------------------ */
6 /// <reference path="../../typings/thenable.d.ts" />
7 var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
8     if (k2 === undefined) k2 = k;
9     Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
10 }) : (function(o, m, k, k2) {
11     if (k2 === undefined) k2 = k;
12     o[k2] = m[k];
13 }));
14 var __exportStar = (this && this.__exportStar) || function(m, exports) {
15     for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
16 };
17 Object.defineProperty(exports, "__esModule", { value: true });
18 exports.createConnection = exports.Files = void 0;
19 const Is = require("../common/utils/is");
20 const server_1 = require("../common/server");
21 const fm = require("./files");
22 const node_1 = require("vscode-languageserver-protocol/node");
23 __exportStar(require("vscode-languageserver-protocol/node"), exports);
24 __exportStar(require("../common/api"), exports);
25 var Files;
26 (function (Files) {
27     Files.uriToFilePath = fm.uriToFilePath;
28     Files.resolveGlobalNodePath = fm.resolveGlobalNodePath;
29     Files.resolveGlobalYarnPath = fm.resolveGlobalYarnPath;
30     Files.resolve = fm.resolve;
31     Files.resolveModulePath = fm.resolveModulePath;
32 })(Files = exports.Files || (exports.Files = {}));
33 let _protocolConnection;
34 function endProtocolConnection() {
35     if (_protocolConnection === undefined) {
36         return;
37     }
38     try {
39         _protocolConnection.end();
40     }
41     catch (_err) {
42         // Ignore. The client process could have already
43         // did and we can't send an end into the connection.
44     }
45 }
46 let _shutdownReceived = false;
47 let exitTimer = undefined;
48 function setupExitTimer() {
49     const argName = '--clientProcessId';
50     function runTimer(value) {
51         try {
52             let processId = parseInt(value);
53             if (!isNaN(processId)) {
54                 exitTimer = setInterval(() => {
55                     try {
56                         process.kill(processId, 0);
57                     }
58                     catch (ex) {
59                         // Parent process doesn't exist anymore. Exit the server.
60                         endProtocolConnection();
61                         process.exit(_shutdownReceived ? 0 : 1);
62                     }
63                 }, 3000);
64             }
65         }
66         catch (e) {
67             // Ignore errors;
68         }
69     }
70     for (let i = 2; i < process.argv.length; i++) {
71         let arg = process.argv[i];
72         if (arg === argName && i + 1 < process.argv.length) {
73             runTimer(process.argv[i + 1]);
74             return;
75         }
76         else {
77             let args = arg.split('=');
78             if (args[0] === argName) {
79                 runTimer(args[1]);
80             }
81         }
82     }
83 }
84 setupExitTimer();
85 const watchDog = {
86     initialize: (params) => {
87         const processId = params.processId;
88         if (Is.number(processId) && exitTimer === undefined) {
89             // We received a parent process id. Set up a timer to periodically check
90             // if the parent is still alive.
91             setInterval(() => {
92                 try {
93                     process.kill(processId, 0);
94                 }
95                 catch (ex) {
96                     // Parent process doesn't exist anymore. Exit the server.
97                     process.exit(_shutdownReceived ? 0 : 1);
98                 }
99             }, 3000);
100         }
101     },
102     get shutdownReceived() {
103         return _shutdownReceived;
104     },
105     set shutdownReceived(value) {
106         _shutdownReceived = value;
107     },
108     exit: (code) => {
109         endProtocolConnection();
110         process.exit(code);
111     }
112 };
113 function createConnection(arg1, arg2, arg3, arg4) {
114     let factories;
115     let input;
116     let output;
117     let options;
118     if (arg1 !== void 0 && arg1.__brand === 'features') {
119         factories = arg1;
120         arg1 = arg2;
121         arg2 = arg3;
122         arg3 = arg4;
123     }
124     if (node_1.ConnectionStrategy.is(arg1) || node_1.ConnectionOptions.is(arg1)) {
125         options = arg1;
126     }
127     else {
128         input = arg1;
129         output = arg2;
130         options = arg3;
131     }
132     return _createConnection(input, output, options, factories);
133 }
134 exports.createConnection = createConnection;
135 function _createConnection(input, output, options, factories) {
136     if (!input && !output && process.argv.length > 2) {
137         let port = void 0;
138         let pipeName = void 0;
139         let argv = process.argv.slice(2);
140         for (let i = 0; i < argv.length; i++) {
141             let arg = argv[i];
142             if (arg === '--node-ipc') {
143                 input = new node_1.IPCMessageReader(process);
144                 output = new node_1.IPCMessageWriter(process);
145                 break;
146             }
147             else if (arg === '--stdio') {
148                 input = process.stdin;
149                 output = process.stdout;
150                 break;
151             }
152             else if (arg === '--socket') {
153                 port = parseInt(argv[i + 1]);
154                 break;
155             }
156             else if (arg === '--pipe') {
157                 pipeName = argv[i + 1];
158                 break;
159             }
160             else {
161                 var args = arg.split('=');
162                 if (args[0] === '--socket') {
163                     port = parseInt(args[1]);
164                     break;
165                 }
166                 else if (args[0] === '--pipe') {
167                     pipeName = args[1];
168                     break;
169                 }
170             }
171         }
172         if (port) {
173             let transport = node_1.createServerSocketTransport(port);
174             input = transport[0];
175             output = transport[1];
176         }
177         else if (pipeName) {
178             let transport = node_1.createServerPipeTransport(pipeName);
179             input = transport[0];
180             output = transport[1];
181         }
182     }
183     var commandLineMessage = 'Use arguments of createConnection or set command line parameters: \'--node-ipc\', \'--stdio\' or \'--socket={number}\'';
184     if (!input) {
185         throw new Error('Connection input stream is not set. ' + commandLineMessage);
186     }
187     if (!output) {
188         throw new Error('Connection output stream is not set. ' + commandLineMessage);
189     }
190     // Backwards compatibility
191     if (Is.func(input.read) && Is.func(input.on)) {
192         let inputStream = input;
193         inputStream.on('end', () => {
194             endProtocolConnection();
195             process.exit(_shutdownReceived ? 0 : 1);
196         });
197         inputStream.on('close', () => {
198             endProtocolConnection();
199             process.exit(_shutdownReceived ? 0 : 1);
200         });
201     }
202     const connectionFactory = (logger) => {
203         const result = node_1.createProtocolConnection(input, output, logger, options);
204         return result;
205     };
206     return server_1.createConnection(connectionFactory, watchDog, factories);
207 }
208 //# sourceMappingURL=main.js.map