@transcend-io/penumbra
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -13,2 +13,3 @@ /// <reference types="node" /> | ||
fileName?: string | null; | ||
progressEventName?: string; | ||
}; | ||
@@ -27,4 +28,5 @@ export declare function downloadEncryptedFile(url: string, key: string | Buffer, iv: string | Buffer, authTag: string | Buffer, options?: DownloadEncryptedFileOptions): Promise<void>; | ||
alwaysBlob?: boolean; | ||
progressEventName?: string; | ||
}; | ||
export declare function getDecryptedContent(url: string, key: string | Buffer, iv: string | Buffer, authTag: string | Buffer, mime: string, options?: GetDecryptedContentOptions): Promise<string | Blob>; | ||
export {}; |
@@ -51,3 +51,3 @@ "use strict"; | ||
*/ | ||
function fetchAndDecipher(url, key, iv, authTag) { | ||
function fetchAndDecipher(url, key, iv, authTag, progressEventName) { | ||
if (typeof key === 'string') | ||
@@ -67,3 +67,3 @@ key = Buffer.from(key, 'base64'); | ||
var contentLength = response.headers.get('Content-Length'); | ||
return decryptStream(response.body, decipher, Number(contentLength), url); | ||
return decryptStream(response.body, decipher, Number(contentLength), url, progressEventName); | ||
})); | ||
@@ -78,5 +78,6 @@ } | ||
*/ | ||
function emitProgress(totalBytesRead, contentLength, url) { | ||
function emitProgress(totalBytesRead, contentLength, url, progressEventName) { | ||
if (progressEventName === void 0) { progressEventName = url; } | ||
var percent = Math.round((totalBytesRead / contentLength) * 100); | ||
var event = new CustomEvent(url, { | ||
var event = new CustomEvent(progressEventName, { | ||
detail: { | ||
@@ -86,2 +87,3 @@ percent: percent, | ||
contentLength: contentLength, | ||
url: url, | ||
}, | ||
@@ -99,3 +101,3 @@ }); | ||
*/ | ||
function decryptStream(rs, decipher, contentLength, url) { | ||
function decryptStream(rs, decipher, contentLength, url, progressEventName) { | ||
// TODO check authTag with decipher.final | ||
@@ -115,3 +117,3 @@ var _this = this; | ||
totalBytesRead += chunk.length; | ||
emitProgress(totalBytesRead, contentLength, url); | ||
emitProgress(totalBytesRead, contentLength, url, progressEventName); | ||
return [2 /*return*/]; | ||
@@ -138,3 +140,3 @@ }); | ||
totalBytesRead += chunk.length; | ||
emitProgress(totalBytesRead, contentLength, url); | ||
emitProgress(totalBytesRead, contentLength, url, progressEventName); | ||
controller.enqueue(decValue); | ||
@@ -216,3 +218,3 @@ push(); | ||
fileName = options.fileName || (url.match(fileFromUrlRegex) || [])[0] || 'download'; | ||
return [4 /*yield*/, fetchAndDecipher(url, key, iv, authTag)]; | ||
return [4 /*yield*/, fetchAndDecipher(url, key, iv, authTag, options.progressEventName)]; | ||
case 1: | ||
@@ -235,3 +237,3 @@ rs = _a.sent(); | ||
type = mime.split('/')[0]; | ||
return [4 /*yield*/, fetchAndDecipher(url, key, iv, authTag)]; | ||
return [4 /*yield*/, fetchAndDecipher(url, key, iv, authTag, options.progressEventName)]; | ||
case 1: | ||
@@ -238,0 +240,0 @@ rs = _a.sent(); |
23
index.ts
@@ -24,2 +24,3 @@ // External modules | ||
authTag: string | Buffer, | ||
progressEventName?: string, | ||
): Promise<ReadableStream> { | ||
@@ -45,3 +46,4 @@ if (typeof key === 'string') key = Buffer.from(key, 'base64'); | ||
Number(contentLength), | ||
url | ||
url, | ||
progressEventName, | ||
); | ||
@@ -64,5 +66,6 @@ }) | ||
url: string, | ||
progressEventName: string = url, | ||
): void { | ||
const percent = Math.round((totalBytesRead / contentLength) * 100); | ||
const event = new CustomEvent(url, { | ||
const event = new CustomEvent(progressEventName, { | ||
detail: { | ||
@@ -72,2 +75,3 @@ percent, | ||
contentLength, | ||
url, | ||
}, | ||
@@ -92,2 +96,3 @@ }); | ||
url: string, | ||
progressEventName?: string, | ||
): ReadableStream { | ||
@@ -111,3 +116,3 @@ // TODO check authTag with decipher.final | ||
totalBytesRead += chunk.length; | ||
emitProgress(totalBytesRead, contentLength, url); | ||
emitProgress(totalBytesRead, contentLength, url, progressEventName); | ||
}, | ||
@@ -136,3 +141,3 @@ }) | ||
totalBytesRead += chunk.length; | ||
emitProgress(totalBytesRead, contentLength, url); | ||
emitProgress(totalBytesRead, contentLength, url, progressEventName); | ||
@@ -235,2 +240,3 @@ controller.enqueue(decValue); | ||
fileName?: string | null; | ||
progressEventName?: string; | ||
} | ||
@@ -248,3 +254,3 @@ | ||
const rs = await fetchAndDecipher(url, key, iv, authTag); | ||
const rs = await fetchAndDecipher(url, key, iv, authTag, options.progressEventName); | ||
@@ -267,3 +273,4 @@ // Stream the file to disk | ||
// useServiceWorker?: boolean, | ||
alwaysBlob?: boolean, | ||
alwaysBlob?: boolean; | ||
progressEventName?: string; | ||
} | ||
@@ -281,3 +288,3 @@ | ||
const rs = await fetchAndDecipher(url, key, iv, authTag); | ||
const rs = await fetchAndDecipher(url, key, iv, authTag, options.progressEventName); | ||
@@ -290,2 +297,2 @@ // Return the decrypted content | ||
return new Response(rs).blob(); | ||
} | ||
} |
{ | ||
"name": "@transcend-io/penumbra", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Crypto streams for the browser.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -26,2 +26,3 @@ ## Usage | ||
fileName: 'myFile.json', // optional values | ||
progressEventName: 'download-progress' // defaults to the url | ||
} | ||
@@ -47,2 +48,2 @@ ); | ||
<ExposeHeader>Content-Length</ExposeHeader> | ||
``` | ||
``` |
547
48
24196
1093
7