unplugin-https-reverse-proxy 1.0.0-beta.16 → 1.0.0-beta.18

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/index.cjs CHANGED
@@ -83,24 +83,21 @@ function isAdmin() {
83
83
  var import_node_os2 = require("os");
84
84
  var import_node_process4 = __toESM(require("process"), 1);
85
85
  var import_node_fs2 = require("fs");
86
- var import_node_child_process3 = require("child_process");
86
+ var import_node_child_process4 = require("child_process");
87
87
  var import_promises2 = require("fs/promises");
88
88
  var import_got_cjs = require("got-cjs");
89
89
  var import_http_proxy_agent = require("http-proxy-agent");
90
90
  var import_https_proxy_agent = require("https-proxy-agent");
91
- var import_kill_port = __toESM(require("kill-port"), 1);
92
91
  var lockfile = __toESM(require("proper-lockfile"), 1);
93
92
 
94
93
  // src/host/index.ts
95
94
  var import_node_process2 = __toESM(require("process"), 1);
96
95
  var import_node_child_process2 = require("child_process");
97
96
  var import_node_fs = require("fs");
98
- var import_hosts_so_easy = __toESM(require("hosts-so-easy"), 1);
99
- var Hosts = import_hosts_so_easy.default.default || import_hosts_so_easy.default;
100
- var hostInstance = new Hosts({
101
- debounceTime: 50,
102
- hostsFile: getPathOfSystemHostsPath()
103
- });
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);
104
101
  function getPathOfSystemHostsPath() {
105
102
  return import_node_process2.default.platform === "win32" ? `${import_node_process2.default.env.windir || "C:\\WINDOWS"}\\system32\\drivers\\etc\\hosts` : "/etc/hosts";
106
103
  }
@@ -121,20 +118,18 @@ async function syncHost() {
121
118
  async function addHost(ip, host) {
122
119
  if (import_node_process2.default.platform !== "win32")
123
120
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
124
- hostInstance.add("127.0.0.1", "localhost");
125
- hostInstance.add(ip, host);
121
+ await _setHost("127.0.0.1", "localhost");
122
+ await _setHost(ip, host);
126
123
  if (ip === "localhost")
127
- hostInstance.add("127.0.0.1", host);
128
- await hostInstance.updateFinish();
124
+ await _setHost("127.0.0.1", host);
129
125
  return await syncHost();
130
126
  }
131
127
  async function removeHost(ip, host) {
132
128
  if (import_node_process2.default.platform !== "win32")
133
129
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
134
- hostInstance.remove(ip, host);
130
+ await _removeHost(ip, host);
135
131
  if (ip === "localhost")
136
- hostInstance.remove("127.0.0.1", host);
137
- await hostInstance.updateFinish();
132
+ await _removeHost("127.0.0.1", host);
138
133
  return await syncHost();
139
134
  }
140
135
 
@@ -268,6 +263,7 @@ var supportList = [
268
263
 
269
264
  // src/caddy/utils.ts
270
265
  var import_node_net = require("net");
266
+ var import_node_child_process3 = require("child_process");
271
267
  var import_ora = __toESM(require("ora"), 1);
272
268
  var spinner;
273
269
  function tryPort(port) {
@@ -288,6 +284,18 @@ function tryPort(port) {
288
284
  }
289
285
  });
290
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
+ }
291
299
  function getNumber(number) {
292
300
  if (number <= 0)
293
301
  return "0%";
@@ -371,7 +379,7 @@ function testCaddy() {
371
379
  if (!(0, import_node_fs2.existsSync)(caddyPath))
372
380
  return resolve(false);
373
381
  (0, import_node_fs2.chmodSync)(caddyPath, 511);
374
- const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process3.spawn)(caddyPath, []) : (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath]);
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]);
375
383
  child.on("close", () => {
376
384
  return resolve(false);
377
385
  });
@@ -440,6 +448,8 @@ var CaddyInstant = class {
440
448
  showCaddyLog = false,
441
449
  https = false
442
450
  } = options || {};
451
+ this._source = source;
452
+ this._target = target;
443
453
  if (!this.inited)
444
454
  await this.init();
445
455
  if (!await addHost(source.split(":")[0], target.split(":")[0]))
@@ -460,24 +470,19 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
460
470
  (0, import_node_fs2.writeFileSync)(caddyLockFilePath, "");
461
471
  if (!this.locked) {
462
472
  if (await tryPort(443))
463
- await (0, import_kill_port.default)(443, "tcp");
473
+ await kill(443);
464
474
  if ((import_node_process4.default.platform === "win32" || !https) && await tryPort(80))
465
- await (0, import_kill_port.default)(80, "tcp");
475
+ await kill(80);
466
476
  }
467
477
  return new Promise((resolve, reject) => {
478
+ var _a, _b;
468
479
  const setupCleanup = () => {
469
480
  import_node_process4.default.on("SIGINT", async () => {
470
- if (!restore || this.stoped)
481
+ if (this.stoped)
471
482
  return;
472
- await this.baseCleanup();
483
+ import_node_process4.default.stdin.resume();
473
484
  try {
474
- if (await removeHost(source.split(":")[0], target.split(":")[0]))
475
- consola.success("Restore host success.\n");
476
- else
477
- consola.fail("Restore host failed.\n");
478
- } catch (e) {
479
- consola.error(e);
480
- consola.fail("Restore host failed.\n");
485
+ await this.baseCleanup(restore);
481
486
  } finally {
482
487
  this.stoped = true;
483
488
  import_node_process4.default.nextTick(() => {
@@ -489,28 +494,25 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
489
494
  import_node_process4.default.exit = async (code) => {
490
495
  const port = Number(source.split(":")[1]);
491
496
  if (!Number.isNaN(port) && await tryPort(port))
492
- await (0, import_kill_port.default)(port, "tcp");
497
+ await kill(port);
493
498
  const pwd = import_node_process4.default.cwd();
494
499
  const viteCacheDir = `${pwd}/node_modules/.vite`;
495
500
  if ((0, import_node_fs2.existsSync)(viteCacheDir))
496
- chmodRecursive(viteCacheDir, 511);
501
+ await chmodRecursive(viteCacheDir, 511).catch(() => {
502
+ });
497
503
  const nuxtCacheDir = `${pwd}/.nuxt`;
498
504
  if ((0, import_node_fs2.existsSync)(nuxtCacheDir))
499
- await chmodRecursive(nuxtCacheDir, 511);
505
+ await chmodRecursive(nuxtCacheDir, 511).catch(() => {
506
+ });
500
507
  const webpackCacheDir = `${pwd}/node_modules/.cache`;
501
508
  if ((0, import_node_fs2.existsSync)(webpackCacheDir))
502
- chmodRecursive(webpackCacheDir, 511);
503
- if (!restore || this.stoped)
509
+ await chmodRecursive(webpackCacheDir, 511).catch(() => {
510
+ });
511
+ if (this.stoped)
504
512
  return originalExit(code);
505
- await this.baseCleanup();
513
+ import_node_process4.default.stdin.resume();
506
514
  try {
507
- if (await removeHost(source.split(":")[0], target.split(":")[0]))
508
- consola.success("Restore host success.\n");
509
- else
510
- consola.fail("Restore host failed.\n");
511
- } catch (e) {
512
- consola.error(e);
513
- consola.fail("Restore host failed.\n");
515
+ await this.baseCleanup();
514
516
  } finally {
515
517
  this.stoped = true;
516
518
  import_node_process4.default.nextTick(() => {
@@ -520,12 +522,12 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
520
522
  };
521
523
  };
522
524
  if (!this.locked) {
523
- const child = import_node_process4.default.platform !== "win32" ? (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath, "run", "--config", caddyFilePath, "--watch"]) : (0, import_node_child_process3.spawn)(caddyPath, ["run", "--config", caddyFilePath, "--watch"]);
525
+ 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"]);
524
526
  lockfile.lockSync(caddyLockFilePath);
525
- child.on("error", (err) => {
527
+ this._caddyChild.on("error", (err) => {
526
528
  return reject(err);
527
529
  });
528
- child.stderr.on("data", (data) => {
530
+ (_a = this._caddyChild.stderr) == null ? void 0 : _a.on("data", (data) => {
529
531
  const lines = data.toString().split("\n").map((line) => line.trim());
530
532
  for (const line of lines) {
531
533
  showCaddyLog && line && console.info(line);
@@ -540,7 +542,7 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
540
542
  return resolve();
541
543
  }
542
544
  });
543
- child.stdout.on("data", (_data) => {
545
+ (_b = this._caddyChild.stdout) == null ? void 0 : _b.on("data", (_data) => {
544
546
  consola.info(_data.toString());
545
547
  resolve();
546
548
  });
@@ -552,13 +554,30 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
552
554
  }
553
555
  });
554
556
  }
555
- async baseCleanup() {
556
- if (!this.locked) {
557
+ async baseCleanup(restore = true) {
558
+ var _a;
559
+ try {
560
+ if (restore && this._source && this._target) {
561
+ if (await removeHost(this._source.split(":")[0], this._target.split(":")[0]))
562
+ consola.success("Restore host success.\n");
563
+ else
564
+ consola.fail("Restore host failed.\n");
565
+ }
566
+ } catch (e) {
567
+ consola.error(e);
568
+ consola.fail("Restore host failed.\n");
569
+ }
570
+ if (!this.locked && !this.stoped) {
571
+ (_a = this._caddyChild) == null ? void 0 : _a.kill();
557
572
  try {
558
- await lockfile.unlock(caddyLockFilePath);
559
- await (0, import_promises2.unlink)(caddyFilePath);
560
- await (0, import_promises2.unlink)(caddyLockFilePath);
573
+ await lockfile.unlock(caddyLockFilePath).catch(() => {
574
+ });
575
+ await (0, import_promises2.unlink)(caddyFilePath).catch(() => {
576
+ });
577
+ await (0, import_promises2.unlink)(caddyLockFilePath).catch(() => {
578
+ });
561
579
  } catch (e) {
580
+ consola.error(e);
562
581
  }
563
582
  }
564
583
  }
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  unplugin,
4
4
  unpluginFactory,
5
5
  vitePrintUrls
6
- } from "./chunk-QMYDD3HL.js";
6
+ } from "./chunk-ZDL3GBDQ.js";
7
7
  import "./chunk-2VGRJ3QS.js";
8
8
  export {
9
9
  src_default as default,
package/dist/nuxt.cjs CHANGED
@@ -85,24 +85,21 @@ var import_picocolors = __toESM(require("picocolors"), 1);
85
85
  var import_node_os2 = require("os");
86
86
  var import_node_process4 = __toESM(require("process"), 1);
87
87
  var import_node_fs2 = require("fs");
88
- var import_node_child_process3 = require("child_process");
88
+ var import_node_child_process4 = require("child_process");
89
89
  var import_promises2 = require("fs/promises");
90
90
  var import_got_cjs = require("got-cjs");
91
91
  var import_http_proxy_agent = require("http-proxy-agent");
92
92
  var import_https_proxy_agent = require("https-proxy-agent");
93
- var import_kill_port = __toESM(require("kill-port"), 1);
94
93
  var lockfile = __toESM(require("proper-lockfile"), 1);
95
94
 
96
95
  // src/host/index.ts
97
96
  var import_node_process2 = __toESM(require("process"), 1);
98
97
  var import_node_child_process2 = require("child_process");
99
98
  var import_node_fs = require("fs");
100
- var import_hosts_so_easy = __toESM(require("hosts-so-easy"), 1);
101
- var Hosts = import_hosts_so_easy.default.default || import_hosts_so_easy.default;
102
- var hostInstance = new Hosts({
103
- debounceTime: 50,
104
- hostsFile: getPathOfSystemHostsPath()
105
- });
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);
106
103
  function getPathOfSystemHostsPath() {
107
104
  return import_node_process2.default.platform === "win32" ? `${import_node_process2.default.env.windir || "C:\\WINDOWS"}\\system32\\drivers\\etc\\hosts` : "/etc/hosts";
108
105
  }
@@ -123,20 +120,18 @@ async function syncHost() {
123
120
  async function addHost(ip, host) {
124
121
  if (import_node_process2.default.platform !== "win32")
125
122
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
126
- hostInstance.add("127.0.0.1", "localhost");
127
- hostInstance.add(ip, host);
123
+ await _setHost("127.0.0.1", "localhost");
124
+ await _setHost(ip, host);
128
125
  if (ip === "localhost")
129
- hostInstance.add("127.0.0.1", host);
130
- await hostInstance.updateFinish();
126
+ await _setHost("127.0.0.1", host);
131
127
  return await syncHost();
132
128
  }
133
129
  async function removeHost(ip, host) {
134
130
  if (import_node_process2.default.platform !== "win32")
135
131
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
136
- hostInstance.remove(ip, host);
132
+ await _removeHost(ip, host);
137
133
  if (ip === "localhost")
138
- hostInstance.remove("127.0.0.1", host);
139
- await hostInstance.updateFinish();
134
+ await _removeHost("127.0.0.1", host);
140
135
  return await syncHost();
141
136
  }
142
137
 
@@ -270,6 +265,7 @@ var supportList = [
270
265
 
271
266
  // src/caddy/utils.ts
272
267
  var import_node_net = require("net");
268
+ var import_node_child_process3 = require("child_process");
273
269
  var import_ora = __toESM(require("ora"), 1);
274
270
  var spinner;
275
271
  function tryPort(port) {
@@ -290,6 +286,18 @@ function tryPort(port) {
290
286
  }
291
287
  });
292
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
+ }
293
301
  function getNumber(number) {
294
302
  if (number <= 0)
295
303
  return "0%";
@@ -373,7 +381,7 @@ function testCaddy() {
373
381
  if (!(0, import_node_fs2.existsSync)(caddyPath))
374
382
  return resolve(false);
375
383
  (0, import_node_fs2.chmodSync)(caddyPath, 511);
376
- const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process3.spawn)(caddyPath, []) : (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath]);
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]);
377
385
  child.on("close", () => {
378
386
  return resolve(false);
379
387
  });
@@ -442,6 +450,8 @@ var CaddyInstant = class {
442
450
  showCaddyLog = false,
443
451
  https = false
444
452
  } = options || {};
453
+ this._source = source;
454
+ this._target = target;
445
455
  if (!this.inited)
446
456
  await this.init();
447
457
  if (!await addHost(source.split(":")[0], target.split(":")[0]))
@@ -462,24 +472,19 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
462
472
  (0, import_node_fs2.writeFileSync)(caddyLockFilePath, "");
463
473
  if (!this.locked) {
464
474
  if (await tryPort(443))
465
- await (0, import_kill_port.default)(443, "tcp");
475
+ await kill(443);
466
476
  if ((import_node_process4.default.platform === "win32" || !https) && await tryPort(80))
467
- await (0, import_kill_port.default)(80, "tcp");
477
+ await kill(80);
468
478
  }
469
479
  return new Promise((resolve, reject) => {
480
+ var _a, _b;
470
481
  const setupCleanup = () => {
471
482
  import_node_process4.default.on("SIGINT", async () => {
472
- if (!restore || this.stoped)
483
+ if (this.stoped)
473
484
  return;
474
- await this.baseCleanup();
485
+ import_node_process4.default.stdin.resume();
475
486
  try {
476
- if (await removeHost(source.split(":")[0], target.split(":")[0]))
477
- consola.success("Restore host success.\n");
478
- else
479
- consola.fail("Restore host failed.\n");
480
- } catch (e) {
481
- consola.error(e);
482
- consola.fail("Restore host failed.\n");
487
+ await this.baseCleanup(restore);
483
488
  } finally {
484
489
  this.stoped = true;
485
490
  import_node_process4.default.nextTick(() => {
@@ -491,28 +496,25 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
491
496
  import_node_process4.default.exit = async (code) => {
492
497
  const port = Number(source.split(":")[1]);
493
498
  if (!Number.isNaN(port) && await tryPort(port))
494
- await (0, import_kill_port.default)(port, "tcp");
499
+ await kill(port);
495
500
  const pwd = import_node_process4.default.cwd();
496
501
  const viteCacheDir = `${pwd}/node_modules/.vite`;
497
502
  if ((0, import_node_fs2.existsSync)(viteCacheDir))
498
- chmodRecursive(viteCacheDir, 511);
503
+ await chmodRecursive(viteCacheDir, 511).catch(() => {
504
+ });
499
505
  const nuxtCacheDir = `${pwd}/.nuxt`;
500
506
  if ((0, import_node_fs2.existsSync)(nuxtCacheDir))
501
- await chmodRecursive(nuxtCacheDir, 511);
507
+ await chmodRecursive(nuxtCacheDir, 511).catch(() => {
508
+ });
502
509
  const webpackCacheDir = `${pwd}/node_modules/.cache`;
503
510
  if ((0, import_node_fs2.existsSync)(webpackCacheDir))
504
- chmodRecursive(webpackCacheDir, 511);
505
- if (!restore || this.stoped)
511
+ await chmodRecursive(webpackCacheDir, 511).catch(() => {
512
+ });
513
+ if (this.stoped)
506
514
  return originalExit(code);
507
- await this.baseCleanup();
515
+ import_node_process4.default.stdin.resume();
508
516
  try {
509
- if (await removeHost(source.split(":")[0], target.split(":")[0]))
510
- consola.success("Restore host success.\n");
511
- else
512
- consola.fail("Restore host failed.\n");
513
- } catch (e) {
514
- consola.error(e);
515
- consola.fail("Restore host failed.\n");
517
+ await this.baseCleanup();
516
518
  } finally {
517
519
  this.stoped = true;
518
520
  import_node_process4.default.nextTick(() => {
@@ -522,12 +524,12 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
522
524
  };
523
525
  };
524
526
  if (!this.locked) {
525
- const child = import_node_process4.default.platform !== "win32" ? (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath, "run", "--config", caddyFilePath, "--watch"]) : (0, import_node_child_process3.spawn)(caddyPath, ["run", "--config", caddyFilePath, "--watch"]);
527
+ 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"]);
526
528
  lockfile.lockSync(caddyLockFilePath);
527
- child.on("error", (err) => {
529
+ this._caddyChild.on("error", (err) => {
528
530
  return reject(err);
529
531
  });
530
- child.stderr.on("data", (data) => {
532
+ (_a = this._caddyChild.stderr) == null ? void 0 : _a.on("data", (data) => {
531
533
  const lines = data.toString().split("\n").map((line) => line.trim());
532
534
  for (const line of lines) {
533
535
  showCaddyLog && line && console.info(line);
@@ -542,7 +544,7 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
542
544
  return resolve();
543
545
  }
544
546
  });
545
- child.stdout.on("data", (_data) => {
547
+ (_b = this._caddyChild.stdout) == null ? void 0 : _b.on("data", (_data) => {
546
548
  consola.info(_data.toString());
547
549
  resolve();
548
550
  });
@@ -554,13 +556,30 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
554
556
  }
555
557
  });
556
558
  }
557
- async baseCleanup() {
558
- if (!this.locked) {
559
+ async baseCleanup(restore = true) {
560
+ var _a;
561
+ try {
562
+ if (restore && this._source && this._target) {
563
+ if (await removeHost(this._source.split(":")[0], this._target.split(":")[0]))
564
+ consola.success("Restore host success.\n");
565
+ else
566
+ consola.fail("Restore host failed.\n");
567
+ }
568
+ } catch (e) {
569
+ consola.error(e);
570
+ consola.fail("Restore host failed.\n");
571
+ }
572
+ if (!this.locked && !this.stoped) {
573
+ (_a = this._caddyChild) == null ? void 0 : _a.kill();
559
574
  try {
560
- await lockfile.unlock(caddyLockFilePath);
561
- await (0, import_promises2.unlink)(caddyFilePath);
562
- await (0, import_promises2.unlink)(caddyLockFilePath);
575
+ await lockfile.unlock(caddyLockFilePath).catch(() => {
576
+ });
577
+ await (0, import_promises2.unlink)(caddyFilePath).catch(() => {
578
+ });
579
+ await (0, import_promises2.unlink)(caddyLockFilePath).catch(() => {
580
+ });
563
581
  } catch (e) {
582
+ consola.error(e);
564
583
  }
565
584
  }
566
585
  }
package/dist/nuxt.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  vitePrintUrls
3
- } from "./chunk-QMYDD3HL.js";
3
+ } from "./chunk-ZDL3GBDQ.js";
4
4
  import {
5
5
  consola,
6
6
  isAdmin