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

@fedeghe/upload-web-worker

v0.0.7

Published

Use a web worker to upload files in a snap in your React app

Downloads

145

Readme

upload-web-worker

Using a web worker to upload a file could not be easier:

Install dependency

yarn add upload-web-worker

use it

import uww from 'upload-web-worker'

export default () => <input
    type="file"
    multiple
    onChange={e => {
        [...e.target.files].forEach(
            file => uww.start({
                // method: 'PUT' // default
                /**
                 * PUT is the default, and actually
                 * the right verb
                 * 
                 * at the moment is THE ONLY one supported,
                 * thus you can skip that;
                 * 
                 * DO NOT expect the upload to work
                 * when setting a different verb 
                 */
                file,
                url: 'YOUR_UPLOAD_URL',
                headers: {
                    'Access-Control-Allow-Origin' : '*',
                    'Access-Control-Allow-Headers' :'*',
                    'Content-Type': 'multipart/form-data'
                },
                onStart: data => { /* ... */},
                onProgress: data => { /* ... + progress data */},
                onAbort: data => { /* ... */},
                onEnd: data => { /* ... */},
            })
        )
    }}
>

No request headers are sent by default, thus you will have to provide them

Listeners

All the onXXX listeners receive a data object which has always at least the following fields:

{
    "id": "UWW_1",
    "action": "start",
    "fileName": "2019_100_00000_F24ESEG_mpdf.pdf"
}

where

  • id: a unique id associated with this upload
  • action: the action
  • fileName: the upload file name, clearly

with the exception of onProgress which additionally receives inside data also a progress field as follows:

"progress": {
    "percent": 4.84,
    "loaded": 9601024,
    "total": 198546361 
}

where

  • percent: the progress percentage with two decimals
  • loaded: amount of Bytes already loaded
  • total: total amount of Bytes of the uploading file

Aborting

upload-web-worker offert two methods, one is start as we saw above, it returns an id for this upload (the same value that is passed to the listeners).

Using the id returned by start we can invoke abort passing this id:

uww.abort("UWW_1") // and this will trigger the onAbort if set

Minimal working example *

In case you cloned this repo you can start a minimal example, which also shows a possible simple way to show the uploading status for all the uploading files. In this example I'm using React.

Start a minimal local server which exposes a PUT enpoint on http://localhost:3000/upload

cd source/srv
yarn
node index.js

and let it run, then in another terminal tab, in the repo root

yarn & yarn start

now upload one or more files and check the source/srv/uploads folder content. **


* requires a clone from github
** aborted streams are anyway partially written, but this is completely another story