npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

node-breakpoint-down

v1.0.0

Published

node breakpoint download

Downloads

7

Readme

背景

在做nw应用的下载更新时候,需求是可以暂停下载。

最开始考虑的是用request下载,会返回一个流,流有暂停、恢复的功能。暂停不长的时间可以恢复正常下载,但暂停较长时间后恢复下载,会立马结束。不满足需求。

网上搜了下node断点下载的资料,大都是断点续传的。搜到些其他语言比如java做断点下载的文章,总结了下,自己用node做了实现。

思路

首先往资源发起一个请求,设置headers Range 为 bytes=0-1,可以获得资源响应的header,判断其中accept-ranges的值是否为bytes,有则支持断点下载。同时还能通过content-range获得资源的整体大小。

判断本地文件是否存在,存在则获取大小判断是否下载完成。不存在则本地文件大小视为0。

创建写入流,追加写入

fileWrite = fs.createWriteStream(destinationPath, {
        flags: "a"
      });

根据本地大小,发起文件下载请求,设置headers的Range

downStream = request(
        {
          method: "GET",
          url,
          forever: true,
          headers: {
            "Cache-Control": "no-cache",
            Range: `bytes=${localSize}-${totalSize - 1}`
          }
        },
        function() {}
      );

返回的流,可以用来中断下载。需要重新下载时,重新走一遍上面流程。根据大小判断下载完成。若已知md5,再校验下载文件的md5和已知的md5是否一致。

代码示例

const path = require('path')
const createDownloadTask = require("../index");
const tmpdir = path.join(__dirname, "down");
createDownloadTask({
    url:
      "https://folger-1251685788.cos.ap-guangzhou.myqcloud.com/blog/breakpoint/downloadtest.txt",//50M
    md5: "c8c3085051e21d57e13d6544b7bbb832",
    filename: "test1.txt",
    tmpdir
  }).then(function(task) {
    task.emit("start");
    let st;
    task
      .on("totalSize", function({ totalSize }) {
        // console.log("get total size", totalSize);
      })
      .on("progress", function({ percent }) {
        // console.log("on progress", percent);
      })
      .on("end", function({ filepath }) {
        console.log("on end", filepath);
        clearInterval(st);
      })
      .on("error", function(err) {
        console.log("on error", err);
        clearInterval(st);
      })
      .on("abort", function() {
        console.log("on abort");
      });

    st = setInterval(function() {
      task.emit("stop");
      task.emit("start");
    }, 2000);
  });