Compare commits

...

2 Commits
0.6.0 ... 0.7.1

Author SHA1 Message Date
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
3 changed files with 14 additions and 8 deletions

View File

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

View File

@ -14,7 +14,15 @@ export function download(href: any, name: string) {
document.body.removeChild(a);
}
export function downloadStream(url: string, name?: string) {
/**
* 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 emitter = new TypedEmitter<downloadEvents>();
fetch(url).then(response => {
const contentLength = response.headers.get('Content-Length') || '0';
@ -24,19 +32,17 @@ 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;
} else {
const chunk = result.value;
chunks.push(chunk);
loaded += chunk.length;
const progress = Math.round((loaded / total) * 100);
const progress = loaded / total;
emitter.emit('progress', progress);
reader.read().then(processResult);
}

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';