Skip to content

Commit

Permalink
feat: 🎸 keep buffer for binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Jul 1, 2024
1 parent 44bdd31 commit e4a30fe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/basics/src/tar-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import writeTo from 'stream-write';
* @name TARExtract
* @param {String} [path="**\/*.json"] Regex to select the files to extract
* @param {String} [json=true] Parse as JSON the content of each file
* @param {Boolean} [text=true] The content of each file is converted to a string (otherwise it remains a buffer)
* @param {Boolean} [compress=false] Enable gzip compression
* @returns {{id: String, value: String}[]}
*/
export default function TARExtract(data, feed) {
const filesPatern = this.getParam('path', '**/*.json');
if (this.isFirst()) {
const { ezs } = this;
const json = this.getParam('json', true);
const text = this.getParam('text', true);
const json = text ? this.getParam('json', true) : false;
const compress = this.getParam('compress', false);
this.input = ezs.createStream(ezs.objectMode());
this.output = ezs.createStream(ezs.objectMode());
Expand All @@ -35,15 +37,24 @@ export default function TARExtract(data, feed) {
extract.on('entry', async (header, stream, next) => {
if (micromatch.isMatch(header.name, filesPatern)) {
try {
const contentRaw = await getStream(stream);
if (json) {
const contentJson = JSON.parse(contentRaw);
const contentText= await getStream(stream);
const contentJson = JSON.parse(contentText);
return writeTo(
this.output,
contentJson,
() => next(),
);
}
if (text) {
const contentText = await getStream(stream);
return writeTo(
this.output,
{ id: header.name, value: contentText },
() => next(),
);
}
const contentRaw = await getStream(stream, { encoding: 'buffer' });
return writeTo(
this.output,
{ id: header.name, value: contentRaw },
Expand Down
16 changes: 16 additions & 0 deletions packages/basics/test/tar-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ describe('TARExtract', () => {
done();
});
});
it('should extract Binary content', (done) => {
const result = [];
fs.createReadStream('./packages/basics/examples/data/test.tar')
.pipe(ezs('TARExtract', { path: '**/*.txt', text: false }))
.pipe(ezs.catch())
.on('data', (chunk) => {
const str = chunk.value.toString();
assert.equal(str, 'ok\n');
result.push(str);
})
.on('error', done)
.on('end', () => {
assert.equal(result.length, 10);
done();
});
});
it('should ignore Wrong JSON files', (done) => {
const result = [];
fs.createReadStream('./packages/basics/examples/data/test2.tar')
Expand Down

0 comments on commit e4a30fe

Please sign in to comment.