'Download zip release from private repository with node.js

I'm trying to download zip release from a private repository, i've tried many solutions but none seems to work.

Here my code :

function Download(url, path, options) {
updateHeader(options.stageTitles.Downloading)
let received_bytes = 0;
let total_bytes = 0;

// eslint-disable-next-line
console.log('-----------------')
// eslint-disable-next-line
console.log(url, path)
console.log('-----------------')

var req = request(
    {
        method: 'GET',
        uri: url,
        headers: {
            Authorization: `token ${options.gitRepoToken}`,
            'Accept': 'application/octet-stream',
            'User-Agent': 'request module',
        },
        //encoding: null
    }
);

// eslint-disable-next-line
console.log(req)

var out = fs.createWriteStream(path);
req.pipe(out);

req.on('response', data => {
    // eslint-disable-next-line
    console.log(data.headers, data)
    total_bytes = parseInt(data.headers['content-length']);
});

req.on('data', chunk => {
    received_bytes += chunk.length;
    showProgress(received_bytes, total_bytes);
});

req.on('end', () => {
    Install(options)
});}

url variable equal something like : https://github.com/SolberLight/PRIVATE_REPO_NAME/releases/download/1.0/MY_ZIP_NAME.zip

Looks like I'm always getting a content size of 9 bytes with a "not found" response so I'm guessing that my headers aren't correct ?

thanks for your help !

EDIT 1 : Made some progress but now it seems that the blob is not the right size

 (async () => {
    //get latest release
    var response = await axios.get(
        `https://api.github.com/repos/${repoWithOwner}/releases/latest`,
        {
            headers: authHeaders,
        }
    );
    var assets = response.data.assets;
    for (var i = 0; i < assets.length; i++) {
        console.log(assets[i].url);
        response = await axios({
            method: "get",
            url: assets[i].url,
            responseType: "blob",
            headers: {
                //Accept: "application/octet-stream",
                ...authHeaders,
            },
        });
        // eslint-disable-next-line
        console.log(response.data, path)

        let reader = new FileReader()
        reader.onload = function() {
            if (reader.readyState == 2) {
                var buffer = new Buffer(reader.result)
                console.log(`Saving ${JSON.stringify({ fileName: 'myfile.zip', size: response.data.size })}`)
                outputFile(path, buffer, err => {
                    if (err) {
                        // eslint-disable-next-line
                        console.log(err.message)
                    } else {
                        // eslint-disable-next-line
                        console.log(path)
                    }
                })
            }
        }
        reader.readAsArrayBuffer(response.data)
    }
})();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source