Compare commits

..

4 Commits
0.6.0 ... 0.8.0

Author SHA1 Message Date
2fe8cdb96a 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
2024-04-24 06:57:15 -04:00
34c2df7a1a Fixed import
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
2024-04-23 09:09:07 -04:00
1d5509a078 Fixed export
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
2024-04-23 09:03:51 -04:00
9f57b93a9f Download stream
All checks were successful
Build / Build NPM Project (push) Successful in 17s
Build / Tag Version (push) Successful in 4s
Build / Publish (push) Successful in 10s
2024-04-23 09:02:10 -04:00
4 changed files with 53 additions and 15 deletions

View File

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

View File

@ -1,7 +1,8 @@
import {TypedEmitter, TypedEvents} from './emitter.ts';
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;
}
@ -14,9 +15,18 @@ export function download(href: any, name: string) {
document.body.removeChild(a);
}
export function downloadStream(url: string, name?: string) {
const emitter = new TypedEmitter<downloadEvents>();
/**
* Download a URL using fetch so progress can be tracked. Uses Typed Emitter to emit a "progress" &
* "complete" event.
*
* @param {string} url
* @param {string} downloadName
* @return {TypedEmitter<DownloadEvents>}
*/
export function downloadProgress(url: string, downloadName?: string) {
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;
@ -24,23 +34,20 @@ export function downloadStream(url: string, name?: string) {
reader?.read().then(function processResult(result) {
if(result.done) {
const blob = new Blob(chunks);
emitter.emit('progress', 1);
if(name) {
if(downloadName) {
const url = URL.createObjectURL(blob);
download(url, name);
download(url, downloadName);
URL.revokeObjectURL(url);
}
emitter.emit('complete', blob);
return;
progress.emit('complete', blob);
} else {
const chunk = result.value;
chunks.push(chunk);
loaded += chunk.length;
const progress = Math.round((loaded / total) * 100);
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

@ -1,6 +1,6 @@
export * from './array';
export * from './aset';
export * from './download.ts';
export * from './download';
export * from './emitter';
export * from './errors';
export * from './logger';
@ -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;
}