]> git.proxmox.com Git - mirror_novnc.git/blobdiff - tests/fake.websocket.js
Fix proper triggering of module fallback
[mirror_novnc.git] / tests / fake.websocket.js
index eb4f203dc5c8a33571ef7921389ee63a5ac53ec9..2e1a3b954b90c8ceef3088bf49d909251f97e454 100644 (file)
@@ -1,43 +1,45 @@
+import Base64 from '../core/base64.js';
+
 // PhantomJS can't create Event objects directly, so we need to use this
 function make_event(name, props) {
-    var evt = document.createEvent('Event');
+    const evt = document.createEvent('Event');
     evt.initEvent(name, true, true);
     if (props) {
-        for (var prop in props) {
+        for (let prop in props) {
             evt[prop] = props[prop];
         }
     }
     return evt;
 }
 
-export default function FakeWebSocket (uri, protocols) {
-    this.url = uri;
-    this.binaryType = "arraybuffer";
-    this.extensions = "";
+export default class FakeWebSocket {
+    constructor(uri, protocols) {
+        this.url = uri;
+        this.binaryType = "arraybuffer";
+        this.extensions = "";
 
-    if (!protocols || typeof protocols === 'string') {
-        this.protocol = protocols;
-    } else {
-        this.protocol = protocols[0];
-    }
+        if (!protocols || typeof protocols === 'string') {
+            this.protocol = protocols;
+        } else {
+            this.protocol = protocols[0];
+        }
 
-    this._send_queue = new Uint8Array(20000);
+        this._send_queue = new Uint8Array(20000);
 
-    this.readyState = FakeWebSocket.CONNECTING;
-    this.bufferedAmount = 0;
+        this.readyState = FakeWebSocket.CONNECTING;
+        this.bufferedAmount = 0;
 
-    this.__is_fake = true;
-};
+        this.__is_fake = true;
+    }
 
-FakeWebSocket.prototype = {
-    close: function (code, reason) {
+    close(code, reason) {
         this.readyState = FakeWebSocket.CLOSED;
         if (this.onclose) {
             this.onclose(make_event("close", { 'code': code, 'reason': reason, 'wasClean': true }));
         }
-    },
+    }
 
-    send: function (data) {
+    send(data) {
         if (this.protocol == 'base64') {
             data = Base64.decode(data);
         } else {
@@ -45,25 +47,25 @@ FakeWebSocket.prototype = {
         }
         this._send_queue.set(data, this.bufferedAmount);
         this.bufferedAmount += data.length;
-    },
+    }
 
-    _get_sent_data: function () {
-        var res = new Uint8Array(this._send_queue.buffer, 0, this.bufferedAmount);
+    _get_sent_data() {
+        const res = new Uint8Array(this._send_queue.buffer, 0, this.bufferedAmount);
         this.bufferedAmount = 0;
         return res;
-    },
+    }
 
-    _open: function (data) {
+    _open() {
         this.readyState = FakeWebSocket.OPEN;
         if (this.onopen) {
             this.onopen(make_event('open'));
         }
-    },
+    }
 
-    _receive_data: function (data) {
+    _receive_data(data) {
         this.onmessage(make_event("message", { 'data': data }));
     }
-};
+}
 
 FakeWebSocket.OPEN = WebSocket.OPEN;
 FakeWebSocket.CONNECTING = WebSocket.CONNECTING;
@@ -72,16 +74,18 @@ FakeWebSocket.CLOSED = WebSocket.CLOSED;
 
 FakeWebSocket.__is_fake = true;
 
-FakeWebSocket.replace = function () {
+FakeWebSocket.replace = () => {
     if (!WebSocket.__is_fake) {
-        var real_version = WebSocket;
+        const real_version = WebSocket;
+        // eslint-disable-next-line no-global-assign
         WebSocket = FakeWebSocket;
         FakeWebSocket.__real_version = real_version;
     }
 };
 
-FakeWebSocket.restore = function () {
+FakeWebSocket.restore = () => {
     if (WebSocket.__is_fake) {
+        // eslint-disable-next-line no-global-assign
         WebSocket = WebSocket.__real_version;
     }
 };