-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (41 loc) · 1.28 KB
/
index.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
(function() {
'use strict';
const fs = require('fs');
const http = require('http');
const repl = require('repl');
const express = require('express');
const websocketStream = require('websocket-stream');
function run(options) {
options = options || {};
const app = express();
const server = http.createServer(app);
websocketStream.createServer(Object.assign({ server, path: '/' }, options.ws || {}), (stream) => {
// Tell a lie to readable: let it treat the stream as TTY.
stream.isTTY = true;
stream.isRaw = true;
stream.setRawMode = (mode) => {
stream.isRaw = mode;
};
const instance = repl.start(Object.assign({ input: stream, output: stream }, options.repl || {}));
let closed = false;
stream.on('close', () => {
if(closed) return;
closed = true;
instance.close();
});
instance.on('close', () => {
if(closed) return;
closed = true;
stream.end();
});
});
app.use(express.static('static'));
server.listen(options.port || 9999);
};
if(require.main === module) {
const configPath = './config.json';
run(fs.existsSync(configPath) && JSON.parse(fs.readFileSync(configPath, 'utf8')));
} else {
module.exports = run;
}
})();