From 38fd349d9b058cd7b15b5bb9819727d2b0344f6d Mon Sep 17 00:00:00 2001 From: Danny Coates Date: Thu, 19 Jul 2018 14:46:12 -0700 Subject: [PATCH] use TransformStream if available --- app/ece.js | 9 ++++++--- app/serviceWorker.js | 4 ++-- app/streams.js | 7 +++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/ece.js b/app/ece.js index 28743473..86ed29cd 100644 --- a/app/ece.js +++ b/app/ece.js @@ -1,5 +1,5 @@ import 'buffer'; -import { transform } from './streams'; +import { transformStream } from './streams'; const NONCE_LENGTH = 12; const TAG_LENGTH = 16; @@ -353,9 +353,12 @@ export default class ECE { new BlobSlicer(this.input, this.rs, this.mode) ); } else { - inputStream = transform(this.input, new StreamSlicer(this.rs, this.mode)); + inputStream = transformStream( + this.input, + new StreamSlicer(this.rs, this.mode) + ); } - return transform( + return transformStream( inputStream, new ECETransformer(this.mode, this.key, this.rs, this.salt) ); diff --git a/app/serviceWorker.js b/app/serviceWorker.js index a539b1d8..e02e6845 100644 --- a/app/serviceWorker.js +++ b/app/serviceWorker.js @@ -1,6 +1,6 @@ import Keychain from './keychain'; import { downloadStream } from './api'; -import { transform } from './streams'; +import { transformStream } from './streams'; import contentDisposition from 'content-disposition'; let noSave = false; @@ -24,7 +24,7 @@ async function decryptStream(request) { const body = await file.download.result; - const readStream = transform(body, { + const readStream = transformStream(body, { transform: (chunk, controller) => { file.progress += chunk.length; controller.enqueue(chunk); diff --git a/app/streams.js b/app/streams.js index b1e95603..8a1c49f5 100644 --- a/app/streams.js +++ b/app/streams.js @@ -1,6 +1,9 @@ -/* global ReadableStream */ +/* global ReadableStream TransformStream */ -export function transform(readable, transformer) { +export function transformStream(readable, transformer) { + if (typeof TransformStream === 'function') { + return readable.pipeThrough(new TransformStream(transformer)); + } const reader = readable.getReader(); const tstream = new ReadableStream({ start(controller) {