unplugin-https-reverse-proxy 0.0.12 → 0.0.13

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,8 +3,22 @@ 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;
6
7
  var __getProtoOf = Object.getPrototypeOf;
7
8
  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
+ };
8
22
  var __export = (target, all) => {
9
23
  for (var name in all)
10
24
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -45,6 +59,15 @@ var import_consola = require("consola");
45
59
  var consola = (0, import_consola.createConsola)({
46
60
  level: 3
47
61
  }).withTag("reverse-proxy");
62
+ function once(fn) {
63
+ let called = false;
64
+ return (...args) => {
65
+ if (called)
66
+ return;
67
+ called = true;
68
+ return fn(...args);
69
+ };
70
+ }
48
71
 
49
72
  // src/caddy/index.ts
50
73
  var import_node_os = require("os");
@@ -54,7 +77,7 @@ var import_node_child_process = require("child_process");
54
77
  var import_got_cjs = require("got-cjs");
55
78
  var import_http_proxy_agent = require("http-proxy-agent");
56
79
  var import_https_proxy_agent = require("https-proxy-agent");
57
- var hostile = __toESM(require("hostile"), 1);
80
+ var import_hosts_so_easy = __toESM(require("hosts-so-easy"), 1);
58
81
  var import_kill_port = __toESM(require("kill-port"), 1);
59
82
 
60
83
  // src/caddy/constants.ts
@@ -176,6 +199,8 @@ var supportList = [
176
199
 
177
200
  // src/caddy/utils.ts
178
201
  var import_node_net = require("net");
202
+ var import_ora = __toESM(require("ora"), 1);
203
+ var spinner;
179
204
  function tryPort(port) {
180
205
  return new Promise((resolve, reject) => {
181
206
  try {
@@ -194,8 +219,41 @@ function tryPort(port) {
194
219
  }
195
220
  });
196
221
  }
222
+ function getNumber(number) {
223
+ if (number <= 0)
224
+ return "0%";
225
+ else if (number < 100)
226
+ return `${number.toFixed(2)}%`;
227
+ else
228
+ return "100%";
229
+ }
230
+ function logProgress(percent) {
231
+ if (!spinner) {
232
+ consola.log("");
233
+ spinner = (0, import_ora.default)({
234
+ text: "Start Download Caddy: 0%",
235
+ indent: 2
236
+ });
237
+ spinner.start();
238
+ } else {
239
+ spinner.text = `Download Caddy: ${getNumber(Number(percent) * 100)}`;
240
+ }
241
+ }
242
+ function logProgressOver() {
243
+ if (!spinner) {
244
+ spinner = (0, import_ora.default)({
245
+ text: "Start Download Caddy: 100%",
246
+ indent: 2
247
+ });
248
+ spinner.start();
249
+ } else {
250
+ spinner.succeed();
251
+ consola.log("");
252
+ }
253
+ }
197
254
 
198
255
  // src/caddy/index.ts
256
+ var Hosts = import_hosts_so_easy.default.default || import_hosts_so_easy.default;
199
257
  async function download() {
200
258
  if (await testCaddy())
201
259
  return caddyPath;
@@ -219,7 +277,10 @@ async function download() {
219
277
  http: httpAgent,
220
278
  https: httpsAgent
221
279
  }
280
+ }).on("downloadProgress", (progress) => {
281
+ logProgress(progress.percent);
222
282
  }).pipe(file).on("finish", () => {
283
+ logProgressOver();
223
284
  if (import_node_process2.default.platform === "win32")
224
285
  return resolve(caddyPath);
225
286
  (0, import_node_fs.chmodSync)(caddyPath, 493);
@@ -234,7 +295,7 @@ function testCaddy() {
234
295
  if (!(0, import_node_fs.existsSync)(caddyPath))
235
296
  return resolve(false);
236
297
  (0, import_node_fs.chmodSync)(caddyPath, 493);
237
- const child = import_node_process2.default.platform === "win32" ? (0, import_node_child_process.spawn)(caddyPath, []) : (0, import_node_child_process.spawn)("sudo", [caddyPath]);
298
+ const child = import_node_process2.default.platform === "win32" ? (0, import_node_child_process.spawn)(caddyPath, []) : (0, import_node_child_process.spawn)("sudo", ["-E", caddyPath]);
238
299
  child.on("close", () => {
239
300
  return resolve(false);
240
301
  });
@@ -249,6 +310,8 @@ function testCaddy() {
249
310
  var CaddyInstant = class {
250
311
  constructor() {
251
312
  this.inited = false;
313
+ this.stoped = false;
314
+ this.host = new Hosts();
252
315
  testCaddy().then(() => {
253
316
  this.inited = true;
254
317
  });
@@ -257,52 +320,46 @@ var CaddyInstant = class {
257
320
  await download();
258
321
  this.inited = true;
259
322
  }
260
- async updateHost(ip, host) {
323
+ updateHost(ip, host) {
261
324
  ["localhost", "0.0.0.0"].includes(ip) && (ip = "127.0.0.1");
325
+ this.host.add(ip, host);
262
326
  return new Promise((resolve, reject) => {
263
- hostile.set(ip, host, (err) => {
264
- if (err) {
265
- return reject(err);
266
- } else {
267
- if (import_node_process2.default.platform === "win32") {
268
- (0, import_node_child_process.spawn)("ipconfig", ["/flushdns"]).on("error", (err2) => {
269
- return reject(err2);
270
- }).on("close", () => {
271
- resolve(true);
272
- });
273
- } else {
274
- (0, import_node_child_process.spawn)("sudo", ["killall", "-HUP", "mDNSResponder"]).on("error", (err2) => {
275
- return reject(err2);
276
- }).on("close", () => {
277
- resolve(true);
278
- });
279
- }
280
- }
281
- });
327
+ if (import_node_process2.default.platform === "win32") {
328
+ (0, import_node_child_process.spawn)("ipconfig", ["/flushdns"]).on("error", (err) => {
329
+ consola.error(err);
330
+ return resolve(false);
331
+ }).on("close", () => {
332
+ resolve(true);
333
+ });
334
+ } else {
335
+ (0, import_node_child_process.spawn)("sudo", ["-E", "killall", "-HUP", "mDNSResponder"]).on("error", (err) => {
336
+ consola.error(err);
337
+ return resolve(false);
338
+ }).on("close", () => {
339
+ resolve(true);
340
+ });
341
+ }
282
342
  });
283
343
  }
284
- async restoreHost(ip, host) {
344
+ restoreHost(ip, host) {
285
345
  ["localhost", "0.0.0.0"].includes(ip) && (ip = "127.0.0.1");
346
+ this.host.remove(ip, host);
286
347
  return new Promise((resolve, reject) => {
287
- hostile.remove(ip, host, (err) => {
288
- if (err) {
289
- return reject(err);
290
- } else {
291
- if (import_node_process2.default.platform === "win32") {
292
- (0, import_node_child_process.spawn)("ipconfig", ["/flushdns"]).on("error", (err2) => {
293
- return reject(err2);
294
- }).on("close", () => {
295
- resolve(true);
296
- });
297
- } else {
298
- (0, import_node_child_process.spawn)("sudo", ["killall", "-HUP", "mDNSResponder"]).on("error", (err2) => {
299
- return reject(err2);
300
- }).on("close", () => {
301
- resolve(true);
302
- });
303
- }
304
- }
305
- });
348
+ if (import_node_process2.default.platform === "win32") {
349
+ (0, import_node_child_process.spawn)("ipconfig", ["/flushdns"]).on("error", (err) => {
350
+ consola.error(err);
351
+ return resolve(false);
352
+ }).on("close", () => {
353
+ resolve(true);
354
+ });
355
+ } else {
356
+ (0, import_node_child_process.spawn)("sudo", ["-E", "killall", "-HUP", "mDNSResponder"]).on("error", (err) => {
357
+ consola.error(err);
358
+ return resolve(false);
359
+ }).on("close", () => {
360
+ resolve(true);
361
+ });
362
+ }
306
363
  });
307
364
  }
308
365
  /**
@@ -319,14 +376,15 @@ var CaddyInstant = class {
319
376
  } = options || {};
320
377
  if (!this.inited)
321
378
  await this.init();
322
- if (!await this.updateHost(source.split(":")[0], target.split(":")[0]))
323
- throw new Error("update host failed");
324
379
  if (await tryPort(443))
325
380
  await (0, import_kill_port.default)(443, "tcp");
326
381
  if (import_node_process2.default.platform === "win32" && await tryPort(80))
327
382
  await (0, import_kill_port.default)(80, "tcp");
383
+ if (!await this.updateHost(source.split(":")[0], target.split(":")[0]))
384
+ throw new Error("update host failed");
385
+ consola.success("update host success\n");
328
386
  return new Promise((resolve, reject) => {
329
- const child = import_node_process2.default.platform !== "win32" ? (0, import_node_child_process.spawn)("sudo", [caddyPath, "reverse-proxy", "--from", `${target.split(":")[0]}`, "--to", `${source}`, "--internal-certs"]) : (0, import_node_child_process.spawn)(caddyPath, ["reverse-proxy", "--from", `${target.split(":")[0]}`, "--to", `${source}`, "--internal-certs"]);
387
+ const child = import_node_process2.default.platform !== "win32" ? (0, import_node_child_process.spawn)("sudo", ["-E", caddyPath, "reverse-proxy", "--from", `${target.split(":")[0]}`, "--to", `${source}`, "--internal-certs"]) : (0, import_node_child_process.spawn)(caddyPath, ["reverse-proxy", "--from", `${target.split(":")[0]}`, "--to", `${source}`, "--internal-certs"]);
330
388
  child.on("error", (err) => {
331
389
  return reject(err);
332
390
  });
@@ -342,14 +400,21 @@ var CaddyInstant = class {
342
400
  });
343
401
  child.stdout.on("data", (_data) => {
344
402
  consola.info(_data.toString());
345
- resolve(async () => {
346
- if (!restore)
403
+ resolve(async (callback) => {
404
+ if (!restore || this.stoped)
347
405
  return;
348
- if (await this.restoreHost(source.split(":")[0], target.split(":")[0]))
349
- consola.success("restore host success\n");
350
- else
406
+ try {
407
+ if (await this.restoreHost(source.split(":")[0], target.split(":")[0]))
408
+ consola.success("restore host success\n");
409
+ else
410
+ consola.fail("restore host failed\n");
411
+ } catch (e) {
412
+ consola.error(e);
351
413
  consola.fail("restore host failed\n");
414
+ }
415
+ this.stoped = true;
352
416
  child.kill();
417
+ callback && callback();
353
418
  });
354
419
  });
355
420
  });
@@ -359,12 +424,53 @@ var CaddyInstant = class {
359
424
  // src/index.ts
360
425
  var config;
361
426
  var caddy;
427
+ var _stop;
428
+ var _servce;
429
+ function registerExit(clear) {
430
+ import_node_process3.default.on("beforeExit", async (_code) => {
431
+ await clear();
432
+ });
433
+ import_node_process3.default.on("uncaughtException", async (err) => {
434
+ console.error("An uncaught error occurred!", err.stack);
435
+ await clear();
436
+ import_node_process3.default.nextTick(() => {
437
+ import_node_process3.default.exit(1);
438
+ });
439
+ });
440
+ import_node_process3.default.on("unhandledRejection", async (reason, promise) => {
441
+ console.error("Unhandled Rejection at:", promise, "reason:", reason);
442
+ await clear();
443
+ import_node_process3.default.nextTick(() => {
444
+ import_node_process3.default.exit(1);
445
+ });
446
+ });
447
+ import_node_process3.default.once("SIGINT", async () => {
448
+ await clear();
449
+ import_node_process3.default.emit("SIGINT");
450
+ });
451
+ import_node_process3.default.once("SIGTERM", async () => {
452
+ await clear();
453
+ import_node_process3.default.emit("SIGTERM");
454
+ });
455
+ }
362
456
  var unpluginFactory = (options) => ({
363
457
  name: "unplugin-https-reverse-proxy",
364
- enforce: "post",
458
+ enforce: "pre",
365
459
  vite: {
366
460
  configResolved(_config) {
367
461
  config = _config;
462
+ registerExit(async () => {
463
+ var _a;
464
+ try {
465
+ _servce && _servce.close();
466
+ (_a = _servce.httpServer) == null ? void 0 : _a.close(async () => {
467
+ _stop && await _stop();
468
+ });
469
+ _stop && await _stop();
470
+ } catch (e) {
471
+ consola.error(e);
472
+ }
473
+ });
368
474
  },
369
475
  configureServer(server) {
370
476
  const {
@@ -377,6 +483,7 @@ var unpluginFactory = (options) => ({
377
483
  consola.fail("please provide target");
378
484
  return;
379
485
  }
486
+ _servce = server;
380
487
  const _printUrls = server.printUrls;
381
488
  server.printUrls = () => {
382
489
  var _a;
@@ -389,18 +496,16 @@ var unpluginFactory = (options) => ({
389
496
  }
390
497
  const base = server.config.base || "/";
391
498
  try {
392
- const caddy2 = new CaddyInstant();
393
- caddy2.run(source, target, {
499
+ if (caddy)
500
+ return;
501
+ caddy = new CaddyInstant();
502
+ caddy.run(source, target, {
394
503
  base,
395
504
  showCaddyLog: options.showCaddyLog
396
505
  }).then((stop) => {
397
506
  const colorUrl = (url2) => import_picocolors.default.green(url2.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
398
507
  consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`https://${target}${base}`)}`);
399
- import_node_process3.default.on("SIGINT", async () => {
400
- await stop();
401
- server.close();
402
- import_node_process3.default.exit();
403
- });
508
+ !_stop && (_stop = once(stop));
404
509
  }).catch((e) => {
405
510
  throw e;
406
511
  });
@@ -408,6 +513,9 @@ var unpluginFactory = (options) => ({
408
513
  consola.error(e);
409
514
  }
410
515
  };
516
+ },
517
+ async buildEnd() {
518
+ _stop && await _stop();
411
519
  }
412
520
  },
413
521
  webpack(compiler) {
@@ -422,23 +530,39 @@ var unpluginFactory = (options) => ({
422
530
  return;
423
531
  }
424
532
  try {
425
- const devServer = compiler.options.devServer || import_node_process3.default.VUE_CLI_SERVICE.projectOptions.devServer;
426
- const source = `${(devServer == null ? void 0 : devServer.host) || "127.0.0.1"}:${(devServer == null ? void 0 : devServer.port) || "8080"}`;
533
+ registerExit(async () => {
534
+ try {
535
+ _stop && await _stop();
536
+ } catch (e) {
537
+ consola.error(e);
538
+ }
539
+ });
540
+ const devServer = __spreadValues(__spreadValues({
541
+ host: "127.0.0.1",
542
+ port: 8080
543
+ }, compiler.options.devServer), import_node_process3.default.VUE_CLI_SERVICE.projectOptions.devServer);
544
+ const source = `${devServer.host}:${devServer.port}`;
427
545
  if (caddy)
428
546
  return;
547
+ const _close = compiler.close.bind(compiler);
548
+ compiler.close = async (callback) => {
549
+ try {
550
+ _stop && await _stop(() => {
551
+ _close && _close(callback);
552
+ });
553
+ } catch (e) {
554
+ consola.error(e);
555
+ import_node_process3.default.exit(1);
556
+ }
557
+ };
429
558
  caddy = new CaddyInstant();
430
559
  caddy.run(source, target, {
431
560
  showCaddyLog: options.showCaddyLog
432
561
  }).then((stop) => {
433
562
  const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
434
- compiler.hooks.done.tap("unplugin-https-reverse-proxy", (stats) => {
435
- if (stats.hasErrors())
436
- return;
563
+ compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
437
564
  consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`https://${target}`)}`);
438
- });
439
- import_node_process3.default.on("SIGINT", async () => {
440
- await stop();
441
- import_node_process3.default.exit();
565
+ !_stop && (_stop = once(stop));
442
566
  });
443
567
  }).catch((e) => {
444
568
  throw e;
package/dist/rollup.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-WUSTJHAZ.js";
4
- import "./chunk-3HKPYCSE.js";
3
+ } from "./chunk-DH6VMKTP.js";
4
+ import "./chunk-CQC5SSJR.js";
5
5
 
6
6
  // src/rollup.ts
7
7
  import { createRollupPlugin } from "unplugin";
package/dist/utils.cjs CHANGED
@@ -20,15 +20,26 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/utils.ts
21
21
  var utils_exports = {};
22
22
  __export(utils_exports, {
23
- consola: () => consola
23
+ consola: () => consola,
24
+ once: () => once
24
25
  });
25
26
  module.exports = __toCommonJS(utils_exports);
26
27
  var import_consola = require("consola");
27
28
  var consola = (0, import_consola.createConsola)({
28
29
  level: 3
29
30
  }).withTag("reverse-proxy");
31
+ function once(fn) {
32
+ let called = false;
33
+ return (...args) => {
34
+ if (called)
35
+ return;
36
+ called = true;
37
+ return fn(...args);
38
+ };
39
+ }
30
40
  // Annotate the CommonJS export names for ESM import in node:
31
41
  0 && (module.exports = {
32
- consola
42
+ consola,
43
+ once
33
44
  });
34
45
  exports.default = module.exports;
package/dist/utils.d.cts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as consola_dist_core from 'consola/dist/core';
2
2
 
3
3
  declare const consola: consola_dist_core.ConsolaInstance;
4
+ declare function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
4
5
 
5
- export { consola };
6
+ export { consola, once };
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as consola_dist_core from 'consola/dist/core';
2
2
 
3
3
  declare const consola: consola_dist_core.ConsolaInstance;
4
+ declare function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
4
5
 
5
- export { consola };
6
+ export { consola, once };
package/dist/utils.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import {
2
- consola
3
- } from "./chunk-3HKPYCSE.js";
2
+ consola,
3
+ once
4
+ } from "./chunk-CQC5SSJR.js";
4
5
  export {
5
- consola
6
+ consola,
7
+ once
6
8
  };