unplugin-https-reverse-proxy 1.0.0-beta.4 → 1.0.0-beta.6

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
@@ -51,7 +51,6 @@ 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
 
@@ -154,7 +153,7 @@ async function removeHost(ip, host) {
154
153
  // src/caddy/constants.ts
155
154
  var import_node_path2 = require("path");
156
155
  var import_node_process3 = __toESM(require("process"), 1);
157
- var TEMP_DIR = import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp";
156
+ var TEMP_DIR = import_node_process3.default.env.UHRP_CADDY_PATH || import_node_process3.default.env.TEMP || import_node_process3.default.env.TMPDIR || "/tmp";
158
157
  var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
159
158
  var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
160
159
  var supportList = [
@@ -328,6 +327,10 @@ async function download() {
328
327
  if (await testCaddy())
329
328
  return caddyPath;
330
329
  return new Promise((resolve, reject) => {
330
+ if (!(0, import_node_fs2.existsSync)(TEMP_DIR)) {
331
+ (0, import_node_fs2.mkdirSync)(TEMP_DIR, { recursive: true });
332
+ (0, import_node_fs2.chmodSync)(TEMP_DIR, 511);
333
+ }
331
334
  const file = (0, import_node_fs2.createWriteStream)(caddyPath);
332
335
  let _platform = (0, import_node_os.platform)().toString();
333
336
  _platform === "win32" && (_platform = "windows");
@@ -342,6 +345,9 @@ async function download() {
342
345
  const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
343
346
  const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
344
347
  const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
348
+ const chmodCaddyOnce = once(() => {
349
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
350
+ });
345
351
  import_got_cjs.got.stream(dowmloadLink, {
346
352
  agent: {
347
353
  http: httpAgent,
@@ -349,11 +355,12 @@ async function download() {
349
355
  }
350
356
  }).on("downloadProgress", (progress) => {
351
357
  logProgress(progress.percent);
358
+ chmodCaddyOnce();
352
359
  }).pipe(file).on("finish", () => {
353
360
  logProgressOver();
354
361
  if (import_node_process4.default.platform === "win32")
355
362
  return resolve(caddyPath);
356
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
363
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
357
364
  resolve(caddyPath);
358
365
  }).on("error", (err) => {
359
366
  reject(err);
@@ -439,6 +446,16 @@ var CaddyInstant = class {
439
446
  const port = Number(source.split(":")[1]);
440
447
  if (!Number.isNaN(port) && await tryPort(port))
441
448
  await (0, import_kill_port.default)(port, "tcp");
449
+ const pwd2 = import_node_process4.default.cwd();
450
+ const viteCacheDir = `${pwd2}/node_modules/.vite`;
451
+ if ((0, import_node_fs2.existsSync)(viteCacheDir))
452
+ chmodRecursive(viteCacheDir, 511);
453
+ const nuxtCacheDir = `${pwd2}/.nuxt`;
454
+ if ((0, import_node_fs2.existsSync)(nuxtCacheDir))
455
+ await chmodRecursive(nuxtCacheDir, 511);
456
+ const webpackCacheDir = `${pwd2}/node_modules/.cache`;
457
+ if ((0, import_node_fs2.existsSync)(webpackCacheDir))
458
+ chmodRecursive(webpackCacheDir, 511);
442
459
  if (!restore || this.stoped)
443
460
  return originalExit(code);
444
461
  try {
@@ -480,27 +497,17 @@ var caddy;
480
497
  var pwd = import_node_process5.default.cwd();
481
498
  var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
482
499
  _printUrls == null ? void 0 : _printUrls();
483
- const viteCacheDir = `${pwd}/node_modules/.vite`;
484
- if ((0, import_node_fs3.existsSync)(viteCacheDir))
485
- chmodRecursive(viteCacheDir, 511);
486
- const nuxtCacheDir = `${pwd}/.nuxt`;
487
- if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
488
- chmodRecursive(nuxtCacheDir, 511);
489
- try {
490
- if (caddy)
491
- return;
492
- caddy = new CaddyInstant();
493
- caddy.run(source, target, __spreadValues({
494
- base
495
- }, options)).then(() => {
496
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
497
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
498
- }).catch((e) => {
499
- throw e;
500
- });
501
- } catch (e) {
502
- consola.error(e);
503
- }
500
+ if (caddy)
501
+ return;
502
+ caddy = new CaddyInstant();
503
+ caddy.run(source, target, __spreadValues({
504
+ base
505
+ }, options)).then(() => {
506
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
507
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
508
+ }).catch((e) => {
509
+ throw e;
510
+ });
504
511
  });
505
512
  var unpluginFactory = (options) => ({
506
513
  name: "unplugin-https-reverse-proxy",
@@ -511,12 +518,12 @@ var unpluginFactory = (options) => ({
511
518
  },
512
519
  configureServer(server) {
513
520
  var _a;
521
+ if (config.command !== "serve")
522
+ return;
514
523
  if (!isAdmin()) {
515
524
  consola.warn("please run as administrator");
516
525
  return;
517
526
  }
518
- if (config.command === "build")
519
- return;
520
527
  const {
521
528
  enable = true,
522
529
  target = ""
@@ -540,12 +547,12 @@ var unpluginFactory = (options) => ({
540
547
  },
541
548
  webpack(compiler) {
542
549
  var _a;
550
+ if (import_node_process5.default.env.NODE_ENV !== "development")
551
+ return;
543
552
  if (!isAdmin()) {
544
553
  consola.warn("please run as administrator");
545
554
  return;
546
555
  }
547
- if (import_node_process5.default.env.NODE_ENV !== "development")
548
- return;
549
556
  const {
550
557
  enable = true,
551
558
  target = ""
@@ -556,39 +563,32 @@ var unpluginFactory = (options) => ({
556
563
  consola.fail("please provide target");
557
564
  return;
558
565
  }
559
- const nuxtCacheDir = `${pwd}/node_modules/.cache`;
560
- if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
561
- chmodRecursive(nuxtCacheDir, 511);
562
- try {
563
- const devServer = __spreadValues(__spreadValues({
564
- host: "localhost",
565
- port: 8080
566
- }, compiler.options.devServer), import_node_process5.default.VUE_CLI_SERVICE.projectOptions.devServer);
567
- const source = `${devServer.host}:${devServer.port}`;
568
- if (caddy)
569
- return;
570
- const _close = (_a = compiler.close) == null ? void 0 : _a.bind(compiler);
571
- if (_close) {
572
- compiler.close = async (callback) => {
573
- import_node_process5.default.on("exit", () => {
574
- _close && _close(callback);
575
- });
576
- };
577
- } else {
578
- options.https = false;
579
- }
580
- caddy = new CaddyInstant();
581
- caddy.run(source, target, __spreadValues({}, options)).then(() => {
582
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
583
- compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
584
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
566
+ const devServer = __spreadValues(__spreadValues({
567
+ host: "localhost",
568
+ port: 8080
569
+ }, compiler.options.devServer), import_node_process5.default.VUE_CLI_SERVICE.projectOptions.devServer);
570
+ const source = `${devServer.host}:${devServer.port}`;
571
+ if (caddy)
572
+ return;
573
+ const _close = (_a = compiler.close) == null ? void 0 : _a.bind(compiler);
574
+ if (_close) {
575
+ compiler.close = async (callback) => {
576
+ import_node_process5.default.on("exit", () => {
577
+ _close && _close(callback);
585
578
  });
586
- }).catch((e) => {
587
- throw e;
588
- });
589
- } catch (e) {
590
- consola.error(e);
579
+ };
580
+ } else {
581
+ options.https = false;
591
582
  }
583
+ caddy = new CaddyInstant();
584
+ caddy.run(source, target, __spreadValues({}, options)).then(() => {
585
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
586
+ compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
587
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
588
+ });
589
+ }).catch((e) => {
590
+ throw e;
591
+ });
592
592
  }
593
593
  });
594
594
 
package/dist/webpack.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-GAKOA5LE.js";
3
+ } from "./chunk-NBFRMP2Y.js";
4
4
  import "./chunk-RUJE3YFB.js";
5
5
 
6
6
  // src/webpack.ts
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.4",
4
+ "version": "1.0.0-beta.6",
5
5
  "packageManager": "pnpm@8.6.12",
6
6
  "description": "",
7
7
  "license": "MIT",
@@ -1,7 +1,7 @@
1
1
  import { join } from 'node:path'
2
2
  import process from 'node:process'
3
3
 
4
- const TEMP_DIR = process.env.TEMP || process.env.TMPDIR || '/tmp'
4
+ export const TEMP_DIR = process.env.UHRP_CADDY_PATH || process.env.TEMP || process.env.TMPDIR || '/tmp'
5
5
  export const caddyPath = join(TEMP_DIR, `caddy${process.platform === 'win32' ? '.exe' : ''}`)
6
6
  export const caddyFilePath = join(TEMP_DIR, 'CADDYFILE')
7
7
 
@@ -1,14 +1,14 @@
1
1
  import { platform } from 'node:os'
2
2
  import process from 'node:process'
3
- import { chmodSync, createWriteStream, existsSync, unlinkSync } from 'node:fs'
3
+ import { chmodSync, createWriteStream, existsSync, mkdirSync, unlinkSync } from 'node:fs'
4
4
  import { spawn } from 'node:child_process'
5
5
  import { got } from 'got-cjs'
6
6
  import { HttpProxyAgent } from 'http-proxy-agent'
7
7
  import { HttpsProxyAgent } from 'https-proxy-agent'
8
8
  import kill from 'kill-port'
9
- import { consola } from '../utils'
9
+ import { chmodRecursive, consola, once } from '../utils'
10
10
  import { addHost, removeHost } from '../host'
11
- import { caddyPath, supportList } from './constants'
11
+ import { TEMP_DIR, caddyPath, supportList } from './constants'
12
12
  import { logProgress, logProgressOver, tryPort } from './utils'
13
13
 
14
14
  export async function download() {
@@ -18,6 +18,11 @@ export async function download() {
18
18
  return new Promise<string>((resolve, reject) => {
19
19
  // https://caddyserver.com/api/download?os=darwin&arch=amd64
20
20
 
21
+ if (!existsSync(TEMP_DIR)) {
22
+ mkdirSync(TEMP_DIR, { recursive: true })
23
+ chmodSync(TEMP_DIR, 0o777)
24
+ }
25
+
21
26
  const file = createWriteStream(caddyPath)
22
27
 
23
28
  let _platform = platform().toString()
@@ -42,6 +47,11 @@ export async function download() {
42
47
  const httpAgent = httpProxy ? new HttpProxyAgent(httpProxy) : undefined
43
48
  const httpsAgent = httpsProxy ? new HttpsProxyAgent(httpsProxy) : undefined
44
49
 
50
+ const chmodCaddyOnce = once(() => {
51
+ // chmod +x
52
+ chmodSync(caddyPath, 0o777)
53
+ })
54
+
45
55
  got.stream(dowmloadLink, {
46
56
  agent: {
47
57
  http: httpAgent,
@@ -49,12 +59,13 @@ export async function download() {
49
59
  },
50
60
  }).on('downloadProgress', (progress) => {
51
61
  logProgress(progress.percent)
62
+ chmodCaddyOnce()
52
63
  }).pipe(file).on('finish', () => {
53
64
  logProgressOver()
54
65
  if (process.platform === 'win32')
55
66
  return resolve(caddyPath)
56
67
  // chmod +x
57
- chmodSync(caddyPath, 0o755)
68
+ chmodSync(caddyPath, 0o777)
58
69
  resolve(caddyPath)
59
70
  }).on('error', (err) => {
60
71
  // consola.error(err)
@@ -170,6 +181,18 @@ export class CaddyInstant {
170
181
  if (!Number.isNaN(port) && await tryPort(port))
171
182
  await kill(port, 'tcp')
172
183
 
184
+ // fix `Error: EPERM: operation not permitted`
185
+ const pwd = process.cwd()
186
+ const viteCacheDir = `${pwd}/node_modules/.vite`
187
+ if (existsSync(viteCacheDir))
188
+ chmodRecursive(viteCacheDir, 0o777)
189
+ const nuxtCacheDir = `${pwd}/.nuxt`
190
+ if (existsSync(nuxtCacheDir))
191
+ await chmodRecursive(nuxtCacheDir, 0o777)
192
+ const webpackCacheDir = `${pwd}/node_modules/.cache`
193
+ if (existsSync(webpackCacheDir))
194
+ chmodRecursive(webpackCacheDir, 0o777)
195
+
173
196
  if (!restore || this.stoped)
174
197
  return originalExit(code)
175
198
 
package/src/index.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  import process from 'node:process'
2
- import { existsSync } from 'node:fs'
3
2
  import type { UnpluginFactory } from 'unplugin'
4
3
  import { createUnplugin } from 'unplugin'
5
4
  import type { ResolvedConfig } from 'vite'
6
5
  import c from 'picocolors'
7
- import { chmodRecursive, consola, isAdmin, once } from './utils'
6
+ import { consola, isAdmin, once } from './utils'
8
7
  import type { Options } from './types'
9
8
  import { CaddyInstant } from './caddy'
10
9
 
@@ -23,31 +22,18 @@ export const vitePrintUrls = once((
23
22
  ) => {
24
23
  _printUrls?.()
25
24
 
26
- // fix `Error: EPERM: operation not permitted`
27
- const viteCacheDir = `${pwd}/node_modules/.vite`
28
- if (existsSync(viteCacheDir))
29
- chmodRecursive(viteCacheDir, 0o777)
30
- const nuxtCacheDir = `${pwd}/.nuxt`
31
- if (existsSync(nuxtCacheDir))
32
- chmodRecursive(nuxtCacheDir, 0o777)
33
-
34
- try {
35
- if (caddy)
36
- return
37
- caddy = new CaddyInstant()
38
- caddy.run(source, target, {
39
- base,
40
- ...options,
41
- }).then(() => {
42
- const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
43
- consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
44
- }).catch((e) => {
45
- throw e
46
- })
47
- }
48
- catch (e) {
49
- consola.error(e)
50
- }
25
+ if (caddy)
26
+ return
27
+ caddy = new CaddyInstant()
28
+ caddy.run(source, target, {
29
+ base,
30
+ ...options,
31
+ }).then(() => {
32
+ const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
33
+ consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}${base}`)}`)
34
+ }).catch((e) => {
35
+ throw e
36
+ })
51
37
  })
52
38
 
53
39
  export const unpluginFactory: UnpluginFactory<Options> = options => ({
@@ -58,14 +44,14 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
58
44
  config = _config
59
45
  },
60
46
  configureServer(server) {
47
+ if (config.command !== 'serve')
48
+ return
49
+
61
50
  if (!isAdmin()) {
62
51
  consola.warn('please run as administrator')
63
52
  return
64
53
  }
65
54
 
66
- if (config.command === 'build')
67
- return
68
-
69
55
  const {
70
56
  enable = true,
71
57
  target = '',
@@ -92,14 +78,14 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
92
78
  },
93
79
  },
94
80
  webpack(compiler) {
81
+ if (process.env.NODE_ENV !== 'development')
82
+ return
83
+
95
84
  if (!isAdmin()) {
96
85
  consola.warn('please run as administrator')
97
86
  return
98
87
  }
99
88
 
100
- if (process.env.NODE_ENV !== 'development')
101
- return
102
-
103
89
  const {
104
90
  enable = true,
105
91
  target = '',
@@ -111,54 +97,45 @@ export const unpluginFactory: UnpluginFactory<Options> = options => ({
111
97
  return
112
98
  }
113
99
 
114
- const nuxtCacheDir = `${pwd}/node_modules/.cache`
115
- if (existsSync(nuxtCacheDir))
116
- chmodRecursive(nuxtCacheDir, 0o777)
117
-
118
- try {
119
- const devServer = {
120
- host: 'localhost',
121
- port: 8080,
122
- ...compiler.options.devServer,
123
- // @ts-expect-error vuecli
124
- ...process.VUE_CLI_SERVICE.projectOptions.devServer,
125
- }
126
- const source = `${devServer.host}:${devServer.port}`
100
+ const devServer = {
101
+ host: 'localhost',
102
+ port: 8080,
103
+ ...compiler.options.devServer,
104
+ // @ts-expect-error vuecli
105
+ ...process.VUE_CLI_SERVICE.projectOptions.devServer,
106
+ }
107
+ const source = `${devServer.host}:${devServer.port}`
127
108
 
128
- if (caddy)
129
- return
109
+ if (caddy)
110
+ return
130
111
 
131
- // https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L1913
132
- const _close = compiler.close?.bind(compiler)
133
- if (_close) {
134
- compiler.close = async (callback: Parameters<typeof _close>[0]) => {
135
- process.on('exit', () => {
136
- _close && _close(callback)
137
- })
138
- }
139
- }
140
- else {
141
- // webpack 4 not support yet
142
- options.https = false
112
+ // https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L1913
113
+ const _close = compiler.close?.bind(compiler)
114
+ if (_close) {
115
+ compiler.close = async (callback: Parameters<typeof _close>[0]) => {
116
+ process.on('exit', () => {
117
+ _close && _close(callback)
118
+ })
143
119
  }
120
+ }
121
+ else {
122
+ // webpack 4 not support yet
123
+ options.https = false
124
+ }
144
125
 
145
- caddy = new CaddyInstant()
126
+ caddy = new CaddyInstant()
146
127
 
147
- caddy.run(source, target, {
148
- ...options,
149
- }).then(() => {
150
- const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
128
+ caddy.run(source, target, {
129
+ ...options,
130
+ }).then(() => {
131
+ const colorUrl = (url: string) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`))
151
132
 
152
- compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
153
- consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
154
- })
155
- }).catch((e) => {
156
- throw e
133
+ compiler.hooks.done.tap('unplugin-https-reverse-proxy', () => {
134
+ consola.success(` ${c.green('➜')} ${c.bold('run caddy reverse proxy success')}: ${colorUrl(`${options.https ? 'https' : 'http'}://${target}`)}`)
157
135
  })
158
- }
159
- catch (e) {
160
- consola.error(e)
161
- }
136
+ }).catch((e) => {
137
+ throw e
138
+ })
162
139
  },
163
140
  })
164
141
 
package/src/nuxt.ts CHANGED
@@ -15,14 +15,14 @@ export default defineNuxtModule<ModuleOptions>({
15
15
  configKey: 'unpluginHttpsReverseProxy',
16
16
  },
17
17
  setup(options, _nuxt) {
18
+ if (process.env.NODE_ENV !== 'development')
19
+ return
20
+
18
21
  if (!isAdmin()) {
19
22
  consola.warn('please run as administrator')
20
23
  return
21
24
  }
22
25
 
23
- if (process.env.NODE_ENV !== 'development')
24
- return
25
-
26
26
  const {
27
27
  enable = true,
28
28
  target = '',