Skip to content

Commit

Permalink
BREAKING: make Worker.deno unstable (#5128)
Browse files Browse the repository at this point in the history
This commit makes "Worker.deno" option unstable.

Added new manual entry "docs/runtime/workers.md".

Removed stale workers tests.
  • Loading branch information
bartlomieju committed May 7, 2020
1 parent 2b66b8a commit aca21da
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 81 deletions.
3 changes: 3 additions & 0 deletions cli/ops/worker_host.rs
Expand Up @@ -184,6 +184,9 @@ fn op_create_worker(
};
let args_name = args.name;
let use_deno_namespace = args.use_deno_namespace;
if use_deno_namespace {
state.check_unstable("Worker.deno");
}
let parent_state = state.clone();
let mut state = state.borrow_mut();
let global_state = state.global_state.clone();
Expand Down
23 changes: 0 additions & 23 deletions cli/tests/039_worker_deno_ns.ts

This file was deleted.

4 changes: 0 additions & 4 deletions cli/tests/039_worker_deno_ns.ts.out

This file was deleted.

10 changes: 0 additions & 10 deletions cli/tests/039_worker_deno_ns/has_ns.ts

This file was deleted.

1 change: 0 additions & 1 deletion cli/tests/039_worker_deno_ns/maybe_ns.ts

This file was deleted.

10 changes: 0 additions & 10 deletions cli/tests/039_worker_deno_ns/no_ns.ts

This file was deleted.

6 changes: 0 additions & 6 deletions cli/tests/040_worker_blob.ts

This file was deleted.

1 change: 0 additions & 1 deletion cli/tests/040_worker_blob.ts.out

This file was deleted.

3 changes: 0 additions & 3 deletions cli/tests/error_worker_dynamic.ts

This file was deleted.

3 changes: 0 additions & 3 deletions cli/tests/error_worker_dynamic.ts.out

This file was deleted.

20 changes: 1 addition & 19 deletions cli/tests/integration_tests.rs
Expand Up @@ -985,6 +985,7 @@ fn workers() {
.arg("test")
.arg("--reload")
.arg("--allow-net")
.arg("--unstable")
.arg("workers_test.ts")
.spawn()
.unwrap()
Expand Down Expand Up @@ -1080,17 +1081,6 @@ itest!(_038_checkjs {
output: "038_checkjs.js.out",
});

// TODO(bartlomieju): re-enable
itest_ignore!(_039_worker_deno_ns {
args: "run --reload 039_worker_deno_ns.ts",
output: "039_worker_deno_ns.ts.out",
});

itest_ignore!(_040_worker_blob {
args: "run --reload 040_worker_blob.ts",
output: "040_worker_blob.ts.out",
});

itest!(_041_dyn_import_eval {
args: "eval import('./subdir/mod4.js').then(console.log)",
output: "041_dyn_import_eval.out",
Expand Down Expand Up @@ -1471,14 +1461,6 @@ itest!(error_local_static_import_from_remote_js {
output: "error_local_static_import_from_remote.js.out",
});

// TODO(bartlomieju) Re-enable
itest_ignore!(error_worker_dynamic {
args: "run --reload error_worker_dynamic.ts",
check_stderr: true,
exit_code: 1,
output: "error_worker_dynamic.ts.out",
});

itest!(exit_error42 {
exit_code: 42,
args: "run --reload exit_error42.ts",
Expand Down
54 changes: 54 additions & 0 deletions docs/runtime/workers.md
@@ -0,0 +1,54 @@
## Workers

Deno supports
[`Web Worker API`](https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker).

Workers can be used to run code on multiple threads. Each instance of `Worker`
is run on a separate thread, dedicated only to that worker.

Currently Deno supports only `module` type workers; thus it's essential to pass
`type: "module"` option when creating new worker:

```ts
// Good
new Worker("./worker.js", { type: "module" });

// Bad
new Worker("./worker.js");
new Worker("./worker.js", { type: "classic" });
```

### Using Deno in worker

**UNSTABLE**: This feature is unstable and requires `--unstable` flag

By default `Deno` namespace is not available in worker scope.

To add `Deno` namespace pass `deno: true` option when creating new worker:

```ts
// main.js
const worker = new Worker("./worker.js", { type: "module", deno: true });
worker.postMessage({ filename: "./log.txt" });

// worker.js
self.onmessage = async (e) => {
const { filename } = e.data;
const text = await Deno.readTextFile(filename);
console.log(text);
self.close();
};

// log.txt
hello world
```

```shell
$ deno run --allow-read --unstable main.js
hello world
```

When `Deno` namespace is available in worker scope; the worker inherits parent
process permissions (the ones specified using `--allow-*` flags).

We intend to make permissions configurable for workers.
3 changes: 2 additions & 1 deletion docs/toc.json
Expand Up @@ -18,7 +18,8 @@
"children": {
"program_lifecycle": "Program Lifecycle",
"compiler_apis": "Compiler APIs",
"unstable": "Unstable APIs"
"unstable": "Unstable APIs",
"workers": "Workers"
}
},
"linking_to_external_code": {
Expand Down

0 comments on commit aca21da

Please sign in to comment.