Skip to content

Commit

Permalink
#462@patch: Fixes problem with Vitest hanging in some scenarios. It w…
Browse files Browse the repository at this point in the history
…as caused by the import of the package "sync-request".
  • Loading branch information
davidortnercybercom committed May 5, 2022
1 parent 5ec653b commit b44ea3f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 76 deletions.
124 changes: 62 additions & 62 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/happy-dom/src/fetch/ResourceFetchHandler.ts
@@ -1,7 +1,6 @@
import RelativeURL from '../location/RelativeURL';
import DOMException from '../exception/DOMException';
import IDocument from '../nodes/document/IDocument';
import SyncRequest from 'sync-request';

/**
* Helper class for performing fetch of resources.
Expand Down Expand Up @@ -34,7 +33,8 @@ export default class ResourceFetchHandler {
public static fetchSync(document: IDocument, url: string): string {
// We want to only load SyncRequest when it is needed to improve performance and not have direct dependencies to server side packages.
const absoluteURL = RelativeURL.getAbsoluteURL(document.defaultView.location, url);
const response = SyncRequest('GET', absoluteURL);
const syncRequest = require('sync-request');
const response = syncRequest('GET', absoluteURL);

if (response.isError()) {
throw new DOMException(
Expand Down
9 changes: 9 additions & 0 deletions packages/happy-dom/src/window/GlobalWindow.ts
Expand Up @@ -79,4 +79,13 @@ export default class GlobalWindow extends Window {
public eval(code: string): unknown {
return eval(code);
}

/**
* Setup of VM context.
*
* @override
*/
protected _setupVMContext(): void {
// Do nothing
}
}
36 changes: 24 additions & 12 deletions packages/happy-dom/src/window/Window.ts
Expand Up @@ -280,6 +280,12 @@ export default class Window extends EventTarget implements IWindow {
public Object;
public Function;

// Private properties
private _setTimeout = setTimeout;
private _clearTimeout = clearTimeout;
private _setInterval = setInterval;
private _clearInterval = clearInterval;

/**
* Constructor.
*/
Expand Down Expand Up @@ -317,14 +323,7 @@ export default class Window extends EventTarget implements IWindow {
}
}

// Only contextify if there isn't any sub-class handling globals (like GlobalWindow)
if (this.Array === undefined && !VM.isContext(this)) {
VM.createContext(this);

// Sets global properties from the VM to the Window object.
// Otherwise "this.Array" will be undefined for example.
VM.runInContext(VMGlobalPropertyScript, this);
}
this._setupVMContext();
}

/**
Expand Down Expand Up @@ -435,7 +434,7 @@ export default class Window extends EventTarget implements IWindow {
* @returns Timeout ID.
*/
public setTimeout(callback: () => void, delay = 0): NodeJS.Timeout {
const id = setTimeout(() => {
const id = this._setTimeout(() => {
this.happyDOM.asyncTaskManager.endTimer(id);
callback();
}, delay);
Expand All @@ -450,7 +449,7 @@ export default class Window extends EventTarget implements IWindow {
* @param id ID of the timeout.
*/
public clearTimeout(id: NodeJS.Timeout): void {
clearTimeout(id);
this._clearTimeout(id);
this.happyDOM.asyncTaskManager.endTimer(id);
}

Expand All @@ -463,7 +462,7 @@ export default class Window extends EventTarget implements IWindow {
* @returns Interval ID.
*/
public setInterval(callback: () => void, delay = 0): NodeJS.Timeout {
const id = setInterval(callback, delay);
const id = this._setInterval(callback, delay);
this.happyDOM.asyncTaskManager.startTimer(id);
return id;
}
Expand All @@ -475,7 +474,7 @@ export default class Window extends EventTarget implements IWindow {
* @param id ID of the interval.
*/
public clearInterval(id: NodeJS.Timeout): void {
clearInterval(id);
this._clearInterval(id);
this.happyDOM.asyncTaskManager.endTimer(id);
}

Expand Down Expand Up @@ -513,4 +512,17 @@ export default class Window extends EventTarget implements IWindow {
public async fetch(url: string, init?: IRequestInit): Promise<IResponse> {
return await FetchHandler.fetch(this.document, url, init);
}

/**
* Setup of VM context.
*/
protected _setupVMContext(): void {
if (!VM.isContext(this)) {
VM.createContext(this);

// Sets global properties from the VM to the Window object.
// Otherwise "this.Array" will be undefined for example.
VM.runInContext(VMGlobalPropertyScript, this);
}
}
}

0 comments on commit b44ea3f

Please sign in to comment.