From c5a9012b09d8e32217619362134e8a6871c307b0 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sat, 15 Aug 2015 11:11:02 -0400 Subject: [PATCH] fix call stack exceeded in #69 --- index.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 86ef1a3..ec7b75d 100644 --- a/index.js +++ b/index.js @@ -622,6 +622,28 @@ function base64Slice (buf, start, end) { } } +function decodeLargeCodePointsArray (array) { + var res = '' + var i + var end = array.length + + for (i = 0; i < end; i++) { + res += String.fromCharCode(array[i]) + } + + return res +} + +function decodeCodePointsArray (array) { + if (array.length < 0x10000) { + return String.fromCharCode.apply(String, array) + } + // Decode using string concatenation to avoid "call stack size exceeded". + // Based on http://stackoverflow.com/a/22747272/680742, the browser with + // the lowest limit is Chrome, with 0x10000 args. + return decodeLargeCodePointsArray(array) +} + function utf8Slice (buf, start, end) { end = Math.min(buf.length, end) var firstByte @@ -700,7 +722,7 @@ function utf8Slice (buf, start, end) { res.push(codePoint) } - return String.fromCharCode.apply(String, res) + return decodeCodePointsArray(res) } function asciiSlice (buf, start, end) { -- 2.34.1