You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
request.clone() doesn't preserve the original request #308
The purpose of the clone() method is to allow reuse of the (normally single-use) Body of the request. Unfortunately, the implementation in this polyfill doesn't allow that.
To illustrate the problem, this example code will fail:
varr1=newRequest('http://somewhere.com',{method: 'POST',body: 'test'});varr2=r1.clone();r1.text();// --> fails - but should succeedr2.text();// --> succeeds
The clone() method in this polyfill just delegates to the constructor, but the constructor is supposed to consume the stream of the original request (which it does), and the clone() method is supposed to leave it untouched.
Thanks for reporting! Could you look into how native implementations in Chrome and Firefox handle this, and what the whatwg spec recommends in this case, if anything?
It would be great if you opened a PR for this by adding extra tests for these clone() cases.
I had a look. The spec wants a copy of the Request for clone() but without the body which will be copied separately by re-reading the original stream. Since this polyfill is not stream based, a simple re-use of the copy-constructor with a bogus body and then overriding the body produces the same effect.
An alternative would be to clone all properties excluding any body-related ones. I actually implemented that but went away from it because this is a bit cleaner.
I also added an extra test based on the code in this issue.
Let me know what you think.
// Seph
From the spec:
To clone a request request, run these steps:
Let newRequest be a copy of request, except for its body.
If request's body is non-null, set newRequest's body to the result of cloning request's body. Rethrow any exceptions.
Return newRequest.
and
To clone a body body, run these steps:
Let «out1, out2» be the result of teeing body's stream. Rethrow any exceptions.
Set body's stream to out1.
Return a body whose stream is out2 and other members are copied from body.
@scarlac Ah I see that you opened a pull request already. I didn't see a cross-reference here because you referenced this issue number in PR title but not body. (Cross-references are only counted when they are in issue/PR body.)
Activity
wmadden commentedon Apr 5, 2016
It should probably be doing something equivalent to what the
Response.clone()method does: https://github.com/github/fetch/blob/master/fetch.js#L364-L371mislav commentedon Apr 5, 2016
Thanks for reporting! Could you look into how native implementations in Chrome and Firefox handle this, and what the whatwg spec recommends in this case, if anything?
It would be great if you opened a PR for this by adding extra tests for these
clone()cases.Fixes JakeChampion#308. Avoid consuming same body when cloning
scarlac commentedon May 27, 2016
I had a look. The spec wants a copy of the Request for
clone()but without the body which will be copied separately by re-reading the original stream. Since this polyfill is not stream based, a simple re-use of the copy-constructor with a bogus body and then overriding the body produces the same effect.An alternative would be to clone all properties excluding any body-related ones. I actually implemented that but went away from it because this is a bit cleaner.
I also added an extra test based on the code in this issue.
Let me know what you think.
// Seph
From the spec:
and
Chrome 51 produces the following output:
mislav commentedon May 27, 2016
@scarlac Thanks for looking into. I see that you started a commit with some fixes. Care to make this into a pull request? Thanks!
mislav commentedon May 27, 2016
@scarlac Ah I see that you opened a pull request already. I didn't see a cross-reference here because you referenced this issue number in PR title but not body. (Cross-references are only counted when they are in issue/PR body.)
scarlac commentedon Jun 3, 2016
@mislav, Let me know if you need more changes to the PR for it to be acceptable. I'll be happy to refine it further.