Skip to content

Commit

Permalink
Add crx to Open Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
ZJONSSON committed May 20, 2019
1 parent 2984813 commit a06abad
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions lib/Open/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -92,4 +107,3 @@ module.exports = {
return directory(source);
}
};

10 changes: 10 additions & 0 deletions test/openBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit a06abad

Please sign in to comment.