@ezs/basics
Version:
Basics statements for EZS
54 lines (53 loc) • 1.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = ZIPExtract;
var _unzipper = _interopRequireDefault(require("unzipper"));
var _micromatch = _interopRequireDefault(require("micromatch"));
var _streamWrite = _interopRequireDefault(require("stream-write"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* Take the content of a zip file, extract some files.
* The JSON object is sent to the output stream for each file.
* It returns to the output stream
*
* ```json
* {
* "id": "file name",
* "value": "file contents"
* }
* ```
*
* @name ZIPExtract
* @param {String} [path="**\/*.json"] Regex to select the files to extract
* @returns {{id: String, value: String}[]}
*/
function ZIPExtract(data, feed) {
const filesPatern = this.getParam('path', '**/*.json');
if (this.isFirst()) {
const {
ezs
} = this;
this.input = ezs.createStream(ezs.objectMode());
this.output = ezs.createStream(ezs.objectMode());
this.whenEnd = this.input.pipe(_unzipper.default.Parse()).on('entry', async entry => {
if (_micromatch.default.isMatch(entry.path, filesPatern)) {
const content = await entry.buffer();
return (0, _streamWrite.default)(this.output, {
id: entry.path,
value: content
}, () => entry.autodrain());
}
return entry.autodrain();
}).promise();
this.whenFinish = feed.flow(this.output);
}
if (this.isLast()) {
this.whenEnd.finally(() => this.output.end());
this.whenFinish.finally(() => feed.close());
this.input.end();
} else {
(0, _streamWrite.default)(this.input, data, () => feed.end());
}
}