const unzip = require("unzip-stream"); /** * Convert a snake-cased string to a camel-cased one. * @param {string} str Original string. * @returns {string} Transformed string. */ const snakeToCamelCase = str => str.replace(/([-_][a-z])/gu, group => group.toUpperCase().replace("-", "").replace("_", "")); exports.snakeToCamelCase = snakeToCamelCase; /** * Check if a value is a JS object. * @param {*} value Value to check. * @returns {boolean} Whether `value` is a JS object. */ const isObject = value => value !== null && typeof value === "object"; exports.isObject = isObject; /** * Check if two arrays are equal in a shallow manner. * @param {Array} array1 First array. * @param {Array} array2 Second array. * @returns {boolean} Whether the two arrays are equal. */ const arraysEqual = (array1, array2) => ( array1.length === array2.length && array1.every((elt1, index) => elt1 === array2[index]) ); exports.arraysEqual = arraysEqual; /** * Find a file in a zipped stream and unzip it. * @param {stream.Readable} data Input zipped stream. * @param {string} fileName Name of the file to find. * @returns {Promise.} Stream of the unzipped file. */ const unzipFile = (data, fileName) => new Promise((res, rej) => { // eslint-disable-next-line new-cap const stream = data.pipe(unzip.Parse()); let found = false; stream.on("entry", entry => { if (entry.type !== "File" || entry.path !== fileName) { entry.autodrain(); return; } found = true; res(entry); }); stream.on("end", () => { if (!found) { rej(new Error(`File ${fileName} not found in archive`)); } }); stream.on("error", err => rej(err)); }); exports.unzipFile = unzipFile;