From 464326e8eb6a602122c030cd40042480a3c0e265 Mon Sep 17 00:00:00 2001 From: Noam Rosenthal Date: Thu, 22 Dec 2022 16:15:41 +0200 Subject: [PATCH] Editorial: stop using promises in internal algorithms They are not designed for that and don't work well (e.g., resolve expects a JS value). Follows https://github.com/whatwg/streams/pull/1250 and addresses the Fetch part of https://github.com/whatwg/streams/issues/1178. Closes #1568. Co-authored-by: Domenic Denicola Co-authored-by: Anne van Kesteren --- fetch.bs | 157 +++++++++++++++++++++++++++---------------------------- 1 file changed, 78 insertions(+), 79 deletions(-) diff --git a/fetch.bs b/fetch.bs index 36f4d8986..d501ee41f 100644 --- a/fetch.bs +++ b/fetch.bs @@ -1465,32 +1465,19 @@ null), run these steps. processBody must be an algorithm accepting a
  • If taskDestination is null, then set taskDestination to the result of starting a new parallel queue. -

  • Let promise be the result of fully reading body as promise given - body. - -

  • Let fulfilledSteps given a byte sequence bytes be to +

  • Let successSteps given a byte sequence bytes be to queue a fetch task to run processBody given bytes, with taskDestination. -

  • Let rejectedSteps be to queue a fetch task to run +

  • Let errorSteps be to queue a fetch task to run processBodyError, with taskDestination. -

  • React to promise with fulfilledSteps and - rejectedSteps. - - - -

    -

    To fully read body as promise, given a -body body, run these steps: - -

    1. Let reader be the result of getting a reader for - body's stream. If that threw an exception, then return - a promise rejected with that exception. + body's stream. If that threw an exception, then run + errorSteps with that exception and return. -

    2. Return the result of reading all bytes from - reader. +

    3. Read all bytes from + reader, given successSteps and errorSteps.

    @@ -6886,38 +6873,78 @@ returns failure or a MIME type.
    +

    The body getter steps are to return null if this's body is null; otherwise this's body's stream. +

    +

    The bodyUsed getter steps are to return true if this's body is non-null and this's body's stream is disturbed; otherwise false. +

    -

    The package data algorithm, given -bytes, type, and a mimeType, switches on type, and runs -the associated steps: +

    +

    The consume body +algorithm, given an object that includes {{Body}} object and an algorithm that takes a +byte sequence and returns a JavaScript value or throws an exception +convertBytesToJSValue, runs these steps: -

    -
    ArrayBuffer -
    -

    Return a new {{ArrayBuffer}} whose contents are bytes. +

      +
    1. If object is unusable, then return a promise rejected with + a {{TypeError}}. -

      Allocating an {{ArrayBuffer}} can throw a {{RangeError}}. +

    2. Let promise be a new promise. -

      Blob -

      Return a {{Blob}} whose contents are bytes and {{Blob/type}} attribute is - mimeType. +

    3. Let errorSteps given error be to reject promise with + error. -
      FormData -
      -

      If mimeType's essence is "multipart/form-data", - then: +

    4. Let successSteps given a byte sequence data be to + resolve promise with the result of running convertBytesToJSValue + with data. If that threw an exception, then run errorSteps with that + exception. + +
    5. If object's body is null, then run successSteps + with an empty byte sequence. + +

    6. Otherwise, fully read object's body given + successSteps, errorSteps, and object's + relevant global object. + +

    7. Return promise. +

    +
    + +
    +

    The arrayBuffer() method steps are to return the result +of running consume body with this and the following step given a +byte sequence bytes: return a new {{ArrayBuffer}} whose contents are +bytes. + +

    The above method can reject with a {{RangeError}}. +

    +
    +

    The blob() method steps are to return the result +of running consume body with this and the following step given a +byte sequence bytes: return a {{Blob}} whose contents are bytes +and whose {{Blob/type}} attribute is this's MIME type. +

    + +
    +

    The formData() method steps are to return the result of +running consume body with this and the following step given a +byte sequence bytes: switch on this's MIME type's +essence and run the corresponding steps: + +

    +
    "multipart/form-data", +
    1. Parse bytes, using the value of the `boundary` parameter from - mimeType, per the rules set forth in + this's MIME type, per the rules set forth in Returning Values from Forms: multipart/form-data. [[!RFC7578]]

      Each part whose `Content-Disposition` header contains a `filename` @@ -6950,12 +6977,11 @@ the associated steps: `multipart/form-data`, a more detailed parsing specification is to be written. Volunteers welcome. -

      Otherwise, if mimeType's essence is - "application/x-www-form-urlencoded", then: - +

      "application/x-www-form-urlencoded", +
        -
      1. Let entries be the result of - parsing bytes. +

      2. Let entries be the result of parsing + bytes.

      3. If entries is failure, then throw a {{TypeError}}. @@ -6963,49 +6989,22 @@ the associated steps: entries.

      -

      Otherwise, throw a {{TypeError}}. - -

      JSON -

      Return the result of running parse JSON from bytes on bytes. - -

      text -

      Return the result of running UTF-8 decode on - bytes. +

      Otherwise +

      Throw a {{TypeError}}.

    +
    -

    The consume body algorithm, given an -object and type, runs these steps: - -

      -
    1. If object is unusable, then return a promise rejected with - a {{TypeError}}. - -

    2. Let promise be a promise resolved with an empty - byte sequence. - -

    3. If object's body is non-null, then set promise to the - result of fully reading body as promise given object's body. - -

    4. Let steps be to return the result of package data with the first argument - given, type, and object's MIME type. - -

    5. Return the result of upon fulfillment of promise given steps. -

    - -

    The arrayBuffer() method steps are to return the result -of running consume body with this and ArrayBuffer. - -

    The blob() method steps are to return the result of -running consume body with this and Blob. - -

    The formData() method steps are to return the result of -running consume body with this and FormData. +

    +

    The json() method steps are to return the result +of running consume body with this and parse JSON from bytes. -

    The json() method steps are to return the result of -running consume body with this and JSON. +

    The above method can reject with a {{SyntaxError}}. +

    -

    The text() method steps are to return the result of -running consume body with this and text. +

    +

    The text() method steps are to return the result +of running consume body with this and UTF-8 decode. +

    Request class