generated from GregBrimble/cf-workers-typescript-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
38 lines (33 loc) · 981 Bytes
/
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
require("cross-fetch/polyfill");
const { spawn } = require("child_process");
const cwd = process.cwd();
const SLEEP_DURATION = 2000;
const hostNameRegex = /userHostname=\"(.*)\"/g;
const startServer = (hostName) => {
process.env.CLOUDFLARED_TUNNEL = "true";
spawn("npm", ["run", "start:worker", "--", "--host", hostName], {
cwd,
stdio: "inherit",
});
};
const findTunnelHostname = async () => {
console.log("Waiting for tunnel to initiate...");
try {
const resp = await fetch("http://localhost:8081/metrics");
const data = await resp.text();
const matches = Array.from(data.matchAll(hostNameRegex));
const hostName = matches[0][1];
console.log(`Tunnel initiated: ${hostName}`);
startServer(hostName);
} catch {
setTimeout(findTunnelHostname, SLEEP_DURATION);
}
};
const startTunnel = () => {
console.log("Starting tunnel...");
spawn("npm", ["run", "start:tunnel"], {
cwd,
});
};
startTunnel();
findTunnelHostname();