Adapt config and link library to web

Make the config and link libraries compatible with packing for web with
webpack.  This would fail due to ws not being web compatible and config
using a node function not available in the web shim for util.
This commit is contained in:
Hornwitser committed 2020-09-28 04:19:00 +02:00
1 parent fbf2843f5b
commit f318f8826b
5 files changed
+54 -29

No files matched your search

+4 -4
View File
@@ -2,8 +2,8 @@
"use strict";
const events = require("events");
const util = require("util");
const isDeepStrictEqual = require("../is_deep_strict_equal");
const { basicType } = require("@clusterio/lib/helpers");
@@ -415,7 +415,7 @@ class ConfigGroup {
let prev = this._fields.get(name);
this._fields.set(name, value);
if (this._config && !util.isDeepStrictEqual(value, prev)) {
if (this._config && !isDeepStrictEqual(value, prev)) {
this._config.emit("fieldChanged", this, name, prev);
}
}
@@ -452,7 +452,7 @@ class ConfigGroup {
updated[prop] = value;
this._fields.set(name, updated);
if (this._config && !util.isDeepStrictEqual(updated, prev)) {
if (this._config && !isDeepStrictEqual(updated, prev)) {
this._config.emit("fieldChanged", this, name, prev);
}
}
@@ -502,7 +502,7 @@ class ConfigGroup {
let prev = this._fields.get(name);
this._fields.set(name, value);
if (notify && this._config && !util.isDeepStrictEqual(value, prev)) {
if (notify && this._config && !isDeepStrictEqual(value, prev)) {
this._config.emit("fieldChanged", this, name, prev);
}
}
+7
View File
@@ -0,0 +1,7 @@
"use strict";
// eslint-disable-next-line no-process-env
if (process.env.APP_ENV === "browser") {
module.exports = require("fast-deep-equal/es6");
} else {
module.exports = require("util").isDeepStrictEqual;
}
+25 -25
View File
@@ -1,7 +1,7 @@
// Connection adapters for Links
"use strict";
const events = require("events");
const WebSocket = require("ws");
const WebSocket = require("isomorphic-ws");
const schema = require("@clusterio/lib/schema");
const errors = require("@clusterio/lib/errors");
@@ -236,11 +236,17 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
// Open WebSocket to master
console.log(`SOCKET | connecting to ${url}`);
this._socket = new WebSocket(url, {
// For now we do not verify TLS certificates since the default setup is
// to create a self-signed certificate.
rejectUnauthorized: false,
});
// eslint-disable-next-line no-process-env
if (process.env.APP_ENV === "browser") {
this._socket = new WebSocket(url);
} else {
this._socket = new WebSocket(url, {
// For now we do not verify TLS certificates since the default setup is
// to create a self-signed certificate.
rejectUnauthorized: false,
});
}
this._attachSocketHandlers();
}
@@ -277,11 +283,11 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
}
_attachSocketHandlers() {
this._socket.on("close", (code, reason) => {
console.log(`SOCKET | Close (code: ${code}, reason: ${reason})`);
this._socket.onclose = event => {
console.log(`SOCKET | Close (code: ${event.code}, reason: ${event.reason})`);
// Authentication failed
if (code === 4003) {
this.emit("error", new errors.AuthenticationFailed(reason));
if (event.code === 4003) {
this.emit("error", new errors.AuthenticationFailed(event.reason));
this._state = "closing";
}
@@ -303,26 +309,20 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
}
this.stopHeartbeat();
});
};
this._socket.on("error", err => {
this._socket.onerror = event => {
// It's assumed that close is always called by ws
console.error("SOCKET | Error:", err);
});
console.error("SOCKET | Error:", event);
};
this._socket.on("open", () => {
this._socket.onopen = () => {
console.log("SOCKET | Open");
});
this._socket.on("ping", data => {
console.log(`SOCKET | Ping (data: ${data}`);
});
this._socket.on("pong", data => {
console.log(`SOCKET | Pong (data: ${data}`);
});
};
// Handle messages
this._socket.on("message", data => {
let message = JSON.parse(data);
this._socket.onmessage = event => {
let message = JSON.parse(event.data);
if (this._state === "handshake") {
this._processHandshake(message);
@@ -341,7 +341,7 @@ class WebSocketClientConnector extends WebSocketBaseConnector {
} else {
throw new Error(`Received message in unexpected state ${this._state}`);
}
});
};
}
_processHandshake(message) {
+1
View File
@@ -13,6 +13,7 @@
"dependencies": {
"fs-extra": "^8.1.0",
"ini": "^1.3.5",
"isomorphic-ws": "^4.0.1",
"jimp": "^0.12.1",
"jsonwebtoken": "^8.2.1",
"jszip": "^3.2.2",
+17
View File
@@ -18,6 +18,23 @@ class MockSocket {
this.events.set(event, fn);
}
/* eslint-disable accessor-pairs */
set onclose(fn) {
this.on("close", (code, reason) => fn({ code, reason }));
}
set onerror(fn) {
this.on("error", fn);
}
set onopen(fn) {
this.on("open", fn);
}
set onmessage(fn) {
this.on("message", (data) => fn({ data }));
}
terminate() {
this.terminateCalled = true;
}