UNPKG

@ezs/basics

Version:
94 lines (93 loc) 3.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = TARExtract; var _tarStream = _interopRequireDefault(require("tar-stream")); var _micromatch = _interopRequireDefault(require("micromatch")); var _zlib = require("zlib"); var _getStream = _interopRequireDefault(require("get-stream")); var _streamWrite = _interopRequireDefault(require("stream-write")); var _debug = _interopRequireDefault(require("debug")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * Take the content of a tar 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 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}[]} */ function TARExtract(data, feed) { const filesPatern = this.getParam('path', '**/*.json'); if (this.isFirst()) { const { ezs } = this; 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()); const extract = _tarStream.default.extract(); this.whenEnd = new Promise((resolve, reject) => { extract.on('entry', async (header, stream, next) => { if (_micromatch.default.isMatch(header.name, filesPatern)) { try { if (json) { const contentText = await (0, _getStream.default)(stream); const contentJson = JSON.parse(contentText); return (0, _streamWrite.default)(this.output, contentJson, () => next()); } if (text) { const contentText = await (0, _getStream.default)(stream); return (0, _streamWrite.default)(this.output, { id: header.name, value: contentText }, () => next()); } const contentRaw = await (0, _getStream.default)(stream, { encoding: 'buffer' }); return (0, _streamWrite.default)(this.output, { id: header.name, value: contentRaw }, () => next()); } catch (e) { (0, _debug.default)('ezs:warn')(`File was ignored (${header.name})`, ezs.serializeError(e)); stream.resume(); return next(); } } stream.resume(); return next(); }); extract.on('error', reject); extract.on('finish', resolve); }); if (compress) { this.input.pipe((0, _zlib.createGunzip)()).pipe(extract); } else { this.input.pipe(extract); } 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()); } }