fune/dom/tests/mochitest/fetch/nested_worker_wrapper.js
Yaron Tausky 1abdde54ea Bug 1480702: Rewrite test to avoid relying on unspecified/erroneous behavior r=asuth
After analyzing test_request.html it became clear that it relied on two false
assumptions:
1. It assumed that a service worker is notified that it's being installed before
   any client can interact with it.
2. It assumed that a service worker will always be installed upon registration,
   if it was unregistered before.
The first assumption is not backed by the spec; it seems that the opposite behavior
is the correct one (https://github.com/w3c/ServiceWorker/issues/1347). The second
assumption ignores the possibility of resurrection, where a service worker is re-
registered before getting uninstalled.

This commit addresses both problems by not relying on the installation phase,
instead passing the script URL as a search parameter and loading it at service
worker script evaluation time. It then runs the test function in response to a
message from the client.

Differential Revision: https://phabricator.services.mozilla.com/D3879

--HG--
extra : moz-landing-system : lando
2018-08-23 14:04:35 +00:00

32 lines
829 B
JavaScript

function getScriptUrl() {
return new URL(location.href).searchParams.get('script');
}
// Hold the nested worker alive until this parent worker closes.
var worker;
addEventListener('message', function nestedWorkerWrapperOnMessage(evt) {
removeEventListener('message', nestedWorkerWrapperOnMessage);
worker = new Worker('worker_wrapper.js?script=' + getScriptUrl());
worker.addEventListener('message', function(evt) {
self.postMessage({
context: 'NestedWorker',
type: evt.data.type,
status: evt.data.status,
msg: evt.data.msg,
});
});
worker.addEventListener('error', function(evt) {
self.postMessage({
context: 'NestedWorker',
type: 'status',
status: false,
msg: 'Nested worker error: ' + evt.message,
});
});
worker.postMessage(evt.data);
});