feat: support Deno namespace in Worker API (#4784)

This commit is contained in:
Bartek Iwańczuk 2020-04-16 23:40:29 +02:00 committed by GitHub
parent 1cd1f7de70
commit d359789c52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 190 additions and 21 deletions

View file

@ -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);
}
}