unplugin-https-reverse-proxy 1.0.0-beta.3 → 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);
@@ -436,6 +443,19 @@ var CaddyInstant = class {
436
443
  });
437
444
  const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
438
445
  import_node_process4.default.exit = async (code) => {
446
+ const port = Number(source.split(":")[1]);
447
+ if (!Number.isNaN(port) && await tryPort(port))
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);
439
459
  if (!restore || this.stoped)
440
460
  return originalExit(code);
441
461
  try {
@@ -477,27 +497,17 @@ var caddy;
477
497
  var pwd = import_node_process5.default.cwd();
478
498
  var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
479
499
  _printUrls == null ? void 0 : _printUrls();
480
- const viteCacheDir = `${pwd}/node_modules/.vite`;
481
- if ((0, import_node_fs3.existsSync)(viteCacheDir))
482
- chmodRecursive(viteCacheDir, 511);
483
- const nuxtCacheDir = `${pwd}/.nuxt`;
484
- if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
485
- chmodRecursive(nuxtCacheDir, 511);
486
- try {
487
- if (caddy)
488
- return;
489
- caddy = new CaddyInstant();
490
- caddy.run(source, target, __spreadValues({
491
- base
492
- }, options)).then(() => {
493
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
494
- consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
495
- }).catch((e) => {
496
- throw e;
497
- });
498
- } catch (e) {
499
- consola.error(e);
500
- }
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
+ });
501
511
  });
502
512
  var unpluginFactory = (options) => ({
503
513
  name: "unplugin-https-reverse-proxy",
@@ -508,12 +518,12 @@ var unpluginFactory = (options) => ({
508
518
  },
509
519
  configureServer(server) {
510
520
  var _a;
521
+ if (config.command !== "serve")
522
+ return;
511
523
  if (!isAdmin()) {
512
524
  consola.warn("please run as administrator");
513
525
  return;
514
526
  }
515
- if (config.command === "build")
516
- return;
517
527
  const {
518
528
  enable = true,
519
529
  target = ""
@@ -537,12 +547,12 @@ var unpluginFactory = (options) => ({
537
547
  },
538
548
  webpack(compiler) {
539
549
  var _a;
550
+ if (import_node_process5.default.env.NODE_ENV !== "development")
551
+ return;
540
552
  if (!isAdmin()) {
541
553
  consola.warn("please run as administrator");
542
554
  return;
543
555
  }
544
- if (import_node_process5.default.env.NODE_ENV !== "development")
545
- return;
546
556
  const {
547
557
  enable = true,
548
558
  target = ""
@@ -553,39 +563,32 @@ var unpluginFactory = (options) => ({
553
563
  consola.fail("please provide target");
554
564
  return;
555
565
  }
556
- const nuxtCacheDir = `${pwd}/node_modules/.cache`;
557
- if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
558
- chmodRecursive(nuxtCacheDir, 511);
559
- try {
560
- const devServer = __spreadValues(__spreadValues({
561
- host: "localhost",
562
- port: 8080
563
- }, compiler.options.devServer), import_node_process5.default.VUE_CLI_SERVICE.projectOptions.devServer);
564
- const source = `${devServer.host}:${devServer.port}`;
565
- if (caddy)
566
- return;
567
- const _close = (_a = compiler.close) == null ? void 0 : _a.bind(compiler);
568
- if (_close) {
569
- compiler.close = async (callback) => {
570
- import_node_process5.default.on("exit", () => {
571
- _close && _close(callback);
572
- });
573
- };
574
- } else {
575
- options.https = false;
576
- }
577
- caddy = new CaddyInstant();
578
- caddy.run(source, target, __spreadValues({}, options)).then(() => {
579
- const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
580
- compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
581
- 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);
582
578
  });
583
- }).catch((e) => {
584
- throw e;
585
- });
586
- } catch (e) {
587
- consola.error(e);
579
+ };
580
+ } else {
581
+ options.https = false;
588
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
+ });
589
592
  }
590
593
  });
591
594
 
package/dist/webpack.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-ZAZWZOPZ.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.3",
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)
@@ -166,6 +177,22 @@ export class CaddyInstant {
166
177
 
167
178
  // @ts-expect-error override
168
179
  process.exit = async (code?: number) => {
180
+ const port = Number(source.split(':')[1])
181
+ if (!Number.isNaN(port) && await tryPort(port))
182
+ await kill(port, 'tcp')
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
+
169
196
  if (!restore || this.stoped)
170
197
  return originalExit(code)
171
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 = '',