This repository has been archived by the owner on Jun 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 230
/
server.js
executable file
·168 lines (145 loc) · 4.67 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
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
#!/usr/bin/env node
var fs = require('fs'),
http = require('http'),
path = require('path'),
url = require('url'),
spawn = require('child_process').spawn,
queryString = require('querystring'),
port = process.argv[2] || 8888;
var CONTENT_TYPES = {
'js': 'application/javascript; charset=utf-8',
'css': 'text/css; charset=utf-8',
'json': 'application/json; charset=utf-8',
'html': 'text/html; charset=utf-8',
'htm': 'text/html; charset=utf-8',
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'png': 'image/png',
'ico': 'image/x-icon',
'gif': 'image/gif',
'txt': 'text/plain; charset=utf-8'
};
function textResponse(res, code, txt) {
txt = txt || '';
res.writeHead(code, {"Content-Type": "text/plain"});
res.end(txt);
}
function genRandomString() {
return Math.random().toString(36).replace(/[^a-z]+/g, '').substring(0,5);
}
function restoreFakeSession(pieceName, res) {
fs.readFile('index.html', String, function (err, file) {
if (err) {
textResponse(res, 500, err + '\n');
return;
}
var savedData = inMemorySaved[pieceName]
var sessionReturnData = {session_id: pieceName,
revision_id: "1",
eval_history: JSON.parse(savedData.eval_history),
editor_text: savedData.editor_text,
language: savedData.language,
console_dump: savedData.console_dump };
file = String(file).replace('SESSION_PLACEHOLDER', 'REPLIT_DATA='+JSON.stringify(sessionReturnData));
res.writeHead(200, {'Content-Type': CONTENT_TYPES['html'], 'max-age': '0'});
res.write(file);
res.end();
});
}
var waiting = {};
var inMemorySaved = {};
var httpCb = function (req, res) {
var uri = url.parse(req.url).pathname;
var uriFirstPiece = uri.split('/')[1]
if (uriFirstPiece in {
languages: 1
, help: 1
, about: 1
, examples: 1
, workspace: 1
}) { uri = '/index.html'; }
if(inMemorySaved[uriFirstPiece]) {
restoreFakeSession(uriFirstPiece, res);
}
var filename = path.join(process.cwd(), uri);;
var m;
if (m = uri.match(/(?:\/jsrepl)?\/emscripten\/input\/(\d+)/)) {
if (req.method === 'GET') {
waiting[m[1]] = {
req: req,
res: res
};
req.on('close', function () {
delete waiting[m[1]];
});
} else {
var body = [];
req.on('data', function (data) {
body.push(data);
});
req.on('end', function () {
var d = queryString.parse(body.join('')),
waiterRes = waiting[m[1]] && waiting[m[1]].res;
if (waiterRes) {
waiterRes.writeHead(200, {'Content-Type': 'text/plain'});
waiterRes.end(d.input);
res.writeHead(200, {'Content-Type': 'text/plain'});
delete waiting[m[1]];
res.end('success');
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('fail');
}
});
}
return;
}
if(m = uri.match(/save/)) {
var thisRandom = genRandomString();
var dataParts = [];
req.on('data', function(data){
dataParts.push(data);
});
req.on('end', function(){
inMemorySaved[thisRandom] = queryString.parse(dataParts.join(''));
res.writeHead(200, {'Content-Type': CONTENT_TYPES.json, 'max-age': '0'});
var responseString = JSON.stringify({ session_id: String(thisRandom),
// not attempting to support multiple revisions
revision_id: '1'
});
res.end(responseString);
});
return;
};
fs.exists(filename, function (exists) {
if (!exists) {
textResponse(res, 404, "Page Not Found!\n");
return;
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, 'binary', function (err, file) {
if (err) {
textResponse(res, 500, err + '\n');
return;
}
var ext = path.extname(filename).slice(1);
res.writeHead(200, {'Content-Type': CONTENT_TYPES[ext] || 'text/plain', 'max-age': '0'});
res.write(file, 'binary');
res.end();
});
});
};
var watcher = spawn('cake', ['watch']);
watcher.stdout.pipe(process.stdout);
watcher.stderr.pipe(process.stderr);
var server_started = false,
start_timer;
watcher.stdout.on('data', function () {
if (server_started) return;
clearTimeout(start_timer);
start_timer = setTimeout(function () {
http.createServer(httpCb).listen(port, '0.0.0.0');
console.log('repl.it server started at http://localhost:' + port + '/');
server_started = true;
}, 3000);
});