From cf2bf606c7a08b566aaf8322f4601757af464a92 Mon Sep 17 00:00:00 2001 From: Eli Grey Date: Mon, 2 Nov 2020 19:43:53 -0800 Subject: [PATCH] Only request worker script on browsers that support WritableStream & TransformStream (#170) --- package.json | 2 +- src/API.ts | 10 ++++------ src/ua-support.ts | 10 ++++++---- src/workers.ts | 5 +++++ 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 2465ce96..d399c980 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@transcend-io/penumbra", - "version": "4.18.5", + "version": "4.18.6", "description": "Crypto streams for the browser.", "main": "build/penumbra.js", "types": "ts-build/src/index.d.ts", diff --git a/src/API.ts b/src/API.ts index 13ab192e..f6e60211 100644 --- a/src/API.ts +++ b/src/API.ts @@ -29,7 +29,7 @@ import { import { PenumbraZipWriter } from './zip'; import { blobCache, isNumber, isViewableText } from './utils'; import { getWorker, setWorkerLocation } from './workers'; -import { supported } from './ua-support'; +import { advancedStreamsSupported, supported } from './ua-support'; import { preconnect, preload } from './resource-hints'; const resolver = document.createElementNS( @@ -37,8 +37,6 @@ const resolver = document.createElementNS( 'a', ) as HTMLAnchorElement; -const writableStreamsSupported = 'WritableStream' in self; - const { createWriteStream } = streamSaver; if (!WritableStreamIsNative) { // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -52,7 +50,7 @@ async function getJob(...resources: RemoteResource[]): Promise { if (resources.length === 0) { throw new Error('penumbra.get() called without arguments'); } - if (writableStreamsSupported) { + if (advancedStreamsSupported) { // WritableStream constructor supported const worker = await getWorker(); const DecryptionChannel = worker.comlink; @@ -308,7 +306,7 @@ async function encryptJob( }); // We stream the encryption if supported by the browser - if (writableStreamsSupported) { + if (advancedStreamsSupported) { // WritableStream constructor supported const worker = await getWorker(); const EncryptionChannel = worker.comlink; @@ -427,7 +425,7 @@ async function decryptJob( if (files.length === 0) { throw new Error('penumbra.decrypt() called without arguments'); } - if (writableStreamsSupported) { + if (advancedStreamsSupported) { // WritableStream constructor supported const worker = await getWorker(); const DecryptionChannel = worker.comlink; diff --git a/src/ua-support.ts b/src/ua-support.ts index 664625c7..7f8c05fd 100644 --- a/src/ua-support.ts +++ b/src/ua-support.ts @@ -1,5 +1,8 @@ import { PenumbraSupportLevel } from './enums'; +/** Whether WritableStream and TransformStream are natively supported */ +export const advancedStreamsSupported = + 'WritableStream' in self && 'TransformStream' in self; let supportLevel: PenumbraSupportLevel = PenumbraSupportLevel.none; // Event/CustomEvent is non-instantiable, among many various other incompatibilities in IE11 @@ -11,10 +14,9 @@ if ( typeof self.Response === 'function' && typeof self.fetch === 'function' ) { - supportLevel = - 'WritableStream' in self && 'TransformStream' in self - ? PenumbraSupportLevel.full - : PenumbraSupportLevel.size_limited; + supportLevel = advancedStreamsSupported + ? PenumbraSupportLevel.full + : PenumbraSupportLevel.size_limited; } /** Get Penumbra user agent support level */ diff --git a/src/workers.ts b/src/workers.ts index adcdd1c7..e8639de2 100644 --- a/src/workers.ts +++ b/src/workers.ts @@ -11,6 +11,7 @@ import { WorkerLocationOptions, ProgressEmit, } from './types'; +import { advancedStreamsSupported } from './ua-support'; // ////// // // Config // @@ -254,6 +255,10 @@ view.addEventListener('beforeunload', cleanup); export async function setWorkerLocation( options: WorkerLocationOptions | string, ): Promise { + // Workers require WritableStream & TransformStream + if (!advancedStreamsSupported) { + return undefined; + } if (initialized) { console.warn('Penumbra Workers are already active. Reinitializing...'); await cleanup();