summaryrefslogtreecommitdiff
path: root/cli/js
diff options
context:
space:
mode:
Diffstat (limited to 'cli/js')
-rw-r--r--cli/js/compiler.ts4
-rw-r--r--cli/js/lib.deno.shared_globals.d.ts40
-rw-r--r--cli/js/lib.deno.worker.d.ts2
-rw-r--r--cli/js/ops/worker_host.ts2
-rw-r--r--cli/js/runtime_worker.ts32
-rw-r--r--cli/js/web/workers.ts4
6 files changed, 79 insertions, 5 deletions
diff --git a/cli/js/compiler.ts b/cli/js/compiler.ts
index b3cd3a481..91653a8e4 100644
--- a/cli/js/compiler.ts
+++ b/cli/js/compiler.ts
@@ -391,12 +391,12 @@ async function wasmCompilerOnMessage({
}
function bootstrapTsCompilerRuntime(): void {
- bootstrapWorkerRuntime("TS");
+ bootstrapWorkerRuntime("TS", false);
globalThis.onmessage = tsCompilerOnMessage;
}
function bootstrapWasmCompilerRuntime(): void {
- bootstrapWorkerRuntime("WASM");
+ bootstrapWorkerRuntime("WASM", false);
globalThis.onmessage = wasmCompilerOnMessage;
}
diff --git a/cli/js/lib.deno.shared_globals.d.ts b/cli/js/lib.deno.shared_globals.d.ts
index fd9f3691d..ef450c201 100644
--- a/cli/js/lib.deno.shared_globals.d.ts
+++ b/cli/js/lib.deno.shared_globals.d.ts
@@ -1080,6 +1080,46 @@ declare class Worker extends EventTarget {
options?: {
type?: "classic" | "module";
name?: string;
+ /** UNSTABLE: New API. Expect many changes; most likely this
+ * field will be made into an object for more granular
+ * configuration of worker thread (permissions, import map, etc.).
+ *
+ * Set to `true` to make `Deno` namespace and all of its methods
+ * available to worker thread.
+ *
+ * Currently worker inherits permissions from main thread (permissions
+ * given using `--allow-*` flags).
+ * Configurable permissions are on the roadmap to be implemented.
+ *
+ * Example:
+ * // mod.ts
+ * const worker = new Worker("./deno_worker.ts", { type: "module", deno: 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
+ *
+ */
+ deno?: boolean;
}
);
postMessage(message: any, transfer: ArrayBuffer[]): void;
diff --git a/cli/js/lib.deno.worker.d.ts b/cli/js/lib.deno.worker.d.ts
index a1e83af48..bba5f3321 100644
--- a/cli/js/lib.deno.worker.d.ts
+++ b/cli/js/lib.deno.worker.d.ts
@@ -3,6 +3,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any */
/// <reference no-default-lib="true" />
+/// <reference lib="deno.ns" />
/// <reference lib="deno.shared_globals" />
/// <reference lib="esnext" />
@@ -15,6 +16,7 @@ declare interface DedicatedWorkerGlobalScope {
name: typeof __workerMain.name;
close: typeof __workerMain.close;
postMessage: typeof __workerMain.postMessage;
+ Deno: typeof Deno;
}
declare const self: DedicatedWorkerGlobalScope & typeof globalThis;
diff --git a/cli/js/ops/worker_host.ts b/cli/js/ops/worker_host.ts
index d483a5313..11e268152 100644
--- a/cli/js/ops/worker_host.ts
+++ b/cli/js/ops/worker_host.ts
@@ -6,6 +6,7 @@ export function createWorker(
specifier: string,
hasSourceCode: boolean,
sourceCode: string,
+ useDenoNamespace: boolean,
name?: string
): { id: number } {
return sendSync("op_create_worker", {
@@ -13,6 +14,7 @@ export function createWorker(
hasSourceCode,
sourceCode,
name,
+ useDenoNamespace,
});
}
diff --git a/cli/js/runtime_worker.ts b/cli/js/runtime_worker.ts
index 1e7a9a67d..60c845a18 100644
--- a/cli/js/runtime_worker.ts
+++ b/cli/js/runtime_worker.ts
@@ -17,12 +17,23 @@ import {
eventTargetProperties,
setEventTargetData,
} from "./globals.ts";
+import * as Deno from "./deno.ts";
import * as webWorkerOps from "./ops/web_worker.ts";
import { LocationImpl } from "./web/location.ts";
import { log, assert, immutableDefine } from "./util.ts";
import { MessageEvent, ErrorEvent } from "./web/workers.ts";
import { TextEncoder } from "./web/text_encoding.ts";
import * as runtime from "./runtime.ts";
+import { internalObject } from "./internals.ts";
+import { symbols } from "./symbols.ts";
+import { setSignals } from "./signals.ts";
+
+// FIXME(bartlomieju): duplicated in `runtime_main.ts`
+// TODO: factor out `Deno` global assignment to separate function
+// Add internal object to Deno object.
+// This is not exposed as part of the Deno types.
+// @ts-ignore
+Deno[symbols.internal] = internalObject;
const encoder = new TextEncoder();
@@ -109,6 +120,7 @@ export const workerRuntimeGlobalProperties = {
export function bootstrapWorkerRuntime(
name: string,
+ useDenoNamespace: boolean,
internalName?: string
): void {
if (hasBootstrapped) {
@@ -128,7 +140,21 @@ export function bootstrapWorkerRuntime(
immutableDefine(globalThis, "location", location);
Object.freeze(globalThis.location);
- // globalThis.Deno is not available in worker scope
- delete globalThis.Deno;
- assert(globalThis.Deno === undefined);
+ if (useDenoNamespace) {
+ Object.defineProperties(Deno, {
+ pid: readOnly(s.pid),
+ noColor: readOnly(s.noColor),
+ args: readOnly(Object.freeze(s.args)),
+ });
+ // Setup `Deno` global - we're actually overriding already
+ // existing global `Deno` with `Deno` namespace from "./deno.ts".
+ immutableDefine(globalThis, "Deno", Deno);
+ Object.freeze(globalThis.Deno);
+ Object.freeze(globalThis.Deno.core);
+ Object.freeze(globalThis.Deno.core.sharedQueue);
+ setSignals();
+ } else {
+ delete globalThis.Deno;
+ assert(globalThis.Deno === undefined);
+ }
}
diff --git a/cli/js/web/workers.ts b/cli/js/web/workers.ts
index 0b7d4f4b6..6fcab3fe3 100644
--- a/cli/js/web/workers.ts
+++ b/cli/js/web/workers.ts
@@ -105,6 +105,7 @@ export interface Worker {
export interface WorkerOptions {
type?: "classic" | "module";
name?: string;
+ deno?: boolean;
}
export class WorkerImpl extends EventTarget implements Worker {
@@ -146,10 +147,13 @@ export class WorkerImpl extends EventTarget implements Worker {
}
*/
+ const useDenoNamespace = options ? !!options.deno : false;
+
const { id } = createWorker(
specifier,
hasSourceCode,
sourceCode,
+ useDenoNamespace,
options?.name
);
this.#id = id;