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

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/README.md CHANGED
@@ -6,6 +6,15 @@ A plugin for https reverse proxy, support `vite` and `webpack`.
6
6
 
7
7
  ## Install
8
8
 
9
+
10
+ Support set caddy file path by environment variable `UHRP_CADDY_PATH`, default is system cache folder.
11
+
12
+ ```env
13
+ #.env.local
14
+ UHRP_CADDY_PATH=~/tmp/.uhrp
15
+ ```
16
+
17
+
9
18
  ```bash
10
19
  # not auto install caddy when postinstall
11
20
  npm i unplugin-https-reverse-proxy --save-dev
@@ -8,14 +8,13 @@ import {
8
8
 
9
9
  // src/index.ts
10
10
  import process4 from "process";
11
- import { existsSync as existsSync2 } from "fs";
12
11
  import { createUnplugin } from "unplugin";
13
12
  import c from "picocolors";
14
13
 
15
14
  // src/caddy/index.ts
16
15
  import { platform } from "os";
17
16
  import process3 from "process";
18
- import { chmodSync as chmodSync2, createWriteStream, existsSync, unlinkSync } from "fs";
17
+ import { chmodSync as chmodSync2, createWriteStream, existsSync, mkdirSync, unlinkSync } from "fs";
19
18
  import { spawn } from "child_process";
20
19
  import { got } from "got-cjs";
21
20
  import { HttpProxyAgent } from "http-proxy-agent";
@@ -71,8 +70,16 @@ async function removeHost(ip, host) {
71
70
 
72
71
  // src/caddy/constants.ts
73
72
  import { join } from "path";
73
+ import os from "os";
74
74
  import process2 from "process";
75
- var TEMP_DIR = process2.env.TEMP || process2.env.TMPDIR || "/tmp";
75
+ import dotenvFlow from "dotenv-flow";
76
+ dotenvFlow.config();
77
+ function resolvePath(path) {
78
+ if (path.startsWith("~"))
79
+ return join(os.homedir() || "", path.slice(1));
80
+ return path;
81
+ }
82
+ var TEMP_DIR = resolvePath(process2.env.UHRP_CADDY_PATH || process2.env.TEMP || process2.env.TMPDIR || "/tmp");
76
83
  var caddyPath = join(TEMP_DIR, `caddy${process2.platform === "win32" ? ".exe" : ""}`);
77
84
  var caddyFilePath = join(TEMP_DIR, "CADDYFILE");
78
85
  var supportList = [
@@ -246,6 +253,10 @@ async function download() {
246
253
  if (await testCaddy())
247
254
  return caddyPath;
248
255
  return new Promise((resolve, reject) => {
256
+ if (!existsSync(TEMP_DIR)) {
257
+ mkdirSync(TEMP_DIR, { recursive: true });
258
+ chmodSync2(TEMP_DIR, 511);
259
+ }
249
260
  const file = createWriteStream(caddyPath);
250
261
  let _platform = platform().toString();
251
262
  _platform === "win32" && (_platform = "windows");
@@ -260,6 +271,9 @@ async function download() {
260
271
  const httpsProxy = process3.env.HTTPS_PROXY || process3.env.https_proxy;
261
272
  const httpAgent = httpProxy ? new HttpProxyAgent(httpProxy) : void 0;
262
273
  const httpsAgent = httpsProxy ? new HttpsProxyAgent(httpsProxy) : void 0;
274
+ const chmodCaddyOnce = once(() => {
275
+ chmodSync2(caddyPath, 511);
276
+ });
263
277
  got.stream(dowmloadLink, {
264
278
  agent: {
265
279
  http: httpAgent,
@@ -267,11 +281,12 @@ async function download() {
267
281
  }
268
282
  }).on("downloadProgress", (progress) => {
269
283
  logProgress(progress.percent);
284
+ chmodCaddyOnce();
270
285
  }).pipe(file).on("finish", () => {
271
286
  logProgressOver();
272
287
  if (process3.platform === "win32")
273
288
  return resolve(caddyPath);
274
- chmodSync2(caddyPath, 493);
289
+ chmodSync2(caddyPath, 511);
275
290
  resolve(caddyPath);
276
291
  }).on("error", (err) => {
277
292
  reject(err);
@@ -282,7 +297,7 @@ function testCaddy() {
282
297
  return new Promise((resolve, reject) => {
283
298
  if (!existsSync(caddyPath))
284
299
  return resolve(false);
285
- chmodSync2(caddyPath, 493);
300
+ chmodSync2(caddyPath, 511);
286
301
  const child = process3.platform === "win32" ? spawn(caddyPath, []) : spawn("sudo", ["-E", caddyPath]);
287
302
  child.on("close", () => {
288
303
  return resolve(false);
@@ -357,6 +372,16 @@ var CaddyInstant = class {
357
372
  const port = Number(source.split(":")[1]);
358
373
  if (!Number.isNaN(port) && await tryPort(port))
359
374
  await kill(port, "tcp");
375
+ const pwd2 = process3.cwd();
376
+ const viteCacheDir = `${pwd2}/node_modules/.vite`;
377
+ if (existsSync(viteCacheDir))
378
+ chmodRecursive(viteCacheDir, 511);
379
+ const nuxtCacheDir = `${pwd2}/.nuxt`;
380
+ if (existsSync(nuxtCacheDir))
381
+ await chmodRecursive(nuxtCacheDir, 511);
382
+ const webpackCacheDir = `${pwd2}/node_modules/.cache`;
383
+ if (existsSync(webpackCacheDir))
384
+ chmodRecursive(webpackCacheDir, 511);
360
385
  if (!restore || this.stoped)
361
386
  return originalExit(code);
362
387
  try {
@@ -398,27 +423,17 @@ var caddy;
398
423
  var pwd = process4.cwd();
399
424
  var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
400
425
  _printUrls == null ? void 0 : _printUrls();
401
- const viteCacheDir = `${pwd}/node_modules/.vite`;
402
- if (existsSync2(viteCacheDir))
403
- chmodRecursive(viteCacheDir, 511);
404
- const nuxtCacheDir = `${pwd}/.nuxt`;
405
- if (existsSync2(nuxtCacheDir))
406
- chmodRecursive(nuxtCacheDir, 511);
407
- try {
408
- if (caddy)
409
- return;
410
- caddy = new CaddyInstant();
411
- caddy.run(source, target, __spreadValues({
412
- base
413
- }, options)).then(() => {
414
- const colorUrl = (url) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`));
415
- consola.success(` ${c.green("\u279C")} ${c.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
416
- }).catch((e) => {
417
- throw e;
418
- });
419
- } catch (e) {
420
- consola.error(e);
421
- }
426
+ if (caddy)
427
+ return;
428
+ caddy = new CaddyInstant();
429
+ caddy.run(source, target, __spreadValues({
430
+ base
431
+ }, options)).then(() => {
432
+ const colorUrl = (url) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`));
433
+ consola.success(` ${c.green("\u279C")} ${c.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
434
+ }).catch((e) => {
435
+ throw e;
436
+ });
422
437
  });
423
438
  var unpluginFactory = (options) => ({
424
439
  name: "unplugin-https-reverse-proxy",
@@ -429,12 +444,12 @@ var unpluginFactory = (options) => ({
429
444
  },
430
445
  configureServer(server) {
431
446
  var _a;
447
+ if (config.command !== "serve")
448
+ return;
432
449
  if (!isAdmin()) {
433
450
  consola.warn("please run as administrator");
434
451
  return;
435
452
  }
436
- if (config.command === "build")
437
- return;
438
453
  const {
439
454
  enable = true,
440
455
  target = ""
@@ -458,12 +473,12 @@ var unpluginFactory = (options) => ({
458
473
  },
459
474
  webpack(compiler) {
460
475
  var _a;
476
+ if (process4.env.NODE_ENV !== "development")
477
+ return;
461
478
  if (!isAdmin()) {
462
479
  consola.warn("please run as administrator");
463
480
  return;
464
481
  }
465
- if (process4.env.NODE_ENV !== "development")
466
- return;
467
482
  const {
468
483
  enable = true,
469
484
  target = ""
@@ -474,39 +489,32 @@ var unpluginFactory = (options) => ({
474
489
  consola.fail("please provide target");
475
490
  return;
476
491
  }
477
- const nuxtCacheDir = `${pwd}/node_modules/.cache`;
478
- if (existsSync2(nuxtCacheDir))
479
- chmodRecursive(nuxtCacheDir, 511);
480
- try {
481
- const devServer = __spreadValues(__spreadValues({
482
- host: "localhost",
483
- port: 8080
484
- }, compiler.options.devServer), process4.VUE_CLI_SERVICE.projectOptions.devServer);
485
- const source = `${devServer.host}:${devServer.port}`;
486
- if (caddy)
487
- return;
488
- const _close = (_a = compiler.close) == null ? void 0 : _a.bind(compiler);
489
- if (_close) {
490
- compiler.close = async (callback) => {
491
- process4.on("exit", () => {
492
- _close && _close(callback);
493
- });
494
- };
495
- } else {
496
- options.https = false;
497
- }
498
- caddy = new CaddyInstant();
499
- caddy.run(source, target, __spreadValues({}, options)).then(() => {
500
- const colorUrl = (url) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`));
501
- compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
502
- consola.success(` ${c.green("\u279C")} ${c.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
492
+ const devServer = __spreadValues(__spreadValues({
493
+ host: "localhost",
494
+ port: 8080
495
+ }, compiler.options.devServer), process4.VUE_CLI_SERVICE.projectOptions.devServer);
496
+ const source = `${devServer.host}:${devServer.port}`;
497
+ if (caddy)
498
+ return;
499
+ const _close = (_a = compiler.close) == null ? void 0 : _a.bind(compiler);
500
+ if (_close) {
501
+ compiler.close = async (callback) => {
502
+ process4.on("exit", () => {
503
+ _close && _close(callback);
503
504
  });
504
- }).catch((e) => {
505
- throw e;
506
- });
507
- } catch (e) {
508
- consola.error(e);
505
+ };
506
+ } else {
507
+ options.https = false;
509
508
  }
509
+ caddy = new CaddyInstant();
510
+ caddy.run(source, target, __spreadValues({}, options)).then(() => {
511
+ const colorUrl = (url) => c.green(url.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`));
512
+ compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
513
+ consola.success(` ${c.green("\u279C")} ${c.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
514
+ });
515
+ }).catch((e) => {
516
+ throw e;
517
+ });
510
518
  }
511
519
  });
512
520
  var unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
package/dist/esbuild.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
 
@@ -95,7 +94,7 @@ function isAdmin() {
95
94
  }
96
95
 
97
96
  // src/caddy/index.ts
98
- var import_node_os = require("os");
97
+ var import_node_os2 = require("os");
99
98
  var import_node_process4 = __toESM(require("process"), 1);
100
99
  var import_node_fs2 = require("fs");
101
100
  var import_node_child_process3 = require("child_process");
@@ -153,8 +152,16 @@ async function removeHost(ip, host) {
153
152
 
154
153
  // src/caddy/constants.ts
155
154
  var import_node_path2 = require("path");
155
+ var import_node_os = __toESM(require("os"), 1);
156
156
  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";
157
+ var import_dotenv_flow = __toESM(require("dotenv-flow"), 1);
158
+ import_dotenv_flow.default.config();
159
+ function resolvePath(path2) {
160
+ if (path2.startsWith("~"))
161
+ return (0, import_node_path2.join)(import_node_os.default.homedir() || "", path2.slice(1));
162
+ return path2;
163
+ }
164
+ 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");
158
165
  var caddyPath = (0, import_node_path2.join)(TEMP_DIR, `caddy${import_node_process3.default.platform === "win32" ? ".exe" : ""}`);
159
166
  var caddyFilePath = (0, import_node_path2.join)(TEMP_DIR, "CADDYFILE");
160
167
  var supportList = [
@@ -328,8 +335,12 @@ async function download() {
328
335
  if (await testCaddy())
329
336
  return caddyPath;
330
337
  return new Promise((resolve, reject) => {
338
+ if (!(0, import_node_fs2.existsSync)(TEMP_DIR)) {
339
+ (0, import_node_fs2.mkdirSync)(TEMP_DIR, { recursive: true });
340
+ (0, import_node_fs2.chmodSync)(TEMP_DIR, 511);
341
+ }
331
342
  const file = (0, import_node_fs2.createWriteStream)(caddyPath);
332
- let _platform = (0, import_node_os.platform)().toString();
343
+ let _platform = (0, import_node_os2.platform)().toString();
333
344
  _platform === "win32" && (_platform = "windows");
334
345
  let _arch = import_node_process4.default.arch.toString();
335
346
  _arch === "x64" && (_arch = "amd64");
@@ -342,6 +353,9 @@ async function download() {
342
353
  const httpsProxy = import_node_process4.default.env.HTTPS_PROXY || import_node_process4.default.env.https_proxy;
343
354
  const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
344
355
  const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
356
+ const chmodCaddyOnce = once(() => {
357
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
358
+ });
345
359
  import_got_cjs.got.stream(dowmloadLink, {
346
360
  agent: {
347
361
  http: httpAgent,
@@ -349,11 +363,12 @@ async function download() {
349
363
  }
350
364
  }).on("downloadProgress", (progress) => {
351
365
  logProgress(progress.percent);
366
+ chmodCaddyOnce();
352
367
  }).pipe(file).on("finish", () => {
353
368
  logProgressOver();
354
369
  if (import_node_process4.default.platform === "win32")
355
370
  return resolve(caddyPath);
356
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
371
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
357
372
  resolve(caddyPath);
358
373
  }).on("error", (err) => {
359
374
  reject(err);
@@ -364,7 +379,7 @@ function testCaddy() {
364
379
  return new Promise((resolve, reject) => {
365
380
  if (!(0, import_node_fs2.existsSync)(caddyPath))
366
381
  return resolve(false);
367
- (0, import_node_fs2.chmodSync)(caddyPath, 493);
382
+ (0, import_node_fs2.chmodSync)(caddyPath, 511);
368
383
  const child = import_node_process4.default.platform === "win32" ? (0, import_node_child_process3.spawn)(caddyPath, []) : (0, import_node_child_process3.spawn)("sudo", ["-E", caddyPath]);
369
384
  child.on("close", () => {
370
385
  return resolve(false);
@@ -439,6 +454,16 @@ var CaddyInstant = class {
439
454
  const port = Number(source.split(":")[1]);
440
455
  if (!Number.isNaN(port) && await tryPort(port))
441
456
  await (0, import_kill_port.default)(port, "tcp");
457
+ const pwd2 = import_node_process4.default.cwd();
458
+ const viteCacheDir = `${pwd2}/node_modules/.vite`;
459
+ if ((0, import_node_fs2.existsSync)(viteCacheDir))
460
+ chmodRecursive(viteCacheDir, 511);
461
+ const nuxtCacheDir = `${pwd2}/.nuxt`;
462
+ if ((0, import_node_fs2.existsSync)(nuxtCacheDir))
463
+ await chmodRecursive(nuxtCacheDir, 511);
464
+ const webpackCacheDir = `${pwd2}/node_modules/.cache`;
465
+ if ((0, import_node_fs2.existsSync)(webpackCacheDir))
466
+ chmodRecursive(webpackCacheDir, 511);
442
467
  if (!restore || this.stoped)
443
468
  return originalExit(code);
444
469
  try {
@@ -480,27 +505,17 @@ var caddy;
480
505
  var pwd = import_node_process5.default.cwd();
481
506
  var vitePrintUrls = once((options, source, target, base = "/", _printUrls) => {
482
507
  _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
- }
508
+ if (caddy)
509
+ return;
510
+ caddy = new CaddyInstant();
511
+ caddy.run(source, target, __spreadValues({
512
+ base
513
+ }, options)).then(() => {
514
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
515
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}${base}`)}`);
516
+ }).catch((e) => {
517
+ throw e;
518
+ });
504
519
  });
505
520
  var unpluginFactory = (options) => ({
506
521
  name: "unplugin-https-reverse-proxy",
@@ -511,12 +526,12 @@ var unpluginFactory = (options) => ({
511
526
  },
512
527
  configureServer(server) {
513
528
  var _a;
529
+ if (config.command !== "serve")
530
+ return;
514
531
  if (!isAdmin()) {
515
532
  consola.warn("please run as administrator");
516
533
  return;
517
534
  }
518
- if (config.command === "build")
519
- return;
520
535
  const {
521
536
  enable = true,
522
537
  target = ""
@@ -540,12 +555,12 @@ var unpluginFactory = (options) => ({
540
555
  },
541
556
  webpack(compiler) {
542
557
  var _a;
558
+ if (import_node_process5.default.env.NODE_ENV !== "development")
559
+ return;
543
560
  if (!isAdmin()) {
544
561
  consola.warn("please run as administrator");
545
562
  return;
546
563
  }
547
- if (import_node_process5.default.env.NODE_ENV !== "development")
548
- return;
549
564
  const {
550
565
  enable = true,
551
566
  target = ""
@@ -556,39 +571,32 @@ var unpluginFactory = (options) => ({
556
571
  consola.fail("please provide target");
557
572
  return;
558
573
  }
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}`)}`);
574
+ const devServer = __spreadValues(__spreadValues({
575
+ host: "localhost",
576
+ port: 8080
577
+ }, compiler.options.devServer), import_node_process5.default.VUE_CLI_SERVICE.projectOptions.devServer);
578
+ const source = `${devServer.host}:${devServer.port}`;
579
+ if (caddy)
580
+ return;
581
+ const _close = (_a = compiler.close) == null ? void 0 : _a.bind(compiler);
582
+ if (_close) {
583
+ compiler.close = async (callback) => {
584
+ import_node_process5.default.on("exit", () => {
585
+ _close && _close(callback);
585
586
  });
586
- }).catch((e) => {
587
- throw e;
588
- });
589
- } catch (e) {
590
- consola.error(e);
587
+ };
588
+ } else {
589
+ options.https = false;
591
590
  }
591
+ caddy = new CaddyInstant();
592
+ caddy.run(source, target, __spreadValues({}, options)).then(() => {
593
+ const colorUrl = (url) => import_picocolors.default.green(url.replace(/:(\d+)\//, (_, port) => `:${import_picocolors.default.bold(port)}/`));
594
+ compiler.hooks.done.tap("unplugin-https-reverse-proxy", () => {
595
+ consola.success(` ${import_picocolors.default.green("\u279C")} ${import_picocolors.default.bold("run caddy reverse proxy success")}: ${colorUrl(`${options.https ? "https" : "http"}://${target}`)}`);
596
+ });
597
+ }).catch((e) => {
598
+ throw e;
599
+ });
592
600
  }
593
601
  });
594
602
 
package/dist/esbuild.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  unpluginFactory
3
- } from "./chunk-GAKOA5LE.js";
3
+ } from "./chunk-IUFVJYJ5.js";
4
4
  import "./chunk-RUJE3YFB.js";
5
5
 
6
6
  // src/esbuild.ts