UNPKG

4.08 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.ZipExploded = void 0;
4var tslib_1 = require("tslib");
5var debug_ = require("debug");
6var fs = require("fs");
7var path = require("path");
8var zip_1 = require("./zip");
9var debug = debug_("r2:utils#zip/zip-ex");
10var ZipExploded = (function (_super) {
11 tslib_1.__extends(ZipExploded, _super);
12 function ZipExploded(dirPath) {
13 var _this = _super.call(this) || this;
14 _this.dirPath = dirPath;
15 return _this;
16 }
17 ZipExploded.loadPromise = function (dirPath) {
18 return tslib_1.__awaiter(this, void 0, void 0, function () {
19 return tslib_1.__generator(this, function (_a) {
20 return [2, Promise.resolve(new ZipExploded(dirPath))];
21 });
22 });
23 };
24 ZipExploded.prototype.freeDestroy = function () {
25 debug("freeDestroy: ZipExploded -- " + this.dirPath);
26 };
27 ZipExploded.prototype.entriesCount = function () {
28 return 0;
29 };
30 ZipExploded.prototype.hasEntries = function () {
31 return true;
32 };
33 ZipExploded.prototype.hasEntry = function (entryPath) {
34 return this.hasEntries()
35 && fs.existsSync(path.join(this.dirPath, entryPath));
36 };
37 ZipExploded.prototype.getEntries = function () {
38 return tslib_1.__awaiter(this, void 0, void 0, function () {
39 var _this = this;
40 return tslib_1.__generator(this, function (_a) {
41 return [2, new Promise(function (resolve, _reject) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
42 var dirPathNormalized, files, adjustedFiles;
43 var _this = this;
44 return tslib_1.__generator(this, function (_a) {
45 dirPathNormalized = fs.realpathSync(this.dirPath);
46 files = fs.readdirSync(this.dirPath, { withFileTypes: true }).
47 filter(function (f) { return f.isFile(); }).map(function (f) { return path.join(_this.dirPath, f.name); });
48 adjustedFiles = files.map(function (file) {
49 var filePathNormalized = fs.realpathSync(file);
50 var relativeFilePath = filePathNormalized.replace(dirPathNormalized, "");
51 if (relativeFilePath.indexOf("/") === 0) {
52 relativeFilePath = relativeFilePath.substr(1);
53 }
54 return relativeFilePath;
55 });
56 resolve(adjustedFiles);
57 return [2];
58 });
59 }); })];
60 });
61 });
62 };
63 ZipExploded.prototype.entryStreamPromise = function (entryPath) {
64 return tslib_1.__awaiter(this, void 0, void 0, function () {
65 var fullPath, stats, streamAndLength;
66 var _this = this;
67 return tslib_1.__generator(this, function (_a) {
68 if (!this.hasEntries() || !this.hasEntry(entryPath)) {
69 return [2, Promise.reject("no such path in zip exploded: " + entryPath)];
70 }
71 fullPath = path.join(this.dirPath, entryPath);
72 stats = fs.lstatSync(fullPath);
73 streamAndLength = {
74 length: stats.size,
75 reset: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
76 return tslib_1.__generator(this, function (_a) {
77 return [2, this.entryStreamPromise(entryPath)];
78 });
79 }); },
80 stream: fs.createReadStream(fullPath, { autoClose: false }),
81 };
82 return [2, Promise.resolve(streamAndLength)];
83 });
84 });
85 };
86 return ZipExploded;
87}(zip_1.Zip));
88exports.ZipExploded = ZipExploded;
89//# sourceMappingURL=zip-ex.js.map
\No newline at end of file