happy-dom 10.11.1 → 11.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of happy-dom might be problematic. Click here for more details.
- package/.eslintrc.cjs +1 -1
- package/cjs/console/VirtualConsole.cjs +351 -0
- package/cjs/console/VirtualConsole.cjs.map +1 -0
- package/cjs/console/VirtualConsole.d.ts +163 -0
- package/cjs/console/VirtualConsole.d.ts.map +1 -0
- package/cjs/console/VirtualConsolePrinter.cjs +101 -0
- package/cjs/console/VirtualConsolePrinter.cjs.map +1 -0
- package/cjs/console/VirtualConsolePrinter.d.ts +55 -0
- package/cjs/console/VirtualConsolePrinter.d.ts.map +1 -0
- package/cjs/console/enums/VirtualConsoleLogLevelEnum.cjs +16 -0
- package/cjs/console/enums/VirtualConsoleLogLevelEnum.cjs.map +1 -0
- package/cjs/console/enums/VirtualConsoleLogLevelEnum.d.ts +13 -0
- package/cjs/console/enums/VirtualConsoleLogLevelEnum.d.ts.map +1 -0
- package/cjs/console/enums/VirtualConsoleLogTypeEnum.cjs +30 -0
- package/cjs/console/enums/VirtualConsoleLogTypeEnum.cjs.map +1 -0
- package/cjs/console/enums/VirtualConsoleLogTypeEnum.d.ts +23 -0
- package/cjs/console/enums/VirtualConsoleLogTypeEnum.d.ts.map +1 -0
- package/cjs/console/types/IVirtualConsoleLogEntry.cjs +3 -0
- package/cjs/console/types/IVirtualConsoleLogEntry.cjs.map +1 -0
- package/cjs/console/types/IVirtualConsoleLogEntry.d.ts +10 -0
- package/cjs/console/types/IVirtualConsoleLogEntry.d.ts.map +1 -0
- package/cjs/console/types/IVirtualConsoleLogGroup.cjs +3 -0
- package/cjs/console/types/IVirtualConsoleLogGroup.cjs.map +1 -0
- package/cjs/console/types/IVirtualConsoleLogGroup.d.ts +7 -0
- package/cjs/console/types/IVirtualConsoleLogGroup.d.ts.map +1 -0
- package/cjs/console/types/IVirtualConsolePrinter.cjs +3 -0
- package/cjs/console/types/IVirtualConsolePrinter.cjs.map +1 -0
- package/cjs/console/types/IVirtualConsolePrinter.d.ts +52 -0
- package/cjs/console/types/IVirtualConsolePrinter.d.ts.map +1 -0
- package/cjs/console/utilities/VirtualConsoleLogEntryStringifier.cjs +97 -0
- package/cjs/console/utilities/VirtualConsoleLogEntryStringifier.cjs.map +1 -0
- package/cjs/console/utilities/VirtualConsoleLogEntryStringifier.d.ts +35 -0
- package/cjs/console/utilities/VirtualConsoleLogEntryStringifier.d.ts.map +1 -0
- package/cjs/css/CSSParser.cjs +34 -13
- package/cjs/css/CSSParser.cjs.map +1 -1
- package/cjs/css/CSSParser.d.ts +8 -0
- package/cjs/css/CSSParser.d.ts.map +1 -1
- package/cjs/event/EventTarget.cjs +23 -5
- package/cjs/event/EventTarget.cjs.map +1 -1
- package/cjs/event/EventTarget.d.ts.map +1 -1
- package/cjs/nodes/element/Element.cjs.map +1 -1
- package/cjs/nodes/element/Element.d.ts +1 -1
- package/cjs/nodes/element/Element.d.ts.map +1 -1
- package/cjs/nodes/html-link-element/HTMLLinkElementUtility.cjs +8 -31
- package/cjs/nodes/html-link-element/HTMLLinkElementUtility.cjs.map +1 -1
- package/cjs/nodes/html-link-element/HTMLLinkElementUtility.d.ts +0 -7
- package/cjs/nodes/html-link-element/HTMLLinkElementUtility.d.ts.map +1 -1
- package/cjs/nodes/html-script-element/HTMLScriptElement.cjs +2 -1
- package/cjs/nodes/html-script-element/HTMLScriptElement.cjs.map +1 -1
- package/cjs/nodes/html-script-element/HTMLScriptElement.d.ts.map +1 -1
- package/cjs/nodes/html-script-element/HTMLScriptElementUtility.cjs +10 -55
- package/cjs/nodes/html-script-element/HTMLScriptElementUtility.cjs.map +1 -1
- package/cjs/nodes/html-script-element/HTMLScriptElementUtility.d.ts +0 -14
- package/cjs/nodes/html-script-element/HTMLScriptElementUtility.d.ts.map +1 -1
- package/cjs/window/GlobalWindow.cjs +1 -9
- package/cjs/window/GlobalWindow.cjs.map +1 -1
- package/cjs/window/GlobalWindow.d.ts +1 -7
- package/cjs/window/GlobalWindow.d.ts.map +1 -1
- package/cjs/window/IHappyDOMOptions.d.ts +2 -0
- package/cjs/window/IHappyDOMOptions.d.ts.map +1 -1
- package/cjs/window/IWindow.d.ts +2 -7
- package/cjs/window/IWindow.d.ts.map +1 -1
- package/cjs/window/VMGlobalPropertyScript.cjs +1 -0
- package/cjs/window/VMGlobalPropertyScript.cjs.map +1 -1
- package/cjs/window/VMGlobalPropertyScript.d.ts +1 -1
- package/cjs/window/VMGlobalPropertyScript.d.ts.map +1 -1
- package/cjs/window/Window.cjs +51 -64
- package/cjs/window/Window.cjs.map +1 -1
- package/cjs/window/Window.d.ts +6 -23
- package/cjs/window/Window.d.ts.map +1 -1
- package/cjs/window/WindowErrorUtility.cjs +84 -0
- package/cjs/window/WindowErrorUtility.cjs.map +1 -0
- package/cjs/window/WindowErrorUtility.d.ts +38 -0
- package/cjs/window/WindowErrorUtility.d.ts.map +1 -0
- package/lib/console/VirtualConsole.d.ts +163 -0
- package/lib/console/VirtualConsole.d.ts.map +1 -0
- package/lib/console/VirtualConsole.js +322 -0
- package/lib/console/VirtualConsole.js.map +1 -0
- package/lib/console/VirtualConsolePrinter.d.ts +55 -0
- package/lib/console/VirtualConsolePrinter.d.ts.map +1 -0
- package/lib/console/VirtualConsolePrinter.js +95 -0
- package/lib/console/VirtualConsolePrinter.js.map +1 -0
- package/lib/console/enums/VirtualConsoleLogLevelEnum.d.ts +13 -0
- package/lib/console/enums/VirtualConsoleLogLevelEnum.d.ts.map +1 -0
- package/lib/console/enums/VirtualConsoleLogLevelEnum.js +14 -0
- package/lib/console/enums/VirtualConsoleLogLevelEnum.js.map +1 -0
- package/lib/console/enums/VirtualConsoleLogTypeEnum.d.ts +23 -0
- package/lib/console/enums/VirtualConsoleLogTypeEnum.d.ts.map +1 -0
- package/lib/console/enums/VirtualConsoleLogTypeEnum.js +28 -0
- package/lib/console/enums/VirtualConsoleLogTypeEnum.js.map +1 -0
- package/lib/console/types/IVirtualConsoleLogEntry.d.ts +10 -0
- package/lib/console/types/IVirtualConsoleLogEntry.d.ts.map +1 -0
- package/lib/console/types/IVirtualConsoleLogEntry.js +2 -0
- package/lib/console/types/IVirtualConsoleLogEntry.js.map +1 -0
- package/lib/console/types/IVirtualConsoleLogGroup.d.ts +7 -0
- package/lib/console/types/IVirtualConsoleLogGroup.d.ts.map +1 -0
- package/lib/console/types/IVirtualConsoleLogGroup.js +2 -0
- package/lib/console/types/IVirtualConsoleLogGroup.js.map +1 -0
- package/lib/console/types/IVirtualConsolePrinter.d.ts +52 -0
- package/lib/console/types/IVirtualConsolePrinter.d.ts.map +1 -0
- package/lib/console/types/IVirtualConsolePrinter.js +2 -0
- package/lib/console/types/IVirtualConsolePrinter.js.map +1 -0
- package/lib/console/utilities/VirtualConsoleLogEntryStringifier.d.ts +35 -0
- package/lib/console/utilities/VirtualConsoleLogEntryStringifier.d.ts.map +1 -0
- package/lib/console/utilities/VirtualConsoleLogEntryStringifier.js +91 -0
- package/lib/console/utilities/VirtualConsoleLogEntryStringifier.js.map +1 -0
- package/lib/css/CSSParser.d.ts +8 -0
- package/lib/css/CSSParser.d.ts.map +1 -1
- package/lib/css/CSSParser.js +34 -13
- package/lib/css/CSSParser.js.map +1 -1
- package/lib/event/EventTarget.d.ts.map +1 -1
- package/lib/event/EventTarget.js +23 -5
- package/lib/event/EventTarget.js.map +1 -1
- package/lib/nodes/element/Element.d.ts +1 -1
- package/lib/nodes/element/Element.d.ts.map +1 -1
- package/lib/nodes/element/Element.js.map +1 -1
- package/lib/nodes/html-link-element/HTMLLinkElementUtility.d.ts +0 -7
- package/lib/nodes/html-link-element/HTMLLinkElementUtility.d.ts.map +1 -1
- package/lib/nodes/html-link-element/HTMLLinkElementUtility.js +8 -31
- package/lib/nodes/html-link-element/HTMLLinkElementUtility.js.map +1 -1
- package/lib/nodes/html-script-element/HTMLScriptElement.d.ts.map +1 -1
- package/lib/nodes/html-script-element/HTMLScriptElement.js +2 -1
- package/lib/nodes/html-script-element/HTMLScriptElement.js.map +1 -1
- package/lib/nodes/html-script-element/HTMLScriptElementUtility.d.ts +0 -14
- package/lib/nodes/html-script-element/HTMLScriptElementUtility.d.ts.map +1 -1
- package/lib/nodes/html-script-element/HTMLScriptElementUtility.js +10 -55
- package/lib/nodes/html-script-element/HTMLScriptElementUtility.js.map +1 -1
- package/lib/window/GlobalWindow.d.ts +1 -7
- package/lib/window/GlobalWindow.d.ts.map +1 -1
- package/lib/window/GlobalWindow.js +1 -9
- package/lib/window/GlobalWindow.js.map +1 -1
- package/lib/window/IHappyDOMOptions.d.ts +2 -0
- package/lib/window/IHappyDOMOptions.d.ts.map +1 -1
- package/lib/window/IWindow.d.ts +2 -7
- package/lib/window/IWindow.d.ts.map +1 -1
- package/lib/window/VMGlobalPropertyScript.d.ts +1 -1
- package/lib/window/VMGlobalPropertyScript.d.ts.map +1 -1
- package/lib/window/VMGlobalPropertyScript.js +1 -0
- package/lib/window/VMGlobalPropertyScript.js.map +1 -1
- package/lib/window/Window.d.ts +6 -23
- package/lib/window/Window.d.ts.map +1 -1
- package/lib/window/Window.js +51 -64
- package/lib/window/Window.js.map +1 -1
- package/lib/window/WindowErrorUtility.d.ts +38 -0
- package/lib/window/WindowErrorUtility.d.ts.map +1 -0
- package/lib/window/WindowErrorUtility.js +78 -0
- package/lib/window/WindowErrorUtility.js.map +1 -0
- package/package.json +2 -2
- package/src/console/VirtualConsole.ts +356 -0
- package/src/console/VirtualConsolePrinter.ts +107 -0
- package/src/console/enums/VirtualConsoleLogLevelEnum.ts +12 -0
- package/src/console/enums/VirtualConsoleLogTypeEnum.ts +29 -0
- package/src/console/types/IVirtualConsoleLogEntry.ts +10 -0
- package/src/console/types/IVirtualConsoleLogGroup.ts +6 -0
- package/src/console/types/IVirtualConsolePrinter.ts +58 -0
- package/src/console/utilities/VirtualConsoleLogEntryStringifier.ts +100 -0
- package/src/css/CSSParser.ts +35 -14
- package/src/event/EventTarget.ts +27 -5
- package/src/nodes/element/Element.ts +1 -1
- package/src/nodes/html-link-element/HTMLLinkElementUtility.ts +13 -35
- package/src/nodes/html-script-element/HTMLScriptElement.ts +4 -1
- package/src/nodes/html-script-element/HTMLScriptElementUtility.ts +19 -62
- package/src/window/GlobalWindow.ts +1 -10
- package/src/window/IHappyDOMOptions.ts +1 -0
- package/src/window/IWindow.ts +2 -8
- package/src/window/VMGlobalPropertyScript.ts +1 -0
- package/src/window/Window.ts +77 -70
- package/src/window/WindowErrorUtility.ts +94 -0
- package/tsconfig.json +2 -3
@@ -0,0 +1,356 @@
|
|
1
|
+
import IVirtualConsolePrinter from './types/IVirtualConsolePrinter.js';
|
2
|
+
import VirtualConsoleLogLevelEnum from './enums/VirtualConsoleLogLevelEnum.js';
|
3
|
+
import VirtualConsoleLogTypeEnum from './enums/VirtualConsoleLogTypeEnum.js';
|
4
|
+
import IVirtualConsoleLogGroup from './types/IVirtualConsoleLogGroup.js';
|
5
|
+
import * as PerfHooks from 'perf_hooks';
|
6
|
+
|
7
|
+
/**
|
8
|
+
* Virtual Console.
|
9
|
+
*
|
10
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Console
|
11
|
+
*/
|
12
|
+
export default class VirtualConsole implements Console {
|
13
|
+
// This is needed as the interface for the NodeJS Console also have a reference to the ConsoleConstructor class as a property for some reason.
|
14
|
+
// This is not part of the browser specs.
|
15
|
+
public Console: NodeJS.ConsoleConstructor;
|
16
|
+
|
17
|
+
private _printer: IVirtualConsolePrinter;
|
18
|
+
private _count: { [label: string]: number } = {};
|
19
|
+
private _time: { [label: string]: number } = {};
|
20
|
+
private _groupID = 0;
|
21
|
+
private _groups: IVirtualConsoleLogGroup[] = [];
|
22
|
+
|
23
|
+
/**
|
24
|
+
* Constructor.
|
25
|
+
*
|
26
|
+
* @param printer Console printer.
|
27
|
+
*/
|
28
|
+
constructor(printer: IVirtualConsolePrinter) {
|
29
|
+
this._printer = printer;
|
30
|
+
}
|
31
|
+
|
32
|
+
/**
|
33
|
+
* Writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.
|
34
|
+
*
|
35
|
+
* @param assertion Assertion.
|
36
|
+
* @param args Arguments.
|
37
|
+
*/
|
38
|
+
public assert(assertion: boolean, ...args: Array<object | string>): void {
|
39
|
+
if (!assertion) {
|
40
|
+
this._printer.print({
|
41
|
+
type: VirtualConsoleLogTypeEnum.assert,
|
42
|
+
level: VirtualConsoleLogLevelEnum.error,
|
43
|
+
message: ['Assertion failed:', ...args],
|
44
|
+
group: this._groups[this._groups.length - 1] || null
|
45
|
+
});
|
46
|
+
}
|
47
|
+
}
|
48
|
+
|
49
|
+
/**
|
50
|
+
* Clears the console.
|
51
|
+
*/
|
52
|
+
public clear(): void {
|
53
|
+
this._printer.clear();
|
54
|
+
}
|
55
|
+
|
56
|
+
/**
|
57
|
+
* Logs the number of times that this particular call to count() has been called.
|
58
|
+
*
|
59
|
+
* @param [label='default'] Label.
|
60
|
+
*/
|
61
|
+
public count(label = 'default'): void {
|
62
|
+
if (!this._count[label]) {
|
63
|
+
this._count[label] = 0;
|
64
|
+
}
|
65
|
+
|
66
|
+
this._count[label]++;
|
67
|
+
|
68
|
+
this._printer.print({
|
69
|
+
type: VirtualConsoleLogTypeEnum.count,
|
70
|
+
level: VirtualConsoleLogLevelEnum.info,
|
71
|
+
message: [`${label}: ${this._count[label]}`],
|
72
|
+
group: this._groups[this._groups.length - 1] || null
|
73
|
+
});
|
74
|
+
}
|
75
|
+
|
76
|
+
/**
|
77
|
+
* Resets the counter.
|
78
|
+
*
|
79
|
+
* @param [label='default'] Label.
|
80
|
+
*/
|
81
|
+
public countReset(label = 'default'): void {
|
82
|
+
delete this._count[label];
|
83
|
+
|
84
|
+
this._printer.print({
|
85
|
+
type: VirtualConsoleLogTypeEnum.countReset,
|
86
|
+
level: VirtualConsoleLogLevelEnum.warn,
|
87
|
+
message: [`${label}: 0`],
|
88
|
+
group: this._groups[this._groups.length - 1] || null
|
89
|
+
});
|
90
|
+
}
|
91
|
+
|
92
|
+
/**
|
93
|
+
* Outputs a message to the web console at the "debug" log level.
|
94
|
+
*
|
95
|
+
* @param args Arguments.
|
96
|
+
*/
|
97
|
+
public debug(...args: Array<object | string>): void {
|
98
|
+
this._printer.print({
|
99
|
+
type: VirtualConsoleLogTypeEnum.debug,
|
100
|
+
level: VirtualConsoleLogLevelEnum.log,
|
101
|
+
message: args,
|
102
|
+
group: this._groups[this._groups.length - 1] || null
|
103
|
+
});
|
104
|
+
}
|
105
|
+
|
106
|
+
/**
|
107
|
+
* Displays an interactive list of the properties of the specified JavaScript object.
|
108
|
+
*
|
109
|
+
* @param data Data.
|
110
|
+
*/
|
111
|
+
public dir(data: object): void {
|
112
|
+
this._printer.print({
|
113
|
+
type: VirtualConsoleLogTypeEnum.dir,
|
114
|
+
level: VirtualConsoleLogLevelEnum.log,
|
115
|
+
message: [data],
|
116
|
+
group: this._groups[this._groups.length - 1] || null
|
117
|
+
});
|
118
|
+
}
|
119
|
+
|
120
|
+
/**
|
121
|
+
* Displays an interactive tree of the descendant elements of the specified XML/HTML element.
|
122
|
+
*
|
123
|
+
* @param data Data.
|
124
|
+
*/
|
125
|
+
public dirxml(data: object): void {
|
126
|
+
this._printer.print({
|
127
|
+
type: VirtualConsoleLogTypeEnum.dirxml,
|
128
|
+
level: VirtualConsoleLogLevelEnum.log,
|
129
|
+
message: [data],
|
130
|
+
group: this._groups[this._groups.length - 1] || null
|
131
|
+
});
|
132
|
+
}
|
133
|
+
|
134
|
+
/**
|
135
|
+
* Outputs an error message to the console.
|
136
|
+
*
|
137
|
+
* @param args Arguments.
|
138
|
+
*/
|
139
|
+
public error(...args: Array<object | string>): void {
|
140
|
+
this._printer.print({
|
141
|
+
type: VirtualConsoleLogTypeEnum.error,
|
142
|
+
level: VirtualConsoleLogLevelEnum.error,
|
143
|
+
message: args,
|
144
|
+
group: this._groups[this._groups.length - 1] || null
|
145
|
+
});
|
146
|
+
}
|
147
|
+
|
148
|
+
/**
|
149
|
+
* Alias for error().
|
150
|
+
*
|
151
|
+
* @deprecated
|
152
|
+
* @alias error()
|
153
|
+
* @param args Arguments.
|
154
|
+
*/
|
155
|
+
public exception(...args: Array<object | string>): void {
|
156
|
+
this.error(...args);
|
157
|
+
}
|
158
|
+
|
159
|
+
/**
|
160
|
+
* Creates a new inline group in the console, causing any subsequent console messages to be indented by an additional level, until console.groupEnd() is called.
|
161
|
+
*
|
162
|
+
* @param [label] Label.
|
163
|
+
*/
|
164
|
+
public group(label?: string): void {
|
165
|
+
this._groupID++;
|
166
|
+
const group = {
|
167
|
+
id: this._groupID,
|
168
|
+
label: label || 'default',
|
169
|
+
collapsed: false,
|
170
|
+
parent: this._groups[this._groups.length - 1] || null
|
171
|
+
};
|
172
|
+
this._groups.push(group);
|
173
|
+
this._printer.print({
|
174
|
+
type: VirtualConsoleLogTypeEnum.group,
|
175
|
+
level: VirtualConsoleLogLevelEnum.log,
|
176
|
+
message: [label || 'default'],
|
177
|
+
group
|
178
|
+
});
|
179
|
+
}
|
180
|
+
|
181
|
+
/**
|
182
|
+
* Creates a new inline group in the console, but prints it as collapsed, requiring the use of a disclosure button to expand it.
|
183
|
+
*
|
184
|
+
* @param [label] Label.
|
185
|
+
*/
|
186
|
+
public groupCollapsed(label?: string): void {
|
187
|
+
this._groupID++;
|
188
|
+
const group = {
|
189
|
+
id: this._groupID,
|
190
|
+
label: label || 'default',
|
191
|
+
collapsed: true,
|
192
|
+
parent: this._groups[this._groups.length - 1] || null
|
193
|
+
};
|
194
|
+
this._groups.push(group);
|
195
|
+
this._printer.print({
|
196
|
+
type: VirtualConsoleLogTypeEnum.groupCollapsed,
|
197
|
+
level: VirtualConsoleLogLevelEnum.log,
|
198
|
+
message: [label || 'default'],
|
199
|
+
group
|
200
|
+
});
|
201
|
+
}
|
202
|
+
|
203
|
+
/**
|
204
|
+
* Exits the current inline group in the console.
|
205
|
+
*/
|
206
|
+
public groupEnd(): void {
|
207
|
+
if (this._groups.length === 0) {
|
208
|
+
return;
|
209
|
+
}
|
210
|
+
this._groups.pop();
|
211
|
+
}
|
212
|
+
|
213
|
+
/**
|
214
|
+
*
|
215
|
+
* @param args
|
216
|
+
*/
|
217
|
+
public info(...args: Array<object | string>): void {
|
218
|
+
this._printer.print({
|
219
|
+
type: VirtualConsoleLogTypeEnum.info,
|
220
|
+
level: VirtualConsoleLogLevelEnum.info,
|
221
|
+
message: args,
|
222
|
+
group: this._groups[this._groups.length - 1] || null
|
223
|
+
});
|
224
|
+
}
|
225
|
+
|
226
|
+
/**
|
227
|
+
* Outputs a message to the console.
|
228
|
+
*
|
229
|
+
* @param args Arguments.
|
230
|
+
*/
|
231
|
+
public log(...args: Array<object | string>): void {
|
232
|
+
this._printer.print({
|
233
|
+
type: VirtualConsoleLogTypeEnum.log,
|
234
|
+
level: VirtualConsoleLogLevelEnum.log,
|
235
|
+
message: args,
|
236
|
+
group: this._groups[this._groups.length - 1] || null
|
237
|
+
});
|
238
|
+
}
|
239
|
+
|
240
|
+
/**
|
241
|
+
* Starts recording a performance profile.
|
242
|
+
*
|
243
|
+
* TODO: Implement this.
|
244
|
+
*/
|
245
|
+
public profile(): void {
|
246
|
+
throw new Error('Method not implemented.');
|
247
|
+
}
|
248
|
+
|
249
|
+
/**
|
250
|
+
* Stops recording a performance profile.
|
251
|
+
*
|
252
|
+
* TODO: Implement this.
|
253
|
+
*/
|
254
|
+
public profileEnd(): void {
|
255
|
+
throw new Error('Method not implemented.');
|
256
|
+
}
|
257
|
+
|
258
|
+
/**
|
259
|
+
* Displays tabular data as a table.
|
260
|
+
*
|
261
|
+
* @param data Data.
|
262
|
+
*/
|
263
|
+
public table(data: { [key: string]: number | string | boolean } | string[]): void {
|
264
|
+
this._printer.print({
|
265
|
+
type: VirtualConsoleLogTypeEnum.table,
|
266
|
+
level: VirtualConsoleLogLevelEnum.log,
|
267
|
+
message: [data],
|
268
|
+
group: this._groups[this._groups.length - 1] || null
|
269
|
+
});
|
270
|
+
}
|
271
|
+
|
272
|
+
/**
|
273
|
+
* Starts a timer you can use to track how long an operation takes.
|
274
|
+
*
|
275
|
+
* @param [label=default] Label.
|
276
|
+
*/
|
277
|
+
public time(label = 'default'): void {
|
278
|
+
this._time[label] = PerfHooks.performance.now();
|
279
|
+
}
|
280
|
+
|
281
|
+
/**
|
282
|
+
* Stops a timer that was previously started by calling console.time().
|
283
|
+
* The method logs the elapsed time in milliseconds.
|
284
|
+
*
|
285
|
+
* @param [label=default] Label.
|
286
|
+
*/
|
287
|
+
public timeEnd(label = 'default'): void {
|
288
|
+
const time = this._time[label];
|
289
|
+
if (time) {
|
290
|
+
const duration = PerfHooks.performance.now() - time;
|
291
|
+
this._printer.print({
|
292
|
+
type: VirtualConsoleLogTypeEnum.timeEnd,
|
293
|
+
level: VirtualConsoleLogLevelEnum.info,
|
294
|
+
message: [`${label}: ${duration}ms - timer ended`],
|
295
|
+
group: this._groups[this._groups.length - 1] || null
|
296
|
+
});
|
297
|
+
}
|
298
|
+
}
|
299
|
+
|
300
|
+
/**
|
301
|
+
* Logs the current value of a timer that was previously started by calling console.time().
|
302
|
+
* The method logs the elapsed time in milliseconds.
|
303
|
+
*
|
304
|
+
* @param [label=default] Label.
|
305
|
+
* @param [args] Arguments.
|
306
|
+
*/
|
307
|
+
public timeLog(label = 'default', ...args: Array<object | string>): void {
|
308
|
+
const time = this._time[label];
|
309
|
+
if (time) {
|
310
|
+
const duration = PerfHooks.performance.now() - time;
|
311
|
+
this._printer.print({
|
312
|
+
type: VirtualConsoleLogTypeEnum.timeLog,
|
313
|
+
level: VirtualConsoleLogLevelEnum.info,
|
314
|
+
message: [`${label}: ${duration}ms`, ...args],
|
315
|
+
group: this._groups[this._groups.length - 1] || null
|
316
|
+
});
|
317
|
+
}
|
318
|
+
}
|
319
|
+
|
320
|
+
/**
|
321
|
+
* Adds a single marker to the browser's Performance tool.
|
322
|
+
*
|
323
|
+
* TODO: Implement this.
|
324
|
+
*/
|
325
|
+
public timeStamp(): void {
|
326
|
+
throw new Error('Method not implemented.');
|
327
|
+
}
|
328
|
+
|
329
|
+
/**
|
330
|
+
* Outputs a stack trace to the console.
|
331
|
+
*
|
332
|
+
* @param args Arguments.
|
333
|
+
*/
|
334
|
+
public trace(...args: Array<object | string>): void {
|
335
|
+
this._printer.print({
|
336
|
+
type: VirtualConsoleLogTypeEnum.trace,
|
337
|
+
level: VirtualConsoleLogLevelEnum.log,
|
338
|
+
message: [...args, new Error('stack').stack.replace('Error: stack', '')],
|
339
|
+
group: this._groups[this._groups.length - 1] || null
|
340
|
+
});
|
341
|
+
}
|
342
|
+
|
343
|
+
/**
|
344
|
+
* Outputs a warning message to the console.
|
345
|
+
*
|
346
|
+
* @param args Arguments.
|
347
|
+
*/
|
348
|
+
public warn(...args: Array<object | string>): void {
|
349
|
+
this._printer.print({
|
350
|
+
type: VirtualConsoleLogTypeEnum.warn,
|
351
|
+
level: VirtualConsoleLogLevelEnum.warn,
|
352
|
+
message: args,
|
353
|
+
group: this._groups[this._groups.length - 1] || null
|
354
|
+
});
|
355
|
+
}
|
356
|
+
}
|
@@ -0,0 +1,107 @@
|
|
1
|
+
import IVirtualConsoleLogEntry from './types/IVirtualConsoleLogEntry.js';
|
2
|
+
import VirtualConsoleLogLevelEnum from './enums/VirtualConsoleLogLevelEnum.js';
|
3
|
+
import Event from '../event/Event.js';
|
4
|
+
import VirtualConsoleLogEntryStringifier from './utilities/VirtualConsoleLogEntryStringifier.js';
|
5
|
+
import IVirtualConsolePrinter from './types/IVirtualConsolePrinter.js';
|
6
|
+
|
7
|
+
/**
|
8
|
+
* Virtual console printer.
|
9
|
+
*/
|
10
|
+
export default class VirtualConsolePrinter implements IVirtualConsolePrinter {
|
11
|
+
private _logEntries: IVirtualConsoleLogEntry[] = [];
|
12
|
+
private _listeners: {
|
13
|
+
print: Array<(event: Event) => void>;
|
14
|
+
clear: Array<(event: Event) => void>;
|
15
|
+
} = { print: [], clear: [] };
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Writes to the output.
|
19
|
+
*
|
20
|
+
* @param logEntry Log entry.
|
21
|
+
*/
|
22
|
+
public print(logEntry: IVirtualConsoleLogEntry): void {
|
23
|
+
this._logEntries.push(logEntry);
|
24
|
+
this.dispatchEvent(new Event('print'));
|
25
|
+
}
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Clears the output.
|
29
|
+
*/
|
30
|
+
public clear(): void {
|
31
|
+
this._logEntries = [];
|
32
|
+
this.dispatchEvent(new Event('clear'));
|
33
|
+
}
|
34
|
+
|
35
|
+
/**
|
36
|
+
* Adds an event listener.
|
37
|
+
*
|
38
|
+
* @param eventType Event type ("print" or "clear").
|
39
|
+
* @param listener Listener.
|
40
|
+
*/
|
41
|
+
public addEventListener(eventType: 'print' | 'clear', listener: (event: Event) => void): void {
|
42
|
+
if (!this._listeners[eventType]) {
|
43
|
+
throw new Error(`Event type "${eventType}" is not supported.`);
|
44
|
+
}
|
45
|
+
this._listeners[eventType].push(listener);
|
46
|
+
}
|
47
|
+
|
48
|
+
/**
|
49
|
+
* Removes an event listener.
|
50
|
+
*
|
51
|
+
* @param eventType Event type ("print" or "clear").
|
52
|
+
* @param listener Listener.
|
53
|
+
*/
|
54
|
+
public removeEventListener(eventType: 'print' | 'clear', listener: (event: Event) => void): void {
|
55
|
+
if (!this._listeners[eventType]) {
|
56
|
+
throw new Error(`Event type "${eventType}" is not supported.`);
|
57
|
+
}
|
58
|
+
const index = this._listeners[eventType].indexOf(listener);
|
59
|
+
if (index !== -1) {
|
60
|
+
this._listeners[eventType].splice(index, 1);
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Dispatches an event.
|
66
|
+
*
|
67
|
+
* @param event Event.
|
68
|
+
*/
|
69
|
+
public dispatchEvent(event: Event): void {
|
70
|
+
if (!this._listeners[event.type]) {
|
71
|
+
throw new Error(`Event type "${event.type}" is not supported.`);
|
72
|
+
}
|
73
|
+
for (const listener of this._listeners[event.type]) {
|
74
|
+
listener(event);
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
/**
|
79
|
+
* Reads the buffer.
|
80
|
+
*
|
81
|
+
* @returns Console log entries.
|
82
|
+
*/
|
83
|
+
public read(): IVirtualConsoleLogEntry[] {
|
84
|
+
const logEntries = this._logEntries;
|
85
|
+
this._logEntries = [];
|
86
|
+
return logEntries;
|
87
|
+
}
|
88
|
+
|
89
|
+
/**
|
90
|
+
* Returns the buffer as a string.
|
91
|
+
*
|
92
|
+
* @param [logLevel] Log level.
|
93
|
+
* @returns Buffer as a string of concatenated log entries.
|
94
|
+
*/
|
95
|
+
public readAsString(
|
96
|
+
logLevel: VirtualConsoleLogLevelEnum = VirtualConsoleLogLevelEnum.log
|
97
|
+
): string {
|
98
|
+
const logEntries = this.read();
|
99
|
+
let output = '';
|
100
|
+
for (const logEntry of logEntries) {
|
101
|
+
if (logEntry.level >= logLevel) {
|
102
|
+
output += VirtualConsoleLogEntryStringifier.toString(logEntry);
|
103
|
+
}
|
104
|
+
}
|
105
|
+
return output;
|
106
|
+
}
|
107
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
/**
|
2
|
+
* Virtual console log type.
|
3
|
+
*/
|
4
|
+
enum VirtualConsoleLogTypeEnum {
|
5
|
+
// Log
|
6
|
+
log = 'log',
|
7
|
+
table = 'table',
|
8
|
+
trace = 'trace',
|
9
|
+
dir = 'dir',
|
10
|
+
dirxml = 'dirxml',
|
11
|
+
group = 'group',
|
12
|
+
groupCollapsed = 'groupCollapsed',
|
13
|
+
debug = 'debug',
|
14
|
+
timeLog = 'timeLog',
|
15
|
+
|
16
|
+
// Info
|
17
|
+
info = 'info',
|
18
|
+
count = 'count',
|
19
|
+
timeEnd = 'timeEnd',
|
20
|
+
|
21
|
+
// Warning
|
22
|
+
warn = 'warn',
|
23
|
+
countReset = 'countReset',
|
24
|
+
|
25
|
+
// Error
|
26
|
+
error = 'error',
|
27
|
+
assert = 'assert'
|
28
|
+
}
|
29
|
+
export default VirtualConsoleLogTypeEnum;
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import IVirtualConsoleLogGroup from './IVirtualConsoleLogGroup.js';
|
2
|
+
import VirtualConsoleLogLevelEnum from '../enums/VirtualConsoleLogLevelEnum.js';
|
3
|
+
import VirtualConsoleLogTypeEnum from '../enums/VirtualConsoleLogTypeEnum.js';
|
4
|
+
|
5
|
+
export default interface IVirtualConsoleLogEntry {
|
6
|
+
type: VirtualConsoleLogTypeEnum;
|
7
|
+
level: VirtualConsoleLogLevelEnum;
|
8
|
+
message: Array<string | object>;
|
9
|
+
group: IVirtualConsoleLogGroup | null;
|
10
|
+
}
|
@@ -0,0 +1,58 @@
|
|
1
|
+
import IVirtualConsoleLogEntry from './IVirtualConsoleLogEntry.js';
|
2
|
+
import VirtualConsoleLogLevelEnum from '../enums/VirtualConsoleLogLevelEnum.js';
|
3
|
+
import Event from '../../event/Event.js';
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Virtual console printer.
|
7
|
+
*/
|
8
|
+
export default interface IVirtualConsolePrinter {
|
9
|
+
/**
|
10
|
+
* Writes to the output.
|
11
|
+
*
|
12
|
+
* @param logEntry Log entry.
|
13
|
+
*/
|
14
|
+
print(logEntry: IVirtualConsoleLogEntry): void;
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Clears the output.
|
18
|
+
*/
|
19
|
+
clear(): void;
|
20
|
+
|
21
|
+
/**
|
22
|
+
* Adds an event listener.
|
23
|
+
*
|
24
|
+
* @param eventType Event type ("print" or "clear").
|
25
|
+
* @param listener Listener.
|
26
|
+
*/
|
27
|
+
addEventListener(eventType: 'print' | 'clear', listener: (event: Event) => void): void;
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Removes an event listener.
|
31
|
+
*
|
32
|
+
* @param eventType Event type ("print" or "clear").
|
33
|
+
* @param listener Listener.
|
34
|
+
*/
|
35
|
+
removeEventListener(eventType: 'print' | 'clear', listener: (event: Event) => void): void;
|
36
|
+
|
37
|
+
/**
|
38
|
+
* Dispatches an event.
|
39
|
+
*
|
40
|
+
* @param event Event.
|
41
|
+
*/
|
42
|
+
dispatchEvent(event: Event): void;
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Reads the buffer.
|
46
|
+
*
|
47
|
+
* @returns Console log entries.
|
48
|
+
*/
|
49
|
+
read(): IVirtualConsoleLogEntry[];
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Returns the buffer as a string.
|
53
|
+
*
|
54
|
+
* @param [logLevel] Log level.
|
55
|
+
* @returns Buffer as a string of concatenated log entries.
|
56
|
+
*/
|
57
|
+
readAsString(logLevel: VirtualConsoleLogLevelEnum): string;
|
58
|
+
}
|
@@ -0,0 +1,100 @@
|
|
1
|
+
import IVirtualConsoleLogEntry from '../types/IVirtualConsoleLogEntry.js';
|
2
|
+
import VirtualConsoleLogTypeEnum from '../enums/VirtualConsoleLogTypeEnum.js';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Virtual console utility.
|
6
|
+
*/
|
7
|
+
export default class VirtualConsoleLogEntryStringifier {
|
8
|
+
/**
|
9
|
+
* Stringifies a log entry.
|
10
|
+
*
|
11
|
+
* @param logEntry Log entry.
|
12
|
+
* @returns Stringified message.
|
13
|
+
*/
|
14
|
+
public static toString(logEntry: IVirtualConsoleLogEntry): string {
|
15
|
+
if (this.isLogEntryCollapsed(logEntry)) {
|
16
|
+
return '';
|
17
|
+
}
|
18
|
+
|
19
|
+
const tabbing = this.getLogEntryGroupTabbing(logEntry);
|
20
|
+
let output = tabbing;
|
21
|
+
for (const part of logEntry.message) {
|
22
|
+
output += output !== '' && output !== tabbing ? ' ' : '';
|
23
|
+
if (
|
24
|
+
typeof part === 'object' &&
|
25
|
+
(part === null || part.constructor.name === 'Object' || Array.isArray(part))
|
26
|
+
) {
|
27
|
+
try {
|
28
|
+
output += JSON.stringify(part);
|
29
|
+
} catch (error) {
|
30
|
+
output += new Error('Failed to JSON stringify object in log entry.').stack.replace(
|
31
|
+
/\n at/gm,
|
32
|
+
'\n ' + tabbing + 'at'
|
33
|
+
);
|
34
|
+
}
|
35
|
+
} else if (typeof part === 'object' && part['message'] && part['stack']) {
|
36
|
+
output += part['stack'].replace(/\n at/gm, '\n ' + tabbing + 'at');
|
37
|
+
} else {
|
38
|
+
output += this.getLogEntryIcon(logEntry) + String(part);
|
39
|
+
}
|
40
|
+
}
|
41
|
+
return output + '\n';
|
42
|
+
}
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Gets the log entry icon.
|
46
|
+
*
|
47
|
+
* @param logEntry Log entry.
|
48
|
+
* @returns Icon.
|
49
|
+
*/
|
50
|
+
private static getLogEntryIcon(logEntry: IVirtualConsoleLogEntry): string {
|
51
|
+
switch (logEntry.type) {
|
52
|
+
case VirtualConsoleLogTypeEnum.group:
|
53
|
+
return '▼ ';
|
54
|
+
case VirtualConsoleLogTypeEnum.groupCollapsed:
|
55
|
+
return '▶ ';
|
56
|
+
}
|
57
|
+
return '';
|
58
|
+
}
|
59
|
+
|
60
|
+
/**
|
61
|
+
* Gets the log entry group tabbing.
|
62
|
+
*
|
63
|
+
* @param logEntry Log entry.
|
64
|
+
* @returns Tabbing.
|
65
|
+
*/
|
66
|
+
private static getLogEntryGroupTabbing(logEntry: IVirtualConsoleLogEntry): string {
|
67
|
+
let tabs = '';
|
68
|
+
let group =
|
69
|
+
logEntry.type === VirtualConsoleLogTypeEnum.group ||
|
70
|
+
logEntry.type === VirtualConsoleLogTypeEnum.groupCollapsed
|
71
|
+
? logEntry.group?.parent
|
72
|
+
: logEntry.group;
|
73
|
+
while (group) {
|
74
|
+
tabs += ' ';
|
75
|
+
group = group.parent;
|
76
|
+
}
|
77
|
+
return tabs;
|
78
|
+
}
|
79
|
+
|
80
|
+
/**
|
81
|
+
* Checks if the log entry content is collapsed.
|
82
|
+
*
|
83
|
+
* @param logEntry Log entry.
|
84
|
+
* @returns True if collapsed.
|
85
|
+
*/
|
86
|
+
private static isLogEntryCollapsed(logEntry: IVirtualConsoleLogEntry): boolean {
|
87
|
+
let group =
|
88
|
+
logEntry.type === VirtualConsoleLogTypeEnum.group ||
|
89
|
+
logEntry.type === VirtualConsoleLogTypeEnum.groupCollapsed
|
90
|
+
? logEntry.group?.parent
|
91
|
+
: logEntry.group;
|
92
|
+
while (group) {
|
93
|
+
if (group.collapsed) {
|
94
|
+
return true;
|
95
|
+
}
|
96
|
+
group = group.parent;
|
97
|
+
}
|
98
|
+
return false;
|
99
|
+
}
|
100
|
+
}
|