]> git.proxmox.com Git - mirror_novnc.git/blobdiff - core/websock.js
Avoid big strings on the stack
[mirror_novnc.git] / core / websock.js
index f07a7be0429d6118d149ca61b9b4dd4b03432ce5..6b0336157bb17ca7f806dbf999abd440bb654e9f 100644 (file)
@@ -46,21 +46,6 @@ const ENABLE_COPYWITHIN = false;
 
 const MAX_RQ_GROW_SIZE = 40 * 1024 * 1024;  // 40 MiB
 
-const typedArrayToString = (function () {
-    // This is only for PhantomJS, which doesn't like apply-ing
-    // with Typed Arrays
-    try {
-        const arr = new Uint8Array([1, 2, 3]);
-        String.fromCharCode.apply(null, arr);
-        return function (a) { return String.fromCharCode.apply(null, a); };
-    } catch (ex) {
-        return function (a) {
-            return String.fromCharCode.apply(
-                null, Array.prototype.slice.call(a));
-        };
-    }
-})();
-
 Websock.prototype = {
     // Getters and Setters
     get_sQ: function () {
@@ -115,9 +100,14 @@ Websock.prototype = {
 
     rQshiftStr: function (len) {
         if (typeof(len) === 'undefined') { len = this.rQlen(); }
-        const arr = new Uint8Array(this._rQ.buffer, this._rQi, len);
-        this._rQi += len;
-        return typedArrayToString(arr);
+        const arr = this.rQshiftBytes(len);
+        let str = "";
+        // Handle large arrays in steps to avoid long strings on the stack
+        for (let i = 0; i < len; i += 4096) {
+            let part = arr.slice(i, i + Math.min(4096, len));
+            str = str.concat(String.fromCharCode.apply(null, part));
+        }
+        return str;
     },
 
     rQshiftBytes: function (len) {