From b975c988bfa4d86d41e70ab4d27e4495c5907c8a Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Sun, 17 Jan 2021 23:12:00 +0000 Subject: [PATCH] feat(cli/dts/lib.deno.shared_globals): Add WorkerOptions --- cli/dts/lib.deno.shared_globals.d.ts | 164 ++++++++++++++------------- cli/tests/workers_test.ts | 3 +- 2 files changed, 85 insertions(+), 82 deletions(-) diff --git a/cli/dts/lib.deno.shared_globals.d.ts b/cli/dts/lib.deno.shared_globals.d.ts index 0c18920a68ccb3..785650c82f7cb1 100644 --- a/cli/dts/lib.deno.shared_globals.d.ts +++ b/cli/dts/lib.deno.shared_globals.d.ts @@ -653,93 +653,95 @@ interface WorkerEventMap extends AbstractWorkerEventMap { "messageerror": MessageEvent; } +interface WorkerOptions { + type?: "classic" | "module"; + name?: string; + /** UNSTABLE: New API. + * + * Set deno.namespace to `true` to make `Deno` namespace and all of its methods + * available to worker thread. The namespace is disabled by default. + * + * Configure deno.permissions options to change the level of access the worker will + * have. By default it will inherit the permissions of its parent thread. The permissions + * of a worker can't be extended beyond its parent's permissions reach. + * - "inherit" will take the permissions of the thread the worker is created in + * - You can disable/enable permissions all together by passing a boolean + * - You can provide a list of routes relative to the file the worker + * is created in to limit the access of the worker (read/write permissions only) + * + * Example: + * + * ```ts + * // mod.ts + * const worker = new Worker( + * new URL("deno_worker.ts", import.meta.url).href, { + * type: "module", + * deno: { + * namespace: true, + * permissions: { + * read: true, + * }, + * }, + * } + * ); + * worker.postMessage({ cmd: "readFile", fileName: "./log.txt" }); + * + * // deno_worker.ts + * + * + * self.onmessage = async function (e) { + * const { cmd, fileName } = e.data; + * if (cmd !== "readFile") { + * throw new Error("Invalid command"); + * } + * const buf = await Deno.readFile(fileName); + * const fileContents = new TextDecoder().decode(buf); + * console.log(fileContents); + * } + * ``` + * + * // log.txt + * hello world + * hello world 2 + * + * // run program + * $ deno run --allow-read mod.ts + * hello world + * hello world2 + * + */ + // TODO(Soremwar) + // `deno: true` is kept for backwards compatibility with the previous worker + // options implementation. Remove for 2.0. + deno?: true | { + namespace?: boolean; + /** Set to `"none"` to disable all the permissions in the worker. */ + permissions?: "inherit" | "none" | { + env?: "inherit" | boolean; + hrtime?: "inherit" | boolean; + /** The format of the net access list must be `hostname[:port]` + * in order to be resolved. + * + * ``` + * net: ["https://deno.land", "localhost:8080"], + * ``` + * */ + net?: "inherit" | boolean | string[]; + plugin?: "inherit" | boolean; + read?: "inherit" | boolean | Array; + run?: "inherit" | boolean; + write?: "inherit" | boolean | Array; + }; + }; +} + declare class Worker extends EventTarget { onerror?: (e: ErrorEvent) => void; onmessage?: (e: MessageEvent) => void; onmessageerror?: (e: MessageEvent) => void; constructor( specifier: string, - options?: { - type?: "classic" | "module"; - name?: string; - /** UNSTABLE: New API. - * - * Set deno.namespace to `true` to make `Deno` namespace and all of its methods - * available to worker thread. The namespace is disabled by default. - * - * Configure deno.permissions options to change the level of access the worker will - * have. By default it will inherit the permissions of its parent thread. The permissions - * of a worker can't be extended beyond its parent's permissions reach. - * - "inherit" will take the permissions of the thread the worker is created in - * - You can disable/enable permissions all together by passing a boolean - * - You can provide a list of routes relative to the file the worker - * is created in to limit the access of the worker (read/write permissions only) - * - * Example: - * - * ```ts - * // mod.ts - * const worker = new Worker( - * new URL("deno_worker.ts", import.meta.url).href, { - * type: "module", - * deno: { - * namespace: true, - * permissions: { - * read: true, - * }, - * }, - * } - * ); - * worker.postMessage({ cmd: "readFile", fileName: "./log.txt" }); - * - * // deno_worker.ts - * - * - * self.onmessage = async function (e) { - * const { cmd, fileName } = e.data; - * if (cmd !== "readFile") { - * throw new Error("Invalid command"); - * } - * const buf = await Deno.readFile(fileName); - * const fileContents = new TextDecoder().decode(buf); - * console.log(fileContents); - * } - * ``` - * - * // log.txt - * hello world - * hello world 2 - * - * // run program - * $ deno run --allow-read mod.ts - * hello world - * hello world2 - * - */ - // TODO(Soremwar) - // `deno: true` is kept for backwards compatibility with the previous worker - // options implementation. Remove for 2.0. - deno?: true | { - namespace?: boolean; - /** Set to `"none"` to disable all the permissions in the worker. */ - permissions?: "inherit" | "none" | { - env?: "inherit" | boolean; - hrtime?: "inherit" | boolean; - /** The format of the net access list must be `hostname[:port]` - * in order to be resolved. - * - * ``` - * net: ["https://deno.land", "localhost:8080"], - * ``` - * */ - net?: "inherit" | boolean | string[]; - plugin?: "inherit" | boolean; - read?: "inherit" | boolean | Array; - run?: "inherit" | boolean; - write?: "inherit" | boolean | Array; - }; - }; - }, + options?: WorkerOptions, ); postMessage(message: any, transfer: ArrayBuffer[]): void; postMessage(message: any, options?: PostMessageOptions): void; diff --git a/cli/tests/workers_test.ts b/cli/tests/workers_test.ts index 66444411e4b93c..1fe8b4538a3bf2 100644 --- a/cli/tests/workers_test.ts +++ b/cli/tests/workers_test.ts @@ -113,9 +113,10 @@ Deno.test({ name: "worker globals", fn: async function (): Promise { const promise = deferred(); + const workerOptions: WorkerOptions = { type: "module" }; const w = new Worker( new URL("workers/worker_globals.ts", import.meta.url).href, - { type: "module" }, + workerOptions, ); w.onmessage = (e): void => { assertEquals(e.data, "true, true, true");