buncargo 3.0.0 → 3.2.0
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 -8
- package/dist/cli/index.js +2 -2
- package/dist/cli/run-cli.d.ts +10 -2
- package/dist/core/quick-tunnel/cloudflared-process.d.ts +10 -0
- package/dist/core/quick-tunnel/constants.d.ts +9 -0
- package/dist/core/quick-tunnel/index.d.ts +17 -0
- package/dist/core/quick-tunnel/install.d.ts +1 -0
- package/dist/core/tunnel.d.ts +3 -2
- package/dist/environment/index.js +2 -2
- package/dist/environment/logging.d.ts +6 -6
- package/dist/environment/only-apps.d.ts +10 -0
- package/dist/index-3eyrdxw9.js +577 -0
- package/dist/index-5aq985p4.js +250 -0
- package/dist/index-6cmex7m5.js +72 -0
- package/dist/index-6d6x175r.js +572 -0
- package/dist/index-7v19es2e.js +666 -0
- package/dist/index-9wyhzw0h.js +574 -0
- package/dist/index-ag90ry8t.js +576 -0
- package/dist/index-byeqyjrz.js +72 -0
- package/dist/index-enj4zdma.js +574 -0
- package/dist/index-k370bech.js +72 -0
- package/dist/index-qa8akv6y.js +666 -0
- package/dist/index-vg55rq0y.js +250 -0
- package/dist/index-vs81yaks.js +244 -0
- package/dist/index-x54nbgs7.js +355 -0
- package/dist/index-yz4jfz7z.js +338 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -8
- package/dist/loader/index.js +3 -3
- package/dist/types/all-types.d.ts +46 -3
- package/package.json +147 -145
- package/readme.md +16 -0
- package/src/cli/run-cli.ts +27 -12
- package/src/core/quick-tunnel/cloudflared-process.ts +83 -0
- package/src/core/quick-tunnel/constants.ts +31 -0
- package/src/core/quick-tunnel/index.ts +96 -0
- package/src/core/quick-tunnel/install.ts +160 -0
- package/src/core/tunnel.ts +22 -8
- package/src/environment/create-dev-environment.ts +123 -13
- package/src/environment/logging.ts +34 -20
- package/src/environment/only-apps.ts +34 -0
- package/src/index.ts +3 -0
- package/src/types/all-types.ts +56 -3
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
// src/core/quick-tunnel/index.ts
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { createInterface } from "node:readline";
|
|
4
|
+
|
|
5
|
+
// src/core/quick-tunnel/cloudflared-process.ts
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
|
|
8
|
+
// src/core/quick-tunnel/constants.ts
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
var CLOUDFLARED_VERSION = process.env.CLOUDFLARED_VERSION || "2023.10.0";
|
|
12
|
+
var RELEASE_BASE = "https://github.com/cloudflare/cloudflared/releases/";
|
|
13
|
+
var cloudflaredBinPath = path.join(tmpdir(), "buncargo-cloudflared", process.platform === "win32" ? `cloudflared.${CLOUDFLARED_VERSION}.exe` : `cloudflared.${CLOUDFLARED_VERSION}`);
|
|
14
|
+
var cloudflaredNotice = `
|
|
15
|
+
\uD83D\uDD25 Your installation of cloudflared software constitutes a symbol of your signature
|
|
16
|
+
indicating that you accept the terms of the Cloudflare License, Terms and Privacy Policy.
|
|
17
|
+
|
|
18
|
+
❯ License: \`https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/license/\`
|
|
19
|
+
❯ Terms: \`https://www.cloudflare.com/terms/\`
|
|
20
|
+
❯ Privacy Policy: \`https://www.cloudflare.com/privacypolicy/\`
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
// src/core/quick-tunnel/cloudflared-process.ts
|
|
24
|
+
var urlRegex = /\|\s+(https?:\/\/\S+)/;
|
|
25
|
+
function startCloudflaredTunnel(options) {
|
|
26
|
+
const args = ["tunnel"];
|
|
27
|
+
for (const [key, value] of Object.entries(options)) {
|
|
28
|
+
if (typeof value === "string") {
|
|
29
|
+
args.push(`${key}`, value);
|
|
30
|
+
} else if (typeof value === "number") {
|
|
31
|
+
args.push(`${key}`, value.toString());
|
|
32
|
+
} else if (value === null) {
|
|
33
|
+
args.push(`${key}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (args.length === 1) {
|
|
37
|
+
args.push("--url", "localhost:8080");
|
|
38
|
+
}
|
|
39
|
+
const child = spawn(cloudflaredBinPath, args, {
|
|
40
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
41
|
+
});
|
|
42
|
+
if (process.env.DEBUG) {
|
|
43
|
+
child.stdout?.pipe(process.stdout);
|
|
44
|
+
child.stderr?.pipe(process.stderr);
|
|
45
|
+
}
|
|
46
|
+
let settled = false;
|
|
47
|
+
let urlResolver;
|
|
48
|
+
let urlRejector;
|
|
49
|
+
const url = new Promise((resolve, reject) => {
|
|
50
|
+
urlResolver = (v) => {
|
|
51
|
+
if (!settled) {
|
|
52
|
+
settled = true;
|
|
53
|
+
resolve(v);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
urlRejector = (e) => {
|
|
57
|
+
if (!settled) {
|
|
58
|
+
settled = true;
|
|
59
|
+
reject(e);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
const parser = (data) => {
|
|
64
|
+
const str = data.toString();
|
|
65
|
+
const urlMatch = str.match(urlRegex);
|
|
66
|
+
if (urlMatch) {
|
|
67
|
+
urlResolver(urlMatch[1] ?? "");
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
child.stdout?.on("data", parser).on("error", urlRejector);
|
|
71
|
+
child.stderr?.on("data", parser).on("error", urlRejector);
|
|
72
|
+
child.on("exit", (code, signal) => {
|
|
73
|
+
if (!settled) {
|
|
74
|
+
urlRejector(new Error(`cloudflared exited before a tunnel URL was parsed (code=${code}, signal=${signal ?? "none"})`));
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
child.on("error", urlRejector);
|
|
78
|
+
const stop = () => child.kill("SIGINT");
|
|
79
|
+
return { url, child, stop };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/core/quick-tunnel/install.ts
|
|
83
|
+
import { execSync } from "node:child_process";
|
|
84
|
+
import fs from "node:fs";
|
|
85
|
+
import https from "node:https";
|
|
86
|
+
import path2 from "node:path";
|
|
87
|
+
var LINUX_URL = {
|
|
88
|
+
arm64: "cloudflared-linux-arm64",
|
|
89
|
+
arm: "cloudflared-linux-arm",
|
|
90
|
+
x64: "cloudflared-linux-amd64",
|
|
91
|
+
ia32: "cloudflared-linux-386"
|
|
92
|
+
};
|
|
93
|
+
var MACOS_URL = {
|
|
94
|
+
arm64: "cloudflared-darwin-amd64.tgz",
|
|
95
|
+
x64: "cloudflared-darwin-amd64.tgz"
|
|
96
|
+
};
|
|
97
|
+
var WINDOWS_URL = {
|
|
98
|
+
x64: "cloudflared-windows-amd64.exe",
|
|
99
|
+
ia32: "cloudflared-windows-386.exe"
|
|
100
|
+
};
|
|
101
|
+
function resolveBase(version) {
|
|
102
|
+
if (version === "latest") {
|
|
103
|
+
return `${RELEASE_BASE}latest/download/`;
|
|
104
|
+
}
|
|
105
|
+
return `${RELEASE_BASE}download/${version}/`;
|
|
106
|
+
}
|
|
107
|
+
function installCloudflared(to = cloudflaredBinPath, version = CLOUDFLARED_VERSION) {
|
|
108
|
+
switch (process.platform) {
|
|
109
|
+
case "linux": {
|
|
110
|
+
return installLinux(to, version);
|
|
111
|
+
}
|
|
112
|
+
case "darwin": {
|
|
113
|
+
return installMacos(to, version);
|
|
114
|
+
}
|
|
115
|
+
case "win32": {
|
|
116
|
+
return installWindows(to, version);
|
|
117
|
+
}
|
|
118
|
+
default: {
|
|
119
|
+
throw new Error(`Unsupported platform: ${process.platform}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async function installLinux(to, version = CLOUDFLARED_VERSION) {
|
|
124
|
+
const file = LINUX_URL[process.arch];
|
|
125
|
+
if (file === undefined) {
|
|
126
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
127
|
+
}
|
|
128
|
+
await download(resolveBase(version) + file, to);
|
|
129
|
+
fs.chmodSync(to, 493);
|
|
130
|
+
return to;
|
|
131
|
+
}
|
|
132
|
+
async function installMacos(to, version = CLOUDFLARED_VERSION) {
|
|
133
|
+
const file = MACOS_URL[process.arch];
|
|
134
|
+
if (file === undefined) {
|
|
135
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
136
|
+
}
|
|
137
|
+
await download(resolveBase(version) + file, `${to}.tgz`);
|
|
138
|
+
if (process.env.DEBUG) {
|
|
139
|
+
console.log(`Extracting to ${to}`);
|
|
140
|
+
}
|
|
141
|
+
execSync(`tar -xzf ${path2.basename(`${to}.tgz`)}`, {
|
|
142
|
+
cwd: path2.dirname(to)
|
|
143
|
+
});
|
|
144
|
+
fs.unlinkSync(`${to}.tgz`);
|
|
145
|
+
fs.renameSync(`${path2.dirname(to)}/cloudflared`, to);
|
|
146
|
+
return to;
|
|
147
|
+
}
|
|
148
|
+
async function installWindows(to, version = CLOUDFLARED_VERSION) {
|
|
149
|
+
const file = WINDOWS_URL[process.arch];
|
|
150
|
+
if (file === undefined) {
|
|
151
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
152
|
+
}
|
|
153
|
+
await download(resolveBase(version) + file, to);
|
|
154
|
+
return to;
|
|
155
|
+
}
|
|
156
|
+
function download(url, to, redirect = 0) {
|
|
157
|
+
if (redirect === 0) {
|
|
158
|
+
if (process.env.DEBUG) {
|
|
159
|
+
console.log(`Downloading ${url} to ${to}`);
|
|
160
|
+
}
|
|
161
|
+
} else if (process.env.DEBUG) {
|
|
162
|
+
console.log(`Redirecting to ${url}`);
|
|
163
|
+
}
|
|
164
|
+
return new Promise((resolve, reject) => {
|
|
165
|
+
if (!fs.existsSync(path2.dirname(to))) {
|
|
166
|
+
fs.mkdirSync(path2.dirname(to), { recursive: true });
|
|
167
|
+
}
|
|
168
|
+
let done = true;
|
|
169
|
+
const file = fs.createWriteStream(to);
|
|
170
|
+
const request = https.get(url, (res) => {
|
|
171
|
+
if (res.statusCode === 302 && res.headers.location !== undefined) {
|
|
172
|
+
const redirection = res.headers.location;
|
|
173
|
+
done = false;
|
|
174
|
+
file.close(() => {
|
|
175
|
+
download(redirection, to, redirect + 1).then(resolve, reject);
|
|
176
|
+
});
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
res.pipe(file);
|
|
180
|
+
});
|
|
181
|
+
file.on("finish", () => {
|
|
182
|
+
if (done) {
|
|
183
|
+
file.close(() => {
|
|
184
|
+
resolve(to);
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
request.on("error", (err) => {
|
|
189
|
+
fs.unlink(to, () => {
|
|
190
|
+
reject(err);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
file.on("error", (err) => {
|
|
194
|
+
fs.unlink(to, () => {
|
|
195
|
+
reject(err);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
request.end();
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// src/core/quick-tunnel/index.ts
|
|
203
|
+
function resolvedLocalUrl(opts) {
|
|
204
|
+
return opts.url ?? `${opts.protocol || "http"}://${opts.hostname ?? "localhost"}:${opts.port ?? 3000}`;
|
|
205
|
+
}
|
|
206
|
+
function envAcceptsCloudflareNotice() {
|
|
207
|
+
const v = process.env.BUNCARGO_ACCEPT_CLOUDFLARE_NOTICE;
|
|
208
|
+
const u = process.env.UNTUN_ACCEPT_CLOUDFLARE_NOTICE;
|
|
209
|
+
return v === "1" || v === "true" || u === "1" || u === "true";
|
|
210
|
+
}
|
|
211
|
+
async function promptInstallCloudflared() {
|
|
212
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
return new Promise((resolve) => {
|
|
216
|
+
const rl = createInterface({
|
|
217
|
+
input: process.stdin,
|
|
218
|
+
output: process.stdout
|
|
219
|
+
});
|
|
220
|
+
rl.question("Do you agree with the above terms and wish to install the binary from GitHub? (y/N) ", (answer) => {
|
|
221
|
+
rl.close();
|
|
222
|
+
resolve(/^y(es)?$/i.test(answer.trim()));
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
async function startQuickTunnel(opts) {
|
|
227
|
+
const url = resolvedLocalUrl(opts);
|
|
228
|
+
console.log(`Starting cloudflared tunnel to ${url}`);
|
|
229
|
+
if (!existsSync(cloudflaredBinPath)) {
|
|
230
|
+
console.log(cloudflaredNotice);
|
|
231
|
+
const canInstall = opts.acceptCloudflareNotice || envAcceptsCloudflareNotice() || await promptInstallCloudflared();
|
|
232
|
+
if (!canInstall) {
|
|
233
|
+
console.error("Skipping tunnel setup.");
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
await installCloudflared();
|
|
237
|
+
}
|
|
238
|
+
const cfArgs = { "--url": url };
|
|
239
|
+
if (!opts.verifyTLS) {
|
|
240
|
+
cfArgs["--no-tls-verify"] = null;
|
|
241
|
+
}
|
|
242
|
+
const tunnel = startCloudflaredTunnel(cfArgs);
|
|
243
|
+
const cleanup = async () => {
|
|
244
|
+
tunnel.stop();
|
|
245
|
+
};
|
|
246
|
+
return {
|
|
247
|
+
getURL: async () => await tunnel.url,
|
|
248
|
+
close: cleanup
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// src/core/tunnel.ts
|
|
253
|
+
function parseExposeNames(exposeValue) {
|
|
254
|
+
if (exposeValue === undefined)
|
|
255
|
+
return null;
|
|
256
|
+
const names = exposeValue.split(",").map((name) => name.trim()).filter(Boolean);
|
|
257
|
+
return new Set(names);
|
|
258
|
+
}
|
|
259
|
+
async function resolvePublicUrl(tunnel) {
|
|
260
|
+
if (typeof tunnel.getURL === "function") {
|
|
261
|
+
return await tunnel.getURL();
|
|
262
|
+
}
|
|
263
|
+
return tunnel.url ?? tunnel.publicUrl ?? tunnel.tunnelUrl ?? null;
|
|
264
|
+
}
|
|
265
|
+
function toCloseFn(tunnel) {
|
|
266
|
+
const close = tunnel.close ?? tunnel.stop ?? tunnel.destroy;
|
|
267
|
+
if (!close)
|
|
268
|
+
return async () => {};
|
|
269
|
+
return async () => {
|
|
270
|
+
await close();
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function resolveExposeTargets(env, exposeValue) {
|
|
274
|
+
const requestedNames = parseExposeNames(exposeValue);
|
|
275
|
+
const knownTargets = new Map;
|
|
276
|
+
const enabledTargets = new Map;
|
|
277
|
+
for (const [name, config] of Object.entries(env.services)) {
|
|
278
|
+
const port = env.ports[name];
|
|
279
|
+
if (port === undefined)
|
|
280
|
+
continue;
|
|
281
|
+
const target = { kind: "service", name, port };
|
|
282
|
+
knownTargets.set(name, target);
|
|
283
|
+
if (config.expose === true) {
|
|
284
|
+
enabledTargets.set(name, target);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
for (const [name, config] of Object.entries(env.apps)) {
|
|
288
|
+
const port = env.ports[name];
|
|
289
|
+
if (port === undefined)
|
|
290
|
+
continue;
|
|
291
|
+
const target = { kind: "app", name, port };
|
|
292
|
+
knownTargets.set(name, target);
|
|
293
|
+
if (config.expose === true) {
|
|
294
|
+
enabledTargets.set(name, target);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (requestedNames === null) {
|
|
298
|
+
return {
|
|
299
|
+
targets: Array.from(enabledTargets.values()),
|
|
300
|
+
unknownNames: [],
|
|
301
|
+
notEnabledNames: []
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
const unknownNames = [];
|
|
305
|
+
const notEnabledNames = [];
|
|
306
|
+
const targets = [];
|
|
307
|
+
for (const name of requestedNames) {
|
|
308
|
+
if (!knownTargets.has(name)) {
|
|
309
|
+
unknownNames.push(name);
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
const enabledTarget = enabledTargets.get(name);
|
|
313
|
+
if (!enabledTarget) {
|
|
314
|
+
notEnabledNames.push(name);
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
targets.push(enabledTarget);
|
|
318
|
+
}
|
|
319
|
+
return { targets, unknownNames, notEnabledNames };
|
|
320
|
+
}
|
|
321
|
+
async function startPublicTunnels(targets, options = {}) {
|
|
322
|
+
const start = options.start ?? ((input) => startQuickTunnel(input));
|
|
323
|
+
const tunnels = [];
|
|
324
|
+
try {
|
|
325
|
+
for (const target of targets) {
|
|
326
|
+
const localUrl = `http://localhost:${target.port}`;
|
|
327
|
+
const tunnel = await start({
|
|
328
|
+
url: localUrl
|
|
329
|
+
});
|
|
330
|
+
if (tunnel === undefined) {
|
|
331
|
+
throw new Error(`Tunnel for "${target.name}" could not be started (cloudflared missing or install declined)`);
|
|
332
|
+
}
|
|
333
|
+
const publicUrl = await resolvePublicUrl(tunnel);
|
|
334
|
+
if (!publicUrl) {
|
|
335
|
+
throw new Error(`Tunnel for "${target.name}" did not provide a public URL`);
|
|
336
|
+
}
|
|
337
|
+
tunnels.push({
|
|
338
|
+
kind: target.kind,
|
|
339
|
+
name: target.name,
|
|
340
|
+
localUrl,
|
|
341
|
+
publicUrl,
|
|
342
|
+
close: toCloseFn(tunnel)
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
return tunnels;
|
|
346
|
+
} catch (error) {
|
|
347
|
+
await stopPublicTunnels(tunnels);
|
|
348
|
+
throw error;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
async function stopPublicTunnels(tunnels) {
|
|
352
|
+
await Promise.allSettled(tunnels.map((tunnel) => tunnel.close()));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export { resolveExposeTargets, startPublicTunnels, stopPublicTunnels };
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
// src/core/quick-tunnel/index.ts
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { createInterface } from "node:readline";
|
|
4
|
+
|
|
5
|
+
// src/core/quick-tunnel/cloudflared-process.ts
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
|
|
8
|
+
// src/core/quick-tunnel/constants.ts
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
var CLOUDFLARED_VERSION = process.env.CLOUDFLARED_VERSION || "2023.10.0";
|
|
12
|
+
var RELEASE_BASE = "https://github.com/cloudflare/cloudflared/releases/";
|
|
13
|
+
var cloudflaredBinPath = path.join(tmpdir(), "buncargo-cloudflared", process.platform === "win32" ? `cloudflared.${CLOUDFLARED_VERSION}.exe` : `cloudflared.${CLOUDFLARED_VERSION}`);
|
|
14
|
+
var cloudflaredNotice = `
|
|
15
|
+
\uD83D\uDD25 Your installation of cloudflared software constitutes a symbol of your signature
|
|
16
|
+
indicating that you accept the terms of the Cloudflare License, Terms and Privacy Policy.
|
|
17
|
+
|
|
18
|
+
❯ License: \`https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/license/\`
|
|
19
|
+
❯ Terms: \`https://www.cloudflare.com/terms/\`
|
|
20
|
+
❯ Privacy Policy: \`https://www.cloudflare.com/privacypolicy/\`
|
|
21
|
+
`;
|
|
22
|
+
|
|
23
|
+
// src/core/quick-tunnel/cloudflared-process.ts
|
|
24
|
+
var urlRegex = /\|\s+(https?:\/\/\S+)/;
|
|
25
|
+
function startCloudflaredTunnel(options) {
|
|
26
|
+
const args = ["tunnel"];
|
|
27
|
+
for (const [key, value] of Object.entries(options)) {
|
|
28
|
+
if (typeof value === "string") {
|
|
29
|
+
args.push(`${key}`, value);
|
|
30
|
+
} else if (typeof value === "number") {
|
|
31
|
+
args.push(`${key}`, value.toString());
|
|
32
|
+
} else if (value === null) {
|
|
33
|
+
args.push(`${key}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (args.length === 1) {
|
|
37
|
+
args.push("--url", "localhost:8080");
|
|
38
|
+
}
|
|
39
|
+
const child = spawn(cloudflaredBinPath, args, {
|
|
40
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
41
|
+
});
|
|
42
|
+
if (process.env.DEBUG) {
|
|
43
|
+
child.stdout?.pipe(process.stdout);
|
|
44
|
+
child.stderr?.pipe(process.stderr);
|
|
45
|
+
}
|
|
46
|
+
let urlResolver;
|
|
47
|
+
let urlRejector;
|
|
48
|
+
const url = new Promise((resolve, reject) => {
|
|
49
|
+
urlResolver = resolve;
|
|
50
|
+
urlRejector = reject;
|
|
51
|
+
});
|
|
52
|
+
const parser = (data) => {
|
|
53
|
+
const str = data.toString();
|
|
54
|
+
const urlMatch = str.match(urlRegex);
|
|
55
|
+
if (urlMatch) {
|
|
56
|
+
urlResolver(urlMatch[1] ?? "");
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
child.stdout?.on("data", parser).on("error", urlRejector);
|
|
60
|
+
child.stderr?.on("data", parser).on("error", urlRejector);
|
|
61
|
+
const stop = () => child.kill("SIGINT");
|
|
62
|
+
return { url, child, stop };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/core/quick-tunnel/install.ts
|
|
66
|
+
import { execSync } from "node:child_process";
|
|
67
|
+
import fs from "node:fs";
|
|
68
|
+
import https from "node:https";
|
|
69
|
+
import path2 from "node:path";
|
|
70
|
+
var LINUX_URL = {
|
|
71
|
+
arm64: "cloudflared-linux-arm64",
|
|
72
|
+
arm: "cloudflared-linux-arm",
|
|
73
|
+
x64: "cloudflared-linux-amd64",
|
|
74
|
+
ia32: "cloudflared-linux-386"
|
|
75
|
+
};
|
|
76
|
+
var MACOS_URL = {
|
|
77
|
+
arm64: "cloudflared-darwin-amd64.tgz",
|
|
78
|
+
x64: "cloudflared-darwin-amd64.tgz"
|
|
79
|
+
};
|
|
80
|
+
var WINDOWS_URL = {
|
|
81
|
+
x64: "cloudflared-windows-amd64.exe",
|
|
82
|
+
ia32: "cloudflared-windows-386.exe"
|
|
83
|
+
};
|
|
84
|
+
function resolveBase(version) {
|
|
85
|
+
if (version === "latest") {
|
|
86
|
+
return `${RELEASE_BASE}latest/download/`;
|
|
87
|
+
}
|
|
88
|
+
return `${RELEASE_BASE}download/${version}/`;
|
|
89
|
+
}
|
|
90
|
+
function installCloudflared(to = cloudflaredBinPath, version = CLOUDFLARED_VERSION) {
|
|
91
|
+
switch (process.platform) {
|
|
92
|
+
case "linux": {
|
|
93
|
+
return installLinux(to, version);
|
|
94
|
+
}
|
|
95
|
+
case "darwin": {
|
|
96
|
+
return installMacos(to, version);
|
|
97
|
+
}
|
|
98
|
+
case "win32": {
|
|
99
|
+
return installWindows(to, version);
|
|
100
|
+
}
|
|
101
|
+
default: {
|
|
102
|
+
throw new Error(`Unsupported platform: ${process.platform}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async function installLinux(to, version = CLOUDFLARED_VERSION) {
|
|
107
|
+
const file = LINUX_URL[process.arch];
|
|
108
|
+
if (file === undefined) {
|
|
109
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
110
|
+
}
|
|
111
|
+
await download(resolveBase(version) + file, to);
|
|
112
|
+
fs.chmodSync(to, 493);
|
|
113
|
+
return to;
|
|
114
|
+
}
|
|
115
|
+
async function installMacos(to, version = CLOUDFLARED_VERSION) {
|
|
116
|
+
const file = MACOS_URL[process.arch];
|
|
117
|
+
if (file === undefined) {
|
|
118
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
119
|
+
}
|
|
120
|
+
await download(resolveBase(version) + file, `${to}.tgz`);
|
|
121
|
+
if (process.env.DEBUG) {
|
|
122
|
+
console.log(`Extracting to ${to}`);
|
|
123
|
+
}
|
|
124
|
+
execSync(`tar -xzf ${path2.basename(`${to}.tgz`)}`, {
|
|
125
|
+
cwd: path2.dirname(to)
|
|
126
|
+
});
|
|
127
|
+
fs.unlinkSync(`${to}.tgz`);
|
|
128
|
+
fs.renameSync(`${path2.dirname(to)}/cloudflared`, to);
|
|
129
|
+
return to;
|
|
130
|
+
}
|
|
131
|
+
async function installWindows(to, version = CLOUDFLARED_VERSION) {
|
|
132
|
+
const file = WINDOWS_URL[process.arch];
|
|
133
|
+
if (file === undefined) {
|
|
134
|
+
throw new Error(`Unsupported architecture: ${process.arch}`);
|
|
135
|
+
}
|
|
136
|
+
await download(resolveBase(version) + file, to);
|
|
137
|
+
return to;
|
|
138
|
+
}
|
|
139
|
+
function download(url, to, redirect = 0) {
|
|
140
|
+
if (redirect === 0) {
|
|
141
|
+
if (process.env.DEBUG) {
|
|
142
|
+
console.log(`Downloading ${url} to ${to}`);
|
|
143
|
+
}
|
|
144
|
+
} else if (process.env.DEBUG) {
|
|
145
|
+
console.log(`Redirecting to ${url}`);
|
|
146
|
+
}
|
|
147
|
+
return new Promise((resolve, reject) => {
|
|
148
|
+
if (!fs.existsSync(path2.dirname(to))) {
|
|
149
|
+
fs.mkdirSync(path2.dirname(to), { recursive: true });
|
|
150
|
+
}
|
|
151
|
+
let done = true;
|
|
152
|
+
const file = fs.createWriteStream(to);
|
|
153
|
+
const request = https.get(url, (res) => {
|
|
154
|
+
if (res.statusCode === 302 && res.headers.location !== undefined) {
|
|
155
|
+
const redirection = res.headers.location;
|
|
156
|
+
done = false;
|
|
157
|
+
file.close(() => {
|
|
158
|
+
download(redirection, to, redirect + 1).then(resolve, reject);
|
|
159
|
+
});
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
res.pipe(file);
|
|
163
|
+
});
|
|
164
|
+
file.on("finish", () => {
|
|
165
|
+
if (done) {
|
|
166
|
+
file.close(() => {
|
|
167
|
+
resolve(to);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
request.on("error", (err) => {
|
|
172
|
+
fs.unlink(to, () => {
|
|
173
|
+
reject(err);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
file.on("error", (err) => {
|
|
177
|
+
fs.unlink(to, () => {
|
|
178
|
+
reject(err);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
request.end();
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// src/core/quick-tunnel/index.ts
|
|
186
|
+
function resolvedLocalUrl(opts) {
|
|
187
|
+
return opts.url ?? `${opts.protocol || "http"}://${opts.hostname ?? "localhost"}:${opts.port ?? 3000}`;
|
|
188
|
+
}
|
|
189
|
+
function envAcceptsCloudflareNotice() {
|
|
190
|
+
const v = process.env.BUNCARGO_ACCEPT_CLOUDFLARE_NOTICE;
|
|
191
|
+
const u = process.env.UNTUN_ACCEPT_CLOUDFLARE_NOTICE;
|
|
192
|
+
return v === "1" || v === "true" || u === "1" || u === "true";
|
|
193
|
+
}
|
|
194
|
+
async function promptInstallCloudflared() {
|
|
195
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
return new Promise((resolve) => {
|
|
199
|
+
const rl = createInterface({
|
|
200
|
+
input: process.stdin,
|
|
201
|
+
output: process.stdout
|
|
202
|
+
});
|
|
203
|
+
rl.question("Do you agree with the above terms and wish to install the binary from GitHub? (y/N) ", (answer) => {
|
|
204
|
+
rl.close();
|
|
205
|
+
resolve(/^y(es)?$/i.test(answer.trim()));
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
async function startQuickTunnel(opts) {
|
|
210
|
+
const url = resolvedLocalUrl(opts);
|
|
211
|
+
console.log(`Starting cloudflared tunnel to ${url}`);
|
|
212
|
+
if (!existsSync(cloudflaredBinPath)) {
|
|
213
|
+
console.log(cloudflaredNotice);
|
|
214
|
+
const canInstall = opts.acceptCloudflareNotice || envAcceptsCloudflareNotice() || await promptInstallCloudflared();
|
|
215
|
+
if (!canInstall) {
|
|
216
|
+
console.error("Skipping tunnel setup.");
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
await installCloudflared();
|
|
220
|
+
}
|
|
221
|
+
const argEntries = [["--url", url]];
|
|
222
|
+
if (!opts.verifyTLS) {
|
|
223
|
+
argEntries.push(["--no-tls-verify", ""]);
|
|
224
|
+
}
|
|
225
|
+
const tunnel = startCloudflaredTunnel(Object.fromEntries(argEntries));
|
|
226
|
+
const cleanup = async () => {
|
|
227
|
+
tunnel.stop();
|
|
228
|
+
};
|
|
229
|
+
return {
|
|
230
|
+
getURL: async () => await tunnel.url,
|
|
231
|
+
close: cleanup
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// src/core/tunnel.ts
|
|
236
|
+
function parseExposeNames(exposeValue) {
|
|
237
|
+
if (exposeValue === undefined)
|
|
238
|
+
return null;
|
|
239
|
+
const names = exposeValue.split(",").map((name) => name.trim()).filter(Boolean);
|
|
240
|
+
return new Set(names);
|
|
241
|
+
}
|
|
242
|
+
async function resolvePublicUrl(tunnel) {
|
|
243
|
+
if (typeof tunnel.getURL === "function") {
|
|
244
|
+
return await tunnel.getURL();
|
|
245
|
+
}
|
|
246
|
+
return tunnel.url ?? tunnel.publicUrl ?? tunnel.tunnelUrl ?? null;
|
|
247
|
+
}
|
|
248
|
+
function toCloseFn(tunnel) {
|
|
249
|
+
const close = tunnel.close ?? tunnel.stop ?? tunnel.destroy;
|
|
250
|
+
if (!close)
|
|
251
|
+
return async () => {};
|
|
252
|
+
return async () => {
|
|
253
|
+
await close();
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
function resolveExposeTargets(env, exposeValue) {
|
|
257
|
+
const requestedNames = parseExposeNames(exposeValue);
|
|
258
|
+
const knownTargets = new Map;
|
|
259
|
+
const enabledTargets = new Map;
|
|
260
|
+
for (const [name, config] of Object.entries(env.services)) {
|
|
261
|
+
const port = env.ports[name];
|
|
262
|
+
if (port === undefined)
|
|
263
|
+
continue;
|
|
264
|
+
const target = { kind: "service", name, port };
|
|
265
|
+
knownTargets.set(name, target);
|
|
266
|
+
if (config.expose === true) {
|
|
267
|
+
enabledTargets.set(name, target);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
for (const [name, config] of Object.entries(env.apps)) {
|
|
271
|
+
const port = env.ports[name];
|
|
272
|
+
if (port === undefined)
|
|
273
|
+
continue;
|
|
274
|
+
const target = { kind: "app", name, port };
|
|
275
|
+
knownTargets.set(name, target);
|
|
276
|
+
if (config.expose === true) {
|
|
277
|
+
enabledTargets.set(name, target);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (requestedNames === null) {
|
|
281
|
+
return {
|
|
282
|
+
targets: Array.from(enabledTargets.values()),
|
|
283
|
+
unknownNames: [],
|
|
284
|
+
notEnabledNames: []
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
const unknownNames = [];
|
|
288
|
+
const notEnabledNames = [];
|
|
289
|
+
const targets = [];
|
|
290
|
+
for (const name of requestedNames) {
|
|
291
|
+
if (!knownTargets.has(name)) {
|
|
292
|
+
unknownNames.push(name);
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
const enabledTarget = enabledTargets.get(name);
|
|
296
|
+
if (!enabledTarget) {
|
|
297
|
+
notEnabledNames.push(name);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
targets.push(enabledTarget);
|
|
301
|
+
}
|
|
302
|
+
return { targets, unknownNames, notEnabledNames };
|
|
303
|
+
}
|
|
304
|
+
async function startPublicTunnels(targets, options = {}) {
|
|
305
|
+
const start = options.start ?? ((input) => startQuickTunnel(input));
|
|
306
|
+
const tunnels = [];
|
|
307
|
+
try {
|
|
308
|
+
for (const target of targets) {
|
|
309
|
+
const localUrl = `http://localhost:${target.port}`;
|
|
310
|
+
const tunnel = await start({
|
|
311
|
+
url: localUrl
|
|
312
|
+
});
|
|
313
|
+
if (tunnel === undefined) {
|
|
314
|
+
throw new Error(`Tunnel for "${target.name}" could not be started (cloudflared missing or install declined)`);
|
|
315
|
+
}
|
|
316
|
+
const publicUrl = await resolvePublicUrl(tunnel);
|
|
317
|
+
if (!publicUrl) {
|
|
318
|
+
throw new Error(`Tunnel for "${target.name}" did not provide a public URL`);
|
|
319
|
+
}
|
|
320
|
+
tunnels.push({
|
|
321
|
+
kind: target.kind,
|
|
322
|
+
name: target.name,
|
|
323
|
+
localUrl,
|
|
324
|
+
publicUrl,
|
|
325
|
+
close: toCloseFn(tunnel)
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
return tunnels;
|
|
329
|
+
} catch (error) {
|
|
330
|
+
await stopPublicTunnels(tunnels);
|
|
331
|
+
throw error;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
async function stopPublicTunnels(tunnels) {
|
|
335
|
+
await Promise.allSettled(tunnels.map((tunnel) => tunnel.close()));
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export { resolveExposeTargets, startPublicTunnels, stopPublicTunnels };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export { service } from "./docker-compose/services";
|
|
|
5
5
|
export { createDevEnvironment } from "./environment/index";
|
|
6
6
|
export { clearDevEnvCache, getDevEnv, loadDevEnv } from "./loader/index";
|
|
7
7
|
export { runWorkspaceTypecheck, type TypecheckResult, type WorkspaceTypecheckOptions, type WorkspaceTypecheckResult, } from "./typecheck/index";
|
|
8
|
-
export type { AppConfig, BuiltInHealthCheck, CliOptions, ComputedPorts, ComputedPublicUrls, ComputedUrls, DevConfig, DevEnvironment, DevHooks, DevOptions, DevServerPids, DockerComposeGenerationOptions, DockerComposeHealthcheckRaw, DockerComposeNode, DockerComposeServiceRaw, DockerComposeVolumeRaw, DockerPresetName, DockerPresetServiceDefinition, DockerServiceDefinition, EnvVarsBuilder, ExecOptions, HealthCheckFn, HookContext, MigrationConfig, PrismaConfig, PrismaRunner, SeedCheckContext, SeedCheckHelpers, SeedConfig, ServiceConfig, StartOptions, StopOptions, UrlBuilderContext, UrlBuilderFn, } from "./types/index";
|
|
8
|
+
export type { AppConfig, BuiltInHealthCheck, CliOptions, ComputedPorts, ComputedPublicUrls, ComputedUrls, DevConfig, DevEnvironment, DevHooks, DevOptions, DevServerPids, DockerComposeGenerationOptions, DockerComposeHealthcheckRaw, DockerComposeNode, DockerComposeServiceRaw, DockerComposeVolumeRaw, DockerPresetName, DockerPresetServiceDefinition, DockerServiceDefinition, EnvVarsBuilder, ExecOptions, HealthCheckFn, HookContext, MigrationConfig, OpenPublicTunnelsOptions, OpenPublicTunnelsResult, PrismaConfig, PrismaRunner, PublicTunnelHandle, SeedCheckContext, SeedCheckHelpers, SeedConfig, ServiceConfig, StartOptions, StopOptions, UrlBuilderContext, UrlBuilderFn, } from "./types/index";
|
|
9
9
|
export { getLocalIp, isPortAvailable, waitForServer } from "./core/network";
|
|
10
10
|
export { calculatePortOffset, computeDevIdentity, findMonorepoRoot, getProjectName, getWorktreeName, getWorktreeProjectSuffix, isWorktree, } from "./core/ports";
|
|
11
11
|
export { getProcessOnPort, isPortInUse, isProcessAlive, killProcessesOnAppPorts, killProcessOnPort, killProcessOnPortAndWait, } from "./core/process";
|