hackpro-sdk
Version:
60 lines • 2.73 kB
JavaScript
;
/*
* Copyright 2018 balena.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const unzip = require("unzip-stream");
const constants_1 = require("./constants");
exports.getFileStreamFromZipStream = (zipStream, match) => __awaiter(void 0, void 0, void 0, function* () {
return yield new Promise((resolve, reject) => {
let found = false;
zipStream.on('error', reject);
const unzipper = unzip.Parse();
unzipper.on('error', reject);
zipStream.pipe(unzipper);
unzipper.on('entry', (entry) => {
if (!found && entry.type === 'File' && match(entry.path)) {
found = true;
// The compressed size is only known if the size is known
if (entry.size !== undefined) {
entry.compressedSize = unzipper.unzipStream.parsedEntity.compressedSize;
}
entry.on('end', () => {
// Stop reading the zip archive once the file we want has been extracted.
zipStream.unpipe(unzipper);
});
resolve(entry);
}
else {
entry.autodrain();
}
});
zipStream.on('end', () => {
if (!found) {
reject(new Error(constants_1.NO_MATCHING_FILE_MSG));
}
});
});
});
//# sourceMappingURL=zip.js.map