mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2026-08-25 14:08:31 +01:00
[tools] Removes the second dev server connection (#87727)
## About The Pull Request This  Apparently this was connecting http for ie8 support, which we dropped a long time ago in #79974. I added some early returns to the code as well ## Why It's Good For The Game Code cleanup ## Changelog n/a
This commit is contained in:
@@ -8,112 +8,109 @@ let socket;
|
||||
const queue = [];
|
||||
const subscribers = [];
|
||||
|
||||
const ensureConnection = () => {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
if (!window.WebSocket) {
|
||||
return;
|
||||
}
|
||||
if (!socket || socket.readyState === WebSocket.CLOSED) {
|
||||
const DEV_SERVER_IP = process.env.DEV_SERVER_IP || '127.0.0.1';
|
||||
socket = new WebSocket(`ws://${DEV_SERVER_IP}:3000`);
|
||||
socket.onopen = () => {
|
||||
// Empty the message queue
|
||||
while (queue.length !== 0) {
|
||||
const msg = queue.shift();
|
||||
socket.send(msg);
|
||||
}
|
||||
};
|
||||
socket.onmessage = (event) => {
|
||||
const msg = JSON.parse(event.data);
|
||||
for (let subscriber of subscribers) {
|
||||
subscriber(msg);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
function ensureConnection() {
|
||||
if (process.env.NODE_ENV === 'production') return;
|
||||
|
||||
window.onunload = () => socket && socket.close();
|
||||
};
|
||||
if (socket && socket.readyState !== WebSocket.CLOSED) return;
|
||||
|
||||
if (!window.WebSocket) return;
|
||||
|
||||
const DEV_SERVER_IP = process.env.DEV_SERVER_IP || '127.0.0.1';
|
||||
|
||||
socket = new WebSocket(`ws://${DEV_SERVER_IP}:3000`);
|
||||
|
||||
socket.onopen = () => {
|
||||
// Empty the message queue
|
||||
while (queue.length !== 0) {
|
||||
const msg = queue.shift();
|
||||
socket.send(msg);
|
||||
}
|
||||
};
|
||||
|
||||
socket.onmessage = (event) => {
|
||||
const msg = JSON.parse(event.data);
|
||||
for (let subscriber of subscribers) {
|
||||
subscriber(msg);
|
||||
}
|
||||
};
|
||||
|
||||
window.onunload = () => socket?.close();
|
||||
}
|
||||
|
||||
const subscribe = (fn) => subscribers.push(fn);
|
||||
|
||||
const primitiveReviver = (value) => {
|
||||
if (typeof value === 'number' && !Number.isFinite(value)) {
|
||||
return {
|
||||
__number__: String(value),
|
||||
};
|
||||
}
|
||||
if (typeof value === 'undefined') {
|
||||
return {
|
||||
__undefined__: true,
|
||||
};
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
/**
|
||||
* A json serializer which handles circular references and other junk.
|
||||
*/
|
||||
const serializeObject = (obj) => {
|
||||
let refs = [];
|
||||
const primitiveReviver = (value) => {
|
||||
if (typeof value === 'number' && !Number.isFinite(value)) {
|
||||
|
||||
const objectReviver = (key, value) => {
|
||||
if (typeof value !== 'object') {
|
||||
return primitiveReviver(value);
|
||||
}
|
||||
|
||||
if (value === null) {
|
||||
return value;
|
||||
}
|
||||
// Circular reference
|
||||
if (refs.includes(value)) {
|
||||
return '[circular ref]';
|
||||
}
|
||||
refs.push(value);
|
||||
// Error object
|
||||
const isError =
|
||||
value instanceof Error ||
|
||||
(value.code && value.message && value.message.includes('Error'));
|
||||
if (isError) {
|
||||
return {
|
||||
__number__: String(value),
|
||||
__error__: true,
|
||||
string: String(value),
|
||||
stack: value.stack,
|
||||
};
|
||||
}
|
||||
if (typeof value === 'undefined') {
|
||||
return {
|
||||
__undefined__: true,
|
||||
};
|
||||
// Array
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(primitiveReviver);
|
||||
}
|
||||
return value;
|
||||
};
|
||||
const objectReviver = (key, value) => {
|
||||
if (typeof value === 'object') {
|
||||
if (value === null) {
|
||||
return value;
|
||||
}
|
||||
// Circular reference
|
||||
if (refs.includes(value)) {
|
||||
return '[circular ref]';
|
||||
}
|
||||
refs.push(value);
|
||||
// Error object
|
||||
// prettier-ignore
|
||||
const isError = value instanceof Error || (
|
||||
value.code && value.message && value.message.includes('Error')
|
||||
);
|
||||
if (isError) {
|
||||
return {
|
||||
__error__: true,
|
||||
string: String(value),
|
||||
stack: value.stack,
|
||||
};
|
||||
}
|
||||
// Array
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(primitiveReviver);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
return primitiveReviver(value);
|
||||
};
|
||||
|
||||
const json = JSON.stringify(obj, objectReviver);
|
||||
refs = null;
|
||||
return json;
|
||||
};
|
||||
|
||||
const sendMessage = (msg) => {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
const json = serializeObject(msg);
|
||||
// Send message using WebSocket
|
||||
if (window.WebSocket) {
|
||||
ensureConnection();
|
||||
if (socket.readyState === WebSocket.OPEN) {
|
||||
socket.send(json);
|
||||
} else {
|
||||
// Keep only 100 latest messages in the queue
|
||||
if (queue.length > 100) {
|
||||
queue.shift();
|
||||
}
|
||||
queue.push(json);
|
||||
}
|
||||
}
|
||||
// Send message using plain HTTP request.
|
||||
else {
|
||||
const DEV_SERVER_IP = process.env.DEV_SERVER_IP || '127.0.0.1';
|
||||
const req = new XMLHttpRequest();
|
||||
req.open('POST', `http://${DEV_SERVER_IP}:3001`, true);
|
||||
req.timeout = 250;
|
||||
req.send(json);
|
||||
if (process.env.NODE_ENV === 'production') return;
|
||||
|
||||
const json = serializeObject(msg);
|
||||
// Send message using WebSocket
|
||||
if (!window.WebSocket) return;
|
||||
|
||||
ensureConnection();
|
||||
if (socket.readyState === WebSocket.OPEN) {
|
||||
socket.send(json);
|
||||
} else {
|
||||
// Keep only 100 latest messages in the queue
|
||||
if (queue.length > 100) {
|
||||
queue.shift();
|
||||
}
|
||||
queue.push(json);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -140,33 +137,33 @@ const setupHotReloading = () => {
|
||||
) {
|
||||
return;
|
||||
}
|
||||
if (module.hot) {
|
||||
ensureConnection();
|
||||
sendLogEntry(0, null, 'setting up hot reloading');
|
||||
subscribe((msg) => {
|
||||
const { type } = msg;
|
||||
sendLogEntry(0, null, 'received', type);
|
||||
if (type === 'hotUpdate') {
|
||||
const status = module.hot.status();
|
||||
if (status !== 'idle') {
|
||||
sendLogEntry(0, null, 'hot reload status:', status);
|
||||
return;
|
||||
}
|
||||
module.hot
|
||||
.check({
|
||||
ignoreUnaccepted: true,
|
||||
ignoreDeclined: true,
|
||||
ignoreErrored: true,
|
||||
})
|
||||
.then((modules) => {
|
||||
sendLogEntry(0, null, 'outdated modules', modules);
|
||||
})
|
||||
.catch((err) => {
|
||||
sendLogEntry(0, null, 'reload error', err);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
if (!module.hot) return;
|
||||
|
||||
ensureConnection();
|
||||
sendLogEntry(0, null, 'setting up hot reloading');
|
||||
subscribe(({ type }) => {
|
||||
sendLogEntry(0, null, 'received', type);
|
||||
if (type !== 'hotUpdate') return;
|
||||
|
||||
const status = module.hot.status();
|
||||
if (status !== 'idle') {
|
||||
sendLogEntry(0, null, 'hot reload status:', status);
|
||||
return;
|
||||
}
|
||||
|
||||
module.hot
|
||||
.check({
|
||||
ignoreUnaccepted: true,
|
||||
ignoreDeclined: true,
|
||||
ignoreErrored: true,
|
||||
})
|
||||
.then((modules) => {
|
||||
sendLogEntry(0, null, 'outdated modules', modules);
|
||||
})
|
||||
.catch((err) => {
|
||||
sendLogEntry(0, null, 'reload error', err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
import http from 'http';
|
||||
import { inspect } from 'util';
|
||||
|
||||
import { createLogger, directLog } from '../logging.js';
|
||||
@@ -26,7 +25,6 @@ class LinkServer {
|
||||
logger.log('setting up');
|
||||
this.wss = null;
|
||||
this.setupWebSocketLink();
|
||||
this.setupHttpLink();
|
||||
}
|
||||
|
||||
// WebSocket-based client link
|
||||
@@ -46,29 +44,6 @@ class LinkServer {
|
||||
logger.log(`listening on port ${port} (WebSocket)`);
|
||||
}
|
||||
|
||||
// One way HTTP-based client link for IE8
|
||||
setupHttpLink() {
|
||||
const port = 3001;
|
||||
this.httpServer = http.createServer((req, res) => {
|
||||
if (req.method === 'POST') {
|
||||
let body = '';
|
||||
req.on('data', (chunk) => {
|
||||
body += chunk.toString();
|
||||
});
|
||||
req.on('end', () => {
|
||||
const msg = deserializeObject(body);
|
||||
this.handleLinkMessage(null, msg);
|
||||
res.end();
|
||||
});
|
||||
return;
|
||||
}
|
||||
res.write('Hello');
|
||||
res.end();
|
||||
});
|
||||
this.httpServer.listen(port);
|
||||
logger.log(`listening on port ${port} (HTTP)`);
|
||||
}
|
||||
|
||||
handleLinkMessage(ws, msg) {
|
||||
const { type, payload } = msg;
|
||||
if (type === 'log') {
|
||||
@@ -77,17 +52,20 @@ class LinkServer {
|
||||
if (level <= 0 && !DEBUG) {
|
||||
return;
|
||||
}
|
||||
// prettier-ignore
|
||||
directLog(ns, ...args.map(arg => {
|
||||
if (typeof arg === 'object') {
|
||||
return inspect(arg, {
|
||||
depth: Infinity,
|
||||
colors: true,
|
||||
compact: 8,
|
||||
});
|
||||
}
|
||||
return arg;
|
||||
}));
|
||||
|
||||
directLog(
|
||||
ns,
|
||||
...args.map((arg) => {
|
||||
if (typeof arg === 'object') {
|
||||
return inspect(arg, {
|
||||
depth: Infinity,
|
||||
colors: true,
|
||||
compact: 8,
|
||||
});
|
||||
}
|
||||
return arg;
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (type === 'relay') {
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
const inception = Date.now();
|
||||
|
||||
// Runtime detection
|
||||
const isNode = process && process.release && process.release.name === 'node';
|
||||
const isNode = process?.release?.name === 'node';
|
||||
let isChrome = false;
|
||||
try {
|
||||
isChrome = window.navigator.userAgent.toLowerCase().includes('chrome');
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
"version": "5.0.3",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@types/ws": "^8.5.13",
|
||||
"axios": "^1.7.7",
|
||||
"glob": "^7.2.3",
|
||||
"source-map": "^0.7.4",
|
||||
|
||||
@@ -114,7 +114,7 @@ export const NtosMain = (props) => {
|
||||
(proposed_login.IDName
|
||||
? '(' + proposed_login.IDName + ')'
|
||||
: '')
|
||||
: proposed_login.IDName ?? ''}
|
||||
: (proposed_login.IDName ?? '')}
|
||||
</Table.Row>
|
||||
<Table.Row>
|
||||
Assignment:{' '}
|
||||
@@ -122,7 +122,7 @@ export const NtosMain = (props) => {
|
||||
? login.IDJob +
|
||||
' ' +
|
||||
(proposed_login.IDJob ? '(' + proposed_login.IDJob + ')' : '')
|
||||
: proposed_login.IDJob ?? ''}
|
||||
: (proposed_login.IDJob ?? '')}
|
||||
</Table.Row>
|
||||
</Table>
|
||||
</Section>
|
||||
|
||||
+1407
-1444
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user