zipster
Version:
TypeScript library built for Node backends to create ZIP files with password protection
137 lines • 5.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const os = require("os");
const uuid = require("uuid");
const guarder_1 = require("guarder");
const Formats_1 = require("./enums/Formats");
const FileParts_1 = require("./libs/FileParts");
const ZipsterError_1 = require("./errors/ZipsterError");
const ArchiverFactory_1 = require("./factories/ArchiverFactory");
/**
* Zipster facilitates the zipping and protecting of data
*/
class Zipster {
/**
* Add a single file to a single ZIP file
*/
static fromPath(path, options) {
const fileParts = new FileParts_1.FileParts(path);
const archiveData = {
name: `${fileParts.getName()}.${fileParts.getExtension()}`
};
const outputLocation = this.getOutputPath(options);
const getSourceBuffer = () => fs.readFileSync(path);
const createZip = (sourceBuffer) => {
const archive = this.getArchiver(options, outputLocation);
archive.append(sourceBuffer, archiveData);
return archive.finalize();
};
const mapSuccess = () => outputLocation;
const tapError = (error) => {
throw new ZipsterError_1.ZipsterError(error.message);
};
return Promise.resolve()
.then(getSourceBuffer)
.then(createZip)
.then(mapSuccess)
.catch(tapError);
}
/**
* Add multiple files to a single ZIP file
*/
static fromPaths(paths, options) {
const outputLocation = this.getOutputPath(options);
const mapToFileParts = () => paths.map((path) => new FileParts_1.FileParts(path));
const appendToZIP = (fileParts) => {
const archive = this.getArchiver(options, outputLocation);
fileParts.forEach((filePart) => {
const sourceBuffer = fs.readFileSync(filePart.getPath());
const archiveData = {
name: `${filePart.getName()}.${filePart.getExtension()}`
};
archive.append(sourceBuffer, archiveData);
});
return archive.finalize();
};
const mapSuccess = () => outputLocation;
const tapError = (error) => {
throw new ZipsterError_1.ZipsterError(error.message);
};
return Promise.resolve()
.then(mapToFileParts)
.then(appendToZIP)
.then(mapSuccess)
.catch(tapError);
}
/**
* Create a ZIP containing all files within specified directory
*/
static fromDirectory(path, options) {
guarder_1.Guarder.empty(path, 'Path is required', ZipsterError_1.ZipsterError);
const outputLocation = this.getOutputPath(options);
const createZip = () => {
const archive = this.getArchiver(options, outputLocation);
archive.directory(path, false);
return archive.finalize();
};
const mapSuccess = () => outputLocation;
const tapError = (error) => {
throw new ZipsterError_1.ZipsterError(error.message);
};
return Promise.resolve()
.then(createZip)
.then(mapSuccess)
.catch(tapError);
}
/**
* Create a ZIP containing files matching the specified pattern at the specified path
*/
static fromPattern(path, pattern, options) {
guarder_1.Guarder.empty(path, 'Path is required', ZipsterError_1.ZipsterError);
guarder_1.Guarder.empty(pattern, 'Pattern is required', ZipsterError_1.ZipsterError);
const outputLocation = this.getOutputPath(options);
const globOptions = {
cwd: path
};
const createZip = () => {
const archive = this.getArchiver(options, outputLocation);
archive.glob(pattern, globOptions);
return archive.finalize();
};
const mapSuccess = () => outputLocation;
const tapError = (error) => {
throw new ZipsterError_1.ZipsterError(error.message);
};
return Promise.resolve()
.then(createZip)
.then(mapSuccess)
.catch(tapError);
}
/**
* Returns the configured archiver
*/
static getArchiver(options, outputLocation) {
const writeStream = fs.createWriteStream(outputLocation);
const archive = ArchiverFactory_1.ArchiverFactory.getArchiver(options);
archive.pipe(writeStream);
return archive;
}
/**
* Returns the output path configured with specified options or defaults
*/
static getOutputPath(options) {
var _a, _b, _c, _d;
const extensionMap = {
[Formats_1.Formats.TAR]: 'tar',
[Formats_1.Formats.ZIP]: 'zip',
[Formats_1.Formats.ZIP_ENCRYPTED]: 'zip'
};
const extension = extensionMap[options.format];
const outputName = (_b = (_a = options.output) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : uuid.v4();
const outputDirectory = (_d = (_c = options.output) === null || _c === void 0 ? void 0 : _c.path) !== null && _d !== void 0 ? _d : os.tmpdir();
return `${outputDirectory}/${outputName}.${extension}`;
}
}
exports.Zipster = Zipster;
//# sourceMappingURL=Zipster.js.map