@ckeditor/ckeditor5-watchdog 41.3.0 → 41.4.0-alpha.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.
@@ -0,0 +1,233 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
7
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
8
+ */
9
+ /**
10
+ * @module watchdog/watchdog
11
+ */
12
+ import type { CKEditorError } from 'ckeditor5/src/utils.js';
13
+ import type { EditorWatchdogRestartEvent } from './editorwatchdog.js';
14
+ import type { ContextWatchdogItemErrorEvent, ContextWatchdogItemRestartEvent } from './contextwatchdog.js';
15
+ /**
16
+ * An abstract watchdog class that handles most of the error handling process and the state of the underlying component.
17
+ *
18
+ * See the {@glink features/watchdog Watchdog feature guide} to learn the rationale behind it and how to use it.
19
+ *
20
+ * @internal
21
+ */
22
+ export default abstract class Watchdog {
23
+ /**
24
+ * An array of crashes saved as an object with the following properties:
25
+ *
26
+ * * `message`: `String`,
27
+ * * `stack`: `String`,
28
+ * * `date`: `Number`,
29
+ * * `filename`: `String | undefined`,
30
+ * * `lineno`: `Number | undefined`,
31
+ * * `colno`: `Number | undefined`,
32
+ */
33
+ readonly crashes: Array<{
34
+ message: string;
35
+ stack?: string;
36
+ date: number;
37
+ filename?: string;
38
+ lineno?: number;
39
+ colno?: number;
40
+ }>;
41
+ /**
42
+ * Specifies the state of the item watched by the watchdog. The state can be one of the following values:
43
+ *
44
+ * * `initializing` &ndash; Before the first initialization, and after crashes, before the item is ready.
45
+ * * `ready` &ndash; A state when the user can interact with the item.
46
+ * * `crashed` &ndash; A state when an error occurs. It quickly changes to `initializing` or `crashedPermanently`
47
+ * depending on how many and how frequent errors have been caught recently.
48
+ * * `crashedPermanently` &ndash; A state when the watchdog stops reacting to errors and keeps the item it is watching crashed,
49
+ * * `destroyed` &ndash; A state when the item is manually destroyed by the user after calling `watchdog.destroy()`.
50
+ */
51
+ state: WatchdogState;
52
+ /**
53
+ * @see module:watchdog/watchdog~WatchdogConfig
54
+ */
55
+ private _crashNumberLimit;
56
+ /**
57
+ * Returns the result of the `Date.now()` call. It can be overridden in tests to mock time as some popular
58
+ * approaches like `sinon.useFakeTimers()` do not work well with error handling.
59
+ */
60
+ private _now;
61
+ /**
62
+ * @see module:watchdog/watchdog~WatchdogConfig
63
+ */
64
+ private _minimumNonErrorTimePeriod;
65
+ /**
66
+ * Checks if the event error comes from the underlying item and restarts the item.
67
+ */
68
+ private _boundErrorHandler;
69
+ /**
70
+ * The method responsible for restarting the watched item.
71
+ */
72
+ protected abstract _restart(): Promise<unknown>;
73
+ /**
74
+ * Traverses the error context and the watched item to find out whether the error should
75
+ * be handled by the given item.
76
+ *
77
+ * @internal
78
+ */
79
+ abstract _isErrorComingFromThisItem(error: CKEditorError): boolean;
80
+ /**
81
+ * The watched item.
82
+ *
83
+ * @internal
84
+ */
85
+ abstract get _item(): unknown;
86
+ /**
87
+ * A dictionary of event emitter listeners.
88
+ */
89
+ private _listeners;
90
+ /**
91
+ * @param {module:watchdog/watchdog~WatchdogConfig} config The watchdog plugin configuration.
92
+ */
93
+ constructor(config: WatchdogConfig);
94
+ /**
95
+ * Destroys the watchdog and releases the resources.
96
+ */
97
+ destroy(): void;
98
+ /**
99
+ * Starts listening to a specific event name by registering a callback that will be executed
100
+ * whenever an event with a given name fires.
101
+ *
102
+ * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
103
+ *
104
+ * @param eventName The event name.
105
+ * @param callback A callback which will be added to event listeners.
106
+ */
107
+ on<K extends keyof EventMap>(eventName: K, callback: EventCallback<K>): void;
108
+ /**
109
+ * Stops listening to the specified event name by removing the callback from event listeners.
110
+ *
111
+ * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
112
+ *
113
+ * @param eventName The event name.
114
+ * @param callback A callback which will be removed from event listeners.
115
+ */
116
+ off(eventName: keyof EventMap, callback: unknown): void;
117
+ /**
118
+ * Fires an event with a given event name and arguments.
119
+ *
120
+ * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
121
+ */
122
+ protected _fire<K extends keyof EventMap>(eventName: K, ...args: EventArgs<K>): void;
123
+ /**
124
+ * Starts error handling by attaching global error handlers.
125
+ */
126
+ protected _startErrorHandling(): void;
127
+ /**
128
+ * Stops error handling by detaching global error handlers.
129
+ */
130
+ protected _stopErrorHandling(): void;
131
+ /**
132
+ * Checks if an error comes from the watched item and restarts it.
133
+ * It reacts to {@link module:utils/ckeditorerror~CKEditorError `CKEditorError` errors} only.
134
+ *
135
+ * @fires error
136
+ * @param error Error.
137
+ * @param evt An error event.
138
+ */
139
+ private _handleError;
140
+ /**
141
+ * Checks whether an error should be handled by the watchdog.
142
+ *
143
+ * @param error An error that was caught by the error handling process.
144
+ */
145
+ private _shouldReactToError;
146
+ /**
147
+ * Checks if the watchdog should restart the underlying item.
148
+ */
149
+ private _shouldRestart;
150
+ }
151
+ /**
152
+ * Fired when a new {@link module:utils/ckeditorerror~CKEditorError `CKEditorError`} error connected to the watchdog instance occurs
153
+ * and the watchdog will react to it.
154
+ *
155
+ * ```ts
156
+ * watchdog.on( 'error', ( evt, { error, causesRestart } ) => {
157
+ * console.log( 'An error occurred.' );
158
+ * } );
159
+ * ```
160
+ *
161
+ * @eventName ~Watchdog#error
162
+ */
163
+ export type WatchdogErrorEvent = {
164
+ name: 'error';
165
+ args: [WatchdogErrorEventData];
166
+ };
167
+ /**
168
+ * The `error` event data.
169
+ */
170
+ export type WatchdogErrorEventData = {
171
+ error: Error;
172
+ causesRestart: boolean;
173
+ };
174
+ /**
175
+ * Fired when the watchdog state changed.
176
+ *
177
+ * @eventName ~Watchdog#stateChange
178
+ */
179
+ export type WatchdogStateChangeEvent = {
180
+ name: 'stateChange';
181
+ args: [];
182
+ };
183
+ /**
184
+ * The map of watchdog events.
185
+ */
186
+ export interface EventMap {
187
+ stateChange: WatchdogStateChangeEvent;
188
+ error: WatchdogErrorEvent;
189
+ restart: EditorWatchdogRestartEvent;
190
+ itemError: ContextWatchdogItemErrorEvent;
191
+ itemRestart: ContextWatchdogItemRestartEvent;
192
+ }
193
+ /**
194
+ * Utility type that gets the arguments type for the given event.
195
+ */
196
+ export type EventArgs<K extends keyof EventMap> = EventMap[K]['args'];
197
+ /**
198
+ * Utility type that gets the callback type for the given event.
199
+ */
200
+ export type EventCallback<K extends keyof EventMap> = (evt: null, ...args: EventArgs<K>) => void;
201
+ /**
202
+ * The watchdog plugin configuration.
203
+ */
204
+ export interface WatchdogConfig {
205
+ /**
206
+ * A threshold specifying the number of watched item crashes
207
+ * when the watchdog stops restarting the item in case of errors.
208
+ * After this limit is reached and the time between the last errors is shorter than `minimumNonErrorTimePeriod`,
209
+ * the watchdog changes its state to `crashedPermanently` and it stops restarting the item. This prevents an infinite restart loop.
210
+ *
211
+ * @default 3
212
+ */
213
+ crashNumberLimit?: number;
214
+ /**
215
+ * An average number of milliseconds between the last watched item errors
216
+ * (defaults to 5000). When the period of time between errors is lower than that and the `crashNumberLimit` is also reached,
217
+ * the watchdog changes its state to `crashedPermanently` and it stops restarting the item. This prevents an infinite restart loop.
218
+ *
219
+ * @default 5000
220
+ */
221
+ minimumNonErrorTimePeriod?: number;
222
+ /**
223
+ * A minimum number of milliseconds between saving the editor data internally (defaults to 5000).
224
+ * Note that for large documents this might impact the editor performance.
225
+ *
226
+ * @default 5000
227
+ */
228
+ saveInterval?: number;
229
+ }
230
+ /**
231
+ * Specifies the state of the item watched by the watchdog.
232
+ */
233
+ export type WatchdogState = 'initializing' | 'ready' | 'crashed' | 'crashedPermanently' | 'destroyed';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckeditor/ckeditor5-watchdog",
3
- "version": "41.3.0",
3
+ "version": "41.4.0-alpha.0",
4
4
  "description": "A watchdog feature for CKEditor 5 editors. It keeps a CKEditor 5 editor instance running.",
5
5
  "keywords": [
6
6
  "ckeditor",
@@ -24,6 +24,7 @@
24
24
  "directory": "packages/ckeditor5-watchdog"
25
25
  },
26
26
  "files": [
27
+ "dist",
27
28
  "lang",
28
29
  "src/**/*.js",
29
30
  "src/**/*.d.ts",