unplugin-https-reverse-proxy 1.0.0-beta.1 → 1.0.0-beta.12

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.d.cts CHANGED
@@ -1,9 +1,8 @@
1
1
  import * as _unplugin from 'unplugin';
2
2
  import { UnpluginFactory } from 'unplugin';
3
- import { ViteDevServer } from 'vite';
4
3
  import { Options } from './types.cjs';
5
4
 
6
- declare function vitePrintUrls(server: ViteDevServer, options: Options, source: string, target: string, base?: string, _printUrls?: () => void): void;
5
+ declare const vitePrintUrls: (options: Options, source: string, target: string, base?: string | undefined, _printUrls?: (() => void) | undefined) => void;
7
6
  declare const unpluginFactory: UnpluginFactory<Options>;
8
7
  declare const unplugin: _unplugin.UnpluginInstance<Options, boolean>;
9
8
 
package/dist/index.d.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import * as _unplugin from 'unplugin';
2
2
  import { UnpluginFactory } from 'unplugin';
3
- import { ViteDevServer } from 'vite';
4
3
  import { Options } from './types.js';
5
4
 
6
- declare function vitePrintUrls(server: ViteDevServer, options: Options, source: string, target: string, base?: string, _printUrls?: () => void): void;
5
+ declare const vitePrintUrls: (options: Options, source: string, target: string, base?: string | undefined, _printUrls?: (() => void) | undefined) => void;
7
6
  declare const unpluginFactory: UnpluginFactory<Options>;
8
7
  declare const unplugin: _unplugin.UnpluginInstance<Options, boolean>;
9
8
 
package/dist/index.js CHANGED
@@ -3,8 +3,8 @@ import {
3
3
  unplugin,
4
4
  unpluginFactory,
5
5
  vitePrintUrls
6
- } from "./chunk-DUCXH7CX.js";
7
- import "./chunk-NNO3ZUSU.js";
6
+ } from "./chunk-XLSIGDFU.js";
7
+ import "./chunk-RUJE3YFB.js";
8
8
  export {
9
9
  src_default as default,
10
10
  unplugin,
package/dist/nuxt.cjs CHANGED
@@ -47,24 +47,27 @@ __export(nuxt_exports, {
47
47
  default: () => nuxt_default
48
48
  });
49
49
  module.exports = __toCommonJS(nuxt_exports);
50
+ var import_node_process6 = __toESM(require("process"), 1);
50
51
  var import_kit = require("@nuxt/kit");
51
-
52
- // src/vite.ts
53
- var import_unplugin2 = require("unplugin");
54
-
55
- // src/index.ts
56
- var import_node_process5 = __toESM(require("process"), 1);
57
- var import_node_fs3 = require("fs");
58
- var import_unplugin = require("unplugin");
59
- var import_picocolors = __toESM(require("picocolors"), 1);
52
+ var import_schema = require("@nuxt/schema");
60
53
 
61
54
  // src/utils.ts
62
55
  var import_node_process = __toESM(require("process"), 1);
56
+ var import_node_path = __toESM(require("path"), 1);
57
+ var import_promises = require("fs/promises");
63
58
  var import_node_child_process = require("child_process");
64
59
  var import_consola = require("consola");
65
60
  var consola = (0, import_consola.createConsola)({
66
61
  level: 3
67
62
  }).withTag("reverse-proxy");
63
+ async function chmodRecursive(_path, mode) {
64
+ await (0, import_promises.chmod)(_path, mode);
65
+ if ((await (0, import_promises.stat)(_path)).isDirectory()) {
66
+ const children = await (0, import_promises.readdir)(_path);
67
+ for (const child of children)
68
+ await chmodRecursive(import_node_path.default.join(_path, child), mode);
69
+ }
70
+ }
68
71
  function once(fn) {
69
72
  let called = false;
70
73
  return (...args) => {
@@ -87,8 +90,13 @@ function isAdmin() {
87
90
  }
88
91
  }
89
92
 
93
+ // src/index.ts
94
+ var import_node_process5 = __toESM(require("process"), 1);
95
+ var import_unplugin = require("unplugin");
96
+ var import_picocolors = __toESM(require("picocolors"), 1);
97
+
90
98
  // src/caddy/index.ts
91
- var import_node_os = require("os");
99
+ var import_node_os2 = require("os");
92
100
  var import_node_process4 = __toESM(require("process"), 1);
93
101
  var import_node_fs2 = require("fs");
94
102
  var import_node_child_process3 = require("child_process");
@@ -145,11 +153,21 @@ async function removeHost(ip, host) {
145
153
  }
146
154
 
147
155
  // src/caddy/constants.ts
148
- var import_node_path = require("path");
156
+ var import_node_path2 = require("path");
157
+ var import_node_os = __toESM(require("os"), 1);
149
158
  var import_node_process3 = __toESM(require("process"), 1);
150
- var TEMP_DIR = import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp";
151
- var caddyPath = (0, import_node_path.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
152
- var caddyFilePath = (0, import_node_path.join)(TEMP_DIR, "CADDYFILE");
159
+ var import_dotenv_flow = __toESM(require("dotenv-flow"), 1);
160
+ import_dotenv_flow.default.config({
161
+ silent: true
162
+ });
163
+ function resolvePath(path2) {
164
+ if (path2.startsWith("~"))
165
+ return (0, import_node_path2.join)(import_node_os.default.homedir() || "", path2.slice(1));
166
+ return path2;
167
+ }
168
+ 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");
169
+ var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
170
+ var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
153
171
  var supportList = [
154
172
  {
155
173
  platform: "dragonfly",
@@ -321,8 +339,12 @@ async function download() {
321
339
  if (await testCaddy())
322
340
  return caddyPath;
323
341
  return new Promise((resolve, reject) => {
342
+ if (!(0, import_node_fs2.existsSync)(TEMP_DIR)) {
343
+ (0, import_node_fs2.mkdirSync)(TEMP_DIR, { recursive: true });
344
+ (0, import_node_fs2.chmodSync)(TEMP_DIR, 511);
345
+ }
324
346
  const file = (0, import_node_fs2.createWriteStream)(caddyPath);
325
- let _platform = (0, import_node_os.platform)().toString();
347
+ let _platform = (0, import_node_os2.platform)().toString();
326
348
  _platform === "win32" && (_platform = "windows");
327
349
  let _arch = import_node_process4.default.arch.toString();
328
350
  _arch === "x64" && (_arch = "amd64");
@@ -335,6 +357,9 @@ async function download() {
335
357
  const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
336
358
  const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
337
359
  const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
360
+ const chmodCaddyOnce = once(() => {
361
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
362
+ });
338
363
  import_got_cjs.got.stream(dowmloadLink, {
339
364
  agent: {
340
365
  http: httpAgent,
@@ -342,11 +367,12 @@ async function download() {
342
367
  }
343
368
  }).on("downloadProgress", (progress) => {
344
369
  logProgress(progress.percent);
370
+ chmodCaddyOnce();
345
371
  }).pipe(file).on("finish", () => {
346
372
  logProgressOver();
347
373
  if (import_node_process4.default.platform === "win32")
348
374
  return resolve(caddyPath);
349
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
375
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
350
376
  resolve(caddyPath);
351
377
  }).on("error", (err) => {
352
378
  reject(err);
@@ -357,7 +383,7 @@ function testCaddy() {
357
383
  return new Promise((resolve, reject) => {
358
384
  if (!(0, import_node_fs2.existsSync)(caddyPath))
359
385
  return resolve(false);
360
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
386
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
361
387
  const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process3.spawn)(caddyPath, []) : (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath]);
362
388
  child.on("close", () => {
363
389
  return resolve(false);
@@ -429,6 +455,19 @@ var CaddyInstant = class {
429
455
  });
430
456
  const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
431
457
  import_node_process4.default.exit = async (code) => {
458
+ const port = Number(source.split(":")[1]);
459
+ if (!Number.isNaN(port) && await tryPort(port))
460
+ await (0, import_kill_port.default)(port, "tcp");
461
+ const pwd = import_node_process4.default.cwd();
462
+ const viteCacheDir = `${pwd}/node_modules/.vite`;
463
+ if ((0, import_node_fs2.existsSync)(viteCacheDir))
464
+ chmodRecursive(viteCacheDir, 511);
465
+ const nuxtCacheDir = `${pwd}/.nuxt`;
466
+ if ((0, import_node_fs2.existsSync)(nuxtCacheDir))
467
+ await chmodRecursive(nuxtCacheDir, 511);
468
+ const webpackCacheDir = `${pwd}/node_modules/.cache`;
469
+ if ((0, import_node_fs2.existsSync)(webpackCacheDir))
470
+ chmodRecursive(webpackCacheDir, 511);
432
471
  if (!restore || this.stoped)
433
472
  return originalExit(code);
434
473
  try {
@@ -465,76 +504,31 @@ var CaddyInstant = class {
465
504
  };
466
505
 
467
506
  // src/index.ts
468
- var config;
469
507
  var caddy;
470
- function vitePrintUrls(server, options, source, target, base = "/", _printUrls) {
508
+ var cwd = import_node_process5.default.cwd();
509
+ var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
471
510
  _printUrls == null ? void 0 : _printUrls();
472
- const pwd = import_node_process5.default.cwd();
473
- const viteCacheDir = `${pwd}/node_modules/.vite`;
474
- if ((0, import_node_fs3.existsSync)(viteCacheDir))
475
- (0, import_node_fs3.chmodSync)(viteCacheDir, 511);
476
- const nuxtCacheDir = `${pwd}/.nuxt`;
477
- if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
478
- (0, import_node_fs3.chmodSync)(nuxtCacheDir, 511);
479
- try {
480
- if (caddy)
481
- return;
482
- caddy = new CaddyInstant();
483
- caddy.run(source, target, __spreadValues({
484
- base
485
- }, options)).then(() => {
486
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
487
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
488
- }).catch((e) => {
489
- throw e;
490
- });
491
- } catch (e) {
492
- consola.error(e);
493
- }
494
- }
495
- var unpluginFactory = (options) => ({
496
- name: "unplugin-https-reverse-proxy",
497
- enforce: "pre",
498
- vite: {
499
- configResolved(_config) {
500
- config = _config;
501
- },
502
- configureServer(server) {
503
- var _a;
504
- if (!isAdmin()) {
505
- consola.warn("please run as administrator");
506
- return;
507
- }
508
- if (config.command === "build")
509
- return;
510
- const {
511
- enable = true,
512
- target = ""
513
- } = options;
514
- if (!enable)
515
- return;
516
- if (!target) {
517
- consola.fail("please provide target");
518
- return;
519
- }
520
- const _printUrls = server.printUrls.bind(server);
521
- let source = `localhost:${config.server.port || 5173}`;
522
- const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
523
- if (url) {
524
- const u = new URL(url);
525
- source = u.host;
526
- }
527
- const base = server.config.base || "/";
528
- server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls);
529
- }
511
+ if (caddy)
512
+ return;
513
+ caddy = new CaddyInstant();
514
+ caddy.run(source, target, __spreadValues({
515
+ base
516
+ }, options)).then(() => {
517
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
518
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
519
+ }).catch((e) => {
520
+ throw e;
521
+ });
522
+ });
523
+
524
+ // src/nuxt.ts
525
+ var nuxt_default = (0, import_kit.defineNuxtModule)({
526
+ meta: {
527
+ name: "nuxt-unplugin-https-reverse-proxy",
528
+ configKey: "unpluginHttpsReverseProxy"
530
529
  },
531
- webpack(compiler) {
532
- var _a;
533
- if (!isAdmin()) {
534
- consola.warn("please run as administrator");
535
- return;
536
- }
537
- if (import_node_process5.default.env.NODE_ENV !== "development")
530
+ setup(options, _nuxt) {
531
+ if (import_node_process6.default.env.NODE_ENV !== "development")
538
532
  return;
539
533
  const {
540
534
  enable = true,
@@ -542,73 +536,20 @@ var unpluginFactory = (options) => ({
542
536
  } = options;
543
537
  if (!enable)
544
538
  return;
539
+ if (!isAdmin()) {
540
+ consola.warn("please run as administrator");
541
+ return;
542
+ }
545
543
  if (!target) {
546
544
  consola.fail("please provide target");
547
545
  return;
548
546
  }
549
- try {
550
- const devServer = __spreadValues(__spreadValues({
551
- host: "localhost",
552
- port: 8080
553
- }, compiler.options.devServer), import_node_process5.default.VUE_CLI_SERVICE.projectOptions.devServer);
554
- const source = `${devServer.host}:${devServer.port}`;
555
- if (caddy)
556
- return;
557
- const _close = (_a = compiler.close) == null ? void 0 : _a.bind(compiler);
558
- if (_close) {
559
- compiler.close = async (callback) => {
560
- import_node_process5.default.on("exit", () => {
561
- _close && _close(callback);
562
- });
563
- };
564
- } else {
565
- options.https = false;
566
- }
567
- caddy = new CaddyInstant();
568
- caddy.run(source, target, __spreadValues({}, options)).then(() => {
569
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
570
- compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
571
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
572
- });
573
- }).catch((e) => {
574
- throw e;
575
- });
576
- } catch (e) {
577
- consola.error(e);
578
- }
579
- }
580
- });
581
-
582
- // src/vite.ts
583
- var vite_default = (0, import_unplugin2.createVitePlugin)(unpluginFactory);
584
-
585
- // src/webpack.ts
586
- var import_unplugin3 = require("unplugin");
587
- var webpack_default = (0, import_unplugin3.createWebpackPlugin)(unpluginFactory);
588
-
589
- // src/nuxt.ts
590
- var import_schema = require("@nuxt/schema");
591
- var nuxt_default = (0, import_kit.defineNuxtModule)({
592
- meta: {
593
- name: "nuxt-unplugin-https-reverse-proxy",
594
- configKey: "unpluginHttpsReverseProxy"
595
- },
596
- setup(options, _nuxt) {
597
- (0, import_kit.addVitePlugin)(() => vite_default(options));
598
- (0, import_kit.addWebpackPlugin)(() => webpack_default(options));
599
- let url = "";
600
547
  _nuxt.hook("listen", (_, listener) => {
601
- url = listener.url;
602
- });
603
- _nuxt.hook("vite:serverCreated", once((server) => {
548
+ const url = listener.url;
604
549
  const source = new URL(url).host;
605
- if (!options.target) {
606
- consola.fail("please provide target");
607
- return;
608
- }
609
550
  const base = _nuxt.options.app.baseURL || "/";
610
- vitePrintUrls(server, options, source, options.target, base);
611
- }));
551
+ vitePrintUrls(options, source, options.target, base);
552
+ });
612
553
  }
613
554
  });
614
- exports.default = module.exports;
555
+ module.exports = module.exports;exports.default = module.exports;
package/dist/nuxt.js CHANGED
@@ -1,19 +1,14 @@
1
- import {
2
- vite_default
3
- } from "./chunk-Y5ZVZOMP.js";
4
- import {
5
- webpack_default
6
- } from "./chunk-QIKJQBZH.js";
7
1
  import {
8
2
  vitePrintUrls
9
- } from "./chunk-DUCXH7CX.js";
3
+ } from "./chunk-XLSIGDFU.js";
10
4
  import {
11
5
  consola,
12
- once
13
- } from "./chunk-NNO3ZUSU.js";
6
+ isAdmin
7
+ } from "./chunk-RUJE3YFB.js";
14
8
 
15
9
  // src/nuxt.ts
16
- import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from "@nuxt/kit";
10
+ import process from "process";
11
+ import { defineNuxtModule } from "@nuxt/kit";
17
12
  import "@nuxt/schema";
18
13
  var nuxt_default = defineNuxtModule({
19
14
  meta: {
@@ -21,21 +16,28 @@ var nuxt_default = defineNuxtModule({
21
16
  configKey: "unpluginHttpsReverseProxy"
22
17
  },
23
18
  setup(options, _nuxt) {
24
- addVitePlugin(() => vite_default(options));
25
- addWebpackPlugin(() => webpack_default(options));
26
- let url = "";
19
+ if (process.env.NODE_ENV !== "development")
20
+ return;
21
+ const {
22
+ enable = true,
23
+ target = ""
24
+ } = options;
25
+ if (!enable)
26
+ return;
27
+ if (!isAdmin()) {
28
+ consola.warn("please run as administrator");
29
+ return;
30
+ }
31
+ if (!target) {
32
+ consola.fail("please provide target");
33
+ return;
34
+ }
27
35
  _nuxt.hook("listen", (_, listener) => {
28
- url = listener.url;
29
- });
30
- _nuxt.hook("vite:serverCreated", once((server) => {
36
+ const url = listener.url;
31
37
  const source = new URL(url).host;
32
- if (!options.target) {
33
- consola.fail("please provide target");
34
- return;
35
- }
36
38
  const base = _nuxt.options.app.baseURL || "/";
37
- vitePrintUrls(server, options, source, options.target, base);
38
- }));
39
+ vitePrintUrls(options, source, options.target, base);
40
+ });
39
41
  }
40
42
  });
41
43
  export {