UNPKG

@alinex/datastore

Version:

Read, work and write data structures from and to differents locations and formats.

41 lines 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compress = exports.uncompress = void 0; const path_1 = require("path"); const debug_1 = require("debug"); const JSZip = require("jszip"); const core_1 = require("@alinex/core"); const debug = debug_1.default('datastore:compression:tar'); exports.uncompress = (async function (parsedUri, buffer, options) { debug(`opening ${path_1.basename(parsedUri.pathname)}`); return JSZip.loadAsync(buffer).then(function (zip) { const files = Object.keys(zip.folder('').files); let archivePath = ''; if (files.length == 1) { archivePath = files[0]; parsedUri.hash = `#${archivePath}`; } else { if (!parsedUri.hash) throw new core_1.ExitError('No file specified in hash part of URL.'); archivePath = parsedUri.hash.substr(1); } debug(`extracting ${archivePath}`); return zip.file(archivePath).async('nodebuffer'); }); }); exports.compress = (async function (parsedUri, buffer, options) { debug(`compressing ${path_1.basename(parsedUri.pathname)}`); if (!parsedUri.hash) throw new core_1.ExitError('No file specified in hash part of URL.'); const archivePath = parsedUri.hash.substr(1); const zip = new JSZip(); debug(`including as ${archivePath}`); zip.file(archivePath, buffer); return zip.generateAsync({ type: 'nodebuffer', compression: options && options.compressionLevel ? 'DEFLATE' : 'STORE', compressionOptions: { level: options && options.compressionLevel } }); }); //# sourceMappingURL=zip.js.map