php - Downloading phar with nodejs. Which encoding should be used? -


i'm trying download phar (php archive) program in node.js. after testing, i've discovered files not same when downloaded browser , node.js.

i've tried few things:
"binary" encoding
"utf8" encoding
none of theses works.

do know encoding should use download phar?
code:

exports.download = function(urlstr, dest, cb) { // urlstr url link of phar, dest destination file, , cb callback.  var options = {      headers: {          "user-agent": "psm (pocketmine server manager, https://psm.mcpe.fun) user requester"      }  }  var data = "";  var url = new url(urlstr);  options.hostname = url.hostname;  options.path = url.pathname;  var request = http.get(options, function(response) {      // check if response success      if (response.statuscode == 302 || response.statuscode == 301) {          exports.download(response.headers["location"], dest, cb);          return;      }      response.on("data", function(chunk) {          data += chunk.tostring("binary");      })      response.on('end', function() {          fs.writefilesync(dest, data, "binary");          cb();      });  }).on('error', function(err) { // handle errors      fs.unlink(dest); // delete file async. (but don't check result)      if (cb) cb(err.message);  });  };

phar used (valid): https://psm.mcpe.fun/download/psmcore/1.1.phar

i noticed few issues in original code:

  • the main issue binary data being implicitly converted utf-8 string, not preserve content. keep data in buffer form or pipe response disk using streams instead of buffering entire response in memory first.

  • when using asynchronous callbacks in node, convention pass actual error object first argument instead of string. many times these objects provide more information either not contained in message itself, or not parseable error message (e.g. stack trace, libuv error code, contextual information such http uris, file paths, hostnames, etc.).

  • no 200 status code being checked before saving response disk. end saving error html pages (e.g. 400, 404, etc.) disk instead of you're expecting. should check content-type header you're expecting further ensure response think is.

an example first 2 items fixed is:

var res; function onerror(err) {   if (res) {     res.resume(); // ensure response drained     res = null;   }   if (cb) {     fs.unlink(dest, function() {});     cb(err);     cb = null;   } } http.get(options, function(response) {   // check if response success   if (response.statuscode === 302 || response.statuscode === 301) {     exports.download(response.headers["location"], dest, cb);     res.resume(); // ensure response drained     return;   }   res = response;   var out = fs.createwritestream(dest);   out.on('error', onerror);   response.pipe(out).on('close', function() {     if (cb) {       cb();       cb = null;     }   }); }).on('error', onerror); 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -