Skip to content

Commit 8a06df0

Browse files
committed
More updates to follow spec
1 parent e37e4e3 commit 8a06df0

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/Request.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Request {
4646
this.signal = request.signal;
4747
this.headers = new Headers(options.headers ?? request.headers);
4848

49-
if (!options.body && request._body._bodyInit) {
49+
if (options.body === undefined && request._body._bodyInit) {
5050
this._body = new Body(request._body._bodyInit);
5151
request._body.bodyUsed = true;
5252
}
@@ -85,7 +85,14 @@ class Request {
8585
}
8686

8787
clone() {
88-
return new Request(this, { body: this._body._bodyInit });
88+
if (this.bodyUsed) {
89+
throw new TypeError("Already read");
90+
}
91+
92+
const newRequest = new Request(this, { body: null });
93+
newRequest._body = this._body.clone();
94+
95+
return newRequest;
8996
}
9097

9198
blob() {

src/Response.js

+5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ class Response {
2121
}
2222

2323
clone() {
24+
if (this.bodyUsed) {
25+
throw new TypeError("Already read");
26+
}
27+
2428
const newResponse = new Response(null, {
2529
status: this.status,
2630
statusText: this.statusText,
2731
headers: new Headers(this.headers),
2832
url: this.url,
2933
});
3034
newResponse._body = this._body.clone();
35+
3136
return newResponse;
3237
}
3338

test/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ test("request", (t) => {
553553
t.isNot(clone.headers, req.headers);
554554
t.notOk(req.bodyUsed);
555555

556-
const bodies = await Promise.all([clone.text(), req.clone().text()]);
556+
const bodies = await Promise.all([clone.text(), req.text()]);
557557

558558
t.eq(bodies, ["I work out", "I work out"]);
559559
});

0 commit comments

Comments
 (0)