vite-plugin-caddy-multiple-tls 1.1.0 → 1.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/README.md +3 -0
- package/dist/index.js +103 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# vite-plugin-caddy-multiple-tls
|
|
2
2
|
|
|
3
|
+
## What it does
|
|
4
|
+
Runs Caddy alongside Vite to give you HTTPS locally with automatic, per-branch domains like `<repo>.<branch>.localhost`, so you can use real hostnames, cookies, and secure APIs without manual proxy setup.
|
|
5
|
+
|
|
3
6
|
## Usage
|
|
4
7
|
|
|
5
8
|
```js
|
package/dist/index.js
CHANGED
|
@@ -174,7 +174,13 @@ async function ensureTlsAutomation() {
|
|
|
174
174
|
throw new Error(`Failed to initialize Caddy TLS automation: ${text}`);
|
|
175
175
|
}
|
|
176
176
|
}
|
|
177
|
-
|
|
177
|
+
function formatDialAddress(host, port) {
|
|
178
|
+
if (host.includes(":") && !host.startsWith("[")) {
|
|
179
|
+
return `[${host}]:${port}`;
|
|
180
|
+
}
|
|
181
|
+
return `${host}:${port}`;
|
|
182
|
+
}
|
|
183
|
+
async function addRoute(id, domains, port, cors, serverName = DEFAULT_SERVER_NAME, upstreamHost = "127.0.0.1") {
|
|
178
184
|
const handlers = [];
|
|
179
185
|
if (cors) {
|
|
180
186
|
handlers.push({
|
|
@@ -197,7 +203,7 @@ async function addRoute(id, domains, port, cors, serverName = DEFAULT_SERVER_NAM
|
|
|
197
203
|
}
|
|
198
204
|
handlers.push({
|
|
199
205
|
handler: "reverse_proxy",
|
|
200
|
-
upstreams: [{ dial:
|
|
206
|
+
upstreams: [{ dial: formatDialAddress(upstreamHost, port) }]
|
|
201
207
|
});
|
|
202
208
|
const route = {
|
|
203
209
|
"@id": id,
|
|
@@ -311,6 +317,15 @@ function resolveBaseDomain(options) {
|
|
|
311
317
|
}
|
|
312
318
|
return "localhost";
|
|
313
319
|
}
|
|
320
|
+
function resolveUpstreamHost(host) {
|
|
321
|
+
if (typeof host === "string") {
|
|
322
|
+
const trimmed = host.trim();
|
|
323
|
+
if (trimmed && trimmed !== "0.0.0.0" && trimmed !== "::") {
|
|
324
|
+
return trimmed;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return "127.0.0.1";
|
|
328
|
+
}
|
|
314
329
|
function sanitizeDomainLabel(value) {
|
|
315
330
|
return value.toLowerCase().replace(/[^a-z0-9-]/g, "-").replace(/-+/g, "-").replace(/^-+|-+$/g, "");
|
|
316
331
|
}
|
|
@@ -353,7 +368,8 @@ function viteCaddyTlsPlugin({
|
|
|
353
368
|
} = {}) {
|
|
354
369
|
return {
|
|
355
370
|
name: "vite:caddy-tls",
|
|
356
|
-
configureServer(
|
|
371
|
+
configureServer(server) {
|
|
372
|
+
const { httpServer, config } = server;
|
|
357
373
|
const fallbackPort = config.server.port || 5173;
|
|
358
374
|
const resolvedDomain = resolveDomain({
|
|
359
375
|
domain,
|
|
@@ -367,6 +383,9 @@ function viteCaddyTlsPlugin({
|
|
|
367
383
|
const shouldUseInternalTls = internalTls ?? (baseDomain !== void 0 || loopbackDomain !== void 0 || domain !== void 0);
|
|
368
384
|
const tlsPolicyId = shouldUseInternalTls ? `${routeId}-tls` : null;
|
|
369
385
|
let cleanupStarted = false;
|
|
386
|
+
let resolvedPort = null;
|
|
387
|
+
let resolvedHost = null;
|
|
388
|
+
let setupStarted = false;
|
|
370
389
|
if (domainArray.length === 0) {
|
|
371
390
|
console.error(
|
|
372
391
|
"No domain resolved. Provide domain, or run inside a git repo, or pass repo/branch."
|
|
@@ -374,13 +393,60 @@ function viteCaddyTlsPlugin({
|
|
|
374
393
|
return;
|
|
375
394
|
}
|
|
376
395
|
let tlsPolicyAdded = false;
|
|
377
|
-
function
|
|
378
|
-
if (!httpServer) return fallbackPort;
|
|
379
|
-
const address = httpServer.address();
|
|
396
|
+
function getPortFromAddress(address) {
|
|
380
397
|
if (address && typeof address === "object" && "port" in address) {
|
|
381
|
-
|
|
398
|
+
const port = address.port;
|
|
399
|
+
if (typeof port === "number") {
|
|
400
|
+
return port;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
function updateResolvedTarget() {
|
|
406
|
+
if (resolvedPort !== null && resolvedHost !== null) return;
|
|
407
|
+
const resolvedUrl = server.resolvedUrls?.local?.[0];
|
|
408
|
+
if (resolvedUrl) {
|
|
409
|
+
try {
|
|
410
|
+
const url = new URL(resolvedUrl);
|
|
411
|
+
if (resolvedHost === null && url.hostname) {
|
|
412
|
+
resolvedHost = url.hostname === "localhost" ? "127.0.0.1" : url.hostname;
|
|
413
|
+
}
|
|
414
|
+
const port = Number(url.port);
|
|
415
|
+
if (resolvedPort === null && !Number.isNaN(port)) {
|
|
416
|
+
resolvedPort = port;
|
|
417
|
+
}
|
|
418
|
+
} catch (e) {
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
if (httpServer) {
|
|
422
|
+
const address = httpServer.address();
|
|
423
|
+
if (address && typeof address === "object") {
|
|
424
|
+
const port = getPortFromAddress(address);
|
|
425
|
+
if (resolvedPort === null && port !== null) {
|
|
426
|
+
resolvedPort = port;
|
|
427
|
+
}
|
|
428
|
+
if (resolvedHost === null && "address" in address) {
|
|
429
|
+
const host = address.address;
|
|
430
|
+
if (typeof host === "string" && host !== "0.0.0.0" && host !== "::") {
|
|
431
|
+
resolvedHost = host;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
if (resolvedPort === null && typeof config.server.port === "number") {
|
|
437
|
+
resolvedPort = config.server.port;
|
|
438
|
+
}
|
|
439
|
+
if (resolvedHost === null) {
|
|
440
|
+
resolvedHost = resolveUpstreamHost(config.server.host);
|
|
382
441
|
}
|
|
383
|
-
|
|
442
|
+
}
|
|
443
|
+
function getServerPort() {
|
|
444
|
+
updateResolvedTarget();
|
|
445
|
+
return resolvedPort ?? fallbackPort;
|
|
446
|
+
}
|
|
447
|
+
function getUpstreamHost() {
|
|
448
|
+
updateResolvedTarget();
|
|
449
|
+
return resolvedHost ?? "127.0.0.1";
|
|
384
450
|
}
|
|
385
451
|
async function cleanupRoute() {
|
|
386
452
|
if (cleanupStarted) return;
|
|
@@ -429,6 +495,7 @@ function viteCaddyTlsPlugin({
|
|
|
429
495
|
return;
|
|
430
496
|
}
|
|
431
497
|
const port = getServerPort();
|
|
498
|
+
const upstreamHost = getUpstreamHost();
|
|
432
499
|
if (tlsPolicyId) {
|
|
433
500
|
try {
|
|
434
501
|
await addTlsPolicy(tlsPolicyId, domainArray);
|
|
@@ -439,7 +506,7 @@ function viteCaddyTlsPlugin({
|
|
|
439
506
|
}
|
|
440
507
|
}
|
|
441
508
|
try {
|
|
442
|
-
await addRoute(routeId, domainArray, port, cors, serverName);
|
|
509
|
+
await addRoute(routeId, domainArray, port, cors, serverName, upstreamHost);
|
|
443
510
|
} catch (e) {
|
|
444
511
|
if (tlsPolicyAdded && tlsPolicyId) {
|
|
445
512
|
await removeTlsPolicy(tlsPolicyId);
|
|
@@ -451,7 +518,7 @@ function viteCaddyTlsPlugin({
|
|
|
451
518
|
console.log("\u{1F512} Caddy is proxying your traffic on https");
|
|
452
519
|
console.log();
|
|
453
520
|
console.log(
|
|
454
|
-
`\u{1F517} Access your local ${domainArray.length > 1 ? "servers" : "server"}
|
|
521
|
+
`\u{1F517} Access your local ${domainArray.length > 1 ? "servers" : "server"}!`
|
|
455
522
|
);
|
|
456
523
|
domainArray.forEach((domain2) => {
|
|
457
524
|
console.log(`\u{1F30D} https://${domain2}`);
|
|
@@ -467,15 +534,37 @@ function viteCaddyTlsPlugin({
|
|
|
467
534
|
registerProcessCleanup();
|
|
468
535
|
httpServer?.once("close", onServerClose);
|
|
469
536
|
}
|
|
470
|
-
function
|
|
537
|
+
function runSetupOnce() {
|
|
538
|
+
if (setupStarted) return;
|
|
539
|
+
setupStarted = true;
|
|
471
540
|
void setupRoute();
|
|
472
541
|
}
|
|
542
|
+
function wrapServerListen() {
|
|
543
|
+
if (typeof server.listen !== "function") return false;
|
|
544
|
+
const originalListen = server.listen.bind(server);
|
|
545
|
+
server.listen = async function(port, isRestart) {
|
|
546
|
+
const result = await originalListen(port, isRestart);
|
|
547
|
+
if (typeof port === "number") {
|
|
548
|
+
resolvedPort = port;
|
|
549
|
+
} else {
|
|
550
|
+
updateResolvedTarget();
|
|
551
|
+
}
|
|
552
|
+
runSetupOnce();
|
|
553
|
+
return result;
|
|
554
|
+
};
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
function onListening() {
|
|
558
|
+
updateResolvedTarget();
|
|
559
|
+
runSetupOnce();
|
|
560
|
+
}
|
|
561
|
+
const listenWrapped = wrapServerListen();
|
|
473
562
|
if (httpServer?.listening) {
|
|
474
|
-
|
|
563
|
+
runSetupOnce();
|
|
475
564
|
} else if (httpServer) {
|
|
476
565
|
httpServer.once("listening", onListening);
|
|
477
|
-
} else {
|
|
478
|
-
|
|
566
|
+
} else if (!listenWrapped) {
|
|
567
|
+
runSetupOnce();
|
|
479
568
|
}
|
|
480
569
|
}
|
|
481
570
|
};
|