buncargo 3.2.3 → 3.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/bin.js +10 -10
- package/dist/cli/index.js +4 -2
- package/dist/core/network.js +1 -1
- package/dist/core/quick-tunnel/cloudflared-process.d.ts +3 -0
- package/dist/core/quick-tunnel/constants.d.ts +2 -0
- package/dist/core/quick-tunnel/index.d.ts +4 -3
- package/dist/core/utils.js +1 -1
- package/dist/docker/index.js +2 -2
- package/dist/environment/index.js +5 -5
- package/dist/index-1fset27q.js +72 -0
- package/dist/index-2bcjw5n0.js +666 -0
- package/dist/index-39s6ez1q.js +250 -0
- package/dist/index-5vg657rh.js +72 -0
- package/dist/index-6att53sd.js +250 -0
- package/dist/index-94kgbw4m.js +72 -0
- package/dist/index-96q4yh56.js +72 -0
- package/dist/index-bgcx898h.js +451 -0
- package/dist/index-c28x1pjb.js +250 -0
- package/dist/index-c2v0t0y2.js +250 -0
- package/dist/index-cm05c27w.js +417 -0
- package/dist/index-emcawhxm.js +250 -0
- package/dist/index-fkgqg6w2.js +125 -0
- package/dist/index-gfjdt37q.js +391 -0
- package/dist/index-gfs10vb8.js +389 -0
- package/dist/index-pbwvaz4v.js +666 -0
- package/dist/index-pmbmwg3x.js +72 -0
- package/dist/index-pt8t9tkg.js +389 -0
- package/dist/index-qnpd5fn5.js +666 -0
- package/dist/index-qtprmjbm.js +399 -0
- package/dist/index-qz66apm2.js +250 -0
- package/dist/index-thsdxz7m.js +250 -0
- package/dist/index-twwcjn9p.js +228 -0
- package/dist/index-tyk17rfn.js +666 -0
- package/dist/index-vj8kaz2d.js +72 -0
- package/dist/index-vr4ygtyj.js +415 -0
- package/dist/index-wmgx8rsm.js +666 -0
- package/dist/index-ymdvr5sn.js +666 -0
- package/dist/index-yw46g4tr.js +666 -0
- package/dist/index-znaek8z2.js +72 -0
- package/dist/index.js +25 -25
- package/dist/loader/index.js +6 -6
- package/package.json +3 -3
- package/src/core/quick-tunnel/cloudflared-process.ts +73 -12
- package/src/core/quick-tunnel/constants.ts +14 -1
- package/src/core/quick-tunnel/index.ts +82 -40
- package/src/core/quick-tunnel/install.ts +1 -1
- package/src/core/tunnel.ts +25 -21
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
calculatePortOffset,
|
|
3
|
+
computePorts,
|
|
4
|
+
computeUrls
|
|
5
|
+
} from "./index-fb29934k.js";
|
|
6
|
+
|
|
7
|
+
// src/core/network.ts
|
|
8
|
+
import { networkInterfaces } from "node:os";
|
|
9
|
+
|
|
10
|
+
// src/core/utils.ts
|
|
11
|
+
function sleep(ms) {
|
|
12
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
13
|
+
}
|
|
14
|
+
function isCI() {
|
|
15
|
+
return process.env.CI === "true" || process.env.CI === "1" || process.env.GITHUB_ACTIONS === "true" || process.env.GITLAB_CI === "true" || process.env.CIRCLECI === "true" || process.env.JENKINS_URL !== undefined;
|
|
16
|
+
}
|
|
17
|
+
function logFrontendPort(port) {
|
|
18
|
+
console.log(`using_frontend_port:${port}`);
|
|
19
|
+
}
|
|
20
|
+
function getEnvVar(config, name, options = {}) {
|
|
21
|
+
const { log = true } = options;
|
|
22
|
+
const offset = calculatePortOffset();
|
|
23
|
+
const localIp = getLocalIp();
|
|
24
|
+
const ports = computePorts(config.services, config.apps, offset);
|
|
25
|
+
const urls = computeUrls(config.services, config.apps, ports, localIp);
|
|
26
|
+
const envVars = config.envVars?.(ports, urls, {
|
|
27
|
+
projectName: config.projectPrefix,
|
|
28
|
+
localIp,
|
|
29
|
+
portOffset: offset,
|
|
30
|
+
publicUrls: {}
|
|
31
|
+
});
|
|
32
|
+
const value = envVars?.[name];
|
|
33
|
+
if (log && name === "VITE_PORT" && typeof value === "number") {
|
|
34
|
+
logFrontendPort(value);
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
function logApiUrl(url) {
|
|
39
|
+
console.log(`using_api_url:${url}`);
|
|
40
|
+
}
|
|
41
|
+
function logExpoApiUrl(url) {
|
|
42
|
+
console.log(`using_expo_api_url:${url}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/core/network.ts
|
|
46
|
+
function getLocalIp() {
|
|
47
|
+
const interfaces = networkInterfaces();
|
|
48
|
+
for (const name of Object.keys(interfaces)) {
|
|
49
|
+
const nets = interfaces[name];
|
|
50
|
+
if (!nets)
|
|
51
|
+
continue;
|
|
52
|
+
for (const net of nets) {
|
|
53
|
+
if (net.family === "IPv4" && !net.internal) {
|
|
54
|
+
return net.address;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return "127.0.0.1";
|
|
59
|
+
}
|
|
60
|
+
async function waitForServer(url, options = {}) {
|
|
61
|
+
const { timeout = 30000, interval = 2000, verbose = false } = options;
|
|
62
|
+
const start = Date.now();
|
|
63
|
+
let attempts = 0;
|
|
64
|
+
while (Date.now() - start < timeout) {
|
|
65
|
+
attempts++;
|
|
66
|
+
const controller = new AbortController;
|
|
67
|
+
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
|
68
|
+
try {
|
|
69
|
+
const response = await fetch(url, {
|
|
70
|
+
signal: controller.signal
|
|
71
|
+
});
|
|
72
|
+
clearTimeout(timeoutId);
|
|
73
|
+
if (response.ok || response.status === 404) {
|
|
74
|
+
if (verbose) {
|
|
75
|
+
console.log(` ✓ ${url} ready after ${attempts} attempts`);
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
} catch {
|
|
80
|
+
clearTimeout(timeoutId);
|
|
81
|
+
if (verbose && attempts % 5 === 0) {
|
|
82
|
+
console.log(` ⏳ Waiting for ${url}... (${Math.round((Date.now() - start) / 1000)}s)`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
await sleep(interval);
|
|
86
|
+
}
|
|
87
|
+
throw new Error(`Server at ${url} did not respond within ${timeout}ms after ${attempts} attempts`);
|
|
88
|
+
}
|
|
89
|
+
async function waitForDevServers(apps, ports, options = {}) {
|
|
90
|
+
const { timeout = 60000, verbose = true } = options;
|
|
91
|
+
if (verbose)
|
|
92
|
+
console.log("⏳ Waiting for servers to be ready...");
|
|
93
|
+
const promises = [];
|
|
94
|
+
for (const [name, config] of Object.entries(apps)) {
|
|
95
|
+
const port = ports[name];
|
|
96
|
+
const healthPath = config.healthEndpoint ?? "/";
|
|
97
|
+
const url = `http://localhost:${port}${healthPath}`;
|
|
98
|
+
const appTimeout = config.healthTimeout ?? timeout;
|
|
99
|
+
promises.push(waitForServer(url, { timeout: appTimeout, verbose }));
|
|
100
|
+
}
|
|
101
|
+
await Promise.all(promises);
|
|
102
|
+
if (verbose)
|
|
103
|
+
console.log("✓ All servers ready");
|
|
104
|
+
}
|
|
105
|
+
async function isPortAvailable(port) {
|
|
106
|
+
const controller = new AbortController;
|
|
107
|
+
const timeoutId = setTimeout(() => controller.abort(), 500);
|
|
108
|
+
try {
|
|
109
|
+
const _response = await fetch(`http://localhost:${port}/`, {
|
|
110
|
+
signal: controller.signal
|
|
111
|
+
});
|
|
112
|
+
clearTimeout(timeoutId);
|
|
113
|
+
return false;
|
|
114
|
+
} catch (error) {
|
|
115
|
+
clearTimeout(timeoutId);
|
|
116
|
+
if (error instanceof Error) {
|
|
117
|
+
if (error.message.includes("ECONNREFUSED") || error.message.includes("fetch failed")) {
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export { getLocalIp, waitForServer, waitForDevServers, isPortAvailable, sleep, isCI, logFrontendPort, getEnvVar, logApiUrl, logExpoApiUrl };
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sleep
|
|
3
|
+
} from "./index-fkgqg6w2.js";
|
|
4
|
+
|
|
5
|
+
// src/core/quick-tunnel/index.ts
|
|
6
|
+
import { existsSync } from "node:fs";
|
|
7
|
+
import { createInterface } from "node:readline";
|
|
8
|
+
|
|
9
|
+
// src/core/quick-tunnel/cloudflared-process.ts
|
|
10
|
+
import { spawn } from "node:child_process";
|
|
11
|
+
|
|
12
|
+
// src/core/quick-tunnel/constants.ts
|
|
13
|
+
import { tmpdir } from "node:os";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
var CLOUDFLARED_VERSION = process.env.CLOUDFLARED_VERSION || "2023.10.0";
|
|
16
|
+
var RELEASE_BASE = "https://github.com/cloudflare/cloudflared/releases/";
|
|
17
|
+
var cloudflaredBinPath = path.join(tmpdir(), "buncargo-cloudflared", process.platform === "win32" ? `cloudflared.${CLOUDFLARED_VERSION}.exe` : `cloudflared.${CLOUDFLARED_VERSION}`);
|
|
18
|
+
var cloudflaredNotice = `
|
|
19
|
+
\uD83D\uDD25 Your installation of cloudflared software constitutes a symbol of your signature
|
|
20
|
+
indicating that you accept the terms of the Cloudflare License, Terms and Privacy Policy.
|
|
21
|
+
|
|
22
|
+
❯ License: \`https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/license/\`
|
|
23
|
+
❯ Terms: \`https://www.cloudflare.com/terms/\`
|
|
24
|
+
❯ Privacy Policy: \`https://www.cloudflare.com/privacypolicy/\`
|
|
25
|
+
`;
|
|
26
|
+
|
|
27
|
+
// src/core/quick-tunnel/cloudflared-process.ts
|
|
28
|
+
var urlRegexPipe = /\|\s+(https?:\/\/\S+)/;
|
|
29
|
+
var urlRegexTryCloudflare = /(https:\/\/[a-zA-Z0-9][-a-zA-Z0-9.]*\.trycloudflare\.com)\b/;
|
|
30
|
+
var MAX_CAPTURED_LOG = 24000;
|
|
31
|
+
function parseQuickTunnelUrlFromOutput(log) {
|
|
32
|
+
const pipe = log.match(urlRegexPipe);
|
|
33
|
+
if (pipe?.[1]) {
|
|
34
|
+
return pipe[1];
|
|
35
|
+
}
|
|
36
|
+
const direct = log.match(urlRegexTryCloudflare);
|
|
37
|
+
return direct?.[1] ?? null;
|
|
38
|
+
}
|
|
39
|
+
function startCloudflaredTunnel(options) {
|
|
40
|
+
const args = ["tunnel"];
|
|
41
|
+
for (const [key, value] of Object.entries(options)) {
|
|
42
|
+
if (typeof value === "string") {
|
|
43
|
+
args.push(`${key}`, value);
|
|
44
|
+
} else if (typeof value === "number") {
|
|
45
|
+
args.push(`${key}`, value.toString());
|
|
46
|
+
} else if (value === null) {
|
|
47
|
+
args.push(`${key}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (args.length === 1) {
|
|
51
|
+
args.push("--url", "localhost:8080");
|
|
52
|
+
}
|
|
53
|
+
const child = spawn(cloudflaredBinPath, args, {
|
|
54
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
55
|
+
});
|
|
56
|
+
if (process.env.DEBUG) {
|
|
57
|
+
child.stdout?.pipe(process.stdout);
|
|
58
|
+
child.stderr?.pipe(process.stderr);
|
|
59
|
+
}
|
|
60
|
+
let settled = false;
|
|
61
|
+
let urlResolver;
|
|
62
|
+
let urlRejector;
|
|
63
|
+
const url = new Promise((resolve, reject) => {
|
|
64
|
+
urlResolver = (v) => {
|
|
65
|
+
if (!settled) {
|
|
66
|
+
settled = true;
|
|
67
|
+
resolve(v);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
urlRejector = (e) => {
|
|
71
|
+
if (!settled) {
|
|
72
|
+
settled = true;
|
|
73
|
+
reject(e);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
const log = { buf: "" };
|
|
78
|
+
const append = (data) => {
|
|
79
|
+
log.buf += data.toString();
|
|
80
|
+
if (log.buf.length > MAX_CAPTURED_LOG) {
|
|
81
|
+
log.buf = log.buf.slice(-MAX_CAPTURED_LOG);
|
|
82
|
+
}
|
|
83
|
+
const url2 = parseQuickTunnelUrlFromOutput(log.buf);
|
|
84
|
+
if (url2) {
|
|
85
|
+
urlResolver(url2);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
child.stdout?.on("data", append).on("error", urlRejector);
|
|
89
|
+
child.stderr?.on("data", append).on("error", urlRejector);
|
|
90
|
+
child.on("exit", (code, signal) => {
|
|
91
|
+
if (!settled) {
|
|
92
|
+
const tail = log.buf.trimEnd();
|
|
93
|
+
const excerpt = tail.length > 1200 ? `…${tail.slice(-1200)}` : tail;
|
|
94
|
+
const detail = excerpt ? `
|
|
95
|
+
cloudflared output (tail):
|
|
96
|
+
${excerpt}` : "";
|
|
97
|
+
urlRejector(new Error(`cloudflared exited before a tunnel URL was parsed (code=${code}, signal=${signal ?? "none"}). ` + `Parallel quick-tunnel requests are often rate-limited; buncargo starts tunnels sequentially with a short pause. ` + `If this persists, try fewer expose targets or increase BUNCARGO_EXPOSE_TUNNEL_STAGGER_MS.${detail}`));
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
child.on("error", urlRejector);
|
|
101
|
+
const stop = () => child.kill("SIGINT");
|
|
102
|
+
return { url, child, stop };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/core/quick-tunnel/install.ts
|
|
106
|
+
import { execSync } from "node:child_process";
|
|
107
|
+
import fs from "node:fs";
|
|
108
|
+
import https from "node:https";
|
|
109
|
+
import path2 from "node:path";
|
|
110
|
+
var LINUX_URL = {
|
|
111
|
+
arm64: "cloudflared-linux-arm64",
|
|
112
|
+
arm: "cloudflared-linux-arm",
|
|
113
|
+
x64: "cloudflared-linux-amd64",
|
|
114
|
+
ia32: "cloudflared-linux-386"
|
|
115
|
+
};
|
|
116
|
+
var MACOS_URL = {
|
|
117
|
+
arm64: "cloudflared-darwin-amd64.tgz",
|
|
118
|
+
x64: "cloudflared-darwin-amd64.tgz"
|
|
119
|
+
};
|
|
120
|
+
var WINDOWS_URL = {
|
|
121
|
+
x64: "cloudflared-windows-amd64.exe",
|
|
122
|
+
ia32: "cloudflared-windows-386.exe"
|
|
123
|
+
};
|
|
124
|
+
function resolveBase(version) {
|
|
125
|
+
if (version === "latest") {
|
|
126
|
+
return `${RELEASE_BASE}latest/download/`;
|
|
127
|
+
}
|
|
128
|
+
return `${RELEASE_BASE}download/${version}/`;
|
|
129
|
+
}
|
|
130
|
+
function installCloudflared(to = cloudflaredBinPath, version = CLOUDFLARED_VERSION) {
|
|
131
|
+
switch (process.platform) {
|
|
132
|
+
case "linux": {
|
|
133
|
+
return installLinux(to, version);
|
|
134
|
+
}
|
|
135
|
+
case "darwin": {
|
|
136
|
+
return installMacos(to, version);
|
|
137
|
+
}
|
|
138
|
+
case "win32": {
|
|
139
|
+
return installWindows(to, version);
|
|
140
|
+
}
|
|
141
|
+
default: {
|
|
142
|
+
throw new Error(`Unsupported platform: ${process.platform}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async function installLinux(to, version = CLOUDFLARED_VERSION) {
|
|
147
|
+
const file = LINUX_URL[process.arch];
|
|
148
|
+
if (file === undefined) {
|
|
149
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
150
|
+
}
|
|
151
|
+
await download(resolveBase(version) + file, to);
|
|
152
|
+
fs.chmodSync(to, 493);
|
|
153
|
+
return to;
|
|
154
|
+
}
|
|
155
|
+
async function installMacos(to, version = CLOUDFLARED_VERSION) {
|
|
156
|
+
const file = MACOS_URL[process.arch];
|
|
157
|
+
if (file === undefined) {
|
|
158
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
159
|
+
}
|
|
160
|
+
await download(resolveBase(version) + file, `${to}.tgz`);
|
|
161
|
+
if (process.env.DEBUG) {
|
|
162
|
+
console.log(`Extracting to ${to}`);
|
|
163
|
+
}
|
|
164
|
+
execSync(`tar -xzf ${path2.basename(`${to}.tgz`)}`, {
|
|
165
|
+
cwd: path2.dirname(to)
|
|
166
|
+
});
|
|
167
|
+
fs.unlinkSync(`${to}.tgz`);
|
|
168
|
+
fs.renameSync(`${path2.dirname(to)}/cloudflared`, to);
|
|
169
|
+
return to;
|
|
170
|
+
}
|
|
171
|
+
async function installWindows(to, version = CLOUDFLARED_VERSION) {
|
|
172
|
+
const file = WINDOWS_URL[process.arch];
|
|
173
|
+
if (file === undefined) {
|
|
174
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
175
|
+
}
|
|
176
|
+
await download(resolveBase(version) + file, to);
|
|
177
|
+
return to;
|
|
178
|
+
}
|
|
179
|
+
function download(url, to, redirect = 0) {
|
|
180
|
+
if (redirect === 0) {
|
|
181
|
+
if (process.env.DEBUG) {
|
|
182
|
+
console.log(`Downloading ${url} to ${to}`);
|
|
183
|
+
}
|
|
184
|
+
} else if (process.env.DEBUG) {
|
|
185
|
+
console.log(`Redirecting to ${url}`);
|
|
186
|
+
}
|
|
187
|
+
return new Promise((resolve, reject) => {
|
|
188
|
+
if (!fs.existsSync(path2.dirname(to))) {
|
|
189
|
+
fs.mkdirSync(path2.dirname(to), { recursive: true });
|
|
190
|
+
}
|
|
191
|
+
let done = true;
|
|
192
|
+
const file = fs.createWriteStream(to);
|
|
193
|
+
const request = https.get(url, (res) => {
|
|
194
|
+
if (res.statusCode === 302 && res.headers.location !== undefined) {
|
|
195
|
+
const redirection = res.headers.location;
|
|
196
|
+
done = false;
|
|
197
|
+
file.close(() => {
|
|
198
|
+
download(redirection, to, redirect + 1).then(resolve, reject);
|
|
199
|
+
});
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
res.pipe(file);
|
|
203
|
+
});
|
|
204
|
+
file.on("finish", () => {
|
|
205
|
+
if (done) {
|
|
206
|
+
file.close(() => {
|
|
207
|
+
resolve(to);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
request.on("error", (err) => {
|
|
212
|
+
fs.unlink(to, () => {
|
|
213
|
+
reject(err);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
file.on("error", (err) => {
|
|
217
|
+
fs.unlink(to, () => {
|
|
218
|
+
reject(err);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
request.end();
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// src/core/quick-tunnel/index.ts
|
|
226
|
+
function resolvedLocalUrl(opts) {
|
|
227
|
+
return opts.url ?? `${opts.protocol || "http"}://${opts.hostname ?? "localhost"}:${opts.port ?? 3000}`;
|
|
228
|
+
}
|
|
229
|
+
function envAcceptsCloudflareNotice() {
|
|
230
|
+
const v = process.env.BUNCARGO_ACCEPT_CLOUDFLARE_NOTICE;
|
|
231
|
+
const u = process.env.UNTUN_ACCEPT_CLOUDFLARE_NOTICE;
|
|
232
|
+
return v === "1" || v === "true" || u === "1" || u === "true";
|
|
233
|
+
}
|
|
234
|
+
async function promptInstallCloudflared() {
|
|
235
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
return new Promise((resolve) => {
|
|
239
|
+
const rl = createInterface({
|
|
240
|
+
input: process.stdin,
|
|
241
|
+
output: process.stdout
|
|
242
|
+
});
|
|
243
|
+
rl.question("Do you agree with the above terms and wish to install the binary from GitHub? (y/N) ", (answer) => {
|
|
244
|
+
rl.close();
|
|
245
|
+
resolve(/^y(es)?$/i.test(answer.trim()));
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
async function startQuickTunnel(opts) {
|
|
250
|
+
const url = resolvedLocalUrl(opts);
|
|
251
|
+
console.log(`Starting cloudflared tunnel to ${url}`);
|
|
252
|
+
if (!existsSync(cloudflaredBinPath)) {
|
|
253
|
+
console.log(cloudflaredNotice);
|
|
254
|
+
const canInstall = opts.acceptCloudflareNotice || envAcceptsCloudflareNotice() || await promptInstallCloudflared();
|
|
255
|
+
if (!canInstall) {
|
|
256
|
+
console.error("Skipping tunnel setup.");
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
await installCloudflared();
|
|
260
|
+
}
|
|
261
|
+
const cfArgs = { "--url": url };
|
|
262
|
+
if (!opts.verifyTLS) {
|
|
263
|
+
cfArgs["--no-tls-verify"] = null;
|
|
264
|
+
}
|
|
265
|
+
const tunnel = startCloudflaredTunnel(cfArgs);
|
|
266
|
+
const cleanup = async () => {
|
|
267
|
+
tunnel.stop();
|
|
268
|
+
};
|
|
269
|
+
return {
|
|
270
|
+
getURL: async () => await tunnel.url,
|
|
271
|
+
close: cleanup
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// src/core/tunnel.ts
|
|
276
|
+
function parseExposeNames(exposeValue) {
|
|
277
|
+
if (exposeValue === undefined)
|
|
278
|
+
return null;
|
|
279
|
+
const names = exposeValue.split(",").map((name) => name.trim()).filter(Boolean);
|
|
280
|
+
return new Set(names);
|
|
281
|
+
}
|
|
282
|
+
async function resolvePublicUrl(tunnel) {
|
|
283
|
+
if (typeof tunnel.getURL === "function") {
|
|
284
|
+
return await tunnel.getURL();
|
|
285
|
+
}
|
|
286
|
+
return tunnel.url ?? tunnel.publicUrl ?? tunnel.tunnelUrl ?? null;
|
|
287
|
+
}
|
|
288
|
+
function toCloseFn(tunnel) {
|
|
289
|
+
const close = tunnel.close ?? tunnel.stop ?? tunnel.destroy;
|
|
290
|
+
if (!close)
|
|
291
|
+
return async () => {};
|
|
292
|
+
return async () => {
|
|
293
|
+
await close();
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
function resolveExposeTargets(env, exposeValue) {
|
|
297
|
+
const requestedNames = parseExposeNames(exposeValue);
|
|
298
|
+
const knownTargets = new Map;
|
|
299
|
+
const enabledTargets = new Map;
|
|
300
|
+
for (const [name, config] of Object.entries(env.services)) {
|
|
301
|
+
const port = env.ports[name];
|
|
302
|
+
if (port === undefined)
|
|
303
|
+
continue;
|
|
304
|
+
const target = { kind: "service", name, port };
|
|
305
|
+
knownTargets.set(name, target);
|
|
306
|
+
if (config.expose === true) {
|
|
307
|
+
enabledTargets.set(name, target);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
for (const [name, config] of Object.entries(env.apps)) {
|
|
311
|
+
const port = env.ports[name];
|
|
312
|
+
if (port === undefined)
|
|
313
|
+
continue;
|
|
314
|
+
const target = { kind: "app", name, port };
|
|
315
|
+
knownTargets.set(name, target);
|
|
316
|
+
if (config.expose === true) {
|
|
317
|
+
enabledTargets.set(name, target);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (requestedNames === null) {
|
|
321
|
+
return {
|
|
322
|
+
targets: Array.from(enabledTargets.values()),
|
|
323
|
+
unknownNames: [],
|
|
324
|
+
notEnabledNames: []
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
const unknownNames = [];
|
|
328
|
+
const notEnabledNames = [];
|
|
329
|
+
const targets = [];
|
|
330
|
+
for (const name of requestedNames) {
|
|
331
|
+
if (!knownTargets.has(name)) {
|
|
332
|
+
unknownNames.push(name);
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
const enabledTarget = enabledTargets.get(name);
|
|
336
|
+
if (!enabledTarget) {
|
|
337
|
+
notEnabledNames.push(name);
|
|
338
|
+
continue;
|
|
339
|
+
}
|
|
340
|
+
targets.push(enabledTarget);
|
|
341
|
+
}
|
|
342
|
+
return { targets, unknownNames, notEnabledNames };
|
|
343
|
+
}
|
|
344
|
+
function resolveExposeTunnelStaggerMs() {
|
|
345
|
+
const raw = process.env.BUNCARGO_EXPOSE_TUNNEL_STAGGER_MS;
|
|
346
|
+
if (raw === undefined || raw === "") {
|
|
347
|
+
return 900;
|
|
348
|
+
}
|
|
349
|
+
const n = Number.parseInt(raw, 10);
|
|
350
|
+
return Number.isFinite(n) && n >= 0 ? n : 900;
|
|
351
|
+
}
|
|
352
|
+
async function startPublicTunnels(targets, options = {}) {
|
|
353
|
+
const start = options.start ?? ((input) => startQuickTunnel(input));
|
|
354
|
+
const staggerMs = resolveExposeTunnelStaggerMs();
|
|
355
|
+
const tunnels = [];
|
|
356
|
+
try {
|
|
357
|
+
for (let i = 0;i < targets.length; i++) {
|
|
358
|
+
if (i > 0 && staggerMs > 0) {
|
|
359
|
+
await sleep(staggerMs);
|
|
360
|
+
}
|
|
361
|
+
const target = targets[i];
|
|
362
|
+
const localUrl = `http://localhost:${target.port}`;
|
|
363
|
+
const tunnel = await start({
|
|
364
|
+
url: localUrl
|
|
365
|
+
});
|
|
366
|
+
if (tunnel === undefined) {
|
|
367
|
+
throw new Error(`Tunnel for "${target.name}" could not be started (cloudflared missing or install declined)`);
|
|
368
|
+
}
|
|
369
|
+
const publicUrl = await resolvePublicUrl(tunnel);
|
|
370
|
+
if (!publicUrl) {
|
|
371
|
+
throw new Error(`Tunnel for "${target.name}" did not provide a public URL`);
|
|
372
|
+
}
|
|
373
|
+
tunnels.push({
|
|
374
|
+
kind: target.kind,
|
|
375
|
+
name: target.name,
|
|
376
|
+
localUrl,
|
|
377
|
+
publicUrl,
|
|
378
|
+
close: toCloseFn(tunnel)
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
return tunnels;
|
|
382
|
+
} catch (e) {
|
|
383
|
+
await stopPublicTunnels(tunnels);
|
|
384
|
+
throw e;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
async function stopPublicTunnels(tunnels) {
|
|
388
|
+
await Promise.allSettled(tunnels.map((tunnel) => tunnel.close()));
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
export { resolveExposeTargets, startPublicTunnels, stopPublicTunnels };
|