From d9efb8c02a0036d755c35e8e9c88d58bd45a9e2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 21 Feb 2020 10:35:41 -0500 Subject: fix: add io ops to worker to fix fetch (#4054) --- cli/compilers/compiler_worker.rs | 2 -- cli/js/unit_tests.ts | 6 +++++- cli/js/workers_test.ts | 23 ++++++++++++++++++++++- cli/tests/subdir/fetching_worker.js | 6 ++++++ cli/web_worker.rs | 4 ++++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 cli/tests/subdir/fetching_worker.js (limited to 'cli') diff --git a/cli/compilers/compiler_worker.rs b/cli/compilers/compiler_worker.rs index f0489e641..37dda67ab 100644 --- a/cli/compilers/compiler_worker.rs +++ b/cli/compilers/compiler_worker.rs @@ -38,9 +38,7 @@ impl CompilerWorker { // TODO(bartlomieju): CompilerWorker should not // depend on those ops ops::os::init(isolate, &state); - ops::files::init(isolate, &state); ops::fs::init(isolate, &state); - ops::io::init(isolate, &state); } Self(worker) } diff --git a/cli/js/unit_tests.ts b/cli/js/unit_tests.ts index c43abaa5c..0bdd17964 100644 --- a/cli/js/unit_tests.ts +++ b/cli/js/unit_tests.ts @@ -57,10 +57,14 @@ import "./url_search_params_test.ts"; import "./utime_test.ts"; import "./write_file_test.ts"; import "./performance_test.ts"; -import "./permissions_test.ts"; import "./version_test.ts"; import "./workers_test.ts"; +// FIXME(bartlomieju): +// This test file revokes permissions, it must be run last, +// otherwise it might revoke permission for tests that need them. +import "./permissions_test.ts"; + if (import.meta.main) { await Deno.runTests(); } diff --git a/cli/js/workers_test.ts b/cli/js/workers_test.ts index 9cb4f4a07..eccf83f65 100644 --- a/cli/js/workers_test.ts +++ b/cli/js/workers_test.ts @@ -1,5 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { test, assert, assertEquals } from "./test_util.ts"; +import { test, testPerm, assert, assertEquals } from "./test_util.ts"; export interface ResolvableMethods { resolve: (value?: T | PromiseLike) => void; @@ -82,3 +82,24 @@ test(async function workerThrowsWhenExecuting(): Promise { await promise; }); + +testPerm({ net: true }, async function workerCanUseFetch(): Promise { + const promise = createResolvable(); + + const fetchingWorker = new Worker("../tests/subdir/fetching_worker.js", { + type: "module" + }); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + fetchingWorker.onerror = (e: any): void => { + e.preventDefault(); + promise.reject(e.message); + }; + + fetchingWorker.onmessage = (e): void => { + assert(e.data === "Done!"); + promise.resolve(); + }; + + await promise; +}); diff --git a/cli/tests/subdir/fetching_worker.js b/cli/tests/subdir/fetching_worker.js new file mode 100644 index 000000000..a4237a97a --- /dev/null +++ b/cli/tests/subdir/fetching_worker.js @@ -0,0 +1,6 @@ +const r = await fetch( + "http://localhost:4545/cli/tests/subdir/fetching_worker.js" +); +await r.text(); +postMessage("Done!"); +close(); diff --git a/cli/web_worker.rs b/cli/web_worker.rs index 7efec476c..5c69f8e06 100644 --- a/cli/web_worker.rs +++ b/cli/web_worker.rs @@ -36,9 +36,13 @@ impl WebWorker { ops::runtime::init(isolate, &state); ops::web_worker::init(isolate, &state, &worker.internal_channels.sender); ops::worker_host::init(isolate, &state); + ops::io::init(isolate, &state); ops::errors::init(isolate, &state); ops::timers::init(isolate, &state); ops::fetch::init(isolate, &state); + // FIXME(bartlomieju): this is added only to provide "close" + // op - it should be moved to `ops::io` + ops::files::init(isolate, &state); } Self { -- cgit v1.2.3