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,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createDevEnvironment
|
|
3
|
+
} from "./index-qnpd5fn5.js";
|
|
4
|
+
|
|
5
|
+
// src/loader/cache.ts
|
|
6
|
+
var cachedEnv = null;
|
|
7
|
+
function setCachedDevEnv(env) {
|
|
8
|
+
cachedEnv = env;
|
|
9
|
+
}
|
|
10
|
+
function getCachedDevEnv() {
|
|
11
|
+
return cachedEnv;
|
|
12
|
+
}
|
|
13
|
+
function clearDevEnvCache() {
|
|
14
|
+
cachedEnv = null;
|
|
15
|
+
}
|
|
16
|
+
// src/loader/find-config-file.ts
|
|
17
|
+
import { existsSync } from "node:fs";
|
|
18
|
+
import { dirname, join } from "node:path";
|
|
19
|
+
var CONFIG_FILES = [
|
|
20
|
+
"dev.config.ts",
|
|
21
|
+
"dev.config.js",
|
|
22
|
+
"dev-tools.config.ts",
|
|
23
|
+
"dev-tools.config.js"
|
|
24
|
+
];
|
|
25
|
+
function findConfigFile(startDir) {
|
|
26
|
+
let currentDir = startDir;
|
|
27
|
+
while (true) {
|
|
28
|
+
for (const file of CONFIG_FILES) {
|
|
29
|
+
const configPath = join(currentDir, file);
|
|
30
|
+
if (existsSync(configPath)) {
|
|
31
|
+
return configPath;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const parentDir = dirname(currentDir);
|
|
35
|
+
if (parentDir === currentDir) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
currentDir = parentDir;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// src/loader/load-dev-env.ts
|
|
42
|
+
async function loadDevEnv(options) {
|
|
43
|
+
if (!options?.reload) {
|
|
44
|
+
const cached = getCachedDevEnv();
|
|
45
|
+
if (cached)
|
|
46
|
+
return cached;
|
|
47
|
+
}
|
|
48
|
+
const cwd = options?.cwd ?? process.cwd();
|
|
49
|
+
const configPath = findConfigFile(cwd);
|
|
50
|
+
if (configPath) {
|
|
51
|
+
const mod = await import(configPath);
|
|
52
|
+
const config = mod.default;
|
|
53
|
+
if (!config?.projectPrefix || !config?.services) {
|
|
54
|
+
throw new Error(`Invalid config in "${configPath}". Use defineDevConfig() and export as default.`);
|
|
55
|
+
}
|
|
56
|
+
const env = createDevEnvironment(config);
|
|
57
|
+
setCachedDevEnv(env);
|
|
58
|
+
return env;
|
|
59
|
+
}
|
|
60
|
+
throw new Error("No config file found. Create dev.config.ts with: export default defineDevConfig({ ... })");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/loader/index.ts
|
|
64
|
+
function getDevEnv() {
|
|
65
|
+
const env = getCachedDevEnv();
|
|
66
|
+
if (!env) {
|
|
67
|
+
throw new Error("Dev environment not loaded. Call loadDevEnv() first.");
|
|
68
|
+
}
|
|
69
|
+
return env;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export { clearDevEnvCache, CONFIG_FILES, findConfigFile, loadDevEnv, getDevEnv };
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sleep
|
|
3
|
+
} from "./index-fkgqg6w2.js";
|
|
4
|
+
|
|
5
|
+
// src/core/quick-tunnel/index.ts
|
|
6
|
+
import { existsSync as existsSync2 } from "node:fs";
|
|
7
|
+
|
|
8
|
+
// src/core/quick-tunnel/cloudflared-process.ts
|
|
9
|
+
import { spawn } from "node:child_process";
|
|
10
|
+
|
|
11
|
+
// src/core/quick-tunnel/constants.ts
|
|
12
|
+
import { existsSync } from "node:fs";
|
|
13
|
+
import { tmpdir } from "node:os";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
var CLOUDFLARED_VERSION = process.env.CLOUDFLARED_VERSION || "2026.3.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
|
+
function resolvedCloudflaredBinPath() {
|
|
19
|
+
const override = process.env.BUNCARGO_CLOUDFLARED_PATH?.trim();
|
|
20
|
+
if (override) {
|
|
21
|
+
if (!existsSync(override)) {
|
|
22
|
+
throw new Error(`BUNCARGO_CLOUDFLARED_PATH does not exist: ${override}`);
|
|
23
|
+
}
|
|
24
|
+
return path.resolve(override);
|
|
25
|
+
}
|
|
26
|
+
return cloudflaredBinPath;
|
|
27
|
+
}
|
|
28
|
+
var cloudflaredNotice = `
|
|
29
|
+
\uD83D\uDD25 Your installation of cloudflared software constitutes a symbol of your signature
|
|
30
|
+
indicating that you accept the terms of the Cloudflare License, Terms and Privacy Policy.
|
|
31
|
+
|
|
32
|
+
❯ License: \`https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/license/\`
|
|
33
|
+
❯ Terms: \`https://www.cloudflare.com/terms/\`
|
|
34
|
+
❯ Privacy Policy: \`https://www.cloudflare.com/privacypolicy/\`
|
|
35
|
+
`;
|
|
36
|
+
|
|
37
|
+
// src/core/quick-tunnel/cloudflared-process.ts
|
|
38
|
+
var urlRegexPipe = /\|\s+(https?:\/\/\S+)/;
|
|
39
|
+
var urlRegexTryCloudflare = /(https:\/\/[a-zA-Z0-9][-a-zA-Z0-9.]*\.trycloudflare\.com)\b/;
|
|
40
|
+
var MAX_CAPTURED_LOG = 24000;
|
|
41
|
+
function resolveQuickTunnelUrlTimeoutMs() {
|
|
42
|
+
const raw = process.env.BUNCARGO_QUICK_TUNNEL_TIMEOUT_MS;
|
|
43
|
+
if (raw === undefined || raw === "") {
|
|
44
|
+
return 30000;
|
|
45
|
+
}
|
|
46
|
+
const n = Number.parseInt(raw, 10);
|
|
47
|
+
return Number.isFinite(n) && n >= 0 ? n : 30000;
|
|
48
|
+
}
|
|
49
|
+
function parseQuickTunnelUrlFromOutput(log) {
|
|
50
|
+
const pipe = log.match(urlRegexPipe);
|
|
51
|
+
if (pipe?.[1]) {
|
|
52
|
+
return pipe[1];
|
|
53
|
+
}
|
|
54
|
+
const direct = log.match(urlRegexTryCloudflare);
|
|
55
|
+
return direct?.[1] ?? null;
|
|
56
|
+
}
|
|
57
|
+
function startCloudflaredTunnel(options) {
|
|
58
|
+
const args = ["tunnel"];
|
|
59
|
+
for (const [key, value] of Object.entries(options)) {
|
|
60
|
+
if (typeof value === "string") {
|
|
61
|
+
args.push(`${key}`, value);
|
|
62
|
+
} else if (typeof value === "number") {
|
|
63
|
+
args.push(`${key}`, value.toString());
|
|
64
|
+
} else if (value === null) {
|
|
65
|
+
args.push(`${key}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (args.length === 1) {
|
|
69
|
+
args.push("--url", "localhost:8080");
|
|
70
|
+
}
|
|
71
|
+
const binPath = resolvedCloudflaredBinPath();
|
|
72
|
+
const child = spawn(binPath, args, {
|
|
73
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
74
|
+
});
|
|
75
|
+
if (process.env.DEBUG) {
|
|
76
|
+
child.stdout?.pipe(process.stdout);
|
|
77
|
+
child.stderr?.pipe(process.stderr);
|
|
78
|
+
}
|
|
79
|
+
let settled = false;
|
|
80
|
+
let urlResolver;
|
|
81
|
+
let urlRejector;
|
|
82
|
+
let timeoutId;
|
|
83
|
+
const clearUrlTimeout = () => {
|
|
84
|
+
if (timeoutId !== undefined) {
|
|
85
|
+
clearTimeout(timeoutId);
|
|
86
|
+
timeoutId = undefined;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const url = new Promise((resolve, reject) => {
|
|
90
|
+
urlResolver = (v) => {
|
|
91
|
+
if (!settled) {
|
|
92
|
+
settled = true;
|
|
93
|
+
clearUrlTimeout();
|
|
94
|
+
resolve(v);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
urlRejector = (e) => {
|
|
98
|
+
if (!settled) {
|
|
99
|
+
settled = true;
|
|
100
|
+
clearUrlTimeout();
|
|
101
|
+
reject(e);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const timeoutMs = resolveQuickTunnelUrlTimeoutMs();
|
|
105
|
+
if (timeoutMs > 0) {
|
|
106
|
+
timeoutId = setTimeout(() => {
|
|
107
|
+
try {
|
|
108
|
+
child.kill("SIGINT");
|
|
109
|
+
} catch {}
|
|
110
|
+
urlRejector(new Error(`quick tunnel URL timed out after ${timeoutMs}ms (no public URL from cloudflared)`));
|
|
111
|
+
}, timeoutMs);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
const log = { buf: "" };
|
|
115
|
+
const append = (data) => {
|
|
116
|
+
log.buf += data.toString();
|
|
117
|
+
if (log.buf.length > MAX_CAPTURED_LOG) {
|
|
118
|
+
log.buf = log.buf.slice(-MAX_CAPTURED_LOG);
|
|
119
|
+
}
|
|
120
|
+
const url2 = parseQuickTunnelUrlFromOutput(log.buf);
|
|
121
|
+
if (url2) {
|
|
122
|
+
urlResolver(url2);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
child.stdout?.on("data", append).on("error", urlRejector);
|
|
126
|
+
child.stderr?.on("data", append).on("error", urlRejector);
|
|
127
|
+
child.on("exit", (code, signal) => {
|
|
128
|
+
if (!settled) {
|
|
129
|
+
const tail = log.buf.trimEnd();
|
|
130
|
+
const excerpt = tail.length > 1200 ? `…${tail.slice(-1200)}` : tail;
|
|
131
|
+
const detail = excerpt ? `
|
|
132
|
+
cloudflared output (tail):
|
|
133
|
+
${excerpt}` : "";
|
|
134
|
+
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}`));
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
child.on("error", urlRejector);
|
|
138
|
+
const stop = () => child.kill("SIGINT");
|
|
139
|
+
return { url, child, stop };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// src/core/quick-tunnel/install.ts
|
|
143
|
+
import { execSync } from "node:child_process";
|
|
144
|
+
import fs from "node:fs";
|
|
145
|
+
import https from "node:https";
|
|
146
|
+
import path2 from "node:path";
|
|
147
|
+
var LINUX_URL = {
|
|
148
|
+
arm64: "cloudflared-linux-arm64",
|
|
149
|
+
arm: "cloudflared-linux-arm",
|
|
150
|
+
x64: "cloudflared-linux-amd64",
|
|
151
|
+
ia32: "cloudflared-linux-386"
|
|
152
|
+
};
|
|
153
|
+
var MACOS_URL = {
|
|
154
|
+
arm64: "cloudflared-darwin-arm64.tgz",
|
|
155
|
+
x64: "cloudflared-darwin-amd64.tgz"
|
|
156
|
+
};
|
|
157
|
+
var WINDOWS_URL = {
|
|
158
|
+
x64: "cloudflared-windows-amd64.exe",
|
|
159
|
+
ia32: "cloudflared-windows-386.exe"
|
|
160
|
+
};
|
|
161
|
+
function resolveBase(version) {
|
|
162
|
+
if (version === "latest") {
|
|
163
|
+
return `${RELEASE_BASE}latest/download/`;
|
|
164
|
+
}
|
|
165
|
+
return `${RELEASE_BASE}download/${version}/`;
|
|
166
|
+
}
|
|
167
|
+
function installCloudflared(to = cloudflaredBinPath, version = CLOUDFLARED_VERSION) {
|
|
168
|
+
switch (process.platform) {
|
|
169
|
+
case "linux": {
|
|
170
|
+
return installLinux(to, version);
|
|
171
|
+
}
|
|
172
|
+
case "darwin": {
|
|
173
|
+
return installMacos(to, version);
|
|
174
|
+
}
|
|
175
|
+
case "win32": {
|
|
176
|
+
return installWindows(to, version);
|
|
177
|
+
}
|
|
178
|
+
default: {
|
|
179
|
+
throw new Error(`Unsupported platform: ${process.platform}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
async function installLinux(to, version = CLOUDFLARED_VERSION) {
|
|
184
|
+
const file = LINUX_URL[process.arch];
|
|
185
|
+
if (file === undefined) {
|
|
186
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
187
|
+
}
|
|
188
|
+
await download(resolveBase(version) + file, to);
|
|
189
|
+
fs.chmodSync(to, 493);
|
|
190
|
+
return to;
|
|
191
|
+
}
|
|
192
|
+
async function installMacos(to, version = CLOUDFLARED_VERSION) {
|
|
193
|
+
const file = MACOS_URL[process.arch];
|
|
194
|
+
if (file === undefined) {
|
|
195
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
196
|
+
}
|
|
197
|
+
await download(resolveBase(version) + file, `${to}.tgz`);
|
|
198
|
+
if (process.env.DEBUG) {
|
|
199
|
+
console.log(`Extracting to ${to}`);
|
|
200
|
+
}
|
|
201
|
+
execSync(`tar -xzf ${path2.basename(`${to}.tgz`)}`, {
|
|
202
|
+
cwd: path2.dirname(to)
|
|
203
|
+
});
|
|
204
|
+
fs.unlinkSync(`${to}.tgz`);
|
|
205
|
+
fs.renameSync(`${path2.dirname(to)}/cloudflared`, to);
|
|
206
|
+
return to;
|
|
207
|
+
}
|
|
208
|
+
async function installWindows(to, version = CLOUDFLARED_VERSION) {
|
|
209
|
+
const file = WINDOWS_URL[process.arch];
|
|
210
|
+
if (file === undefined) {
|
|
211
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
212
|
+
}
|
|
213
|
+
await download(resolveBase(version) + file, to);
|
|
214
|
+
return to;
|
|
215
|
+
}
|
|
216
|
+
function download(url, to, redirect = 0) {
|
|
217
|
+
if (redirect === 0) {
|
|
218
|
+
if (process.env.DEBUG) {
|
|
219
|
+
console.log(`Downloading ${url} to ${to}`);
|
|
220
|
+
}
|
|
221
|
+
} else if (process.env.DEBUG) {
|
|
222
|
+
console.log(`Redirecting to ${url}`);
|
|
223
|
+
}
|
|
224
|
+
return new Promise((resolve, reject) => {
|
|
225
|
+
if (!fs.existsSync(path2.dirname(to))) {
|
|
226
|
+
fs.mkdirSync(path2.dirname(to), { recursive: true });
|
|
227
|
+
}
|
|
228
|
+
let done = true;
|
|
229
|
+
const file = fs.createWriteStream(to);
|
|
230
|
+
const request = https.get(url, (res) => {
|
|
231
|
+
if (res.statusCode === 302 && res.headers.location !== undefined) {
|
|
232
|
+
const redirection = res.headers.location;
|
|
233
|
+
done = false;
|
|
234
|
+
file.close(() => {
|
|
235
|
+
download(redirection, to, redirect + 1).then(resolve, reject);
|
|
236
|
+
});
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
res.pipe(file);
|
|
240
|
+
});
|
|
241
|
+
file.on("finish", () => {
|
|
242
|
+
if (done) {
|
|
243
|
+
file.close(() => {
|
|
244
|
+
resolve(to);
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
request.on("error", (err) => {
|
|
249
|
+
fs.unlink(to, () => {
|
|
250
|
+
reject(err);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
file.on("error", (err) => {
|
|
254
|
+
fs.unlink(to, () => {
|
|
255
|
+
reject(err);
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
request.end();
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// src/core/quick-tunnel/index.ts
|
|
263
|
+
function resolveMaxQuickTunnelAttempts() {
|
|
264
|
+
const raw = process.env.BUNCARGO_QUICK_TUNNEL_MAX_ATTEMPTS;
|
|
265
|
+
if (raw === undefined || raw === "") {
|
|
266
|
+
return 5;
|
|
267
|
+
}
|
|
268
|
+
const n = Number.parseInt(raw, 10);
|
|
269
|
+
return Number.isFinite(n) && n >= 1 ? n : 5;
|
|
270
|
+
}
|
|
271
|
+
function resolveQuickTunnelRetryBaseMs() {
|
|
272
|
+
const raw = process.env.BUNCARGO_QUICK_TUNNEL_RETRY_BASE_MS;
|
|
273
|
+
if (raw === undefined || raw === "") {
|
|
274
|
+
return 2000;
|
|
275
|
+
}
|
|
276
|
+
const n = Number.parseInt(raw, 10);
|
|
277
|
+
return Number.isFinite(n) && n >= 0 ? n : 2000;
|
|
278
|
+
}
|
|
279
|
+
function usesBundledCloudflaredCache() {
|
|
280
|
+
return !process.env.BUNCARGO_CLOUDFLARED_PATH?.trim();
|
|
281
|
+
}
|
|
282
|
+
function isRetryableQuickTunnelError(message) {
|
|
283
|
+
return message.includes("429") || message.includes("Too Many Requests") || message.includes('status_code="429') || message.includes("failed to unmarshal quick Tunnel") || message.includes("failed to unmarshall quick Tunnel") || message.includes("Error unmarshaling QuickTunnel") || message.includes("invalid character '<'") || message.includes("quick tunnel URL timed out");
|
|
284
|
+
}
|
|
285
|
+
async function startCloudflaredTunnelWithRetry(cfArgs) {
|
|
286
|
+
const maxAttempts = resolveMaxQuickTunnelAttempts();
|
|
287
|
+
const baseMs = resolveQuickTunnelRetryBaseMs();
|
|
288
|
+
for (let attempt = 1;attempt <= maxAttempts; attempt++) {
|
|
289
|
+
const tunnel = startCloudflaredTunnel(cfArgs);
|
|
290
|
+
try {
|
|
291
|
+
await tunnel.url;
|
|
292
|
+
return tunnel;
|
|
293
|
+
} catch (e) {
|
|
294
|
+
try {
|
|
295
|
+
tunnel.stop();
|
|
296
|
+
} catch {}
|
|
297
|
+
const msg = String(e);
|
|
298
|
+
if (attempt < maxAttempts && isRetryableQuickTunnelError(msg)) {
|
|
299
|
+
const delayMs = baseMs * attempt;
|
|
300
|
+
console.log(`Cloudflare quick tunnel temporarily unavailable (${attempt}/${maxAttempts}), retrying in ${delayMs}ms…`);
|
|
301
|
+
await sleep(delayMs);
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
throw e;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
throw new Error("startCloudflaredTunnelWithRetry: exhausted attempts");
|
|
308
|
+
}
|
|
309
|
+
function resolvedLocalUrl(opts) {
|
|
310
|
+
return opts.url ?? `${opts.protocol || "http"}://${opts.hostname ?? "localhost"}:${opts.port ?? 3000}`;
|
|
311
|
+
}
|
|
312
|
+
async function startQuickTunnel(opts) {
|
|
313
|
+
const url = resolvedLocalUrl(opts);
|
|
314
|
+
console.log(`Starting cloudflared tunnel to ${url}`);
|
|
315
|
+
resolvedCloudflaredBinPath();
|
|
316
|
+
if (usesBundledCloudflaredCache() && !existsSync2(cloudflaredBinPath)) {
|
|
317
|
+
console.log(cloudflaredNotice);
|
|
318
|
+
await installCloudflared();
|
|
319
|
+
}
|
|
320
|
+
const cfArgs = { "--url": url };
|
|
321
|
+
if (!opts.verifyTLS) {
|
|
322
|
+
cfArgs["--no-tls-verify"] = null;
|
|
323
|
+
}
|
|
324
|
+
const tunnel = await startCloudflaredTunnelWithRetry(cfArgs);
|
|
325
|
+
const cleanup = async () => {
|
|
326
|
+
tunnel.stop();
|
|
327
|
+
};
|
|
328
|
+
return {
|
|
329
|
+
getURL: async () => await tunnel.url,
|
|
330
|
+
close: cleanup
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
// src/core/tunnel.ts
|
|
335
|
+
function parseExposeNames(exposeValue) {
|
|
336
|
+
if (exposeValue === undefined)
|
|
337
|
+
return null;
|
|
338
|
+
const names = exposeValue.split(",").map((name) => name.trim()).filter(Boolean);
|
|
339
|
+
return new Set(names);
|
|
340
|
+
}
|
|
341
|
+
async function resolvePublicUrl(tunnel) {
|
|
342
|
+
if (typeof tunnel.getURL === "function") {
|
|
343
|
+
return await tunnel.getURL();
|
|
344
|
+
}
|
|
345
|
+
return tunnel.url ?? tunnel.publicUrl ?? tunnel.tunnelUrl ?? null;
|
|
346
|
+
}
|
|
347
|
+
function toCloseFn(tunnel) {
|
|
348
|
+
const close = tunnel.close ?? tunnel.stop ?? tunnel.destroy;
|
|
349
|
+
if (!close)
|
|
350
|
+
return async () => {};
|
|
351
|
+
return async () => {
|
|
352
|
+
await close();
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
function resolveExposeTargets(env, exposeValue) {
|
|
356
|
+
const requestedNames = parseExposeNames(exposeValue);
|
|
357
|
+
const knownTargets = new Map;
|
|
358
|
+
const enabledTargets = new Map;
|
|
359
|
+
for (const [name, config] of Object.entries(env.services)) {
|
|
360
|
+
const port = env.ports[name];
|
|
361
|
+
if (port === undefined)
|
|
362
|
+
continue;
|
|
363
|
+
const target = { kind: "service", name, port };
|
|
364
|
+
knownTargets.set(name, target);
|
|
365
|
+
if (config.expose === true) {
|
|
366
|
+
enabledTargets.set(name, target);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
for (const [name, config] of Object.entries(env.apps)) {
|
|
370
|
+
const port = env.ports[name];
|
|
371
|
+
if (port === undefined)
|
|
372
|
+
continue;
|
|
373
|
+
const target = { kind: "app", name, port };
|
|
374
|
+
knownTargets.set(name, target);
|
|
375
|
+
if (config.expose === true) {
|
|
376
|
+
enabledTargets.set(name, target);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (requestedNames === null) {
|
|
380
|
+
return {
|
|
381
|
+
targets: Array.from(enabledTargets.values()),
|
|
382
|
+
unknownNames: [],
|
|
383
|
+
notEnabledNames: []
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
const unknownNames = [];
|
|
387
|
+
const notEnabledNames = [];
|
|
388
|
+
const targets = [];
|
|
389
|
+
for (const name of requestedNames) {
|
|
390
|
+
if (!knownTargets.has(name)) {
|
|
391
|
+
unknownNames.push(name);
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
const enabledTarget = enabledTargets.get(name);
|
|
395
|
+
if (!enabledTarget) {
|
|
396
|
+
notEnabledNames.push(name);
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
targets.push(enabledTarget);
|
|
400
|
+
}
|
|
401
|
+
return { targets, unknownNames, notEnabledNames };
|
|
402
|
+
}
|
|
403
|
+
function resolveExposeTunnelStaggerMs() {
|
|
404
|
+
const raw = process.env.BUNCARGO_EXPOSE_TUNNEL_STAGGER_MS;
|
|
405
|
+
if (raw === undefined || raw === "") {
|
|
406
|
+
return 900;
|
|
407
|
+
}
|
|
408
|
+
const n = Number.parseInt(raw, 10);
|
|
409
|
+
return Number.isFinite(n) && n >= 0 ? n : 900;
|
|
410
|
+
}
|
|
411
|
+
async function startPublicTunnels(targets, options = {}) {
|
|
412
|
+
const start = options.start ?? ((input) => startQuickTunnel(input));
|
|
413
|
+
const staggerMs = resolveExposeTunnelStaggerMs();
|
|
414
|
+
const tunnels = [];
|
|
415
|
+
try {
|
|
416
|
+
let index = 0;
|
|
417
|
+
for (const target of targets) {
|
|
418
|
+
if (index > 0 && staggerMs > 0) {
|
|
419
|
+
await sleep(staggerMs);
|
|
420
|
+
}
|
|
421
|
+
index += 1;
|
|
422
|
+
const localUrl = `http://localhost:${target.port}`;
|
|
423
|
+
const tunnel = await start({
|
|
424
|
+
url: localUrl
|
|
425
|
+
});
|
|
426
|
+
if (tunnel === undefined) {
|
|
427
|
+
throw new Error(`Tunnel for "${target.name}" could not be started (tunnel backend returned no instance)`);
|
|
428
|
+
}
|
|
429
|
+
const publicUrl = await resolvePublicUrl(tunnel);
|
|
430
|
+
if (!publicUrl) {
|
|
431
|
+
throw new Error(`Tunnel for "${target.name}" did not provide a public URL`);
|
|
432
|
+
}
|
|
433
|
+
tunnels.push({
|
|
434
|
+
kind: target.kind,
|
|
435
|
+
name: target.name,
|
|
436
|
+
localUrl,
|
|
437
|
+
publicUrl,
|
|
438
|
+
close: toCloseFn(tunnel)
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
return tunnels;
|
|
442
|
+
} catch (e) {
|
|
443
|
+
await stopPublicTunnels(tunnels);
|
|
444
|
+
throw e;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
async function stopPublicTunnels(tunnels) {
|
|
448
|
+
await Promise.allSettled(tunnels.map((tunnel) => tunnel.close()));
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
export { resolveExposeTargets, startPublicTunnels, stopPublicTunnels };
|