UNPKG

@vercel/fun

Version:

Local Lambda development environment

93 lines 4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZipFile = exports.zipFromBuffer = exports.zipFromFile = void 0; exports.unzipToTemp = unzipToTemp; exports.unzip = unzip; const node_os_1 = require("node:os"); const stat_mode_1 = __importDefault(require("stat-mode")); const debug_1 = __importDefault(require("debug")); const node_path_1 = require("node:path"); const node_fs_1 = require("node:fs"); const promises_1 = require("node:fs/promises"); const consumers_1 = require("node:stream/consumers"); const promises_2 = require("node:stream/promises"); const yauzl_promise_1 = require("yauzl-promise"); Object.defineProperty(exports, "ZipFile", { enumerable: true, get: function () { return yauzl_promise_1.ZipFile; } }); Object.defineProperty(exports, "zipFromFile", { enumerable: true, get: function () { return yauzl_promise_1.open; } }); Object.defineProperty(exports, "zipFromBuffer", { enumerable: true, get: function () { return yauzl_promise_1.fromBuffer; } }); const debug = (0, debug_1.default)('@vercel/fun:unzip'); async function unzipToTemp(data, tmpDir = (0, node_os_1.tmpdir)()) { const dir = (0, node_path_1.join)(tmpDir, `zeit-fun-${Math.random() .toString(16) .substring(2)}`); let zip; if (Buffer.isBuffer(data)) { debug('Unzipping buffer (length=%o) to temp dir %o', data.length, dir); zip = await (0, yauzl_promise_1.fromBuffer)(data); } else { debug('Unzipping %o to temp dir %o', data, dir); zip = await (0, yauzl_promise_1.open)(data); } await unzip(zip, dir); await zip.close(); debug('Finished unzipping to %o', dir); return dir; } const getMode = (entry) => new stat_mode_1.default({ mode: entry.externalFileAttributes >>> 16 }); async function unzip(zipFile, dir, opts = {}) { let entry; const strip = opts.strip || 0; while ((entry = await zipFile.readEntry()) !== null) { const fileName = strip === 0 ? entry.fileName : entry.fileName .split('/') .slice(strip) .join('/'); const destPath = (0, node_path_1.join)(dir, fileName); if (/\/$/.test(entry.fileName)) { debug('Creating directory %o', destPath); await (0, promises_1.mkdir)(destPath, { recursive: true }); } else { const [entryStream] = await Promise.all([ entry.openReadStream(), // ensure parent directory exists (0, promises_1.mkdir)((0, node_path_1.dirname)(destPath), { recursive: true }) ]); const mode = getMode(entry); if (mode.isSymbolicLink()) { const linkDest = await (0, consumers_1.text)(entryStream); debug('Creating symboling link %o to %o', destPath, linkDest); await (0, promises_1.symlink)(linkDest, destPath); } else { const modeOctal = mode.toOctal(); const modeVal = parseInt(modeOctal, 8); if (modeVal === 0) { debug('Unzipping file to %o', destPath); } else { debug('Unzipping file to %o with mode %s (%s)', destPath, modeOctal, String(mode)); } try { await (0, promises_1.unlink)(destPath); } catch (err) { if (err.code !== 'ENOENT') { throw err; } } const destStream = (0, node_fs_1.createWriteStream)(destPath, { mode: modeVal }); await (0, promises_2.pipeline)(entryStream, destStream); } } } } //# sourceMappingURL=unzip.js.map