UNPKG

@ezs/basics

Version:
64 lines (63 loc) 1.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = FILELoad; var _zlib = require("zlib"); var _fs = require("fs"); var _path = require("path"); var _os = require("os"); var _higherPath = _interopRequireDefault(require("higher-path")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } /** * Take `Object` containing filename et throw content by chunk * * ```json * [ fi1e1.csv, file2.csv ] * ``` * * Script: * * ```ini * [use] * plugin = analytics * plugin = basics * * [FILELoad] * location = /tmp * [CSVParse] * * ``` * * Output: * * ```json * [ * (...) * ] * ``` * * @name FILELoad * @param {String} [location=TMPDIR] Directory location * @param {Boolean} [compress=false] Enable gzip compression * @returns {Object} */ async function FILELoad(data, feed) { if (this.isLast()) { feed.close(); return; } const compress = this.getParam('compress', false); const location = (0, _path.normalize)(this.getParam('location', '/')); const locations = [(0, _higherPath.default)((0, _os.tmpdir)(), location), (0, _higherPath.default)(process.cwd(), location)]; const filename = locations.filter(Boolean).map(dir => (0, _path.resolve)(dir, String(data).trim())).filter(fil => (0, _fs.existsSync)(fil)).shift(); if (!filename) { feed.stop(new Error('File location check failed.')); return; } (0, _fs.accessSync)((0, _path.dirname)(filename), _fs.constants.R_OK | _fs.constants.W_OK); (0, _fs.accessSync)(filename, _fs.constants.R_OK | _fs.constants.W_OK); const stream = compress ? (0, _fs.createReadStream)(filename).pipe((0, _zlib.createGunzip)()) : (0, _fs.createReadStream)(filename); await feed.flow(stream); return; }