unplugin-https-reverse-proxy 1.0.0-beta.1 → 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/utils.d.cts CHANGED
@@ -1,7 +1,8 @@
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 chmodRecursive(_path: string, mode: number): Promise<void>;
4
5
  declare function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
5
6
  declare function isAdmin(): boolean | undefined;
6
7
 
7
- export { consola, isAdmin, once };
8
+ export { chmodRecursive, consola, isAdmin, once };
package/dist/utils.d.ts CHANGED
@@ -1,7 +1,8 @@
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 chmodRecursive(_path: string, mode: number): Promise<void>;
4
5
  declare function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T>;
5
6
  declare function isAdmin(): boolean | undefined;
6
7
 
7
- export { consola, isAdmin, once };
8
+ export { chmodRecursive, consola, isAdmin, once };
package/dist/utils.js CHANGED
@@ -1,9 +1,11 @@
1
1
  import {
2
+ chmodRecursive,
2
3
  consola,
3
4
  isAdmin,
4
5
  once
5
- } from "./chunk-NNO3ZUSU.js";
6
+ } from "./chunk-RUJE3YFB.js";
6
7
  export {
8
+ chmodRecursive,
7
9
  consola,
8
10
  isAdmin,
9
11
  once
package/dist/vite.cjs CHANGED
@@ -51,17 +51,35 @@ var import_unplugin2 = require("unplugin");
51
51
 
52
52
  // src/index.ts
53
53
  var import_node_process5 = __toESM(require("process"), 1);
54
- var import_node_fs3 = require("fs");
55
54
  var import_unplugin = require("unplugin");
56
55
  var import_picocolors = __toESM(require("picocolors"), 1);
57
56
 
58
57
  // src/utils.ts
59
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");
60
61
  var import_node_child_process = require("child_process");
61
62
  var import_consola = require("consola");
62
63
  var consola = (0, import_consola.createConsola)({
63
64
  level: 3
64
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
+ }
65
83
  function isAdmin() {
66
84
  if (import_node_process.default.platform === "win32") {
67
85
  try {
@@ -76,7 +94,7 @@ function isAdmin() {
76
94
  }
77
95
 
78
96
  // src/caddy/index.ts
79
- var import_node_os = require("os");
97
+ var import_node_os2 = require("os");
80
98
  var import_node_process4 = __toESM(require("process"), 1);
81
99
  var import_node_fs2 = require("fs");
82
100
  var import_node_child_process3 = require("child_process");
@@ -133,11 +151,21 @@ async function removeHost(ip, host) {
133
151
  }
134
152
 
135
153
  // src/caddy/constants.ts
136
- var import_node_path = require("path");
154
+ var import_node_path2 = require("path");
155
+ var import_node_os = __toESM(require("os"), 1);
137
156
  var import_node_process3 = __toESM(require("process"), 1);
138
- var TEMP_DIR = import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp";
139
- var caddyPath = (0, import_node_path.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
140
- 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");
141
169
  var supportList = [
142
170
  {
143
171
  platform: "dragonfly",
@@ -309,8 +337,12 @@ async function download() {
309
337
  if (await testCaddy())
310
338
  return caddyPath;
311
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
+ }
312
344
  const file = (0, import_node_fs2.createWriteStream)(caddyPath);
313
- let _platform = (0, import_node_os.platform)().toString();
345
+ let _platform = (0, import_node_os2.platform)().toString();
314
346
  _platform === "win32" && (_platform = "windows");
315
347
  let _arch = import_node_process4.default.arch.toString();
316
348
  _arch === "x64" && (_arch = "amd64");
@@ -323,6 +355,9 @@ async function download() {
323
355
  const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
324
356
  const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
325
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
+ });
326
361
  import_got_cjs.got.stream(dowmloadLink, {
327
362
  agent: {
328
363
  http: httpAgent,
@@ -330,11 +365,12 @@ async function download() {
330
365
  }
331
366
  }).on("downloadProgress", (progress) => {
332
367
  logProgress(progress.percent);
368
+ chmodCaddyOnce();
333
369
  }).pipe(file).on("finish", () => {
334
370
  logProgressOver();
335
371
  if (import_node_process4.default.platform === "win32")
336
372
  return resolve(caddyPath);
337
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
373
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
338
374
  resolve(caddyPath);
339
375
  }).on("error", (err) => {
340
376
  reject(err);
@@ -345,7 +381,7 @@ function testCaddy() {
345
381
  return new Promise((resolve, reject) => {
346
382
  if (!(0, import_node_fs2.existsSync)(caddyPath))
347
383
  return resolve(false);
348
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
384
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
349
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]);
350
386
  child.on("close", () => {
351
387
  return resolve(false);
@@ -417,6 +453,19 @@ var CaddyInstant = class {
417
453
  });
418
454
  const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
419
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);
420
469
  if (!restore || this.stoped)
421
470
  return originalExit(code);
422
471
  try {
@@ -455,31 +504,21 @@ var CaddyInstant = class {
455
504
  // src/index.ts
456
505
  var config;
457
506
  var caddy;
458
- function vitePrintUrls(server, options, source, target, base = "/", _printUrls) {
507
+ var cwd = import_node_process5.default.cwd();
508
+ var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
459
509
  _printUrls == null ? void 0 : _printUrls();
460
- const pwd = import_node_process5.default.cwd();
461
- const viteCacheDir = `${pwd}/node_modules/.vite`;
462
- if ((0, import_node_fs3.existsSync)(viteCacheDir))
463
- (0, import_node_fs3.chmodSync)(viteCacheDir, 511);
464
- const nuxtCacheDir = `${pwd}/.nuxt`;
465
- if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
466
- (0, import_node_fs3.chmodSync)(nuxtCacheDir, 511);
467
- try {
468
- if (caddy)
469
- return;
470
- caddy = new CaddyInstant();
471
- caddy.run(source, target, __spreadValues({
472
- base
473
- }, options)).then(() => {
474
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
475
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
476
- }).catch((e) => {
477
- throw e;
478
- });
479
- } catch (e) {
480
- consola.error(e);
481
- }
482
- }
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
+ });
483
522
  var unpluginFactory = (options) => ({
484
523
  name: "unplugin-https-reverse-proxy",
485
524
  enforce: "pre",
@@ -489,11 +528,7 @@ var unpluginFactory = (options) => ({
489
528
  },
490
529
  configureServer(server) {
491
530
  var _a;
492
- if (!isAdmin()) {
493
- consola.warn("please run as administrator");
494
- return;
495
- }
496
- if (config.command === "build")
531
+ if (config.command !== "serve")
497
532
  return;
498
533
  const {
499
534
  enable = true,
@@ -501,6 +536,10 @@ var unpluginFactory = (options) => ({
501
536
  } = options;
502
537
  if (!enable)
503
538
  return;
539
+ if (!isAdmin()) {
540
+ consola.warn("please run as administrator");
541
+ return;
542
+ }
504
543
  if (!target) {
505
544
  consola.fail("please provide target");
506
545
  return;
@@ -513,15 +552,56 @@ var unpluginFactory = (options) => ({
513
552
  source = u.host;
514
553
  }
515
554
  const base = server.config.base || "/";
516
- server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls);
555
+ server.printUrls = () => vitePrintUrls(options, source, target, base, _printUrls);
517
556
  }
518
557
  },
519
558
  webpack(compiler) {
520
- 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;
521
568
  if (!isAdmin()) {
522
569
  consola.warn("please run as administrator");
523
570
  return;
524
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;
525
605
  if (import_node_process5.default.env.NODE_ENV !== "development")
526
606
  return;
527
607
  const {
@@ -530,40 +610,30 @@ var unpluginFactory = (options) => ({
530
610
  } = options;
531
611
  if (!enable)
532
612
  return;
613
+ if (!isAdmin()) {
614
+ consola.warn("please run as administrator");
615
+ return;
616
+ }
533
617
  if (!target) {
534
618
  consola.fail("please provide target");
535
619
  return;
536
620
  }
537
- try {
538
- const devServer = __spreadValues(__spreadValues({
539
- host: "localhost",
540
- port: 8080
541
- }, compiler.options.devServer), import_node_process5.default.VUE_CLI_SERVICE.projectOptions.devServer);
542
- const source = `${devServer.host}:${devServer.port}`;
543
- if (caddy)
544
- return;
545
- const _close = (_a = compiler.close) == null ? void 0 : _a.bind(compiler);
546
- if (_close) {
547
- compiler.close = async (callback) => {
548
- import_node_process5.default.on("exit", () => {
549
- _close && _close(callback);
550
- });
551
- };
552
- } else {
553
- options.https = false;
554
- }
555
- caddy = new CaddyInstant();
556
- caddy.run(source, target, __spreadValues({}, options)).then(() => {
557
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
558
- compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
559
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
560
- });
561
- }).catch((e) => {
562
- throw e;
563
- });
564
- } catch (e) {
565
- consola.error(e);
566
- }
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
+ });
567
637
  }
568
638
  });
569
639
 
package/dist/vite.d.cts CHANGED
@@ -1,6 +1,5 @@
1
- import * as vite from 'vite';
2
1
  import { Options } from './types.cjs';
3
2
 
4
- declare const _default: (options: Options) => vite.Plugin | vite.Plugin[];
3
+ declare const _default: (options: Options) => any;
5
4
 
6
5
  export { _default as default };
package/dist/vite.d.ts CHANGED
@@ -1,6 +1,5 @@
1
- import * as vite from 'vite';
2
1
  import { Options } from './types.js';
3
2
 
4
- declare const _default: (options: Options) => vite.Plugin | vite.Plugin[];
3
+ declare const _default: (options: Options) => any;
5
4
 
6
5
  export { _default as default };
package/dist/vite.js CHANGED
@@ -1,8 +1,11 @@
1
1
  import {
2
- vite_default
3
- } from "./chunk-Y5ZVZOMP.js";
4
- import "./chunk-DUCXH7CX.js";
5
- import "./chunk-NNO3ZUSU.js";
2
+ unpluginFactory
3
+ } from "./chunk-XLSIGDFU.js";
4
+ import "./chunk-RUJE3YFB.js";
5
+
6
+ // src/vite.ts
7
+ import { createVitePlugin } from "unplugin";
8
+ var vite_default = createVitePlugin(unpluginFactory);
6
9
  export {
7
10
  vite_default as default
8
11
  };