Skip to content

request.clone() doesn't preserve the original request #308

Closed

Description

@wmadden

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:

var r1 = new Request('http://somewhere.com', { method: 'POST', body: 'test' });
var r2 = r1.clone();
r1.text(); // --> fails - but should succeed
r2.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.

Activity

  1. wmadden commented on Apr 5, 2016

    @wmadden
    Author

    It should probably be doing something equivalent to what the Response.clone() method does: https://github.com/github/fetch/blob/master/fetch.js#L364-L371

  2. mislav commented on Apr 5, 2016

    @mislav
    Contributor

    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.

  3. added a commit that references this issue on May 27, 2016
    35ed4c3
  4. scarlac commented on May 27, 2016

    @scarlac
    Contributor

    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.

    Chrome 51 produces the following output:

    > var r1 = new Request('http://somewhere.com', { method: 'POST', body: 'test' });
    <· undefined
    > var r2 = r1.clone()
    <· undefined
    > r1.text();
    <· Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
    > r2.text()
    <· Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}

  5. mislav commented on May 27, 2016

    @mislav
    Contributor

    @scarlac Thanks for looking into. I see that you started a commit with some fixes. Care to make this into a pull request? Thanks!

  6. mislav commented on May 27, 2016

    @mislav
    Contributor

    @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.)

  7. scarlac commented on Jun 3, 2016

    @scarlac
    Contributor

    @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.

  8. locked as resolved and limited conversation to collaborators on Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions