"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const asar_1 = __importDefault(require("asar")); const buffer_1 = require("./buffer"); const path_1 = __importDefault(require("path")); const promisified_functions_1 = require("./promisified-functions"); const ASAR_EXTNAME = '.asar'; class Asar { constructor() { this._archivePaths = new Set(); } static async _isAsarArchive(archivePath) { try { const stats = await (0, promisified_functions_1.stat)(archivePath); return stats.isFile() && path_1.default.extname(archivePath) === ASAR_EXTNAME; } catch (e) { return false; } } async _findArchivePath(fullPath) { let currentPath = fullPath; let currentDir = path_1.default.dirname(currentPath); while (currentPath !== currentDir) { if (await Asar._isAsarArchive(currentPath)) return currentPath; currentPath = path_1.default.dirname(currentPath); currentDir = path_1.default.dirname(currentPath); } return ''; } parse(fullPath) { for (const archivePath of this._archivePaths) { if (fullPath.startsWith(archivePath)) return { archive: archivePath, fileName: fullPath.substr(archivePath.length + 1) }; } return { archive: '', fileName: '' }; } async isAsar(fullPath) { for (const archivePath of this._archivePaths) { if (fullPath.startsWith(archivePath)) { if (!await Asar._isAsarArchive(archivePath)) { this._archivePaths.delete(archivePath); break; } return true; } } const archivePath = await this._findArchivePath(fullPath); if (archivePath) { this._archivePaths.add(archivePath); return true; } return false; } extractFileToReadStream(archive, fileName) { const extractedFile = asar_1.default.extractFile(archive, fileName); return (0, buffer_1.toReadableStream)(extractedFile); } getFileInAsarNotFoundErrorMessage(archive, fileName) { return `Cannot find the "${fileName.replace(/\\/g, '/')}" file in the "${archive.replace(/\\/g, '/')}" archive.`; } getArchivePath(fullPath) { return this.parse(fullPath).archive; } } exports.default = Asar;module.exports = exports.default;