mirror of
https://github.com/denoland/deno.git
synced 2025-01-03 12:58:54 -05:00
parent
0095611af9
commit
cd67f7bdc8
2 changed files with 50 additions and 29 deletions
|
@ -66,6 +66,32 @@
|
|||
return buffer.bytes().buffer;
|
||||
}
|
||||
|
||||
function bodyToArrayBuffer(bodySource) {
|
||||
if (isTypedArray(bodySource)) {
|
||||
return bodySource.buffer;
|
||||
} else if (bodySource instanceof ArrayBuffer) {
|
||||
return bodySource;
|
||||
} else if (typeof bodySource === "string") {
|
||||
const enc = new TextEncoder();
|
||||
return enc.encode(bodySource).buffer;
|
||||
} else if (bodySource instanceof ReadableStream) {
|
||||
throw new Error(
|
||||
`Can't convert stream to ArrayBuffer (try bufferFromStream)`,
|
||||
);
|
||||
} else if (
|
||||
bodySource instanceof FormData ||
|
||||
bodySource instanceof URLSearchParams
|
||||
) {
|
||||
const enc = new TextEncoder();
|
||||
return enc.encode(bodySource.toString()).buffer;
|
||||
} else if (!bodySource) {
|
||||
return null;
|
||||
}
|
||||
throw new Error(
|
||||
`Body type not implemented: ${bodySource.constructor.name}`,
|
||||
);
|
||||
}
|
||||
|
||||
const BodyUsedError =
|
||||
"Failed to execute 'clone' on 'Body': body is already used";
|
||||
|
||||
|
@ -86,18 +112,26 @@
|
|||
return this._stream;
|
||||
}
|
||||
|
||||
if (this._bodySource instanceof ReadableStream) {
|
||||
if (!this._bodySource) {
|
||||
return null;
|
||||
} else if (this._bodySource instanceof ReadableStream) {
|
||||
this._stream = this._bodySource;
|
||||
} else {
|
||||
const buf = bodyToArrayBuffer(this._bodySource);
|
||||
if (!(buf instanceof ArrayBuffer)) {
|
||||
throw new Error(
|
||||
`Expected ArrayBuffer from body`,
|
||||
);
|
||||
}
|
||||
if (typeof this._bodySource === "string") {
|
||||
const bodySource = this._bodySource;
|
||||
|
||||
this._stream = new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(bodySource);
|
||||
controller.enqueue(buf);
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return this._stream;
|
||||
}
|
||||
|
||||
|
@ -172,31 +206,10 @@
|
|||
}
|
||||
|
||||
arrayBuffer() {
|
||||
if (isTypedArray(this._bodySource)) {
|
||||
return Promise.resolve(this._bodySource.buffer);
|
||||
} else if (this._bodySource instanceof ArrayBuffer) {
|
||||
return Promise.resolve(this._bodySource);
|
||||
} else if (typeof this._bodySource === "string") {
|
||||
const enc = new TextEncoder();
|
||||
return Promise.resolve(
|
||||
enc.encode(this._bodySource).buffer,
|
||||
);
|
||||
} else if (this._bodySource instanceof ReadableStream) {
|
||||
if (this._bodySource instanceof ReadableStream) {
|
||||
return bufferFromStream(this._bodySource.getReader(), this.#size);
|
||||
} else if (
|
||||
this._bodySource instanceof FormData ||
|
||||
this._bodySource instanceof URLSearchParams
|
||||
) {
|
||||
const enc = new TextEncoder();
|
||||
return Promise.resolve(
|
||||
enc.encode(this._bodySource.toString()).buffer,
|
||||
);
|
||||
} else if (!this._bodySource) {
|
||||
return Promise.resolve(new ArrayBuffer(0));
|
||||
}
|
||||
throw new Error(
|
||||
`Body type not yet implemented: ${this._bodySource.constructor.name}`,
|
||||
);
|
||||
return bodyToArrayBuffer(this._bodySource);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
import { unitTest, assertEquals, assert } from "./test_util.ts";
|
||||
import {
|
||||
unitTest,
|
||||
assertEquals,
|
||||
assert,
|
||||
} from "./test_util.ts";
|
||||
|
||||
// just a hack to get a body object
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
|
@ -39,6 +43,8 @@ unitTest(
|
|||
const response = await fetch(
|
||||
"http://localhost:4545/multipart_form_data.txt",
|
||||
);
|
||||
assert(response.body instanceof ReadableStream);
|
||||
|
||||
const text = await response.text();
|
||||
|
||||
const body = buildBody(text, response.headers);
|
||||
|
@ -56,6 +62,8 @@ unitTest(
|
|||
const response = await fetch(
|
||||
"http://localhost:4545/cli/tests/subdir/form_urlencoded.txt",
|
||||
);
|
||||
assert(response.body instanceof ReadableStream);
|
||||
|
||||
const text = await response.text();
|
||||
|
||||
const body = buildBody(text, response.headers);
|
||||
|
|
Loading…
Reference in a new issue