From a6796ad8deb096f32512d904c3e3898d2078fada Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 10 Oct 2022 18:43:53 +1100 Subject: [PATCH 1/2] feat(crypto): support ReadableStreams in digest --- crypto/mod.ts | 10 +++++++++- crypto/test.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/crypto/mod.ts b/crypto/mod.ts index c85ee6e936b1..c2623bddd729 100644 --- a/crypto/mod.ts +++ b/crypto/mod.ts @@ -107,9 +107,17 @@ const stdCrypto: StdCrypto = ((x) => x)({ subtle: { ...webCrypto.subtle, + /** + * Polyfills stream support until the Web Crypto API does so: + * @see {@link https://github.com/wintercg/proposal-webcrypto-streams} + */ async digest( algorithm: DigestAlgorithm, - data: BufferSource | AsyncIterable | Iterable, + data: + | BufferSource + | AsyncIterable + | Iterable + | ReadableStream, ): Promise { const { name, length } = normalizeAlgorithm(algorithm); const bytes = bufferSourceBytes(data); diff --git a/crypto/test.ts b/crypto/test.ts index d5282348827d..7ec6b7cabb61 100644 --- a/crypto/test.ts +++ b/crypto/test.ts @@ -25,6 +25,21 @@ Deno.test( expectedDigest, ); + assertEquals( + toHexString( + await stdCrypto.subtle.digest( + "SHA-384", + new ReadableStream({ + start(controller) { + controller.enqueue(inputBytes); + controller.close(); + }, + }), + ), + ), + expectedDigest, + ); + assertEquals( toHexString( await stdCrypto.subtle.digest( @@ -126,6 +141,18 @@ Deno.test("[crypto/digest] Should return an ArrayBuffer", async () => { })(), )) instanceof ArrayBuffer, ); + + assert( + (await stdCrypto.subtle.digest( + "BLAKE3", + new ReadableStream({ + start(controller) { + controller.enqueue(inputBytes); + controller.close(); + }, + }), + )) instanceof ArrayBuffer, + ); }); Deno.test("[crypto/digest] Should not ignore length option", async () => { From a0b93ebb4bfd0258ef044c4e611034c23c64a0bd Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Tue, 18 Oct 2022 05:29:18 +1100 Subject: [PATCH 2/2] remove: ReadableStream as input type for digest --- crypto/mod.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crypto/mod.ts b/crypto/mod.ts index c2623bddd729..280cc6b8b99f 100644 --- a/crypto/mod.ts +++ b/crypto/mod.ts @@ -113,11 +113,7 @@ const stdCrypto: StdCrypto = ((x) => x)({ */ async digest( algorithm: DigestAlgorithm, - data: - | BufferSource - | AsyncIterable - | Iterable - | ReadableStream, + data: BufferSource | AsyncIterable | Iterable, ): Promise { const { name, length } = normalizeAlgorithm(algorithm); const bytes = bufferSourceBytes(data);