From 672ed2e7cad9f5eb8577171438c7d6a3ba818e65 Mon Sep 17 00:00:00 2001 From: Anne van Kesteren Date: Wed, 17 Feb 2021 16:13:31 +0100 Subject: [PATCH] Formalize reading bodies Closes #661. --- fetch.bs | 141 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 127 insertions(+), 14 deletions(-) diff --git a/fetch.bs b/fetch.bs index c17629dce..0b5972233 100644 --- a/fetch.bs +++ b/fetch.bs @@ -1037,6 +1037,131 @@ worked on in issue #1156 body. +
+ +

To incrementally read a body body, given an +algorithm processBodyChunk, an algorithm processError, an optional null or +algorithm processEndOfBody (default +null), and an optional null or global object +taskDestination (default null), run these steps: + +

    +
  1. Assert: body's stream is non-null. + +

  2. If taskDestination is null, then set taskDestination to the result of + starting a new parallel queue. + +

  3. +

    Let reader be the result of getting a reader for + body's stream. + +

    This operation cannot throw an exception. + +

  4. Perform the incrementally-read loop given reader, + taskDestination, processBodyChunk, processError, and + processEndOfBody. +

+ +

To perform the incrementally-read loop, given a {{ReadableStreamDefaultReader}} object +reader, parallel queue or global object taskDestination, +algorithm processBodyChunk, algorithm processError, and null or algorithm +processEndOfBody: + +

    +
  1. +

    Let readRequest be the following read request: + +

    +
    chunk steps, given chunk +
    +
      +
    1. Let continueAlgorithm be null. + +

    2. If chunk is not a {{Uint8Array}} object, then set + continueAlgorithm to this step: run processError given a {{TypeError}}. + +

    3. +

      Otherwise: + +

        +
      1. Let bytes be a copy of + chunk. + +

      2. +

        Set continueAlgorithm to these steps: + +

          +
        1. Run processBodyChunk given bytes. + +

        2. Perform the incrementally-read loop given reader, + taskDestination, processBodyChunk, processError, and + processEndOfBody. +

        +
      + +
    4. Queue a fetch task given continueAlgorithm and + taskDestination. +

    + +
    close steps +
    1. If processEndOfBody is non-null, then queue a fetch task given + processEndOfBody and taskDestination.

    + +
    error steps, given e +
    +
      +
    1. Let errorAlgorithm be this step: run processError given + e. + +

    2. Queue a fetch task given errorAlgorithm and + taskDestination. +

    +
    + +
  2. Read a chunk from reader given + readRequest. +

+ +

To fully read a body body, given an algorithm +processBody, an optional algorithm +processError, and an optional null or +global object taskDestination (default null), +run these steps: + +

    +
  1. If taskDestination is null, then set taskDestination to the result of + starting a new parallel queue. + +

  2. Let promise be the result of fully reading body as promise given + body. + +

  3. Let fulfilledSteps given a byte sequence bytes be to + queue a fetch task to run processBody given bytes, with + taskDestination. + +

  4. Let rejectedSteps be to queue a fetch task to run processError, + with taskDestination. + +

  5. React to promise with fulfilledSteps and + rejectedSteps. +

+ +

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

    +
  1. Assert: body's stream is non-null. + +

  2. 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. + +

  3. Return the result of reading all bytes from + reader. +

+ +
+

To handle content codings given codings and bytes, run these steps: @@ -5727,20 +5852,8 @@ the associated steps:

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

  • -

    If object's body is non-null, then: - -

      -
    1. Let stream be object's body's - stream. - -

    2. Let reader be the result of getting a reader from - stream. If that threw an exception, then return a promise rejected with that - exception. - -

    3. Set promise to the result of - reading all bytes from reader. -

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

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