From 544f7bb080bdbc9d04c4021b10c486884a6f2af3 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kahanskyi Date: Fri, 3 Jun 2016 17:03:35 +0200 Subject: [PATCH] fix(client): don't crash if receive array-like results. fixes #2061 --- client/karma.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/client/karma.js b/client/karma.js index 01d5f1e29..e0d41aad8 100644 --- a/client/karma.js +++ b/client/karma.js @@ -125,17 +125,32 @@ var Karma = function (socket, iframe, opener, navigator, location) { return false } - this.result = function (result) { + this.result = function (originalResult) { + var convertedResult = {} + + // Convert all array-like objects to real arrays. + for (var propertyName in originalResult) { + if (originalResult.hasOwnProperty(propertyName)) { + var propertyValue = originalResult[propertyName] + + if (Object.prototype.toString.call(propertyValue) === '[object Array]') { + convertedResult[propertyName] = Array.prototype.slice.call(propertyValue) + } else { + convertedResult[propertyName] = propertyValue + } + } + } + if (!startEmitted) { socket.emit('start', {total: null}) startEmitted = true } if (resultsBufferLimit === 1) { - return socket.emit('result', result) + return socket.emit('result', convertedResult) } - resultsBuffer.push(result) + resultsBuffer.push(convertedResult) if (resultsBuffer.length === resultsBufferLimit) { socket.emit('result', resultsBuffer)