-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathdebugAdapter.ts
71 lines (58 loc) · 2.16 KB
/
debugAdapter.ts
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
import fs = require('fs');
import path = require('path');
import net = require('net');
import utils = require('./utils');
import logging = require('./logging');
// NOTE: The purpose of this file is to serve as a bridge between
// VS Code's debug adapter client (which communicates via stdio) and
// PowerShell Editor Services' debug service (which communicates via
// named pipes or a network protocol). It is purely a naive data
// relay between the two transports.
var logBasePath = path.resolve(__dirname, "../logs");
utils.ensurePathExists(logBasePath);
var debugAdapterLogWriter =
fs.createWriteStream(
path.resolve(
logBasePath,
logging.getLogName("DebugAdapterClient")));
// Pause the stdin buffer until we're connected to the
// debug server
process.stdin.pause();
// Read the details of the current session to learn
// the connection details for the debug service
let sessionDetails = utils.readSessionFile();
// Establish connection before setting up the session
debugAdapterLogWriter.write("Connecting to port: " + sessionDetails.debugServicePort + "\r\n");
let debugServiceSocket = net.connect(sessionDetails.debugServicePort);
// Write any errors to the log file
debugServiceSocket.on(
'error',
(e) => debugAdapterLogWriter.write("Socket connect ERROR: " + e + "\r\n"));
// Route any output from the socket through stdout
debugServiceSocket.on(
'data',
(data: Buffer) => process.stdout.write(data));
// Wait for the connection to complete
debugServiceSocket.on(
'connect',
() => {
debugAdapterLogWriter.write("Connected to socket!\r\n\r\n");
// When data comes on stdin, route it through the socket
process.stdin.on(
'data',
(data: Buffer) => debugServiceSocket.write(data));
// Resume the stdin stream
process.stdin.resume();
});
// When the socket closes, end the session
debugServiceSocket.on(
'close',
() => {
debugAdapterLogWriter.write("Socket closed, shutting down.");
// Close after a short delay to give the client time
// to finish up
setTimeout(() => {
process.exit(0);
}, 1000);
}
)