This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
forked from audaxis/c9.ide.run.debug.ikpdb
-
Notifications
You must be signed in to change notification settings - Fork 3
/
netproxy.js
131 lines (104 loc) · 3.18 KB
/
netproxy.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
/*
* Copyright (C) 2015, Ajax.org B.V.
* This software is available under the Cloud9 SDK License, available from
* https://github.com/c9/core/blob/master/LICENSE.
*/
var net = require("net");
var debuggedProcessHost = "{DEBUGGED_PROCESS_HOST}";
var debuggedProcessPort = parseInt("{DEBUGGED_PROCESS_PORT}", 10);
var debugBuffer = [];
var browserBuffer = [];
var browserClient,
debugClient;
var MAX_RETRIES = 100;
var RETRY_INTERVAL = 300;
var log = console.log;
console.warn = console.log = function() {
return console.error.apply(console, arguments);
};
function send() {
log.apply(console, arguments);
}
var server = net.createServer(function(client) {
if (browserClient)
browserClient.destroy(); // Client is probably unloaded because a new client is connecting
browserClient = client;
debugBuffer = [];
browserClient.on("end", function() {
browserClient = null;
});
browserClient.on("data", function(data) {
console.error("data = " + data);
if (debugClient) {
debugClient.write(data);
} else {
debugBuffer.push(data);
}
});
if (browserBuffer.length) {
browserBuffer.forEach(function(data) {
browserClient.write(data);
});
browserBuffer = [];
}
});
// Start listening for browser clients
server.listen(debuggedProcessPort + 1, "127.0.0.1", function() {
//console.log("netproxy listening on port " + ( debuggedProcessPort + 1) );
start();
});
// Handle errors
server.on("error", function() { process.exit(0); });
function tryConnect(retries, callback) {
if (!retries)
return callback(new Error("Cannot connect to port " + debuggedProcessPort));
var connection = net.connect(debuggedProcessPort, debuggedProcessHost);
connection.on("connect", function() {
//console.log("netproxy connected to debugger");
connection.removeListener("error", onError);
callback(null, connection);
});
connection.addListener("error", onError);
function onError(e) {
if (e.code !== "ECONNREFUSED")
return callback(e);
setTimeout(function() {
tryConnect(retries - 1, callback);
}, RETRY_INTERVAL);
}
}
tryConnect(MAX_RETRIES, function(err, connection) {
if (err)
return errHandler(err);
debugClient = connection;
browserBuffer = [];
debugClient.on("data", function(data) {
if (browserClient) {
browserClient.write(data);
} else {
browserBuffer.push(data);
}
});
function errHandler(e) {
console.log(e);
process.exit(0);
}
debugClient.on("error", errHandler);
debugClient.on("end", function(data) {
server.close();
});
start();
if (debugBuffer.length) {
console.error("sending buffer to debugger...");
console.error("debugBuffer = " + debugBuffer);
debugBuffer.forEach(function(data) {
debugClient.write(data);
});
debugBuffer = [];
}
});
var I = 0;
function start() {
if (++I == 2)
send("ß");
}