From c8bd0d70ec855eed7dce988fa3a9c672156c6c65 Mon Sep 17 00:00:00 2001 From: Martii Date: Mon, 5 Oct 2015 20:09:57 -0600 Subject: [PATCH] Try merging the Chunks * Works on dev with write/edit, upload, and import... trying on pro for webhook Applies to #678 --- controllers/scriptStorage.js | 43 +++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/controllers/scriptStorage.js b/controllers/scriptStorage.js index a8cb0ac3b..da85ad63f 100644 --- a/controllers/scriptStorage.js +++ b/controllers/scriptStorage.js @@ -331,8 +331,6 @@ exports.getMeta = function (aChunks, aCallback) { // parse the blocks. But strings are memory inefficient compared // to buffers so we only convert the least number of chunks to // get the metadata blocks. - var i = 0; - var str = ''; var parser = null; var rHeaderContent = null; var headerContent = null; @@ -340,33 +338,38 @@ exports.getMeta = function (aChunks, aCallback) { var blocksContent = {}; var blocks = {}; - for (; i < aChunks.length; ++i) { - str += aChunks[i].toString('utf8'); + var buf = Buffer.concat(aChunks); + var str = buf.toString('utf8'); - for (parser in parsers) { - rHeaderContent = new RegExp( - '^(?:\\uFEFF)?\/\/ ==' + parser + '==([\\s\\S]*?)^\/\/ ==\/'+ parser + '==', 'm' - ); - headerContent = rHeaderContent.exec(str); - if (headerContent && headerContent[1]) { - if (parser === 'UserScript') { - hasUserScriptHeaderContent = true; - } + if (isDbg) { + console.log('> getMeta() > str'); + console.log(str); + } - blocksContent[parser] = headerContent[1]; + for (parser in parsers) { + rHeaderContent = new RegExp( + '^(?:\\uFEFF)?\/\/ ==' + parser + '==([\\s\\S]*?)^\/\/ ==\/'+ parser + '==', 'm' + ); + headerContent = rHeaderContent.exec(str); + if (headerContent && headerContent[1]) { + if (parser === 'UserScript') { + hasUserScriptHeaderContent = true; } + + blocksContent[parser] = headerContent[1]; } + } - if (hasUserScriptHeaderContent) { - for (parser in parsers) { - if (blocksContent[parser]) { - blocks[parser] = parseMeta(parsers[parser], blocksContent[parser]); - } + if (hasUserScriptHeaderContent) { + for (parser in parsers) { + if (blocksContent[parser]) { + blocks[parser] = parseMeta(parsers[parser], blocksContent[parser]); } - return aCallback(blocks); } + return aCallback(blocks); } + aCallback(null); };