UNPKG

@vercel/fun

Version:

Local Lambda development environment

63 lines 2.74 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateNodeTarballUrl = generateNodeTarballUrl; exports.installNode = installNode; const tar_1 = require("tar"); const node_fetch_1 = __importDefault(require("node-fetch")); const debug_1 = __importDefault(require("debug")); const node_zlib_1 = require("node:zlib"); const promises_1 = require("node:stream/promises"); const node_path_1 = require("node:path"); const node_fs_1 = require("node:fs"); const semver_1 = require("semver"); const unzip_1 = require("./unzip"); const debug = (0, debug_1.default)('@vercel/fun:install-node'); function generateNodeTarballUrl(version, platform = process.platform, arch = process.arch) { if (!version.startsWith('v')) { version = `v${version}`; } let ext; let plat = platform; if (platform === 'win32') { ext = 'zip'; plat = 'win'; } else { ext = 'tar.gz'; } return `https://nodejs.org/dist/${version}/node-${version}-${plat}-${arch}.${ext}`; } async function installNode(dest, version, platform = process.platform, arch = process.arch) { // For Apple M1, use the x64 binaries for v14 or less, // since there are no arm64 binaries for these versions if (platform === 'darwin' && arch === 'arm64' && (0, semver_1.satisfies)(version, '<= 14')) { arch = 'x64'; } const tarballUrl = generateNodeTarballUrl(version, platform, arch); debug('Downloading Node.js %s tarball %o', version, tarballUrl); const res = await (0, node_fetch_1.default)(tarballUrl); if (!res.ok) { throw new Error(`HTTP request failed: ${res.status}`); } if (platform === 'win32') { // Put it in the `bin` dir for consistency with the tarballs const finalDest = (0, node_path_1.join)(dest, 'bin'); const zipName = (0, node_path_1.basename)(tarballUrl); const zipPath = (0, node_path_1.join)(dest, zipName); debug('Saving Node.js %s zip file to %o', version, zipPath); await (0, promises_1.pipeline)(res.body, (0, node_fs_1.createWriteStream)(zipPath)); debug('Extracting Node.js %s zip file to %o', version, finalDest); const zipFile = await (0, unzip_1.zipFromFile)(zipPath); await (0, unzip_1.unzip)(zipFile, finalDest, { strip: 1 }); } else { debug('Extracting Node.js %s tarball to %o', version, dest); await (0, promises_1.pipeline)(res.body, (0, node_zlib_1.createGunzip)(), (0, tar_1.extract)({ strip: 1, C: dest })); } } //# sourceMappingURL=install-node.js.map