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

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/rollup.cjs CHANGED
@@ -3,22 +3,8 @@ var __create = Object.create;
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
6
  var __getProtoOf = Object.getPrototypeOf;
8
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
- var __spreadValues = (a, b) => {
12
- for (var prop in b || (b = {}))
13
- if (__hasOwnProp.call(b, prop))
14
- __defNormalProp(a, prop, b[prop]);
15
- if (__getOwnPropSymbols)
16
- for (var prop of __getOwnPropSymbols(b)) {
17
- if (__propIsEnum.call(b, prop))
18
- __defNormalProp(a, prop, b[prop]);
19
- }
20
- return a;
21
- };
22
8
  var __export = (target, all) => {
23
9
  for (var name in all)
24
10
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -51,7 +37,6 @@ var import_unplugin2 = require("unplugin");
51
37
 
52
38
  // src/index.ts
53
39
  var import_node_process5 = __toESM(require("process"), 1);
54
- var import_node_fs3 = require("fs");
55
40
  var import_unplugin = require("unplugin");
56
41
  var import_picocolors = __toESM(require("picocolors"), 1);
57
42
 
@@ -72,12 +57,21 @@ async function chmodRecursive(_path, mode) {
72
57
  await chmodRecursive(import_node_path.default.join(_path, child), mode);
73
58
  }
74
59
  }
60
+ function once(fn) {
61
+ let called = false;
62
+ return (...args) => {
63
+ if (called)
64
+ return;
65
+ called = true;
66
+ return fn(...args);
67
+ };
68
+ }
75
69
  function isAdmin() {
76
70
  if (import_node_process.default.platform === "win32") {
77
71
  try {
78
72
  (0, import_node_child_process.execSync)("net session", { stdio: "ignore" });
79
73
  return true;
80
- } catch (e) {
74
+ } catch {
81
75
  return false;
82
76
  }
83
77
  } else {
@@ -86,25 +80,24 @@ function isAdmin() {
86
80
  }
87
81
 
88
82
  // src/caddy/index.ts
89
- var import_node_os = require("os");
83
+ var import_node_os2 = require("os");
90
84
  var import_node_process4 = __toESM(require("process"), 1);
91
85
  var import_node_fs2 = require("fs");
92
- var import_node_child_process3 = require("child_process");
86
+ var import_node_child_process4 = require("child_process");
87
+ var import_promises2 = require("fs/promises");
93
88
  var import_got_cjs = require("got-cjs");
94
89
  var import_http_proxy_agent = require("http-proxy-agent");
95
90
  var import_https_proxy_agent = require("https-proxy-agent");
96
- var import_kill_port = __toESM(require("kill-port"), 1);
91
+ var lockfile = __toESM(require("proper-lockfile"), 1);
97
92
 
98
93
  // src/host/index.ts
99
94
  var import_node_process2 = __toESM(require("process"), 1);
100
95
  var import_node_child_process2 = require("child_process");
101
96
  var import_node_fs = require("fs");
102
- var import_hosts_so_easy = __toESM(require("hosts-so-easy"), 1);
103
- var Hosts = import_hosts_so_easy.default.default || import_hosts_so_easy.default;
104
- var hostInstance = new Hosts({
105
- debounceTime: 50,
106
- hostsFile: getPathOfSystemHostsPath()
107
- });
97
+ var import_node_util = require("util");
98
+ var import_hostile = __toESM(require("hostile"), 1);
99
+ var _setHost = (0, import_node_util.promisify)(import_hostile.default.set);
100
+ var _removeHost = (0, import_node_util.promisify)(import_hostile.default.remove);
108
101
  function getPathOfSystemHostsPath() {
109
102
  return import_node_process2.default.platform === "win32" ? `${import_node_process2.default.env.windir || "C:\\WINDOWS"}\\system32\\drivers\\etc\\hosts` : "/etc/hosts";
110
103
  }
@@ -125,29 +118,38 @@ async function syncHost() {
125
118
  async function addHost(ip, host) {
126
119
  if (import_node_process2.default.platform !== "win32")
127
120
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
128
- hostInstance.add("127.0.0.1", "localhost");
129
- hostInstance.add(ip, host);
121
+ await _setHost("127.0.0.1", "localhost");
122
+ await _setHost(ip, host);
130
123
  if (ip === "localhost")
131
- hostInstance.add("127.0.0.1", host);
132
- await hostInstance.updateFinish();
124
+ await _setHost("127.0.0.1", host);
133
125
  return await syncHost();
134
126
  }
135
127
  async function removeHost(ip, host) {
136
128
  if (import_node_process2.default.platform !== "win32")
137
129
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
138
- hostInstance.remove(ip, host);
130
+ await _removeHost(ip, host);
139
131
  if (ip === "localhost")
140
- hostInstance.remove("127.0.0.1", host);
141
- await hostInstance.updateFinish();
132
+ await _removeHost("127.0.0.1", host);
142
133
  return await syncHost();
143
134
  }
144
135
 
145
136
  // src/caddy/constants.ts
146
137
  var import_node_path2 = require("path");
138
+ var import_node_os = __toESM(require("os"), 1);
147
139
  var import_node_process3 = __toESM(require("process"), 1);
148
- var TEMP_DIR = import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp";
140
+ var import_dotenv_flow = __toESM(require("dotenv-flow"), 1);
141
+ import_dotenv_flow.default.config({
142
+ silent: true
143
+ });
144
+ function resolvePath(path2) {
145
+ if (path2.startsWith("~"))
146
+ return (0, import_node_path2.join)(import_node_os.default.homedir() || "", path2.slice(1));
147
+ return path2;
148
+ }
149
+ 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
150
  var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
150
151
  var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
152
+ var caddyLockFilePath = (0, import_node_path2.join)(TEMP_DIR, "caddylock");
151
153
  var supportList = [
152
154
  {
153
155
  platform: "dragonfly",
@@ -261,6 +263,7 @@ var supportList = [
261
263
 
262
264
  // src/caddy/utils.ts
263
265
  var import_node_net = require("net");
266
+ var import_node_child_process3 = require("child_process");
264
267
  var import_ora = __toESM(require("ora"), 1);
265
268
  var spinner;
266
269
  function tryPort(port) {
@@ -281,6 +284,18 @@ function tryPort(port) {
281
284
  }
282
285
  });
283
286
  }
287
+ async function kill(port) {
288
+ return new Promise((resolve, reject) => {
289
+ (0, import_node_child_process3.exec)(`npx @maxbo/free-port ${port} -s`, (err, stdout, stderr) => {
290
+ if (err) {
291
+ reject(err);
292
+ } else {
293
+ consola.log(stdout);
294
+ resolve();
295
+ }
296
+ });
297
+ });
298
+ }
284
299
  function getNumber(number) {
285
300
  if (number <= 0)
286
301
  return "0%";
@@ -319,8 +334,12 @@ async function download() {
319
334
  if (await testCaddy())
320
335
  return caddyPath;
321
336
  return new Promise((resolve, reject) => {
337
+ if (!(0, import_node_fs2.existsSync)(TEMP_DIR)) {
338
+ (0, import_node_fs2.mkdirSync)(TEMP_DIR, { recursive: true });
339
+ (0, import_node_fs2.chmodSync)(TEMP_DIR, 511);
340
+ }
322
341
  const file = (0, import_node_fs2.createWriteStream)(caddyPath);
323
- let _platform = (0, import_node_os.platform)().toString();
342
+ let _platform = (0, import_node_os2.platform)().toString();
324
343
  _platform === "win32" && (_platform = "windows");
325
344
  let _arch = import_node_process4.default.arch.toString();
326
345
  _arch === "x64" && (_arch = "amd64");
@@ -333,6 +352,9 @@ async function download() {
333
352
  const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
334
353
  const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
335
354
  const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
355
+ const chmodCaddyOnce = once(() => {
356
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
357
+ });
336
358
  import_got_cjs.got.stream(dowmloadLink, {
337
359
  agent: {
338
360
  http: httpAgent,
@@ -340,11 +362,12 @@ async function download() {
340
362
  }
341
363
  }).on("downloadProgress", (progress) => {
342
364
  logProgress(progress.percent);
365
+ chmodCaddyOnce();
343
366
  }).pipe(file).on("finish", () => {
344
367
  logProgressOver();
345
368
  if (import_node_process4.default.platform === "win32")
346
369
  return resolve(caddyPath);
347
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
370
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
348
371
  resolve(caddyPath);
349
372
  }).on("error", (err) => {
350
373
  reject(err);
@@ -355,8 +378,8 @@ function testCaddy() {
355
378
  return new Promise((resolve, reject) => {
356
379
  if (!(0, import_node_fs2.existsSync)(caddyPath))
357
380
  return resolve(false);
358
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
359
- const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process3.spawn)(caddyPath, []) : (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath]);
381
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
382
+ const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process4.spawn)(caddyPath, []) : (0, import_node_child_process4.spawn)("sudo", ["-E", caddyPath]);
360
383
  child.on("close", () => {
361
384
  return resolve(false);
362
385
  });
@@ -372,9 +395,41 @@ var CaddyInstant = class {
372
395
  constructor() {
373
396
  this.inited = false;
374
397
  this.stoped = false;
375
- testCaddy().then(() => {
398
+ /** has locked */
399
+ this.locked = false;
400
+ testCaddy().then(async () => {
401
+ await this.getCaddyfile();
376
402
  this.inited = true;
377
403
  });
404
+ this.locked = this.checkLock();
405
+ }
406
+ checkLock() {
407
+ if (!(0, import_node_fs2.existsSync)(caddyLockFilePath))
408
+ (0, import_node_fs2.writeFileSync)(caddyLockFilePath, "");
409
+ return lockfile.checkSync(caddyLockFilePath);
410
+ }
411
+ async getCaddyfile() {
412
+ if (!(0, import_node_fs2.existsSync)(caddyFilePath))
413
+ await this.initCaddyfile();
414
+ this.caddyfile = await (0, import_promises2.readFile)(caddyFilePath, "utf-8");
415
+ return this.caddyfile;
416
+ }
417
+ async initCaddyfile() {
418
+ const contet = `
419
+ {
420
+ debug
421
+ auto_https disable_redirects
422
+ }
423
+
424
+ `;
425
+ await (0, import_promises2.writeFile)(caddyFilePath, contet);
426
+ this.caddyfile = contet;
427
+ }
428
+ async writeCaddyfile() {
429
+ return await (0, import_promises2.writeFile)(caddyFilePath, this.caddyfile || "");
430
+ }
431
+ async deleteCaddyfile() {
432
+ return await (0, import_promises2.unlink)(caddyFilePath);
378
433
  }
379
434
  async init() {
380
435
  await download();
@@ -393,103 +448,190 @@ var CaddyInstant = class {
393
448
  showCaddyLog = false,
394
449
  https = false
395
450
  } = options || {};
451
+ this._source = source;
452
+ this._target = target;
396
453
  if (!this.inited)
397
454
  await this.init();
398
- if (https && await tryPort(443))
399
- await (0, import_kill_port.default)(443, "tcp");
400
- if ((import_node_process4.default.platform === "win32" || !https) && await tryPort(80))
401
- await (0, import_kill_port.default)(80, "tcp");
402
455
  if (!await addHost(source.split(":")[0], target.split(":")[0]))
403
456
  throw new Error("update host failed");
404
457
  consola.success("update host success\n");
458
+ await this.getCaddyfile();
459
+ if (!(this.caddyfile || "").includes(`${target.split(":")[0]}${https ? "" : ":80"}`)) {
460
+ this.caddyfile = `${this.caddyfile}
461
+ ${target.split(":")[0]}${https ? "" : ":80"} {
462
+ ${https ? "tls internal" : ""}
463
+ reverse_proxy http://${source} {
464
+ health_uri ${base}
465
+ health_interval 2s
466
+ health_timeout 5s
467
+ fail_duration 2s
468
+ unhealthy_status 502 503 504 404
469
+ lb_try_duration 3s
470
+ lb_try_interval 300ms
471
+ health_headers {
472
+ Accept text/html
473
+ Accept-Encoding gzip, deflate, br
474
+ User-Agent Caddy-Health-Check
475
+ }
476
+
477
+ @error status 502 503 504 404
478
+ handle_response @error {
479
+ respond * 503 {
480
+ body "Service Unavailable"
481
+ close
482
+ }
483
+ }
484
+ }
485
+ }
486
+
487
+ `;
488
+ await this.writeCaddyfile();
489
+ }
490
+ if (!(0, import_node_fs2.existsSync)(caddyLockFilePath))
491
+ (0, import_node_fs2.writeFileSync)(caddyLockFilePath, "");
492
+ if (!this.locked) {
493
+ if (await tryPort(443))
494
+ await kill(443);
495
+ if ((import_node_process4.default.platform === "win32" || !https) && await tryPort(80))
496
+ await kill(80);
497
+ }
405
498
  return new Promise((resolve, reject) => {
406
- const child = import_node_process4.default.platform !== "win32" ? (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath, "reverse-proxy", "--from", `${target.split(":")[0]}${https ? "" : ":80"}`, "--to", `${source}`, "--internal-certs", "--insecure", "--disable-redirects"]) : (0, import_node_child_process3.spawn)(caddyPath, ["reverse-proxy", "--from", `${target.split(":")[0]}`, "--to", `${source}`, "--internal-certs"]);
407
- child.on("error", (err) => {
408
- return reject(err);
409
- });
410
- import_node_process4.default.on("SIGINT", async () => {
411
- if (!restore || this.stoped)
412
- return;
413
- try {
414
- if (await removeHost(source.split(":")[0], target.split(":")[0]))
415
- consola.success("restore host success\n");
416
- else
417
- consola.fail("restore host failed\n");
418
- } catch (e) {
419
- consola.error(e);
420
- consola.fail("restore host failed\n");
421
- } finally {
422
- this.stoped = true;
423
- import_node_process4.default.nextTick(() => {
424
- import_node_process4.default.exit();
425
- });
426
- }
427
- });
428
- const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
429
- import_node_process4.default.exit = async (code) => {
430
- if (!restore || this.stoped)
431
- return originalExit(code);
432
- try {
433
- if (await removeHost(source.split(":")[0], target.split(":")[0]))
434
- consola.success("restore host success\n");
435
- else
436
- consola.fail("restore host failed\n");
437
- } catch (e) {
438
- consola.error(e);
439
- consola.fail("restore host failed\n");
440
- } finally {
441
- this.stoped = true;
442
- import_node_process4.default.nextTick(() => {
443
- originalExit(code);
444
- });
445
- }
499
+ var _a, _b;
500
+ const setupCleanup = () => {
501
+ import_node_process4.default.on("SIGINT", async () => {
502
+ if (this.stoped)
503
+ return;
504
+ import_node_process4.default.stdin.resume();
505
+ try {
506
+ await this.baseCleanup(restore);
507
+ } finally {
508
+ this.stoped = true;
509
+ import_node_process4.default.nextTick(() => {
510
+ import_node_process4.default.exit();
511
+ });
512
+ }
513
+ });
514
+ const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
515
+ import_node_process4.default.exit = async (code) => {
516
+ const port = Number(source.split(":")[1]);
517
+ if (!Number.isNaN(port) && await tryPort(port))
518
+ await kill(port);
519
+ const pwd = import_node_process4.default.cwd();
520
+ const viteCacheDir = `${pwd}/node_modules/.vite`;
521
+ if ((0, import_node_fs2.existsSync)(viteCacheDir))
522
+ await chmodRecursive(viteCacheDir, 511).catch(() => {
523
+ });
524
+ const nuxtCacheDir = `${pwd}/.nuxt`;
525
+ if ((0, import_node_fs2.existsSync)(nuxtCacheDir))
526
+ await chmodRecursive(nuxtCacheDir, 511).catch(() => {
527
+ });
528
+ const webpackCacheDir = `${pwd}/node_modules/.cache`;
529
+ if ((0, import_node_fs2.existsSync)(webpackCacheDir))
530
+ await chmodRecursive(webpackCacheDir, 511).catch(() => {
531
+ });
532
+ if (this.stoped)
533
+ return originalExit(code);
534
+ import_node_process4.default.stdin.resume();
535
+ try {
536
+ await this.baseCleanup();
537
+ } finally {
538
+ this.stoped = true;
539
+ import_node_process4.default.nextTick(() => {
540
+ originalExit(code);
541
+ });
542
+ }
543
+ };
446
544
  };
447
- child.stderr.on("data", (data) => {
448
- const lines = data.toString().split("\n").map((line) => line.trim());
449
- for (const line of lines) {
450
- showCaddyLog && line && console.info(line);
451
- if (line.includes("Error:") || line && JSON.parse(line).level === "error") {
452
- consola.error(line);
453
- return reject(line);
545
+ if (!this.locked) {
546
+ 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"]);
547
+ lockfile.lockSync(caddyLockFilePath);
548
+ this._caddyChild.on("error", (err) => {
549
+ return reject(err);
550
+ });
551
+ (_a = this._caddyChild.stderr) == null ? void 0 : _a.on("data", async (data) => {
552
+ const lines = data.toString().split("\n").map((line) => line.trim());
553
+ for (const line of lines) {
554
+ showCaddyLog && line && console.info(line);
555
+ if (line.includes("Error:") || line && JSON.parse(line).level === "error") {
556
+ if (line.includes("parsing caddyfile tokens")) {
557
+ consola.error("Caddyfile syntax error, please contact the author:\n", `${this.caddyfile}
558
+
559
+ ${line}
560
+ `);
561
+ await this.baseCleanup();
562
+ import_node_process4.default.exit();
563
+ } else {
564
+ consola.error(`${line}
565
+ `);
566
+ return reject(line);
567
+ }
568
+ }
569
+ if (line.includes("config file changed"))
570
+ consola.success("Detect added a new reverse proxy config, restarted.\n");
571
+ if (line.includes("caddy proxying") || line.includes("serving initial"))
572
+ return resolve();
454
573
  }
455
- }
456
- });
457
- child.stdout.on("data", (_data) => {
458
- consola.info(_data.toString());
459
- resolve();
460
- });
574
+ });
575
+ (_b = this._caddyChild.stdout) == null ? void 0 : _b.on("data", (_data) => {
576
+ consola.info(_data.toString());
577
+ resolve();
578
+ });
579
+ setupCleanup();
580
+ } else {
581
+ consola.info("Caddy is running in another process.");
582
+ setupCleanup();
583
+ return resolve();
584
+ }
461
585
  });
462
586
  }
587
+ async baseCleanup(restore = true) {
588
+ var _a;
589
+ try {
590
+ if (restore && this._source && this._target) {
591
+ if (await removeHost(this._source.split(":")[0], this._target.split(":")[0]))
592
+ consola.success("Restore host success.\n");
593
+ else
594
+ consola.fail("Restore host failed.\n");
595
+ }
596
+ } catch (e) {
597
+ consola.error(e);
598
+ consola.fail("Restore host failed.\n");
599
+ }
600
+ if (!this.locked && !this.stoped) {
601
+ (_a = this._caddyChild) == null ? void 0 : _a.kill();
602
+ try {
603
+ await lockfile.unlock(caddyLockFilePath).catch(() => {
604
+ });
605
+ await (0, import_promises2.unlink)(caddyFilePath).catch(() => {
606
+ });
607
+ await (0, import_promises2.unlink)(caddyLockFilePath).catch(() => {
608
+ });
609
+ } catch (e) {
610
+ consola.error(e);
611
+ }
612
+ }
613
+ }
463
614
  };
464
615
 
465
616
  // src/index.ts
466
617
  var config;
467
618
  var caddy;
468
- var pwd = import_node_process5.default.cwd();
469
- function vitePrintUrls(server, options, source, target, base = "/", _printUrls) {
619
+ var cwd = import_node_process5.default.cwd();
620
+ var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
470
621
  _printUrls == null ? void 0 : _printUrls();
471
- const viteCacheDir = `${pwd}/node_modules/.vite`;
472
- if ((0, import_node_fs3.existsSync)(viteCacheDir))
473
- chmodRecursive(viteCacheDir, 511);
474
- const nuxtCacheDir = `${pwd}/.nuxt`;
475
- if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
476
- chmodRecursive(nuxtCacheDir, 511);
477
- try {
478
- if (caddy)
479
- return;
480
- caddy = new CaddyInstant();
481
- caddy.run(source, target, __spreadValues({
482
- base
483
- }, options)).then(() => {
484
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
485
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
486
- }).catch((e) => {
487
- throw e;
488
- });
489
- } catch (e) {
490
- consola.error(e);
491
- }
492
- }
622
+ if (caddy)
623
+ return;
624
+ caddy = new CaddyInstant();
625
+ caddy.run(source, target, {
626
+ base,
627
+ ...options
628
+ }).then(() => {
629
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
630
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
631
+ }).catch((e) => {
632
+ throw e;
633
+ });
634
+ });
493
635
  var unpluginFactory = (options) => ({
494
636
  name: "unplugin-https-reverse-proxy",
495
637
  enforce: "pre",
@@ -499,11 +641,7 @@ var unpluginFactory = (options) => ({
499
641
  },
500
642
  configureServer(server) {
501
643
  var _a;
502
- if (!isAdmin()) {
503
- consola.warn("please run as administrator");
504
- return;
505
- }
506
- if (config.command === "build")
644
+ if (config.command !== "serve")
507
645
  return;
508
646
  const {
509
647
  enable = true,
@@ -511,6 +649,10 @@ var unpluginFactory = (options) => ({
511
649
  } = options;
512
650
  if (!enable)
513
651
  return;
652
+ if (!isAdmin()) {
653
+ consola.warn("please run as administrator");
654
+ return;
655
+ }
514
656
  if (!target) {
515
657
  consola.fail("please provide target");
516
658
  return;
@@ -523,15 +665,61 @@ var unpluginFactory = (options) => ({
523
665
  source = u.host;
524
666
  }
525
667
  const base = server.config.base || "/";
526
- server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls);
668
+ server.printUrls = () => vitePrintUrls(options, source, target, base, _printUrls);
527
669
  }
528
670
  },
529
671
  webpack(compiler) {
530
- var _a;
672
+ var _a, _b;
673
+ if (import_node_process5.default.env.NODE_ENV !== "development")
674
+ return;
675
+ const {
676
+ enable = true,
677
+ target = ""
678
+ } = options;
679
+ if (!enable)
680
+ return;
531
681
  if (!isAdmin()) {
532
682
  consola.warn("please run as administrator");
533
683
  return;
534
684
  }
685
+ if (!target) {
686
+ consola.fail("please provide target");
687
+ return;
688
+ }
689
+ const devServer = {
690
+ host: "localhost",
691
+ port: 8080,
692
+ ...compiler.options.devServer,
693
+ // @ts-expect-error vuecli
694
+ ...(_a = import_node_process5.default.VUE_CLI_SERVICE) == null ? void 0 : _a.projectOptions.devServer
695
+ };
696
+ const source = `${devServer.host}:${devServer.port}`;
697
+ if (caddy)
698
+ return;
699
+ const _close = (_b = compiler.close) == null ? void 0 : _b.bind(compiler);
700
+ if (_close) {
701
+ compiler.close = async (callback) => {
702
+ import_node_process5.default.on("exit", () => {
703
+ _close && _close(callback);
704
+ });
705
+ };
706
+ } else {
707
+ options.https = false;
708
+ }
709
+ caddy = new CaddyInstant();
710
+ caddy.run(source, target, {
711
+ ...options
712
+ }).then(() => {
713
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
714
+ compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
715
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
716
+ });
717
+ }).catch((e) => {
718
+ throw e;
719
+ });
720
+ },
721
+ async rspack(compiler) {
722
+ var _a, _b, _c;
535
723
  if (import_node_process5.default.env.NODE_ENV !== "development")
536
724
  return;
537
725
  const {
@@ -540,46 +728,37 @@ var unpluginFactory = (options) => ({
540
728
  } = options;
541
729
  if (!enable)
542
730
  return;
731
+ if (!isAdmin()) {
732
+ consola.warn("please run as administrator");
733
+ return;
734
+ }
543
735
  if (!target) {
544
736
  consola.fail("please provide target");
545
737
  return;
546
738
  }
547
- const nuxtCacheDir = `${pwd}/node_modules/.cache`;
548
- if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
549
- chmodRecursive(nuxtCacheDir, 511);
550
- try {
551
- const devServer = __spreadValues(__spreadValues({
552
- host: "localhost",
553
- port: 8080
554
- }, compiler.options.devServer), import_node_process5.default.VUE_CLI_SERVICE.projectOptions.devServer);
555
- const source = `${devServer.host}:${devServer.port}`;
556
- if (caddy)
557
- return;
558
- const _close = (_a = compiler.close) == null ? void 0 : _a.bind(compiler);
559
- if (_close) {
560
- compiler.close = async (callback) => {
561
- import_node_process5.default.on("exit", () => {
562
- _close && _close(callback);
563
- });
564
- };
565
- } else {
566
- options.https = false;
567
- }
568
- caddy = new CaddyInstant();
569
- caddy.run(source, target, __spreadValues({}, options)).then(() => {
570
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
571
- compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
572
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
573
- });
574
- }).catch((e) => {
575
- throw e;
576
- });
577
- } catch (e) {
578
- consola.error(e);
579
- }
739
+ const devServer = {
740
+ host: "localhost",
741
+ port: 8080,
742
+ ...compiler.options.devServer,
743
+ ...(_c = (_b = await ((_a = await import("@rsbuild/core")) == null ? void 0 : _a.loadConfig({
744
+ cwd
745
+ }))) == null ? void 0 : _b.content) == null ? void 0 : _c.server
746
+ };
747
+ const source = `${devServer.host}:${devServer.port}`;
748
+ if (caddy)
749
+ return;
750
+ caddy = new CaddyInstant();
751
+ caddy.run(source, target, {
752
+ ...options
753
+ }).then(() => {
754
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
755
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
756
+ }).catch((e) => {
757
+ throw e;
758
+ });
580
759
  }
581
760
  });
582
761
 
583
762
  // src/rollup.ts
584
763
  var rollup_default = (0, import_unplugin2.createRollupPlugin)(unpluginFactory);
585
- exports.default = module.exports;
764
+ module.exports = module.exports;exports.default = module.exports;