I have the following curl command that works fine...
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 test.tgz
This outputs a test.tgz file I can decompress.
I am trying to automate this using NodeJS (mind the -L
for redirect). I came up with the following...
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}`));
When I run it fails saying that the tar-streamer can't parse the response...
Error decompressing Error: Unexpected end of data
What am I missing? Why isn't the pipe working for the tgz file?
CodePudding user response:
tgz
is not tar
. It's a gzipped tar-ball.
From https://www.npmjs.com/package/tar-stream:
Note that you still need to gunzip your data if you have a .tar.gz. We recommend using gunzip-maybe in conjunction with this.