-
Notifications
You must be signed in to change notification settings - Fork 15
/
ngrok-typescript.ts
69 lines (60 loc) · 1.79 KB
/
ngrok-typescript.ts
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
// Run with 'ts-node ngrok-typescript.ts'
import * as http from "http";
const httpServer = http.createServer(function (req, res) {
res.writeHead(200);
res.write("Hello");
res.end();
});
import * as ngrok from "@ngrok/ngrok";
ngrok.consoleLog();
const run = async (): Promise<void> => {
// await listenServer();
// await listenable();
// await listen();
await standardConfig();
};
async function listenServer() {
const listener = await ngrok.listen(httpServer);
if (typeof listener["url"] === "function") {
console.log("Ingress established at: ", listener["url"]());
}
}
async function listenable() {
const listener = await ngrok.listenable();
httpServer.listen(listener);
}
async function listen() {
const sessionBuilder = new ngrok.SessionBuilder().authtokenFromEnv();
const session = await sessionBuilder.connect();
const listener = await session.httpEndpoint().listen();
httpServer.listen(listener);
}
async function standardConfig() {
ngrok.loggingCallback(function (level, target, message) {
console.log(level, target, "-", message);
});
const sessionBuilder = new ngrok.SessionBuilder()
.authtokenFromEnv()
.handleStopCommand(() => {
console.log("stop command");
})
.handleRestartCommand(() => {
console.log("restart command");
})
.handleUpdateCommand((update) => {
console.log(
"update command, version: " +
update.version +
" permitMajorVersion: " +
update.permitMajorVersion
);
});
const session = await sessionBuilder.connect();
const listener = await session.httpEndpoint().listen();
console.log("Ingress established at:", listener.url());
httpServer.listen(8081);
listener.forward("localhost:8081");
// unregister logging callback
ngrok.loggingCallback();
}
run();