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

Invalid array length #166

Open
rawnly opened this issue Nov 3, 2017 · 7 comments
Open

Invalid array length #166

rawnly opened this issue Nov 3, 2017 · 7 comments

Comments

@rawnly
Copy link

rawnly commented Nov 3, 2017

//...
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)
@kokarn
Copy link

kokarn commented Nov 30, 2017

Getting this as well

@junfeisu
Copy link

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.

@actongorton
Copy link

Currently experiencing this exact issue.

@alwxkxk
Copy link

alwxkxk commented Apr 10, 2019

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

@dcaillibaud
Copy link

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)

@fredgan
Copy link

fredgan commented Nov 26, 2020

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.

zhizhuxialiwen pushed a commit to zhizhuxialiwen/node-progress that referenced this issue Dec 11, 2020
The input `:total` value is float, but the output `:current` value is more than `:total` value. The input `:total` is nagative integer or zero, but the output rendering failed.

Closes visionmedia#177, visionmedia#166
@iseasonora
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants