Skip to content

Commit

Permalink
Add set up the request/validate the response hooks to script fetching
Browse files Browse the repository at this point in the history
This allows more complicated callers, like service workers, to
customize script fetching as necessary. See [1], and in particular [2].

[1]: w3c/ServiceWorker#831
[2]: w3c/ServiceWorker#831 (comment)
  • Loading branch information
domenic committed Feb 26, 2016
1 parent a63c9de commit 6ae424e
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion source
Original file line number Diff line number Diff line change
Expand Up @@ -84174,6 +84174,24 @@ interface <dfn>NavigatorOnLine</dfn> {

<h5 id="fetching-scripts">Fetching scripts</h5>

<p>The various script-fetching algorithms below have two hooks that may be customized by their
callers:</p>

<ul>

<li><p><dfn data-x="fetching-scripts-set-up-request">set up the request</dfn>, which takes a

This comment has been minimized.

Copy link
@jungkees

jungkees Feb 26, 2016

Contributor

It seems other ul-li elements in the document start with capital?

<span data-x="concept-request">request</span> object which it may modify before the algorithm
continues</p></li>

<li><p><dfn data-x="fetching-scripts-validate-response">validate the response</dfn>, which takes

This comment has been minimized.

Copy link
@jungkees

jungkees Feb 26, 2016

Contributor

ditto

a <span data-x="concept-response">response</span> object and must either accept or reject

This comment has been minimized.

Copy link
@jungkees

jungkees Feb 26, 2016

Contributor

I expect "validate the response" is called with response concept not a response object.

it.</p></li>

</ul>

This comment has been minimized.

Copy link
@annevk

annevk Feb 26, 2016

Member

We should have some kind of clarification or enumeration here which fetches are covered and which are not. E.g., I think it might be better to have the note below be here, since otherwise you'll have to read through everything to figure out the architecture.

This comment has been minimized.

Copy link
@domenic

domenic Feb 26, 2016

Author Member

It's kind of tricky since only three algorithms are ever meant to be called by other specs: fetch a classic script, fetch a classic worker script, and fetch a module script tree. fetch a single module script also uses these hooks as part of internal bookkeeping; I couldn't figure out a cleaner way to do this, but am open to concrete suggestions.

Maybe what you're asking is best satisfied by adding a sentence separating those three from the rest, e.g. "The following algorithms are used when fetching a module script tree and should not be called directly by other specifications:"?

This comment has been minimized.

Copy link
@annevk

annevk Feb 26, 2016

Member

Maybe, I'm not sure I follow 100%. I was mostly talking about "Those hooks only apply to the top-level fetch at the root of the module script tree." which may be sufficient for service workers, but may not be elsewhere?

It might be clearer to just have all fetching logic here.

This comment has been minimized.

Copy link
@domenic

domenic Feb 26, 2016

Author Member

Maybe, I'm not sure I follow 100%. I was mostly talking about "Those hooks only apply to the top-level fetch at the root of the module script tree." which may be sufficient for service workers, but may not be elsewhere?

It should be sufficient since nobody should be calling the subordinate fetches (fetch the descendants of a module script and fetch a single module script).

It might be clearer to just have all fetching logic here.

This turns out to be really hard because then we need to know about service worker registration objects and last update times.

This comment has been minimized.

Copy link
@annevk

annevk Feb 26, 2016

Member

Okay, fair and fair.


<p class="note"><cite>Service Workers</cite> is an example of a specification that runs these
algorithms with its own options for the hooks. <ref spec=SW></p>

<p>To <dfn>fetch a classic script</dfn> for a <code>script</code> element <var>element</var>,
given a <var>url</var>, a <var>CORS setting</var>, a <var>settings object</var>, and a
<var>character encoding</var>, run these steps. The algorithm will asynchronously complete with
Expand All @@ -84189,6 +84207,9 @@ interface <dfn>NavigatorOnLine</dfn> {
data-x="">script</code>", and its <span data-x="concept-request-destination">destination</span>
to "<code data-x="">script</code>".</p></li>

<li><p>If the caller specified custom steps to <span data-x="fetching-scripts-set-up-request">set
up the request</span>, perform them on <var>request</var>.</p></li>

<!--FETCH-->
<li><p><span data-x="concept-fetch">Fetch</span> <var>request</var>.</p></li>

Expand All @@ -84206,6 +84227,11 @@ interface <dfn>NavigatorOnLine</dfn> {
data-x="concept-response-status">status</span> is not an <span>ok status</span>, asynchronously
complete this algorithm with null, and abort these steps.</p></li>

<li><p>If the caller specified custom steps to <span
data-x="fetching-scripts-validate-response">validate the response</span>, perform them on
<var>response</var>. If they reject the <var>response</var>, complete this algorithm with null,

This comment has been minimized.

Copy link
@jungkees

jungkees Feb 26, 2016

Contributor

I'd like to clarify what "reject" means here. What the "validate the response" steps are rejecting on errors is the update promise rather than the response. May something like "If they throw an exception or reject a promise returned to the calling algorithm, .." work? Or if you expected SW does something else, please let me know.

This comment has been minimized.

Copy link
@domenic

domenic Feb 26, 2016

Author Member

Maybe we should just use a different word than "reject". I didn't mean it to have anything to do with promises. Basically it should return true or false.

and abort these steps.</p></li>

<li><p>If <var>response</var>'s <span data-x="Content-Type">Content Type metadata</span>, if
any, specifies a character encoding, and the user agent supports that encoding, then set
<var>character encoding</var> to that encoding (ignoring the passed-in value).</p></li>
Expand Down Expand Up @@ -84247,6 +84273,9 @@ interface <dfn>NavigatorOnLine</dfn> {
data-x="concept-request-credentials-mode">credentials mode</span> is "<code
data-x="">same-origin</code>", and whose <span>use-URL-credentials flag</span> is set.</p></li>

<li><p>If the caller specified custom steps to <span data-x="fetching-scripts-set-up-request">set
up the request</span>, perform them on <var>request</var>.</p></li>

<!--FETCH-->
<li><p><span data-x="concept-fetch">Fetch</span> <var>request</var>.</p></li>

Expand All @@ -84259,6 +84288,11 @@ interface <dfn>NavigatorOnLine</dfn> {
data-x="concept-response-status">status</span> is not an <span>ok status</span>, asynchronously
complete this algorithm with null, and abort these steps.</p></li>

<li><p>If the caller specified custom steps to <span
data-x="fetching-scripts-validate-response">validate the response</span>, perform them on
<var>response</var>. If they reject the <var>response</var>, complete this algorithm with null,

This comment has been minimized.

Copy link
@jungkees

jungkees Feb 26, 2016

Contributor

ditto

and abort these steps.</p></li>

<li><p>Let <var>source text</var> be the result of <span data-x="UTF-8 decode">UTF-8
decoding</span> <var>response</var>'s <span data-x="concept-response-body">body</span>.</p></li>

Expand All @@ -84277,7 +84311,11 @@ interface <dfn>NavigatorOnLine</dfn> {
<li><p>If <var>ancestor list</var> is not given, let it be an empty list.</p></li>

<li><p><span>Fetch a single module script</span> given <var>url</var>, <var>credentials
mode</var>, <var>destination</var>, and <var>settings object</var>.</p>
mode</var>, <var>destination</var>, and <var>settings object</var>. If the caller of this
algorithm specified custom <span data-x="fetching-scripts-set-up-request">set up the
request</span> or <span data-x="fetching-scripts-validate-response">validate the response</span>
steps, pass those along while <span data-x="fetch a single module script">fetching a single
module script</span>.</p>

<li><p>Return from this algorithm and run the following steps when <span data-x="fetch a single
module script">fetching a single module script</span> asynchronously completes with
Expand Down Expand Up @@ -84341,6 +84379,11 @@ interface <dfn>NavigatorOnLine</dfn> {
data-x="concept-module-script-credentials-mode">credentials mode</span>, <var>destination</var>,
<var>module script</var>'s <span>settings object</span>, and <var>ancestor list</var>.</p>

<p class="note">It is intentional that no custom <span
data-x="fetching-scripts-set-up-request">set up the request</span> or <span
data-x="fetching-scripts-validate-response">validate the response</span> steps are passed along
here. Those hooks only apply to the top-level fetch at the root of the module script tree.</p>

<p>If any of the <span>fetch a module script tree</span> invocations asynchronously complete
with null, the user agent may <span data-x="concept-fetch-terminate">terminate</span> any or
all of the other fetches, and must then asynchronously complete this algorithm with null.</p>
Expand Down Expand Up @@ -84378,6 +84421,9 @@ interface <dfn>NavigatorOnLine</dfn> {
mode</var>, and <span data-x="concept-request-client">client</span> is
<var>settings object</var>.</p></li>

<li><p>If the caller specified custom steps to <span data-x="fetching-scripts-set-up-request">set
up the request</span>, perform them on <var>request</var>.</p></li>

<!--FETCH-->
<li><p><span data-x="concept-fetch">Fetch</span> <var>request</var>.</p></li>

Expand Down Expand Up @@ -84410,6 +84456,10 @@ interface <dfn>NavigatorOnLine</dfn> {
classic script</span> does not include MIME type checking. In contrast, module scripts will
fail to load if they are not of a correct MIME type.</p>
</li>

<li><p>The caller specified custom steps to <span
data-x="fetching-scripts-validate-response">validate the response</span>, which when performed
on <var>response</var> reject it.</p></li>
</ul>
</li>

Expand Down

0 comments on commit 6ae424e

Please sign in to comment.