Skip to content

Commit

Permalink
capricorn86#1158@patch: Conditional import of 'webcrypt' from 'node:c…
Browse files Browse the repository at this point in the history
…rypt'.
  • Loading branch information
mash-graz committed Nov 7, 2023
1 parent 5a5dfc7 commit 76efd7a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/happy-dom/src/window/IWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -133,6 +132,11 @@ 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.
*/
Expand Down Expand Up @@ -414,7 +418,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.
Expand Down
8 changes: 6 additions & 2 deletions packages/happy-dom/src/window/Window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -147,6 +146,11 @@ 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;
Expand Down Expand Up @@ -483,7 +487,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;
Expand Down

0 comments on commit 76efd7a

Please sign in to comment.