Skip to content

Support cloning of Responses #185

Closed
Closed

Description

@kirill-konshin

According to MDN https://developer.mozilla.org/en-US/docs/Web/API/Response/clone and the spec https://fetch.spec.whatwg.org/#dom-response-clone Response class should have a clone() method, which seems to be missing now.

Activity

  1. mislav commented on Jul 31, 2015

    @mislav
    Contributor

    Good catch. Care to make a pull request?

  2. kirill-konshin commented on Jul 31, 2015

    @kirill-konshin
    ContributorAuthor

    Cloning of Headers is trivial -- just call new Headers(this.headers), primitives can be easily cloned as well, the biggest question is how to clone body's Blob or FormData instances and whether it is needed.

    It seems that Blob can be cloned by calling blob.slice(blob.size) which returns a new blob. But readBlobAs* functions in polyfill can read same blob multiple times and since blobs are immutable, it should be safe to just pass the same blob as new body.

    From what I see in https://developer.mozilla.org/en-US/docs/Web/API/FormData there is no way to get keys/values and iterate through them filling a new FormData instance with values... But based on implementation nuances and lack of ability to actually read FormData I think it's fine to pass the same instance of FormData body too.

    The new Response will have bodyUsed = false anyway, so even if source Response has been read already, new one will still be readable...

    If you agree with passing non-cloned bodies then I can make a PR in the next few days.

    P.S. Method Body.formData() should resolve with a FormData object, not with a text:

    this.formData = function() {
        return this.text().then(decode)
    }

    In polyfill text() with FormData body will throw an exception anyway. I should probably file a separate bug for that.

  3. mislav commented on Jul 31, 2015

    @mislav
    Contributor

    Method Body.formData() should resolve with a FormData object, not with a text:

    Thanks for looking into this. One thing that baffles me is that Chrome's fetch implementation doesn't have the formData() method for some reason.

  4. kirill-konshin commented on Aug 1, 2015

    @kirill-konshin
    ContributorAuthor

    I suppose that Body.formData() is not implemented because FormData is totally useless so far. You can write but you cannot read.

    I'll make a PR.

  5. added a commit that references this issue on Sep 29, 2015
  6. kirill-konshin commented on Sep 29, 2015

    @kirill-konshin
    ContributorAuthor

    After quite a while I finally had time to implement PR: #209

  7. locked as resolved and limited conversation to collaborators on Oct 8, 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