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

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/webpack.cjs CHANGED
@@ -57,11 +57,21 @@ var import_picocolors = __toESM(require("picocolors"), 1);
57
57
 
58
58
  // src/utils.ts
59
59
  var import_node_process = __toESM(require("process"), 1);
60
+ var import_node_path = __toESM(require("path"), 1);
61
+ var import_promises = require("fs/promises");
60
62
  var import_node_child_process = require("child_process");
61
63
  var import_consola = require("consola");
62
64
  var consola = (0, import_consola.createConsola)({
63
65
  level: 3
64
66
  }).withTag("reverse-proxy");
67
+ async function chmodRecursive(_path, mode) {
68
+ await (0, import_promises.chmod)(_path, mode);
69
+ if ((await (0, import_promises.stat)(_path)).isDirectory()) {
70
+ const children = await (0, import_promises.readdir)(_path);
71
+ for (const child of children)
72
+ await chmodRecursive(import_node_path.default.join(_path, child), mode);
73
+ }
74
+ }
65
75
  function isAdmin() {
66
76
  if (import_node_process.default.platform === "win32") {
67
77
  try {
@@ -104,7 +114,7 @@ async function syncHost() {
104
114
  import_node_process2.default.platform === "win32" ? "ipconfig /flushdns" : "sudo -E killall -HUP mDNSResponder",
105
115
  (error, stdout, stderr) => {
106
116
  if (error) {
107
- console.error(`exec error: ${error}`);
117
+ consola.error(`exec error: ${error}`);
108
118
  return resolve(false);
109
119
  }
110
120
  return resolve(true);
@@ -117,6 +127,8 @@ async function addHost(ip, host) {
117
127
  (0, import_node_fs.chmodSync)(getPathOfSystemHostsPath(), 511);
118
128
  hostInstance.add("127.0.0.1", "localhost");
119
129
  hostInstance.add(ip, host);
130
+ if (ip === "localhost")
131
+ hostInstance.add("127.0.0.1", host);
120
132
  await hostInstance.updateFinish();
121
133
  return await syncHost();
122
134
  }
@@ -131,11 +143,11 @@ async function removeHost(ip, host) {
131
143
  }
132
144
 
133
145
  // src/caddy/constants.ts
134
- var import_node_path = require("path");
146
+ var import_node_path2 = require("path");
135
147
  var import_node_process3 = __toESM(require("process"), 1);
136
148
  var TEMP_DIR = import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp";
137
- var caddyPath = (0, import_node_path.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
138
- var caddyFilePath = (0, import_node_path.join)(TEMP_DIR, "CADDYFILE");
149
+ var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
150
+ var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
139
151
  var supportList = [
140
152
  {
141
153
  platform: "dragonfly",
@@ -453,7 +465,31 @@ var CaddyInstant = class {
453
465
  // src/index.ts
454
466
  var config;
455
467
  var caddy;
456
- var _servce;
468
+ var pwd = import_node_process5.default.cwd();
469
+ function vitePrintUrls(server, options, source, target, base = "/", _printUrls) {
470
+ _printUrls == null ? void 0 : _printUrls();
471
+ const viteCacheDir = `${pwd}/node_modules/.vite`;
472
+ if ((0, import_node_fs3.existsSync)(viteCacheDir))
473
+ chmodRecursive(viteCacheDir, 511);
474
+ const nuxtCacheDir = `${pwd}/.nuxt`;
475
+ if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
476
+ chmodRecursive(nuxtCacheDir, 511);
477
+ try {
478
+ if (caddy)
479
+ return;
480
+ caddy = new CaddyInstant();
481
+ caddy.run(source, target, __spreadValues({
482
+ base
483
+ }, options)).then(() => {
484
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
485
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
486
+ }).catch((e) => {
487
+ throw e;
488
+ });
489
+ } catch (e) {
490
+ consola.error(e);
491
+ }
492
+ }
457
493
  var unpluginFactory = (options) => ({
458
494
  name: "unplugin-https-reverse-proxy",
459
495
  enforce: "pre",
@@ -462,6 +498,7 @@ var unpluginFactory = (options) => ({
462
498
  config = _config;
463
499
  },
464
500
  configureServer(server) {
501
+ var _a;
465
502
  if (!isAdmin()) {
466
503
  consola.warn("please run as administrator");
467
504
  return;
@@ -478,38 +515,15 @@ var unpluginFactory = (options) => ({
478
515
  consola.fail("please provide target");
479
516
  return;
480
517
  }
481
- _servce = server;
482
- const _printUrls = server.printUrls;
483
- server.printUrls = () => {
484
- var _a;
485
- _printUrls();
486
- const pwd = import_node_process5.default.cwd();
487
- const vireCacheDir = `${pwd}/node_modules/.vite`;
488
- if ((0, import_node_fs3.existsSync)(vireCacheDir))
489
- (0, import_node_fs3.chmodSync)(vireCacheDir, 511);
490
- let source = `localhost:${config.server.port}`;
491
- const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
492
- if (url) {
493
- const u = new URL(url);
494
- source = u.host;
495
- }
496
- const base = server.config.base || "/";
497
- try {
498
- if (caddy)
499
- return;
500
- caddy = new CaddyInstant();
501
- caddy.run(source, target, __spreadValues({
502
- base
503
- }, options)).then(() => {
504
- const colorUrl = (url2) => import_picocolors.default.green(url2.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
505
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
506
- }).catch((e) => {
507
- throw e;
508
- });
509
- } catch (e) {
510
- consola.error(e);
511
- }
512
- };
518
+ const _printUrls = server.printUrls.bind(server);
519
+ let source = `localhost:${config.server.port || 5173}`;
520
+ const url = (_a = server.resolvedUrls) == null ? void 0 : _a.local[0];
521
+ if (url) {
522
+ const u = new URL(url);
523
+ source = u.host;
524
+ }
525
+ const base = server.config.base || "/";
526
+ server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls);
513
527
  }
514
528
  },
515
529
  webpack(compiler) {
@@ -530,6 +544,9 @@ var unpluginFactory = (options) => ({
530
544
  consola.fail("please provide target");
531
545
  return;
532
546
  }
547
+ const nuxtCacheDir = `${pwd}/node_modules/.cache`;
548
+ if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
549
+ chmodRecursive(nuxtCacheDir, 511);
533
550
  try {
534
551
  const devServer = __spreadValues(__spreadValues({
535
552
  host: "localhost",
package/dist/webpack.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  webpack_default
3
- } from "./chunk-AXBUFN6T.js";
4
- import "./chunk-KYMRDRAJ.js";
5
- import "./chunk-3RTK3FSZ.js";
3
+ } from "./chunk-VGLXVBHU.js";
4
+ import "./chunk-N6ZBBGYD.js";
5
+ import "./chunk-RUJE3YFB.js";
6
6
  export {
7
7
  webpack_default as default
8
8
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unplugin-https-reverse-proxy",
3
3
  "type": "module",
4
- "version": "1.0.0-beta.0",
4
+ "version": "1.0.0-beta.2",
5
5
  "packageManager": "pnpm@8.6.12",
6
6
  "description": "",
7
7
  "license": "MIT",
@@ -141,6 +141,7 @@
141
141
  "build:fix": "esno scripts/postbuild.ts",
142
142
  "lint": "eslint .",
143
143
  "play": "npm -C examples/vite run dev",
144
+ "play:nuxt": "npm -C examples/nuxt run dev",
144
145
  "play:vuecli": "npm -C examples/vuecli run serve",
145
146
  "play:vuecli4": "npm -C examples/vuecli4 run serve",
146
147
  "release": "bumpp",
package/src/host/index.ts CHANGED
@@ -4,6 +4,7 @@ import { chmodSync } from 'node:fs'
4
4
 
5
5
  // @ts-expect-error no type
6
6
  import _Hosts from 'hosts-so-easy'
7
+ import { consola } from '../utils'
7
8
 
8
9
  const Hosts = _Hosts.default || _Hosts
9
10
 
@@ -26,7 +27,7 @@ async function syncHost() {
26
27
  : 'sudo -E killall -HUP mDNSResponder',
27
28
  (error, stdout, stderr) => {
28
29
  if (error) {
29
- console.error(`exec error: ${error}`)
30
+ consola.error(`exec error: ${error}`)
30
31
  return resolve(false)
31
32
  }
32
33
  return resolve(true)
@@ -39,6 +40,8 @@ export async function addHost(ip: string, host: string) {
39
40
  chmodSync(getPathOfSystemHostsPath(), 0o777)
40
41
  hostInstance.add('127.0.0.1', 'localhost')
41
42
  hostInstance.add(ip, host)
43
+ if (ip === 'localhost')
44
+ hostInstance.add('127.0.0.1', host)
42
45
  await hostInstance.updateFinish()
43
46
  return await syncHost()
44
47
  }
package/src/index.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import process from 'node:process'
2
- import { chmodSync, existsSync } from 'node:fs'
2
+ import { existsSync } from 'node:fs'
3
3
  import type { UnpluginFactory } from 'unplugin'
4
4
  import { createUnplugin } from 'unplugin'
5
5
  import type { ResolvedConfig, ViteDevServer } from 'vite'
6
6
  import c from 'picocolors'
7
- import { consola, isAdmin } from './utils'
7
+ import { chmodRecursive, consola, isAdmin } from './utils'
8
8
  import type { Options } from './types'
9
9
  import { CaddyInstant } from './caddy'
10
10
 
@@ -12,7 +12,44 @@ let config: ResolvedConfig
12
12
 
13
13
  let caddy: CaddyInstant
14
14
 
15
- let _servce: ViteDevServer
15
+ const pwd = process.cwd()
16
+
17
+ export function vitePrintUrls(
18
+ server: ViteDevServer,
19
+ options: Options,
20
+ source: string,
21
+ target: string,
22
+ base: string = '/',
23
+ _printUrls?: () => void,
24
+ ) {
25
+ _printUrls?.()
26
+
27
+ // fix `Error: EPERM: operation not permitted`
28
+ const viteCacheDir = `${pwd}/node_modules/.vite`
29
+ if (existsSync(viteCacheDir))
30
+ chmodRecursive(viteCacheDir, 0o777)
31
+ const nuxtCacheDir = `${pwd}/.nuxt`
32
+ if (existsSync(nuxtCacheDir))
33
+ chmodRecursive(nuxtCacheDir, 0o777)
34
+
35
+ try {
36
+ if (caddy)
37
+ return
38
+ caddy = new CaddyInstant()
39
+ caddy.run(source, target, {
40
+ base,
41
+ ...options,
42
+ }).then(() => {
43
+ const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
44
+ consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
45
+ }).catch((e) => {
46
+ throw e
47
+ })
48
+ }
49
+ catch (e) {
50
+ consola.error(e)
51
+ }
52
+ }
16
53
 
17
54
  export const unpluginFactory: UnpluginFactory<Options> = options => ({
18
55
  name: 'unplugin-https-reverse-proxy',
@@ -41,43 +78,18 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
41
78
  return
42
79
  }
43
80
 
44
- _servce = server
81
+ const _printUrls = server.printUrls.bind(server)
45
82
 
46
- const _printUrls = server.printUrls
47
- server.printUrls = () => {
48
- _printUrls()
83
+ let source = `localhost:${config.server.port || 5173}`
84
+ const url = server.resolvedUrls?.local[0]
85
+ if (url) {
86
+ const u = new URL(url)
87
+ source = u.host
88
+ }
49
89
 
50
- // fix `Error: EPERM: operation not permitted`
51
- const pwd = process.cwd()
52
- const vireCacheDir = `${pwd}/node_modules/.vite`
53
- if (existsSync(vireCacheDir))
54
- chmodSync(vireCacheDir, 0o777)
90
+ const base = server.config.base || '/'
55
91
 
56
- let source = `localhost:${config.server.port}`
57
- const url = server.resolvedUrls?.local[0]
58
- if (url) {
59
- const u = new URL(url)
60
- source = u.host
61
- }
62
- const base = server.config.base || '/'
63
- try {
64
- if (caddy)
65
- return
66
- caddy = new CaddyInstant()
67
- caddy.run(source, target, {
68
- base,
69
- ...options,
70
- }).then(() => {
71
- const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
72
- consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
73
- }).catch((e) => {
74
- throw e
75
- })
76
- }
77
- catch (e) {
78
- consola.error(e)
79
- }
80
- }
92
+ server.printUrls = () => vitePrintUrls(server, options, source, target, base, _printUrls)
81
93
  },
82
94
  },
83
95
  webpack(compiler) {
@@ -100,6 +112,10 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
100
112
  return
101
113
  }
102
114
 
115
+ const nuxtCacheDir = `${pwd}/node_modules/.cache`
116
+ if (existsSync(nuxtCacheDir))
117
+ chmodRecursive(nuxtCacheDir, 0o777)
118
+
103
119
  try {
104
120
  const devServer = {
105
121
  host: 'localhost',
package/src/nuxt.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  import { addVitePlugin, addWebpackPlugin, defineNuxtModule } from '@nuxt/kit'
2
+ import type { ViteDevServer } from 'vite'
2
3
  import vite from './vite'
3
4
  import webpack from './webpack'
4
5
  import type { Options } from './types'
5
6
  import '@nuxt/schema'
7
+ import { consola, once } from './utils'
8
+ import { vitePrintUrls } from '.'
6
9
 
7
10
  export interface ModuleOptions extends Options {
8
11
 
@@ -17,6 +20,23 @@ export default defineNuxtModule<ModuleOptions>({
17
20
  addVitePlugin(() => vite(options))
18
21
  addWebpackPlugin(() => webpack(options))
19
22
 
20
- // ...
23
+ // https://github.com/nuxt/cli/issues/220#issuecomment-1735394141
24
+ let url = ''
25
+ _nuxt.hook('listen', (_, listener) => {
26
+ url = listener.url
27
+ })
28
+
29
+ _nuxt.hook('vite:serverCreated', once((server: ViteDevServer) => {
30
+ const source = new URL(url).host
31
+
32
+ if (!options.target) {
33
+ consola.fail('please provide target')
34
+ return
35
+ }
36
+
37
+ const base = _nuxt.options.app.baseURL || '/'
38
+
39
+ vitePrintUrls(server, options, source, options.target, base)
40
+ }))
21
41
  },
22
42
  })
package/src/utils.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import process from 'node:process'
2
+ import path from 'node:path'
3
+ import { chmod, readdir, stat } from 'node:fs/promises'
2
4
  import { execSync } from 'node:child_process'
3
5
  import { createConsola } from 'consola'
4
6
 
@@ -6,6 +8,25 @@ export const consola = createConsola({
6
8
  level: 3,
7
9
  }).withTag('reverse-proxy')
8
10
 
11
+ export async function chmodRecursive(_path: string, mode: number) {
12
+ await chmod(_path, mode)
13
+ if ((await stat(_path)).isDirectory()) {
14
+ const children = await readdir(_path)
15
+ for (const child of children)
16
+ await chmodRecursive(path.join(_path, child), mode)
17
+ }
18
+ }
19
+
20
+ export function once<T extends (...args: any[]) => any>(fn: T): (...args: Parameters<T>) => ReturnType<T> {
21
+ let called = false
22
+ return (...args: any[]) => {
23
+ if (called)
24
+ return
25
+ called = true
26
+ return fn(...args)
27
+ }
28
+ }
29
+
9
30
  export function isAdmin() {
10
31
  if (process.platform === 'win32') {
11
32
  try {