diff --git a/cli/js/globals.ts b/cli/js/globals.ts index 5a04ec4434..be327722cf 100644 --- a/cli/js/globals.ts +++ b/cli/js/globals.ts @@ -6,6 +6,7 @@ import * as abortController from "./web/abort_controller.ts"; import * as abortSignal from "./web/abort_signal.ts"; import * as blob from "./web/blob.ts"; import * as consoleTypes from "./web/console.ts"; +import * as csprng from "./ops/get_random_values.ts"; import * as promiseTypes from "./web/promise.ts"; import * as customEvent from "./web/custom_event.ts"; import * as domException from "./web/dom_exception.ts"; @@ -222,6 +223,7 @@ export const windowOrWorkerGlobalScopeProperties = { queuingStrategy.ByteLengthQueuingStrategyImpl ), CountQueuingStrategy: nonEnumerable(queuingStrategy.CountQueuingStrategyImpl), + crypto: readOnly(csprng), File: nonEnumerable(domFile.DomFileImpl), CustomEvent: nonEnumerable(customEvent.CustomEventImpl), DOMException: nonEnumerable(domException.DOMExceptionImpl), diff --git a/cli/js/lib.deno.shared_globals.d.ts b/cli/js/lib.deno.shared_globals.d.ts index d962304473..617a75feac 100644 --- a/cli/js/lib.deno.shared_globals.d.ts +++ b/cli/js/lib.deno.shared_globals.d.ts @@ -192,6 +192,7 @@ declare function clearInterval(id?: number): void; declare function queueMicrotask(func: Function): void; declare var console: Console; +declare var crypto: Crypto; declare function addEventListener( type: string, @@ -578,6 +579,26 @@ declare class Console { static [Symbol.hasInstance](instance: Console): boolean; } +declare interface Crypto { + readonly subtle: null; + getRandomValues< + T extends + | Int8Array + | Int16Array + | Int32Array + | Uint8Array + | Uint16Array + | Uint32Array + | Uint8ClampedArray + | Float32Array + | Float64Array + | DataView + | null + >( + array: T + ): T; +} + type FormDataEntryValue = File | string; /** Provides a way to easily construct a set of key/value pairs representing diff --git a/cli/js/lib.deno.window.d.ts b/cli/js/lib.deno.window.d.ts index 21619af4db..61c5abf8ad 100644 --- a/cli/js/lib.deno.window.d.ts +++ b/cli/js/lib.deno.window.d.ts @@ -12,7 +12,6 @@ declare interface Window extends EventTarget { readonly self: Window & typeof globalThis; onload: ((this: Window, ev: Event) => any) | null; onunload: ((this: Window, ev: Event) => any) | null; - crypto: Crypto; close: () => void; readonly closed: boolean; Deno: typeof Deno; @@ -22,26 +21,5 @@ declare const window: Window & typeof globalThis; declare const self: Window & typeof globalThis; declare const onload: ((this: Window, ev: Event) => any) | null; declare const onunload: ((this: Window, ev: Event) => any) | null; -declare const crypto: Crypto; - -declare interface Crypto { - readonly subtle: null; - getRandomValues< - T extends - | Int8Array - | Int16Array - | Int32Array - | Uint8Array - | Uint16Array - | Uint32Array - | Uint8ClampedArray - | Float32Array - | Float64Array - | DataView - | null - >( - array: T - ): T; -} /* eslint-enable @typescript-eslint/no-explicit-any */ diff --git a/cli/js/runtime_main.ts b/cli/js/runtime_main.ts index a9787d7cb1..3e81fc6801 100644 --- a/cli/js/runtime_main.ts +++ b/cli/js/runtime_main.ts @@ -9,7 +9,6 @@ import * as denoNs from "./deno.ts"; import * as denoUnstableNs from "./deno_unstable.ts"; -import * as csprng from "./ops/get_random_values.ts"; import { exit } from "./ops/os.ts"; import { readOnly, @@ -57,7 +56,6 @@ function windowClose(): void { export const mainRuntimeGlobalProperties = { window: readOnly(globalThis), self: readOnly(globalThis), - crypto: readOnly(csprng), // TODO(bartlomieju): from MDN docs (https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope) // it seems those two properties should be available to workers as well onload: writable(null), diff --git a/cli/tests/subdir/worker_crypto.js b/cli/tests/subdir/worker_crypto.js new file mode 100644 index 0000000000..a86340005b --- /dev/null +++ b/cli/tests/subdir/worker_crypto.js @@ -0,0 +1,3 @@ +onmessage = function () { + postMessage(!!self.crypto); +}; diff --git a/cli/tests/workers_test.ts b/cli/tests/workers_test.ts index c4b93db3c5..df2cdf2aa7 100644 --- a/cli/tests/workers_test.ts +++ b/cli/tests/workers_test.ts @@ -290,3 +290,20 @@ Deno.test({ await promise2; }, }); + +Deno.test({ + name: "worker with crypto in scope", + fn: async function (): Promise { + const promise = createResolvable(); + const w = new Worker("../tests/subdir/worker_crypto.js", { + type: "module", + }); + w.onmessage = (e): void => { + assertEquals(e.data, true); + promise.resolve(); + }; + w.postMessage(null); + await promise; + w.terminate(); + }, +});