Skip to content

Commit ad40a47

Browse files
committed
Follow spec and fix tests
1 parent cbda72c commit ad40a47

File tree

6 files changed

+44
-75
lines changed

6 files changed

+44
-75
lines changed

src/Body.js

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Body {
66
this._bodyInit = body;
77

88
if (!body) {
9+
this._bodyNull = true;
910
this._bodyText = "";
1011
return this;
1112
}
@@ -179,6 +180,10 @@ class Body {
179180
}
180181

181182
get body() {
183+
if (this._bodyNull) {
184+
return null;
185+
}
186+
182187
if (this._bodyReadableStream) {
183188
return this._bodyReadableStream;
184189
}
@@ -228,6 +233,16 @@ class Body {
228233
},
229234
});
230235
}
236+
237+
clone() {
238+
if (this._bodyReadableStream) {
239+
const [stream1, stream2] = this._bodyReadableStream.tee();
240+
this._bodyReadableStream = stream1;
241+
return new Body(stream2);
242+
} else {
243+
return new Body(this._bodyInit);
244+
}
245+
}
231246
}
232247

233248
export default Body;

src/Fetch.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import Request from "./Request";
44
import Response from "./Response";
55
import BlobResponse from "./BlobResponse";
66
import ArrayBufferResponse from "./ArrayBufferResponse";
7-
import StreamControllersController from "./StreamControllersController";
87

98
class AbortError extends Error {
109
constructor() {
@@ -14,6 +13,22 @@ class AbortError extends Error {
1413
}
1514
}
1615

16+
function createStream(cancel) {
17+
let streamController;
18+
19+
const stream = new ReadableStream({
20+
start(controller) {
21+
streamController = controller;
22+
},
23+
cancel,
24+
});
25+
26+
return {
27+
stream,
28+
streamController,
29+
};
30+
}
31+
1732
class Fetch {
1833
_nativeNetworkSubscriptions = new Set();
1934
_nativeResponseType = "blob";
@@ -124,11 +139,9 @@ class Fetch {
124139
return;
125140
}
126141

127-
const streamController = new StreamControllersController({
128-
cancelRequest: () => {
129-
this.__clearNetworkSubscriptions();
130-
Networking.abortRequest(this._requestId);
131-
},
142+
const { stream, streamController } = createStream(() => {
143+
this.__clearNetworkSubscriptions();
144+
Networking.abortRequest(this._requestId);
132145
});
133146

134147
this._streamController = streamController;
@@ -137,7 +150,7 @@ class Fetch {
137150
this._responseUrl = url;
138151

139152
if (this._nativeResponseType === "text") {
140-
this._response = new Response(streamController, {
153+
this._response = new Response(stream, {
141154
status,
142155
headers,
143156
url,

src/Response.js

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import Body from "./Body";
22
import Headers from "./Headers";
3-
import StreamControllersController from "./StreamControllersController";
4-
import { createStream } from "./utils";
53

64
class Response {
75
constructor(bodyInit, options = {}) {
@@ -11,16 +9,7 @@ class Response {
119
this.statusText = options.statusText ?? "";
1210
this.headers = new Headers(options.headers);
1311
this.url = options.url ?? "";
14-
this._bodyInit = bodyInit;
15-
if (bodyInit instanceof StreamControllersController) {
16-
const { stream, streamController } = createStream(() => {
17-
bodyInit.cancelRequest();
18-
});
19-
bodyInit.registerController(streamController);
20-
this._body = new Body(stream);
21-
} else {
22-
this._body = new Body(bodyInit);
23-
}
12+
this._body = new Body(bodyInit);
2413

2514
if (!this.headers.has("content-type") && this._body._mimeType) {
2615
this.headers.set("content-type", this._body._mimeType);
@@ -32,12 +21,14 @@ class Response {
3221
}
3322

3423
clone() {
35-
return new Response(this._bodyInit, {
24+
const newResponse = new Response(null, {
3625
status: this.status,
3726
statusText: this.statusText,
3827
headers: new Headers(this.headers),
3928
url: this.url,
4029
});
30+
newResponse._body = this._body.clone();
31+
return newResponse;
4132
}
4233

4334
blob() {

src/StreamControllersController.js

-34
This file was deleted.

src/utils.js

+1-17
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@ function createBlobReader(blob) {
2121
};
2222
}
2323

24-
function createStream(cancel) {
25-
let streamController;
26-
27-
const stream = new ReadableStream({
28-
start(controller) {
29-
streamController = controller;
30-
},
31-
cancel,
32-
});
33-
34-
return {
35-
stream,
36-
streamController,
37-
};
38-
}
39-
4024
async function drainStream(stream) {
4125
const chunks = [];
4226
const reader = stream.getReader();
@@ -67,4 +51,4 @@ function readArrayBufferAsText(array) {
6751
return decoder.decode(array);
6852
}
6953

70-
export { createBlobReader, createStream, drainStream, readArrayBufferAsText };
54+
export { createBlobReader, drainStream, readArrayBufferAsText };

test/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ test("response", (t) => {
10151015
}
10161016
);
10171017

1018-
t.skip("consume request body as stream when input is stream", async (t) => {
1018+
t.test("consume request body as stream when input is stream", async (t) => {
10191019
const rs = new ReadableStream({
10201020
async pull(c) {
10211021
await delay(100);
@@ -1033,7 +1033,7 @@ test("response", (t) => {
10331033
t.eq(text, "Hello world!");
10341034
});
10351035

1036-
t.skip("consume request body as text when input is stream", async (t) => {
1036+
t.test("consume request body as text when input is stream", async (t) => {
10371037
const rs = new ReadableStream({
10381038
async pull(c) {
10391039
await delay(100);
@@ -1070,7 +1070,7 @@ test("response", (t) => {
10701070
t.eq(text, "Hello world!");
10711071
});
10721072

1073-
t.skip(
1073+
t.test(
10741074
"consume request body as ArrayBuffer when input is stream",
10751075
async (t) => {
10761076
const rs = new ReadableStream({
@@ -1858,7 +1858,7 @@ test("fetch method", (t) => {
18581858
});
18591859

18601860
t.test("cloning", (t) => {
1861-
t.skip("cloning response from text stream", async (t) => {
1861+
t.test("cloning response from text stream", async (t) => {
18621862
const url = new URL("/stream", BASE_URL);
18631863
const res = await fetch(url, {
18641864
reactNative: { textStreaming: true },

0 commit comments

Comments
 (0)