我有以下 curl 命令可以正常工作...
curl -L -v "https://us-central1-npm.pkg.dev/my-project/npm-public/@my-scope/bucky-barnes/-/@my-scope/bucky-barnes-0.0。 1.tgz" -o 测试.tgz
这会输出一个我可以解压缩的 test.tgz 文件。
我正在尝试使用 NodeJS 自动执行此操作(注意-L
for 重定向)。我想出了以下...
import express from "express";
import tar from "tar-stream";
import https from "https";
const app = express();
const PORT = 3000;
const request = function(req, res, url) {
const reqSent = https.get(url, (response) => {
if (response.statusCode == 302 || response.statusCode == 307) {
const url = `https://${reqSent.host}${response.headers.location}`;
console.log("Redirecting ", url);
request(req, res, url);
} else {
console.log("Extracting ", reqSent.host);
const extract = tar.extract()
extract.on('entry', function(header, stream, next) {
console.log(`The filename is ${header.name}`)
stream.on('end', function() {
next() // ready for next entry
})
stream.resume() // just auto drain the stream
})
extract.on("error", (e)=>{
res.status(500).send(`Error decompressing ${e}`)
})
extract.on('finish', function() {
res.send("Completed")
})
response.pipe(extract);
};
} ).on("error", (e)=>{
console.log(`There was an error ${e}`);
res.status(500).send("Failed");
});
};
app.use((req, res)=>{
request(req, res, "https://us-central1-npm.pkg.dev/my-project/npm-public/@my-scope/bucky-barnes/-/@my-scope/bucky-barnes-0.0.1.tgz")
});
app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));
当我运行它失败时说 tar-streamer 无法解析响应......
解压错误错误:数据意外结束
我错过了什么?为什么管道不适用于 tgz 文件?
tgz
不是tar
。这是一个压缩的 tar 球。
来自https://www.npmjs.com/package/tar-stream:
请注意,如果您有 .tar.gz,您仍然需要对数据进行压缩。我们建议将 gunzip-maybe 与此结合使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句