Skip to content

Commit

Permalink
Merge pull request #420 from Inist-CNRS/tar-extract-buffer
Browse files Browse the repository at this point in the history
Tar extract buffer
  • Loading branch information
touv authored Jul 4, 2024
2 parents 483fb74 + cb879fa commit d2f6095
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 4 deletions.
52 changes: 52 additions & 0 deletions packages/basics/src/file-merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Take `Object` or `Buffer` and throw only one document
*
* ```json
* [ fi1e1.csv, file2.csv ]
* ```
*
* Script:
*
* ```ini
* [use]
* plugin = basics
*
* [FILELoad]
* [FILEMerge]
* [replace]
* path = contentOfFile1AndFile2
* value = self()
*
* ```
*
* Output:
*
* ```json
* [
* (...)
* ]
* ```
*
* @name FILEMerge
* @returns {Object}
*/
export default function FILEMerge(data, feed) {
if (this.isFirst()) {
this.chunks = [];
this.length = 0;
this.isBuffer = Buffer.isBuffer(data);
}
if (this.isLast()) {
feed.write(this.isBuffer ? Buffer.concat(this.chunks, this.length) : this.chunks.join(''));
feed.close();
return;
}

if (this.isBuffer) {
this.length += data.length;
} else {
this.length = this.chunks.length;
}
this.chunks.push(data);
feed.end();
}
2 changes: 2 additions & 0 deletions packages/basics/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import TARDump from './tar-dump';
import INIString from './ini-string';
import FILESave from './file-save';
import FILELoad from './file-load';
import FILEMerge from './file-merge';

const funcs = {
BUFObject,
Expand Down Expand Up @@ -66,6 +67,7 @@ const funcs = {
INIString,
FILESave,
FILELoad,
FILEMerge,
// aliases
bufferify: BUFObject.BUFObject,
concat: TXTConcat.TXTConcat,
Expand Down
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
56 changes: 56 additions & 0 deletions packages/basics/test/file-merge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import fs from 'fs';
import assert from 'assert';
import from from 'from';
import ezs from '../../core/src';
import statements from '../src';
import basics from '../../basics/src';

ezs.addPath(__dirname);

describe('FILEMerge', () => {
test('with existing file (buffer)', (done) => {
ezs.use(statements);
ezs.use(basics);
const input = [
'data01.json',
];
const output = [];
from(input)
.pipe(ezs('FILELoad', { location: __dirname }))
.pipe(ezs('FILEMerge', { location: __dirname }))
.pipe(ezs.catch())
.on('error', done)
.on('data', (chunk) => {
output.push(JSON.parse(String(chunk)));
})
.on('end', () => {
assert.equal(output.length, 1);
assert.equal(output[0][0].id, '9.928');
assert.equal(output[0][0].value, 1);
done();
});
});
test('with existing file (string)', (done) => {
ezs.use(statements);
ezs.use(basics);
const input = [
'data01.json',
];
const output = [];
from(input)
.pipe(ezs('FILELoad', { location: __dirname }))
.pipe(ezs('TXTParse'))
.pipe(ezs('FILEMerge', { location: __dirname }))
.pipe(ezs.catch())
.on('error', done)
.on('data', (chunk) => {
output.push(JSON.parse(chunk));
})
.on('end', () => {
assert.equal(output.length, 1);
assert.equal(output[0][0].id, '9.928');
assert.equal(output[0][0].value, 1);
done();
});
});
});
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
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"directories": {
"test": "test"
},
"gitHead": "bc1be6636627b450c72d59ec404c43d87d7a42aa",
"gitHead": "483fb747db3ae5d72ffeb49dc29fa44fe4dbde84",
"homepage": "https://github.com/Inist-CNRS/ezs/tree/master/packages/core#readme",
"keywords": [
"stream",
Expand Down

0 comments on commit d2f6095

Please sign in to comment.