-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
55 lines (47 loc) · 1.52 KB
/
cli.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
const { isApplication } = require("./src/types");
const ORIGINAL_CREATE_SERVER = Symbol("Original createServer");
const ORIGINAL_SERVER_LISTEN = Symbol("Original Server.prototype.listen");
function proxyHttp(http, inspector, options) {
const createServer = http[ORIGINAL_CREATE_SERVER] || http.createServer;
http[ORIGINAL_CREATE_SERVER] = createServer;
const listen = http[ORIGINAL_SERVER_LISTEN] || http.Server.prototype.listen;
http[ORIGINAL_SERVER_LISTEN] = listen;
const expressServers = new WeakMap();
http.createServer = new Proxy(createServer, {
apply(target, context, args) {
const [requestListener] = args;
const server = Reflect.apply(target, context, args);
if (requestListener && isApplication(requestListener)) {
expressServers.set(server, requestListener);
}
return server;
}
});
http.Server.prototype.listen = new Proxy(listen, {
apply(target, context, args) {
const server = context;
const app = expressServers.get(server);
if (app) {
inspector.inspect(app, options);
}
return Reflect.apply(target, context, args);
}
});
}
function run(path, inspector, options) {
inspector.trace();
require("express");
proxyHttp(require("http"), inspector, options);
proxyHttp(require("https"), inspector, options);
require(path);
}
function inspect(path, inspector, options) {
inspector.trace();
require("express");
const app = require(path);
inspector.inspect(app, options);
}
module.exports = {
run,
inspect
};