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/README.md CHANGED
@@ -220,7 +220,17 @@ export default defineNuxtConfig({
220
220
  })
221
221
  ```
222
222
 
223
- > This module works for both Nuxt 2 and [Nuxt Vite](https://github.com/nuxt/vite)
223
+ **!important:** You need to update the `nuxt dev` script by adding `--fork=false` to enable host restoration.
224
+
225
+ ```json
226
+ // package.json
227
+
228
+ {
229
+ "scripts": {
230
+ "dev": "nuxt dev --fork=false"
231
+ }
232
+ }
233
+ ```
224
234
 
225
235
  <br></details>
226
236
 
@@ -19,19 +19,16 @@ import { readFile, unlink, writeFile } from "node:fs/promises";
19
19
  import { got } from "got-cjs";
20
20
  import { HttpProxyAgent } from "http-proxy-agent";
21
21
  import { HttpsProxyAgent } from "https-proxy-agent";
22
- import kill from "kill-port";
23
22
  import * as lockfile from "proper-lockfile";
24
23
 
25
24
  // src/host/index.ts
26
25
  import process from "node:process";
27
26
  import { exec } from "node:child_process";
28
27
  import { chmodSync } from "node:fs";
29
- import _Hosts from "hosts-so-easy";
30
- var Hosts = _Hosts.default || _Hosts;
31
- var hostInstance = new Hosts({
32
- debounceTime: 50,
33
- hostsFile: getPathOfSystemHostsPath()
34
- });
28
+ import { promisify } from "node:util";
29
+ import hostile from "hostile";
30
+ var _setHost = promisify(hostile.set);
31
+ var _removeHost = promisify(hostile.remove);
35
32
  function getPathOfSystemHostsPath() {
36
33
  return process.platform === "win32" ? `${process.env.windir || "C:\\WINDOWS"}\\system32\\drivers\\etc\\hosts` : "/etc/hosts";
37
34
  }
@@ -52,20 +49,18 @@ async function syncHost() {
52
49
  async function addHost(ip, host) {
53
50
  if (process.platform !== "win32")
54
51
  chmodSync(getPathOfSystemHostsPath(), 511);
55
- hostInstance.add("127.0.0.1", "localhost");
56
- hostInstance.add(ip, host);
52
+ await _setHost("127.0.0.1", "localhost");
53
+ await _setHost(ip, host);
57
54
  if (ip === "localhost")
58
- hostInstance.add("127.0.0.1", host);
59
- await hostInstance.updateFinish();
55
+ await _setHost("127.0.0.1", host);
60
56
  return await syncHost();
61
57
  }
62
58
  async function removeHost(ip, host) {
63
59
  if (process.platform !== "win32")
64
60
  chmodSync(getPathOfSystemHostsPath(), 511);
65
- hostInstance.remove(ip, host);
61
+ await _removeHost(ip, host);
66
62
  if (ip === "localhost")
67
- hostInstance.remove("127.0.0.1", host);
68
- await hostInstance.updateFinish();
63
+ await _removeHost("127.0.0.1", host);
69
64
  return await syncHost();
70
65
  }
71
66
 
@@ -199,6 +194,7 @@ var supportList = [
199
194
 
200
195
  // src/caddy/utils.ts
201
196
  import { createServer } from "node:net";
197
+ import { exec as exec2 } from "node:child_process";
202
198
  import ora from "ora";
203
199
  var spinner;
204
200
  function tryPort(port) {
@@ -219,6 +215,18 @@ function tryPort(port) {
219
215
  }
220
216
  });
221
217
  }
218
+ async function kill(port) {
219
+ return new Promise((resolve, reject) => {
220
+ exec2(`npx @maxbo/free-port ${port} -s`, (err, stdout, stderr) => {
221
+ if (err) {
222
+ reject(err);
223
+ } else {
224
+ consola.log(stdout);
225
+ resolve();
226
+ }
227
+ });
228
+ });
229
+ }
222
230
  function getNumber(number) {
223
231
  if (number <= 0)
224
232
  return "0%";
@@ -371,6 +379,8 @@ var CaddyInstant = class {
371
379
  showCaddyLog = false,
372
380
  https = false
373
381
  } = options || {};
382
+ this._source = source;
383
+ this._target = target;
374
384
  if (!this.inited)
375
385
  await this.init();
376
386
  if (!await addHost(source.split(":")[0], target.split(":")[0]))
@@ -391,24 +401,19 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
391
401
  writeFileSync(caddyLockFilePath, "");
392
402
  if (!this.locked) {
393
403
  if (await tryPort(443))
394
- await kill(443, "tcp");
404
+ await kill(443);
395
405
  if ((process3.platform === "win32" || !https) && await tryPort(80))
396
- await kill(80, "tcp");
406
+ await kill(80);
397
407
  }
398
408
  return new Promise((resolve, reject) => {
409
+ var _a, _b;
399
410
  const setupCleanup = () => {
400
411
  process3.on("SIGINT", async () => {
401
- if (!restore || this.stoped)
412
+ if (this.stoped)
402
413
  return;
403
- await this.baseCleanup();
414
+ process3.stdin.resume();
404
415
  try {
405
- if (await removeHost(source.split(":")[0], target.split(":")[0]))
406
- consola.success("Restore host success.\n");
407
- else
408
- consola.fail("Restore host failed.\n");
409
- } catch (e) {
410
- consola.error(e);
411
- consola.fail("Restore host failed.\n");
416
+ await this.baseCleanup(restore);
412
417
  } finally {
413
418
  this.stoped = true;
414
419
  process3.nextTick(() => {
@@ -420,28 +425,25 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
420
425
  process3.exit = async (code) => {
421
426
  const port = Number(source.split(":")[1]);
422
427
  if (!Number.isNaN(port) && await tryPort(port))
423
- await kill(port, "tcp");
428
+ await kill(port);
424
429
  const pwd = process3.cwd();
425
430
  const viteCacheDir = `${pwd}/node_modules/.vite`;
426
431
  if (existsSync(viteCacheDir))
427
- chmodRecursive(viteCacheDir, 511);
432
+ await chmodRecursive(viteCacheDir, 511).catch(() => {
433
+ });
428
434
  const nuxtCacheDir = `${pwd}/.nuxt`;
429
435
  if (existsSync(nuxtCacheDir))
430
- await chmodRecursive(nuxtCacheDir, 511);
436
+ await chmodRecursive(nuxtCacheDir, 511).catch(() => {
437
+ });
431
438
  const webpackCacheDir = `${pwd}/node_modules/.cache`;
432
439
  if (existsSync(webpackCacheDir))
433
- chmodRecursive(webpackCacheDir, 511);
434
- if (!restore || this.stoped)
440
+ await chmodRecursive(webpackCacheDir, 511).catch(() => {
441
+ });
442
+ if (this.stoped)
435
443
  return originalExit(code);
436
- await this.baseCleanup();
444
+ process3.stdin.resume();
437
445
  try {
438
- if (await removeHost(source.split(":")[0], target.split(":")[0]))
439
- consola.success("Restore host success.\n");
440
- else
441
- consola.fail("Restore host failed.\n");
442
- } catch (e) {
443
- consola.error(e);
444
- consola.fail("Restore host failed.\n");
446
+ await this.baseCleanup();
445
447
  } finally {
446
448
  this.stoped = true;
447
449
  process3.nextTick(() => {
@@ -451,12 +453,12 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
451
453
  };
452
454
  };
453
455
  if (!this.locked) {
454
- const child = process3.platform !== "win32" ? spawn("sudo", ["-E", caddyPath, "run", "--config", caddyFilePath, "--watch"]) : spawn(caddyPath, ["run", "--config", caddyFilePath, "--watch"]);
456
+ this._caddyChild = process3.platform !== "win32" ? spawn("sudo", ["-E", caddyPath, "run", "--config", caddyFilePath, "--watch"]) : spawn(caddyPath, ["run", "--config", caddyFilePath, "--watch"]);
455
457
  lockfile.lockSync(caddyLockFilePath);
456
- child.on("error", (err) => {
458
+ this._caddyChild.on("error", (err) => {
457
459
  return reject(err);
458
460
  });
459
- child.stderr.on("data", (data) => {
461
+ (_a = this._caddyChild.stderr) == null ? void 0 : _a.on("data", (data) => {
460
462
  const lines = data.toString().split("\n").map((line) => line.trim());
461
463
  for (const line of lines) {
462
464
  showCaddyLog && line && console.info(line);
@@ -471,7 +473,7 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
471
473
  return resolve();
472
474
  }
473
475
  });
474
- child.stdout.on("data", (_data) => {
476
+ (_b = this._caddyChild.stdout) == null ? void 0 : _b.on("data", (_data) => {
475
477
  consola.info(_data.toString());
476
478
  resolve();
477
479
  });
@@ -483,13 +485,30 @@ ${target.split(":")[0]}${https ? "" : ":80"} {
483
485
  }
484
486
  });
485
487
  }
486
- async baseCleanup() {
487
- if (!this.locked) {
488
+ async baseCleanup(restore = true) {
489
+ var _a;
490
+ try {
491
+ if (restore && this._source && this._target) {
492
+ if (await removeHost(this._source.split(":")[0], this._target.split(":")[0]))
493
+ consola.success("Restore host success.\n");
494
+ else
495
+ consola.fail("Restore host failed.\n");
496
+ }
497
+ } catch (e) {
498
+ consola.error(e);
499
+ consola.fail("Restore host failed.\n");
500
+ }
501
+ if (!this.locked && !this.stoped) {
502
+ (_a = this._caddyChild) == null ? void 0 : _a.kill();
488
503
  try {
489
- await lockfile.unlock(caddyLockFilePath);
490
- await unlink(caddyFilePath);
491
- await unlink(caddyLockFilePath);
504
+ await lockfile.unlock(caddyLockFilePath).catch(() => {
505
+ });
506
+ await unlink(caddyFilePath).catch(() => {
507
+ });
508
+ await unlink(caddyLockFilePath).catch(() => {
509
+ });
492
510
  } catch (e) {
511
+ consola.error(e);
493
512
  }
494
513
  }
495
514
  }
package/dist/esbuild.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/esbuild.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-QMYDD3HL.js";
3
+ } from "./chunk-ZDL3GBDQ.js";
4
4
  import "./chunk-2VGRJ3QS.js";
5
5
 
6
6
  // src/esbuild.ts