node-progress icon indicating copy to clipboard operation
node-progress copied to clipboard

Invalid array length

Open rawnly opened this issue 8 years ago • 7 comments

//...
https.get(url, response => {
			// if --progress run the progressbar
			if (flags.progress) {
				const len = parseInt(response.headers['content-length'], 10);
				const bar = new ProgressBar(chalk `{yellow ↓} {red :percent} [:bar] :elapsed s`, {
					complete: '=',
					incomplete: ' ',
					width: 20,
					total: len,
					clear: true
				});
			
				// on data received fill the progressbar
				response.on('data', chunk => {
					bar.tick(chunk.length, {
						passphrase: 'Making something awesome'
					});
				});
			}

                  //... rest of the function

causes:

/Users/rawnly/code/NODE/splash-cli-2018/node_modules/progress/lib/node-progress.js:155
  complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete);
             ^

RangeError: Invalid array length
    at ProgressBar.render (/Users/rawnly/code/NODE/splash-cli-2018/node_modules/progress/lib/node-progress.js:155:14)
    at ontimeout (timers.js:471:11)
    at tryOnTimeout (timers.js:306:5)
    at Timer.listOnTimeout (timers.js:266:5)

rawnly avatar Nov 03 '17 18:11 rawnly

Getting this as well

kokarn avatar Nov 30 '17 12:11 kokarn

The reason for this problem is probably the total parameter except the problem, the value is NaN, you can check if this is the problem.

junfeisu avatar Aug 23 '18 02:08 junfeisu

Currently experiencing this exact issue.

actongorton avatar Oct 02 '18 18:10 actongorton

content-length isn't guaranteed so it could be 0 and make bug. set a value if content-length is 0.

      // content-length isn't guaranteed so it could be 0 and make bug. set a value if content-length is 0.
      const len = parseInt(response.headers["content-length"], 10) || 100000000;
      const bar = new ProgressBar("downloading [:bar] :rate/bps :percent :etas", {
        complete: "=",
        incomplete: " ",
        width: 20,
        total: len,
      });
      response.on("data", (chunk) => {
        bar.tick(chunk.length);
      });

it need validation for the better error info tips: #171

alwxkxk avatar Apr 10 '19 08:04 alwxkxk

I confirm the bug in 2.0.3 in case total is 0 :

const ProgressBar = require('progress')
const format = 'progress: :percent [:bar] :current/:total (~:etas left)'
const options = {total: 0}
const progressBar = new ProgressBar(format, options)
// next line throw RangeError: Invalid array length at ProgressBar.render
// on the line `complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete);`
// because completeLength isn't a number
progressBar.tick(0)

dcaillibaud avatar Jun 05 '19 08:06 dcaillibaud

The index of array must be integer. The input :total values is NAN or float, while the output array's index is not integer so than rendering error.

fredgan avatar Nov 26 '20 08:11 fredgan

For a temp solution you can change node-progress.js

completeLength = Math.round(width * ratio) with this one

completeLength = Math.round(width * ratio) | 0;

that works for me image

iseasonora avatar Apr 02 '21 07:04 iseasonora