From f58b6194cea7b52b2de66f6ead18df165b6b1923 Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Tue, 17 Jul 2018 11:40:01 -0700 Subject: [PATCH] updated streams ponyfill --- app/ece.js | 14 ++++---------- app/serviceWorker.js | 6 +++--- app/streams.js | 35 +++++++++++++++++++++++++++++++++++ app/transformStream.js | 25 ------------------------- 4 files changed, 42 insertions(+), 38 deletions(-) create mode 100644 app/streams.js delete mode 100644 app/transformStream.js diff --git a/app/ece.js b/app/ece.js index 631116a9..75cd3aa8 100644 --- a/app/ece.js +++ b/app/ece.js @@ -1,12 +1,8 @@ import 'buffer'; -import TransformStream from './transformStream'; -import { ReadableStream as ReadableStreamPony } from 'web-streams-ponyfill'; -try { - new ReadableStream().pipeThrough(new TransformStream()); -} catch (e) { - // eslint-disable-next-line no-global-assign - ReadableStream = ReadableStreamPony; -} +import { + TStream as TransformStream, + RStream as ReadableStream +} from './streams'; const NONCE_LENGTH = 12; const TAG_LENGTH = 16; @@ -360,14 +356,12 @@ export default class ECE { new BlobSlicer(this.input, this.rs, this.mode) ); } else { - // eslint-disable-next-line no-undef const sliceStream = new TransformStream( new StreamSlicer(this.rs, this.mode) ); inputStream = this.input.pipeThrough(sliceStream); } - // eslint-disable-next-line no-undef const cryptoStream = new TransformStream( new ECETransformer(this.mode, this.key, this.rs, this.salt) ); diff --git a/app/serviceWorker.js b/app/serviceWorker.js index f017e433..f78b5cd3 100644 --- a/app/serviceWorker.js +++ b/app/serviceWorker.js @@ -1,6 +1,6 @@ import Keychain from './keychain'; import { downloadStream } from './api'; -import TransformStream from './transformStream'; +import { TStream as TransformStream, wrapReadable } from './streams'; let noSave = false; const map = new Map(); @@ -30,7 +30,7 @@ async function decryptStream(request) { } }); - const readStream = stream.pipeThrough(progStream); + const readStream = wrapReadable(stream).pipeThrough(progStream); const decrypted = file.keychain.decryptStream(readStream); const headers = { @@ -38,7 +38,7 @@ async function decryptStream(request) { 'Content-Type': file.type, 'Content-Length': file.size }; - const body = decrypted.local ? decrypted.nativeReadable : decrypted; + const body = decrypted.isPony ? decrypted.toNative() : decrypted; return new Response(body, { headers }); } catch (e) { if (noSave) { diff --git a/app/streams.js b/app/streams.js new file mode 100644 index 00000000..5e8e254e --- /dev/null +++ b/app/streams.js @@ -0,0 +1,35 @@ +/* global TransformStream ReadableStream */ +import { createReadableStreamWrapper } from '@mattiasbuelens/web-streams-adapter'; +import { + TransformStream as TransformStreamPony, + ReadableStream as ReadableStreamPony +} from 'web-streams-ponyfill'; + +const toNativeReadable = createReadableStreamWrapper(ReadableStream); +const toPonyReadable = createReadableStreamWrapper(ReadableStreamPony); + +export let TStream; +if (typeof TransformStream === 'function') { + TStream = TransformStream; +} else { + TStream = TransformStreamPony; + TStream.prototype.isPony = true; +} + +export let RStream = ReadableStream; +try { + new ReadableStream().pipeThrough(new TransformStream()); +} catch (e) { + RStream = ReadableStreamPony; + RStream.prototype.isPony = true; + RStream.prototype.toNative = function() { + return toNativeReadable(this); + }; +} + +export function wrapReadable(stream) { + if (RStream === ReadableStream) { + return stream; + } + return toPonyReadable(stream); +} diff --git a/app/transformStream.js b/app/transformStream.js deleted file mode 100644 index f0dc1605..00000000 --- a/app/transformStream.js +++ /dev/null @@ -1,25 +0,0 @@ -/* global TransformStream */ -import { createReadableStreamWrapper } from '@mattiasbuelens/web-streams-adapter'; -import { TransformStream as TransformStreamPony } from 'web-streams-ponyfill'; - -const toNative = createReadableStreamWrapper(ReadableStream); - -class TransformStreamLocal { - constructor(transformer) { - this.stream = new TransformStreamPony(transformer); - this.local = true; - } - get nativeReadable() { - return toNative(this.stream.readable); - } - get readable() { - return this.stream.readable; - } - get writable() { - return this.stream.writable; - } -} - -export default (typeof TransformStream === 'function' - ? TransformStream - : TransformStreamLocal);