UNPKG

@feroomjs/npm-fetcher

Version:
152 lines (137 loc) 6.12 kB
'use strict'; var path = require('path'); var node_zlib = require('node:zlib'); var tarStream = require('tar-stream'); var node_stream = require('node:stream'); /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __awaiter(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()); }); } const banner = () => `[${"@feroomjs/npm-fetcher"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `; /* istanbul ignore file */ function logError(error) { console.error('' + '' + banner() + error + ''); } function panic(error) { logError(error); return new Error(error); } function isTextFile(path) { return path.endsWith('.js') || path.endsWith('.map') || path.endsWith('.css') || path.endsWith('.json') || path.endsWith('.txt') || path.endsWith('.mjs') || path.endsWith('.cjs') || path.endsWith('.md') || path.endsWith('.html'); } const tagsCache = {}; function fetchDistTags(registryUrl, packageName) { return __awaiter(this, void 0, void 0, function* () { if (!tagsCache[packageName]) { const res = yield fetch(path.join(registryUrl, '-', 'package', packageName, 'dist-tags')); if (res.status > 200) { throw panic(`Could not read dist-tags of "${packageName}"\n` + (yield res.text())); } tagsCache[packageName] = JSON.parse(yield res.text()); } return tagsCache[packageName]; }); } function fetchPackageInfo(registryUrl, packageName, packageVersion) { return __awaiter(this, void 0, void 0, function* () { if (!packageVersion) { packageVersion = (yield fetchDistTags(registryUrl, packageName)).latest; } if (!packageVersion) { throw panic(`Could not determine the latest version of "${packageName}"`); } const res = yield fetch(path.join(registryUrl, packageName, packageVersion)); if (res.status > 200) { throw panic(`Could not read package "${packageName}"\n` + (yield res.text())); } return JSON.parse(yield res.text()); }); } function fetchTgz(registryUrl, packageName, packageVersion) { return __awaiter(this, void 0, void 0, function* () { const pkg = yield fetchPackageInfo(registryUrl, packageName, packageVersion); const res = yield fetch(pkg.dist.tarball); if (res.status > 200) { throw panic(`Could not fetch package "${packageName}"\n` + (yield res.text())); } return res; }); } // import { Stream } from 'node:stream' function unpackTgzToMemory(buffer) { return new Promise((resolve, reject) => { const ex = tarStream.extract(); const files = {}; ex.on('entry', function (header, stream, cb) { stream.on('data', function (chunk) { const fileName = header.name.replace(/^package\//, ''); const isText = isTextFile(fileName); if (!files[fileName]) { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment files[fileName] = isText ? chunk.toString() : chunk; } else { if (isText) files[fileName] += chunk; if (!isText) files[fileName] = Buffer.concat([files[fileName], chunk]); } }); stream.on('end', function () { cb(); }); stream.resume(); }); ex.on('finish', () => { resolve(files); }); node_stream.pipeline(buffer, node_zlib.createGunzip(), ex, reject); }); } function pKey(p, v) { return `${p}@${v}`; } const cache = {}; function getNpmPackageVersion(registryUrl, packageName, version) { return __awaiter(this, void 0, void 0, function* () { if (!version) { const tags = yield fetchDistTags(registryUrl, packageName); version = tags.latest; } return version; }); } function getNpmPackageFiles(registryUrl, packageName, version) { return __awaiter(this, void 0, void 0, function* () { if (!version) { const tags = yield fetchDistTags(registryUrl, packageName); version = tags.latest; } const k = pKey(packageName, version); if (!cache[k]) { const res = yield fetchTgz(registryUrl, packageName, version); cache[k] = unpackTgzToMemory(res.body); } return cache[k]; }); } exports.getNpmPackageFiles = getNpmPackageFiles; exports.getNpmPackageVersion = getNpmPackageVersion;