unplugin-https-reverse-proxy 1.0.0-beta.0 → 1.0.0-beta.10

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
@@ -46,21 +46,40 @@ var src_exports = {};
46
46
  __export(src_exports, {
47
47
  default: () => src_default,
48
48
  unplugin: () => unplugin,
49
- unpluginFactory: () => unpluginFactory
49
+ unpluginFactory: () => unpluginFactory,
50
+ vitePrintUrls: () => vitePrintUrls
50
51
  });
51
52
  module.exports = __toCommonJS(src_exports);
52
53
  var import_node_process5 = __toESM(require("process"), 1);
53
- var import_node_fs3 = require("fs");
54
54
  var import_unplugin = require("unplugin");
55
55
  var import_picocolors = __toESM(require("picocolors"), 1);
56
56
 
57
57
  // src/utils.ts
58
58
  var import_node_process = __toESM(require("process"), 1);
59
+ var import_node_path = __toESM(require("path"), 1);
60
+ var import_promises = require("fs/promises");
59
61
  var import_node_child_process = require("child_process");
60
62
  var import_consola = require("consola");
61
63
  var consola = (0, import_consola.createConsola)({
62
64
  level: 3
63
65
  }).withTag("reverse-proxy");
66
+ async function chmodRecursive(_path, mode) {
67
+ await (0, import_promises.chmod)(_path, mode);
68
+ if ((await (0, import_promises.stat)(_path)).isDirectory()) {
69
+ const children = await (0, import_promises.readdir)(_path);
70
+ for (const child of children)
71
+ await chmodRecursive(import_node_path.default.join(_path, child), mode);
72
+ }
73
+ }
74
+ function once(fn) {
75
+ let called = false;
76
+ return (...args) => {
77
+ if (called)
78
+ return;
79
+ called = true;
80
+ return fn(...args);
81
+ };
82
+ }
64
83
  function isAdmin() {
65
84
  if (import_node_process.default.platform === "win32") {
66
85
  try {
@@ -75,7 +94,7 @@ function isAdmin() {
75
94
  }
76
95
 
77
96
  // src/caddy/index.ts
78
- var import_node_os = require("os");
97
+ var import_node_os2 = require("os");
79
98
  var import_node_process4 = __toESM(require("process"), 1);
80
99
  var import_node_fs2 = require("fs");
81
100
  var import_node_child_process3 = require("child_process");
@@ -103,7 +122,7 @@ async function syncHost() {
103
122
  import_node_process2.default.platform === "win32" ? "ipconfig /flushdns" : "sudo -E killall -HUP mDNSResponder",
104
123
  (error, stdout, stderr) => {
105
124
  if (error) {
106
- console.error(`exec error: ${error}`);
125
+ consola.error(`exec error: ${error}`);
107
126
  return resolve(false);
108
127
  }
109
128
  return resolve(true);
@@ -116,6 +135,8 @@ async function addHost(ip, host) {
116
135
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
117
136
  hostInstance.add("127.0.0.1", "localhost");
118
137
  hostInstance.add(ip, host);
138
+ if (ip === "localhost")
139
+ hostInstance.add("127.0.0.1", host);
119
140
  await hostInstance.updateFinish();
120
141
  return await syncHost();
121
142
  }
@@ -130,11 +151,21 @@ async function removeHost(ip, host) {
130
151
  }
131
152
 
132
153
  // src/caddy/constants.ts
133
- var import_node_path = require("path");
154
+ var import_node_path2 = require("path");
155
+ var import_node_os = __toESM(require("os"), 1);
134
156
  var import_node_process3 = __toESM(require("process"), 1);
135
- var TEMP_DIR = import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp";
136
- var caddyPath = (0, import_node_path.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
137
- var caddyFilePath = (0, import_node_path.join)(TEMP_DIR, "CADDYFILE");
157
+ var import_dotenv_flow = __toESM(require("dotenv-flow"), 1);
158
+ import_dotenv_flow.default.config({
159
+ silent: true
160
+ });
161
+ function resolvePath(path2) {
162
+ if (path2.startsWith("~"))
163
+ return (0, import_node_path2.join)(import_node_os.default.homedir() || "", path2.slice(1));
164
+ return path2;
165
+ }
166
+ 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");
167
+ var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
168
+ var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
138
169
  var supportList = [
139
170
  {
140
171
  platform: "dragonfly",
@@ -306,8 +337,12 @@ async function download() {
306
337
  if (await testCaddy())
307
338
  return caddyPath;
308
339
  return new Promise((resolve, reject) => {
340
+ if (!(0, import_node_fs2.existsSync)(TEMP_DIR)) {
341
+ (0, import_node_fs2.mkdirSync)(TEMP_DIR, { recursive: true });
342
+ (0, import_node_fs2.chmodSync)(TEMP_DIR, 511);
343
+ }
309
344
  const file = (0, import_node_fs2.createWriteStream)(caddyPath);
310
- let _platform = (0, import_node_os.platform)().toString();
345
+ let _platform = (0, import_node_os2.platform)().toString();
311
346
  _platform === "win32" && (_platform = "windows");
312
347
  let _arch = import_node_process4.default.arch.toString();
313
348
  _arch === "x64" && (_arch = "amd64");
@@ -320,6 +355,9 @@ async function download() {
320
355
  const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
321
356
  const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
322
357
  const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
358
+ const chmodCaddyOnce = once(() => {
359
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
360
+ });
323
361
  import_got_cjs.got.stream(dowmloadLink, {
324
362
  agent: {
325
363
  http: httpAgent,
@@ -327,11 +365,12 @@ async function download() {
327
365
  }
328
366
  }).on("downloadProgress", (progress) => {
329
367
  logProgress(progress.percent);
368
+ chmodCaddyOnce();
330
369
  }).pipe(file).on("finish", () => {
331
370
  logProgressOver();
332
371
  if (import_node_process4.default.platform === "win32")
333
372
  return resolve(caddyPath);
334
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
373
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
335
374
  resolve(caddyPath);
336
375
  }).on("error", (err) => {
337
376
  reject(err);
@@ -342,7 +381,7 @@ function testCaddy() {
342
381
  return new Promise((resolve, reject) => {
343
382
  if (!(0, import_node_fs2.existsSync)(caddyPath))
344
383
  return resolve(false);
345
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
384
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
346
385
  const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process3.spawn)(caddyPath, []) : (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath]);
347
386
  child.on("close", () => {
348
387
  return resolve(false);
@@ -414,6 +453,19 @@ var CaddyInstant = class {
414
453
  });
415
454
  const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
416
455
  import_node_process4.default.exit = async (code) => {
456
+ const port = Number(source.split(":")[1]);
457
+ if (!Number.isNaN(port) && await tryPort(port))
458
+ await (0, import_kill_port.default)(port, "tcp");
459
+ const pwd = import_node_process4.default.cwd();
460
+ const viteCacheDir = `${pwd}/node_modules/.vite`;
461
+ if ((0, import_node_fs2.existsSync)(viteCacheDir))
462
+ chmodRecursive(viteCacheDir, 511);
463
+ const nuxtCacheDir = `${pwd}/.nuxt`;
464
+ if ((0, import_node_fs2.existsSync)(nuxtCacheDir))
465
+ await chmodRecursive(nuxtCacheDir, 511);
466
+ const webpackCacheDir = `${pwd}/node_modules/.cache`;
467
+ if ((0, import_node_fs2.existsSync)(webpackCacheDir))
468
+ chmodRecursive(webpackCacheDir, 511);
417
469
  if (!restore || this.stoped)
418
470
  return originalExit(code);
419
471
  try {
@@ -452,7 +504,21 @@ var CaddyInstant = class {
452
504
  // src/index.ts
453
505
  var config;
454
506
  var caddy;
455
- var _servce;
507
+ var cwd = import_node_process5.default.cwd();
508
+ var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
509
+ _printUrls == null ? void 0 : _printUrls();
510
+ if (caddy)
511
+ return;
512
+ caddy = new CaddyInstant();
513
+ caddy.run(source, target, __spreadValues({
514
+ base
515
+ }, options)).then(() => {
516
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
517
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
518
+ }).catch((e) => {
519
+ throw e;
520
+ });
521
+ });
456
522
  var unpluginFactory = (options) => ({
457
523
  name: "unplugin-https-reverse-proxy",
458
524
  enforce: "pre",
@@ -461,11 +527,8 @@ var unpluginFactory = (options) => ({
461
527
  config = _config;
462
528
  },
463
529
  configureServer(server) {
464
- if (!isAdmin()) {
465
- consola.warn("please run as administrator");
466
- return;
467
- }
468
- if (config.command === "build")
530
+ var _a;
531
+ if (config.command !== "serve")
469
532
  return;
470
533
  const {
471
534
  enable = true,
@@ -473,50 +536,72 @@ var unpluginFactory = (options) => ({
473
536
  } = options;
474
537
  if (!enable)
475
538
  return;
539
+ if (!isAdmin()) {
540
+ consola.warn("please run as administrator");
541
+ return;
542
+ }
476
543
  if (!target) {
477
544
  consola.fail("please provide target");
478
545
  return;
479
546
  }
480
- _servce = server;
481
- const _printUrls = server.printUrls;
482
- server.printUrls = () => {
483
- var _a;
484
- _printUrls();
485
- const pwd = import_node_process5.default.cwd();
486
- const vireCacheDir = `${pwd}/node_modules/.vite`;
487
- if ((0, import_node_fs3.existsSync)(vireCacheDir))
488
- (0, import_node_fs3.chmodSync)(vireCacheDir, 511);
489
- let source = `localhost:${config.server.port}`;
490
- const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
491
- if (url) {
492
- const u = new URL(url);
493
- source = u.host;
494
- }
495
- const base = server.config.base || "/";
496
- try {
497
- if (caddy)
498
- return;
499
- caddy = new CaddyInstant();
500
- caddy.run(source, target, __spreadValues({
501
- base
502
- }, options)).then(() => {
503
- const colorUrl = (url2) => import_picocolors.default.green(url2.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
504
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
505
- }).catch((e) => {
506
- throw e;
507
- });
508
- } catch (e) {
509
- consola.error(e);
510
- }
511
- };
547
+ const _printUrls = server.printUrls.bind(server);
548
+ let source = `localhost:${config.server.port || 5173}`;
549
+ const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
550
+ if (url) {
551
+ const u = new URL(url);
552
+ source = u.host;
553
+ }
554
+ const base = server.config.base || "/";
555
+ server.printUrls = () => vitePrintUrls(options, source, target, base, _printUrls);
512
556
  }
513
557
  },
514
558
  webpack(compiler) {
515
- var _a;
559
+ var _a, _b;
560
+ if (import_node_process5.default.env.NODE_ENV !== "development")
561
+ return;
562
+ const {
563
+ enable = true,
564
+ target = ""
565
+ } = options;
566
+ if (!enable)
567
+ return;
516
568
  if (!isAdmin()) {
517
569
  consola.warn("please run as administrator");
518
570
  return;
519
571
  }
572
+ if (!target) {
573
+ consola.fail("please provide target");
574
+ return;
575
+ }
576
+ const devServer = __spreadValues(__spreadValues({
577
+ host: "localhost",
578
+ port: 8080
579
+ }, compiler.options.devServer), (_a = import_node_process5.default.VUE_CLI_SERVICE) == null ? void 0 : _a.projectOptions.devServer);
580
+ const source = `${devServer.host}:${devServer.port}`;
581
+ if (caddy)
582
+ return;
583
+ const _close = (_b = compiler.close) == null ? void 0 : _b.bind(compiler);
584
+ if (_close) {
585
+ compiler.close = async (callback) => {
586
+ import_node_process5.default.on("exit", () => {
587
+ _close && _close(callback);
588
+ });
589
+ };
590
+ } else {
591
+ options.https = false;
592
+ }
593
+ caddy = new CaddyInstant();
594
+ caddy.run(source, target, __spreadValues({}, options)).then(() => {
595
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
596
+ compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
597
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
598
+ });
599
+ }).catch((e) => {
600
+ throw e;
601
+ });
602
+ },
603
+ async rspack(compiler) {
604
+ var _a, _b, _c;
520
605
  if (import_node_process5.default.env.NODE_ENV !== "development")
521
606
  return;
522
607
  const {
@@ -525,40 +610,30 @@ var unpluginFactory = (options) => ({
525
610
  } = options;
526
611
  if (!enable)
527
612
  return;
613
+ if (!isAdmin()) {
614
+ consola.warn("please run as administrator");
615
+ return;
616
+ }
528
617
  if (!target) {
529
618
  consola.fail("please provide target");
530
619
  return;
531
620
  }
532
- try {
533
- const devServer = __spreadValues(__spreadValues({
534
- host: "localhost",
535
- port: 8080
536
- }, compiler.options.devServer), import_node_process5.default.VUE_CLI_SERVICE.projectOptions.devServer);
537
- const source = `${devServer.host}:${devServer.port}`;
538
- if (caddy)
539
- return;
540
- const _close = (_a = compiler.close) == null ? void 0 : _a.bind(compiler);
541
- if (_close) {
542
- compiler.close = async (callback) => {
543
- import_node_process5.default.on("exit", () => {
544
- _close && _close(callback);
545
- });
546
- };
547
- } else {
548
- options.https = false;
549
- }
550
- caddy = new CaddyInstant();
551
- caddy.run(source, target, __spreadValues({}, options)).then(() => {
552
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
553
- compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
554
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
555
- });
556
- }).catch((e) => {
557
- throw e;
558
- });
559
- } catch (e) {
560
- consola.error(e);
561
- }
621
+ const devServer = __spreadValues(__spreadValues({
622
+ host: "localhost",
623
+ port: 8080
624
+ }, compiler.options.devServer), (_c = (_b = await ((_a = await import("@rsbuild/core")) == null ? void 0 : _a.loadConfig({
625
+ cwd
626
+ }))) == null ? void 0 : _b.content) == null ? void 0 : _c.server);
627
+ const source = `${devServer.host}:${devServer.port}`;
628
+ if (caddy)
629
+ return;
630
+ caddy = new CaddyInstant();
631
+ caddy.run(source, target, __spreadValues({}, options)).then(() => {
632
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
633
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
634
+ }).catch((e) => {
635
+ throw e;
636
+ });
562
637
  }
563
638
  });
564
639
  var unplugin = /* @__PURE__ */ (0, import_unplugin.createUnplugin)(unpluginFactory);
@@ -566,6 +641,7 @@ var src_default = unplugin;
566
641
  // Annotate the CommonJS export names for ESM import in node:
567
642
  0 && (module.exports = {
568
643
  unplugin,
569
- unpluginFactory
644
+ unpluginFactory,
645
+ vitePrintUrls
570
646
  });
571
647
  exports.default = module.exports;
package/dist/index.d.cts CHANGED
@@ -2,7 +2,8 @@ import * as _unplugin from 'unplugin';
2
2
  import { UnpluginFactory } from 'unplugin';
3
3
  import { Options } from './types.cjs';
4
4
 
5
+ declare const vitePrintUrls: (options: Options, source: string, target: string, base?: string | undefined, _printUrls?: (() => void) | undefined) => void;
5
6
  declare const unpluginFactory: UnpluginFactory<Options>;
6
7
  declare const unplugin: _unplugin.UnpluginInstance<Options, boolean>;
7
8
 
8
- export { unplugin as default, unplugin, unpluginFactory };
9
+ export { unplugin as default, unplugin, unpluginFactory, vitePrintUrls };
package/dist/index.d.ts CHANGED
@@ -2,7 +2,8 @@ import * as _unplugin from 'unplugin';
2
2
  import { UnpluginFactory } from 'unplugin';
3
3
  import { Options } from './types.js';
4
4
 
5
+ declare const vitePrintUrls: (options: Options, source: string, target: string, base?: string | undefined, _printUrls?: (() => void) | undefined) => void;
5
6
  declare const unpluginFactory: UnpluginFactory<Options>;
6
7
  declare const unplugin: _unplugin.UnpluginInstance<Options, boolean>;
7
8
 
8
- export { unplugin as default, unplugin, unpluginFactory };
9
+ export { unplugin as default, unplugin, unpluginFactory, vitePrintUrls };
package/dist/index.js CHANGED
@@ -1,11 +1,13 @@
1
1
  import {
2
2
  src_default,
3
3
  unplugin,
4
- unpluginFactory
5
- } from "./chunk-KYMRDRAJ.js";
6
- import "./chunk-3RTK3FSZ.js";
4
+ unpluginFactory,
5
+ vitePrintUrls
6
+ } from "./chunk-XLSIGDFU.js";
7
+ import "./chunk-RUJE3YFB.js";
7
8
  export {
8
9
  src_default as default,
9
10
  unplugin,
10
- unpluginFactory
11
+ unpluginFactory,
12
+ vitePrintUrls
11
13
  };