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 🙏

© 2024 – Pkg Stats / Ryan Hefner

send-big-file

v1.0.0

Published

分片发送大文件。

Downloads

1

Readme

send-big-file

分片发送大文件。

快速安装

yarn add send-big-file

或者

npm i --save send-big-file

快速使用

import sendBigFile, { UploadFileStat } from 'send-big-file';

// 定义文件
const file: File = ...;
// 上传状态
const uploadFileStat: UploadFileStat | undefined;
sendBigFile(file).subscribe(
  (stat) => {
    uploadFileStat = stat;
    console.log(`上传进度: ${((stat.loaded / stat.total) * 100).toFixed(2)}%`);
  },
  (error) => {
    console.log('上传失败');
  },
  () => {
    console.log(uploadFileStat.success ? '上传成功' : '上传失败');
  },
);

参数说明

  • file 要上传的文件

  • options 上传配置,主要包括:

    • concurrent 并发数目,默认为 10

    • chunkSize 分片大小,以 b 为单位,默认为 2MB

    • chunkExists 检查分片是否已经上传的方法

      默认实现:

      /**
       * 判断分片是否已经上传
       *
       * @param md5 分片md5
       */
      function chunkExists(md5: string): Observable<boolean> {
        return from(
          http.get < { exists: boolean } > `${url}/chunk/${md5}/status`,
        ).pipe(map((result) => result.exists));
      }

      上述示例中:

      • 默认接收一个 md5 参数,表示分片的 md5 值
      • 函数必须返回一个Observable形式的布尔值
    • sendChunk 上传分片的方法

      默认实现:

      /**
       * 上传分片
       * @param file 整个文件
       * @param md5 文件的md5
       */
      function sendChunk(file: Blob, md5: string) {
        return new Observable<ProgressEvent>((observer) => {
          sendFile(`${url}/chunk/${md5}`, file as File, 'file', {
            onUploadProgress: (event) => observer.next(event),
          })
            .then(() => {
              observer.complete();
            })
            .catch((error) => {
              observer.error(error);
            });
        });
      }

      上述示例中:

      • 接收两个参数,其中file表示分片文件,md5表示分片文件对应的 md5
      • 返回一个 promise
    • mergeChunks 合并分片的方法

      默认实现:

      /**
       * 合并分片,形成文件
       *
       * @param {string[]} chunks
       * @returns {Observable<string>}
       */
      function mergeChunks(_file: File, chunks: string[]): Observable<string> {
        return new Observable<string>((observer) => {
          http.post<{ fileId: string }>(`${baseUrl}/merge`, chunks).then(
            (result) => {
              observer.next(result.fileId);
              observer.complete();
            },
            (error) => observer.error(error),
          );
        });
      }

      上述示例:

      • 接收两个参数,file表示整个文件,chunks表示文件分片
      • 返回一个字符串,表示文件 id

特性

  • 支持分片并发上传
  • 失败自动重试
  • 断点续传