English 中文(简体)
Node.JS HTTPdown——记忆问题
原标题:Node.JS HTTP Download — Memory Issue

因此,我有我认为是一个缓冲问题。

我们在此重写的方案是一个简单的下载加速器。 它把下载量分成5个单独的链接。 在较大的档案(500MB+)上,该方案的执行被停止,CALL_AND_RETRY_0分拨款的错误被推翻。

(function() {
  var Getbot, fs, http, makeReadable, path, progressbar, request, url, util;

  util = require( util );

  fs = require( fs );

  path = require( path );

  http = require( http );

  url = require( url );

  request = require( request );

  progressbar = require( progress );

  Getbot = (function() {

    Getbot.totalDownloaded = Getbot.lastDownloaded = Getbot.downloadStart = 0;

    Getbot.bar;

    function Getbot(address, user, pass) {
      var downloads, options, req;
      options = {
        uri: address,
        headers: {},
        method:  HEAD 
      };
      if (!options.auth) options.auth = "" + user + ":" + pass;
      downloads = 2;
      req = request(options, function(error, response, body) {
        var fileBasename, fileExt, filename, newFilename, size;
        if (!error) {
          switch (response.statusCode) {
            case 200:
              size = response.headers[ content-length ];
              filename = decodeURI(url.parse(address).pathname.split("/").pop());
              fileExt = path.extname(filename);
              fileBasename = path.basename(filename, fileExt);
              newFilename = "" + fileBasename + ".getbot";
              Getbot.bar = new progressbar( Downloading: [:bar] :percent :eta | :rate , {
                complete:  = ,
                incomplete:    ,
                width: 20,
                total: parseInt(size, 10)
              });
              Getbot.downloadStart = new Date;
              try {
                return fs.open(newFilename,  w , function(err, fd) {
                  fs.truncate(fd, size);
                  return Getbot.startParts(options, size, 5, Getbot.download);
                });
              } catch (error) {
                console.log("Not enough space.");
              }
              break;
            case 401:
              return console.log("401 Unauthorized");
            default:
              return console.log("" + response.statusCode);
          }
        } else {
          return console.log("" + error);
        }
      });
      req.end();
    }

    Getbot.download = function(options, offset, end) {
      var file, fileBasename, fileExt, filename, fops, newFilename, req;
      filename = decodeURI(url.parse(options.uri).pathname.split("/").pop());
      fileExt = path.extname(filename);
      fileBasename = path.basename(filename, fileExt);
      newFilename = "" + fileBasename + ".getbot";
      options.headers = {};
      options.method =  GET ;
      options.headers["range"] = "bytes=" + offset + "-" + end;
      fops = {
        flags:  r+ ,
        start: offset
      };
      file = fs.createWriteStream(newFilename, fops);
      req = request(options, function(error, response, body) {
        if (error) return console.log(error);
      });
      req.on( data , function(data) {
        Getbot.totalDownloaded += data.length;
        Getbot.bar.tick(data.length, {
           rate : Getbot.downloadRate(Getbot.downloadStart)
        });
        return file.write(data);
      });
      return req.on( end , function() {
        file.end();
        return fs.rename(newFilename, filename);
      });
    };

    Getbot.downloadRate = function(start) {
      return makeReadable(Getbot.totalDownloaded / (new Date - start) * 1024) +  /s ;
    };

    Getbot.startParts = function(options, bytes, parts, callback) {
      var i, partSize, _results;
      partSize = Math.ceil(1 * bytes / parts);
      i = 0;
      _results = [];
      while (i < parts) {
        console.log("Starting part " + (i + 1));
        callback(options, partSize * i, Math.min(partSize * (i + 1) - 1, bytes - 1));
        _results.push(i++);
      }
      return _results;
    };

    Getbot.status = function(status) {
      return process.stdout.write( 
33[2K  + status);
    };

    Getbot.save = function(buffer) {
      return console.log("Writing file...");
    };

    return Getbot;

  })();

  makeReadable = function(bytes) {
    var precision, unit, units;
    units = [ Bytes ,  KB ,  MB ,  GB ,  TB ];
    unit = 0;
    while (bytes >= 1024) {
      unit++;
      bytes = bytes / 1024;
      precision = unit > 2 ? 2 : 1;
    }
    return "" + (bytes.toFixed(precision)) + " " + units[unit];
  };

  module.exports = Getbot;

}).call(this);
最佳回答

分配失败是一个明显错误。 我认为,我看问题是什么。

由于这一反馈接受“<条码>第1条论点,这将迫使这一请求在援助团中缓冲所有请求数据,直到这一职能得到执行!

req = request(options, function(error, response, body) {
  if (error) return console.log(error);
});

你们需要告诉<代码>request,以便在请求开始回来时,不要把所有数据放在最后事件火上。

在你们的选择中加上这一点并更改背书。

options.onResponse = true;
req = request(options, function(error, response) {
  if (error) return console.log(error);
});
问题回答

暂无回答




相关问题
How to upload and download file from a server using C#

I am developing a webpage in that the user can download their Resume for Edit. So I have a link to download the article. I use the following code for download. DataTable dt = user.getUserDetails(2); ...

Detecting PDF Download

I have recently added functionality for generating pdf reports to a web application. However the reports can be quite large and take some time to show the pdf download dialog. I want to show a spinner ...

Writing file to users giving sporadic error in IE

I have a very interesting issue with only specific IE implementations. I have an ASPX page that is used to write files down to the user, as part of the process the page uses the following code to ...

PHP - List of Excel files to download from Intranet Site?

I have a intranet site running PHP 5 that needs to list a folder containing only Excel files. Ideally the user needs to be able to input some search criteria (ie date) and the files would be filtered ...

Determine total size of SVN directory/trunk

Is there a way to count/calculate the total size of a svn directory if you were to checkout a revision? I have limited internet downloads so I need to know how big something is before I go and ...

Scala and html: download an image (*.jpg, etc) to Hard drive

I ve got a Scala program that downloads and parses html. I got the links to the image files form the html, Now I need to transfer those images to my hard drive. I m wondering what the best Scala ...

Downloading file with wxHTTP?

Im really stumped. Im using the wxHTTP class in wxWidgets to try and download two files. The first request succeeds but the second one fails when wxHTTP->GetInputStream is called. Between downloads, ...

热门标签