-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
180 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import tar from 'tar-stream'; | ||
import { createGzip } from 'zlib'; | ||
import merge from 'lodash.merge'; | ||
|
||
const eol = '\n'; | ||
|
||
/** | ||
* Take all recevied objects and build a tar file | ||
* | ||
* ```json | ||
* { | ||
* } | ||
* ``` | ||
* | ||
* @name TARDump | ||
* @param {String} [manifest] Location path to store files in the tarball | ||
* @param {String} [location=data] Location path to store files in the tarball | ||
* @param {String} [json=true] Parse as JSON the content of each file | ||
* @param {Boolean} [compress=false] Enable gzip compression | ||
*/ | ||
export default function TARDump(data, feed) { | ||
if (!this.pack) { | ||
this.pack = tar.pack(); | ||
const compress = this.getParam('compress', false); | ||
const d = new Date(); | ||
const manifest = merge({ created: d.toUTCString() }, [].concat(this.getParam('manifest', [])).filter(Boolean)); | ||
this.pack.entry({ name: 'manifest.json' }, JSON.stringify(manifest, null, ' ')); | ||
const stream = compress ? this.pack.pipe(createGzip()) : this.pack; | ||
feed.flow(stream).finally(() => feed.close()); | ||
} | ||
if (this.isLast()) { | ||
this.pack.finalize(); | ||
return; | ||
} | ||
const id = this.getIndex().toString().padStart(10, '0'); | ||
const value = JSON.stringify(data).concat(eol); | ||
const location = this.getParam('location', 'data'); | ||
this.pack.entry({ name: `${location}/f${id}.json` }, value); | ||
feed.end(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
const assert = require('assert'); | ||
const from = require('from'); | ||
const ezs = require('../../core/src'); | ||
|
||
ezs.use(require('../src')); | ||
|
||
describe('TARDump', () => { | ||
const input = [ | ||
{ a: 1, b: true, c: 'un' }, | ||
{ a: 2, b: true, c: 'deux' }, | ||
{ a: 3, b: true, c: 'trois' }, | ||
]; | ||
it('should dump and extract few objects (tmpfile)', (done) => { | ||
const result = []; | ||
const script = ` | ||
[TARDump] | ||
[FILESave] | ||
identifier = test-a.tar | ||
location = /tmp | ||
[exchange] | ||
value = get('filename') | ||
[FILELoad] | ||
location = /tmp | ||
[TARExtract] | ||
path = data/*.json | ||
`; | ||
from(input) | ||
.pipe(ezs('delegate', { script })) | ||
.pipe(ezs.catch()) | ||
.on('error', done) | ||
.on('data', (obj) => { | ||
assert.equal(obj.b, true); | ||
result.push(obj); | ||
}) | ||
.on('end', () => { | ||
assert.equal(result.length, 3); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should dump and extract few objects (stream)', (done) => { | ||
const result = []; | ||
const script = ` | ||
[TARDump] | ||
[TARExtract] | ||
path = data/*.json | ||
`; | ||
from(input) | ||
.pipe(ezs('delegate', { script })) | ||
.pipe(ezs.catch()) | ||
.on('error', done) | ||
.on('data', (obj) => { | ||
assert.equal(obj.b, true); | ||
result.push(obj); | ||
}) | ||
.on('end', () => { | ||
assert.equal(result.length, 3); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should dump and extract few objects (stream & custom path)', (done) => { | ||
const result = []; | ||
const script = ` | ||
[TARDump] | ||
location = another/pathname | ||
[TARExtract] | ||
path = another/pathname/*.json | ||
`; | ||
from(input) | ||
.pipe(ezs('delegate', { script })) | ||
.pipe(ezs.catch()) | ||
.on('error', done) | ||
.on('data', (obj) => { | ||
assert.equal(obj.b, true); | ||
result.push(obj); | ||
}) | ||
.on('end', () => { | ||
assert.equal(result.length, 3); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should dump and extract few objects (stream & compress)', (done) => { | ||
const result = []; | ||
const script = ` | ||
[TARDump] | ||
compress = true | ||
[TARExtract] | ||
compress = true | ||
path = data/*.json | ||
`; | ||
from(input) | ||
.pipe(ezs('delegate', { script })) | ||
.pipe(ezs.catch()) | ||
.on('error', done) | ||
.on('data', (obj) => { | ||
assert.equal(obj.b, true); | ||
result.push(obj); | ||
}) | ||
.on('end', () => { | ||
assert.equal(result.length, 3); | ||
done(); | ||
}); | ||
}); | ||
|
||
|
||
|
||
}); |