mirror of
https://github.com/denoland/deno.git
synced 2025-08-04 02:48:24 +00:00
feat: support Deno namespace in Worker API (#4784)
This commit is contained in:
parent
1cd1f7de70
commit
d359789c52
13 changed files with 190 additions and 21 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
40
cli/js/lib.deno.shared_globals.d.ts
vendored
40
cli/js/lib.deno.shared_globals.d.ts
vendored
|
@ -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;
|
||||
|
|
2
cli/js/lib.deno.worker.d.ts
vendored
2
cli/js/lib.deno.worker.d.ts
vendored
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue