From 1ebdedee0566614a652c98a3a5776a309eedbd3a Mon Sep 17 00:00:00 2001 From: Martin Schitter Date: Tue, 7 Nov 2023 14:09:44 +0100 Subject: [PATCH] #1158@patch: Conditional import of 'webcrypt' from 'node:crypt'. --- packages/happy-dom/src/window/IWindow.ts | 10 ++++++++-- packages/happy-dom/src/window/Window.ts | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/happy-dom/src/window/IWindow.ts b/packages/happy-dom/src/window/IWindow.ts index ce21b64d4..2187542c0 100644 --- a/packages/happy-dom/src/window/IWindow.ts +++ b/packages/happy-dom/src/window/IWindow.ts @@ -116,7 +116,6 @@ import IHappyDOMSettings from './IHappyDOMSettings.js'; import RequestInfo from '../fetch/types/IRequestInfo.js'; import FileList from '../nodes/html-input-element/FileList.js'; import Stream from 'stream'; -import { webcrypto } from 'crypto'; import FormData from '../form-data/FormData.js'; import AbortController from '../fetch/AbortController.js'; import AbortSignal from '../fetch/AbortSignal.js'; @@ -133,6 +132,13 @@ import Clipboard from '../clipboard/Clipboard.js'; import ClipboardItem from '../clipboard/ClipboardItem.js'; import ClipboardEvent from '../event/events/ClipboardEvent.js'; +if ( + !('crypto' in globalThis) || + Object.getPrototypeOf(globalThis.crypto) === Object.getPrototypeOf({}) +) { + globalThis['crypto'] = import('crypto').then((c) => c.webcrypto); +} + /** * Browser window. */ @@ -414,7 +420,7 @@ export default interface IWindow extends IEventTarget, INodeJSGlobal { readonly pageYOffset: number; readonly scrollX: number; readonly scrollY: number; - readonly crypto: typeof webcrypto; + readonly crypto: typeof globalThis.crypto; /** * Returns an object containing the values of all CSS properties of an element. diff --git a/packages/happy-dom/src/window/Window.ts b/packages/happy-dom/src/window/Window.ts index d94bd337f..afa603e05 100644 --- a/packages/happy-dom/src/window/Window.ts +++ b/packages/happy-dom/src/window/Window.ts @@ -114,7 +114,6 @@ import VMGlobalPropertyScript from './VMGlobalPropertyScript.js'; import * as PerfHooks from 'perf_hooks'; import VM from 'vm'; import { Buffer } from 'buffer'; -import { webcrypto } from 'crypto'; import XMLHttpRequestImplementation from '../xml-http-request/XMLHttpRequest.js'; import XMLHttpRequestUpload from '../xml-http-request/XMLHttpRequestUpload.js'; import XMLHttpRequestEventTarget from '../xml-http-request/XMLHttpRequestEventTarget.js'; @@ -147,6 +146,13 @@ import Clipboard from '../clipboard/Clipboard.js'; import ClipboardItem from '../clipboard/ClipboardItem.js'; import ClipboardEvent from '../event/events/ClipboardEvent.js'; +if ( + !('crypto' in globalThis) || + Object.getPrototypeOf(globalThis.crypto) === Object.getPrototypeOf({}) +) { + globalThis['crypto'] = import('crypto').then((c) => c.webcrypto); +} + const ORIGINAL_SET_TIMEOUT = setTimeout; const ORIGINAL_CLEAR_TIMEOUT = clearTimeout; const ORIGINAL_SET_INTERVAL = setInterval; @@ -483,7 +489,7 @@ export default class Window extends EventTarget implements IWindow { public readonly innerHeight: number = 768; public readonly outerWidth: number = 1024; public readonly outerHeight: number = 768; - public readonly crypto = webcrypto; + public readonly crypto = globalThis.crypto; // Node.js Globals public Array: typeof Array;