Truly independent web browser
LibWeb: Throw from importScripts() in module workers
Shannon Boothd27558e6cdc0
diff --git a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WorkerGlobalScope.cppindex 2e9a42dc7ff..c34c1f4536f 100644--- a/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp+++ b/Libraries/LibWeb/HTML/WorkerGlobalScope.cpp@@ -96,7 +96,9 @@ WebIDL::ExceptionOr<void> WorkerGlobalScope::import_scripts(Vector<Utf16String>// which if provided will be used when invoking fetch a classic worker-imported script.// NOTE: Service Workers is an example of a specification that runs this algorithm with its own options for the perform the fetch hook.- // FIXME: 1. If worker global scope's type is "module", throw a TypeError exception.+ // 1. If worker global scope's type is "module", throw a TypeError exception.+ if (m_type == WorkerType::Module)+ return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "importScripts() is not allowed in module workers"_utf16 };// 2. Let settings object be the current settings object.auto& settings_object = HTML::current_settings_object();diff --git a/Services/WebWorker/WorkerHost.cpp b/Services/WebWorker/WorkerHost.cppindex 18fbd74c80d..8bed86542f0 100644--- a/Services/WebWorker/WorkerHost.cpp+++ b/Services/WebWorker/WorkerHost.cpp@@ -140,6 +140,11 @@ void WorkerHost::run(GC::Ref<Web::Page> page, Web::HTML::TransferDataEncoder mes// FIXME: 5. Set worker global scope's extended lifetime to options["extendedLifetime"].}+ // AD-HOC: The spec only sets a shared worker's type, which leaves a dedicated module worker "classic" and lets it+ // call importScripts().+ else {+ worker_global_scope->set_type(m_type);+ }// 11. Let destination be "sharedworker" if is shared is true, and "worker" otherwise.auto destination = is_shared ? Web::Fetch::Infrastructure::Request::Destination::SharedWorkerdiff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex f4d0a5437d5..c807aa8707e 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -1198,6 +1198,8 @@ Text/input/wpt-import/workers/SharedWorker-extendedLifetime-data.htmlText/input/wpt-import/workers/SharedWorker-extendedLifetime-named-module.htmlText/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-classic-DOMException.htmlText/input/wpt-import/workers/interfaces/WorkerGlobalScope/onerror/message-module-DOMException.html+Text/input/wpt-import/workers/modules/dedicated-worker-import-failure.html+Text/input/wpt-import/workers/modules/shared-worker-import-failure.html[CapFrameRate]; Ends a smooth scroll once 20 frames see no change, which frames faster than the compositor's ticks dodiff --git a/Tests/LibWeb/Text/expected/wpt-import/workers/modules/dedicated-worker-import-failure.txt b/Tests/LibWeb/Text/expected/wpt-import/workers/modules/dedicated-worker-import-failure.txtnew file mode 100644index 00000000000..e9ebf989387--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/workers/modules/dedicated-worker-import-failure.txt@@ -0,0 +1,11 @@+Harness status: OK++Found 6 tests++6 Pass+Pass importScripts() on module worker should throw an exception.+Pass Worker construction for non-existent script should dispatch an ErrorEvent.+Pass Static import for non-existent script should dispatch an ErrorEvent.+Pass Dynamic import for non-existent script should throw an exception.+Pass Worker construction for an invalid URL should throw an exception.+Pass Worker construction for a file URL should fail\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/workers/modules/shared-worker-import-failure.txt b/Tests/LibWeb/Text/expected/wpt-import/workers/modules/shared-worker-import-failure.txtnew file mode 100644index 00000000000..94ed700ceee--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/workers/modules/shared-worker-import-failure.txt@@ -0,0 +1,11 @@+Harness status: OK++Found 6 tests++6 Pass+Pass importScripts() on module worker should throw an exception.+Pass Worker construction for non-existent script should dispatch an ErrorEvent.+Pass Static import for non-existent script should dispatch an ErrorEvent.+Pass Dynamic import for non-existent script should throw an exception.+Pass SharedWorker construction for an invalid URL should throw an exception.+Pass SharedWorker construction for a file URL should throw an exception.\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/wpt-import/workers/modules/dedicated-worker-import-failure.html b/Tests/LibWeb/Text/input/wpt-import/workers/modules/dedicated-worker-import-failure.htmlnew file mode 100644index 00000000000..6b4d1275a8f--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/workers/modules/dedicated-worker-import-failure.html@@ -0,0 +1,57 @@+<!DOCTYPE html>+<title>DedicatedWorker: import failure</title>+<script src="../../resources/testharness.js"></script>+<script src="../../resources/testharnessreport.js"></script>+<script> setup({allow_uncaught_exception: true}); </script>+<script>++promise_test(async () => {+ const scriptURL = 'resources/import-scripts-worker.js';+ const worker = new Worker(scriptURL, { type: 'module' });+ const msg_event = await new Promise(resolve => worker.onmessage = resolve);+ assert_equals(msg_event.data, 'TypeError');+}, 'importScripts() on module worker should throw an exception.');++promise_test(() => {+ const scriptURL = 'resources/non-existent-worker.js';+ const worker = new Worker(scriptURL, { type: 'module' });+ return new Promise(resolve => worker.onerror = resolve);+}, 'Worker construction for non-existent script should dispatch an ' ++ 'ErrorEvent.');++promise_test(() => {+ const scriptURL = 'resources/static-import-non-existent-script-worker.js';+ const worker = new Worker(scriptURL, { type: 'module' });+ return new Promise(resolve => worker.onerror = resolve);+}, 'Static import for non-existent script should dispatch an ErrorEvent.');++promise_test(async () => {+ const scriptURL = './non-existent-worker.js';+ const worker = new Worker('resources/dynamic-import-given-url-worker.js',+ { type: 'module' });+ worker.postMessage(scriptURL);+ const msg_event = await new Promise((resolve, reject) => {+ worker.onmessage = resolve;+ worker.onerror = error => {+ const msg = error instanceof ErrorEvent ? error.message+ : 'unknown error';+ reject(msg);+ };+ });+ assert_equals(msg_event.data, 'TypeError');+}, 'Dynamic import for non-existent script should throw an exception.');++test(() => {+ const scriptURL = 'http://invalid:123$';+ assert_throws_dom('SyntaxError', () => new Worker(scriptURL, { type: 'module' }));+}, 'Worker construction for an invalid URL should throw an exception.');++async_test((t) => {+ const scriptURL = 'file:///static-import-worker.js';+ const worker = new Worker(scriptURL, { type: 'module' });+ worker.onerror = t.step_func_done(function(e) {+ assert_true(e instanceof Event);+ });+}, 'Worker construction for a file URL should fail');++</script>diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/dynamic-import-given-url-worker.js b/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/dynamic-import-given-url-worker.jsnew file mode 100644index 00000000000..55102759352--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/dynamic-import-given-url-worker.js@@ -0,0 +1,21 @@+// This worker dynamically imports the script URL sent by postMessage(), and+// sends back an error name if the dynamic import fails.+if ('DedicatedWorkerGlobalScope' in self &&+ self instanceof DedicatedWorkerGlobalScope) {+ self.onmessage = msg_event => {+ import(msg_event.data)+ .then(module => postMessage(module.meta_url))+ .catch(e => postMessage(e.name));+ };+} else if (+ 'SharedWorkerGlobalScope' in self &&+ self instanceof SharedWorkerGlobalScope) {+ self.onconnect = connect_event => {+ const port = connect_event.ports[0];+ port.onmessage = msg_event => {+ import(msg_event.data)+ .then(module => port.postMessage(module.meta_url))+ .catch(e => port.postMessage(e.name));+ };+ };+}diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/empty-worker.js b/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/empty-worker.jsnew file mode 100644index 00000000000..49ceb2648a9--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/empty-worker.js@@ -0,0 +1 @@+// Do nothing.diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/import-scripts-worker.js b/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/import-scripts-worker.jsnew file mode 100644index 00000000000..d85f573ffe3--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/import-scripts-worker.js@@ -0,0 +1,33 @@+try {+ importScripts('empty-worker.js');+ if ('DedicatedWorkerGlobalScope' in self &&+ self instanceof DedicatedWorkerGlobalScope) {+ postMessage('LOADED');+ } else if (+ 'SharedWorkerGlobalScope' in self &&+ self instanceof SharedWorkerGlobalScope) {+ onconnect = e => {+ e.ports[0].postMessage('LOADED');+ };+ }+} catch (e) {+ // Post a message instead of propagating an ErrorEvent to the page because+ // propagated event loses an error name.+ //+ // Step 1. "Let notHandled be the result of firing an event named error at the+ // Worker object associated with the worker, using ErrorEvent, with the+ // cancelable attribute initialized to true, the message, filename, lineno,+ // and colno attributes initialized appropriately, and the error attribute+ // initialized to null."+ // https://html.spec.whatwg.org/multipage/workers.html#runtime-script-errors-2+ if ('DedicatedWorkerGlobalScope' in self &&+ self instanceof DedicatedWorkerGlobalScope) {+ postMessage(e.name);+ } else if (+ 'SharedWorkerGlobalScope' in self &&+ self instanceof SharedWorkerGlobalScope) {+ onconnect = connectEvent => {+ connectEvent.ports[0].postMessage(e.name);+ };+ }+}diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/static-import-non-existent-script-worker.js b/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/static-import-non-existent-script-worker.jsnew file mode 100644index 00000000000..16f70e9daf4--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/workers/modules/resources/static-import-non-existent-script-worker.js@@ -0,0 +1 @@+import './non-existent-script.js';diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/modules/shared-worker-import-failure.html b/Tests/LibWeb/Text/input/wpt-import/workers/modules/shared-worker-import-failure.htmlnew file mode 100644index 00000000000..8abcb94f133--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/workers/modules/shared-worker-import-failure.html@@ -0,0 +1,70 @@+<!DOCTYPE html>+<title>SharedWorker: import failure</title>+<script src="../../resources/testharness.js"></script>+<script src="../../resources/testharnessreport.js"></script>+<script> setup({allow_uncaught_exception: true}); </script>+<script>++// TODO: Factor out the test cases into a separate file.+// (like import-test-cases.js)++// SharedWorkers doesn't fire error events when runtime script errors occur.+//+// "For shared workers, if the error is still not handled afterwards, the error+// may be reported to a developer console." [spec text]+// https://html.spec.whatwg.org/C/#runtime-script-errors-2++promise_test(async () => {+ const scriptURL = 'resources/import-scripts-worker.js';+ const worker = new SharedWorker(scriptURL, { type: 'module' });+ const msg_event =+ await new Promise(resolve => worker.port.onmessage = resolve);+ assert_equals(msg_event.data, 'TypeError');+}, 'importScripts() on module worker should throw an exception.');++promise_test(() => {+ const scriptURL = 'resources/non-existent-worker.js';+ const worker = new SharedWorker(scriptURL, { type: 'module' });+ return new Promise(resolve => worker.onerror = resolve);+}, 'Worker construction for non-existent script should dispatch an ' ++ 'ErrorEvent.');++promise_test(() => {+ const scriptURL = 'resources/static-import-non-existent-script-worker.js';+ const worker = new SharedWorker(scriptURL, { type: 'module' });+ return new Promise(resolve => worker.onerror = resolve);+}, 'Static import for non-existent script should dispatch an ErrorEvent.');++promise_test(async () => {+ const scriptURL = './non-existent-worker.js';+ const worker =+ new SharedWorker('resources/dynamic-import-given-url-worker.js',+ { type: 'module' });+ worker.port.postMessage(scriptURL);+ const msg_event = await new Promise((resolve, reject) => {+ worker.port.onmessage = resolve;+ worker.onerror = error => {+ const msg = error instanceof ErrorEvent ? error.message+ : 'unknown error';+ reject(msg);+ };+ });+ assert_equals(msg_event.data, 'TypeError');+}, 'Dynamic import for non-existent script should throw an exception.');++test(() => {+ const scriptURL = 'http://invalid:123$';+ assert_throws_dom('SyntaxError',+ () => new SharedWorker(scriptURL, { type: 'module' }));+}, 'SharedWorker construction for an invalid URL should throw an exception.');++async_test((t) => {+ const scriptURL = 'file:///static-import-worker.js';+ const worker = new SharedWorker(scriptURL, { type: 'module' });+ worker.onerror = t.step_func_done(function(e) {+ assert_true(e instanceof Event);+ });++}, 'SharedWorker construction for a file URL should throw an exception.');++</script>