unpkg/server/utils/fetchPackage.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-05-21 22:44:00 +00:00
require("isomorphic-fetch");
const gunzip = require("gunzip-maybe");
const tar = require("tar-fs");
function stripNamePrefix(headers) {
// Most packages have header names that look like "package/index.js"
// so we shorten that to just "index.js" here. A few packages use a
// prefix other than "package/". e.g. the firebase package uses the
// "firebase_npm/" prefix. So we just strip the first dir name.
headers.name = headers.name.replace(/^[^/]+\//, "");
return headers;
}
function ignoreLinks(file, headers) {
return headers.type === "link" || headers.type === "symlink";
}
function extractResponse(response, outputDir) {
return new Promise((resolve, reject) => {
const extract = tar.extract(outputDir, {
readable: true, // All dirs/files should be readable.
map: stripNamePrefix,
ignore: ignoreLinks
});
response.body
.pipe(gunzip())
.pipe(extract)
.on("finish", resolve)
.on("error", reject);
});
}
function fetchPackage(tarballURL, outputDir) {
console.log(`info: Fetching ${tarballURL} and extracting to ${outputDir}`);
2018-05-21 22:44:00 +00:00
return fetch(tarballURL).then(res => extractResponse(res, outputDir));
2018-05-21 22:44:00 +00:00
}
module.exports = fetchPackage;