@forzalabs/remora
Version:
A powerful CLI tool for seamless data translation.
102 lines (101 loc) • 5.26 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const decompress_1 = __importDefault(require("decompress"));
const zlib_1 = __importDefault(require("zlib"));
const path_1 = __importDefault(require("path"));
const promises_1 = require("stream/promises");
class ParseCompressionClass {
constructor() {
this.decompressToFile = (compressionType, fileKey, standardPath, executionPath) => __awaiter(this, void 0, void 0, function* () {
// Ensure the directory for the file exists
const fileDir = path_1.default.dirname(executionPath);
if (!fs.existsSync(fileDir)) {
fs.mkdirSync(fileDir, { recursive: true });
}
switch (compressionType === null || compressionType === void 0 ? void 0 : compressionType.toUpperCase()) {
case 'ZIP':
case 'TAR': {
this._warningPossibleConvertionError(compressionType, fileKey);
yield (0, decompress_1.default)(path_1.default.join(standardPath, fileKey), executionPath);
return executionPath;
}
case 'GZ': {
this._warningPossibleConvertionError(compressionType, fileKey);
yield this._gzipFile(fileKey, standardPath, executionPath);
return executionPath;
}
case null:
case undefined: {
if (fileKey.includes('.zip') || fileKey.includes('.tar') || fileKey.includes('.gz'))
console.log(`The file ${fileKey} seems to use a compressionType while there are no producer settings for compressionType `);
// No decompression needed for request
return path_1.default.join(standardPath, fileKey);
}
default: {
// throw an error if the request ask for an unsupported compression type
throw new Error(`this compression type it has not been implemented ${compressionType}`);
}
}
});
this._warningPossibleConvertionError = (compression, fileKey) => {
if (!fileKey.split('.').includes(compression.toLowerCase()) && compression) {
console.warn(`The compressionType ${compression.toUpperCase()} seems different to the file compression of ${fileKey}`);
}
};
this._gzipFile = (fileKey, standardPath, localPath) => __awaiter(this, void 0, void 0, function* () {
const fileContents = fs.createReadStream(path_1.default.join(standardPath, fileKey));
const unzip = zlib_1.default.createGunzip();
const writeStream = fs.createWriteStream(localPath);
yield (0, promises_1.pipeline)(fileContents, unzip, writeStream);
});
// TODO add a decompression method fo tarball (.tar.gz)
}
}
const ParseCompression = new ParseCompressionClass();
exports.default = ParseCompression;