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.
package/dist/nuxt.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 });
@@ -47,16 +33,9 @@ __export(nuxt_exports, {
47
33
  default: () => nuxt_default
48
34
  });
49
35
  module.exports = __toCommonJS(nuxt_exports);
36
+ var import_node_process6 = __toESM(require("process"), 1);
50
37
  var import_kit = require("@nuxt/kit");
51
-
52
- // src/vite.ts
53
- var import_unplugin2 = require("unplugin");
54
-
55
- // src/index.ts
56
- var import_node_process5 = __toESM(require("process"), 1);
57
- var import_node_fs3 = require("fs");
58
- var import_unplugin = require("unplugin");
59
- var import_picocolors = __toESM(require("picocolors"), 1);
38
+ var import_schema = require("@nuxt/schema");
60
39
 
61
40
  // src/utils.ts
62
41
  var import_node_process = __toESM(require("process"), 1);
@@ -89,7 +68,7 @@ function isAdmin() {
89
68
  try {
90
69
  (0, import_node_child_process.execSync)("net session", { stdio: "ignore" });
91
70
  return true;
92
- } catch (e) {
71
+ } catch {
93
72
  return false;
94
73
  }
95
74
  } else {
@@ -97,26 +76,30 @@ function isAdmin() {
97
76
  }
98
77
  }
99
78
 
79
+ // src/index.ts
80
+ var import_node_process5 = __toESM(require("process"), 1);
81
+ var import_unplugin = require("unplugin");
82
+ var import_picocolors = __toESM(require("picocolors"), 1);
83
+
100
84
  // src/caddy/index.ts
101
- var import_node_os = require("os");
85
+ var import_node_os2 = require("os");
102
86
  var import_node_process4 = __toESM(require("process"), 1);
103
87
  var import_node_fs2 = require("fs");
104
- var import_node_child_process3 = require("child_process");
88
+ var import_node_child_process4 = require("child_process");
89
+ var import_promises2 = require("fs/promises");
105
90
  var import_got_cjs = require("got-cjs");
106
91
  var import_http_proxy_agent = require("http-proxy-agent");
107
92
  var import_https_proxy_agent = require("https-proxy-agent");
108
- var import_kill_port = __toESM(require("kill-port"), 1);
93
+ var lockfile = __toESM(require("proper-lockfile"), 1);
109
94
 
110
95
  // src/host/index.ts
111
96
  var import_node_process2 = __toESM(require("process"), 1);
112
97
  var import_node_child_process2 = require("child_process");
113
98
  var import_node_fs = require("fs");
114
- var import_hosts_so_easy = __toESM(require("hosts-so-easy"), 1);
115
- var Hosts = import_hosts_so_easy.default.default || import_hosts_so_easy.default;
116
- var hostInstance = new Hosts({
117
- debounceTime: 50,
118
- hostsFile: getPathOfSystemHostsPath()
119
- });
99
+ var import_node_util = require("util");
100
+ var import_hostile = __toESM(require("hostile"), 1);
101
+ var _setHost = (0, import_node_util.promisify)(import_hostile.default.set);
102
+ var _removeHost = (0, import_node_util.promisify)(import_hostile.default.remove);
120
103
  function getPathOfSystemHostsPath() {
121
104
  return import_node_process2.default.platform === "win32" ? `${import_node_process2.default.env.windir || "C:\\WINDOWS"}\\system32\\drivers\\etc\\hosts` : "/etc/hosts";
122
105
  }
@@ -137,29 +120,38 @@ async function syncHost() {
137
120
  async function addHost(ip, host) {
138
121
  if (import_node_process2.default.platform !== "win32")
139
122
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
140
- hostInstance.add("127.0.0.1", "localhost");
141
- hostInstance.add(ip, host);
123
+ await _setHost("127.0.0.1", "localhost");
124
+ await _setHost(ip, host);
142
125
  if (ip === "localhost")
143
- hostInstance.add("127.0.0.1", host);
144
- await hostInstance.updateFinish();
126
+ await _setHost("127.0.0.1", host);
145
127
  return await syncHost();
146
128
  }
147
129
  async function removeHost(ip, host) {
148
130
  if (import_node_process2.default.platform !== "win32")
149
131
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
150
- hostInstance.remove(ip, host);
132
+ await _removeHost(ip, host);
151
133
  if (ip === "localhost")
152
- hostInstance.remove("127.0.0.1", host);
153
- await hostInstance.updateFinish();
134
+ await _removeHost("127.0.0.1", host);
154
135
  return await syncHost();
155
136
  }
156
137
 
157
138
  // src/caddy/constants.ts
158
139
  var import_node_path2 = require("path");
140
+ var import_node_os = __toESM(require("os"), 1);
159
141
  var import_node_process3 = __toESM(require("process"), 1);
160
- var TEMP_DIR = import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp";
142
+ var import_dotenv_flow = __toESM(require("dotenv-flow"), 1);
143
+ import_dotenv_flow.default.config({
144
+ silent: true
145
+ });
146
+ function resolvePath(path2) {
147
+ if (path2.startsWith("~"))
148
+ return (0, import_node_path2.join)(import_node_os.default.homedir() || "", path2.slice(1));
149
+ return path2;
150
+ }
151
+ 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");
161
152
  var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
162
153
  var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
154
+ var caddyLockFilePath = (0, import_node_path2.join)(TEMP_DIR, "caddylock");
163
155
  var supportList = [
164
156
  {
165
157
  platform: "dragonfly",
@@ -273,6 +265,7 @@ var supportList = [
273
265
 
274
266
  // src/caddy/utils.ts
275
267
  var import_node_net = require("net");
268
+ var import_node_child_process3 = require("child_process");
276
269
  var import_ora = __toESM(require("ora"), 1);
277
270
  var spinner;
278
271
  function tryPort(port) {
@@ -293,6 +286,18 @@ function tryPort(port) {
293
286
  }
294
287
  });
295
288
  }
289
+ async function kill(port) {
290
+ return new Promise((resolve, reject) => {
291
+ (0, import_node_child_process3.exec)(`npx @maxbo/free-port ${port} -s`, (err, stdout, stderr) => {
292
+ if (err) {
293
+ reject(err);
294
+ } else {
295
+ consola.log(stdout);
296
+ resolve();
297
+ }
298
+ });
299
+ });
300
+ }
296
301
  function getNumber(number) {
297
302
  if (number <= 0)
298
303
  return "0%";
@@ -331,8 +336,12 @@ async function download() {
331
336
  if (await testCaddy())
332
337
  return caddyPath;
333
338
  return new Promise((resolve, reject) => {
339
+ if (!(0, import_node_fs2.existsSync)(TEMP_DIR)) {
340
+ (0, import_node_fs2.mkdirSync)(TEMP_DIR, { recursive: true });
341
+ (0, import_node_fs2.chmodSync)(TEMP_DIR, 511);
342
+ }
334
343
  const file = (0, import_node_fs2.createWriteStream)(caddyPath);
335
- let _platform = (0, import_node_os.platform)().toString();
344
+ let _platform = (0, import_node_os2.platform)().toString();
336
345
  _platform === "win32" && (_platform = "windows");
337
346
  let _arch = import_node_process4.default.arch.toString();
338
347
  _arch === "x64" && (_arch = "amd64");
@@ -345,6 +354,9 @@ async function download() {
345
354
  const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
346
355
  const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
347
356
  const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
357
+ const chmodCaddyOnce = once(() => {
358
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
359
+ });
348
360
  import_got_cjs.got.stream(dowmloadLink, {
349
361
  agent: {
350
362
  http: httpAgent,
@@ -352,11 +364,12 @@ async function download() {
352
364
  }
353
365
  }).on("downloadProgress", (progress) => {
354
366
  logProgress(progress.percent);
367
+ chmodCaddyOnce();
355
368
  }).pipe(file).on("finish", () => {
356
369
  logProgressOver();
357
370
  if (import_node_process4.default.platform === "win32")
358
371
  return resolve(caddyPath);
359
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
372
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
360
373
  resolve(caddyPath);
361
374
  }).on("error", (err) => {
362
375
  reject(err);
@@ -367,8 +380,8 @@ function testCaddy() {
367
380
  return new Promise((resolve, reject) => {
368
381
  if (!(0, import_node_fs2.existsSync)(caddyPath))
369
382
  return resolve(false);
370
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
371
- const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process3.spawn)(caddyPath, []) : (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath]);
383
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
384
+ const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process4.spawn)(caddyPath, []) : (0, import_node_child_process4.spawn)("sudo", ["-E", caddyPath]);
372
385
  child.on("close", () => {
373
386
  return resolve(false);
374
387
  });
@@ -384,9 +397,41 @@ var CaddyInstant = class {
384
397
  constructor() {
385
398
  this.inited = false;
386
399
  this.stoped = false;
387
- testCaddy().then(() => {
400
+ /** has locked */
401
+ this.locked = false;
402
+ testCaddy().then(async () => {
403
+ await this.getCaddyfile();
388
404
  this.inited = true;
389
405
  });
406
+ this.locked = this.checkLock();
407
+ }
408
+ checkLock() {
409
+ if (!(0, import_node_fs2.existsSync)(caddyLockFilePath))
410
+ (0, import_node_fs2.writeFileSync)(caddyLockFilePath, "");
411
+ return lockfile.checkSync(caddyLockFilePath);
412
+ }
413
+ async getCaddyfile() {
414
+ if (!(0, import_node_fs2.existsSync)(caddyFilePath))
415
+ await this.initCaddyfile();
416
+ this.caddyfile = await (0, import_promises2.readFile)(caddyFilePath, "utf-8");
417
+ return this.caddyfile;
418
+ }
419
+ async initCaddyfile() {
420
+ const contet = `
421
+ {
422
+ debug
423
+ auto_https disable_redirects
424
+ }
425
+
426
+ `;
427
+ await (0, import_promises2.writeFile)(caddyFilePath, contet);
428
+ this.caddyfile = contet;
429
+ }
430
+ async writeCaddyfile() {
431
+ return await (0, import_promises2.writeFile)(caddyFilePath, this.caddyfile || "");
432
+ }
433
+ async deleteCaddyfile() {
434
+ return await (0, import_promises2.unlink)(caddyFilePath);
390
435
  }
391
436
  async init() {
392
437
  await download();
@@ -405,146 +450,193 @@ var CaddyInstant = class {
405
450
  showCaddyLog = false,
406
451
  https = false
407
452
  } = options || {};
453
+ this._source = source;
454
+ this._target = target;
408
455
  if (!this.inited)
409
456
  await this.init();
410
- if (https && await tryPort(443))
411
- await (0, import_kill_port.default)(443, "tcp");
412
- if ((import_node_process4.default.platform === "win32" || !https) && await tryPort(80))
413
- await (0, import_kill_port.default)(80, "tcp");
414
457
  if (!await addHost(source.split(":")[0], target.split(":")[0]))
415
458
  throw new Error("update host failed");
416
459
  consola.success("update host success\n");
460
+ await this.getCaddyfile();
461
+ if (!(this.caddyfile || "").includes(`${target.split(":")[0]}${https ? "" : ":80"}`)) {
462
+ this.caddyfile = `${this.caddyfile}
463
+ ${target.split(":")[0]}${https ? "" : ":80"} {
464
+ ${https ? "tls internal" : ""}
465
+ reverse_proxy http://${source} {
466
+ health_uri /
467
+ health_interval 2s
468
+ health_timeout 5s
469
+ fail_duration 2s
470
+ unhealthy_status 502 503 504 404
471
+ lb_try_duration 3s
472
+ lb_try_interval 300ms
473
+
474
+ @error status 502 503 504 404
475
+ handle_response @error {
476
+ respond * 503 {
477
+ body "Service Unavailable"
478
+ close
479
+ }
480
+ }
481
+ }
482
+ }
483
+
484
+ `;
485
+ await this.writeCaddyfile();
486
+ }
487
+ if (!(0, import_node_fs2.existsSync)(caddyLockFilePath))
488
+ (0, import_node_fs2.writeFileSync)(caddyLockFilePath, "");
489
+ if (!this.locked) {
490
+ if (await tryPort(443))
491
+ await kill(443);
492
+ if ((import_node_process4.default.platform === "win32" || !https) && await tryPort(80))
493
+ await kill(80);
494
+ }
417
495
  return new Promise((resolve, reject) => {
418
- 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"]);
419
- child.on("error", (err) => {
420
- return reject(err);
421
- });
422
- import_node_process4.default.on("SIGINT", async () => {
423
- if (!restore || this.stoped)
424
- return;
425
- try {
426
- if (await removeHost(source.split(":")[0], target.split(":")[0]))
427
- consola.success("restore host success\n");
428
- else
429
- consola.fail("restore host failed\n");
430
- } catch (e) {
431
- consola.error(e);
432
- consola.fail("restore host failed\n");
433
- } finally {
434
- this.stoped = true;
435
- import_node_process4.default.nextTick(() => {
436
- import_node_process4.default.exit();
437
- });
438
- }
439
- });
440
- const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
441
- import_node_process4.default.exit = async (code) => {
442
- if (!restore || this.stoped)
443
- return originalExit(code);
444
- try {
445
- if (await removeHost(source.split(":")[0], target.split(":")[0]))
446
- consola.success("restore host success\n");
447
- else
448
- consola.fail("restore host failed\n");
449
- } catch (e) {
450
- consola.error(e);
451
- consola.fail("restore host failed\n");
452
- } finally {
453
- this.stoped = true;
454
- import_node_process4.default.nextTick(() => {
455
- originalExit(code);
456
- });
457
- }
496
+ var _a, _b;
497
+ const setupCleanup = () => {
498
+ import_node_process4.default.on("SIGINT", async () => {
499
+ if (this.stoped)
500
+ return;
501
+ import_node_process4.default.stdin.resume();
502
+ try {
503
+ await this.baseCleanup(restore);
504
+ } finally {
505
+ this.stoped = true;
506
+ import_node_process4.default.nextTick(() => {
507
+ import_node_process4.default.exit();
508
+ });
509
+ }
510
+ });
511
+ const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
512
+ import_node_process4.default.exit = async (code) => {
513
+ const port = Number(source.split(":")[1]);
514
+ if (!Number.isNaN(port) && await tryPort(port))
515
+ await kill(port);
516
+ const pwd = import_node_process4.default.cwd();
517
+ const viteCacheDir = `${pwd}/node_modules/.vite`;
518
+ if ((0, import_node_fs2.existsSync)(viteCacheDir))
519
+ await chmodRecursive(viteCacheDir, 511).catch(() => {
520
+ });
521
+ const nuxtCacheDir = `${pwd}/.nuxt`;
522
+ if ((0, import_node_fs2.existsSync)(nuxtCacheDir))
523
+ await chmodRecursive(nuxtCacheDir, 511).catch(() => {
524
+ });
525
+ const webpackCacheDir = `${pwd}/node_modules/.cache`;
526
+ if ((0, import_node_fs2.existsSync)(webpackCacheDir))
527
+ await chmodRecursive(webpackCacheDir, 511).catch(() => {
528
+ });
529
+ if (this.stoped)
530
+ return originalExit(code);
531
+ import_node_process4.default.stdin.resume();
532
+ try {
533
+ await this.baseCleanup();
534
+ } finally {
535
+ this.stoped = true;
536
+ import_node_process4.default.nextTick(() => {
537
+ originalExit(code);
538
+ });
539
+ }
540
+ };
458
541
  };
459
- child.stderr.on("data", (data) => {
460
- const lines = data.toString().split("\n").map((line) => line.trim());
461
- for (const line of lines) {
462
- showCaddyLog && line && console.info(line);
463
- if (line.includes("Error:") || line && JSON.parse(line).level === "error") {
464
- consola.error(line);
465
- return reject(line);
542
+ if (!this.locked) {
543
+ 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"]);
544
+ lockfile.lockSync(caddyLockFilePath);
545
+ this._caddyChild.on("error", (err) => {
546
+ return reject(err);
547
+ });
548
+ (_a = this._caddyChild.stderr) == null ? void 0 : _a.on("data", async (data) => {
549
+ const lines = data.toString().split("\n").map((line) => line.trim());
550
+ for (const line of lines) {
551
+ showCaddyLog && line && console.info(line);
552
+ if (line.includes("Error:") || line && JSON.parse(line).level === "error") {
553
+ if (line.includes("parsing caddyfile tokens")) {
554
+ consola.error("Caddyfile syntax error, please contact the author:\n", `${this.caddyfile}
555
+
556
+ ${line}
557
+ `);
558
+ await this.baseCleanup();
559
+ import_node_process4.default.exit();
560
+ } else {
561
+ consola.error(`${line}
562
+ `);
563
+ return reject(line);
564
+ }
565
+ }
566
+ if (line.includes("config file changed"))
567
+ consola.success("Detect added a new reverse proxy config, restarted.\n");
568
+ if (line.includes("caddy proxying") || line.includes("serving initial"))
569
+ return resolve();
466
570
  }
467
- }
468
- });
469
- child.stdout.on("data", (_data) => {
470
- consola.info(_data.toString());
471
- resolve();
472
- });
571
+ });
572
+ (_b = this._caddyChild.stdout) == null ? void 0 : _b.on("data", (_data) => {
573
+ consola.info(_data.toString());
574
+ resolve();
575
+ });
576
+ setupCleanup();
577
+ } else {
578
+ consola.info("Caddy is running in another process.");
579
+ setupCleanup();
580
+ return resolve();
581
+ }
473
582
  });
474
583
  }
584
+ async baseCleanup(restore = true) {
585
+ var _a;
586
+ try {
587
+ if (restore && this._source && this._target) {
588
+ if (await removeHost(this._source.split(":")[0], this._target.split(":")[0]))
589
+ consola.success("Restore host success.\n");
590
+ else
591
+ consola.fail("Restore host failed.\n");
592
+ }
593
+ } catch (e) {
594
+ consola.error(e);
595
+ consola.fail("Restore host failed.\n");
596
+ }
597
+ if (!this.locked && !this.stoped) {
598
+ (_a = this._caddyChild) == null ? void 0 : _a.kill();
599
+ try {
600
+ await lockfile.unlock(caddyLockFilePath).catch(() => {
601
+ });
602
+ await (0, import_promises2.unlink)(caddyFilePath).catch(() => {
603
+ });
604
+ await (0, import_promises2.unlink)(caddyLockFilePath).catch(() => {
605
+ });
606
+ } catch (e) {
607
+ consola.error(e);
608
+ }
609
+ }
610
+ }
475
611
  };
476
612
 
477
613
  // src/index.ts
478
- var config;
479
614
  var caddy;
480
- var pwd = import_node_process5.default.cwd();
481
- function vitePrintUrls(server, options, source, target, base = "/", _printUrls) {
615
+ var cwd = import_node_process5.default.cwd();
616
+ var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
482
617
  _printUrls == null ? void 0 : _printUrls();
483
- const viteCacheDir = `${pwd}/node_modules/.vite`;
484
- if ((0, import_node_fs3.existsSync)(viteCacheDir))
485
- chmodRecursive(viteCacheDir, 511);
486
- const nuxtCacheDir = `${pwd}/.nuxt`;
487
- if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
488
- chmodRecursive(nuxtCacheDir, 511);
489
- try {
490
- if (caddy)
491
- return;
492
- caddy = new CaddyInstant();
493
- caddy.run(source, target, __spreadValues({
494
- base
495
- }, options)).then(() => {
496
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
497
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
498
- }).catch((e) => {
499
- throw e;
500
- });
501
- } catch (e) {
502
- consola.error(e);
503
- }
504
- }
505
- var unpluginFactory = (options) => ({
506
- name: "unplugin-https-reverse-proxy",
507
- enforce: "pre",
508
- vite: {
509
- configResolved(_config) {
510
- config = _config;
511
- },
512
- configureServer(server) {
513
- var _a;
514
- if (!isAdmin()) {
515
- consola.warn("please run as administrator");
516
- return;
517
- }
518
- if (config.command === "build")
519
- return;
520
- const {
521
- enable = true,
522
- target = ""
523
- } = options;
524
- if (!enable)
525
- return;
526
- if (!target) {
527
- consola.fail("please provide target");
528
- return;
529
- }
530
- const _printUrls = server.printUrls.bind(server);
531
- let source = `localhost:${config.server.port || 5173}`;
532
- const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
533
- if (url) {
534
- const u = new URL(url);
535
- source = u.host;
536
- }
537
- const base = server.config.base || "/";
538
- server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls);
539
- }
618
+ if (caddy)
619
+ return;
620
+ caddy = new CaddyInstant();
621
+ caddy.run(source, target, {
622
+ base,
623
+ ...options
624
+ }).then(() => {
625
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
626
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
627
+ }).catch((e) => {
628
+ throw e;
629
+ });
630
+ });
631
+
632
+ // src/nuxt.ts
633
+ var nuxt_default = (0, import_kit.defineNuxtModule)({
634
+ meta: {
635
+ name: "nuxt-unplugin-https-reverse-proxy",
636
+ configKey: "unpluginHttpsReverseProxy"
540
637
  },
541
- webpack(compiler) {
542
- var _a;
543
- if (!isAdmin()) {
544
- consola.warn("please run as administrator");
545
- return;
546
- }
547
- if (import_node_process5.default.env.NODE_ENV !== "development")
638
+ setup(options, _nuxt) {
639
+ if (import_node_process6.default.env.NODE_ENV !== "development")
548
640
  return;
549
641
  const {
550
642
  enable = true,
@@ -552,76 +644,20 @@ var unpluginFactory = (options) => ({
552
644
  } = options;
553
645
  if (!enable)
554
646
  return;
647
+ if (!isAdmin()) {
648
+ consola.warn("please run as administrator");
649
+ return;
650
+ }
555
651
  if (!target) {
556
652
  consola.fail("please provide target");
557
653
  return;
558
654
  }
559
- const nuxtCacheDir = `${pwd}/node_modules/.cache`;
560
- if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
561
- chmodRecursive(nuxtCacheDir, 511);
562
- try {
563
- const devServer = __spreadValues(__spreadValues({
564
- host: "localhost",
565
- port: 8080
566
- }, compiler.options.devServer), import_node_process5.default.VUE_CLI_SERVICE.projectOptions.devServer);
567
- const source = `${devServer.host}:${devServer.port}`;
568
- if (caddy)
569
- return;
570
- const _close = (_a = compiler.close) == null ? void 0 : _a.bind(compiler);
571
- if (_close) {
572
- compiler.close = async (callback) => {
573
- import_node_process5.default.on("exit", () => {
574
- _close && _close(callback);
575
- });
576
- };
577
- } else {
578
- options.https = false;
579
- }
580
- caddy = new CaddyInstant();
581
- caddy.run(source, target, __spreadValues({}, options)).then(() => {
582
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
583
- compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
584
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
585
- });
586
- }).catch((e) => {
587
- throw e;
588
- });
589
- } catch (e) {
590
- consola.error(e);
591
- }
592
- }
593
- });
594
-
595
- // src/vite.ts
596
- var vite_default = (0, import_unplugin2.createVitePlugin)(unpluginFactory);
597
-
598
- // src/webpack.ts
599
- var import_unplugin3 = require("unplugin");
600
- var webpack_default = (0, import_unplugin3.createWebpackPlugin)(unpluginFactory);
601
-
602
- // src/nuxt.ts
603
- var import_schema = require("@nuxt/schema");
604
- var nuxt_default = (0, import_kit.defineNuxtModule)({
605
- meta: {
606
- name: "nuxt-unplugin-https-reverse-proxy",
607
- configKey: "unpluginHttpsReverseProxy"
608
- },
609
- setup(options, _nuxt) {
610
- (0, import_kit.addVitePlugin)(() => vite_default(options));
611
- (0, import_kit.addWebpackPlugin)(() => webpack_default(options));
612
- let url = "";
613
655
  _nuxt.hook("listen", (_, listener) => {
614
- url = listener.url;
615
- });
616
- _nuxt.hook("vite:serverCreated", once((server) => {
656
+ const url = listener.url;
617
657
  const source = new URL(url).host;
618
- if (!options.target) {
619
- consola.fail("please provide target");
620
- return;
621
- }
622
658
  const base = _nuxt.options.app.baseURL || "/";
623
- vitePrintUrls(server, options, source, options.target, base);
624
- }));
659
+ vitePrintUrls(options, source, options.target, base);
660
+ });
625
661
  }
626
662
  });
627
- exports.default = module.exports;
663
+ module.exports = module.exports;exports.default = module.exports;