@ckeditor/ckeditor5-watchdog 44.1.0-alpha.5 → 44.2.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.
@@ -1,195 +0,0 @@
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-licensing-options
8
- */
9
- /**
10
- * @module watchdog/editorwatchdog
11
- */
12
- import type { CKEditorError } from 'ckeditor5/src/utils.js';
13
- import type { Editor, EditorConfig, Context } from 'ckeditor5/src/core.js';
14
- import Watchdog, { type WatchdogConfig } from './watchdog.js';
15
- /**
16
- * A watchdog for CKEditor 5 editors.
17
- *
18
- * See the {@glink features/watchdog Watchdog feature guide} to learn the rationale behind it and
19
- * how to use it.
20
- */
21
- export default class EditorWatchdog<TEditor extends Editor = Editor> extends Watchdog {
22
- /**
23
- * The current editor instance.
24
- */
25
- private _editor;
26
- /**
27
- * A promise associated with the life cycle of the editor (creation or destruction processes).
28
- *
29
- * It is used to prevent the initialization of the editor if the previous instance has not been destroyed yet,
30
- * and conversely, to prevent the destruction of the editor if it has not been initialized.
31
- */
32
- private _lifecyclePromise;
33
- /**
34
- * Throttled save method. The `save()` method is called the specified `saveInterval` after `throttledSave()` is called,
35
- * unless a new action happens in the meantime.
36
- */
37
- private _throttledSave;
38
- /**
39
- * The latest saved editor data represented as a root name -> root data object.
40
- */
41
- private _data?;
42
- /**
43
- * The last document version.
44
- */
45
- private _lastDocumentVersion?;
46
- /**
47
- * The editor source element or data.
48
- */
49
- private _elementOrData?;
50
- /**
51
- * Specifies whether the editor was initialized using document data (`true`) or HTML elements (`false`).
52
- */
53
- private _initUsingData;
54
- /**
55
- * The latest record of the editor editable elements. Used to restart the editor.
56
- */
57
- private _editables;
58
- /**
59
- * The editor configuration.
60
- */
61
- private _config?;
62
- /**
63
- * The creation method.
64
- *
65
- * @see #setCreator
66
- */
67
- protected _creator: EditorCreatorFunction<TEditor>;
68
- /**
69
- * The destruction method.
70
- *
71
- * @see #setDestructor
72
- */
73
- protected _destructor: (editor: Editor) => Promise<unknown>;
74
- private _excludedProps?;
75
- /**
76
- * @param Editor The editor class.
77
- * @param watchdogConfig The watchdog plugin configuration.
78
- */
79
- constructor(Editor: {
80
- create(...args: any): Promise<TEditor>;
81
- } | null, watchdogConfig?: WatchdogConfig);
82
- /**
83
- * The current editor instance.
84
- */
85
- get editor(): TEditor | null;
86
- /**
87
- * @internal
88
- */
89
- get _item(): TEditor | null;
90
- /**
91
- * Sets the function that is responsible for the editor creation.
92
- * It expects a function that should return a promise.
93
- *
94
- * ```ts
95
- * watchdog.setCreator( ( element, config ) => ClassicEditor.create( element, config ) );
96
- * ```
97
- */
98
- setCreator(creator: EditorCreatorFunction<TEditor>): void;
99
- /**
100
- * Sets the function that is responsible for the editor destruction.
101
- * Overrides the default destruction function, which destroys only the editor instance.
102
- * It expects a function that should return a promise or `undefined`.
103
- *
104
- * ```ts
105
- * watchdog.setDestructor( editor => {
106
- * // Do something before the editor is destroyed.
107
- *
108
- * return editor
109
- * .destroy()
110
- * .then( () => {
111
- * // Do something after the editor is destroyed.
112
- * } );
113
- * } );
114
- * ```
115
- */
116
- setDestructor(destructor: (editor: Editor) => Promise<unknown>): void;
117
- /**
118
- * Restarts the editor instance. This method is called whenever an editor error occurs. It fires the `restart` event and changes
119
- * the state to `initializing`.
120
- *
121
- * @fires restart
122
- */
123
- protected _restart(): Promise<unknown>;
124
- /**
125
- * Creates the editor instance and keeps it running, using the defined creator and destructor.
126
- *
127
- * @param elementOrData The editor source element or the editor data.
128
- * @param config The editor configuration.
129
- * @param context A context for the editor.
130
- */
131
- create(elementOrData?: HTMLElement | string | Record<string, string> | Record<string, HTMLElement>, config?: EditorConfig, context?: Context): Promise<unknown>;
132
- /**
133
- * Destroys the watchdog and the current editor instance. It fires the callback
134
- * registered in {@link #setDestructor `setDestructor()`} and uses it to destroy the editor instance.
135
- * It also sets the state to `destroyed`.
136
- */
137
- destroy(): Promise<unknown>;
138
- private _destroy;
139
- /**
140
- * Saves the editor data, so it can be restored after the crash even if the data cannot be fetched at
141
- * the moment of the crash.
142
- */
143
- private _save;
144
- /**
145
- * @internal
146
- */
147
- _setExcludedProperties(props: Set<unknown>): void;
148
- /**
149
- * Gets all data that is required to reinitialize editor instance.
150
- */
151
- private _getData;
152
- /**
153
- * For each attached model root, returns its HTML editable element (if available).
154
- */
155
- private _getEditables;
156
- /**
157
- * Traverses the error context and the current editor to find out whether these structures are connected
158
- * to each other via properties.
159
- *
160
- * @internal
161
- */
162
- _isErrorComingFromThisItem(error: CKEditorError): boolean;
163
- /**
164
- * Clones the editor configuration.
165
- */
166
- private _cloneEditorConfiguration;
167
- }
168
- export type EditorData = {
169
- roots: Record<string, {
170
- content: string;
171
- attributes: string;
172
- isLoaded: boolean;
173
- }>;
174
- markers: Record<string, {
175
- rangeJSON: {
176
- start: any;
177
- end: any;
178
- };
179
- usingOperation: boolean;
180
- affectsData: boolean;
181
- }>;
182
- commentThreads: string;
183
- suggestions: string;
184
- };
185
- /**
186
- * Fired after the watchdog restarts the error in case of a crash.
187
- *
188
- * @eventName ~EditorWatchdog#restart
189
- */
190
- export type EditorWatchdogRestartEvent = {
191
- name: 'restart';
192
- args: [];
193
- return: undefined;
194
- };
195
- export type EditorCreatorFunction<TEditor = Editor> = (elementOrData: HTMLElement | string | Record<string, string> | Record<string, HTMLElement>, config: EditorConfig) => Promise<TEditor>;
package/dist/index.d.ts DELETED
@@ -1,15 +0,0 @@
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-licensing-options
8
- */
9
- /**
10
- * @module watchdog
11
- */
12
- export { default as ContextWatchdog } from './contextwatchdog.js';
13
- export { default as EditorWatchdog, type EditorCreatorFunction } from './editorwatchdog.js';
14
- export { default as Watchdog, type WatchdogConfig } from './watchdog.js';
15
- import './augmentation.js';
@@ -1,12 +0,0 @@
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-licensing-options
8
- */
9
- /**
10
- * Traverses both structures to find out whether there is a reference that is shared between both structures.
11
- */
12
- export default function areConnectedThroughProperties(target1: unknown, target2: unknown, excludedNodes?: Set<unknown>): boolean;
@@ -1,12 +0,0 @@
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-licensing-options
8
- */
9
- /**
10
- * @module watchdog/utils/getsubnodes
11
- */
12
- export default function getSubNodes(head: unknown, excludedProperties?: Set<unknown>): Set<unknown>;
@@ -1,233 +0,0 @@
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-licensing-options
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';