2020-05-07 15:15:59 -04:00
|
|
|
## 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
|
2020-05-18 15:53:25 -04:00
|
|
|
`type: "module"` option when creating a new worker:
|
2020-05-07 15:15:59 -04:00
|
|
|
|
|
|
|
```ts
|
|
|
|
// Good
|
|
|
|
new Worker("./worker.js", { type: "module" });
|
|
|
|
|
|
|
|
// Bad
|
|
|
|
new Worker("./worker.js");
|
|
|
|
new Worker("./worker.js", { type: "classic" });
|
|
|
|
```
|
|
|
|
|
2020-05-11 07:13:27 -04:00
|
|
|
### Permissions
|
|
|
|
|
|
|
|
Creating a new `Worker` instance is similar to a dynamic import; therefore Deno
|
|
|
|
requires appropriate permission for this action.
|
|
|
|
|
|
|
|
For workers using local modules; `--allow-read` permission is required:
|
|
|
|
|
2020-05-26 22:12:02 -04:00
|
|
|
**main.ts**
|
|
|
|
|
2020-05-11 07:13:27 -04:00
|
|
|
```ts
|
|
|
|
new Worker("./worker.ts", { type: "module" });
|
2020-05-26 22:12:02 -04:00
|
|
|
```
|
2020-05-11 07:13:27 -04:00
|
|
|
|
2020-05-26 22:12:02 -04:00
|
|
|
**worker.ts**
|
|
|
|
|
|
|
|
```ts
|
2020-05-11 07:13:27 -04:00
|
|
|
console.log("hello world");
|
|
|
|
self.close();
|
|
|
|
```
|
|
|
|
|
|
|
|
```shell
|
|
|
|
$ deno run main.ts
|
|
|
|
error: Uncaught PermissionDenied: read access to "./worker.ts", run again with the --allow-read flag
|
|
|
|
|
|
|
|
$ deno run --allow-read main.ts
|
|
|
|
hello world
|
|
|
|
```
|
|
|
|
|
2020-05-13 06:13:06 -04:00
|
|
|
For workers using remote modules; `--allow-net` permission is required:
|
2020-05-11 07:13:27 -04:00
|
|
|
|
2020-05-26 22:12:02 -04:00
|
|
|
**main.ts**
|
|
|
|
|
2020-05-11 07:13:27 -04:00
|
|
|
```ts
|
|
|
|
new Worker("https://example.com/worker.ts", { type: "module" });
|
2020-05-26 22:12:02 -04:00
|
|
|
```
|
2020-05-11 07:13:27 -04:00
|
|
|
|
2020-05-26 22:12:02 -04:00
|
|
|
**worker.ts** (at https[]()://example.com/worker.ts)
|
|
|
|
|
|
|
|
```ts
|
2020-05-11 07:13:27 -04:00
|
|
|
console.log("hello world");
|
|
|
|
self.close();
|
|
|
|
```
|
|
|
|
|
|
|
|
```shell
|
|
|
|
$ deno run main.ts
|
|
|
|
error: Uncaught PermissionDenied: net access to "https://example.com/worker.ts", run again with the --allow-net flag
|
|
|
|
|
|
|
|
$ deno run --allow-net main.ts
|
|
|
|
hello world
|
|
|
|
```
|
|
|
|
|
2020-05-07 15:15:59 -04:00
|
|
|
### Using Deno in worker
|
|
|
|
|
2020-05-09 21:09:42 -04:00
|
|
|
> This is an unstable Deno feature. Learn more about
|
|
|
|
> [unstable features](./stability.md).
|
2020-05-07 15:15:59 -04:00
|
|
|
|
2020-05-18 15:53:25 -04:00
|
|
|
By default the `Deno` namespace is not available in worker scope.
|
2020-05-07 15:15:59 -04:00
|
|
|
|
2020-05-18 15:53:25 -04:00
|
|
|
To add the `Deno` namespace pass `deno: true` option when creating new worker:
|
2020-05-07 15:15:59 -04:00
|
|
|
|
2020-05-26 22:12:02 -04:00
|
|
|
**main.js**
|
|
|
|
|
2020-05-07 15:15:59 -04:00
|
|
|
```ts
|
|
|
|
const worker = new Worker("./worker.js", { type: "module", deno: true });
|
|
|
|
worker.postMessage({ filename: "./log.txt" });
|
2020-05-26 22:12:02 -04:00
|
|
|
```
|
2020-05-07 15:15:59 -04:00
|
|
|
|
2020-05-26 22:12:02 -04:00
|
|
|
**worker.js**
|
|
|
|
|
|
|
|
```ts
|
2020-05-07 15:15:59 -04:00
|
|
|
self.onmessage = async (e) => {
|
|
|
|
const { filename } = e.data;
|
|
|
|
const text = await Deno.readTextFile(filename);
|
|
|
|
console.log(text);
|
|
|
|
self.close();
|
|
|
|
};
|
2020-05-26 22:12:02 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
**log.txt**
|
2020-05-07 15:15:59 -04:00
|
|
|
|
2020-05-26 22:12:02 -04:00
|
|
|
```
|
2020-05-07 15:15:59 -04:00
|
|
|
hello world
|
|
|
|
```
|
|
|
|
|
|
|
|
```shell
|
|
|
|
$ deno run --allow-read --unstable main.js
|
|
|
|
hello world
|
|
|
|
```
|
|
|
|
|
2020-05-18 15:53:25 -04:00
|
|
|
When the `Deno` namespace is available in worker scope, the worker inherits its
|
|
|
|
parent process' permissions (the ones specified using `--allow-*` flags).
|
2020-05-07 15:15:59 -04:00
|
|
|
|
|
|
|
We intend to make permissions configurable for workers.
|