<commit_before>Service Workers Algorithms
===
> **NOTE:**
>
> - **Register** and **_Update** honor HTTP caching rule.
> - Underscored function and attribute are UA-internal properties.

--
**Register**(_script_, _scope_)

1. Let _promise_ be a newly-created Promise.
2. Return _promise_.
3. Run the following steps asynchronously.
  1. Let _scope_ be _scope_ resolved against the document url.
  2. Let _script_ be _script_ resolved against the document url.
  3. If the origin of _script_ does not match the document's origin, then
    1. Reject _promise_ with a new SecurityError.
    2. Abort these steps.
  4. If the origin of _scope_ does not match the document's origin, then
    1. Reject _promise_ with a new SecurityError.
    2. Abort these steps.
  5. Let _serviceWorkerRegistration_ be **_GetRegistration**(_scope_).
  6. If _serviceWorkerRegistration_ is not null and _script_ is equal to _serviceWorkerRegistration_.*scriptUrl*, then
    1. If _serviceWorkerRegistration_.*updatePromise* is not null, then
      1. Resolve promise with _serviceWorkerRegistration_.*updatePromise*.
      2. Abort these steps.
    2. Else,
      1. Resolve _promise_ with **_GetNewestWorker**(_serviceWorkerRegistration_).
      2. Abort these steps.
  7. If _serviceWorkerRegistration_ is null, then
    1. Let _serviceWorkerRegistration_ be a newly-created *_ServiceWorkerRegistration* object.
    2. Set _serviceWorkerRegistration_ to the value of key _scope_ in *_ScopeToServiceWorkerRegistrationMap*.
    3. Set _serviceWorkerRegistration_.*scope* to _scope_.
  8. Set _serviceWorkerRegistration_.*scriptUrl* to _script_.
  9. Resolve _promise_ with **_Update**(_serviceWorkerRegistration_).

--
**_Update**(_serviceWorkerRegistration_)

1. If _serviceWorkerRegistration_.*updatePromise* is not null, then
  1. Reject _serviceWorkerRegistration_.*updatePromise* with a new AbortError.
  2. The browser may abort in-flight requests, parsing or worker execution relating to _serviceWorkerRegistration_.*updatePromise*.
2. Let _promise_ be a newly-created Promise.
3. Set _serviceWorkerRegistration_.*updatePromise* to _promise_.
4. Return _promise_.
5. Run the following steps asynchronously.
  1. Perform a fetch of _serviceWorkerRegistration_.*scriptUrl*, forcing a network fetch if cached entry is greater than 1 day old.
  2. If _promise_ has been rejected (eg, another registration has aborted it), then
    1. Set _serviceWorkerRegistration_.*updatePromise* to null.
    2. Abort these steps.
  3. If fetching the script fails due to the server returning a 4xx or 5xx response or equivalent, or there is a DNS error, or the connection times out, then
    1. Reject _promise_ with a new NetworkError.
    2. Set _serviceWorkerRegistration_.*updatePromise* to null.
    3. Abort these steps.
  4. If the server returned a redirect, then
    1. Reject _promise_ with a new SecurityError.
    2. Set _serviceWorkerRegistration_.*updatePromise* to null.
    3. Abort these steps.
  5. Let _fetchedScript_ be the fetched script.
  6. Let _newestWorker_ be **_GetNewestWorker**(_serviceWorkerRegistration_).
  7. If _newestWorker_ is not null, and _newestWorker_.*url* is equal to _serviceWorkerRegistration_.*scriptUrl* and _fetchedScript_ is a byte-for-byte match with the script of _newestWorker_, then
    1. Resolve _promise_ with _newestWorker_.
    2. Set _serviceWorkerRegistration_.*updatePromise* to null.
    3. Abort these steps.
  8. Else,
    1. Let _serviceWorker_ be a newly-created ServiceWorker object, using _fetchedScript_.
    2. If _promise_ has been rejected (e.g, another registration has aborted it), then
      1. Set _serviceWorkerRegistration_.*updatePromise* to null.
      2. Abort these steps.
    3. If _serviceWorker_ fails to start up, due to parse errors or uncaught errors, then
      1. Reject _promise_ with the error.
      2. Set _serviceWorkerRegistration_.*updatePromise* to null.
      3. Abort these steps.
    4. Resolve _promise_ with _serviceWorker_.
    5. Set _serviceWorkerRegistration_.*updatePromise* to null.
    6. Queue a task to call **_Install** with _serviceWorkerRegistration_ and _serviceWorker_.

--
**_SoftUpdate**(_serviceWorkerRegistration_)

> The browser may call this as often as it likes to check for updates

1. If _serviceWorkerRegistration_.*updatePromise* is not null, then
  1. Abort these steps.
2. If _serviceWorkerRegistration_.*pendingWorker* is not null, then
  1. Abort these steps.
3. Queue a task to call **_Update** with _serviceWorkerRegistration_.

--
**_Install**(_serviceWorkerRegistration_, _serviceWorker_)

1. If _serviceWorkerRegistration_.*pendingWorker* is not null, then
  1. Terminate _serviceWorkerRegistration_.*pendingWorker*.
  2. The user agent may abort any in-flight requests triggered by _serviceWorkerRegistration_.*pendingWorker*.
2. Set _serviceWorkerRegistration_.*pendingWorker* to _serviceWorker_.
3. Set _serviceWorkerRegistration_.*pendingWorker*.*_state* to _installing_.
4. Fire _install_ event on the associated _ServiceWorkerGlobalScope_ object.
5. Fire _install_ event on _navigator.serviceWorker_ for all documents which match _serviceWorkerRegistration_.*scope*.
6. If any handler called _waitUntil()_, then
  1. Extend this process until the associated promises resolve.
  2. If the resulting promise rejects, then
    1. Abort these steps. TODO: we should retry at some point?
7. Set _serviceWorkerRegistration_.*pendingWorker*.*_state* to _installed_.
8. Fire _installend_ event on _navigator.serviceWorker_ for all documents which match _serviceWorkerRegistration_.*scope*.
9. If any handler called _replace()_, then
  1. For each document matching _serviceWorkerRegistration_.*scope*
    1. Set _serviceWorkerRegistration_ as the document's service worker registration.
  2. Call **_Activate** with _serviceWorkerRegistration_.
  3. Abort these steps.
10. If no document is using _serviceWorkerRegistration_ as their service worker registration, then<commit_msg>Avoiding concurrency in the lifecycle as much as possible<commit_after>Service Workers Algorithms
===
> **NOTE:**
>
> - **Register** and **_Update** honor HTTP caching rule.
> - Underscored function and attribute are UA-internal properties.

--
**Register**(_script_, _scope_)

1. Let _promise_ be a newly-created Promise.
2. Return _promise_.
3. Run the following steps asynchronously.
  1. Let _scope_ be _scope_ resolved against the document url.
  2. Let _script_ be _script_ resolved against the document url.
  3. If the origin of _script_ does not match the document's origin, then
    1. Reject _promise_ with a new SecurityError.
    2. Abort these steps.
  4. If the origin of _scope_ does not match the document's origin, then
    1. Reject _promise_ with a new SecurityError.
    2. Abort these steps.
  5. Let _serviceWorkerRegistration_ be **_GetRegistration**(_scope_).
  6. If _serviceWorkerRegistration_ is not null and _script_ is equal to _serviceWorkerRegistration_.*scriptUrl*, then
    1. If _serviceWorkerRegistration_.*updatePromise* is not null, then
      1. Resolve promise with _serviceWorkerRegistration_.*updatePromise*.
      2. Abort these steps.
    2. Else,
      1. Resolve _promise_ with **_GetNewestWorker**(_serviceWorkerRegistration_).
      2. Abort these steps.
  7. If _serviceWorkerRegistration_ is null, then
    1. Let _serviceWorkerRegistration_ be a newly-created *_ServiceWorkerRegistration* object.
    2. Set _serviceWorkerRegistration_ to the value of key _scope_ in *_ScopeToServiceWorkerRegistrationMap*.
    3. Set _serviceWorkerRegistration_.*scope* to _scope_.
  8. Set _serviceWorkerRegistration_.*scriptUrl* to _script_.
  9. Resolve _promise_ with **_Update**(_serviceWorkerRegistration_).

--
**_Update**(_serviceWorkerRegistration_)

1. If _serviceWorkerRegistration_.*updatePromise* is not null, then
  1. Reject _serviceWorkerRegistration_.*updatePromise* with a new AbortError.
  2. The browser may abort in-flight requests, parsing or worker execution relating to _serviceWorkerRegistration_.*updatePromise*.
2. If _serviceWorkerRegistration_.*pendingWorker* is not null, then
  1. Terminate _serviceWorkerRegistration_.*pendingWorker*.
  2. Set _serviceWorkerRegistration_.*pendingWorker* to null
  3. The user agent may abort any in-flight requests triggered by _serviceWorkerRegistration_.*pendingWorker*.
3. Let _promise_ be a newly-created Promise.
4. Set _serviceWorkerRegistration_.*updatePromise* to _promise_.
5. Return _promise_.
6. Run the following steps asynchronously.
  1. Perform a fetch of _serviceWorkerRegistration_.*scriptUrl*, forcing a network fetch if cached entry is greater than 1 day old.
  2. If _promise_ has been rejected (eg, another registration has aborted it), then
    1. Set _serviceWorkerRegistration_.*updatePromise* to null.
    2. Abort these steps.
  3. If fetching the script fails due to the server returning a 4xx or 5xx response or equivalent, or there is a DNS error, or the connection times out, then
    1. Reject _promise_ with a new NetworkError.
    2. Set _serviceWorkerRegistration_.*updatePromise* to null.
    3. Abort these steps.
  4. If the server returned a redirect, then
    1. Reject _promise_ with a new SecurityError.
    2. Set _serviceWorkerRegistration_.*updatePromise* to null.
    3. Abort these steps.
  5. Let _fetchedScript_ be the fetched script.
  6. Let _activeWorker_ be _serviceWorkerRegistration_.*activeWorker*.
  7. If _activeWorker_ is not null, and _activeWorker_.*url* is equal to _serviceWorkerRegistration_.*scriptUrl* and _fetchedScript_ is a byte-for-byte match with the script of _activeWorker_, then
    1. Resolve _promise_ with _activeWorker_.
    2. Set _serviceWorkerRegistration_.*updatePromise* to null.
    3. Abort these steps.
  8. Else,
    1. Let _serviceWorker_ be a newly-created ServiceWorker object, using _fetchedScript_.
    2. If _promise_ has been rejected (e.g, another registration has aborted it), then
      1. Set _serviceWorkerRegistration_.*updatePromise* to null.
      2. Abort these steps.
    3. If _serviceWorker_ fails to start up, due to parse errors or uncaught errors, then
      1. Reject _promise_ with the error.
      2. Set _serviceWorkerRegistration_.*updatePromise* to null.
      3. Abort these steps.
    4. Resolve _promise_ with _serviceWorker_.
    5. Set _serviceWorkerRegistration_.*updatePromise* to null.
    6. Queue a task to call **_Install** with _serviceWorkerRegistration_ and _serviceWorker_.

--
**_SoftUpdate**(_serviceWorkerRegistration_)

> The browser may call this as often as it likes to check for updates

1. If _serviceWorkerRegistration_.*updatePromise* is not null, then
  1. Abort these steps.
2. If _serviceWorkerRegistration_.*pendingWorker* is not null, then
  1. Abort these steps.
3. Queue a task to call **_Update** with _serviceWorkerRegistration_.

--
**_Install**(_serviceWorkerRegistration_, _serviceWorker_)

1. Set _serviceWorkerRegistration_.*pendingWorker* to _serviceWorker_.
2. Set _serviceWorkerRegistration_.*pendingWorker*.*_state* to _installing_.
4. Fire _install_ event on the associated _ServiceWorkerGlobalScope_ object.
5. Fire _install_ event on _navigator.serviceWorker_ for all documents which match _serviceWorkerRegistration_.*scope*.
6. If any handler called _waitUntil()_, then
  1. Extend this process until the associated promises resolve.
  2. If the resulting promise rejects, then
    1. Abort these steps. TODO: we should retry at some point?
7. Set _serviceWorkerRegistration_.*pendingWorker*.*_state* to _installed_.
8. Fire _installend_ event on _navigator.serviceWorker_ for all documents which match _serviceWorkerRegistration_.*scope*.
9. If any handler called _replace()_, then
  1. For each document matching _serviceWorkerRegistration_.*scope*
    1. Set _serviceWorkerRegistration_ as the document's service worker registration.
  2. Call **_Activate** with _serviceWorkerRegistration_.
  3. Abort these steps.
10. If no document is using _serviceWorkerRegistration_ as their service worker registration, then
{'fraction_non_alphanumeric': 0.06690432764052395, 'fraction_numerical': 0.015005803349361631, 'mean_word_length': 4.297760210803689, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6794746', 'n_tokens_mistral': 3333, 'n_tokens_neox': 3272, 'n_words': 1374}