Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

happy-dom

Package Overview
Dependencies
Maintainers
1
Versions
593
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

happy-dom - npm Package Compare versions

Comparing version 0.2.14 to 0.2.15

lib/AsyncManager.d.ts

20

lib/AsyncWindow.d.ts

@@ -8,8 +8,8 @@ /// <reference types="node" />

export default class AsyncWindow extends Window {
private isDisposed;
private asyncTaskCount;
private hasAsyncError;
private asyncTimeout;
private asyncPromises;
private async;
/**
* Constructor.
*/
constructor();
/**
* Sets a timer which executes a function once the timer expires.

@@ -61,12 +61,2 @@ *

dispose(): void;
/**
* Starts an async task.
*/
private startAsyncTask;
/**
* Ends an async task.
*
* @param {Error} [error] Error.
*/
private endAsyncTask;
}

@@ -57,2 +57,3 @@ "use strict";

var Window_1 = __importDefault(require("./Window"));
var AsyncTaskManager_1 = __importDefault(require("./AsyncTaskManager"));
var FETCH_RESPONSE_TYPE_METHODS = ['blob', 'json', 'formData', 'text'];

@@ -64,10 +65,16 @@ /**

__extends(AsyncWindow, _super);
/**
* Constructor.
*/
function AsyncWindow() {
var _this = _super !== null && _super.apply(this, arguments) || this;
var _this = _super.call(this) || this;
// Private Properties
_this.isDisposed = false;
_this.asyncTaskCount = 0;
_this.hasAsyncError = false;
_this.asyncTimeout = null;
_this.asyncPromises = [];
_this.async = new AsyncTaskManager_1.default();
// Binds all methods to "this", so that it will use the correct context when called globally.
for (var _i = 0, _a = Object.keys(AsyncWindow.prototype); _i < _a.length; _i++) {
var key = _a[_i];
if (typeof _this[key] === 'function') {
_this[key] = _this[key].bind(_this);
}
}
return _this;

@@ -84,6 +91,6 @@ }

var _this = this;
this.startAsyncTask();
this.async.startTask('timeout');
return global.setTimeout(function () {
callback();
_this.endAsyncTask();
_this.async.endTask('timeout');
}, delay);

@@ -98,3 +105,3 @@ };

global.clearTimeout(id);
this.endAsyncTask();
this.async.endTask('timeout');
};

@@ -109,3 +116,3 @@ /**

AsyncWindow.prototype.setInterval = function (callback, delay) {
this.startAsyncTask();
this.async.startTask('interval');
return global.setInterval(callback, delay);

@@ -120,3 +127,3 @@ };

global.clearInterval(id);
this.endAsyncTask();
this.async.endTask('interval');
};

@@ -135,3 +142,3 @@ /**

return [2 /*return*/, new Promise(function (resolve, reject) {
_this.startAsyncTask();
_this.async.startTask('fetch');
node_fetch_1.default(url, options)

@@ -143,11 +150,11 @@ .then(function (response) {

return new Promise(function (resolve, reject) {
_this.startAsyncTask();
_this.async.startTask('fetch');
asyncMethod
.then(function (response) {
resolve(response);
_this.endAsyncTask();
_this.async.endTask('fetch');
})
.catch(function (error) {
reject(error);
_this.endAsyncTask(error);
_this.async.endTask('fetch', error);
});

@@ -162,7 +169,7 @@ });

resolve(response);
_this.endAsyncTask();
_this.async.endTask('fetch');
})
.catch(function (error) {
reject(error);
_this.endAsyncTask(error);
_this.async.endTask('fetch', error);
});

@@ -181,10 +188,4 @@ })];

return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, new Promise(function (resolve, reject) {
_this.startAsyncTask();
_this.asyncPromises.push({ resolve: resolve, reject: reject });
global.clearTimeout(_this.asyncTimeout);
_this.asyncTimeout = global.setTimeout(function () { return _this.endAsyncTask(); }, 0);
})];
return [2 /*return*/, this.async.whenComplete()];
});

@@ -198,40 +199,5 @@ });

_super.prototype.dispose.call(this);
this.isDisposed = true;
this.hasAsyncError = false;
this.asyncTaskCount = 0;
this.asyncPromises = [];
global.clearTimeout(this.asyncTimeout);
this.async.dispose();
this.async = null;
};
/**
* Starts an async task.
*/
AsyncWindow.prototype.startAsyncTask = function () {
this.asyncTaskCount++;
};
/**
* Ends an async task.
*
* @param {Error} [error] Error.
*/
AsyncWindow.prototype.endAsyncTask = function (error) {
if (!this.isDisposed) {
this.asyncTaskCount--;
var promises = this.asyncPromises;
if (error) {
this.hasAsyncError = true;
this.asyncPromises = [];
for (var _i = 0, promises_1 = promises; _i < promises_1.length; _i++) {
var promise = promises_1[_i];
promise.reject(error);
}
}
else if (this.asyncTaskCount === 0 && !this.hasAsyncError) {
this.asyncPromises = [];
for (var _a = 0, promises_2 = promises; _a < promises_2.length; _a++) {
var promise = promises_2[_a];
promise.resolve();
}
}
}
};
return AsyncWindow;

@@ -238,0 +204,0 @@ }(Window_1.default));

@@ -95,6 +95,3 @@ "use strict";

VMContext.prototype.createContext = function () {
var sandbox = new AsyncWindow_1.default();
sandbox['window'] = sandbox;
sandbox['global'] = global;
return vm_1.default.createContext(sandbox);
return vm_1.default.createContext(new AsyncWindow_1.default());
};

@@ -101,0 +98,0 @@ return VMContext;

@@ -58,12 +58,2 @@ import CustomElementRegistry from './html-element/CustomElementRegistry';

Window: typeof Window;
Array: ArrayConstructor;
Object: ObjectConstructor;
Number: NumberConstructor;
Symbol: Function;
Function: FunctionConstructor;
RegExp: RegExpConstructor;
Date: DateConstructor;
JSON: JSON;
Promise: Function;
Error: ErrorConstructor;
document: Document;

@@ -75,4 +65,5 @@ customElements: CustomElementRegistry;

};
self: this;
console: Console;
self: Window;
window: Window;
shadowRootRenderOptions: ShadowRootRenderOptions;

@@ -79,0 +70,0 @@ /**

@@ -68,17 +68,8 @@ "use strict";

_this.Window = Window;
_this.Array = typeof global !== undefined ? global.Array : null;
_this.Object = typeof global !== undefined ? global.Object : null;
_this.Number = typeof global !== undefined ? global.Number : null;
_this.Symbol = typeof global !== undefined ? global.Symbol : null;
_this.Function = typeof global !== undefined ? global.Function : null;
_this.RegExp = typeof global !== undefined ? global.RegExp : null;
_this.Date = typeof global !== undefined ? global.Date : null;
_this.JSON = typeof global !== undefined ? global.JSON : null;
_this.Promise = typeof global !== undefined ? global.Promise : null;
_this.Error = typeof global !== undefined ? global.Error : null;
_this.customElements = new CustomElementRegistry_1.default();
_this.location = new Location_1.default();
_this.navigator = { userAgent: 'happy-dom' };
_this.console = typeof global !== undefined ? global.console : null;
_this.self = _this;
_this.console = typeof global !== undefined ? global.console : null;
_this.window = _this;
// Custom Properties (not part of HTML standard)

@@ -94,2 +85,11 @@ _this.shadowRootRenderOptions = new ShadowRootRenderOptions_1.default();

}
// Copies functionality from global (like eval, String, Array, Object etc.)
if (global !== undefined) {
for (var _a = 0, _b = Object.keys(global); _a < _b.length; _a++) {
var key = _b[_a];
if (typeof _this[key] === 'undefined') {
_this[key] = global[key];
}
}
}
return _this;

@@ -96,0 +96,0 @@ }

{
"name": "happy-dom",
"version": "0.2.14",
"version": "0.2.15",
"license": "MIT",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/capricorn86/happy-dom#readme",

import NodeFetch from 'node-fetch';
import Window from './Window';
import AsyncTaskManager from './AsyncTaskManager';

@@ -11,9 +12,19 @@ const FETCH_RESPONSE_TYPE_METHODS = ['blob', 'json', 'formData', 'text'];

// Private Properties
private isDisposed = false;
private asyncTaskCount: number = 0;
private hasAsyncError: boolean = false;
private asyncTimeout: NodeJS.Timeout = null;
private asyncPromises: { resolve: () => void; reject: (error: Error) => void }[] = [];
private async = new AsyncTaskManager();
/**
* Constructor.
*/
constructor() {
super();
// Binds all methods to "this", so that it will use the correct context when called globally.
for (const key of Object.keys(AsyncWindow.prototype)) {
if (typeof this[key] === 'function') {
this[key] = this[key].bind(this);
}
}
}
/**
* Sets a timer which executes a function once the timer expires.

@@ -26,6 +37,6 @@ *

public setTimeout(callback: () => void, delay?: number): NodeJS.Timeout {
this.startAsyncTask();
this.async.startTask('timeout');
return global.setTimeout(() => {
callback();
this.endAsyncTask();
this.async.endTask('timeout');
}, delay);

@@ -41,3 +52,3 @@ }

global.clearTimeout(id);
this.endAsyncTask();
this.async.endTask('timeout');
}

@@ -53,3 +64,3 @@

public setInterval(callback: () => void, delay?: number): NodeJS.Timeout {
this.startAsyncTask();
this.async.startTask('interval');
return global.setInterval(callback, delay);

@@ -65,3 +76,3 @@ }

global.clearInterval(id);
this.endAsyncTask();
this.async.endTask('interval');
}

@@ -78,3 +89,3 @@

return new Promise((resolve, reject) => {
this.startAsyncTask();
this.async.startTask('fetch');

@@ -87,3 +98,3 @@ NodeFetch(url, options)

return new Promise((resolve, reject) => {
this.startAsyncTask();
this.async.startTask('fetch');

@@ -93,7 +104,7 @@ asyncMethod

resolve(response);
this.endAsyncTask();
this.async.endTask('fetch');
})
.catch(error => {
reject(error);
this.endAsyncTask(error);
this.async.endTask('fetch', error);
});

@@ -105,7 +116,7 @@ });

resolve(response);
this.endAsyncTask();
this.async.endTask('fetch');
})
.catch(error => {
reject(error);
this.endAsyncTask(error);
this.async.endTask('fetch', error);
});

@@ -122,8 +133,3 @@ });

public async whenAsyncComplete(): Promise<void> {
return new Promise((resolve, reject) => {
this.startAsyncTask();
this.asyncPromises.push({ resolve, reject });
global.clearTimeout(this.asyncTimeout);
this.asyncTimeout = global.setTimeout(() => this.endAsyncTask(), 0);
});
return this.async.whenComplete();
}

@@ -136,41 +142,5 @@

super.dispose();
this.isDisposed = true;
this.hasAsyncError = false;
this.asyncTaskCount = 0;
this.asyncPromises = [];
global.clearTimeout(this.asyncTimeout);
this.async.dispose();
this.async = null;
}
/**
* Starts an async task.
*/
private startAsyncTask(): void {
this.asyncTaskCount++;
}
/**
* Ends an async task.
*
* @param {Error} [error] Error.
*/
private endAsyncTask(error?: Error): void {
if (!this.isDisposed) {
this.asyncTaskCount--;
const promises = this.asyncPromises;
if (error) {
this.hasAsyncError = true;
this.asyncPromises = [];
for (const promise of promises) {
promise.reject(error);
}
} else if (this.asyncTaskCount === 0 && !this.hasAsyncError) {
this.asyncPromises = [];
for (const promise of promises) {
promise.resolve();
}
}
}
}
}

@@ -61,7 +61,4 @@ import VM from 'vm';

private createContext(): VM.Context {
const sandbox = new AsyncWindow();
sandbox['window'] = sandbox;
sandbox['global'] = global;
return VM.createContext(sandbox);
return VM.createContext(new AsyncWindow());
}
}

@@ -46,13 +46,2 @@ import CustomElementRegistry from './html-element/CustomElementRegistry';

public Array = typeof global !== undefined ? global.Array : null;
public Object = typeof global !== undefined ? global.Object : null;
public Number = typeof global !== undefined ? global.Number : null;
public Symbol = typeof global !== undefined ? global.Symbol : null;
public Function = typeof global !== undefined ? global.Function : null;
public RegExp = typeof global !== undefined ? global.RegExp : null;
public Date = typeof global !== undefined ? global.Date : null;
public JSON = typeof global !== undefined ? global.JSON : null;
public Promise = typeof global !== undefined ? global.Promise : null;
public Error = typeof global !== undefined ? global.Error : null;
// Public Properties

@@ -63,4 +52,5 @@ public document: Document;

public navigator = { userAgent: 'happy-dom' };
public self = this;
public console = typeof global !== undefined ? global.console : null;
public self: Window = this;
public window: Window = this;

@@ -85,2 +75,11 @@ // Custom Properties (not part of HTML standard)

}
// Copies functionality from global (like eval, String, Array, Object etc.)
if (global !== undefined) {
for (const key of Object.keys(global)) {
if (typeof this[key] === 'undefined') {
this[key] = global[key];
}
}
}
}

@@ -87,0 +86,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc