Download stream
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	"name": "@ztimson/utils",
 | 
						"name": "@ztimson/utils",
 | 
				
			||||||
	"version": "0.6.0",
 | 
						"version": "0.7.0",
 | 
				
			||||||
	"description": "Utility library",
 | 
						"description": "Utility library",
 | 
				
			||||||
	"author": "Zak Timson",
 | 
						"author": "Zak Timson",
 | 
				
			||||||
	"license": "MIT",
 | 
						"license": "MIT",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,7 +14,15 @@ export function download(href: any, name: string) {
 | 
				
			|||||||
	document.body.removeChild(a);
 | 
						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>();
 | 
						const emitter = new TypedEmitter<downloadEvents>();
 | 
				
			||||||
	fetch(url).then(response => {
 | 
						fetch(url).then(response => {
 | 
				
			||||||
		const contentLength = response.headers.get('Content-Length') || '0';
 | 
							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) {
 | 
							reader?.read().then(function processResult(result) {
 | 
				
			||||||
			if(result.done) {
 | 
								if(result.done) {
 | 
				
			||||||
				const blob = new Blob(chunks);
 | 
									const blob = new Blob(chunks);
 | 
				
			||||||
				emitter.emit('progress', 1);
 | 
									if(downloadName) {
 | 
				
			||||||
				if(name) {
 | 
					 | 
				
			||||||
					const url = URL.createObjectURL(blob);
 | 
										const url = URL.createObjectURL(blob);
 | 
				
			||||||
					download(url, name);
 | 
										download(url, downloadName);
 | 
				
			||||||
					URL.revokeObjectURL(url);
 | 
										URL.revokeObjectURL(url);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				emitter.emit('complete', blob);
 | 
									emitter.emit('complete', blob);
 | 
				
			||||||
				return;
 | 
					 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				const chunk = result.value;
 | 
									const chunk = result.value;
 | 
				
			||||||
				chunks.push(chunk);
 | 
									chunks.push(chunk);
 | 
				
			||||||
				loaded += chunk.length;
 | 
									loaded += chunk.length;
 | 
				
			||||||
				const progress = Math.round((loaded / total) * 100);
 | 
									const progress = loaded / total;
 | 
				
			||||||
				emitter.emit('progress', progress);
 | 
									emitter.emit('progress', progress);
 | 
				
			||||||
				reader.read().then(processResult);
 | 
									reader.read().then(processResult);
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user