From a06abadc822767d43bfb1885dcc7df257f4dce51 Mon Sep 17 00:00:00 2001 From: Ziggy Jonsson Date: Sun, 19 May 2019 09:51:03 -0400 Subject: [PATCH] Add crx to Open Buffer --- README.md | 2 ++ lib/Open/index.js | 18 ++++++++++++++++-- test/openBuffer.js | 10 ++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7d75d38..e350f6b 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ There are no added compiled dependencies - inflation is handled by node.js's bui Please note: Methods that use the Central Directory instead of parsing entire file can be found under [`Open`](#open) +Chrome extension files (.crx) are zipfiles with an [extra header](http://www.adambarth.com/experimental/crx/docs/crx.html) at the start of the file. Unzipper will parse .crx file with the streaming methods (`Parse` and `ParseOne`) and `Open.buffer` method. + ## Installation ```bash diff --git a/lib/Open/index.js b/lib/Open/index.js index a6148f4..0756b8d 100644 --- a/lib/Open/index.js +++ b/lib/Open/index.js @@ -2,13 +2,28 @@ var fs = require('fs'); var Promise = require('bluebird'); var directory = require('./directory'); var Stream = require('stream'); +var binary = require('binary'); // Backwards compatibility for node versions < 8 if (!Stream.Writable || !Stream.Writable.prototype.destroy) Stream = require('readable-stream'); module.exports = { - buffer: function(buffer) { + buffer: function(buffer) { + // slice the buffer to correct start location if crx files + var signature = buffer.readUInt32LE(0); + if (signature === 0x34327243) { + var baseHeaderLength = 4*4; + var h = binary.parse(buffer) + .word32lu('signature') + .word32lu('version') + .word32lu('pubKeyLength') + .word32lu('signatureLength') + .vars; + + buffer = buffer.slice(baseHeaderLength + h.pubKeyLength + h.signatureLength); + } + var source = { stream: function(offset, length) { var stream = Stream.PassThrough(); @@ -92,4 +107,3 @@ module.exports = { return directory(source); } }; - diff --git a/test/openBuffer.js b/test/openBuffer.js index 8ca4f46..ed09a6b 100644 --- a/test/openBuffer.js +++ b/test/openBuffer.js @@ -22,4 +22,14 @@ test("get content of a single file entry out of a buffer", function (t) { t.end(); }); }); +}); + +test('open buffer containing a crx file', function(t) { + var archive = path.join(__dirname, '../testData/compressed-standard-crx/archive.crx'); + var buffer = fs.readFileSync(archive); + return unzip.Open.buffer(buffer) + .then(function(d) { + t.same(d.files[1].path, 'dir/fileInsideDir.txt'); + t.end(); + }); }); \ No newline at end of file