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

chunk-uploader

v1.2.0

Published

A js tool to help implementing a chunk uploading system.

Downloads

8

Readme

js chunk uploader

2020-04-08 -> 2020-05-14

A js tool to help implementing a chunk uploading system.

This can be used as the js client in a simple-chunk-upload-protocol communication.

Install

npm i chunk-uploader

Usage

Example 1: the basics

const ChunkUploader = require("chunk-uploader");


// assuming file is a js File object
async function demo(file){
    
    let data = {
        firstName: "paul",        
        csrf_token: "abc",        
        whatever: "dude",        
    };

    let chunkUploader = new ChunkUploader({
        serverUrl: "/my-upload-server.php",
        chunkSize: 2 * 1024 * 1024, // 2M
        onChunkAborted: (start, end, isLastChunk) => {
            // do something...
        },
        onChunkLoaded: (start, end, size, isLastChunk) => {
             let percent = Math.round(end / size * 100, 2) + "%";
            // update your gui...
        },
    });
    let response = await chunkUploader.sendByChunks(file, data, {
        start: 0, // note, you can resume an interrupted upload, if that's the case, change start to the size of the partially uploaded file
    });
    
    let jsonResponse = await response.json(); // for instance
}

Example 2: handling failure at the chunk level

In this example we use the onChunkResponseReceived callback to test every chunk's response. If a chunk's response fails, the upload of the file stops immediately.

This allows us to fail early, rather than having to wait until the end of the upload before triggering an error message.

const ChunkUploader = require("chunk-uploader");

async function example(){

    let chunkUploader = new ChunkUploader({
        serverUrl: options.serverUrl,
        // chunkSize: 5 * 1024 * 1024,
        onChunkAborted: (start, end, isLastChunk) => {
            console.log("the upload has been aborted...");
        },
        onChunkLoaded: (start, end, size, isLastChunk) => {
            let percent = Math.round(end / size * 100, 2) + "%";
            // update your gui...
        },
        onChunkResponseReceived: async function (response) {
            try {
                let jsonResponse = await response.json();
                // analyze the chunk response here, and return true if ok...
                return true;
            } catch (e) {
                // otherwise return a message error, this will interrupt the upload.
                // or return false to use a default error message
                return e.toString();
            }
        }
    });
            
    let file = 0 // replace with some js File object
    let data = {}; // extra data if necessary, this is sent via post
    let response = await chunkUploader.sendByChunks(file, data);
    let jsonResponse = await response.json();

}

example();


History log

  • 1.2.0 -- 2020-05-14

    • add onChunkResponseReceived callback option
  • 1.1.0 -- 2020-04-20

    • Update sendByChunks, now handles data parameter recursively
  • 1.0.1 -- 2020-04-13

    • add link in README.md
  • 1.0.0 -- 2020-04-08

    • initial commit