forked from bubenshchykov/ngrok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.js
121 lines (102 loc) · 2.72 KB
/
process.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
const { spawn } = require('child_process');
const platform = require('os').platform();
const defaultDir = __dirname + '/bin';
const bin = './ngrok' + (platform === 'win32' ? '.exe' : '');
const ready = /starting web service.*addr=(\d+\.\d+\.\d+\.\d+:\d+)/;
const inUse = /address already in use/;
let processPromise, activeProcess;
/*
ngrok process runs internal ngrok api
and should be spawned only ONCE
(respawn allowed if it fails or .kill method called)
*/
async function getProcess(opts) {
if (processPromise) return processPromise;
try {
processPromise = startProcess(opts);
return await processPromise;
}
catch(ex) {
processPromise = null;
throw ex;
}
}
async function startProcess (opts) {
let dir = defaultDir;
const start = ['start', '--none', '--log=stdout'];
if (opts.region) start.push('--region=' + opts.region);
if (opts.configPath) start.push('--config=' + opts.configPath);
if (opts.binPath) dir = opts.binPath(dir);
const ngrok = spawn(bin, start, {cwd: dir});
let resolve, reject;
const apiUrl = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
ngrok.stdout.on('data', data => {
const msg = data.toString();
const addr = msg.match(ready);
if (addr) {
resolve(`http://${addr[1]}`);
} else if (msg.match(inUse)) {
reject(new Error(msg.substring(0, 10000)));
}
});
ngrok.stderr.on('data', data => {
const msg = data.toString().substring(0, 10000);
reject(new Error(msg));
});
ngrok.on('exit', () => {
processPromise = null;
activeProcess = null;
});
process.on('exit', async () => await killProcess());
try {
const url = await apiUrl;
activeProcess = ngrok;
return url;
}
catch(ex) {
ngrok.kill();
throw ex;
}
finally {
ngrok.stdout.removeAllListeners('data');
ngrok.stderr.removeAllListeners('data');
}
}
function killProcess () {
if (!activeProcess) return;
return new Promise(resolve => {
activeProcess.on('exit', () => resolve());
activeProcess.kill();
});
}
/**
* @param {string | INgrokOptions} optsOrToken
*/
async function setAuthtoken (optsOrToken) {
const isOpts = typeof optsOrToken !== 'string'
const opts = isOpts ? optsOrToken : {}
const token = isOpts ? opts.authtoken : optsOrToken
const authtoken = ['authtoken', token];
if (opts.configPath) authtoken.push('--config=' + opts.configPath);
let dir = defaultDir;
if (opts.binPath) dir = opts.binPath(dir)
const ngrok = spawn(bin, authtoken, {cwd: dir});
const killed = new Promise((resolve, reject) => {
ngrok.stdout.once('data', () => resolve());
ngrok.stderr.once('data', () => reject(new Error('cant set authtoken')));
});
try {
return await killed;
}
finally {
ngrok.kill();
}
}
module.exports = {
getProcess,
killProcess,
setAuthtoken
};