mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
fix(cli/web): Body.bodyUsed should use IsReadableStreamDisturbed
This commit is contained in:
parent
1d3dce9a68
commit
29db4104c4
3 changed files with 40 additions and 1 deletions
|
@ -2,6 +2,7 @@ import * as blob from "./blob.ts";
|
||||||
import * as encoding from "./text_encoding.ts";
|
import * as encoding from "./text_encoding.ts";
|
||||||
import * as domTypes from "./dom_types.d.ts";
|
import * as domTypes from "./dom_types.d.ts";
|
||||||
import { ReadableStreamImpl } from "./streams/readable_stream.ts";
|
import { ReadableStreamImpl } from "./streams/readable_stream.ts";
|
||||||
|
import { isReadableStreamDisturbed } from "./streams/internals.ts";
|
||||||
import { getHeaderValueParams, hasHeaderValueOf } from "./util.ts";
|
import { getHeaderValueParams, hasHeaderValueOf } from "./util.ts";
|
||||||
import { MultipartParser } from "./fetch/multipart.ts";
|
import { MultipartParser } from "./fetch/multipart.ts";
|
||||||
|
|
||||||
|
@ -116,7 +117,7 @@ export class Body implements domTypes.Body {
|
||||||
}
|
}
|
||||||
|
|
||||||
get bodyUsed(): boolean {
|
get bodyUsed(): boolean {
|
||||||
if (this.body && this.body.locked) {
|
if (this.body && isReadableStreamDisturbed(this.body)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -367,6 +367,11 @@ export function isReadableStreamLocked(stream: ReadableStreamImpl): boolean {
|
||||||
return stream[sym.reader] ? true : false;
|
return stream[sym.reader] ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isReadableStreamDisturbed(stream: ReadableStream): boolean {
|
||||||
|
assert(isReadableStream(stream));
|
||||||
|
return stream[sym.disturbed] ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
export function isTransformStream(
|
export function isTransformStream(
|
||||||
x: unknown
|
x: unknown
|
||||||
): x is TransformStreamImpl<any, any> {
|
): x is TransformStreamImpl<any, any> {
|
||||||
|
|
|
@ -100,6 +100,39 @@ unitTest({ perms: { net: true } }, async function fetchBodyUsed(): Promise<
|
||||||
assertEquals(response.bodyUsed, true);
|
assertEquals(response.bodyUsed, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
unitTest(
|
||||||
|
{ perms: { net: true } },
|
||||||
|
async function fetchBodyUsedReader(): Promise<void> {
|
||||||
|
const response = await fetch(
|
||||||
|
"http://localhost:4545/cli/tests/fixture.json"
|
||||||
|
);
|
||||||
|
assert(response.body !== null);
|
||||||
|
|
||||||
|
const reader = response.body.getReader();
|
||||||
|
// Getting a reader should lock the stream but does not consume the body
|
||||||
|
// so bodyUsed should not be true
|
||||||
|
assertEquals(response.bodyUsed, false);
|
||||||
|
reader.releaseLock();
|
||||||
|
await response.json();
|
||||||
|
assertEquals(response.bodyUsed, true);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
unitTest(
|
||||||
|
{ perms: { net: true } },
|
||||||
|
async function fetchBodyUsedCancelStream(): Promise<void> {
|
||||||
|
const response = await fetch(
|
||||||
|
"http://localhost:4545/cli/tests/fixture.json"
|
||||||
|
);
|
||||||
|
assert(response.body !== null);
|
||||||
|
|
||||||
|
assertEquals(response.bodyUsed, false);
|
||||||
|
const promise = response.body.cancel();
|
||||||
|
assertEquals(response.bodyUsed, true);
|
||||||
|
await promise;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
unitTest({ perms: { net: true } }, async function fetchAsyncIterator(): Promise<
|
unitTest({ perms: { net: true } }, async function fetchAsyncIterator(): Promise<
|
||||||
void
|
void
|
||||||
> {
|
> {
|
||||||
|
|
Loading…
Add table
Reference in a new issue