Skip to content

Commit

Permalink
Merge pull request #369 from Inist-CNRS/patch-file-save
Browse files Browse the repository at this point in the history
fix: 🐛 FILESave and direct buffer input
  • Loading branch information
touv authored Aug 24, 2023
2 parents d0f7b4f + d4c14ee commit 73d4a40
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/basics/src/file-save.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ export default function FILESave(data, feed) {
}
if (this.isLast()) {
this.input.end();
return this.whenFinish
this.whenFinish
.then((stats) => feed.write(stats))
.catch((err) => feed.stop(err))
.finally(() => feed.close());
return;
}
const jsonl = Boolean(this.getParam('jsonl', false));
const bufContent = this.getParam('content', data);
const bufFunc = jsonl ? toJSONL : String;
const buf = Buffer.from(bufFunc(bufContent));
const buf = Buffer.isBuffer(bufContent) ? bufContent : Buffer.from(bufFunc(bufContent));
writeTo(this.input, buf, () => feed.end());
}
46 changes: 44 additions & 2 deletions packages/basics/test/file-save.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ describe('FILESave #2', () => {

describe('FILESave #1bis', () => {
const identifier = Date.now();
const filename = `/tmp/${identifier}`;
const filenamegz = `/tmp/${identifier}.gz`;
it('should return stat', (done) => {
const output = [];
Expand Down Expand Up @@ -167,7 +166,7 @@ describe('FILESave (delegated)', () => {
});
});

describe('FILESave #1ter', () => {
describe('FILESave #2', () => {
const identifier = Date.now();
const filenamegz = `/tmp/${identifier}.gz`;
const script = `
Expand Down Expand Up @@ -200,6 +199,49 @@ describe('FILESave #1ter', () => {
});
});

describe('FILESave #2bis', () => {
const identifier = Date.now();
const filenamegz = `/tmp/${identifier}`;
const script = `
[TARDump]
compress = true
[FILESave]
identifier = ${identifier}
location = /tmp
compress = false
[exchange]
value = get('filename')
[FILELoad]
compress = false
location = /tmp
[TARExtract]
path = data/*.json
compress = true
`;

it('should return the same', (done) => {
const output = [];
from(['a'])
.pipe(ezs('delegate', { script }))
.pipe(ezs.catch())
.on('error', done)
.on('data', (chunk) => {
output.push(chunk);
})
.on('end', () => {
expect(output.length).toBe(1);
expect(output[0]).toStrictEqual('a');
fs.unlink(filenamegz, done);
});
});
});




describe('FILESave errors', () => {
test('write to unauthorized files ', (done) => {
Expand Down

0 comments on commit 73d4a40

Please sign in to comment.