-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
140 lines (124 loc) · 3.97 KB
/
extension.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var vscode = require('vscode');
var outputChannel = vscode.window.createOutputChannel("Fuse");
outputChannel.show();
// Spawn daemon client
var spawn = require('child_process');
var fuseClient = spawn.spawn("fuse", ['daemon-client', 'Simple Client']);
var buffer = new Buffer(0);
fuseClient.stdout.on('data', function (data) {
// Data is a stream and must be parsed as that
var latestBuf = Buffer.concat([buffer, data]);
buffer = parseMsgFromBuffer(latestBuf, function (message) {
var json = JSON.parse(message);
var data = json.Data;
switch(json.Name)
{
case "Fuse.BuildStarted":
outputChannel.clear();
outputChannel.appendLine("Build started...");
break;
case "Fuse.BuildIssueDetected":
if (data.StartPosition != null)
{
outputChannel.appendLine(data.IssueType + " in file '" + data.Path + "' (" + (data.StartPosition.Line+1)+":"+data.StartPosition.Character+"):");
outputChannel.appendLine(data.Message);
}
else
{
outputChannel.appendLine(data.Message);
}
break;
case "Fuse.BuildEnded":
outputChannel.appendLine(data.Status);
outputChannel.appendLine("");
break;
case "Fuse.LogEvent":
outputChannel.appendLine("LOG: ("+data.ClientName + "): " + data.Message);
break;
case "Fuse.ExceptionEvent":
outputChannel.appendLine("EXCEPTION: ("+data.ClientName + "): " +data.Type + ": " + data.Message);
outputChannel.appendLine("StackTrace:");
outputChannel.appendLine(data.StackTrace);
break;
}
});
});
fuseClient.stderr.on('data', function (data) {
//console.log(data.toString('utf-8'));
});
fuseClient.on('close', function (code) {
outputChannel.append("Daemon client closed with code " + code);
});
var subRequest = JSON.stringify({
Name: "Subscribe",
Id: 0, // Arbitrary identifier to map responses back to requests
Arguments: {
Filter: "Fuse.*",
Replay: false, // Use replay to receive messages sent before you connected
SubscriptionId: 10 // Arbitrary ID to map events to a particular subscription
}
});
send(fuseClient, "Request", subRequest);
function parseMsgFromBuffer(buffer, msgCallback) {
var start = 0;
while (start < buffer.length) {
var endOfMsgType = buffer.indexOf('\n', start);
if (endOfMsgType < 0)
break; // Incomplete or corrupt data
var startOfLength = endOfMsgType + 1;
var endOfLength = buffer.indexOf('\n', startOfLength);
if (endOfLength < 0)
break; // Incomplete or corrupt data
//var msgType = buffer.toString('utf-8', start, endOfMsgType);
var length = parseInt(buffer.toString('utf-8', startOfLength, endOfLength));
if (isNaN(length)) {
console.log('fuse: Corrupt length in data received from Fuse.');
// Try to recover by starting from the beginning
start = endOfLength + 1;
continue;
}
var startOfData = endOfLength + 1;
var endOfData = startOfData + length;
if (buffer.length < endOfData)
break; // Incomplete data
var jsonData = buffer.toString('utf-8', startOfData, endOfData);
msgCallback(jsonData);
start = endOfData;
}
return buffer.slice(start, buffer.length);
}
function send(fuseClient, msgType, serializedMsg) {
// Pack the message to be compatible with Fuse Protocol.
// As:
// '''
// MessageType (msgType)
// Length (length)
// JSON(serializedMsg)
// '''
// For example:
// '''
// Event
// 50
// {
// "Name": "Test",
// "Data":
// {
// "Foo": "Bar"
// }
// }
// '''
var length = Buffer.byteLength(serializedMsg, 'utf-8');
var packedMsg = msgType + '\n' + length + '\n' + serializedMsg;
try {
fuseClient.stdin.write(packedMsg);
}
catch (e) {
console.log(e);
}
}
function activate(context) {
}
exports.activate = activate;
function deactivate() {
}
exports.deactivate = deactivate;