securityos/node_modules/next/dist/client/dev/error-overlay/websocket.js

96 lines
3.1 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
0 && (module.exports = {
addMessageListener: null,
sendMessage: null,
connectHMR: null
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
addMessageListener: function() {
return addMessageListener;
},
sendMessage: function() {
return sendMessage;
},
connectHMR: function() {
return connectHMR;
}
});
let source;
const eventCallbacks = [];
function getSocketProtocol(assetPrefix) {
let protocol = location.protocol;
try {
// assetPrefix is a url
protocol = new URL(assetPrefix).protocol;
} catch (e) {}
return protocol === "http:" ? "ws" : "wss";
}
function addMessageListener(callback) {
eventCallbacks.push(callback);
}
function sendMessage(data) {
if (!source || source.readyState !== source.OPEN) return;
return source.send(data);
}
let reconnections = 0;
function connectHMR(options) {
function init() {
if (source) source.close();
function handleOnline() {
reconnections = 0;
window.console.log("[HMR] connected");
}
function handleMessage(event) {
// Coerce into HMR_ACTION_TYPES as that is the format.
const msg = JSON.parse(event.data);
for (const eventCallback of eventCallbacks){
eventCallback(msg);
}
}
let timer;
function handleDisconnect() {
source.onerror = null;
source.onclose = null;
source.close();
reconnections++;
// After 25 reconnects we'll want to reload the page as it indicates the dev server is no longer running.
if (reconnections > 25) {
window.location.reload();
return;
}
clearTimeout(timer);
// Try again after 5 seconds
timer = setTimeout(init, reconnections > 5 ? 5000 : 1000);
}
const { hostname, port } = location;
const protocol = getSocketProtocol(options.assetPrefix || "");
const assetPrefix = options.assetPrefix.replace(/^\/+/, "");
let url = protocol + "://" + hostname + ":" + port + (assetPrefix ? "/" + assetPrefix : "");
if (assetPrefix.startsWith("http")) {
url = protocol + "://" + assetPrefix.split("://")[1];
}
source = new window.WebSocket("" + url + options.path);
source.onopen = handleOnline;
source.onerror = handleDisconnect;
source.onclose = handleDisconnect;
source.onmessage = handleMessage;
}
init();
}
if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
Object.defineProperty(exports.default, '__esModule', { value: true });
Object.assign(exports.default, exports);
module.exports = exports.default;
}
//# sourceMappingURL=websocket.js.map