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 --- http/server.ts | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) (limited to 'http') diff --git a/http/server.ts b/http/server.ts index 9ca6c4a89..da335a58c 100644 --- a/http/server.ts +++ b/http/server.ts @@ -8,7 +8,12 @@ import { BufReader, BufState, BufWriter } from "../io/bufio.ts"; import { TextProtoReader } from "../textproto/mod.ts"; import { STATUS_TEXT } from "./http_status.ts"; import { assert, fail } from "../testing/asserts.ts"; -import { deferred, Deferred, MuxAsyncIterator } from "../util/async.ts"; +import { + collectUint8Arrays, + deferred, + Deferred, + MuxAsyncIterator +} from "../util/async.ts"; function bufWriter(w: Writer): BufWriter { if (w instanceof BufWriter) { @@ -94,28 +99,6 @@ export async function writeResponse(w: Writer, r: Response): Promise { await writer.flush(); } -async function readAllIterator( - it: AsyncIterableIterator -): Promise { - const chunks = []; - let len = 0; - for await (const chunk of it) { - chunks.push(chunk); - len += chunk.length; - } - if (chunks.length === 0) { - // No need for copy - return chunks[0]; - } - const collected = new Uint8Array(len); - let offset = 0; - for (let chunk of chunks) { - collected.set(chunk, offset); - offset += chunk.length; - } - return collected; -} - export class ServerRequest { url: string; method: string; @@ -203,7 +186,7 @@ export class ServerRequest { // Read the body of the request into a single Uint8Array public async body(): Promise { - return readAllIterator(this.bodyStream()); + return collectUint8Arrays(this.bodyStream()); } async respond(r: Response): Promise { -- cgit v1.2.3