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

file-chunked

v1.0.4

Published

File Chunk or simple upload

Downloads

42

Readme

file-chunked

File Chunk or simple upload

Description

A NodeJS module that makes simple to handle the chunk uploaded on the server side

Requirements

Nodejs >=v8.16.0

Installation

npm install file-chunked --save

How frontend should work

  • The frontend should send not the entire file (ex: 20MB file) but it should send the file splited in 20 chunks of 1MB each.
  • The frontend on each request that sends a chunk should send also:
    • uploadId - a unique ID for each file upload (ex: md5(filename+((new Date()).getTime())+user.id)). The uploadId is the same for all chunks of a single file uploaded.
    • chunkIndex - an ordered number of the chunk
    • totalChunksCount - the total number number of chunks the file was divided. Example: a 5.3MB file splited in chunks of 1MB will have a total of 6 chunks(5 chunks of 1MB and one chunk of 0.3MB).

How the chunk upload works:

  • uploaded files are saved into a storage dir
  • file-chunked inside that dir creates a dir tmp_chunks and moves the file chunk in that directory by giving another name
  • When another chunk arrives it merges this chunk with the rest of the file uploaded and deletes this chunk but keeps only the merged chunks inside tmp_chunks
  • When the last chunk arrives it merges the last chunk with the rest of file, so file file is complete. After that it moves merged file from tmp_chunks to the default storage dir where all uploaded files are saved giving this file the original name.
  • After each chunk uploaded it checks if there are orphaned chunks that are older than 24 hours. If there are any it deletes all them. This is made to not keep dead chunks that can be created when a user starts to upload a large file and in the middle of the upload process he interrupts the upload(user can interrupt it, network problems etc...)
  • on the backend after the last chunk is uploaded you assume that the file is uploaded so you can do the other stuff like: move that file where you want to save it and insert records into db if needed.

Example implementation on server-side

// call all the required packages
const express = require('express')
const cors = require('cors');
const bodyParser= require('body-parser')
const multer = require('multer');
const fs = require('fs');
const FileChunked = require('file-chunked');

// SET STORAGE
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/tmp')
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
  }
})
 
var upload = multer({ storage: storage })

//CREATE EXPRESS APP
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({extended: true}))
 
app.post('/upload', upload.single('file'), (req, res, next) => {
  const {file, body} = req;
	if(body.chunking == 'true'){
		
		FileChunked.upload({
			chunkStorage:"/tmp/", // where the uploaded file(chunked file in this case) are saved
			uploadId: body.uploadId,
			chunkIndex: body.chunkIndex,
			totalChunksCount: body.totalChunksCount,
			filePath: req.file.path,
		});
	}
	
  res.json({ message: 'WELCOME' });
  
})

app.listen(3000, () => console.log('Server started on port 3000'));