unplugin-https-reverse-proxy 1.0.0-beta.2 → 1.0.0-beta.20

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.
@@ -0,0 +1,760 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/rspack.ts
31
+ var rspack_exports = {};
32
+ __export(rspack_exports, {
33
+ default: () => rspack_default
34
+ });
35
+ module.exports = __toCommonJS(rspack_exports);
36
+
37
+ // src/index.ts
38
+ var import_node_process5 = __toESM(require("process"), 1);
39
+ var import_unplugin = require("unplugin");
40
+ var import_picocolors = __toESM(require("picocolors"), 1);
41
+
42
+ // src/utils.ts
43
+ var import_node_process = __toESM(require("process"), 1);
44
+ var import_node_path = __toESM(require("path"), 1);
45
+ var import_promises = require("fs/promises");
46
+ var import_node_child_process = require("child_process");
47
+ var import_consola = require("consola");
48
+ var consola = (0, import_consola.createConsola)({
49
+ level: 3
50
+ }).withTag("reverse-proxy");
51
+ async function chmodRecursive(_path, mode) {
52
+ await (0, import_promises.chmod)(_path, mode);
53
+ if ((await (0, import_promises.stat)(_path)).isDirectory()) {
54
+ const children = await (0, import_promises.readdir)(_path);
55
+ for (const child of children)
56
+ await chmodRecursive(import_node_path.default.join(_path, child), mode);
57
+ }
58
+ }
59
+ function once(fn) {
60
+ let called = false;
61
+ return (...args) => {
62
+ if (called)
63
+ return;
64
+ called = true;
65
+ return fn(...args);
66
+ };
67
+ }
68
+ function isAdmin() {
69
+ if (import_node_process.default.platform === "win32") {
70
+ try {
71
+ (0, import_node_child_process.execSync)("net session", { stdio: "ignore" });
72
+ return true;
73
+ } catch {
74
+ return false;
75
+ }
76
+ } else {
77
+ return import_node_process.default.getuid && import_node_process.default.getuid() === 0;
78
+ }
79
+ }
80
+
81
+ // src/caddy/index.ts
82
+ var import_node_os2 = require("os");
83
+ var import_node_process4 = __toESM(require("process"), 1);
84
+ var import_node_fs2 = require("fs");
85
+ var import_node_child_process4 = require("child_process");
86
+ var import_promises2 = require("fs/promises");
87
+ var import_got_cjs = require("got-cjs");
88
+ var import_http_proxy_agent = require("http-proxy-agent");
89
+ var import_https_proxy_agent = require("https-proxy-agent");
90
+ var lockfile = __toESM(require("proper-lockfile"), 1);
91
+
92
+ // src/host/index.ts
93
+ var import_node_process2 = __toESM(require("process"), 1);
94
+ var import_node_child_process2 = require("child_process");
95
+ var import_node_fs = require("fs");
96
+ var import_node_util = require("util");
97
+ var import_hostile = __toESM(require("hostile"), 1);
98
+ var _setHost = (0, import_node_util.promisify)(import_hostile.default.set);
99
+ var _removeHost = (0, import_node_util.promisify)(import_hostile.default.remove);
100
+ function getPathOfSystemHostsPath() {
101
+ return import_node_process2.default.platform === "win32" ? `${import_node_process2.default.env.windir || "C:\\WINDOWS"}\\system32\\drivers\\etc\\hosts` : "/etc/hosts";
102
+ }
103
+ async function syncHost() {
104
+ return new Promise((resolve, reject) => {
105
+ (0, import_node_child_process2.exec)(
106
+ import_node_process2.default.platform === "win32" ? "ipconfig /flushdns" : "sudo -E killall -HUP mDNSResponder",
107
+ (error, stdout, stderr) => {
108
+ if (error) {
109
+ consola.error(`exec error: ${error}`);
110
+ return resolve(false);
111
+ }
112
+ return resolve(true);
113
+ }
114
+ );
115
+ });
116
+ }
117
+ async function addHost(ip, host) {
118
+ if (import_node_process2.default.platform !== "win32")
119
+ (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
120
+ await _setHost("127.0.0.1", "localhost");
121
+ await _setHost(ip, host);
122
+ if (ip === "localhost")
123
+ await _setHost("127.0.0.1", host);
124
+ return await syncHost();
125
+ }
126
+ async function removeHost(ip, host) {
127
+ if (import_node_process2.default.platform !== "win32")
128
+ (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
129
+ await _removeHost(ip, host);
130
+ if (ip === "localhost")
131
+ await _removeHost("127.0.0.1", host);
132
+ return await syncHost();
133
+ }
134
+
135
+ // src/caddy/constants.ts
136
+ var import_node_path2 = require("path");
137
+ var import_node_os = __toESM(require("os"), 1);
138
+ var import_node_process3 = __toESM(require("process"), 1);
139
+ var import_dotenv_flow = __toESM(require("dotenv-flow"), 1);
140
+ import_dotenv_flow.default.config({
141
+ silent: true
142
+ });
143
+ function resolvePath(path2) {
144
+ if (path2.startsWith("~"))
145
+ return (0, import_node_path2.join)(import_node_os.default.homedir() || "", path2.slice(1));
146
+ return path2;
147
+ }
148
+ var TEMP_DIR = resolvePath(import_node_process3.default.env.UHRP_CADDY_PATH || import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp");
149
+ var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
150
+ var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
151
+ var caddyLockFilePath = (0, import_node_path2.join)(TEMP_DIR, "caddylock");
152
+ var supportList = [
153
+ {
154
+ platform: "dragonfly",
155
+ arch: "amd64"
156
+ },
157
+ {
158
+ platform: "freebsd",
159
+ arch: "amd64"
160
+ },
161
+ {
162
+ platform: "freebsd",
163
+ arch: "arm-6"
164
+ },
165
+ {
166
+ platform: "freebsd",
167
+ arch: "arm-7"
168
+ },
169
+ {
170
+ platform: "freebsd",
171
+ arch: "arm64"
172
+ },
173
+ {
174
+ platform: "linux",
175
+ arch: "amd64"
176
+ },
177
+ {
178
+ platform: "linux",
179
+ arch: "arm-5"
180
+ },
181
+ {
182
+ platform: "linux",
183
+ arch: "arm-6"
184
+ },
185
+ {
186
+ platform: "linux",
187
+ arch: "arm-7"
188
+ },
189
+ {
190
+ platform: "linux",
191
+ arch: "arm64"
192
+ },
193
+ {
194
+ platform: "linux",
195
+ arch: "mips"
196
+ },
197
+ {
198
+ platform: "linux",
199
+ arch: "mips64"
200
+ },
201
+ {
202
+ platform: "linux",
203
+ arch: "mips64le"
204
+ },
205
+ {
206
+ platform: "linux",
207
+ arch: "mipsle"
208
+ },
209
+ {
210
+ platform: "linux",
211
+ arch: "ppc64"
212
+ },
213
+ {
214
+ platform: "linux",
215
+ arch: "ppc64le"
216
+ },
217
+ {
218
+ platform: "linux",
219
+ arch: "s390x"
220
+ },
221
+ {
222
+ platform: "darwin",
223
+ arch: "amd64"
224
+ },
225
+ {
226
+ platform: "darwin",
227
+ arch: "arm64"
228
+ },
229
+ {
230
+ platform: "openbsd",
231
+ arch: "amd64"
232
+ },
233
+ {
234
+ platform: "openbsd",
235
+ arch: "arm-6"
236
+ },
237
+ {
238
+ platform: "openbsd",
239
+ arch: "arm-7"
240
+ },
241
+ {
242
+ platform: "openbsd",
243
+ arch: "arm64"
244
+ },
245
+ {
246
+ platform: "windows",
247
+ arch: "amd64"
248
+ },
249
+ {
250
+ platform: "windows",
251
+ arch: "arm-6"
252
+ },
253
+ {
254
+ platform: "windows",
255
+ arch: "arm-7"
256
+ },
257
+ {
258
+ platform: "windows",
259
+ arch: "arm64"
260
+ }
261
+ ];
262
+
263
+ // src/caddy/utils.ts
264
+ var import_node_net = require("net");
265
+ var import_node_child_process3 = require("child_process");
266
+ var import_ora = __toESM(require("ora"), 1);
267
+ var spinner;
268
+ function tryPort(port) {
269
+ return new Promise((resolve, reject) => {
270
+ try {
271
+ const server = (0, import_node_net.createServer)().listen(port);
272
+ server.on("listening", () => {
273
+ server.close();
274
+ resolve(false);
275
+ });
276
+ server.on("error", (err) => {
277
+ if (err.code === "EADDRINUSE")
278
+ resolve(true);
279
+ server.close();
280
+ });
281
+ } catch (e) {
282
+ resolve(true);
283
+ }
284
+ });
285
+ }
286
+ async function kill(port) {
287
+ return new Promise((resolve, reject) => {
288
+ (0, import_node_child_process3.exec)(`npx @maxbo/free-port ${port} -s`, (err, stdout, stderr) => {
289
+ if (err) {
290
+ reject(err);
291
+ } else {
292
+ consola.log(stdout);
293
+ resolve();
294
+ }
295
+ });
296
+ });
297
+ }
298
+ function getNumber(number) {
299
+ if (number <= 0)
300
+ return "0%";
301
+ else if (number < 100)
302
+ return `${number.toFixed(2)}%`;
303
+ else
304
+ return "100%";
305
+ }
306
+ function logProgress(percent) {
307
+ if (!spinner) {
308
+ consola.log("");
309
+ spinner = (0, import_ora.default)({
310
+ text: "Start Download Caddy: 0%",
311
+ indent: 2
312
+ });
313
+ spinner.start();
314
+ } else {
315
+ spinner.text = `Download Caddy: ${getNumber(Number(percent) * 100)}`;
316
+ }
317
+ }
318
+ function logProgressOver() {
319
+ if (!spinner) {
320
+ spinner = (0, import_ora.default)({
321
+ text: "Start Download Caddy: 100%",
322
+ indent: 2
323
+ });
324
+ spinner.start();
325
+ } else {
326
+ spinner.succeed();
327
+ consola.log("");
328
+ }
329
+ }
330
+
331
+ // src/caddy/index.ts
332
+ async function download() {
333
+ if (await testCaddy())
334
+ return caddyPath;
335
+ return new Promise((resolve, reject) => {
336
+ if (!(0, import_node_fs2.existsSync)(TEMP_DIR)) {
337
+ (0, import_node_fs2.mkdirSync)(TEMP_DIR, { recursive: true });
338
+ (0, import_node_fs2.chmodSync)(TEMP_DIR, 511);
339
+ }
340
+ const file = (0, import_node_fs2.createWriteStream)(caddyPath);
341
+ let _platform = (0, import_node_os2.platform)().toString();
342
+ _platform === "win32" && (_platform = "windows");
343
+ let _arch = import_node_process4.default.arch.toString();
344
+ _arch === "x64" && (_arch = "amd64");
345
+ const support = supportList.find((item) => item.platform === _platform && item.arch === _arch);
346
+ if (!support)
347
+ return reject(new Error("not support"));
348
+ const dowmloadLink = `https://caddyserver.com/api/download?os=${support.platform}&arch=${support.arch}`;
349
+ (0, import_node_fs2.existsSync)(caddyPath) && (0, import_node_fs2.unlinkSync)(caddyPath);
350
+ const httpProxy = import_node_process4.default.env.HTTP_PROXY || import_node_process4.default.env.http_proxy;
351
+ const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
352
+ const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
353
+ const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
354
+ const chmodCaddyOnce = once(() => {
355
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
356
+ });
357
+ import_got_cjs.got.stream(dowmloadLink, {
358
+ agent: {
359
+ http: httpAgent,
360
+ https: httpsAgent
361
+ }
362
+ }).on("downloadProgress", (progress) => {
363
+ logProgress(progress.percent);
364
+ chmodCaddyOnce();
365
+ }).pipe(file).on("finish", () => {
366
+ logProgressOver();
367
+ if (import_node_process4.default.platform === "win32")
368
+ return resolve(caddyPath);
369
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
370
+ resolve(caddyPath);
371
+ }).on("error", (err) => {
372
+ reject(err);
373
+ });
374
+ });
375
+ }
376
+ function testCaddy() {
377
+ return new Promise((resolve, reject) => {
378
+ if (!(0, import_node_fs2.existsSync)(caddyPath))
379
+ return resolve(false);
380
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
381
+ const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process4.spawn)(caddyPath, []) : (0, import_node_child_process4.spawn)("sudo", ["-E", caddyPath]);
382
+ child.on("close", () => {
383
+ return resolve(false);
384
+ });
385
+ child.on("error", (err) => {
386
+ return reject(err);
387
+ });
388
+ child.stdout.on("data", (_data) => {
389
+ return resolve(true);
390
+ });
391
+ });
392
+ }
393
+ var CaddyInstant = class {
394
+ constructor() {
395
+ this.inited = false;
396
+ this.stoped = false;
397
+ /** has locked */
398
+ this.locked = false;
399
+ testCaddy().then(async () => {
400
+ await this.getCaddyfile();
401
+ this.inited = true;
402
+ });
403
+ this.locked = this.checkLock();
404
+ }
405
+ checkLock() {
406
+ if (!(0, import_node_fs2.existsSync)(caddyLockFilePath))
407
+ (0, import_node_fs2.writeFileSync)(caddyLockFilePath, "");
408
+ return lockfile.checkSync(caddyLockFilePath);
409
+ }
410
+ async getCaddyfile() {
411
+ if (!(0, import_node_fs2.existsSync)(caddyFilePath))
412
+ await this.initCaddyfile();
413
+ this.caddyfile = await (0, import_promises2.readFile)(caddyFilePath, "utf-8");
414
+ return this.caddyfile;
415
+ }
416
+ async initCaddyfile() {
417
+ const contet = `
418
+ {
419
+ debug
420
+ auto_https disable_redirects
421
+ }
422
+
423
+ `;
424
+ await (0, import_promises2.writeFile)(caddyFilePath, contet);
425
+ this.caddyfile = contet;
426
+ }
427
+ async writeCaddyfile() {
428
+ return await (0, import_promises2.writeFile)(caddyFilePath, this.caddyfile || "");
429
+ }
430
+ async deleteCaddyfile() {
431
+ return await (0, import_promises2.unlink)(caddyFilePath);
432
+ }
433
+ async init() {
434
+ await download();
435
+ this.inited = true;
436
+ }
437
+ /**
438
+ * @param source like `127.0.0.1:8080`
439
+ * @param target like `test.abc.com`. And if you provide a port ,it will be ignore.
440
+ * @returns
441
+ */
442
+ async run(source, target, options) {
443
+ source = source.replace("0.0.0.0", "127.0.0.1");
444
+ const {
445
+ restore = true,
446
+ base = "/",
447
+ showCaddyLog = false,
448
+ https = false
449
+ } = options || {};
450
+ this._source = source;
451
+ this._target = target;
452
+ if (!this.inited)
453
+ await this.init();
454
+ if (!await addHost(source.split(":")[0], target.split(":")[0]))
455
+ throw new Error("update host failed");
456
+ consola.success("update host success\n");
457
+ await this.getCaddyfile();
458
+ if (!(this.caddyfile || "").includes(`${target.split(":")[0]}${https ? "" : ":80"}`)) {
459
+ this.caddyfile = `${this.caddyfile}
460
+ ${target.split(":")[0]}${https ? "" : ":80"} {
461
+ ${https ? "tls internal" : ""}
462
+ reverse_proxy http://${source} {
463
+ health_uri /
464
+ health_interval 2s
465
+ health_timeout 5s
466
+ fail_duration 2s
467
+ unhealthy_status 502 503 504 404
468
+ lb_try_duration 3s
469
+ lb_try_interval 300ms
470
+
471
+ @error status 502 503 504 404
472
+ handle_response @error {
473
+ respond * 503 {
474
+ body "Service Unavailable"
475
+ close
476
+ }
477
+ }
478
+ }
479
+ }
480
+
481
+ `;
482
+ await this.writeCaddyfile();
483
+ }
484
+ if (!(0, import_node_fs2.existsSync)(caddyLockFilePath))
485
+ (0, import_node_fs2.writeFileSync)(caddyLockFilePath, "");
486
+ if (!this.locked) {
487
+ if (await tryPort(443))
488
+ await kill(443);
489
+ if ((import_node_process4.default.platform === "win32" || !https) && await tryPort(80))
490
+ await kill(80);
491
+ }
492
+ return new Promise((resolve, reject) => {
493
+ var _a, _b;
494
+ const setupCleanup = () => {
495
+ import_node_process4.default.on("SIGINT", async () => {
496
+ if (this.stoped)
497
+ return;
498
+ import_node_process4.default.stdin.resume();
499
+ try {
500
+ await this.baseCleanup(restore);
501
+ } finally {
502
+ this.stoped = true;
503
+ import_node_process4.default.nextTick(() => {
504
+ import_node_process4.default.exit();
505
+ });
506
+ }
507
+ });
508
+ const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
509
+ import_node_process4.default.exit = async (code) => {
510
+ const port = Number(source.split(":")[1]);
511
+ if (!Number.isNaN(port) && await tryPort(port))
512
+ await kill(port);
513
+ const pwd = import_node_process4.default.cwd();
514
+ const viteCacheDir = `${pwd}/node_modules/.vite`;
515
+ if ((0, import_node_fs2.existsSync)(viteCacheDir))
516
+ await chmodRecursive(viteCacheDir, 511).catch(() => {
517
+ });
518
+ const nuxtCacheDir = `${pwd}/.nuxt`;
519
+ if ((0, import_node_fs2.existsSync)(nuxtCacheDir))
520
+ await chmodRecursive(nuxtCacheDir, 511).catch(() => {
521
+ });
522
+ const webpackCacheDir = `${pwd}/node_modules/.cache`;
523
+ if ((0, import_node_fs2.existsSync)(webpackCacheDir))
524
+ await chmodRecursive(webpackCacheDir, 511).catch(() => {
525
+ });
526
+ if (this.stoped)
527
+ return originalExit(code);
528
+ import_node_process4.default.stdin.resume();
529
+ try {
530
+ await this.baseCleanup();
531
+ } finally {
532
+ this.stoped = true;
533
+ import_node_process4.default.nextTick(() => {
534
+ originalExit(code);
535
+ });
536
+ }
537
+ };
538
+ };
539
+ if (!this.locked) {
540
+ this._caddyChild = import_node_process4.default.platform !== "win32" ? (0, import_node_child_process4.spawn)("sudo", ["-E", caddyPath, "run", "--config", caddyFilePath, "--watch"]) : (0, import_node_child_process4.spawn)(caddyPath, ["run", "--config", caddyFilePath, "--watch"]);
541
+ lockfile.lockSync(caddyLockFilePath);
542
+ this._caddyChild.on("error", (err) => {
543
+ return reject(err);
544
+ });
545
+ (_a = this._caddyChild.stderr) == null ? void 0 : _a.on("data", async (data) => {
546
+ const lines = data.toString().split("\n").map((line) => line.trim());
547
+ for (const line of lines) {
548
+ showCaddyLog && line && console.info(line);
549
+ if (line.includes("Error:") || line && JSON.parse(line).level === "error") {
550
+ if (line.includes("parsing caddyfile tokens")) {
551
+ consola.error("Caddyfile syntax error, please contact the author:\n", `${this.caddyfile}
552
+
553
+ ${line}
554
+ `);
555
+ await this.baseCleanup();
556
+ import_node_process4.default.exit();
557
+ } else {
558
+ consola.error(`${line}
559
+ `);
560
+ return reject(line);
561
+ }
562
+ }
563
+ if (line.includes("config file changed"))
564
+ consola.success("Detect added a new reverse proxy config, restarted.\n");
565
+ if (line.includes("caddy proxying") || line.includes("serving initial"))
566
+ return resolve();
567
+ }
568
+ });
569
+ (_b = this._caddyChild.stdout) == null ? void 0 : _b.on("data", (_data) => {
570
+ consola.info(_data.toString());
571
+ resolve();
572
+ });
573
+ setupCleanup();
574
+ } else {
575
+ consola.info("Caddy is running in another process.");
576
+ setupCleanup();
577
+ return resolve();
578
+ }
579
+ });
580
+ }
581
+ async baseCleanup(restore = true) {
582
+ var _a;
583
+ try {
584
+ if (restore && this._source && this._target) {
585
+ if (await removeHost(this._source.split(":")[0], this._target.split(":")[0]))
586
+ consola.success("Restore host success.\n");
587
+ else
588
+ consola.fail("Restore host failed.\n");
589
+ }
590
+ } catch (e) {
591
+ consola.error(e);
592
+ consola.fail("Restore host failed.\n");
593
+ }
594
+ if (!this.locked && !this.stoped) {
595
+ (_a = this._caddyChild) == null ? void 0 : _a.kill();
596
+ try {
597
+ await lockfile.unlock(caddyLockFilePath).catch(() => {
598
+ });
599
+ await (0, import_promises2.unlink)(caddyFilePath).catch(() => {
600
+ });
601
+ await (0, import_promises2.unlink)(caddyLockFilePath).catch(() => {
602
+ });
603
+ } catch (e) {
604
+ consola.error(e);
605
+ }
606
+ }
607
+ }
608
+ };
609
+
610
+ // src/index.ts
611
+ var config;
612
+ var caddy;
613
+ var cwd = import_node_process5.default.cwd();
614
+ var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
615
+ _printUrls == null ? void 0 : _printUrls();
616
+ if (caddy)
617
+ return;
618
+ caddy = new CaddyInstant();
619
+ caddy.run(source, target, {
620
+ base,
621
+ ...options
622
+ }).then(() => {
623
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
624
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
625
+ }).catch((e) => {
626
+ throw e;
627
+ });
628
+ });
629
+ var unpluginFactory = (options) => ({
630
+ name: "unplugin-https-reverse-proxy",
631
+ enforce: "pre",
632
+ vite: {
633
+ configResolved(_config) {
634
+ config = _config;
635
+ },
636
+ configureServer(server) {
637
+ var _a;
638
+ if (config.command !== "serve")
639
+ return;
640
+ const {
641
+ enable = true,
642
+ target = ""
643
+ } = options;
644
+ if (!enable)
645
+ return;
646
+ if (!isAdmin()) {
647
+ consola.warn("please run as administrator");
648
+ return;
649
+ }
650
+ if (!target) {
651
+ consola.fail("please provide target");
652
+ return;
653
+ }
654
+ const _printUrls = server.printUrls.bind(server);
655
+ let source = `localhost:${config.server.port || 5173}`;
656
+ const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
657
+ if (url) {
658
+ const u = new URL(url);
659
+ source = u.host;
660
+ }
661
+ const base = server.config.base || "/";
662
+ server.printUrls = () => vitePrintUrls(options, source, target, base, _printUrls);
663
+ }
664
+ },
665
+ webpack(compiler) {
666
+ var _a, _b;
667
+ if (import_node_process5.default.env.NODE_ENV !== "development")
668
+ return;
669
+ const {
670
+ enable = true,
671
+ target = ""
672
+ } = options;
673
+ if (!enable)
674
+ return;
675
+ if (!isAdmin()) {
676
+ consola.warn("please run as administrator");
677
+ return;
678
+ }
679
+ if (!target) {
680
+ consola.fail("please provide target");
681
+ return;
682
+ }
683
+ const devServer = {
684
+ host: "localhost",
685
+ port: 8080,
686
+ ...compiler.options.devServer,
687
+ // @ts-expect-error vuecli
688
+ ...(_a = import_node_process5.default.VUE_CLI_SERVICE) == null ? void 0 : _a.projectOptions.devServer
689
+ };
690
+ const source = `${devServer.host}:${devServer.port}`;
691
+ if (caddy)
692
+ return;
693
+ const _close = (_b = compiler.close) == null ? void 0 : _b.bind(compiler);
694
+ if (_close) {
695
+ compiler.close = async (callback) => {
696
+ import_node_process5.default.on("exit", () => {
697
+ _close && _close(callback);
698
+ });
699
+ };
700
+ } else {
701
+ options.https = false;
702
+ }
703
+ caddy = new CaddyInstant();
704
+ caddy.run(source, target, {
705
+ ...options
706
+ }).then(() => {
707
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
708
+ compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
709
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
710
+ });
711
+ }).catch((e) => {
712
+ throw e;
713
+ });
714
+ },
715
+ async rspack(compiler) {
716
+ var _a, _b, _c;
717
+ if (import_node_process5.default.env.NODE_ENV !== "development")
718
+ return;
719
+ const {
720
+ enable = true,
721
+ target = ""
722
+ } = options;
723
+ if (!enable)
724
+ return;
725
+ if (!isAdmin()) {
726
+ consola.warn("please run as administrator");
727
+ return;
728
+ }
729
+ if (!target) {
730
+ consola.fail("please provide target");
731
+ return;
732
+ }
733
+ const devServer = {
734
+ host: "localhost",
735
+ port: 8080,
736
+ ...compiler.options.devServer,
737
+ ...(_c = (_b = await ((_a = await import("@rsbuild/core")) == null ? void 0 : _a.loadConfig({
738
+ cwd
739
+ }))) == null ? void 0 : _b.content) == null ? void 0 : _c.server
740
+ };
741
+ const source = `${devServer.host}:${devServer.port}`;
742
+ if (caddy)
743
+ return;
744
+ caddy = new CaddyInstant();
745
+ caddy.run(source, target, {
746
+ ...options
747
+ }).then(() => {
748
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
749
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
750
+ }).catch((e) => {
751
+ throw e;
752
+ });
753
+ }
754
+ });
755
+ var unplugin = /* @__PURE__ */ (0, import_unplugin.createUnplugin)(unpluginFactory);
756
+ var src_default = unplugin;
757
+
758
+ // src/rspack.ts
759
+ var rspack_default = src_default.rspack;
760
+ module.exports = module.exports;exports.default = module.exports;