Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from request to node-fetch #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 16 additions & 43 deletions binstall.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'
var fs = require("fs");
var request = require("request");
var fetch = require('node-fetch')
var tar = require("tar");
var zlib = require("zlib");
var unzip = require("unzip-stream");
Expand Down Expand Up @@ -48,29 +49,16 @@ function untgz(url, path, options) {
if (error.code !== 'EEXIST') throw error;
}

request
.get(url, function(error, response) {
if (error) {
reject("Error communicating with URL " + url + " " + error);
return;
}
if (response.statusCode == 404) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://www.npmjs.com/package/node-fetch#handling-exceptions

3xx-5xx responses are NOT exceptions, and should be handled in then()

Doesn't that mean we still need to check for 404 or any other 300-599 status codes?

var errorMessage = options.errorMessage || "Not Found: " + url;

reject(new Error(errorMessage));
return;
}

if (verbose) {
console.log("Downloading binaries from " + url);
}

response.on("error", function() {
reject("Error receiving " + url);
});
})
if (verbose) {
console.log("Downloading binaries from " + url);
}
return fetch(url)
.then(function (response) {
response.body
.pipe(gunzip)
.pipe(untar);
return
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to return the stream or the promise or whatever so that the caller will be able to wait for the operation to fully complete? (Or is that happening somehow as this is?)

})
});
}

Expand Down Expand Up @@ -103,28 +91,13 @@ function unzipUrl(url, path, options) {
}
});

request
.get(url, function(error, response) {
if (error) {
reject("Error communicating with URL " + url + " " + error);
return;
}
if (response.statusCode == 404) {
var errorMessage = options.errorMessage || "Not Found: " + url;

reject(new Error(errorMessage));
return;
}

if (verbose) {
console.log("Downloading binaries from " + url);
}

response.on("error", function() {
reject("Error receiving " + url);
});
if (verbose) {
console.log("Downloading binaries from " + url);
}
return fetch(url)
.then(function (response) {
response.body.pipe(writeStream);
})
.pipe(writeStream);
});
}

Expand Down
Loading