Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 DEEPAK-SUTHAR

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
165 changes: 165 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
# Load Stream

[![npm version](https://badge.fury.io/js/load-stream.svg)](https://www.npmjs.com/package/load-stream)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)

## Description

Load Stream is a versatile package that facilitates the uploading of large files to a server in chunks. The package provides both server-side (`load-stream`) and client-side (`loadstream-client`) components. It utilizes HTTP short polling to break files into chunks and send them to the server, providing multiple listeners for events like progress, completion, and errors.

### Features

- Efficient file uploading with chunking
- HTTP short polling for improved performance
- Multiple event listeners: onprogress, onload, onerror
- Methods for stopping and resuming uploads

## Installation

### Server-Side (load-stream)

Install the package using npm:

```bash
npm install load-stream
```

### Integrate it into your server with Express:

```bash
const loadStream = require('load-stream');
const express = require('express');
const app = express();

/* cb function to provide and modify the fileName and destination(where to save file)
this cb function expect an argument which contains file information and current uploading directory.
Example:
file-> {
fileName:'hello.png',
fileSize:1242,
destination:'/' //default destination
}
*/
const cb = (file) => {
return {
fileName: Date.now() + file.fileName,
destination: '/public/uploads'
}
}
//creating instance of loadStream
let loader = new loadStream(cb);

//use this loadStream instance as middleware to handle and make http requests
app.use(loader.load());
//on Error
loader.onerror = (e) => {
console.log(e);
}
```



### Client-Side (CDN)

You can include the client-side library via CDN in your HTML file:

```bash
<script src="/loadStream/loadStream.js"></script>
<script>
/*
options = {
chunksize: 1024 * 1024, // default 1024*1024 (in bytes)
method: 'http' or 'websocket' // websocket currently unavailabile
}
*/
let options = {
chunksize: 1024
}
let loader = new LoadStream(options); //options argument is optional fallback to default options

const uploadFile = async(file)=>{
let fileInfo= await loader.upload(file);
//fileInfo contain the filename destination and size which is served by server after accepting the callback by user on server side integration
}

loader.onprogress = (event) => {
console.log(event.progress);// progress Example: to use in progress bar
console.log(event.time); // Expected time remains in uploading file (in seconds)
console.log(event.speed);// Uploading speed (in bytes/sec)
}

loader.onload = () => {
console.log("Completed");
}

loader.onerror = (event) => {
console.log(event.error);
}

const resumeUpload = ()=>{
loader.resume(); // returns true if resumed else false
}

const pauseUpload = ()=>{
loader.stop();// returns true if paused else false
}
</script>

```



### Client-Side (loadstream-client)
To include client side script in React.Js/Next.Js etc

```bash
npm install loadstream-client
```

### Integrate it into your client side:
```bash
import loadStream from 'loadstream-client'
```
```bash
/*
const url='http://localhost:5000'; // url is server's host url
*/
let loader = new loadStream({ url }) // url is required*

const uploadFile = async (file) => {
try {
loader.upload(file);

loader.onprogress = (event) => {
console.log(event.progress);// progress Example: to use in progress bar
console.log(event.time); // Expected time remains in uploading file (in seconds)
console.log(event.speed);// Uploading speed (in bytes/sec)
}

loader.onload = () => {
console.log("Completed");
}

loader.onerror = (event) => {
console.log(event.error);
}

const resumeUpload = ()=>{
loader.resume(); // returns true if resumed else false
}

const pauseUpload = ()=>{
loader.stop();// returns true if paused else false
}

} catch (error) {
console.error("Upload error:", error);
}
```

# Contributing

We welcome contributions! Your contribution means to us.

# License
This project is licensed under the MIT License - see the LICENSE file for details.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "load-stream",
"version": "1.0.2",
"version": "1.0.3",
"description": "A npm package to upload files as chunk ",
"main": "src/index.js",
"scripts": {
Expand Down
16 changes: 12 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,18 @@ class StreamLoader {
fileInfo['chunksize'] = chunksize as number;
fileInfo['transferedSize'] = 0;
let userId: string = uuidv4();
fileInfo['writeStream'] = fs.createWriteStream(path.join((fileInfo['destination'] as string) + '/' + (fileInfo['fileName'] as string))).on('ready', () => {
fileInfo['writeStream'] = fs.createWriteStream(path.join(path.resolve() + (fileInfo['destination'] as string) + '/' + (fileInfo['fileName'] as string))).on('ready', () => {
this.#fileInfoStore[userId] = fileInfo;
res.json({ handshake: true, userId });
}).on('error', (err) => {
res.json({
handshake: true,
userId, info:
{
file: fileInfo.fileName,
size: fileInfo.fileSize,
destination: fileInfo.destination
}
});
}).on('error', (err: Error) => {
this.onerror({ error: err });
res.json({ handshake: false, error: err });
});
Expand Down Expand Up @@ -196,4 +204,4 @@ class StreamLoader {


export default StreamLoader;
module.exports = StreamLoader;
module.exports = StreamLoader;
40 changes: 22 additions & 18 deletions src/static/js/loadStream.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class loadStream {
class LoadStream {
//options for transfer file
#options = {
method: 'http',
Expand All @@ -21,7 +21,6 @@ class loadStream {
* @param {Object} options {uploadMethod:http|websocket,ChunkSize}
*/
constructor(options = null) {
this.file = file;
if (options) {
this.#options.method = options.method ? options.method : 'http';
this.#options.chunksize = options.chunksize ? options.chunksize : 1024 * 1024;
Expand All @@ -33,23 +32,28 @@ class loadStream {
* @param {File|Blob|String} file File to Upload
*/
async upload(file) {
this.file = file;
this.#fileSize = this.file.size;
if (this.#fileSize > 0) {
if (this.#options.method === 'http') {
let res = await this.#handShake();
if (res.handshake) {
this.#userId = res.userId;
this.#uploadFile();
} else {
if (this.onerror)
this.onerror({ error: res.error || "Something Wrong During Handshaking!" });
return new Promise(async (resolve, reject) => {
this.file = file;
this.#fileSize = this.file.size;
if (this.#fileSize > 0) {
if (this.#options.method === 'http') {
let res = await this.#handShake();
if (res.handshake) {
this.#userId = res.userId;
this.#uploadFile();
resolve({ success: true, info: res.info });
} else {
if (this.onerror)
this.onerror({ error: res.error || "Something Wrong During Handshaking!" });
resolve({ success: false, error: res.error || "Something Wrong During Handshaking!" })
}
} else if (this.#options.method === 'websocket') {
// let socket = io({ path: '/loadStream.io' });
resolve({ success: false, error: "Websocket Currently Unavalible!" })
console.log("Currently Unavalible!");
}
} else if (this.#options.method === 'websocket') {
// let socket = io({ path: '/loadStream.io' });
console.log("Currently Unavalible!");
}
}
})
}
/**
*
Expand Down Expand Up @@ -176,4 +180,4 @@ class loadStream {
resolve(res);
})
}
}
}