From f318f8826bb63c9aceab35b4b2ea319f51bf0cf5 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 25 Sep 2020 22:05:14 +0200 Subject: [PATCH] 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. --- packages/lib/config/classes.js | 8 ++--- packages/lib/is_deep_strict_equal.js | 7 ++++ packages/lib/link/connectors.js | 50 ++++++++++++++-------------- packages/lib/package.json | 1 + test/mock.js | 17 ++++++++++ 5 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 packages/lib/is_deep_strict_equal.js diff --git a/packages/lib/config/classes.js b/packages/lib/config/classes.js index 101b8037..1a2cca36 100644 --- a/packages/lib/config/classes.js +++ b/packages/lib/config/classes.js @@ -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); } } diff --git a/packages/lib/is_deep_strict_equal.js b/packages/lib/is_deep_strict_equal.js new file mode 100644 index 00000000..2a66f443 --- /dev/null +++ b/packages/lib/is_deep_strict_equal.js @@ -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; +} diff --git a/packages/lib/link/connectors.js b/packages/lib/link/connectors.js index b18f9844..d3f3fe1f 100644 --- a/packages/lib/link/connectors.js +++ b/packages/lib/link/connectors.js @@ -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) { diff --git a/packages/lib/package.json b/packages/lib/package.json index 6936c27c..290ead6f 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -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", diff --git a/test/mock.js b/test/mock.js index 03e851f9..f24f8708 100644 --- a/test/mock.js +++ b/test/mock.js @@ -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; }