updated streams ponyfill

This commit is contained in:
Danny Coates 2018-07-17 11:40:01 -07:00
parent f4f8332f96
commit f58b6194ce
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
4 changed files with 42 additions and 38 deletions

View File

@ -1,12 +1,8 @@
import 'buffer'; import 'buffer';
import TransformStream from './transformStream'; import {
import { ReadableStream as ReadableStreamPony } from 'web-streams-ponyfill'; TStream as TransformStream,
try { RStream as ReadableStream
new ReadableStream().pipeThrough(new TransformStream()); } from './streams';
} catch (e) {
// eslint-disable-next-line no-global-assign
ReadableStream = ReadableStreamPony;
}
const NONCE_LENGTH = 12; const NONCE_LENGTH = 12;
const TAG_LENGTH = 16; const TAG_LENGTH = 16;
@ -360,14 +356,12 @@ export default class ECE {
new BlobSlicer(this.input, this.rs, this.mode) new BlobSlicer(this.input, this.rs, this.mode)
); );
} else { } else {
// eslint-disable-next-line no-undef
const sliceStream = new TransformStream( const sliceStream = new TransformStream(
new StreamSlicer(this.rs, this.mode) new StreamSlicer(this.rs, this.mode)
); );
inputStream = this.input.pipeThrough(sliceStream); inputStream = this.input.pipeThrough(sliceStream);
} }
// eslint-disable-next-line no-undef
const cryptoStream = new TransformStream( const cryptoStream = new TransformStream(
new ECETransformer(this.mode, this.key, this.rs, this.salt) new ECETransformer(this.mode, this.key, this.rs, this.salt)
); );

View File

@ -1,6 +1,6 @@
import Keychain from './keychain'; import Keychain from './keychain';
import { downloadStream } from './api'; import { downloadStream } from './api';
import TransformStream from './transformStream'; import { TStream as TransformStream, wrapReadable } from './streams';
let noSave = false; let noSave = false;
const map = new Map(); 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 decrypted = file.keychain.decryptStream(readStream);
const headers = { const headers = {
@ -38,7 +38,7 @@ async function decryptStream(request) {
'Content-Type': file.type, 'Content-Type': file.type,
'Content-Length': file.size 'Content-Length': file.size
}; };
const body = decrypted.local ? decrypted.nativeReadable : decrypted; const body = decrypted.isPony ? decrypted.toNative() : decrypted;
return new Response(body, { headers }); return new Response(body, { headers });
} catch (e) { } catch (e) {
if (noSave) { if (noSave) {

35
app/streams.js Normal file
View File

@ -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);
}

View File

@ -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);