unplugin-https-reverse-proxy 3.0.0 → 4.0.1

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/nuxt.cjs CHANGED
@@ -13477,33 +13477,63 @@ var caddyTemplate = `{
13477
13477
  }
13478
13478
  }
13479
13479
 
13480
- {% for p in proxies %}
13481
- {{ p.target }}{{ p.portSuffix }} {
13482
- {{ p.tls }}
13483
- reverse_proxy http://{{ p.source }} {% if p.healthCheck %} {
13484
- health_uri {{ p.base }}
13485
- health_interval 2s
13486
- health_timeout 5s
13487
- fail_duration 2s
13488
- unhealthy_status 502 503 504 404
13489
- lb_try_duration 3s
13490
- lb_try_interval 300ms
13480
+ {% for host in hosts %}
13481
+ {{ host.host }}{{ host.portSuffix }} {
13482
+ {{ host.tls }}
13491
13483
 
13492
- @error status 502 503 504 404
13493
- handle_response @error {
13494
- respond * 503 {
13495
- body "Service Unavailable"
13496
- close
13484
+ {% for route in host.routes %}
13485
+ handle {{ route.base }}* {
13486
+ reverse_proxy http://{{ route.source }} {% if route.healthCheck %} {
13487
+ health_uri {{ route.base }}
13488
+ health_interval 2s
13489
+ health_timeout 5s
13490
+ health_headers {
13491
+ User-Agent {http.request.header.User-Agent}
13492
+ Accept text/html,application/json,*/*
13493
+ }
13494
+ fail_duration 2s
13495
+ unhealthy_status 502 503 504 404
13496
+ lb_try_duration 3s
13497
+ lb_try_interval 300ms
13498
+
13499
+ @error status 502 503 504 404
13500
+ handle_response @error {
13501
+ respond * 503 {
13502
+ body "Service Unavailable"
13503
+ close
13504
+ }
13497
13505
  }
13498
13506
  }
13507
+ {% endif %}
13499
13508
  }
13500
- {% endif %}
13509
+ {% endfor %}
13501
13510
  }
13502
13511
  {% endfor %}
13503
13512
  `;
13504
13513
  var liquid = new Liquid();
13505
13514
  async function genCaddyfile(ctx) {
13506
- return await liquid.parseAndRender(caddyTemplate, ctx);
13515
+ const hostsMap = /* @__PURE__ */ new Map();
13516
+ for (const proxy of ctx.proxies) {
13517
+ const key = `${proxy.target}${proxy.portSuffix}`;
13518
+ if (!hostsMap.has(key)) {
13519
+ hostsMap.set(key, {
13520
+ host: proxy.target,
13521
+ portSuffix: proxy.portSuffix,
13522
+ tls: proxy.tls,
13523
+ routes: []
13524
+ });
13525
+ }
13526
+ let base = proxy.base || "/";
13527
+ if (!base.startsWith("/"))
13528
+ base = `/${base}`;
13529
+ if (base !== "/" && !base.endsWith("/"))
13530
+ base = `${base}/`;
13531
+ hostsMap.get(key).routes.push({ ...proxy, base });
13532
+ }
13533
+ for (const host of hostsMap.values())
13534
+ host.routes.sort((a, b) => b.base.length - a.base.length);
13535
+ const hosts = Array.from(hostsMap.values());
13536
+ return await liquid.parseAndRender(caddyTemplate, { ...ctx, hosts });
13507
13537
  }
13508
13538
 
13509
13539
  // src/caddy/config.ts
@@ -13668,8 +13698,13 @@ function useCaddyConfig() {
13668
13698
  function writeCaddyConfig(_proxies) {
13669
13699
  const proxies = configJsonRef() || {};
13670
13700
  _proxies.forEach((p) => {
13671
- const index = `${p.target}${p.portSuffix}`;
13672
- proxies[index] = p;
13701
+ let base = p.base || "/";
13702
+ if (!base.startsWith("/"))
13703
+ base = `/${base}`;
13704
+ if (base !== "/" && !base.endsWith("/"))
13705
+ base = `${base}/`;
13706
+ const index = `${p.target}${p.portSuffix}${base}`;
13707
+ proxies[index] = { ...p, base };
13673
13708
  });
13674
13709
  const content = JSON.stringify(proxies, null, 2);
13675
13710
  configContentRef(content);
@@ -13887,13 +13922,22 @@ var CaddyInstant = class {
13887
13922
  throw new Error("update host failed");
13888
13923
  consola.success("update host success\n");
13889
13924
  const targetHost = target.split(":")[0];
13925
+ const normalizeBase = (b) => {
13926
+ let out = b || "/";
13927
+ if (!out.startsWith("/"))
13928
+ out = `/${out}`;
13929
+ if (out !== "/" && !out.endsWith("/"))
13930
+ out = `${out}/`;
13931
+ return out;
13932
+ };
13933
+ const baseNormalized = normalizeBase(base);
13890
13934
  const proxies = [
13891
13935
  {
13892
13936
  target: targetHost,
13893
13937
  portSuffix: https ? "" : ":80",
13894
13938
  tls: https ? "tls internal" : "",
13895
13939
  source,
13896
- base,
13940
+ base: baseNormalized,
13897
13941
  healthCheck
13898
13942
  }
13899
13943
  ];
@@ -13903,14 +13947,12 @@ var CaddyInstant = class {
13903
13947
  effect(async () => {
13904
13948
  const { configJsonRef } = getCaddyConfig();
13905
13949
  const proxies2 = Object.values(configJsonRef() || {});
13906
- if (!(this.caddyfile || "").includes(`${target.split(":")[0]}${https ? "" : ":80"}`)) {
13907
- const ctx = validateTemplateContext({
13908
- caddy_root: this.caddyRoot,
13909
- proxies: proxies2
13910
- });
13911
- this.caddyfile = await genCaddyfile(ctx);
13912
- await this.writeCaddyfile();
13913
- }
13950
+ const ctx = validateTemplateContext({
13951
+ caddy_root: this.caddyRoot,
13952
+ proxies: proxies2
13953
+ });
13954
+ this.caddyfile = await genCaddyfile(ctx);
13955
+ await this.writeCaddyfile();
13914
13956
  })
13915
13957
  );
13916
13958
  if (!(0, import_node_fs3.existsSync)(caddyLockFilePath))
@@ -14046,6 +14088,137 @@ ${line}
14046
14088
  }
14047
14089
  };
14048
14090
 
14091
+ // src/hmr-sse-bridge.ts
14092
+ var SSE_PATH = "/__hmr_sse";
14093
+ var SEND_PATH = "/__hmr_send";
14094
+ var MAX_POST_BODY = 64 * 1024;
14095
+ var hmrSseBridgeClientScript = `
14096
+ (function() {
14097
+ // Activate when served over HTTPS to bypass WSS self-signed cert issues
14098
+ if (location.protocol !== 'https:') return;
14099
+
14100
+ var OriginalWebSocket = window.WebSocket;
14101
+
14102
+ function SseWebSocket(originalUrl) {
14103
+ var self = this;
14104
+ // WebSocket readyState constants on instance
14105
+ self.CONNECTING = 0;
14106
+ self.OPEN = 1;
14107
+ self.CLOSING = 2;
14108
+ self.CLOSED = 3;
14109
+ self.readyState = 0;
14110
+ self.onopen = null;
14111
+ self.onmessage = null;
14112
+ self.onclose = null;
14113
+ self.onerror = null;
14114
+ self.bufferedAmount = 0;
14115
+ self.extensions = '';
14116
+ self.binaryType = 'blob';
14117
+ self.protocol = 'vite-hmr';
14118
+ self.url = originalUrl;
14119
+ self._listeners = {};
14120
+ self._opened = false;
14121
+
14122
+ var sseUrl = location.origin + '${SSE_PATH}';
14123
+ var sendUrl = location.origin + '${SEND_PATH}';
14124
+ self._sendUrl = sendUrl;
14125
+
14126
+ var es = new EventSource(sseUrl);
14127
+ self._es = es;
14128
+
14129
+ es.onopen = function() {
14130
+ if (self._opened) return;
14131
+ self._opened = true;
14132
+ self.readyState = 1;
14133
+ var evt = new Event('open');
14134
+ if (self.onopen) self.onopen(evt);
14135
+ self._emit('open', evt);
14136
+ };
14137
+
14138
+ es.onmessage = function(e) {
14139
+ var evt = new MessageEvent('message', { data: e.data });
14140
+ if (self.onmessage) self.onmessage(evt);
14141
+ self._emit('message', evt);
14142
+ };
14143
+
14144
+ es.onerror = function() {
14145
+ // EventSource auto-reconnects on error; don't treat as fatal close
14146
+ // Only emit error event, never close event (SSE handles reconnect internally)
14147
+ if (self.readyState >= 2) return;
14148
+ // EventSource auto-reconnects; no action needed
14149
+ };
14150
+ }
14151
+
14152
+ SseWebSocket.prototype.send = function(data) {
14153
+ if (this.readyState !== 1) return;
14154
+ fetch(this._sendUrl, {
14155
+ method: 'POST',
14156
+ headers: { 'Content-Type': 'application/json' },
14157
+ body: data
14158
+ }).catch(function() {});
14159
+ };
14160
+
14161
+ SseWebSocket.prototype.close = function() {
14162
+ if (this.readyState >= 2) return;
14163
+ this.readyState = 3;
14164
+ if (this._es) this._es.close();
14165
+ var evt = new CloseEvent('close', { code: 1000, reason: '', wasClean: true });
14166
+ if (this.onclose) this.onclose(evt);
14167
+ this._emit('close', evt);
14168
+ };
14169
+
14170
+ SseWebSocket.prototype.addEventListener = function(type, fn, options) {
14171
+ if (!this._listeners[type]) this._listeners[type] = [];
14172
+ var entry = { fn: fn, once: !!(options && options.once) };
14173
+ this._listeners[type].push(entry);
14174
+ };
14175
+
14176
+ SseWebSocket.prototype.removeEventListener = function(type, fn) {
14177
+ var list = this._listeners[type];
14178
+ if (!list) return;
14179
+ this._listeners[type] = list.filter(function(e) { return e.fn !== fn; });
14180
+ };
14181
+
14182
+ SseWebSocket.prototype.dispatchEvent = function(evt) {
14183
+ this._emit(evt.type, evt);
14184
+ return true;
14185
+ };
14186
+
14187
+ SseWebSocket.prototype._emit = function(type, evt) {
14188
+ var list = this._listeners[type];
14189
+ if (!list) return;
14190
+ var remaining = [];
14191
+ list.forEach(function(entry) {
14192
+ entry.fn(evt);
14193
+ if (!entry.once) remaining.push(entry);
14194
+ });
14195
+ this._listeners[type] = remaining;
14196
+ };
14197
+
14198
+ function PatchedWebSocket(url, protocols) {
14199
+ // Only intercept Vite HMR connections (protocol 'vite-hmr')
14200
+ var isViteHmr = protocols === 'vite-hmr'
14201
+ || (Array.isArray(protocols) && protocols.indexOf('vite-hmr') !== -1);
14202
+ if (!isViteHmr) {
14203
+ if (protocols !== undefined) {
14204
+ return new OriginalWebSocket(url, protocols);
14205
+ }
14206
+ return new OriginalWebSocket(url);
14207
+ }
14208
+ return new SseWebSocket(url);
14209
+ }
14210
+
14211
+ PatchedWebSocket.CONNECTING = 0;
14212
+ PatchedWebSocket.OPEN = 1;
14213
+ PatchedWebSocket.CLOSING = 2;
14214
+ PatchedWebSocket.CLOSED = 3;
14215
+ PatchedWebSocket.prototype = Object.create(OriginalWebSocket.prototype);
14216
+ PatchedWebSocket.prototype.constructor = PatchedWebSocket;
14217
+
14218
+ window.WebSocket = PatchedWebSocket;
14219
+ })();
14220
+ `;
14221
+
14049
14222
  // src/index.ts
14050
14223
  var caddy;
14051
14224
  var cwd = import_node_process5.default.cwd();
package/dist/nuxt.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  vitePrintUrls
3
- } from "./chunk-5MC5SB7O.js";
3
+ } from "./chunk-QURDG7SD.js";
4
+ import "./chunk-RSBXFJ3Z.js";
4
5
  import {
5
6
  consola,
6
7
  isAdmin