-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsocket_bridge2.js
318 lines (262 loc) · 7.82 KB
/
socket_bridge2.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* The MIT License (MIT)
* Copyright (c) 2012 Dennis Kehrig. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, forin: true, maxerr: 50, regexp: true */
/*global require, module, exports, setTimeout, Buffer */
/**
* Node script that creates a WebSocket to V8's native sockets
*/
(function () {
'use strict';
var net = require('net');
var io = require('node-inspector/node_modules/socket.io');
// There's just no reason to write this ourselves
var DataParser = require('node-inspector/lib/protocol.js');
// Function to summarize function parameters
var summarize = require('./summarize');
// Configuration
var _bridgePort = 3858;
var _debuggerPort = 5858;
var _reconnectInterval = 1000;
// State
var _nextCallId = 0;
var _reconnecting = false;
// Web Socket
var _bridge;
// Client (Brackets)
var _editor;
// V8 debugger
var _debugger;
var _dataParser;
// Called at the end of the script
function init() {
//_startBridge(_bridgePort);
_dataParser = new DataParser();
_dataParser.on('message', function (message) {
_onDebuggerMessage(message);
});
console.log("Connecting...");
_debugger = net.connect(_debuggerPort);
_debugger.setNoDelay(true);
var didConnect = false;
_debugger.on("connect", function () {
didConnect = true;
console.log("Connected!");
});
_debugger.on("close", function (had_error) {
var message = "Disconnected!";
if (had_error) {
message += " (had error)";
}
console.log(message);
});
var isHello = true;
_debugger.on("data", function (data) {
console.log("Received data");
_dataParser.append(data);
if (isHello) {
isHello = false;
console.log("Debugger says hi");
_onDebuggerConnect2();
}
});
_debugger.on("error", function (error) {
if (! didConnect) {
console.log("Could not connect");
} else {
console.log(error);
}
});
}
var _seq = 0;
function _serializeMessage(message) {
message.seq = _seq++;
message.type = 'request';
var body = JSON.stringify(message);
return 'Content-Length: ' + Buffer.byteLength(body) + "\r\n\r\n" + body;
}
function _sendDebugger(message) {
console.log("Sending command " + message.command);
var string = _serializeMessage(message);
_debugger.write(string);
}
function _onDebuggerConnect2() {
var message;
// message = {
// "seq" : 0,
// "type" : "request",
// "command" : "scripts",
// "arguments" : {
// "types" : 4,
// //"ids" : []
// "includeSource" : false
// //"filter" : ...
// }
// };
// _debugger.write(JSON.stringify(message));
setTimeout(function () {
_sendDebugger({ "command": "evaluate", "arguments": { "expression": "(function() { return 1+2; })();" }});
}, 1000);
setTimeout(function () {
_sendDebugger({ "command": "continue" });
}, 2000);
setTimeout(function () {
_sendDebugger({ "command" : "disconnect" });
console.log("Disconnecting...");
_debugger.destroy();
}, 3000);
}
function _printIndented(string) {
string = string.split("\n").map(function (line) { return "\t" + line; }).join("\n");
console.log(string);
}
function _onDebuggerMessage(message) {
console.log("Received message!");
console.log(message);
}
// Start a socket.io server
function _startBridge(port) {
_bridge = io.listen(port);
_bridge.set('log level', 1);
_bridge.sockets.on('connection', _onEditorConnect);
}
// Called when somebody connects to the bridge socket
function _onEditorConnect(editor) {
console.log("Editor connected");
// Called when a potential old editor was disconnected (i.e. multiple instances of Brackets are not supported)
var next = function () {
_setupEditor(editor);
_connectDebugger();
};
if (_editor) {
// Cleanup
console.log("Disconnecting previous editor");
_editor.on("disconnect", next);
_editor.disconnect();
} else {
next();
}
}
function _setupEditor(editor) {
_editor = editor;
_editor.on("call", _onEditorCall);
_editor.on("disconnect", _onEditorDisconnect);
}
function _onEditorDisconnect() {
console.log("Editor disconnected");
_editor = null;
_reconnecting = false;
_disconnectDebugger();
}
function _onEditorCall(messageName, messageArgs, callbackId) {
if (! _debugger) {
return;
}
var callId = _nextCallId++;
var prefix = "[" + callId + "] ";
var call = "_debugger." + messageName + "(" + messageArgs.map(summarize).join(", ") + ")";
console.log(prefix + "Calling " + call + ", replying to " + callbackId);
messageArgs.push(function () {
var args = Array.prototype.slice.call(arguments);
console.log(prefix + "Got back " + args.map(summarize).join(", "));
if (_editor && callbackId !== null && typeof callbackId !== 'undefined') {
console.log(prefix + "Notifying callback " + callbackId);
_editor.emit('callback', callbackId, args);
}
});
_debugger[messageName].apply(_debugger, messageArgs);
}
function _disconnectEditor() {
if (_editor) {
console.log("Disconnecting from editor");
_editor.disconnect();
}
}
function _connectDebugger() {
if (_debugger) {
return;
}
_debugger = new Client();
var didConnect = false;
_debugger.on("connect", function () {
console.log("Connected to debugger");
didConnect = true;
_onDebuggerConnect();
});
_debugger.on("close", function () {
console.log("Disconnected from debugger");
_debugger = null;
_onDebuggerDisconnect();
});
_debugger.on("error", function (error) {
console.log("Debugger sent error");
if (didConnect) {
_onDebuggerError(error);
}
});
console.log("Connecting to debugger on port 5858");
_debugger.connect(5858);
}
function _reconnectDebugger() {
// Turned off when the debugger connects or when the editor disconnects
_reconnecting = true;
var reconnect = function () {
console.log("Reconnecting to the debugger");
_connectDebugger();
setTimeout(function () {
if (! _reconnecting) {
return;
}
reconnect();
}, _reconnectInterval);
};
setTimeout(reconnect, _reconnectInterval);
}
function _disconnectDebugger() {
if (_debugger) {
console.log("Disconnecting from debugger");
_debugger.disconnect();
}
}
function _onDebuggerConnect() {
_reconnecting = false;
if (_editor) {
console.log("Telling editor we're connected to the debugger");
_editor.emit('bridgeConnected');
}
}
function _onDebuggerError(error) {
if (_editor) {
console.log("Telling editor about the error");
_editor.emit("error", error);
}
}
function _onDebuggerDisconnect() {
if (_editor && ! _reconnecting) {
console.log("Telling editor we're no longer connected to the debugger");
_editor.emit('bridgeDisconnected');
_reconnectDebugger();
}
}
init();
}());