summaryrefslogtreecommitdiff
path: root/cli/js/web/body.ts
diff options
context:
space:
mode:
authorMarcos Casagrande <marcoscvp90@gmail.com>2020-07-06 00:05:38 +0200
committerGitHub <noreply@github.com>2020-07-05 18:05:38 -0400
commitf85a0ce6346eda1366e2ddd2c3868b8853cd81cc (patch)
tree424cc3aeea8c6401a9091059f359e544ae07b31a /cli/js/web/body.ts
parent960800888a24938d006dd5a218ae482c2e9cb1d0 (diff)
refactor(cli/body): use Deno.Buffer in bufferFromStream (#6632)
Diffstat (limited to 'cli/js/web/body.ts')
-rw-r--r--cli/js/web/body.ts26
1 files changed, 7 insertions, 19 deletions
diff --git a/cli/js/web/body.ts b/cli/js/web/body.ts
index 8530154b3..35fc5ebbe 100644
--- a/cli/js/web/body.ts
+++ b/cli/js/web/body.ts
@@ -3,6 +3,8 @@ import * as encoding from "./text_encoding.ts";
import * as domTypes from "./dom_types.d.ts";
import { ReadableStreamImpl } from "./streams/readable_stream.ts";
import { isReadableStreamDisturbed } from "./streams/internals.ts";
+import { Buffer } from "../buffer.ts";
+
import {
getHeaderValueParams,
hasHeaderValueOf,
@@ -35,25 +37,11 @@ function validateBodyType(owner: Body, bodySource: BodyInit | null): boolean {
);
}
-function concatenate(arrays: Uint8Array[]): ArrayBuffer {
- let totalLength = 0;
- for (const arr of arrays) {
- totalLength += arr.length;
- }
- const result = new Uint8Array(totalLength);
- let offset = 0;
- for (const arr of arrays) {
- result.set(arr, offset);
- offset += arr.length;
- }
- return result.buffer as ArrayBuffer;
-}
-
async function bufferFromStream(
stream: ReadableStreamReader
): Promise<ArrayBuffer> {
- const parts: Uint8Array[] = [];
const encoder = new TextEncoder();
+ const buffer = new Buffer();
while (true) {
const { done, value } = await stream.read();
@@ -61,11 +49,11 @@ async function bufferFromStream(
if (done) break;
if (typeof value === "string") {
- parts.push(encoder.encode(value));
+ buffer.writeSync(encoder.encode(value));
} else if (value instanceof ArrayBuffer) {
- parts.push(new Uint8Array(value));
+ buffer.writeSync(new Uint8Array(value));
} else if (value instanceof Uint8Array) {
- parts.push(value);
+ buffer.writeSync(value);
} else if (!value) {
// noop for undefined
} else {
@@ -73,7 +61,7 @@ async function bufferFromStream(
}
}
- return concatenate(parts);
+ return buffer.bytes().buffer;
}
export const BodyUsedError =