hgouveia / node-downloader-helper

A simple http file downloader for node.js
MIT License
247 stars 54 forks source link

Error when calling pause or stop method #64

Closed Luury closed 2 years ago

Luury commented 2 years ago

When I call the pause() function using a download object, the following error occurs:

image

what is the correct way to call the function just passing the object?

Chaphasilor commented 2 years ago

You don't pass the object, pause() is a method on the download object.

So you could do:

const downloadObject = new DownloaderHelper(`http://example.com/download`, __dirname)

downloadObject.start().then(() => {
  setTimeout(() => downloadObject.pause(), 3000)
})
Luury commented 2 years ago

Sorry for my bad English, I know it's an object method, but I figured out the real problem, I'm using Vue 3, and I stored the download object in vue's data(), and tried using pause() or stop() through it. The other methods work, but for some reason these two don't work, when I do with pure javascript it works, I'm still trying to figure out the problem.

My code:

Data:

  data() {
    return {
      downloads: [],
    };
  },

Methods:

Add Download to data:

 addDownload() {
      const download = {
        name: "Test",
        header_image:
          "https://images.sftcdn.net/images/t_app-cover-l,f_auto/p/ce2ece60-9b32-11e6-95ab-00163ed833e7/260663710/the-test-fun-for-friends-screenshot.jpg",
        status: "",
        downloading: null,
        progress: 0,
        speed: 0,
        size: 0,
        downloaded: 0,
        object: new DownloaderHelper(
          "https://nodejs.org/dist/v14.17.5/node-v14.17.5-x64.msi",
          "./downloads",
          {
            fileName: { name: "Test" }, // Custom filename when saved
            override: { skip: true, skipSmaller: true },
            removeOnStop: true, // remove the file when is stopped (default:true)
            removeOnFail: true, // remove the file when fail (default:true)
          }
        ),
      };

      this.downloads.push(download);
    }

Download: (this works)

    download(item) {
      item.object.on("start", () => {
        item.downloading = true;
      });

      item.object.on("progress", (stats) => {
        item.progress = parseInt(stats.progress);
        item.status = parseInt(stats.progress) + "%";
        item.speed = byteHelper(stats.speed);
        item.downloaded = byteHelper(stats.downloaded);
        item.size = byteHelper(stats.total);
      });

      item.object.on("end", () => console.log("Download Completed"));

      item.object.on("error", (error) => console.log(error));

      item.object.start();
    }

Pause:

    pauseDownload(item) {
      item.object.pause();
      item.downloading = false;
    }

Resume: (this works)

    resumeDownload(item) {
      item.object.resume();
      item.downloading = true;
    }

Stop:

    stopDownload(item) {
      item.downloading = false;
      item.object.stop();
    }

Stats: (this works)

    statsDownload(item){
      console.log(item.object.getStats())
    }
hgouveia commented 2 years ago

@Luury what i suspect is happening, is that when the download objects are store in vue data, they are treated as browser object, and this library only works in the nodejs context, what i would suggest is just keep the downloader object outside the vue data, and try to map them by id or something that you can call refer to them later

Luury commented 2 years ago

@hgouveia, You're right, that solved the problem.