From 4561d5f4b2462e2bb5f91668504a90fc5a725786 Mon Sep 17 00:00:00 2001 From: Noam Rosenthal Date: Mon, 14 Mar 2022 14:19:32 +0200 Subject: [PATCH] Return a fetch controller from the fetch algorithm This allows callers to perform certain actions on an ongoing fetch. Initially to abort or terminate it, and later on to conclude it. This will eventually remove the need of using response as the object that retains timing info, as that creates certain ambiguities. Corresponding Service Worker PR: https://github.com/w3c/ServiceWorker/pull/1620. Follow-up: #1403 and #1412. --- fetch.bs | 205 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 112 insertions(+), 93 deletions(-) diff --git a/fetch.bs b/fetch.bs index 0db48e92b..bd8fae92f 100644 --- a/fetch.bs +++ b/fetch.bs @@ -210,10 +210,38 @@ lt="authentication entry">authentication entries (for HTTP authentication).
cross-origin isolated capability (default false)
A boolean. +
controller (default a new fetch controller) +
A fetch controller. +
timing info
A fetch timing info. +

A fetch controller is a struct used to enable callers of +fetch to perform certain operations on it after it has started. It has the following +items: + +

+
state (default "ongoing") +
"ongoing", "terminated", or "aborted" +
+ +

To abort a fetch controller +controller, set controller's state to +"aborted". + +

To terminate a fetch controller +controller, set controller's state to +"terminated". + +

A fetch params fetchParams is aborted if +its controller's state is +"aborted". + +

A fetch params fetchParams is canceled if +its controller's state is +"aborted" or "terminated". +

A fetch timing info is a struct used to maintain timing information needed by Resource Timing and Navigation Timing. It has the following items: [[RESOURCE-TIMING]] [[NAVIGATION-TIMING]] @@ -2104,6 +2132,16 @@ known as an aborted network errorheader list is always empty, and body is always null. +

To create the appropriate network error given fetch params +fetchParams: + +

    +
  1. Assert: fetchParams is canceled. + +

  2. Return an aborted network error if fetchParams is + aborted; otherwise return a network error. +

+

A filtered response is a limited view on a @@ -2285,16 +2323,18 @@ functionality. request).

A fetch record has an associated -fetch (a -fetch algorithm or null). +controller (a +fetch controller or null).


When a fetch group is terminated, for each associated -fetch record whose request's done flag is -unset or keepalive is false, terminate the -fetch record's fetch. +fetch record whose fetch record's +controller is not null, and whose request's +done flag is unset or keepalive is false, +terminate the fetch record's +controller.

Resolving domains

@@ -3757,10 +3797,6 @@ an integer representing the number of bytes transmitted. If given, given, processResponseConsumeBody must be an algorithm accepting a response and null, failure, or a byte sequence. -

An ongoing fetch can be -terminated with flag aborted, -which is unset unless otherwise specified. -

The user agent may be asked to suspend the ongoing fetch. The user agent may either accept or ignore the suspension request. The suspended fetch can be @@ -3929,14 +3965,17 @@ the request.

If request is a subresource request, then:

    -
  1. Let record be a new fetch record consisting of - request and this instance of the fetch algorithm. +

  2. Let record be a new fetch record whose + request is request and controller + is fetchParams's controller.

  3. Append record to request's client's fetch group list of fetch records.

  • Run main fetch given fetchParams. + +

  • Return fetchParams's controller. @@ -4394,7 +4433,8 @@ steps:

    1. -

      Run these steps, but abort when the ongoing fetch is terminated: +

      Run these steps, but abort when fetchParams is + canceled:

      1. Let blob be request's current URL's @@ -4425,16 +4465,8 @@ steps:

      2. Return response.

      -
    2. -

      If aborted, then: - -

        -
      1. Let aborted be the termination's aborted flag. - -

      2. If aborted is set, then return an aborted network error. - -

      3. Return a network error. -

      +
    3. If aborted, then return the appropriate network error for + fetchParams.

    "data" @@ -4501,10 +4533,12 @@ these steps:

    Let transformAlgorithm given chunk be these steps:

      -
    1. If the ongoing fetch is terminated, then abort these steps. +

    2. If fetchParams is canceled, then abort these + steps.

    3. If chunk is not a {{Uint8Array}} object, then - terminate the ongoing fetch. + terminate fetchParams's + controller.

    4. Otherwise, enqueue chunk. The user agent may split the chunk into implementation-defined practical sizes and @@ -4528,7 +4562,9 @@ these steps: given fetchParams's cross-origin isolated capability.

    5. Set response to the result of invoking handle fetch for - requestForServiceWorker. [[!HTML]] [[!SW]] + requestForServiceWorker, with fetchParams's + controller and fetchParams's + cross-origin isolated capability. [[!HTML]] [[!SW]]

    6. If response is not null, then: @@ -4833,7 +4869,8 @@ steps. They return a response.

    7. Let the revalidatingFlag be unset.

    8. -

      Run these steps, but abort when the ongoing fetch is terminated: +

      Run these steps, but abort when fetchParams is + canceled:

      1. If request's window is "no-window" and @@ -5167,16 +5204,9 @@ steps. They return a response.

    -
  • -

    If aborted, then: - -

      -
    1. Let aborted be the termination's aborted flag. - -

    2. If aborted is set, then return an aborted network error. +

    3. If aborted, then return the appropriate network error for + fetchParams. -

    4. Return a network error. -

  • @@ -5272,16 +5302,8 @@ steps. They return a response. isAuthenticationFetch is true, then:
      -
    1. -

      If the ongoing fetch is terminated, then: - -

        -
      1. Let aborted be the termination's aborted flag. - -

      2. If aborted is set, then return an aborted network error. - -

      3. Return a network error. -

      +
    2. If fetchParams is canceled, then return the + appropriate network error for fetchParams.

    3. Let username and password be the result of prompting the end user for a username and password, respectively, in request's @@ -5308,16 +5330,8 @@ steps. They return a response.

    4. Needs testing: multiple `Proxy-Authenticate` headers, missing, parsing issues. -

    5. -

      If the ongoing fetch is terminated, then: - -

        -
      1. Let aborted be the termination's aborted flag. - -

      2. If aborted is set, then return an aborted network error. - -

      3. Return a network error. -

      +
    6. If fetchParams is canceled, then return the + appropriate network error for fetchParams.

    7. Prompt the end user as appropriate in request's @@ -5346,16 +5360,8 @@ steps. They return a response.

      then:

        -
      1. -

        If the ongoing fetch is terminated, then: - -

          -
        1. Let aborted be the termination's aborted flag. - -

        2. If aborted is set, then return an aborted network error. - -

        3. Return a network error. -

        +
      2. If fetchParams is canceled, then return the + appropriate network error for fetchParams.

      3. Set response to the result of running HTTP-network-or-cache fetch given fetchParams, isAuthenticationFetch, and true. @@ -5412,7 +5418,8 @@ optional boolean forceNewConnection (default false), run these steps:

      4. -

        Run these steps, but abort when the ongoing fetch is terminated: +

        Run these steps, but abort when fetchParams is + canceled:

        1. If connection is failure, then return a network error. @@ -5514,7 +5521,8 @@ optional boolean forceNewConnection (default false), run these steps:

          Let processBodyChunk given bytes be these steps:

            -
          1. If the ongoing fetch is terminated, then abort these steps. +

          2. If fetchParams is canceled, then abort these + steps.

          3. Run this step in parallel: transmit bytes. @@ -5528,7 +5536,8 @@ optional boolean forceNewConnection (default false), run these steps:

            Let processEndOfBody be these steps:

              -
            1. If the ongoing fetch is terminated, then abort these steps. +

            2. If fetchParams is canceled, then abort these + steps.

            3. If fetchParams's process request end-of-body is non-null, then run fetchParams's @@ -5539,12 +5548,15 @@ optional boolean forceNewConnection (default false), run these steps:

              Let processBodyError given e be these steps:

                -
              1. If the ongoing fetch is terminated, then abort these steps. +

              2. If fetchParams is canceled, then abort these + steps.

              3. If e is an "AbortError" {{DOMException}}, - then terminate the ongoing fetch with the aborted flag set. + then abort fetchParams's + controller. -

              4. Otherwise, terminate the ongoing fetch. +

              5. Otherwise, terminate fetchParams's + controller.

            4. Incrementally read request's body given @@ -5561,20 +5573,16 @@ optional boolean forceNewConnection (default false), run these steps:

              If aborted, then:

                -
              1. Let aborted be the termination's aborted flag. -

              2. If connection uses HTTP/2, then transmit an RST_STREAM frame. -

              3. If aborted is set, then return an aborted network error. - -

              4. Return a network error. +

              5. Return the appropriate network error for fetchParams.

              -
            5. Let pullAlgorithm be an action that resumes the +

            6. Let pullAlgorithm be an algorithm that resumes the ongoing fetch if it is suspended. -

            7. Let cancelAlgorithm be an action that terminates - the ongoing fetch with the aborted flag set. +

            8. Let cancelAlgorithm be an algorithm that aborts + fetchParams's controller.

            9. Let highWaterMark be a non-negative, non-NaN number, chosen by the user agent. @@ -5590,7 +5598,8 @@ optional boolean forceNewConnection (default false), run these steps: sizeAlgorithm set to sizeAlgorithm.

            10. -

              Run these steps, but abort when the ongoing fetch is terminated: +

              Run these steps, but abort when fetchParams is + canceled:

              1. Set response's body to a new @@ -5619,10 +5628,8 @@ optional boolean forceNewConnection (default false), run these steps:

                If aborted, then:

                  -
                1. Let aborted be the termination's aborted flag. - -

                2. If aborted is set, then set response's - aborted flag. +

                3. If fetchParams is aborted, then set + response's aborted flag.

                4. Return response.

                @@ -5632,7 +5639,8 @@ optional boolean forceNewConnection (default false), run these steps:
                1. -

                  Run these steps, but abort when the ongoing fetch is terminated: +

                  Run these steps, but abort when fetchParams is + canceled:

                  1. @@ -5661,14 +5669,15 @@ optional boolean forceNewConnection (default false), run these steps:
                  2. Increase timingInfo's decoded body size by bytes's length. -

                  3. If bytes is failure, then terminate the - ongoing fetch. +

                  4. If bytes is failure, then terminate + fetchParams's controller.

                  5. Enqueue a {{Uint8Array}} wrapping an {{ArrayBuffer}} containing bytes into stream.

                  6. If stream is errored, then - terminate the ongoing fetch. + terminate fetchParams.'s + controller.

                  7. If stream doesn't need more data ask the user agent to suspend the ongoing fetch. @@ -5684,10 +5693,9 @@ optional boolean forceNewConnection (default false), run these steps:

                    If aborted, then:

                      -
                    1. Let aborted be the termination's aborted flag.

                    2. -

                      If aborted is set, then: +

                      If fetchParams is aborted, then:

                      1. Set response's aborted flag. @@ -7613,6 +7621,8 @@ method steps are:

                        This lets us reject promises with predictable timing, when the request to abort comes from the same thread as the call to fetch. +

                      2. Let controller be null. +

                      3. Add the following abort steps to requestObject's signal: @@ -7622,7 +7632,8 @@ method steps are:

                      4. Abort fetch with p, request, and responseObject. -

                      5. Terminate the ongoing fetch with the aborted flag set. +

                      6. If controller is not null, then abort + controller.

                    3. Let handleFetchDone given response response be to @@ -7630,7 +7641,8 @@ method steps are: "fetch".

                    4. -

                      Fetch request with processResponseEndOfBody set to +

                      Set controller to the result of calling fetch given + request, with processResponseEndOfBody set to handleFetchDone, and processResponse given response being these substeps: @@ -7680,7 +7692,7 @@ method steps are:

                      Garbage collection

                      -

                      The user agent may terminate an ongoing fetch if that termination +

                      The user agent may terminate an ongoing fetch if that termination is not observable through script.

                      "Observable through script" means observable through @@ -8046,6 +8058,7 @@ resource — for non-CORS requests as well as CORS requests — and do not use `Vary`. +

                      Using fetch in other standards

                      In its essence fetching is an exchange of a request for a @@ -8114,6 +8127,12 @@ in a parallel queue if useParallelQueue is as arguments will be invoked. Hopefully most standards will not need this. +

                      When invoked, the fetch operation returns a fetch controller. The +controller is used for performing actions on a fetch operation that has already started, such as +aborting the operation by the user or page logic, or +terminating it due to a browser-internal circumstance. + +

                      Acknowledgments