Added upload function which tracks progress
All checks were successful
Build / Build NPM Project (push) Successful in 16s
Build / Tag Version (push) Successful in 4s
Build / Publish (push) Successful in 7s

This commit is contained in:
Zakary Timson 2024-04-24 06:57:15 -04:00
parent 34c2df7a1a
commit 2fe8cdb96a
4 changed files with 41 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.7.2",
"version": "0.8.0",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -1,7 +1,8 @@
import {TypedEmitter, TypedEvents} from './emitter';
export type downloadEvents = TypedEvents & {
export type DownloadEvents = TypedEvents & {
complete: (blob: Blob) => any;
failed: (error: Error) => any;
progress: (progress: number) => any;
}
@ -20,11 +21,12 @@ export function download(href: any, name: string) {
*
* @param {string} url
* @param {string} downloadName
* @return {TypedEmitter<downloadEvents>}
* @return {TypedEmitter<DownloadEvents>}
*/
export function downloadProgress(url: string, downloadName?: string) {
const emitter = new TypedEmitter<downloadEvents>();
const progress = new TypedEmitter<DownloadEvents>();
fetch(url).then(response => {
if(!response.ok) return progress.emit('failed', new Error(response.statusText));
const contentLength = response.headers.get('Content-Length') || '0';
const total = parseInt(contentLength, 10);
let chunks: any[] = [], loaded = 0;
@ -37,16 +39,15 @@ export function downloadProgress(url: string, downloadName?: string) {
download(url, downloadName);
URL.revokeObjectURL(url);
}
emitter.emit('complete', blob);
progress.emit('complete', blob);
} else {
const chunk = result.value;
chunks.push(chunk);
loaded += chunk.length;
const progress = loaded / total;
emitter.emit('progress', progress);
progress.emit('progress', loaded / total);
reader.read().then(processResult);
}
});
});
return emitter;
}).catch(err => progress.emit('failed', err));
return progress;
}

View File

@ -8,4 +8,5 @@ export * from './misc';
export * from './objects';
export * from './string';
export * from './time';
export * from './upload';
export * from './xhr';

30
src/upload.ts Normal file
View File

@ -0,0 +1,30 @@
import {TypedEmitter, TypedEvents} from './emitter';
export type UploadEvents = TypedEvents & {
complete: () => any;
failed: (err: Error) => any;
progress: (progress: number) => any;
}
function uploadProgress(files : File | FileList, url: string) {
const xhr = new XMLHttpRequest();
const progress = new TypedEmitter<UploadEvents>();
const formData = new FormData();
(Array.isArray(files) ? files : [files])
.forEach(f => formData.append('file', f));
xhr.upload.addEventListener("progress", (event) => {
if(event.lengthComputable) progress.emit('progress', event.loaded / event.total);
});
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if(xhr.status >= 200 && xhr.status < 300) progress.emit('complete');
else progress.emit('failed', new Error(xhr.responseText));
}
};
xhr.open("POST", url, true);
xhr.send(formData);
return progress;
}