Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I decompress a hosted tar file on the fly using NodeJS

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading