wormhole-crypto
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -82,7 +82,8 @@ /* eslint-env browser */ | ||
new SliceTransformer(rs - TAG_LENGTH - 1) | ||
) | ||
).readable | ||
return transformStream( | ||
stream, | ||
new ECETransformer(MODE_ENCRYPT, secretKey, rs, salt) | ||
) | ||
).readable | ||
} | ||
@@ -98,7 +99,8 @@ | ||
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 | ||
} | ||
@@ -105,0 +107,0 @@ |
@@ -5,20 +5,43 @@ /* eslint-env browser */ | ||
/** | ||
* 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 resolveDone | ||
let rejectDone | ||
done = new Promise((resolve, reject) => { | ||
resolveDone = resolve | ||
rejectDone = reject | ||
}) | ||
transformedReadable = new ReadableStream(new TransformStreamSource(sourceReadable, transformer, { resolveDone, rejectDone })) | ||
} | ||
// 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, { resolveDone, rejectDone }) { | ||
this.readable = readable | ||
this.transformer = transformer | ||
this.resolveDone = resolveDone | ||
this.rejectDone = rejectDone | ||
this.reader = readable.getReader() | ||
@@ -28,4 +51,9 @@ } | ||
async start (controller) { | ||
if (this.transformer.start) { | ||
return await this.transformer.start(controller) | ||
if (this.transformer?.start) { | ||
try { | ||
await this.transformer.start(controller) | ||
} catch (err) { | ||
this.rejectDone(err) | ||
throw err | ||
} | ||
} | ||
@@ -45,15 +73,21 @@ } | ||
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.resolveDone() | ||
return | ||
} | ||
controller.close() | ||
return | ||
if (this.transformer?.transform) { | ||
await this.transformer.transform(data.value, wrappedController) | ||
} else { | ||
wrappedController.enqueue(data.value) | ||
} | ||
} catch (err) { | ||
this.rejectDone(err) | ||
throw err | ||
} | ||
if (this.transformer.transform) { | ||
await this.transformer.transform(data.value, wrappedController) | ||
} else { | ||
wrappedController.enqueue(data.value) | ||
} | ||
} | ||
@@ -63,4 +97,9 @@ } | ||
async cancel (reason) { | ||
return await this.reader.cancel(reason) | ||
await this.reader.cancel(reason) | ||
if (reason instanceof Error) { | ||
this.rejectDone(reason) | ||
} else { | ||
this.rejectDone(new Error(`stream cancelled; reason: ${reason}`)) | ||
} | ||
} | ||
} |
{ | ||
"name": "wormhole-crypto", | ||
"description": "Streaming encryption for Wormhole.app, based on Encrypted Content-Encoding for HTTP (RFC 8188)", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"author": { | ||
@@ -19,3 +19,3 @@ "name": "Socket Inc", | ||
"airtap-manual": "^1.0.0", | ||
"airtap-sauce": "^1.1.0", | ||
"airtap-sauce": "^1.1.2", | ||
"airtap-system": "^0.1.0", | ||
@@ -22,0 +22,0 @@ "babel-eslint": "^10.1.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27369
606