forked from remy/jsconsole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
84 lines (69 loc) · 2.64 KB
/
server.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
'use strict';
// pending connect exposing static.mime (not available in npm yet)
var mime = require('connect/node_modules/mime');
mime.define({ 'text/cache-manifest': ['appcache'] });
var connect = require('connect'),
parse = require('url').parse,
querystring = require('querystring').parse,
sessions = { run: {}, log: {} },
eventid = 0,
port = process.env.PORT || parseInt(process.argv[2]) || 80,
uuid = require('node-uuid');
function remoteServer(app) {
app.get('/remote/:id?', function (req, res) {
var url = parse(req.url),
query = querystring(url.query);
// save a new session id - maybe give it a token back?
// serve up some JavaScript
var id = req.params.id || uuid();
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.end((query.callback || 'callback') + '("' + id + '");');
});
app.get('/remote/:id/log', function (req, res) {
var id = req.params.id;
res.writeHead(200, {'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache'});
res.write('eventId:0\n\n');
sessions.log[id] = res;
sessions.log[id].xhr = req.headers['x-requested-with'] === 'XMLHttpRequest';
});
app.post('/remote/:id/log', function (req, res) {
// post made to send log to jsconsole
var id = req.params.id;
// passed over to Server Sent Events on jsconsole.com
if (sessions.log[id]) {
sessions.log[id].write('data: ' + req.body.data + '\neventId:' + (++eventid) + '\n\n');
if (sessions.log[id].xhr) {
sessions.log[id].end(); // lets older browsers finish their xhr request
}
}
res.writeHead(200, { 'Content-Type' : 'text/plain' });
res.end();
});
app.get('/remote/:id/run', function (req, res) {
var id = req.params.id;
res.writeHead(200, {'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache'});
res.write('eventId:0\n\n');
sessions.run[id] = res;
sessions.run[id].xhr = req.headers['x-requested-with'] === 'XMLHttpRequest';
});
app.post('/remote/:id/run', function (req, res) {
var id = req.params.id;
if (sessions.run[id]) {
sessions.run[id].write('data: ' + req.body.data + '\neventId:' + (++eventid) + '\n\n');
if (sessions.run[id].xhr) {
sessions.run[id].end(); // lets older browsers finish their xhr request
}
}
res.writeHead(200, { 'Content-Type' : 'text/plain' });
res.end();
});
}
// connect.static.mime.define('text/cache-manifest', ['appcache']);
var server = connect.createServer(
connect.bodyParser(),
connect.logger(),
connect.static(__dirname),
connect.router(remoteServer)
);
console.log('Listening on ' + port);
server.listen(port);