unplugin-https-reverse-proxy 4.0.0 → 4.0.2
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/{chunk-K7HSLE6X.js → chunk-JA3D5AWS.js} +40 -4
- package/dist/chunk-RSBXFJ3Z.js +232 -0
- package/dist/esbuild.cjs +264 -4
- package/dist/esbuild.js +2 -1
- package/dist/hmr-sse-bridge.cjs +258 -0
- package/dist/hmr-sse-bridge.d.cts +10 -0
- package/dist/hmr-sse-bridge.d.ts +10 -0
- package/dist/hmr-sse-bridge.js +8 -0
- package/dist/index.cjs +264 -4
- package/dist/index.js +2 -1
- package/dist/nuxt.cjs +152 -4
- package/dist/nuxt.js +2 -1
- package/dist/rollup.cjs +264 -4
- package/dist/rollup.js +2 -1
- package/dist/rspack.cjs +264 -4
- package/dist/rspack.js +2 -1
- package/dist/vite.cjs +264 -4
- package/dist/vite.js +2 -1
- package/dist/webpack.cjs +264 -4
- package/dist/webpack.js +2 -1
- package/package.json +1 -1
- package/src/caddy/index.ts +24 -5
- package/src/hmr-sse-bridge.ts +266 -0
- package/src/index.ts +20 -0
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hmrSseBridgeClientScript,
|
|
3
|
+
setupHmrSseBridge
|
|
4
|
+
} from "./chunk-RSBXFJ3Z.js";
|
|
1
5
|
import {
|
|
2
6
|
chmodRecursive,
|
|
3
7
|
consola,
|
|
@@ -13774,6 +13778,17 @@ function getCaddyConfig() {
|
|
|
13774
13778
|
}
|
|
13775
13779
|
return caddyConfig;
|
|
13776
13780
|
}
|
|
13781
|
+
function splitHostPort(addr) {
|
|
13782
|
+
const lastColon = addr.lastIndexOf(":");
|
|
13783
|
+
if (lastColon === -1)
|
|
13784
|
+
return { host: addr, port: "" };
|
|
13785
|
+
if (addr.startsWith("[")) {
|
|
13786
|
+
const end = addr.indexOf("]");
|
|
13787
|
+
if (end !== -1 && addr[end + 1] === ":")
|
|
13788
|
+
return { host: addr.slice(1, end), port: addr.slice(end + 2) };
|
|
13789
|
+
}
|
|
13790
|
+
return { host: addr.slice(0, lastColon), port: addr.slice(lastColon + 1) };
|
|
13791
|
+
}
|
|
13777
13792
|
var CaddyInstant = class {
|
|
13778
13793
|
constructor() {
|
|
13779
13794
|
this.inited = false;
|
|
@@ -13821,6 +13836,7 @@ var CaddyInstant = class {
|
|
|
13821
13836
|
*/
|
|
13822
13837
|
async run(source, target, options) {
|
|
13823
13838
|
source = source.replace("0.0.0.0", "127.0.0.1");
|
|
13839
|
+
source = source.replace(/^localhost(?=:)/i, "127.0.0.1");
|
|
13824
13840
|
const {
|
|
13825
13841
|
restore = true,
|
|
13826
13842
|
base = "/",
|
|
@@ -13832,10 +13848,11 @@ var CaddyInstant = class {
|
|
|
13832
13848
|
this._target = target;
|
|
13833
13849
|
if (!this.inited)
|
|
13834
13850
|
await this.init();
|
|
13835
|
-
|
|
13851
|
+
const sourceHost = splitHostPort(source).host;
|
|
13852
|
+
const targetHost = splitHostPort(target).host;
|
|
13853
|
+
if (!await addHost(sourceHost, targetHost))
|
|
13836
13854
|
throw new Error("update host failed");
|
|
13837
13855
|
consola.success("update host success\n");
|
|
13838
|
-
const targetHost = target.split(":")[0];
|
|
13839
13856
|
const normalizeBase = (b) => {
|
|
13840
13857
|
let out = b || "/";
|
|
13841
13858
|
if (!out.startsWith("/"))
|
|
@@ -13895,7 +13912,7 @@ var CaddyInstant = class {
|
|
|
13895
13912
|
});
|
|
13896
13913
|
const originalExit = process4.exit.bind(process4);
|
|
13897
13914
|
process4.exit = async (code) => {
|
|
13898
|
-
const port = Number(source.
|
|
13915
|
+
const port = Number(splitHostPort(source).port);
|
|
13899
13916
|
if (!Number.isNaN(port) && await tryPort(port))
|
|
13900
13917
|
await kill(port);
|
|
13901
13918
|
const pwd = process4.cwd();
|
|
@@ -13903,6 +13920,10 @@ var CaddyInstant = class {
|
|
|
13903
13920
|
if (existsSync3(viteCacheDir))
|
|
13904
13921
|
await chmodRecursive(viteCacheDir, 511).catch(() => {
|
|
13905
13922
|
});
|
|
13923
|
+
const viteTempCacheDir = `${pwd}/node_modules/.vite-temp`;
|
|
13924
|
+
if (existsSync3(viteTempCacheDir))
|
|
13925
|
+
await chmodRecursive(viteTempCacheDir, 511).catch(() => {
|
|
13926
|
+
});
|
|
13906
13927
|
const nuxtCacheDir = `${pwd}/.nuxt`;
|
|
13907
13928
|
if (existsSync3(nuxtCacheDir))
|
|
13908
13929
|
await chmodRecursive(nuxtCacheDir, 511).catch(() => {
|
|
@@ -13974,7 +13995,7 @@ ${line}
|
|
|
13974
13995
|
this._stopEffects.forEach((fn) => fn());
|
|
13975
13996
|
try {
|
|
13976
13997
|
if (restore && this._source && this._target) {
|
|
13977
|
-
if (await removeHost(this._source.
|
|
13998
|
+
if (await removeHost(splitHostPort(this._source).host, splitHostPort(this._target).host))
|
|
13978
13999
|
consola.success("Restore host success.\n");
|
|
13979
14000
|
else
|
|
13980
14001
|
consola.fail("Restore host failed.\n");
|
|
@@ -14005,6 +14026,7 @@ ${line}
|
|
|
14005
14026
|
// src/index.ts
|
|
14006
14027
|
var config;
|
|
14007
14028
|
var caddy;
|
|
14029
|
+
var hmrSseBridgeActive = false;
|
|
14008
14030
|
var cwd = process5.cwd();
|
|
14009
14031
|
function colorUrl(url2) {
|
|
14010
14032
|
return c.green(url2.replace(/:(\d+)\//, (_, port) => `:${c.bold(port)}/`));
|
|
@@ -14091,6 +14113,20 @@ var unpluginFactory = (options) => ({
|
|
|
14091
14113
|
}
|
|
14092
14114
|
const base = server.config.base || "/";
|
|
14093
14115
|
server.printUrls = () => vitePrintUrls(options, source, target, base, _printUrls);
|
|
14116
|
+
if (options.https) {
|
|
14117
|
+
setupHmrSseBridge(server);
|
|
14118
|
+
hmrSseBridgeActive = true;
|
|
14119
|
+
consola.info("HMR SSE bridge enabled for iOS Safari WSS workaround");
|
|
14120
|
+
}
|
|
14121
|
+
},
|
|
14122
|
+
transformIndexHtml() {
|
|
14123
|
+
if (!hmrSseBridgeActive)
|
|
14124
|
+
return [];
|
|
14125
|
+
return [{
|
|
14126
|
+
tag: "script",
|
|
14127
|
+
children: hmrSseBridgeClientScript,
|
|
14128
|
+
injectTo: "head-prepend"
|
|
14129
|
+
}];
|
|
14094
14130
|
}
|
|
14095
14131
|
},
|
|
14096
14132
|
webpack(compiler) {
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// src/hmr-sse-bridge.ts
|
|
2
|
+
var SSE_PATH = "/__hmr_sse";
|
|
3
|
+
var SEND_PATH = "/__hmr_send";
|
|
4
|
+
var MAX_POST_BODY = 64 * 1024;
|
|
5
|
+
function setupHmrSseBridge(server) {
|
|
6
|
+
const sseClients = /* @__PURE__ */ new Set();
|
|
7
|
+
const originalSend = server.ws.send.bind(server.ws);
|
|
8
|
+
server.ws.send = (...args) => {
|
|
9
|
+
;
|
|
10
|
+
originalSend(...args);
|
|
11
|
+
let payload;
|
|
12
|
+
if (typeof args[0] === "string") {
|
|
13
|
+
payload = JSON.stringify({ type: "custom", event: args[0], data: args[1] });
|
|
14
|
+
} else {
|
|
15
|
+
payload = JSON.stringify(args[0]);
|
|
16
|
+
}
|
|
17
|
+
for (const res of sseClients) {
|
|
18
|
+
try {
|
|
19
|
+
res.write(`data: ${payload}
|
|
20
|
+
|
|
21
|
+
`);
|
|
22
|
+
} catch {
|
|
23
|
+
sseClients.delete(res);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
server.middlewares.use((req, res, next) => {
|
|
28
|
+
var _a;
|
|
29
|
+
const url = (_a = req.url) == null ? void 0 : _a.split("?")[0];
|
|
30
|
+
if (url === SSE_PATH && req.method === "GET") {
|
|
31
|
+
handleSseConnection(req, res, sseClients);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (url === SEND_PATH && req.method === "POST") {
|
|
35
|
+
handleClientMessage(req, res, server);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
next();
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function handleSseConnection(_req, res, sseClients) {
|
|
42
|
+
res.writeHead(200, {
|
|
43
|
+
"Content-Type": "text/event-stream",
|
|
44
|
+
"Cache-Control": "no-cache",
|
|
45
|
+
"Connection": "keep-alive"
|
|
46
|
+
});
|
|
47
|
+
res.write(`data: ${JSON.stringify({ type: "connected" })}
|
|
48
|
+
|
|
49
|
+
`);
|
|
50
|
+
sseClients.add(res);
|
|
51
|
+
const keepAlive = setInterval(() => {
|
|
52
|
+
try {
|
|
53
|
+
res.write(": ping\n\n");
|
|
54
|
+
} catch {
|
|
55
|
+
cleanup();
|
|
56
|
+
}
|
|
57
|
+
}, 3e4);
|
|
58
|
+
function cleanup() {
|
|
59
|
+
clearInterval(keepAlive);
|
|
60
|
+
sseClients.delete(res);
|
|
61
|
+
}
|
|
62
|
+
res.on("close", cleanup);
|
|
63
|
+
res.on("error", cleanup);
|
|
64
|
+
}
|
|
65
|
+
function handleClientMessage(req, res, server) {
|
|
66
|
+
let body = "";
|
|
67
|
+
let overflow = false;
|
|
68
|
+
req.on("data", (chunk) => {
|
|
69
|
+
body += chunk.toString();
|
|
70
|
+
if (body.length > MAX_POST_BODY) {
|
|
71
|
+
overflow = true;
|
|
72
|
+
req.destroy();
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
req.on("end", () => {
|
|
76
|
+
var _a, _b, _c, _d, _e;
|
|
77
|
+
if (overflow) {
|
|
78
|
+
res.writeHead(413);
|
|
79
|
+
res.end('{"ok":false}');
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
const data = JSON.parse(body);
|
|
84
|
+
if (data.type === "custom" && data.event) {
|
|
85
|
+
const dummyClient = {
|
|
86
|
+
send: (...args) => {
|
|
87
|
+
server.ws.send(...args);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
(_e = (_d = (_c = (_b = (_a = server.environments) == null ? void 0 : _a.client) == null ? void 0 : _b.hot) == null ? void 0 : _c.api) == null ? void 0 : _d.emit) == null ? void 0 : _e.call(_d, data.event, data.data, dummyClient);
|
|
91
|
+
}
|
|
92
|
+
res.writeHead(200, {
|
|
93
|
+
"Content-Type": "application/json"
|
|
94
|
+
});
|
|
95
|
+
res.end('{"ok":true}');
|
|
96
|
+
} catch {
|
|
97
|
+
res.writeHead(400);
|
|
98
|
+
res.end('{"ok":false}');
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
var hmrSseBridgeClientScript = `
|
|
103
|
+
(function() {
|
|
104
|
+
// Activate when served over HTTPS to bypass WSS self-signed cert issues
|
|
105
|
+
if (location.protocol !== 'https:') return;
|
|
106
|
+
|
|
107
|
+
var OriginalWebSocket = window.WebSocket;
|
|
108
|
+
|
|
109
|
+
function SseWebSocket(originalUrl) {
|
|
110
|
+
var self = this;
|
|
111
|
+
// WebSocket readyState constants on instance
|
|
112
|
+
self.CONNECTING = 0;
|
|
113
|
+
self.OPEN = 1;
|
|
114
|
+
self.CLOSING = 2;
|
|
115
|
+
self.CLOSED = 3;
|
|
116
|
+
self.readyState = 0;
|
|
117
|
+
self.onopen = null;
|
|
118
|
+
self.onmessage = null;
|
|
119
|
+
self.onclose = null;
|
|
120
|
+
self.onerror = null;
|
|
121
|
+
self.bufferedAmount = 0;
|
|
122
|
+
self.extensions = '';
|
|
123
|
+
self.binaryType = 'blob';
|
|
124
|
+
self.protocol = 'vite-hmr';
|
|
125
|
+
self.url = originalUrl;
|
|
126
|
+
self._listeners = {};
|
|
127
|
+
self._opened = false;
|
|
128
|
+
|
|
129
|
+
var sseUrl = location.origin + '${SSE_PATH}';
|
|
130
|
+
var sendUrl = location.origin + '${SEND_PATH}';
|
|
131
|
+
self._sendUrl = sendUrl;
|
|
132
|
+
|
|
133
|
+
var es = new EventSource(sseUrl);
|
|
134
|
+
self._es = es;
|
|
135
|
+
|
|
136
|
+
es.onopen = function() {
|
|
137
|
+
if (self._opened) return;
|
|
138
|
+
self._opened = true;
|
|
139
|
+
self.readyState = 1;
|
|
140
|
+
var evt = new Event('open');
|
|
141
|
+
if (self.onopen) self.onopen(evt);
|
|
142
|
+
self._emit('open', evt);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
es.onmessage = function(e) {
|
|
146
|
+
var evt = new MessageEvent('message', { data: e.data });
|
|
147
|
+
if (self.onmessage) self.onmessage(evt);
|
|
148
|
+
self._emit('message', evt);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
es.onerror = function() {
|
|
152
|
+
// EventSource auto-reconnects on error; don't treat as fatal close
|
|
153
|
+
// Only emit error event, never close event (SSE handles reconnect internally)
|
|
154
|
+
if (self.readyState >= 2) return;
|
|
155
|
+
// EventSource auto-reconnects; no action needed
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
SseWebSocket.prototype.send = function(data) {
|
|
160
|
+
if (this.readyState !== 1) return;
|
|
161
|
+
fetch(this._sendUrl, {
|
|
162
|
+
method: 'POST',
|
|
163
|
+
headers: { 'Content-Type': 'application/json' },
|
|
164
|
+
body: data
|
|
165
|
+
}).catch(function() {});
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
SseWebSocket.prototype.close = function() {
|
|
169
|
+
if (this.readyState >= 2) return;
|
|
170
|
+
this.readyState = 3;
|
|
171
|
+
if (this._es) this._es.close();
|
|
172
|
+
var evt = new CloseEvent('close', { code: 1000, reason: '', wasClean: true });
|
|
173
|
+
if (this.onclose) this.onclose(evt);
|
|
174
|
+
this._emit('close', evt);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
SseWebSocket.prototype.addEventListener = function(type, fn, options) {
|
|
178
|
+
if (!this._listeners[type]) this._listeners[type] = [];
|
|
179
|
+
var entry = { fn: fn, once: !!(options && options.once) };
|
|
180
|
+
this._listeners[type].push(entry);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
SseWebSocket.prototype.removeEventListener = function(type, fn) {
|
|
184
|
+
var list = this._listeners[type];
|
|
185
|
+
if (!list) return;
|
|
186
|
+
this._listeners[type] = list.filter(function(e) { return e.fn !== fn; });
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
SseWebSocket.prototype.dispatchEvent = function(evt) {
|
|
190
|
+
this._emit(evt.type, evt);
|
|
191
|
+
return true;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
SseWebSocket.prototype._emit = function(type, evt) {
|
|
195
|
+
var list = this._listeners[type];
|
|
196
|
+
if (!list) return;
|
|
197
|
+
var remaining = [];
|
|
198
|
+
list.forEach(function(entry) {
|
|
199
|
+
entry.fn(evt);
|
|
200
|
+
if (!entry.once) remaining.push(entry);
|
|
201
|
+
});
|
|
202
|
+
this._listeners[type] = remaining;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
function PatchedWebSocket(url, protocols) {
|
|
206
|
+
// Only intercept Vite HMR connections (protocol 'vite-hmr')
|
|
207
|
+
var isViteHmr = protocols === 'vite-hmr'
|
|
208
|
+
|| (Array.isArray(protocols) && protocols.indexOf('vite-hmr') !== -1);
|
|
209
|
+
if (!isViteHmr) {
|
|
210
|
+
if (protocols !== undefined) {
|
|
211
|
+
return new OriginalWebSocket(url, protocols);
|
|
212
|
+
}
|
|
213
|
+
return new OriginalWebSocket(url);
|
|
214
|
+
}
|
|
215
|
+
return new SseWebSocket(url);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
PatchedWebSocket.CONNECTING = 0;
|
|
219
|
+
PatchedWebSocket.OPEN = 1;
|
|
220
|
+
PatchedWebSocket.CLOSING = 2;
|
|
221
|
+
PatchedWebSocket.CLOSED = 3;
|
|
222
|
+
PatchedWebSocket.prototype = Object.create(OriginalWebSocket.prototype);
|
|
223
|
+
PatchedWebSocket.prototype.constructor = PatchedWebSocket;
|
|
224
|
+
|
|
225
|
+
window.WebSocket = PatchedWebSocket;
|
|
226
|
+
})();
|
|
227
|
+
`;
|
|
228
|
+
|
|
229
|
+
export {
|
|
230
|
+
setupHmrSseBridge,
|
|
231
|
+
hmrSseBridgeClientScript
|
|
232
|
+
};
|
package/dist/esbuild.cjs
CHANGED
|
@@ -13859,6 +13859,17 @@ function getCaddyConfig() {
|
|
|
13859
13859
|
}
|
|
13860
13860
|
return caddyConfig;
|
|
13861
13861
|
}
|
|
13862
|
+
function splitHostPort(addr) {
|
|
13863
|
+
const lastColon = addr.lastIndexOf(":");
|
|
13864
|
+
if (lastColon === -1)
|
|
13865
|
+
return { host: addr, port: "" };
|
|
13866
|
+
if (addr.startsWith("[")) {
|
|
13867
|
+
const end = addr.indexOf("]");
|
|
13868
|
+
if (end !== -1 && addr[end + 1] === ":")
|
|
13869
|
+
return { host: addr.slice(1, end), port: addr.slice(end + 2) };
|
|
13870
|
+
}
|
|
13871
|
+
return { host: addr.slice(0, lastColon), port: addr.slice(lastColon + 1) };
|
|
13872
|
+
}
|
|
13862
13873
|
var CaddyInstant = class {
|
|
13863
13874
|
constructor() {
|
|
13864
13875
|
this.inited = false;
|
|
@@ -13906,6 +13917,7 @@ var CaddyInstant = class {
|
|
|
13906
13917
|
*/
|
|
13907
13918
|
async run(source, target, options) {
|
|
13908
13919
|
source = source.replace("0.0.0.0", "127.0.0.1");
|
|
13920
|
+
source = source.replace(/^localhost(?=:)/i, "127.0.0.1");
|
|
13909
13921
|
const {
|
|
13910
13922
|
restore = true,
|
|
13911
13923
|
base = "/",
|
|
@@ -13917,10 +13929,11 @@ var CaddyInstant = class {
|
|
|
13917
13929
|
this._target = target;
|
|
13918
13930
|
if (!this.inited)
|
|
13919
13931
|
await this.init();
|
|
13920
|
-
|
|
13932
|
+
const sourceHost = splitHostPort(source).host;
|
|
13933
|
+
const targetHost = splitHostPort(target).host;
|
|
13934
|
+
if (!await addHost(sourceHost, targetHost))
|
|
13921
13935
|
throw new Error("update host failed");
|
|
13922
13936
|
consola.success("update host success\n");
|
|
13923
|
-
const targetHost = target.split(":")[0];
|
|
13924
13937
|
const normalizeBase = (b) => {
|
|
13925
13938
|
let out = b || "/";
|
|
13926
13939
|
if (!out.startsWith("/"))
|
|
@@ -13980,7 +13993,7 @@ var CaddyInstant = class {
|
|
|
13980
13993
|
});
|
|
13981
13994
|
const originalExit = import_node_process4.default.exit.bind(import_node_process4.default);
|
|
13982
13995
|
import_node_process4.default.exit = async (code) => {
|
|
13983
|
-
const port = Number(source.
|
|
13996
|
+
const port = Number(splitHostPort(source).port);
|
|
13984
13997
|
if (!Number.isNaN(port) && await tryPort(port))
|
|
13985
13998
|
await kill(port);
|
|
13986
13999
|
const pwd = import_node_process4.default.cwd();
|
|
@@ -13988,6 +14001,10 @@ var CaddyInstant = class {
|
|
|
13988
14001
|
if ((0, import_node_fs3.existsSync)(viteCacheDir))
|
|
13989
14002
|
await chmodRecursive(viteCacheDir, 511).catch(() => {
|
|
13990
14003
|
});
|
|
14004
|
+
const viteTempCacheDir = `${pwd}/node_modules/.vite-temp`;
|
|
14005
|
+
if ((0, import_node_fs3.existsSync)(viteTempCacheDir))
|
|
14006
|
+
await chmodRecursive(viteTempCacheDir, 511).catch(() => {
|
|
14007
|
+
});
|
|
13991
14008
|
const nuxtCacheDir = `${pwd}/.nuxt`;
|
|
13992
14009
|
if ((0, import_node_fs3.existsSync)(nuxtCacheDir))
|
|
13993
14010
|
await chmodRecursive(nuxtCacheDir, 511).catch(() => {
|
|
@@ -14059,7 +14076,7 @@ ${line}
|
|
|
14059
14076
|
this._stopEffects.forEach((fn) => fn());
|
|
14060
14077
|
try {
|
|
14061
14078
|
if (restore && this._source && this._target) {
|
|
14062
|
-
if (await removeHost(this._source.
|
|
14079
|
+
if (await removeHost(splitHostPort(this._source).host, splitHostPort(this._target).host))
|
|
14063
14080
|
consola.success("Restore host success.\n");
|
|
14064
14081
|
else
|
|
14065
14082
|
consola.fail("Restore host failed.\n");
|
|
@@ -14087,9 +14104,238 @@ ${line}
|
|
|
14087
14104
|
}
|
|
14088
14105
|
};
|
|
14089
14106
|
|
|
14107
|
+
// src/hmr-sse-bridge.ts
|
|
14108
|
+
var SSE_PATH = "/__hmr_sse";
|
|
14109
|
+
var SEND_PATH = "/__hmr_send";
|
|
14110
|
+
var MAX_POST_BODY = 64 * 1024;
|
|
14111
|
+
function setupHmrSseBridge(server) {
|
|
14112
|
+
const sseClients = /* @__PURE__ */ new Set();
|
|
14113
|
+
const originalSend = server.ws.send.bind(server.ws);
|
|
14114
|
+
server.ws.send = (...args2) => {
|
|
14115
|
+
;
|
|
14116
|
+
originalSend(...args2);
|
|
14117
|
+
let payload;
|
|
14118
|
+
if (typeof args2[0] === "string") {
|
|
14119
|
+
payload = JSON.stringify({ type: "custom", event: args2[0], data: args2[1] });
|
|
14120
|
+
} else {
|
|
14121
|
+
payload = JSON.stringify(args2[0]);
|
|
14122
|
+
}
|
|
14123
|
+
for (const res of sseClients) {
|
|
14124
|
+
try {
|
|
14125
|
+
res.write(`data: ${payload}
|
|
14126
|
+
|
|
14127
|
+
`);
|
|
14128
|
+
} catch {
|
|
14129
|
+
sseClients.delete(res);
|
|
14130
|
+
}
|
|
14131
|
+
}
|
|
14132
|
+
};
|
|
14133
|
+
server.middlewares.use((req, res, next2) => {
|
|
14134
|
+
var _a4;
|
|
14135
|
+
const url2 = (_a4 = req.url) == null ? void 0 : _a4.split("?")[0];
|
|
14136
|
+
if (url2 === SSE_PATH && req.method === "GET") {
|
|
14137
|
+
handleSseConnection(req, res, sseClients);
|
|
14138
|
+
return;
|
|
14139
|
+
}
|
|
14140
|
+
if (url2 === SEND_PATH && req.method === "POST") {
|
|
14141
|
+
handleClientMessage(req, res, server);
|
|
14142
|
+
return;
|
|
14143
|
+
}
|
|
14144
|
+
next2();
|
|
14145
|
+
});
|
|
14146
|
+
}
|
|
14147
|
+
function handleSseConnection(_req, res, sseClients) {
|
|
14148
|
+
res.writeHead(200, {
|
|
14149
|
+
"Content-Type": "text/event-stream",
|
|
14150
|
+
"Cache-Control": "no-cache",
|
|
14151
|
+
"Connection": "keep-alive"
|
|
14152
|
+
});
|
|
14153
|
+
res.write(`data: ${JSON.stringify({ type: "connected" })}
|
|
14154
|
+
|
|
14155
|
+
`);
|
|
14156
|
+
sseClients.add(res);
|
|
14157
|
+
const keepAlive = setInterval(() => {
|
|
14158
|
+
try {
|
|
14159
|
+
res.write(": ping\n\n");
|
|
14160
|
+
} catch {
|
|
14161
|
+
cleanup();
|
|
14162
|
+
}
|
|
14163
|
+
}, 3e4);
|
|
14164
|
+
function cleanup() {
|
|
14165
|
+
clearInterval(keepAlive);
|
|
14166
|
+
sseClients.delete(res);
|
|
14167
|
+
}
|
|
14168
|
+
res.on("close", cleanup);
|
|
14169
|
+
res.on("error", cleanup);
|
|
14170
|
+
}
|
|
14171
|
+
function handleClientMessage(req, res, server) {
|
|
14172
|
+
let body = "";
|
|
14173
|
+
let overflow = false;
|
|
14174
|
+
req.on("data", (chunk) => {
|
|
14175
|
+
body += chunk.toString();
|
|
14176
|
+
if (body.length > MAX_POST_BODY) {
|
|
14177
|
+
overflow = true;
|
|
14178
|
+
req.destroy();
|
|
14179
|
+
}
|
|
14180
|
+
});
|
|
14181
|
+
req.on("end", () => {
|
|
14182
|
+
var _a4, _b3, _c2, _d2, _e;
|
|
14183
|
+
if (overflow) {
|
|
14184
|
+
res.writeHead(413);
|
|
14185
|
+
res.end('{"ok":false}');
|
|
14186
|
+
return;
|
|
14187
|
+
}
|
|
14188
|
+
try {
|
|
14189
|
+
const data = JSON.parse(body);
|
|
14190
|
+
if (data.type === "custom" && data.event) {
|
|
14191
|
+
const dummyClient = {
|
|
14192
|
+
send: (...args2) => {
|
|
14193
|
+
server.ws.send(...args2);
|
|
14194
|
+
}
|
|
14195
|
+
};
|
|
14196
|
+
(_e = (_d2 = (_c2 = (_b3 = (_a4 = server.environments) == null ? void 0 : _a4.client) == null ? void 0 : _b3.hot) == null ? void 0 : _c2.api) == null ? void 0 : _d2.emit) == null ? void 0 : _e.call(_d2, data.event, data.data, dummyClient);
|
|
14197
|
+
}
|
|
14198
|
+
res.writeHead(200, {
|
|
14199
|
+
"Content-Type": "application/json"
|
|
14200
|
+
});
|
|
14201
|
+
res.end('{"ok":true}');
|
|
14202
|
+
} catch {
|
|
14203
|
+
res.writeHead(400);
|
|
14204
|
+
res.end('{"ok":false}');
|
|
14205
|
+
}
|
|
14206
|
+
});
|
|
14207
|
+
}
|
|
14208
|
+
var hmrSseBridgeClientScript = `
|
|
14209
|
+
(function() {
|
|
14210
|
+
// Activate when served over HTTPS to bypass WSS self-signed cert issues
|
|
14211
|
+
if (location.protocol !== 'https:') return;
|
|
14212
|
+
|
|
14213
|
+
var OriginalWebSocket = window.WebSocket;
|
|
14214
|
+
|
|
14215
|
+
function SseWebSocket(originalUrl) {
|
|
14216
|
+
var self = this;
|
|
14217
|
+
// WebSocket readyState constants on instance
|
|
14218
|
+
self.CONNECTING = 0;
|
|
14219
|
+
self.OPEN = 1;
|
|
14220
|
+
self.CLOSING = 2;
|
|
14221
|
+
self.CLOSED = 3;
|
|
14222
|
+
self.readyState = 0;
|
|
14223
|
+
self.onopen = null;
|
|
14224
|
+
self.onmessage = null;
|
|
14225
|
+
self.onclose = null;
|
|
14226
|
+
self.onerror = null;
|
|
14227
|
+
self.bufferedAmount = 0;
|
|
14228
|
+
self.extensions = '';
|
|
14229
|
+
self.binaryType = 'blob';
|
|
14230
|
+
self.protocol = 'vite-hmr';
|
|
14231
|
+
self.url = originalUrl;
|
|
14232
|
+
self._listeners = {};
|
|
14233
|
+
self._opened = false;
|
|
14234
|
+
|
|
14235
|
+
var sseUrl = location.origin + '${SSE_PATH}';
|
|
14236
|
+
var sendUrl = location.origin + '${SEND_PATH}';
|
|
14237
|
+
self._sendUrl = sendUrl;
|
|
14238
|
+
|
|
14239
|
+
var es = new EventSource(sseUrl);
|
|
14240
|
+
self._es = es;
|
|
14241
|
+
|
|
14242
|
+
es.onopen = function() {
|
|
14243
|
+
if (self._opened) return;
|
|
14244
|
+
self._opened = true;
|
|
14245
|
+
self.readyState = 1;
|
|
14246
|
+
var evt = new Event('open');
|
|
14247
|
+
if (self.onopen) self.onopen(evt);
|
|
14248
|
+
self._emit('open', evt);
|
|
14249
|
+
};
|
|
14250
|
+
|
|
14251
|
+
es.onmessage = function(e) {
|
|
14252
|
+
var evt = new MessageEvent('message', { data: e.data });
|
|
14253
|
+
if (self.onmessage) self.onmessage(evt);
|
|
14254
|
+
self._emit('message', evt);
|
|
14255
|
+
};
|
|
14256
|
+
|
|
14257
|
+
es.onerror = function() {
|
|
14258
|
+
// EventSource auto-reconnects on error; don't treat as fatal close
|
|
14259
|
+
// Only emit error event, never close event (SSE handles reconnect internally)
|
|
14260
|
+
if (self.readyState >= 2) return;
|
|
14261
|
+
// EventSource auto-reconnects; no action needed
|
|
14262
|
+
};
|
|
14263
|
+
}
|
|
14264
|
+
|
|
14265
|
+
SseWebSocket.prototype.send = function(data) {
|
|
14266
|
+
if (this.readyState !== 1) return;
|
|
14267
|
+
fetch(this._sendUrl, {
|
|
14268
|
+
method: 'POST',
|
|
14269
|
+
headers: { 'Content-Type': 'application/json' },
|
|
14270
|
+
body: data
|
|
14271
|
+
}).catch(function() {});
|
|
14272
|
+
};
|
|
14273
|
+
|
|
14274
|
+
SseWebSocket.prototype.close = function() {
|
|
14275
|
+
if (this.readyState >= 2) return;
|
|
14276
|
+
this.readyState = 3;
|
|
14277
|
+
if (this._es) this._es.close();
|
|
14278
|
+
var evt = new CloseEvent('close', { code: 1000, reason: '', wasClean: true });
|
|
14279
|
+
if (this.onclose) this.onclose(evt);
|
|
14280
|
+
this._emit('close', evt);
|
|
14281
|
+
};
|
|
14282
|
+
|
|
14283
|
+
SseWebSocket.prototype.addEventListener = function(type, fn, options) {
|
|
14284
|
+
if (!this._listeners[type]) this._listeners[type] = [];
|
|
14285
|
+
var entry = { fn: fn, once: !!(options && options.once) };
|
|
14286
|
+
this._listeners[type].push(entry);
|
|
14287
|
+
};
|
|
14288
|
+
|
|
14289
|
+
SseWebSocket.prototype.removeEventListener = function(type, fn) {
|
|
14290
|
+
var list = this._listeners[type];
|
|
14291
|
+
if (!list) return;
|
|
14292
|
+
this._listeners[type] = list.filter(function(e) { return e.fn !== fn; });
|
|
14293
|
+
};
|
|
14294
|
+
|
|
14295
|
+
SseWebSocket.prototype.dispatchEvent = function(evt) {
|
|
14296
|
+
this._emit(evt.type, evt);
|
|
14297
|
+
return true;
|
|
14298
|
+
};
|
|
14299
|
+
|
|
14300
|
+
SseWebSocket.prototype._emit = function(type, evt) {
|
|
14301
|
+
var list = this._listeners[type];
|
|
14302
|
+
if (!list) return;
|
|
14303
|
+
var remaining = [];
|
|
14304
|
+
list.forEach(function(entry) {
|
|
14305
|
+
entry.fn(evt);
|
|
14306
|
+
if (!entry.once) remaining.push(entry);
|
|
14307
|
+
});
|
|
14308
|
+
this._listeners[type] = remaining;
|
|
14309
|
+
};
|
|
14310
|
+
|
|
14311
|
+
function PatchedWebSocket(url, protocols) {
|
|
14312
|
+
// Only intercept Vite HMR connections (protocol 'vite-hmr')
|
|
14313
|
+
var isViteHmr = protocols === 'vite-hmr'
|
|
14314
|
+
|| (Array.isArray(protocols) && protocols.indexOf('vite-hmr') !== -1);
|
|
14315
|
+
if (!isViteHmr) {
|
|
14316
|
+
if (protocols !== undefined) {
|
|
14317
|
+
return new OriginalWebSocket(url, protocols);
|
|
14318
|
+
}
|
|
14319
|
+
return new OriginalWebSocket(url);
|
|
14320
|
+
}
|
|
14321
|
+
return new SseWebSocket(url);
|
|
14322
|
+
}
|
|
14323
|
+
|
|
14324
|
+
PatchedWebSocket.CONNECTING = 0;
|
|
14325
|
+
PatchedWebSocket.OPEN = 1;
|
|
14326
|
+
PatchedWebSocket.CLOSING = 2;
|
|
14327
|
+
PatchedWebSocket.CLOSED = 3;
|
|
14328
|
+
PatchedWebSocket.prototype = Object.create(OriginalWebSocket.prototype);
|
|
14329
|
+
PatchedWebSocket.prototype.constructor = PatchedWebSocket;
|
|
14330
|
+
|
|
14331
|
+
window.WebSocket = PatchedWebSocket;
|
|
14332
|
+
})();
|
|
14333
|
+
`;
|
|
14334
|
+
|
|
14090
14335
|
// src/index.ts
|
|
14091
14336
|
var config;
|
|
14092
14337
|
var caddy;
|
|
14338
|
+
var hmrSseBridgeActive = false;
|
|
14093
14339
|
var cwd = import_node_process5.default.cwd();
|
|
14094
14340
|
function colorUrl(url2) {
|
|
14095
14341
|
return import_picocolors2.default.green(url2.replace(/:(\d+)\//, (_, port) => `:${import_picocolors2.default.bold(port)}/`));
|
|
@@ -14176,6 +14422,20 @@ var unpluginFactory = (options) => ({
|
|
|
14176
14422
|
}
|
|
14177
14423
|
const base = server.config.base || "/";
|
|
14178
14424
|
server.printUrls = () => vitePrintUrls(options, source, target, base, _printUrls);
|
|
14425
|
+
if (options.https) {
|
|
14426
|
+
setupHmrSseBridge(server);
|
|
14427
|
+
hmrSseBridgeActive = true;
|
|
14428
|
+
consola.info("HMR SSE bridge enabled for iOS Safari WSS workaround");
|
|
14429
|
+
}
|
|
14430
|
+
},
|
|
14431
|
+
transformIndexHtml() {
|
|
14432
|
+
if (!hmrSseBridgeActive)
|
|
14433
|
+
return [];
|
|
14434
|
+
return [{
|
|
14435
|
+
tag: "script",
|
|
14436
|
+
children: hmrSseBridgeClientScript,
|
|
14437
|
+
injectTo: "head-prepend"
|
|
14438
|
+
}];
|
|
14179
14439
|
}
|
|
14180
14440
|
},
|
|
14181
14441
|
webpack(compiler) {
|