通过nodejs spawn执行wget时动态进度条无法正确显示

发布于 2025-01-11 04:02:12 字数 1137 浏览 0 评论 0原文

我编写了一个node.js 代码,就像下面的示例一样。

我使用 child_process.spawn 创建子进程来执行命令“wget”。同时我将子进程的标准 I/O 重定向到 Node.js 进程的标准 I/O

let cp = spawn('wget', 'some_file_url')
cp.stdin.pipe(process.stdin)
cp.stdout.pipe(process.stdout)
cp.stderr.pipe(process.stderr)

现在我在 Node.js 中执行上面的代码,一切正常,但进度条。它看起来像这样:

     0K .......... .......... .......... .......... ..........  0%  134K 5m21s
    50K .......... .......... .......... .......... ..........  0%  631K 3m14s
   100K .......... .......... .......... .......... ..........  0% 4.64M 2m12s
   150K .......... .......... .......... .......... ..........  0%  167M 99s

通常,如果我在终端上执行 wget 命令,

$ wget some_file_url

动态进度条将显示在输出中

file.tar.gz                  15%[======>                                         ]   6.69M  2.27MB/s

,所以,我应该如何使进度条显示如上面的

环境

  • node.js: v14.16.1
  • wget: GNU Wget 1.21 .2 建立在 darwin21.1.0 上,

抱歉

我的英语很差。请原谅语法或打字错误。我正在尽力改进。

i write a node.js code just like below sample.

i use child_process.spawn to create child process for execute command 'wget'. at the same time i redirec standard i/o of child process to standard i/o of node.js process

let cp = spawn('wget', 'some_file_url')
cp.stdin.pipe(process.stdin)
cp.stdout.pipe(process.stdout)
cp.stderr.pipe(process.stderr)

now i execute code above in node.js, everything is ok but progress bar. it just looks like this:

     0K .......... .......... .......... .......... ..........  0%  134K 5m21s
    50K .......... .......... .......... .......... ..........  0%  631K 3m14s
   100K .......... .......... .......... .......... ..........  0% 4.64M 2m12s
   150K .......... .......... .......... .......... ..........  0%  167M 99s

normally, if i execute wget command on terminal

$ wget some_file_url

a dynamic progress bar will display in output

file.tar.gz                  15%[======>                                         ]   6.69M  2.27MB/s

so, how should i do to make the progress bar display like above

environments

  • node.js: v14.16.1
  • wget: GNU Wget 1.21.2 built on darwin21.1.0

sorry

my english is poor. please excuse grammar or typing error. i'm trying my best to imporve.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伏妖词 2025-01-18 04:02:12

我怀疑你得到的效果是由于 GNU Wget 认为它的 stderr 被重定向到文件。我不知道如何使用 GNU Wget 获得所需的效果,但如果您只需要下载文件(并且不需要使用 GNU Wget 这样做),那么请考虑将 https进度。最后需要先安装,

npm install progress

然后你就可以像这样使用它(改编自 progress - 下载示例

var fs = require('fs');
var ProgressBar = require('progress');
var https = require('https');
 
var req = https.request({
  host: 'www.example.com',
  port: 443,
  path: '/'
}, function (res) {
  var filePath = fs.createWriteStream('index.html');
  res.pipe(filePath);
});
 
req.on('response', function(res){
  var len = parseInt(res.headers['content-length'], 10);
 
  console.log();
  var bar = new ProgressBar('  downloading [:bar] :rate/bps :percent :etas', {
    complete: '=',
    incomplete: ' ',
    width: 20,
    total: len
  });
 
  res.on('data', function (chunk) {
    bar.tick(chunk.length);
  });
 
  res.on('end', function () {
    console.log('\n');
  });
});
 
req.end();

代码下载 www.example.com 页面并保存作为index.html,请注意,与 wget 的 url 不同,您不在主机中包含协议(在本例中为 https)。 443 是 https 的默认端口。 / 是服务器上资源的路径(在本例中:主页)。 Content-Length 标头保存有关文件总大小的信息。

I suspect effect you did get is due to GNU Wget thinking its' stderr was redirected to file. I do not know how to get desired effect using GNU Wget, but if you need to just download file (and are not required to do so using GNU Wget) then consider combining https with progress. Last need to be installed first, do

npm install progress

then you might use it like so (adapted from progress - download example)

var fs = require('fs');
var ProgressBar = require('progress');
var https = require('https');
 
var req = https.request({
  host: 'www.example.com',
  port: 443,
  path: '/'
}, function (res) {
  var filePath = fs.createWriteStream('index.html');
  res.pipe(filePath);
});
 
req.on('response', function(res){
  var len = parseInt(res.headers['content-length'], 10);
 
  console.log();
  var bar = new ProgressBar('  downloading [:bar] :rate/bps :percent :etas', {
    complete: '=',
    incomplete: ' ',
    width: 20,
    total: len
  });
 
  res.on('data', function (chunk) {
    bar.tick(chunk.length);
  });
 
  res.on('end', function () {
    console.log('\n');
  });
});
 
req.end();

Code do download www.example.com page and save it as index.html, note that unlike url for wget you do NOT include protocol (in this case https) in host. 443 is default port for https. / is path to resource on server (in this case: main page). Content-Length header hold information about total file size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文