unplugin-https-reverse-proxy 4.1.0-beta.12 → 4.1.0-beta.13

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/rspack.cjs CHANGED
@@ -42,10 +42,10 @@ var import_unplugin = require("unplugin");
42
42
 
43
43
  // src/caddy/index.ts
44
44
  var import_node_child_process3 = require("child_process");
45
- var import_node_fs4 = require("fs");
45
+ var import_node_fs5 = require("fs");
46
46
  var import_promises2 = require("fs/promises");
47
47
  var import_node_os5 = require("os");
48
- var import_node_path5 = require("path");
48
+ var import_node_path6 = require("path");
49
49
  var import_node_process8 = __toESM(require("process"), 1);
50
50
  var import_got_cjs = require("got-cjs");
51
51
  var import_http_proxy_agent = require("http-proxy-agent");
@@ -13294,7 +13294,7 @@ function isCaddyReloadCancellation(line) {
13294
13294
  // src/caddy/privileged.ts
13295
13295
  var import_node_buffer2 = require("buffer");
13296
13296
  var import_node_events = require("events");
13297
- var import_node_path4 = require("path");
13297
+ var import_node_path5 = require("path");
13298
13298
  var import_node_process7 = __toESM(require("process"), 1);
13299
13299
  var import_node_url = require("url");
13300
13300
 
@@ -13330,16 +13330,18 @@ function findUp(from, relative, exists2) {
13330
13330
 
13331
13331
  // src/privileged/session.ts
13332
13332
  var import_node_buffer = require("buffer");
13333
- var import_node_fs3 = require("fs");
13333
+ var import_node_fs4 = require("fs");
13334
13334
  var import_promises = require("fs/promises");
13335
13335
  var import_node_os4 = require("os");
13336
- var import_node_path3 = require("path");
13336
+ var import_node_path4 = require("path");
13337
13337
  var lockfile2 = __toESM(require("proper-lockfile"), 1);
13338
13338
 
13339
13339
  // src/privileged/launch.ts
13340
13340
  var import_node_child_process2 = require("child_process");
13341
13341
  var import_node_crypto = require("crypto");
13342
+ var import_node_fs3 = require("fs");
13342
13343
  var import_node_os3 = require("os");
13344
+ var import_node_path3 = require("path");
13343
13345
  var import_node_process6 = __toESM(require("process"), 1);
13344
13346
 
13345
13347
  // src/privileged/client.ts
@@ -13453,7 +13455,7 @@ async function connectClient(options) {
13453
13455
  } catch (error) {
13454
13456
  lastError = error;
13455
13457
  const code = lastError.code;
13456
- if (code === "EACCES" || lastError instanceof HelperConnectionError)
13458
+ if (code === "EACCES" || code === "EPERM" || lastError instanceof HelperConnectionError)
13457
13459
  throw lastError;
13458
13460
  await new Promise((resolve2) => setTimeout(resolve2, retryIntervalMs));
13459
13461
  }
@@ -13585,39 +13587,78 @@ function createClient(socket, initialBuffer, callTimeoutMs) {
13585
13587
  // src/privileged/windows-elevation.ts
13586
13588
  var import_node_child_process = require("child_process");
13587
13589
  var import_node_process5 = __toESM(require("process"), 1);
13590
+ var ERROR_CANCELLED = 1223;
13588
13591
  function quotePowerShell(value2) {
13589
13592
  return `'${value2.replace(/'/g, "''")}'`;
13590
13593
  }
13594
+ function quoteWindowsArg(arg) {
13595
+ if (arg === "")
13596
+ return '""';
13597
+ if (!/[\s"]/.test(arg))
13598
+ return arg;
13599
+ let quoted = '"';
13600
+ let backslashes = 0;
13601
+ for (const char of arg) {
13602
+ if (char === "\\") {
13603
+ backslashes++;
13604
+ continue;
13605
+ }
13606
+ quoted += char === '"' ? `${"\\".repeat(backslashes * 2 + 1)}"` : `${"\\".repeat(backslashes)}${char}`;
13607
+ backslashes = 0;
13608
+ }
13609
+ return `${quoted}${"\\".repeat(backslashes * 2)}"`;
13610
+ }
13591
13611
  function buildElevationCommand(options) {
13592
- const argumentList = options.args.map(quotePowerShell).join(",");
13593
- const parts = [
13594
- "Start-Process",
13595
- "-FilePath",
13596
- quotePowerShell(options.file)
13612
+ const lines = [
13613
+ // The message read back on failure is localized; keep it readable as UTF-8.
13614
+ "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8",
13615
+ "$psi = New-Object System.Diagnostics.ProcessStartInfo",
13616
+ `$psi.FileName = ${quotePowerShell(options.file)}`,
13617
+ `$psi.Arguments = ${quotePowerShell(options.args.map(quoteWindowsArg).join(" "))}`
13597
13618
  ];
13598
- if (argumentList)
13599
- parts.push("-ArgumentList", argumentList);
13600
13619
  if (options.cwd)
13601
- parts.push("-WorkingDirectory", quotePowerShell(options.cwd));
13602
- parts.push("-Verb", "RunAs");
13603
- parts.push("-WindowStyle", "Hidden");
13604
- return parts.join(" ");
13620
+ lines.push(`$psi.WorkingDirectory = ${quotePowerShell(options.cwd)}`);
13621
+ lines.push(
13622
+ "$psi.Verb = 'runas'",
13623
+ "$psi.UseShellExecute = $true",
13624
+ "$psi.WindowStyle = 'Hidden'",
13625
+ "try { $p = [System.Diagnostics.Process]::Start($psi); if ($p) { [Console]::Out.Write($p.Id) } }",
13626
+ `catch { $e = $_.Exception; while ($e.InnerException) { $e = $e.InnerException }; if ($e.NativeErrorCode -eq ${ERROR_CANCELLED}) { exit ${ERROR_CANCELLED} }; [Console]::Error.Write($e.Message); exit 1 }`
13627
+ );
13628
+ return lines.join("\n");
13605
13629
  }
13606
13630
  function spawnElevatedOnWindows(options) {
13607
- return new Promise((resolve2, reject2) => {
13608
- const script = buildElevationCommand(options);
13631
+ return new Promise((resolve2) => {
13609
13632
  const child = (0, import_node_child_process.spawn)(
13610
13633
  "powershell.exe",
13611
- ["-NoProfile", "-NonInteractive", "-Command", script],
13612
- { stdio: "ignore", windowsHide: true }
13634
+ ["-NoProfile", "-NonInteractive", "-Command", buildElevationCommand(options)],
13635
+ { stdio: ["ignore", "pipe", "pipe"], windowsHide: true }
13613
13636
  );
13614
- child.once("error", reject2);
13615
- child.once("exit", (code) => {
13616
- if (code === 0)
13617
- return resolve2(child);
13618
- reject2(new Error(
13619
- `administrator permission was not granted (PowerShell exited with ${code}). Accept the UAC prompt to let the plugin edit the hosts file and bind ports 80/443.`
13620
- ));
13637
+ let stdout = "";
13638
+ let stderr = "";
13639
+ child.stdout.on("data", (data) => {
13640
+ stdout += data.toString();
13641
+ });
13642
+ child.stderr.on("data", (data) => {
13643
+ stderr = (stderr + data.toString()).slice(-4096);
13644
+ });
13645
+ child.once("error", (error) => resolve2({
13646
+ ok: false,
13647
+ denied: false,
13648
+ message: `could not start PowerShell to request administrator permission: ${error.message}`
13649
+ }));
13650
+ child.once("close", (code) => {
13651
+ if (code === 0) {
13652
+ const pid = Number(stdout.trim());
13653
+ return resolve2({ ok: true, pid: Number.isSafeInteger(pid) && pid > 0 ? pid : void 0 });
13654
+ }
13655
+ if (code === ERROR_CANCELLED)
13656
+ return resolve2({ ok: false, denied: true, message: "administrator permission was cancelled" });
13657
+ resolve2({
13658
+ ok: false,
13659
+ denied: false,
13660
+ message: `could not start the privileged helper as administrator (PowerShell exited with ${code}).${stderr.trim() ? ` ${stderr.trim()}` : ""}`
13661
+ });
13621
13662
  });
13622
13663
  });
13623
13664
  }
@@ -13748,8 +13789,18 @@ async function startPrivilegedHelper(options) {
13748
13789
  ...options.caddyAdminSocket ? ["--caddy-admin", options.caddyAdminSocket] : []
13749
13790
  ];
13750
13791
  let child;
13792
+ let helperExited;
13751
13793
  if (!direct && (0, import_node_os3.platform)() === "win32") {
13752
- await spawnElevatedOnWindows({ file: helperEntry, args: childArgv, cwd: import_node_process6.default.cwd() });
13794
+ const errorFile = (0, import_node_path3.join)((0, import_node_os3.tmpdir)(), `uhrp-helper-${(0, import_node_crypto.randomBytes)(6).toString("hex")}.log`);
13795
+ const elevated = await spawnElevatedOnWindows({
13796
+ file: helperEntry,
13797
+ args: [...childArgv, "--error-file", errorFile],
13798
+ cwd: import_node_process6.default.cwd()
13799
+ });
13800
+ if (!elevated.ok)
13801
+ throw elevated.denied ? new PrivilegeDeniedError(elevated.message) : new Error(elevated.message);
13802
+ if (elevated.pid !== void 0)
13803
+ helperExited = watchWindowsHelper(elevated.pid, errorFile);
13753
13804
  } else {
13754
13805
  const command = direct ? helperEntry : sudoCommand;
13755
13806
  const spawnArgs = direct ? childArgv : ["-n", "env", ...forwardedArgs, helperEntry, ...childArgv];
@@ -13783,7 +13834,10 @@ async function startPrivilegedHelper(options) {
13783
13834
  }
13784
13835
  }
13785
13836
  try {
13786
- const client = await connectClient({ socketPath, token, timeoutMs: connectTimeoutMs });
13837
+ const connecting = connectClient({ socketPath, token, timeoutMs: connectTimeoutMs });
13838
+ connecting.catch(() => {
13839
+ });
13840
+ const client = await (helperExited ? Promise.race([connecting, helperExited.promise]) : connecting);
13787
13841
  return {
13788
13842
  client,
13789
13843
  async close() {
@@ -13798,6 +13852,42 @@ async function startPrivilegedHelper(options) {
13798
13852
  } catch {
13799
13853
  }
13800
13854
  throw error;
13855
+ } finally {
13856
+ helperExited == null ? void 0 : helperExited.stop();
13857
+ }
13858
+ }
13859
+ function watchWindowsHelper(pid, errorFile) {
13860
+ let timer;
13861
+ const promise = new Promise((_, reject2) => {
13862
+ timer = setInterval(() => {
13863
+ if (isProcessRunning(pid))
13864
+ return;
13865
+ clearInterval(timer);
13866
+ let detail = "";
13867
+ try {
13868
+ detail = (0, import_node_fs3.readFileSync)(errorFile, "utf8").trim().slice(-4096);
13869
+ } catch {
13870
+ }
13871
+ reject2(new Error(`privileged helper exited before accepting connections.${detail ? ` ${detail}` : " No diagnostic output from the helper."}`));
13872
+ }, 250);
13873
+ });
13874
+ return {
13875
+ promise,
13876
+ stop() {
13877
+ clearInterval(timer);
13878
+ try {
13879
+ (0, import_node_fs3.rmSync)(errorFile, { force: true });
13880
+ } catch {
13881
+ }
13882
+ }
13883
+ };
13884
+ }
13885
+ function isProcessRunning(pid) {
13886
+ try {
13887
+ import_node_process6.default.kill(pid, 0);
13888
+ return true;
13889
+ } catch (error) {
13890
+ return error.code !== "ESRCH";
13801
13891
  }
13802
13892
  }
13803
13893
  async function connectExistingHelper(socketPath, token, timeoutMs = 1500) {
@@ -13810,12 +13900,12 @@ async function connectExistingHelper(socketPath, token, timeoutMs = 1500) {
13810
13900
 
13811
13901
  // src/privileged/session.ts
13812
13902
  function tokenPath(runtimeDir) {
13813
- return (0, import_node_path3.join)(runtimeDir, "helper.token");
13903
+ return (0, import_node_path4.join)(runtimeDir, "helper.token");
13814
13904
  }
13815
13905
  function sessionSocketPath(runtimeDir) {
13816
13906
  if ((0, import_node_os4.platform)() === "win32")
13817
13907
  return `\\\\.\\pipe\\uhrp-${import_node_buffer.Buffer.from(runtimeDir).toString("hex").slice(0, 40)}`;
13818
- return (0, import_node_path3.join)(runtimeDir, "helper.sock");
13908
+ return (0, import_node_path4.join)(runtimeDir, "helper.sock");
13819
13909
  }
13820
13910
  async function openHelperSession(scope2) {
13821
13911
  const socketPath = sessionSocketPath(scope2.runtimeDir);
@@ -13830,7 +13920,7 @@ async function openHelperSession(scope2) {
13830
13920
  }
13831
13921
  }
13832
13922
  async function tryReuse(socketPath, scope2) {
13833
- if (!(0, import_node_fs3.existsSync)(socketPath) && (0, import_node_os4.platform)() !== "win32")
13923
+ if (!(0, import_node_fs4.existsSync)(socketPath) && (0, import_node_os4.platform)() !== "win32")
13834
13924
  return void 0;
13835
13925
  const token = await readToken(scope2.runtimeDir);
13836
13926
  if (!token)
@@ -13874,7 +13964,7 @@ async function startNew(socketPath, scope2) {
13874
13964
  };
13875
13965
  }
13876
13966
  async function acquireStartLock(runtimeDir) {
13877
- const lockPath = (0, import_node_path3.join)(runtimeDir, "helper.start.lock");
13967
+ const lockPath = (0, import_node_path4.join)(runtimeDir, "helper.start.lock");
13878
13968
  try {
13879
13969
  await (0, import_promises.writeFile)(lockPath, "", { flag: "a" });
13880
13970
  } catch {
@@ -13908,7 +13998,7 @@ async function writeToken(runtimeDir, token) {
13908
13998
  // src/caddy/privileged.ts
13909
13999
  var import_meta = {};
13910
14000
  function resolvePrivilegedEnv() {
13911
- const moduleDir = typeof __dirname === "string" ? __dirname : (0, import_node_path4.dirname)((0, import_node_url.fileURLToPath)(import_meta.url));
14001
+ const moduleDir = typeof __dirname === "string" ? __dirname : (0, import_node_path5.dirname)((0, import_node_url.fileURLToPath)(import_meta.url));
13912
14002
  const resolved = resolveHelperEntry({ moduleDir });
13913
14003
  return { helperEntry: resolved.entry, helperArgs: resolved.args };
13914
14004
  }
@@ -13948,7 +14038,7 @@ var HelperCaddyProcess = class extends import_node_events.EventEmitter {
13948
14038
  }
13949
14039
  };
13950
14040
  function caddyStoragePath() {
13951
- return import_node_process7.default.platform === "win32" ? (0, import_node_path4.join)(import_node_process7.default.env.AppData ?? "", "Caddy") : (0, import_node_path4.join)(import_node_process7.default.env.HOME ?? "", "Library/Application Support/Caddy");
14041
+ return import_node_process7.default.platform === "win32" ? (0, import_node_path5.join)(import_node_process7.default.env.AppData ?? "", "Caddy") : (0, import_node_path5.join)(import_node_process7.default.env.HOME ?? "", "Library/Application Support/Caddy");
13952
14042
  }
13953
14043
  function openPrivilegedSession(runtimeDir, options) {
13954
14044
  const env2 = resolvePrivilegedEnv();
@@ -14092,11 +14182,11 @@ async function download() {
14092
14182
  if (await testCaddy())
14093
14183
  return caddyPath;
14094
14184
  return new Promise((resolve2, reject2) => {
14095
- if (!(0, import_node_fs4.existsSync)(TEMP_DIR)) {
14096
- (0, import_node_fs4.mkdirSync)(TEMP_DIR, { recursive: true });
14097
- (0, import_node_fs4.chmodSync)(TEMP_DIR, 448);
14185
+ if (!(0, import_node_fs5.existsSync)(TEMP_DIR)) {
14186
+ (0, import_node_fs5.mkdirSync)(TEMP_DIR, { recursive: true });
14187
+ (0, import_node_fs5.chmodSync)(TEMP_DIR, 448);
14098
14188
  }
14099
- const file = (0, import_node_fs4.createWriteStream)(caddyPath);
14189
+ const file = (0, import_node_fs5.createWriteStream)(caddyPath);
14100
14190
  let _platform = (0, import_node_os5.platform)().toString();
14101
14191
  _platform === "win32" && (_platform = "windows");
14102
14192
  let _arch = import_node_process8.default.arch.toString();
@@ -14105,13 +14195,13 @@ async function download() {
14105
14195
  if (!support)
14106
14196
  return reject2(new Error("not support"));
14107
14197
  const dowmloadLink = `https://caddyserver.com/api/download?os=${support.platform}&arch=${support.arch}&p=github.com%2Fcaddyserver%2Fforwardproxy`;
14108
- (0, import_node_fs4.existsSync)(caddyPath) && (0, import_node_fs4.unlinkSync)(caddyPath);
14198
+ (0, import_node_fs5.existsSync)(caddyPath) && (0, import_node_fs5.unlinkSync)(caddyPath);
14109
14199
  const httpProxy = import_node_process8.default.env.HTTP_PROXY || import_node_process8.default.env.http_proxy;
14110
14200
  const httpsProxy = import_node_process8.default.env.HTTPS_PROXY || import_node_process8.default.env.https_proxy;
14111
14201
  const httpAgent = httpProxy ? new import_http_proxy_agent.HttpProxyAgent(httpProxy) : void 0;
14112
14202
  const httpsAgent = httpsProxy ? new import_https_proxy_agent.HttpsProxyAgent(httpsProxy) : void 0;
14113
14203
  const chmodCaddyOnce = once(() => {
14114
- (0, import_node_fs4.chmodSync)(caddyPath, 493);
14204
+ (0, import_node_fs5.chmodSync)(caddyPath, 493);
14115
14205
  });
14116
14206
  import_got_cjs.got.stream(dowmloadLink, {
14117
14207
  agent: {
@@ -14125,7 +14215,7 @@ async function download() {
14125
14215
  logProgressOver();
14126
14216
  if (import_node_process8.default.platform === "win32")
14127
14217
  return resolve2(caddyPath);
14128
- (0, import_node_fs4.chmodSync)(caddyPath, 493);
14218
+ (0, import_node_fs5.chmodSync)(caddyPath, 493);
14129
14219
  resolve2(caddyPath);
14130
14220
  }).on("error", (err) => {
14131
14221
  reject2(err);
@@ -14134,10 +14224,10 @@ async function download() {
14134
14224
  }
14135
14225
  function testCaddy() {
14136
14226
  return new Promise((resolve2) => {
14137
- if (!(0, import_node_fs4.existsSync)(caddyPath))
14227
+ if (!(0, import_node_fs5.existsSync)(caddyPath))
14138
14228
  return resolve2(false);
14139
14229
  try {
14140
- (0, import_node_fs4.chmodSync)(caddyPath, 493);
14230
+ (0, import_node_fs5.chmodSync)(caddyPath, 493);
14141
14231
  } catch {
14142
14232
  }
14143
14233
  (0, import_node_child_process3.execFile)(caddyPath, [], { timeout: 1e4 }, (error, stdout) => {
@@ -14148,7 +14238,7 @@ function testCaddy() {
14148
14238
  });
14149
14239
  }
14150
14240
  function caddyRootPath() {
14151
- return (0, import_node_path5.join)(
14241
+ return (0, import_node_path6.join)(
14152
14242
  import_node_process8.default.platform === "win32" ? `${import_node_process8.default.env.AppData}/Caddy` : `${import_node_process8.default.env.HOME}/Library/Application Support/Caddy`,
14153
14243
  "./pki/authorities/local"
14154
14244
  );
@@ -14218,7 +14308,7 @@ var CaddyInstant = class {
14218
14308
  return this._session;
14219
14309
  }
14220
14310
  async getCaddyfile() {
14221
- if (!(0, import_node_fs4.existsSync)(caddyFilePath)) {
14311
+ if (!(0, import_node_fs5.existsSync)(caddyFilePath)) {
14222
14312
  this.caddyfile = "";
14223
14313
  return "";
14224
14314
  }
@@ -14783,6 +14873,8 @@ var unpluginFactory = (options) => ({
14783
14873
  return;
14784
14874
  }
14785
14875
  const _printUrls = server.printUrls.bind(server);
14876
+ if (server.config.server.middlewareMode)
14877
+ consola.warn("Vite runs in middleware mode, so the reverse proxy cannot start from the Vite plugin. In Nuxt, add `unplugin-https-reverse-proxy/nuxt` to `modules` instead.");
14786
14878
  let source = `localhost:${config.server.port || 5173}`;
14787
14879
  const url2 = (_a4 = server.resolvedUrls) == null ? void 0 : _a4.local[0];
14788
14880
  if (url2) {
package/dist/rspack.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  src_default
3
- } from "./chunk-OVYS4ED4.js";
3
+ } from "./chunk-U6J2RMIR.js";
4
4
  import "./chunk-EY3HXBJ4.js";
5
5
  import "./chunk-C5A7XLKL.js";
6
6
  import "./chunk-WOMR3ZLM.js";