flexdesk 0.1.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.
- package/LICENSE +21 -0
- package/README.md +195 -0
- package/THIRD_PARTY_LICENSES.md +53 -0
- package/css/base.css +3439 -0
- package/css/flexdesk.css +5797 -0
- package/css/overrides.css +2349 -0
- package/css/reset.css +46 -0
- package/css/tokens.css +137 -0
- package/dist/charts.js +780 -0
- package/dist/charts.js.map +7 -0
- package/dist/chunk-3PHCPZHT.js +222 -0
- package/dist/chunk-3PHCPZHT.js.map +7 -0
- package/dist/chunk-CT4YXXLP.js +1770 -0
- package/dist/chunk-CT4YXXLP.js.map +7 -0
- package/dist/chunk-DRYCDMEG.js +2217 -0
- package/dist/chunk-DRYCDMEG.js.map +7 -0
- package/dist/chunk-DVU44T77.js +855 -0
- package/dist/chunk-DVU44T77.js.map +7 -0
- package/dist/chunk-FL5KFNQH.js +640 -0
- package/dist/chunk-FL5KFNQH.js.map +7 -0
- package/dist/chunk-FOOS3T5L.js +117 -0
- package/dist/chunk-FOOS3T5L.js.map +7 -0
- package/dist/chunk-JYWURG5T.js +32 -0
- package/dist/chunk-JYWURG5T.js.map +7 -0
- package/dist/chunk-TLZUUFOE.js +276 -0
- package/dist/chunk-TLZUUFOE.js.map +7 -0
- package/dist/chunk-UCJ2WD4D.js +625 -0
- package/dist/chunk-UCJ2WD4D.js.map +7 -0
- package/dist/chunk-WVFGV5FT.js +205 -0
- package/dist/chunk-WVFGV5FT.js.map +7 -0
- package/dist/core.js +1098 -0
- package/dist/core.js.map +7 -0
- package/dist/editor.js +1661 -0
- package/dist/editor.js.map +7 -0
- package/dist/flexdesk.css +5797 -0
- package/dist/host.js +132 -0
- package/dist/host.js.map +7 -0
- package/dist/reset.css +46 -0
- package/dist/tile_registry-6WZPTUZV.js +28 -0
- package/dist/tile_registry-6WZPTUZV.js.map +7 -0
- package/dist/tiles.js +2068 -0
- package/dist/tiles.js.map +7 -0
- package/dist/tokens.css +137 -0
- package/dist/widgets.js +5438 -0
- package/dist/widgets.js.map +7 -0
- package/dist/wm.js +4830 -0
- package/dist/wm.js.map +7 -0
- package/package.json +80 -0
- package/src/charts/chart_types.js +699 -0
- package/src/charts/data_series_plot_window.js +761 -0
- package/src/charts/downsample.js +119 -0
- package/src/charts/plot_config.js +176 -0
- package/src/charts/plot_popout_window.js +1560 -0
- package/src/charts/plotly_wrapper.js +432 -0
- package/src/core/event_bus.js +230 -0
- package/src/core/logging.js +300 -0
- package/src/core/settings.js +813 -0
- package/src/core/state_guard.js +382 -0
- package/src/core/state_guard_config.js +309 -0
- package/src/core/state_machine.js +258 -0
- package/src/editor/editor_pane.js +264 -0
- package/src/editor/monaco_editor_factory.js +449 -0
- package/src/editor/monaco_loader.js +74 -0
- package/src/editor/notebook_search.js +460 -0
- package/src/editor/notebook_tab_bar.js +456 -0
- package/src/editor/notebook_undo_manager.js +108 -0
- package/src/editor/split_pane_container.js +197 -0
- package/src/help/help_modal.js +409 -0
- package/src/help/help_registry.js +65 -0
- package/src/host/host.js +69 -0
- package/src/host/pywebview_host.js +133 -0
- package/src/tiles/config_schema.js +256 -0
- package/src/tiles/layout_persistence.js +662 -0
- package/src/tiles/tile_base.js +785 -0
- package/src/tiles/tile_grid.js +905 -0
- package/src/tiles/tile_registry.js +136 -0
- package/src/tiling/command_palette.js +328 -0
- package/src/tiling/content_registry.js +110 -0
- package/src/tiling/desktops.js +133 -0
- package/src/tiling/entity_sources.js +179 -0
- package/src/tiling/keymap.js +156 -0
- package/src/tiling/kind_taxonomy.js +172 -0
- package/src/tiling/landing_table.js +717 -0
- package/src/tiling/loading_overlay.js +33 -0
- package/src/tiling/nav_panel.js +142 -0
- package/src/tiling/page_factory.js +215 -0
- package/src/tiling/panel_keys.js +145 -0
- package/src/tiling/shell.js +437 -0
- package/src/tiling/tile_breadcrumb.js +222 -0
- package/src/tiling/tile_renderer.js +459 -0
- package/src/tiling/tile_tab_menu.js +340 -0
- package/src/tiling/tile_tree.js +880 -0
- package/src/tiling/wm.js +1352 -0
- package/src/ui/base/component_base.js +30 -0
- package/src/ui/base/controller_base.js +48 -0
- package/src/ui/base/page_base.js +102 -0
- package/src/ui/components/about_dialog.js +224 -0
- package/src/ui/components/action_dropdown.js +389 -0
- package/src/ui/components/attribute_list_editor.js +652 -0
- package/src/ui/components/autocomplete_field.js +732 -0
- package/src/ui/components/computing_status_window.js +81 -0
- package/src/ui/components/confirm_dialog.js +307 -0
- package/src/ui/components/context_menu.js +87 -0
- package/src/ui/components/data_table.js +2228 -0
- package/src/ui/components/detail_header.js +177 -0
- package/src/ui/components/drag_reorder.js +0 -0
- package/src/ui/components/gallery_picker.js +147 -0
- package/src/ui/components/inline_renamer.js +129 -0
- package/src/ui/components/managed_window.js +804 -0
- package/src/ui/components/modal.js +512 -0
- package/src/ui/components/notification_history.js +264 -0
- package/src/ui/components/slide_out_panel.js +185 -0
- package/src/ui/components/slider_field.js +122 -0
- package/src/ui/components/sortable_list.js +309 -0
- package/src/ui/components/table_state_store.js +81 -0
- package/src/ui/components/toast.js +59 -0
- package/src/ui/components/tree_view.js +460 -0
- package/src/ui/components/window_taskbar.js +143 -0
- package/src/ui/controllers/panel_state_machine.js +634 -0
- package/src/ui/controllers/window_chrome_controller.js +659 -0
- package/src/ui/notification_center.js +576 -0
- package/src/ui/utils/overlay_scrollbar.js +492 -0
- package/src/ui/utils/raf_resize_observer.js +31 -0
- package/src/ui/utils/tooltip_service.js +406 -0
package/dist/core.js
ADDED
|
@@ -0,0 +1,1098 @@
|
|
|
1
|
+
import {
|
|
2
|
+
StateMachine
|
|
3
|
+
} from "./chunk-3PHCPZHT.js";
|
|
4
|
+
import {
|
|
5
|
+
createSettingsStore,
|
|
6
|
+
getAllSettings,
|
|
7
|
+
getCategories,
|
|
8
|
+
getDefaultValue,
|
|
9
|
+
getSchema,
|
|
10
|
+
getSetting,
|
|
11
|
+
getSettingsByCategory,
|
|
12
|
+
registerSettings,
|
|
13
|
+
registerSettingsEventBus,
|
|
14
|
+
resetAllSettings,
|
|
15
|
+
resetCategory,
|
|
16
|
+
resetSetting,
|
|
17
|
+
setSetting
|
|
18
|
+
} from "./chunk-FL5KFNQH.js";
|
|
19
|
+
import "./chunk-JYWURG5T.js";
|
|
20
|
+
|
|
21
|
+
// src/core/event_bus.js
|
|
22
|
+
var PATTERN_TOKEN_RE = /[.*+?^${}()|[\]\\]/g;
|
|
23
|
+
var HAS_WILDCARD_RE = /[\*\?]/;
|
|
24
|
+
var EventBus = class {
|
|
25
|
+
constructor({ logger, historyLimit = 200 } = {}) {
|
|
26
|
+
this.logger = logger;
|
|
27
|
+
this.historyLimit = historyLimit;
|
|
28
|
+
this.sequence = 0;
|
|
29
|
+
this.listeners = /* @__PURE__ */ new Map();
|
|
30
|
+
this.wildcardListeners = /* @__PURE__ */ new Map();
|
|
31
|
+
this.listenerIndex = /* @__PURE__ */ new Map();
|
|
32
|
+
this.eventHistory = [];
|
|
33
|
+
}
|
|
34
|
+
on(eventName, handler, options = {}) {
|
|
35
|
+
if (typeof eventName !== "string" || !eventName.trim()) {
|
|
36
|
+
throw new TypeError("eventName must be a non-empty string");
|
|
37
|
+
}
|
|
38
|
+
if (typeof handler !== "function") {
|
|
39
|
+
throw new TypeError("handler must be a function");
|
|
40
|
+
}
|
|
41
|
+
const entry = {
|
|
42
|
+
id: ++this.sequence,
|
|
43
|
+
eventName,
|
|
44
|
+
handler,
|
|
45
|
+
namespace: options.namespace ?? null,
|
|
46
|
+
once: Boolean(options.once),
|
|
47
|
+
priority: options.priority ?? 0,
|
|
48
|
+
regex: null,
|
|
49
|
+
abortCleanup: null
|
|
50
|
+
};
|
|
51
|
+
if (HAS_WILDCARD_RE.test(eventName)) {
|
|
52
|
+
entry.regex = this._compilePattern(eventName);
|
|
53
|
+
this.wildcardListeners.set(entry.id, entry);
|
|
54
|
+
} else {
|
|
55
|
+
if (!this.listeners.has(eventName)) {
|
|
56
|
+
this.listeners.set(eventName, /* @__PURE__ */ new Set());
|
|
57
|
+
}
|
|
58
|
+
this.listeners.get(eventName).add(entry);
|
|
59
|
+
}
|
|
60
|
+
const useAbortSignal = typeof AbortSignal !== "undefined" && options.signal instanceof AbortSignal;
|
|
61
|
+
if (useAbortSignal) {
|
|
62
|
+
if (options.signal.aborted) {
|
|
63
|
+
return { dispose: () => {
|
|
64
|
+
}, id: entry.id };
|
|
65
|
+
}
|
|
66
|
+
const abortFn = () => this.off(entry.id);
|
|
67
|
+
options.signal.addEventListener("abort", abortFn, { once: true });
|
|
68
|
+
entry.abortCleanup = () => options.signal.removeEventListener("abort", abortFn);
|
|
69
|
+
}
|
|
70
|
+
this.listenerIndex.set(entry.id, entry);
|
|
71
|
+
return {
|
|
72
|
+
id: entry.id,
|
|
73
|
+
dispose: () => this.off(entry.id)
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
once(eventName, handler, options = {}) {
|
|
77
|
+
return this.on(eventName, handler, { ...options, once: true });
|
|
78
|
+
}
|
|
79
|
+
off(target) {
|
|
80
|
+
const id = typeof target === "number" ? target : target?.id;
|
|
81
|
+
if (!this.listenerIndex.has(id)) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
const entry = this.listenerIndex.get(id);
|
|
85
|
+
this.listenerIndex.delete(id);
|
|
86
|
+
if (entry.abortCleanup) {
|
|
87
|
+
try {
|
|
88
|
+
entry.abortCleanup();
|
|
89
|
+
} catch (err) {
|
|
90
|
+
this.logger?.warn?.("event-bus", "Abort cleanup failed", err);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (entry.regex) {
|
|
94
|
+
this.wildcardListeners.delete(entry.id);
|
|
95
|
+
} else {
|
|
96
|
+
const bucket = this.listeners.get(entry.eventName);
|
|
97
|
+
if (bucket) {
|
|
98
|
+
bucket.delete(entry);
|
|
99
|
+
if (bucket.size === 0) {
|
|
100
|
+
this.listeners.delete(entry.eventName);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
emit(eventName, payload = void 0, options = {}) {
|
|
107
|
+
const context = this._createContext(eventName, options);
|
|
108
|
+
const listeners = this._collectListeners(eventName, context.namespace);
|
|
109
|
+
this._recordHistory(eventName, context.namespace, listeners.length);
|
|
110
|
+
return this._invokeListeners(listeners, payload, context);
|
|
111
|
+
}
|
|
112
|
+
emitAsync(eventName, payload = void 0, options = {}) {
|
|
113
|
+
const context = this._createContext(eventName, options);
|
|
114
|
+
const listeners = this._collectListeners(eventName, context.namespace);
|
|
115
|
+
this._recordHistory(eventName, context.namespace, listeners.length);
|
|
116
|
+
return Promise.resolve().then(() => this._invokeListeners(listeners, payload, context));
|
|
117
|
+
}
|
|
118
|
+
listenerCount(eventName) {
|
|
119
|
+
const direct = this.listeners.get(eventName)?.size ?? 0;
|
|
120
|
+
const wildcard = [...this.wildcardListeners.values()].filter((entry) => entry.regex.test(eventName)).length;
|
|
121
|
+
return direct + wildcard;
|
|
122
|
+
}
|
|
123
|
+
getHistory() {
|
|
124
|
+
return [...this.eventHistory];
|
|
125
|
+
}
|
|
126
|
+
clearHistory() {
|
|
127
|
+
this.eventHistory.length = 0;
|
|
128
|
+
}
|
|
129
|
+
reset() {
|
|
130
|
+
this.listeners.clear();
|
|
131
|
+
this.wildcardListeners.clear();
|
|
132
|
+
this.listenerIndex.clear();
|
|
133
|
+
this.eventHistory.length = 0;
|
|
134
|
+
}
|
|
135
|
+
_collectListeners(eventName, namespace) {
|
|
136
|
+
const matches = [];
|
|
137
|
+
const bucket = this.listeners.get(eventName);
|
|
138
|
+
if (bucket) {
|
|
139
|
+
matches.push(...bucket);
|
|
140
|
+
}
|
|
141
|
+
for (const entry of this.wildcardListeners.values()) {
|
|
142
|
+
if (entry.regex.test(eventName)) {
|
|
143
|
+
matches.push(entry);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return matches.filter((entry) => !entry.namespace || entry.namespace === namespace).sort((a, b) => {
|
|
147
|
+
if (a.priority === b.priority) {
|
|
148
|
+
return a.id - b.id;
|
|
149
|
+
}
|
|
150
|
+
return b.priority - a.priority;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
_invokeListeners(listeners, payload, context) {
|
|
154
|
+
const results = [];
|
|
155
|
+
context.stopPropagation = () => {
|
|
156
|
+
context.__stopped = true;
|
|
157
|
+
};
|
|
158
|
+
for (const entry of listeners) {
|
|
159
|
+
if (context.__stopped) {
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
const result = entry.handler(payload, context);
|
|
164
|
+
results.push(result);
|
|
165
|
+
if (entry.once) {
|
|
166
|
+
this.off(entry.id);
|
|
167
|
+
}
|
|
168
|
+
} catch (err) {
|
|
169
|
+
this._handleListenerError(entry, context, err);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return results;
|
|
173
|
+
}
|
|
174
|
+
_createContext(eventName, options) {
|
|
175
|
+
return {
|
|
176
|
+
event: eventName,
|
|
177
|
+
namespace: options.namespace ?? null,
|
|
178
|
+
timestamp: Date.now(),
|
|
179
|
+
stopPropagation: () => {
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
_recordHistory(eventName, namespace, listenerCount) {
|
|
184
|
+
this.eventHistory.push({
|
|
185
|
+
event: eventName,
|
|
186
|
+
namespace,
|
|
187
|
+
timestamp: Date.now(),
|
|
188
|
+
listeners: listenerCount
|
|
189
|
+
});
|
|
190
|
+
if (this.eventHistory.length > this.historyLimit) {
|
|
191
|
+
this.eventHistory.shift();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
_handleListenerError(entry, context, error) {
|
|
195
|
+
this.logger?.error?.("event-bus", "Listener error", {
|
|
196
|
+
event: context.event,
|
|
197
|
+
namespace: context.namespace,
|
|
198
|
+
listenerId: entry.id,
|
|
199
|
+
error
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
_compilePattern(pattern) {
|
|
203
|
+
const escaped = pattern.replace(PATTERN_TOKEN_RE, "\\$&");
|
|
204
|
+
const regexSource = `^${escaped.replace(/\\\*/g, ".*").replace(/\\\?/g, ".")}$`;
|
|
205
|
+
return new RegExp(regexSource);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// src/core/logging.js
|
|
210
|
+
var LEVELS = ["trace", "debug", "info", "warn", "error", "fatal"];
|
|
211
|
+
var LEVEL_INDEX = new Map(LEVELS.map((level, idx) => [level, idx]));
|
|
212
|
+
var DEFAULT_NAMESPACE = "app";
|
|
213
|
+
var QUIET_MODE_LEVEL = "warn";
|
|
214
|
+
var LoggingService = class {
|
|
215
|
+
constructor({
|
|
216
|
+
name = "ecosim",
|
|
217
|
+
eventBus = null,
|
|
218
|
+
minLevel = "info",
|
|
219
|
+
historyLimit = 500,
|
|
220
|
+
transports = [],
|
|
221
|
+
enableConsole = false,
|
|
222
|
+
namespaceLevels = null
|
|
223
|
+
} = {}) {
|
|
224
|
+
this.name = name;
|
|
225
|
+
this.eventBus = eventBus;
|
|
226
|
+
this.historyLimit = historyLimit;
|
|
227
|
+
this.entries = [];
|
|
228
|
+
this.sequence = 0;
|
|
229
|
+
this.transports = /* @__PURE__ */ new Set();
|
|
230
|
+
this.namespaceLevels = { ...namespaceLevels || {} };
|
|
231
|
+
this._quietMode = false;
|
|
232
|
+
this._savedLevel = null;
|
|
233
|
+
this.setLevel(minLevel);
|
|
234
|
+
transports.forEach((transport) => this.addTransport(transport));
|
|
235
|
+
if (enableConsole) {
|
|
236
|
+
this.addTransport(this._consoleTransport.bind(this));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
setLevel(level) {
|
|
240
|
+
const normalized = this._normalizeLevel(level);
|
|
241
|
+
this.minLevel = normalized;
|
|
242
|
+
}
|
|
243
|
+
getLevel() {
|
|
244
|
+
return this.minLevel;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Enter quiet mode - suppresses DEBUG/INFO logs during simulation.
|
|
248
|
+
* Only WARNING and above are logged.
|
|
249
|
+
*/
|
|
250
|
+
enterQuietMode() {
|
|
251
|
+
if (this._quietMode) return;
|
|
252
|
+
this._quietMode = true;
|
|
253
|
+
this._savedLevel = this.minLevel;
|
|
254
|
+
if (LEVEL_INDEX.get(this.minLevel) < LEVEL_INDEX.get(QUIET_MODE_LEVEL)) {
|
|
255
|
+
this.minLevel = QUIET_MODE_LEVEL;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Exit quiet mode - restores previous log level.
|
|
260
|
+
*/
|
|
261
|
+
exitQuietMode() {
|
|
262
|
+
if (!this._quietMode) return;
|
|
263
|
+
this._quietMode = false;
|
|
264
|
+
if (this._savedLevel !== null) {
|
|
265
|
+
this.minLevel = this._savedLevel;
|
|
266
|
+
this._savedLevel = null;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Check if quiet mode is active.
|
|
271
|
+
*/
|
|
272
|
+
isQuietMode() {
|
|
273
|
+
return this._quietMode;
|
|
274
|
+
}
|
|
275
|
+
setNamespaceLevel(namespace, level) {
|
|
276
|
+
if (!namespace) return;
|
|
277
|
+
this.namespaceLevels[namespace] = this._normalizeLevel(level);
|
|
278
|
+
}
|
|
279
|
+
setNamespaceLevels(map) {
|
|
280
|
+
if (!map || typeof map !== "object") return;
|
|
281
|
+
Object.entries(map).forEach(([ns, lvl]) => {
|
|
282
|
+
if (ns) {
|
|
283
|
+
this.namespaceLevels[ns] = this._normalizeLevel(lvl);
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
getNamespaceLevel(namespace) {
|
|
288
|
+
if (!namespace) return void 0;
|
|
289
|
+
return this.namespaceLevels[namespace];
|
|
290
|
+
}
|
|
291
|
+
addTransport(transport) {
|
|
292
|
+
if (typeof transport !== "function") {
|
|
293
|
+
throw new TypeError("Logging transport must be a function");
|
|
294
|
+
}
|
|
295
|
+
this.transports.add(transport);
|
|
296
|
+
}
|
|
297
|
+
removeTransport(transport) {
|
|
298
|
+
this.transports.delete(transport);
|
|
299
|
+
}
|
|
300
|
+
log(level, message, metadata = null, options = {}) {
|
|
301
|
+
const normalizedLevel = this._normalizeLevel(level);
|
|
302
|
+
const namespace = options.namespace || DEFAULT_NAMESPACE;
|
|
303
|
+
if (!this._shouldLog(normalizedLevel, namespace)) {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
const entry = this._buildEntry(normalizedLevel, message, metadata, options);
|
|
307
|
+
this._recordHistory(entry);
|
|
308
|
+
this._emit(entry);
|
|
309
|
+
this._dispatch(entry);
|
|
310
|
+
return entry;
|
|
311
|
+
}
|
|
312
|
+
trace(message, metadata, options) {
|
|
313
|
+
return this.log("trace", message, metadata, options);
|
|
314
|
+
}
|
|
315
|
+
debug(message, metadata, options) {
|
|
316
|
+
return this.log("debug", message, metadata, options);
|
|
317
|
+
}
|
|
318
|
+
info(message, metadata, options) {
|
|
319
|
+
return this.log("info", message, metadata, options);
|
|
320
|
+
}
|
|
321
|
+
warn(message, metadata, options) {
|
|
322
|
+
return this.log("warn", message, metadata, options);
|
|
323
|
+
}
|
|
324
|
+
error(message, metadata, options) {
|
|
325
|
+
return this.log("error", message, metadata, options);
|
|
326
|
+
}
|
|
327
|
+
fatal(message, metadata, options) {
|
|
328
|
+
return this.log("fatal", message, metadata, options);
|
|
329
|
+
}
|
|
330
|
+
scoped(namespace) {
|
|
331
|
+
const ns = namespace || DEFAULT_NAMESPACE;
|
|
332
|
+
const make = (level) => (first, second, third) => {
|
|
333
|
+
if (typeof second === "string") {
|
|
334
|
+
return this.log(level, second, third ?? null, { namespace: ns });
|
|
335
|
+
}
|
|
336
|
+
return this.log(level, first, second ?? null, { namespace: ns });
|
|
337
|
+
};
|
|
338
|
+
return {
|
|
339
|
+
trace: make("trace"),
|
|
340
|
+
debug: make("debug"),
|
|
341
|
+
info: make("info"),
|
|
342
|
+
warn: make("warn"),
|
|
343
|
+
error: make("error"),
|
|
344
|
+
fatal: make("fatal")
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
getHistory() {
|
|
348
|
+
return [...this.entries];
|
|
349
|
+
}
|
|
350
|
+
clearHistory() {
|
|
351
|
+
this.entries.length = 0;
|
|
352
|
+
}
|
|
353
|
+
_normalizeLevel(level) {
|
|
354
|
+
const normalized = typeof level === "string" ? level.toLowerCase() : "";
|
|
355
|
+
if (!LEVEL_INDEX.has(normalized)) {
|
|
356
|
+
throw new Error(`Unknown log level "${level}"`);
|
|
357
|
+
}
|
|
358
|
+
return normalized;
|
|
359
|
+
}
|
|
360
|
+
_shouldLog(level, namespace) {
|
|
361
|
+
const effectiveLevel = this._resolveEffectiveLevel(namespace);
|
|
362
|
+
return LEVEL_INDEX.get(level) >= LEVEL_INDEX.get(effectiveLevel);
|
|
363
|
+
}
|
|
364
|
+
_resolveEffectiveLevel(namespace) {
|
|
365
|
+
if (namespace && this.namespaceLevels?.[namespace]) {
|
|
366
|
+
return this.namespaceLevels[namespace];
|
|
367
|
+
}
|
|
368
|
+
return this.minLevel;
|
|
369
|
+
}
|
|
370
|
+
_buildEntry(level, message, metadata, options) {
|
|
371
|
+
const namespace = options.namespace || DEFAULT_NAMESPACE;
|
|
372
|
+
const tags = options.tags || [];
|
|
373
|
+
const context = options.context || null;
|
|
374
|
+
const data = this._sanitizeMetadata(metadata);
|
|
375
|
+
return {
|
|
376
|
+
id: ++this.sequence,
|
|
377
|
+
timestamp: Date.now(),
|
|
378
|
+
service: this.name,
|
|
379
|
+
level,
|
|
380
|
+
namespace,
|
|
381
|
+
message,
|
|
382
|
+
data,
|
|
383
|
+
tags,
|
|
384
|
+
context
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
_sanitizeMetadata(metadata) {
|
|
388
|
+
if (!metadata) {
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
if (metadata instanceof Error) {
|
|
392
|
+
return this._errorToObject(metadata);
|
|
393
|
+
}
|
|
394
|
+
if (typeof metadata === "object") {
|
|
395
|
+
const clone = { ...metadata };
|
|
396
|
+
if (metadata.error instanceof Error) {
|
|
397
|
+
clone.error = this._errorToObject(metadata.error);
|
|
398
|
+
}
|
|
399
|
+
return clone;
|
|
400
|
+
}
|
|
401
|
+
return { value: metadata };
|
|
402
|
+
}
|
|
403
|
+
_errorToObject(error) {
|
|
404
|
+
return {
|
|
405
|
+
name: error.name,
|
|
406
|
+
message: error.message,
|
|
407
|
+
stack: error.stack
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
_recordHistory(entry) {
|
|
411
|
+
this.entries.push(entry);
|
|
412
|
+
if (this.entries.length > this.historyLimit) {
|
|
413
|
+
this.entries.shift();
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
_emit(entry) {
|
|
417
|
+
if (!this.eventBus) {
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
try {
|
|
421
|
+
this.eventBus.emit("log:entry", entry);
|
|
422
|
+
} catch (err) {
|
|
423
|
+
this._consoleTransport({ ...entry, level: "warn", message: `Failed to emit log entry: ${err.message}` });
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
_dispatch(entry) {
|
|
427
|
+
for (const transport of this.transports) {
|
|
428
|
+
try {
|
|
429
|
+
transport(entry);
|
|
430
|
+
} catch (err) {
|
|
431
|
+
this._consoleTransport({
|
|
432
|
+
...entry,
|
|
433
|
+
level: "warn",
|
|
434
|
+
namespace: "logging",
|
|
435
|
+
message: `Logging transport failed: ${err.message}`,
|
|
436
|
+
data: { transportError: this._errorToObject(err) }
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
_consoleTransport(entry) {
|
|
442
|
+
const prefix = `[${entry.service}][${entry.namespace}]`;
|
|
443
|
+
const method = entry.level === "trace" || entry.level === "debug" ? console.log : console[entry.level] || console.log;
|
|
444
|
+
method(`${prefix} ${entry.message}`, entry.data ?? "");
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
// src/core/state_guard_config.js
|
|
449
|
+
var DEFAULT_ALLOWED_ATTRIBUTES = Object.freeze([
|
|
450
|
+
"data-testid",
|
|
451
|
+
"data-field",
|
|
452
|
+
"data-descriptor-key",
|
|
453
|
+
"aria-label",
|
|
454
|
+
"aria-labelledby",
|
|
455
|
+
"aria-describedby",
|
|
456
|
+
"aria-hidden",
|
|
457
|
+
"aria-expanded",
|
|
458
|
+
"aria-selected",
|
|
459
|
+
"aria-disabled",
|
|
460
|
+
"aria-checked",
|
|
461
|
+
"aria-controls",
|
|
462
|
+
"aria-haspopup",
|
|
463
|
+
"aria-live",
|
|
464
|
+
"aria-busy",
|
|
465
|
+
"role",
|
|
466
|
+
"title",
|
|
467
|
+
"twm-has-tooltip",
|
|
468
|
+
"data-tooltip",
|
|
469
|
+
"data-help-topic"
|
|
470
|
+
]);
|
|
471
|
+
var DEFAULT_ALLOWED_ATTRIBUTE_PREFIXES = Object.freeze([]);
|
|
472
|
+
var DEFAULT_ALLOWED_DATASET_KEYS = Object.freeze([
|
|
473
|
+
"testid",
|
|
474
|
+
"phase",
|
|
475
|
+
"page-key",
|
|
476
|
+
"deleting",
|
|
477
|
+
// Notification center
|
|
478
|
+
"notification-id",
|
|
479
|
+
"notificationId",
|
|
480
|
+
// Plotly.js internal attributes (third-party charting library)
|
|
481
|
+
"unformatted",
|
|
482
|
+
"toggle",
|
|
483
|
+
"notex",
|
|
484
|
+
"math",
|
|
485
|
+
"originalindex",
|
|
486
|
+
"original-index",
|
|
487
|
+
"gravity",
|
|
488
|
+
"attr",
|
|
489
|
+
"val",
|
|
490
|
+
"subplot",
|
|
491
|
+
"group",
|
|
492
|
+
"trace",
|
|
493
|
+
"point",
|
|
494
|
+
"curveNumber",
|
|
495
|
+
"pointNumber",
|
|
496
|
+
// UI state keys used by original HTML structure
|
|
497
|
+
"active-content",
|
|
498
|
+
"activeContent",
|
|
499
|
+
"panel-content",
|
|
500
|
+
"panelContent",
|
|
501
|
+
"collapsible-id",
|
|
502
|
+
"collapsibleId",
|
|
503
|
+
"collapsible-default",
|
|
504
|
+
"collapsibleDefault",
|
|
505
|
+
"twm-collapsible-header",
|
|
506
|
+
"collapsibleHeader",
|
|
507
|
+
"twm-collapsible-content",
|
|
508
|
+
"collapsibleContent",
|
|
509
|
+
"collapsible-group",
|
|
510
|
+
"collapsibleGroup",
|
|
511
|
+
"collapsible-exclusive",
|
|
512
|
+
"collapsibleExclusive",
|
|
513
|
+
"collapsible-float",
|
|
514
|
+
"collapsibleFloat",
|
|
515
|
+
"collapsible-fill",
|
|
516
|
+
"collapsibleFill",
|
|
517
|
+
"tool",
|
|
518
|
+
"tab",
|
|
519
|
+
"role",
|
|
520
|
+
"op",
|
|
521
|
+
"func-name",
|
|
522
|
+
"funcName",
|
|
523
|
+
"func-expr",
|
|
524
|
+
"funcExpr",
|
|
525
|
+
"arity",
|
|
526
|
+
"tooltip",
|
|
527
|
+
"twm-latex-tooltip",
|
|
528
|
+
"latexTooltip",
|
|
529
|
+
"node-type",
|
|
530
|
+
"nodeType",
|
|
531
|
+
"twm-node-name",
|
|
532
|
+
"nodeName",
|
|
533
|
+
"lang",
|
|
534
|
+
"playback-control",
|
|
535
|
+
"playbackControl",
|
|
536
|
+
// Simulation/scenario state keys
|
|
537
|
+
"status",
|
|
538
|
+
"scenario-id",
|
|
539
|
+
"scenarioId",
|
|
540
|
+
"run-id",
|
|
541
|
+
"runId",
|
|
542
|
+
"run-state",
|
|
543
|
+
"runState",
|
|
544
|
+
"selected",
|
|
545
|
+
"active",
|
|
546
|
+
"disabled",
|
|
547
|
+
"expanded",
|
|
548
|
+
"collapsed",
|
|
549
|
+
"visible",
|
|
550
|
+
"hidden",
|
|
551
|
+
// Node/canvas state keys
|
|
552
|
+
"node-id",
|
|
553
|
+
"nodeId",
|
|
554
|
+
"connector-id",
|
|
555
|
+
"connectorId",
|
|
556
|
+
"connector-type",
|
|
557
|
+
"connectorType",
|
|
558
|
+
"connection-id",
|
|
559
|
+
"connectionId",
|
|
560
|
+
"input-index",
|
|
561
|
+
"inputIndex",
|
|
562
|
+
"output-index",
|
|
563
|
+
"outputIndex",
|
|
564
|
+
"port-index",
|
|
565
|
+
"portIndex",
|
|
566
|
+
// Scenario editor keys
|
|
567
|
+
"source",
|
|
568
|
+
"series-id",
|
|
569
|
+
"seriesId",
|
|
570
|
+
"dataset-id",
|
|
571
|
+
"datasetId",
|
|
572
|
+
"mirror-input-bound",
|
|
573
|
+
"mirrorInputBound",
|
|
574
|
+
"mirror-change-bound",
|
|
575
|
+
"mirrorChangeBound",
|
|
576
|
+
"mirror-input",
|
|
577
|
+
"mirrorInput",
|
|
578
|
+
"bound",
|
|
579
|
+
"field",
|
|
580
|
+
"key",
|
|
581
|
+
"namespace",
|
|
582
|
+
"namespace-id",
|
|
583
|
+
"namespaceId",
|
|
584
|
+
"tab-id",
|
|
585
|
+
"tabId",
|
|
586
|
+
"param-id",
|
|
587
|
+
"paramId",
|
|
588
|
+
"target-id",
|
|
589
|
+
"targetId",
|
|
590
|
+
"override-type",
|
|
591
|
+
"overrideType",
|
|
592
|
+
// Tree view component keys
|
|
593
|
+
"item-id",
|
|
594
|
+
"itemId",
|
|
595
|
+
"category-id",
|
|
596
|
+
"categoryId",
|
|
597
|
+
// Calibration page keys
|
|
598
|
+
"info-for",
|
|
599
|
+
"infoFor",
|
|
600
|
+
"column",
|
|
601
|
+
// Data page keys
|
|
602
|
+
"readonly",
|
|
603
|
+
"id",
|
|
604
|
+
"action",
|
|
605
|
+
"type",
|
|
606
|
+
"name",
|
|
607
|
+
"path",
|
|
608
|
+
"index",
|
|
609
|
+
"count",
|
|
610
|
+
"total",
|
|
611
|
+
"page",
|
|
612
|
+
"sort",
|
|
613
|
+
"order",
|
|
614
|
+
"filter",
|
|
615
|
+
"search",
|
|
616
|
+
"value",
|
|
617
|
+
"label",
|
|
618
|
+
"title",
|
|
619
|
+
"description",
|
|
620
|
+
// DSL generator keys
|
|
621
|
+
"dsl-state",
|
|
622
|
+
"dslState",
|
|
623
|
+
"dsl-section-registered",
|
|
624
|
+
"dslSectionRegistered",
|
|
625
|
+
"dsl-scrollbars-installed",
|
|
626
|
+
"dslScrollbarsInstalled",
|
|
627
|
+
"dsl-tabs-bound",
|
|
628
|
+
"dslTabsBound",
|
|
629
|
+
"dsl-controls-bound",
|
|
630
|
+
"dslControlsBound",
|
|
631
|
+
// Inline highlighter keys
|
|
632
|
+
"inline-hl",
|
|
633
|
+
"inlineHl",
|
|
634
|
+
"tooltip",
|
|
635
|
+
// Overlay scrollbar keys (UI-only, not state storage)
|
|
636
|
+
"scrollable",
|
|
637
|
+
"scrollable-x",
|
|
638
|
+
"scrollableX",
|
|
639
|
+
"scrollable-y",
|
|
640
|
+
"scrollableY",
|
|
641
|
+
// ManagedWindow component keys
|
|
642
|
+
"window-id",
|
|
643
|
+
"windowId",
|
|
644
|
+
// Help system keys
|
|
645
|
+
"help-topic",
|
|
646
|
+
"helpTopic"
|
|
647
|
+
]);
|
|
648
|
+
var DEFAULT_OVERRIDES = Object.freeze([
|
|
649
|
+
{
|
|
650
|
+
// Panel controller writes dataset hooks for field identification/focus.
|
|
651
|
+
pattern: /js_new\/ui\/controllers\/panel_controller\.js/,
|
|
652
|
+
datasetKeys: ["field", "descriptor-key", "descriptorKey"],
|
|
653
|
+
attributes: ["data-field", "data-descriptor-key"],
|
|
654
|
+
attributePrefixes: []
|
|
655
|
+
}
|
|
656
|
+
]);
|
|
657
|
+
var DEFAULT_MODE = typeof window !== "undefined" && Boolean(window.__ECOSIM_DEBUG__) ? "strict" : "warn";
|
|
658
|
+
var DEFAULT_STATE_GUARD_CONFIG = Object.freeze({
|
|
659
|
+
mode: DEFAULT_MODE,
|
|
660
|
+
allowedAttributes: DEFAULT_ALLOWED_ATTRIBUTES,
|
|
661
|
+
allowedAttributePrefixes: DEFAULT_ALLOWED_ATTRIBUTE_PREFIXES,
|
|
662
|
+
allowedDatasetKeys: DEFAULT_ALLOWED_DATASET_KEYS,
|
|
663
|
+
moduleOverrides: DEFAULT_OVERRIDES
|
|
664
|
+
});
|
|
665
|
+
function normalizeStateGuardConfig(config = {}) {
|
|
666
|
+
if (!config || typeof config !== "object") {
|
|
667
|
+
return { ...DEFAULT_STATE_GUARD_CONFIG };
|
|
668
|
+
}
|
|
669
|
+
return {
|
|
670
|
+
mode: normalizeMode(config.mode ?? DEFAULT_STATE_GUARD_CONFIG.mode),
|
|
671
|
+
allowedAttributes: normalizeStringList(
|
|
672
|
+
config.allowedAttributes ?? DEFAULT_STATE_GUARD_CONFIG.allowedAttributes
|
|
673
|
+
),
|
|
674
|
+
allowedAttributePrefixes: normalizeStringList(
|
|
675
|
+
config.allowedAttributePrefixes ?? DEFAULT_STATE_GUARD_CONFIG.allowedAttributePrefixes
|
|
676
|
+
),
|
|
677
|
+
allowedDatasetKeys: normalizeStringList(
|
|
678
|
+
config.allowedDatasetKeys ?? DEFAULT_STATE_GUARD_CONFIG.allowedDatasetKeys
|
|
679
|
+
),
|
|
680
|
+
moduleOverrides: normalizeOverrides(
|
|
681
|
+
config.moduleOverrides ?? DEFAULT_STATE_GUARD_CONFIG.moduleOverrides
|
|
682
|
+
)
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
function normalizeMode(mode) {
|
|
686
|
+
const normalized = typeof mode === "string" ? mode.toLowerCase() : "";
|
|
687
|
+
return normalized === "off" || normalized === "warn" || normalized === "strict" ? normalized : DEFAULT_STATE_GUARD_CONFIG.mode;
|
|
688
|
+
}
|
|
689
|
+
function normalizeStringList(value) {
|
|
690
|
+
if (!value) {
|
|
691
|
+
return [];
|
|
692
|
+
}
|
|
693
|
+
if (Array.isArray(value)) {
|
|
694
|
+
return value.map((entry) => typeof entry === "string" ? entry.trim() : "").filter(Boolean);
|
|
695
|
+
}
|
|
696
|
+
if (typeof value === "string") {
|
|
697
|
+
return value.split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
698
|
+
}
|
|
699
|
+
return [];
|
|
700
|
+
}
|
|
701
|
+
function normalizeOverrides(overrides) {
|
|
702
|
+
if (!Array.isArray(overrides)) {
|
|
703
|
+
return [];
|
|
704
|
+
}
|
|
705
|
+
return overrides.map((entry) => normalizeOverride(entry)).filter(Boolean);
|
|
706
|
+
}
|
|
707
|
+
function normalizeOverride(entry) {
|
|
708
|
+
if (!entry || typeof entry !== "object") {
|
|
709
|
+
return null;
|
|
710
|
+
}
|
|
711
|
+
if (!entry.pattern) {
|
|
712
|
+
return null;
|
|
713
|
+
}
|
|
714
|
+
const attributes = normalizeStringList(entry.attributes);
|
|
715
|
+
const datasetKeys = normalizeStringList(entry.datasetKeys);
|
|
716
|
+
const prefixes = normalizeStringList(entry.attributePrefixes);
|
|
717
|
+
if (!attributes.length && !datasetKeys.length && !prefixes.length) {
|
|
718
|
+
return null;
|
|
719
|
+
}
|
|
720
|
+
return {
|
|
721
|
+
pattern: patternToRegExp(entry.pattern),
|
|
722
|
+
attributes,
|
|
723
|
+
datasetKeys,
|
|
724
|
+
attributePrefixes: prefixes
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
function patternToRegExp(pattern) {
|
|
728
|
+
if (pattern instanceof RegExp) {
|
|
729
|
+
return pattern;
|
|
730
|
+
}
|
|
731
|
+
const source = String(pattern ?? "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
732
|
+
return new RegExp(source);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// src/core/state_guard.js
|
|
736
|
+
var HOOK_REGISTRY = {
|
|
737
|
+
installed: false,
|
|
738
|
+
service: null,
|
|
739
|
+
originals: {
|
|
740
|
+
setAttribute: null,
|
|
741
|
+
removeAttribute: null,
|
|
742
|
+
getAttribute: null
|
|
743
|
+
},
|
|
744
|
+
datasetDescriptor: null,
|
|
745
|
+
datasetProxyCache: /* @__PURE__ */ new WeakMap()
|
|
746
|
+
};
|
|
747
|
+
var STACK_CACHE = /* @__PURE__ */ new Map();
|
|
748
|
+
var DATA_ATTRIBUTE_PREFIX = "data-";
|
|
749
|
+
var EVENT_CHANNELS = Object.freeze({
|
|
750
|
+
READY: "state-guard:ready",
|
|
751
|
+
VIOLATION: "state-guard:violation",
|
|
752
|
+
MODE_CHANGED: "state-guard:mode-changed"
|
|
753
|
+
});
|
|
754
|
+
var StateGuardService = class {
|
|
755
|
+
constructor({
|
|
756
|
+
eventBus = null,
|
|
757
|
+
logger = null,
|
|
758
|
+
config = DEFAULT_STATE_GUARD_CONFIG
|
|
759
|
+
} = {}) {
|
|
760
|
+
this.eventBus = eventBus;
|
|
761
|
+
this.logger = logger;
|
|
762
|
+
this.enabled = true;
|
|
763
|
+
this.bypassDepth = 0;
|
|
764
|
+
this.config = normalizeStateGuardConfig(config);
|
|
765
|
+
this.mode = this.config.mode;
|
|
766
|
+
this.allowedAttributes = new Set(this.config.allowedAttributes.map((attr) => attr.toLowerCase()));
|
|
767
|
+
this.allowedAttributePrefixes = this.config.allowedAttributePrefixes.map((attr) => attr.toLowerCase());
|
|
768
|
+
this.allowedDatasetKeys = new Set(this.config.allowedDatasetKeys.map((key) => key.toLowerCase()));
|
|
769
|
+
this.moduleOverrides = (this.config.moduleOverrides ?? []).map((entry) => ({
|
|
770
|
+
pattern: entry.pattern,
|
|
771
|
+
attributes: new Set(entry.attributes.map((attr) => attr.toLowerCase())),
|
|
772
|
+
datasetKeys: new Set(entry.datasetKeys.map((key) => key.toLowerCase())),
|
|
773
|
+
attributePrefixes: entry.attributePrefixes.map((prefix) => prefix.toLowerCase())
|
|
774
|
+
}));
|
|
775
|
+
this.#installHooks();
|
|
776
|
+
}
|
|
777
|
+
setMode(mode) {
|
|
778
|
+
const normalized = typeof mode === "string" ? mode.toLowerCase() : "";
|
|
779
|
+
if (!["off", "warn", "strict"].includes(normalized)) {
|
|
780
|
+
throw new Error(`Invalid state guard mode "${mode}"`);
|
|
781
|
+
}
|
|
782
|
+
if (this.mode === normalized) {
|
|
783
|
+
return this.mode;
|
|
784
|
+
}
|
|
785
|
+
this.mode = normalized;
|
|
786
|
+
this.eventBus?.emit?.(EVENT_CHANNELS.MODE_CHANGED, {
|
|
787
|
+
mode: this.mode,
|
|
788
|
+
timestamp: Date.now()
|
|
789
|
+
});
|
|
790
|
+
return this.mode;
|
|
791
|
+
}
|
|
792
|
+
getMode() {
|
|
793
|
+
return this.mode;
|
|
794
|
+
}
|
|
795
|
+
allow(modulePattern, attributes = [], { datasetKeys = [], attributePrefixes = [] } = {}) {
|
|
796
|
+
const pattern = modulePattern instanceof RegExp ? modulePattern : new RegExp(String(modulePattern ?? "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
797
|
+
const entry = {
|
|
798
|
+
pattern,
|
|
799
|
+
attributes: new Set(attributes.map((attr) => String(attr).toLowerCase())),
|
|
800
|
+
datasetKeys: new Set(datasetKeys.map((key) => String(key).toLowerCase())),
|
|
801
|
+
attributePrefixes: attributePrefixes.map((prefix) => String(prefix).toLowerCase())
|
|
802
|
+
};
|
|
803
|
+
this.moduleOverrides.push(entry);
|
|
804
|
+
return () => {
|
|
805
|
+
const idx = this.moduleOverrides.indexOf(entry);
|
|
806
|
+
if (idx >= 0) {
|
|
807
|
+
this.moduleOverrides.splice(idx, 1);
|
|
808
|
+
}
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
executeWithBypass(label, fn) {
|
|
812
|
+
if (typeof fn !== "function") {
|
|
813
|
+
throw new TypeError("executeWithBypass requires a function");
|
|
814
|
+
}
|
|
815
|
+
try {
|
|
816
|
+
this.bypassDepth += 1;
|
|
817
|
+
return fn();
|
|
818
|
+
} finally {
|
|
819
|
+
this.bypassDepth = Math.max(0, this.bypassDepth - 1);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
/**
|
|
823
|
+
* Execute an async function with state guard bypass.
|
|
824
|
+
* The bypass remains active for the entire duration of the async operation.
|
|
825
|
+
* @param {string} label - Label for debugging
|
|
826
|
+
* @param {Function} fn - Async function to execute
|
|
827
|
+
* @returns {Promise<*>} Result of the function
|
|
828
|
+
*/
|
|
829
|
+
async executeWithBypassAsync(label, fn) {
|
|
830
|
+
if (typeof fn !== "function") {
|
|
831
|
+
throw new TypeError("executeWithBypassAsync requires a function");
|
|
832
|
+
}
|
|
833
|
+
try {
|
|
834
|
+
this.bypassDepth += 1;
|
|
835
|
+
return await fn();
|
|
836
|
+
} finally {
|
|
837
|
+
this.bypassDepth = Math.max(0, this.bypassDepth - 1);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
handleAttributeMutation(element, action, attribute, value) {
|
|
841
|
+
const attrName = this.#normalizeAttribute(attribute);
|
|
842
|
+
if (!this.#shouldGuard(attrName)) {
|
|
843
|
+
return;
|
|
844
|
+
}
|
|
845
|
+
this.#evaluateMutation({
|
|
846
|
+
action,
|
|
847
|
+
attribute: attrName,
|
|
848
|
+
element,
|
|
849
|
+
value,
|
|
850
|
+
source: "attribute"
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
handleDatasetMutation(element, key, value, { deleting = false } = {}) {
|
|
854
|
+
const attrName = this.#datasetKeyToAttribute(key);
|
|
855
|
+
if (!this.#shouldGuard(attrName)) {
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
this.#evaluateMutation({
|
|
859
|
+
action: deleting ? "dataset:delete" : "dataset:set",
|
|
860
|
+
attribute: attrName,
|
|
861
|
+
element,
|
|
862
|
+
value,
|
|
863
|
+
source: "dataset"
|
|
864
|
+
});
|
|
865
|
+
}
|
|
866
|
+
#installHooks() {
|
|
867
|
+
if (typeof Element === "undefined" || typeof HTMLElement === "undefined") {
|
|
868
|
+
this.enabled = false;
|
|
869
|
+
this.logger?.warn?.("state-guard", "DOM environment unavailable; guard disabled");
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
installDomHooks(this);
|
|
873
|
+
this.eventBus?.emit?.(EVENT_CHANNELS.READY, {
|
|
874
|
+
mode: this.mode,
|
|
875
|
+
timestamp: Date.now()
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
#shouldGuard(attribute) {
|
|
879
|
+
if (!this.enabled) {
|
|
880
|
+
return false;
|
|
881
|
+
}
|
|
882
|
+
if (this.mode === "off") {
|
|
883
|
+
return false;
|
|
884
|
+
}
|
|
885
|
+
if (this.bypassDepth > 0) {
|
|
886
|
+
return false;
|
|
887
|
+
}
|
|
888
|
+
if (!attribute || !attribute.startsWith(DATA_ATTRIBUTE_PREFIX)) {
|
|
889
|
+
return false;
|
|
890
|
+
}
|
|
891
|
+
if (this.allowedAttributes.has(attribute)) {
|
|
892
|
+
return false;
|
|
893
|
+
}
|
|
894
|
+
if (attribute.startsWith(DATA_ATTRIBUTE_PREFIX)) {
|
|
895
|
+
const datasetKey = attribute.slice(DATA_ATTRIBUTE_PREFIX.length);
|
|
896
|
+
if (this.allowedDatasetKeys.has(datasetKey)) {
|
|
897
|
+
return false;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
if (this.allowedAttributePrefixes.some((prefix) => attribute.startsWith(prefix))) {
|
|
901
|
+
return false;
|
|
902
|
+
}
|
|
903
|
+
return true;
|
|
904
|
+
}
|
|
905
|
+
#evaluateMutation({ action, attribute, element, value, source }) {
|
|
906
|
+
const moduleKey = this.#resolveModuleFromStack();
|
|
907
|
+
if (this.#isAllowedForModule(moduleKey, attribute)) {
|
|
908
|
+
return;
|
|
909
|
+
}
|
|
910
|
+
const payload = {
|
|
911
|
+
attribute,
|
|
912
|
+
action,
|
|
913
|
+
source,
|
|
914
|
+
module: moduleKey,
|
|
915
|
+
mode: this.mode,
|
|
916
|
+
timestamp: Date.now()
|
|
917
|
+
};
|
|
918
|
+
this.logger?.warn?.("state-guard", "Blocked DOM state mutation", {
|
|
919
|
+
...payload,
|
|
920
|
+
valuePreview: this.#previewValue(value)
|
|
921
|
+
});
|
|
922
|
+
this.eventBus?.emit?.(EVENT_CHANNELS.VIOLATION, payload);
|
|
923
|
+
if (this.mode === "strict") {
|
|
924
|
+
const label = moduleKey ? `${moduleKey}` : "unknown module";
|
|
925
|
+
throw new Error(`StateGuard blocked ${action} on "${attribute}" from ${label}`);
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
#isAllowedForModule(moduleKey, attribute) {
|
|
929
|
+
if (!moduleKey || !this.moduleOverrides.length) {
|
|
930
|
+
return false;
|
|
931
|
+
}
|
|
932
|
+
const lowerAttr = attribute.toLowerCase();
|
|
933
|
+
for (const entry of this.moduleOverrides) {
|
|
934
|
+
if (!entry.pattern.test(moduleKey)) {
|
|
935
|
+
continue;
|
|
936
|
+
}
|
|
937
|
+
if (entry.attributes.has(lowerAttr)) {
|
|
938
|
+
return true;
|
|
939
|
+
}
|
|
940
|
+
if (entry.attributePrefixes.some((prefix) => lowerAttr.startsWith(prefix))) {
|
|
941
|
+
return true;
|
|
942
|
+
}
|
|
943
|
+
if (lowerAttr.startsWith(DATA_ATTRIBUTE_PREFIX)) {
|
|
944
|
+
const datasetKey = lowerAttr.slice(DATA_ATTRIBUTE_PREFIX.length);
|
|
945
|
+
if (entry.datasetKeys.has(datasetKey)) {
|
|
946
|
+
return true;
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
return false;
|
|
951
|
+
}
|
|
952
|
+
#normalizeAttribute(attribute) {
|
|
953
|
+
if (!attribute) {
|
|
954
|
+
return "";
|
|
955
|
+
}
|
|
956
|
+
return String(attribute).toLowerCase();
|
|
957
|
+
}
|
|
958
|
+
#datasetKeyToAttribute(key) {
|
|
959
|
+
if (!key && key !== 0) {
|
|
960
|
+
return "";
|
|
961
|
+
}
|
|
962
|
+
const normalized = String(key).replace(/([A-Z])/g, "-$1").replace(/[^a-zA-Z0-9_-]/g, "-").toLowerCase().replace(/-{2,}/g, "-").replace(/^-+/, "").replace(/-+$/, "");
|
|
963
|
+
if (!normalized) {
|
|
964
|
+
return "";
|
|
965
|
+
}
|
|
966
|
+
return `${DATA_ATTRIBUTE_PREFIX}${normalized}`;
|
|
967
|
+
}
|
|
968
|
+
#resolveModuleFromStack() {
|
|
969
|
+
const stack = new Error().stack;
|
|
970
|
+
if (!stack) {
|
|
971
|
+
return null;
|
|
972
|
+
}
|
|
973
|
+
if (STACK_CACHE.has(stack)) {
|
|
974
|
+
return STACK_CACHE.get(stack);
|
|
975
|
+
}
|
|
976
|
+
const lines = stack.split("\n");
|
|
977
|
+
let modulePath = null;
|
|
978
|
+
for (const line of lines) {
|
|
979
|
+
const match = line.match(/(js_new\/[^)\s]+)/);
|
|
980
|
+
if (match) {
|
|
981
|
+
modulePath = match[1];
|
|
982
|
+
break;
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
if (STACK_CACHE.size > 500) {
|
|
986
|
+
STACK_CACHE.clear();
|
|
987
|
+
}
|
|
988
|
+
STACK_CACHE.set(stack, modulePath);
|
|
989
|
+
return modulePath;
|
|
990
|
+
}
|
|
991
|
+
#previewValue(value) {
|
|
992
|
+
if (value === null || value === void 0) {
|
|
993
|
+
return String(value);
|
|
994
|
+
}
|
|
995
|
+
if (typeof value === "object") {
|
|
996
|
+
return "[object]";
|
|
997
|
+
}
|
|
998
|
+
const str = String(value);
|
|
999
|
+
return str.length > 50 ? `${str.slice(0, 47)}...` : str;
|
|
1000
|
+
}
|
|
1001
|
+
};
|
|
1002
|
+
function installDomHooks(service) {
|
|
1003
|
+
if (HOOK_REGISTRY.installed) {
|
|
1004
|
+
HOOK_REGISTRY.service = service;
|
|
1005
|
+
return;
|
|
1006
|
+
}
|
|
1007
|
+
HOOK_REGISTRY.installed = true;
|
|
1008
|
+
HOOK_REGISTRY.service = service;
|
|
1009
|
+
const { setAttribute, removeAttribute } = Element.prototype;
|
|
1010
|
+
const nativeSetAttribute = setAttribute;
|
|
1011
|
+
const nativeRemoveAttribute = removeAttribute;
|
|
1012
|
+
HOOK_REGISTRY.originals.setAttribute = nativeSetAttribute;
|
|
1013
|
+
HOOK_REGISTRY.originals.removeAttribute = nativeRemoveAttribute;
|
|
1014
|
+
Element.prototype.setAttribute = function patchedSetAttribute(name, value) {
|
|
1015
|
+
const guard = HOOK_REGISTRY.service;
|
|
1016
|
+
try {
|
|
1017
|
+
guard?.handleAttributeMutation(this, "setAttribute", name, value);
|
|
1018
|
+
} catch (error) {
|
|
1019
|
+
throw error;
|
|
1020
|
+
}
|
|
1021
|
+
return nativeSetAttribute.call(this, name, value);
|
|
1022
|
+
};
|
|
1023
|
+
Element.prototype.removeAttribute = function patchedRemoveAttribute(name) {
|
|
1024
|
+
const guard = HOOK_REGISTRY.service;
|
|
1025
|
+
try {
|
|
1026
|
+
guard?.handleAttributeMutation(this, "removeAttribute", name, void 0);
|
|
1027
|
+
} catch (error) {
|
|
1028
|
+
throw error;
|
|
1029
|
+
}
|
|
1030
|
+
return nativeRemoveAttribute.call(this, name);
|
|
1031
|
+
};
|
|
1032
|
+
wrapDataset(service);
|
|
1033
|
+
}
|
|
1034
|
+
function wrapDataset(service) {
|
|
1035
|
+
const descriptor = Object.getOwnPropertyDescriptor(HTMLElement.prototype, "dataset");
|
|
1036
|
+
if (!descriptor || typeof descriptor.get !== "function") {
|
|
1037
|
+
return;
|
|
1038
|
+
}
|
|
1039
|
+
HOOK_REGISTRY.datasetDescriptor = descriptor;
|
|
1040
|
+
Object.defineProperty(HTMLElement.prototype, "dataset", {
|
|
1041
|
+
configurable: true,
|
|
1042
|
+
enumerable: descriptor.enumerable,
|
|
1043
|
+
get: function datasetGetter() {
|
|
1044
|
+
const nativeDataset = descriptor.get.call(this);
|
|
1045
|
+
if (!nativeDataset || typeof nativeDataset !== "object") {
|
|
1046
|
+
return nativeDataset;
|
|
1047
|
+
}
|
|
1048
|
+
const cached = HOOK_REGISTRY.datasetProxyCache.get(nativeDataset);
|
|
1049
|
+
if (cached && cached.proxy) {
|
|
1050
|
+
cached.element = this;
|
|
1051
|
+
return cached.proxy;
|
|
1052
|
+
}
|
|
1053
|
+
const proxyState = { element: this };
|
|
1054
|
+
const proxy = new Proxy(nativeDataset, {
|
|
1055
|
+
set(target, prop, value) {
|
|
1056
|
+
const guard = HOOK_REGISTRY.service;
|
|
1057
|
+
const key = typeof prop === "string" ? prop : null;
|
|
1058
|
+
if (guard && key) {
|
|
1059
|
+
guard.handleDatasetMutation(proxyState.element, key, value);
|
|
1060
|
+
}
|
|
1061
|
+
return Reflect.set(target, prop, value);
|
|
1062
|
+
},
|
|
1063
|
+
deleteProperty(target, prop) {
|
|
1064
|
+
const guard = HOOK_REGISTRY.service;
|
|
1065
|
+
const key = typeof prop === "string" ? prop : null;
|
|
1066
|
+
if (guard && key) {
|
|
1067
|
+
guard.handleDatasetMutation(proxyState.element, key, void 0, { deleting: true });
|
|
1068
|
+
}
|
|
1069
|
+
return Reflect.deleteProperty(target, prop);
|
|
1070
|
+
}
|
|
1071
|
+
});
|
|
1072
|
+
HOOK_REGISTRY.datasetProxyCache.set(nativeDataset, { proxy, element: this });
|
|
1073
|
+
return proxy;
|
|
1074
|
+
}
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
export {
|
|
1078
|
+
DEFAULT_STATE_GUARD_CONFIG,
|
|
1079
|
+
EventBus,
|
|
1080
|
+
LoggingService,
|
|
1081
|
+
StateGuardService,
|
|
1082
|
+
StateMachine,
|
|
1083
|
+
createSettingsStore,
|
|
1084
|
+
getAllSettings,
|
|
1085
|
+
getCategories,
|
|
1086
|
+
getDefaultValue,
|
|
1087
|
+
getSchema,
|
|
1088
|
+
getSetting,
|
|
1089
|
+
getSettingsByCategory,
|
|
1090
|
+
normalizeStateGuardConfig,
|
|
1091
|
+
registerSettings,
|
|
1092
|
+
registerSettingsEventBus,
|
|
1093
|
+
resetAllSettings,
|
|
1094
|
+
resetCategory,
|
|
1095
|
+
resetSetting,
|
|
1096
|
+
setSetting
|
|
1097
|
+
};
|
|
1098
|
+
//# sourceMappingURL=core.js.map
|