This repository has been archived by the owner on Nov 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathrepl.js
111 lines (100 loc) · 2.83 KB
/
repl.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
'use strict';
const fs = require('fs');
const path = require('path');
const net = require('net');
const repl = require('repl');
/**
* Contains the pathnames of all active REPL sockets.
* @type {Set<string>}
*/
const socketPathnames = new Set();
// Clean up REPL sockets and child processes on forced exit.
process.once('exit', code => {
socketPathnames.forEach(s => {
try {
fs.unlinkSync(s);
} catch (e) {}
});
if (code === 129 || code === 130) {
process.exitCode = 0;
}
});
if (!process.listeners('SIGHUP').length) {
process.once('SIGHUP', () => process.exit(128 + 1));
}
if (!process.listeners('SIGINT').length) {
process.once('SIGINT', () => process.exit(128 + 2));
}
/**
* Starts a REPL server, using a UNIX socket for IPC. The eval function
* parametre is passed in because there is no other way to access a file's
* non-global context.
*
* @param {string} filename
* @param {Function} evalFunction
*/
exports.start = function start(filename, evalFunction) {
if ('repl' in Config && !Config.repl) return;
// TODO: Windows does support the REPL when using named pipes. For now,
// this only supports UNIX sockets.
if (process.platform === 'win32') return;
if (filename === 'app') {
// Clean up old REPL sockets.
let directory = path.dirname(path.resolve(__dirname, Config.replsocketprefix || 'logs/repl', 'app'));
for (let file of fs.readdirSync(directory)) {
let pathname = path.resolve(directory, file);
let stat = fs.statSync(pathname);
if (!stat.isSocket()) continue;
let socket = net.connect(pathname, () => {
socket.end();
socket.destroy();
}).on('error', () => {
fs.unlink(pathname, () => {});
});
}
}
let server = net.createServer(socket => {
// @ts-ignore
repl.start({
input: socket,
output: socket,
/**
* @param {string} cmd
* @param {any} context
* @param {string} filename
* @param {Function} callback
* @return {any}
*/
eval(cmd, context, filename, callback) {
try {
return callback(null, evalFunction(cmd));
} catch (e) {
return callback(e);
}
},
}).on('exit', () => socket.end());
socket.on('error', () => socket.destroy());
});
let pathname = path.resolve(__dirname, Config.replsocketprefix || 'logs/repl', filename);
server.listen(pathname, () => {
fs.chmodSync(pathname, Config.replsocketmode || 0o600);
socketPathnames.add(pathname);
});
server.once('error', /** @param {NodeJS.ErrnoException} err */ err => {
if (err.code === "EADDRINUSE") {
fs.unlink(pathname, _err => {
if (_err && _err.code !== "ENOENT") {
require('./crashlogger')(_err, `REPL: ${filename}`);
}
server.close();
});
} else {
require('./crashlogger')(err, `REPL: ${filename}`);
server.close();
}
});
server.once('close', () => {
socketPathnames.delete(pathname);
start(filename, evalFunction);
});
};