From 16e52ee4b000870da8ef570e5182e3d14ca41d03 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Mon, 20 May 2019 17:08:05 -0700 Subject: Move collectUint8Arrays() to util/async.ts, clean up, fix bugs, add tests Original: https://github.com/denoland/deno_std/commit/dcad420b92f79311b386ef524bd1487c04400cc0 --- util/async.ts | 25 +++++++++++++++++++++++++ util/async_test.ts | 43 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 2 deletions(-) (limited to 'util') diff --git a/util/async.ts b/util/async.ts index f9f2477d0..15edb1b42 100644 --- a/util/async.ts +++ b/util/async.ts @@ -83,3 +83,28 @@ export class MuxAsyncIterator implements AsyncIterable { return this.iterate(); } } + +/** Collects all Uint8Arrays from an AsyncIterable and retuns a single + * Uint8Array with the concatenated contents of all the collected arrays. + */ +export async function collectUint8Arrays( + it: AsyncIterable +): Promise { + const chunks = []; + let length = 0; + for await (const chunk of it) { + chunks.push(chunk); + length += chunk.length; + } + if (chunks.length === 1) { + // No need to copy. + return chunks[0]; + } + const collected = new Uint8Array(length); + let offset = 0; + for (let chunk of chunks) { + collected.set(chunk, offset); + offset += chunk.length; + } + return collected; +} diff --git a/util/async_test.ts b/util/async_test.ts index c704002d4..adaac1e22 100644 --- a/util/async_test.ts +++ b/util/async_test.ts @@ -1,7 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. import { test, runIfMain } from "../testing/mod.ts"; -import { assertEquals } from "../testing/asserts.ts"; -import { MuxAsyncIterator, deferred } from "./async.ts"; +import { assert, assertEquals, assertStrictEq } from "../testing/asserts.ts"; +import { collectUint8Arrays, deferred, MuxAsyncIterator } from "./async.ts"; test(async function asyncDeferred(): Promise { const d = deferred(); @@ -31,4 +31,43 @@ test(async function asyncMuxAsyncIterator(): Promise { assertEquals(results.size, 6); }); +test(async function collectUint8Arrays0(): Promise { + async function* gen(): AsyncIterableIterator {} + const result = await collectUint8Arrays(gen()); + assert(result instanceof Uint8Array); + assertEquals(result.length, 0); +}); + +test(async function collectUint8Arrays0(): Promise { + async function* gen(): AsyncIterableIterator {} + const result = await collectUint8Arrays(gen()); + assert(result instanceof Uint8Array); + assertStrictEq(result.length, 0); +}); + +test(async function collectUint8Arrays1(): Promise { + const buf = new Uint8Array([1, 2, 3]); + async function* gen(): AsyncIterableIterator { + yield buf; + } + const result = await collectUint8Arrays(gen()); + assertStrictEq(result, buf); + assertStrictEq(result.length, 3); +}); + +test(async function collectUint8Arrays4(): Promise { + async function* gen(): AsyncIterableIterator { + yield new Uint8Array([1, 2, 3]); + yield new Uint8Array([]); + yield new Uint8Array([4, 5]); + yield new Uint8Array([6]); + } + const result = await collectUint8Arrays(gen()); + assert(result instanceof Uint8Array); + assertStrictEq(result.length, 6); + for (let i = 0; i < 6; i++) { + assertStrictEq(result[i], i + 1); + } +}); + runIfMain(import.meta); -- cgit v1.2.3