Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a promise from transformStream shim #21

Merged
merged 4 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/ece.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ export function encryptStream (
const stream = transformStream(
input,
new SliceTransformer(rs - TAG_LENGTH - 1)
)
).readable

return transformStream(
stream,
new ECETransformer(MODE_ENCRYPT, secretKey, rs, salt)
)
).readable
}

/**
Expand All @@ -94,11 +95,12 @@ export function encryptStream (
* rs: int containing record size, optional
*/
export function decryptStream (input, secretKey, rs = RECORD_SIZE) {
const stream = transformStream(input, new SliceTransformer(HEADER_LENGTH, rs))
const stream = transformStream(input, new SliceTransformer(HEADER_LENGTH, rs)).readable

return transformStream(
stream,
new ECETransformer(MODE_DECRYPT, secretKey, rs)
)
).readable
}

function checkSecretKey (secretKey) {
Expand Down
88 changes: 66 additions & 22 deletions lib/transform-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,62 @@
/* global TransformStream */

/**
* Pipe a readable stream through a transformer. Return the readable end of the
* TransformStream.
* Pipe a readable stream through a transformer. Returns a result, where
* result.readable is the readable end of the TransformStream and
* result.done is a promise that fulfills or rejects once the stream is done.
* Includes a shim for environments where TransformStream is not available.
*/
export function transformStream (readable, transformer) {
// Chrome, Edge, Safari TP
export function transformStream (sourceReadable, transformer) {
let transformedReadable
let done

if (typeof TransformStream !== 'undefined') {
return readable.pipeThrough(new TransformStream(transformer))
// Chrome, Edge, Safari 14.1+
const transform = new TransformStream(transformer)

done = sourceReadable.pipeTo(transform.writable)
transformedReadable = transform.readable
} else {
// Firefox, Safari 14 and older
let doneCb = null

done = new Promise((resolve, reject) => {
doneCb = (err) => {
if (err) {
reject(err)
} else {
resolve()
}
}
})
transformedReadable = new ReadableStream(new TransformStreamSource(sourceReadable, transformer, doneCb))
jhiesey marked this conversation as resolved.
Show resolved Hide resolved
}

// Firefox, Safari 14 and older
return new ReadableStream(new TransformStreamSource(readable, transformer))
// Ensure the caller doesn't need to catch errors
done.catch(() => {})

return {
readable: transformedReadable,
done
}
}

class TransformStreamSource {
constructor (readable, transformer) {
constructor (readable, transformer, doneCb) {
this.readable = readable
this.transformer = transformer
this.doneCb = doneCb
this.reader = readable.getReader()
}

async start (controller) {
if (this.transformer.start) {
return await this.transformer.start(controller)
if (this.transformer?.start) {
try {
return await this.transformer.start(controller)
jhiesey marked this conversation as resolved.
Show resolved Hide resolved
} catch (err) {
this.doneCb(err)
throw err
}
}
}

Expand All @@ -40,23 +72,35 @@ class TransformStreamSource {

// eslint-disable-next-line no-unmodified-loop-condition
while (!enqueued) {
const data = await this.reader.read()
if (data.done) {
if (this.transformer.flush) {
await this.transformer.flush(controller)
try {
const data = await this.reader.read()
if (data.done) {
if (this.transformer?.flush) {
await this.transformer.flush(controller)
}
controller.close()
this.doneCb(null)
return
}
controller.close()
return
}
if (this.transformer.transform) {
await this.transformer.transform(data.value, wrappedController)
} else {
wrappedController.enqueue(data.value)
if (this.transformer?.transform) {
await this.transformer.transform(data.value, wrappedController)
} else {
wrappedController.enqueue(data.value)
}
} catch (err) {
this.doneCb(err)
throw err
}
}
}

async cancel (reason) {
return await this.reader.cancel(reason)
await this.reader.cancel(reason)
if (reason instanceof Error) {
this.doneCb(reason)
} else {
this.doneCb(new Error(`stream cancelled; reason: ${reason}`))
}
return reason
jhiesey marked this conversation as resolved.
Show resolved Hide resolved
}
}