wu-framework 1.1.7 → 1.1.9

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.
Files changed (95) hide show
  1. package/LICENSE +19 -1
  2. package/README.md +257 -1122
  3. package/dist/wu-framework.cjs.js +3 -1
  4. package/dist/wu-framework.cjs.js.map +1 -0
  5. package/dist/wu-framework.dev.js +9867 -3183
  6. package/dist/wu-framework.dev.js.map +1 -1
  7. package/dist/wu-framework.esm.js +3 -0
  8. package/dist/wu-framework.esm.js.map +1 -0
  9. package/dist/wu-framework.umd.js +3 -1
  10. package/dist/wu-framework.umd.js.map +1 -0
  11. package/integrations/astro/README.md +127 -0
  12. package/integrations/astro/WuApp.astro +63 -0
  13. package/integrations/astro/WuShell.astro +39 -0
  14. package/integrations/astro/index.js +68 -0
  15. package/integrations/astro/package.json +38 -0
  16. package/integrations/astro/types.d.ts +53 -0
  17. package/package.json +96 -72
  18. package/src/adapters/angular/ai.js +30 -0
  19. package/src/adapters/angular/index.d.ts +154 -0
  20. package/src/adapters/angular/index.js +932 -0
  21. package/src/adapters/angular.d.ts +3 -154
  22. package/src/adapters/angular.js +3 -813
  23. package/src/adapters/index.js +35 -24
  24. package/src/adapters/lit/ai.js +20 -0
  25. package/src/adapters/lit/index.d.ts +120 -0
  26. package/src/adapters/lit/index.js +721 -0
  27. package/src/adapters/lit.d.ts +3 -120
  28. package/src/adapters/lit.js +3 -726
  29. package/src/adapters/preact/ai.js +33 -0
  30. package/src/adapters/preact/index.d.ts +108 -0
  31. package/src/adapters/preact/index.js +661 -0
  32. package/src/adapters/preact.d.ts +3 -108
  33. package/src/adapters/preact.js +3 -665
  34. package/src/adapters/react/ai.js +135 -0
  35. package/src/adapters/react/index.d.ts +246 -0
  36. package/src/adapters/react/index.js +694 -0
  37. package/src/adapters/react.d.ts +3 -212
  38. package/src/adapters/react.js +3 -513
  39. package/src/adapters/shared.js +64 -0
  40. package/src/adapters/solid/ai.js +32 -0
  41. package/src/adapters/solid/index.d.ts +101 -0
  42. package/src/adapters/solid/index.js +586 -0
  43. package/src/adapters/solid.d.ts +3 -101
  44. package/src/adapters/solid.js +3 -591
  45. package/src/adapters/svelte/ai.js +31 -0
  46. package/src/adapters/svelte/index.d.ts +166 -0
  47. package/src/adapters/svelte/index.js +798 -0
  48. package/src/adapters/svelte.d.ts +3 -166
  49. package/src/adapters/svelte.js +3 -803
  50. package/src/adapters/vanilla/ai.js +30 -0
  51. package/src/adapters/vanilla/index.d.ts +179 -0
  52. package/src/adapters/vanilla/index.js +785 -0
  53. package/src/adapters/vanilla.d.ts +3 -179
  54. package/src/adapters/vanilla.js +3 -791
  55. package/src/adapters/vue/ai.js +52 -0
  56. package/src/adapters/vue/index.d.ts +299 -0
  57. package/src/adapters/vue/index.js +608 -0
  58. package/src/adapters/vue.d.ts +3 -299
  59. package/src/adapters/vue.js +3 -611
  60. package/src/ai/wu-ai-actions.js +261 -0
  61. package/src/ai/wu-ai-agent.js +546 -0
  62. package/src/ai/wu-ai-browser-primitives.js +354 -0
  63. package/src/ai/wu-ai-browser.js +380 -0
  64. package/src/ai/wu-ai-context.js +332 -0
  65. package/src/ai/wu-ai-conversation.js +613 -0
  66. package/src/ai/wu-ai-orchestrate.js +1021 -0
  67. package/src/ai/wu-ai-permissions.js +381 -0
  68. package/src/ai/wu-ai-provider.js +700 -0
  69. package/src/ai/wu-ai-schema.js +225 -0
  70. package/src/ai/wu-ai-triggers.js +396 -0
  71. package/src/ai/wu-ai.js +804 -0
  72. package/src/core/wu-app.js +50 -8
  73. package/src/core/wu-cache.js +2 -3
  74. package/src/core/wu-core.js +648 -681
  75. package/src/core/wu-html-parser.js +121 -211
  76. package/src/core/wu-iframe-sandbox.js +328 -0
  77. package/src/core/wu-mcp-bridge.js +431 -0
  78. package/src/core/wu-overrides.js +510 -0
  79. package/src/core/wu-plugin.js +4 -1
  80. package/src/core/wu-prefetch.js +414 -0
  81. package/src/core/wu-proxy-sandbox.js +398 -75
  82. package/src/core/wu-sandbox.js +86 -268
  83. package/src/core/wu-script-executor.js +79 -182
  84. package/src/core/wu-snapshot-sandbox.js +149 -106
  85. package/src/core/wu-strategies.js +13 -0
  86. package/src/core/wu-style-bridge.js +23 -23
  87. package/src/index.js +162 -665
  88. package/dist/wu-framework.hex.js +0 -23
  89. package/dist/wu-framework.min.js +0 -1
  90. package/dist/wu-framework.obf.js +0 -1
  91. package/scripts/build-protected.js +0 -366
  92. package/scripts/build.js +0 -212
  93. package/scripts/rollup-plugin-hex.js +0 -143
  94. package/src/core/wu-registry.js +0 -60
  95. package/src/core/wu-sandbox-pool.js +0 -390
@@ -0,0 +1,328 @@
1
+ /**
2
+ * WU-IFRAME-SANDBOX: Real JS isolation using hidden iframes.
3
+ *
4
+ * Architecture:
5
+ * ┌── Main Window ────────────────────────────────┐
6
+ * │ ┌── Shadow DOM Container ──────────────────┐ │
7
+ * │ │ App renders here (CSS isolated) │ │
8
+ * │ └──────────────────────────────────────────┘ │
9
+ * │ ┌── Hidden iframe ────────────────────────┐ │
10
+ * │ │ import() runs here (REAL modules) │ │
11
+ * │ │ window = iframe.contentWindow (ISOLATED)│ │
12
+ * │ │ document patched → Shadow DOM │ │
13
+ * │ └────────────────────────────────────────-─┘ │
14
+ * └───────────────────────────────────────────────┘
15
+ *
16
+ * Why iframe?
17
+ * - import() is REAL → tree shaking, source maps, HMR all work
18
+ * - iframe has its own window → globals are isolated
19
+ * - Destroying iframe kills all timers/listeners at once
20
+ *
21
+ * How it works:
22
+ * 1. Create hidden iframe with <base href="appUrl"> for URL resolution
23
+ * 2. Patch iframe's document: createElement → main document (no ownerDocument issues),
24
+ * querySelector/body → Shadow DOM container
25
+ * 3. Track timers for guaranteed cleanup (some browsers don't kill iframe timers)
26
+ * 4. import() the app module inside iframe → runs in isolated context
27
+ * 5. App calls wu.define() → lifecycle registered on parent's WuCore
28
+ * 6. On unmount: destroy iframe = nuclear cleanup
29
+ *
30
+ * Fallback:
31
+ * If import() fails (CORS, module errors), wu-core falls back to eval mode
32
+ * (fetch HTML + parse + execute with(proxy)).
33
+ */
34
+
35
+ import { logger } from './wu-logger.js';
36
+
37
+ export class WuIframeSandbox {
38
+ constructor(appName) {
39
+ this.appName = appName;
40
+ this.iframe = null;
41
+ this._active = false;
42
+
43
+ // Side-effect tracking for guaranteed cleanup
44
+ this._timers = new Set();
45
+ this._intervals = new Set();
46
+ this._rafs = new Set();
47
+ this._listeners = [];
48
+ }
49
+
50
+ /**
51
+ * Create and activate the iframe sandbox.
52
+ *
53
+ * @param {string} appUrl - App's base URL (for <base href> and relative imports)
54
+ * @param {HTMLElement} shadowContainer - Shadow DOM container for DOM redirection
55
+ * @param {ShadowRoot|null} shadowRoot - Shadow root for query scoping
56
+ * @returns {Window} The iframe's contentWindow (isolated execution context)
57
+ */
58
+ activate(appUrl, shadowContainer, shadowRoot) {
59
+ if (this._active) return this.iframe.contentWindow;
60
+
61
+ // 1. Create hidden iframe
62
+ const iframe = document.createElement('iframe');
63
+ iframe.setAttribute('data-wu-sandbox', this.appName);
64
+ iframe.style.cssText = 'display:none !important;position:absolute;width:0;height:0;border:0;';
65
+
66
+ // Must be in DOM before accessing contentWindow
67
+ document.body.appendChild(iframe);
68
+ this.iframe = iframe;
69
+
70
+ // 2. Write base HTML with <base href> pointing to app URL.
71
+ // This makes relative URL resolution work for fetch(), CSS url(), etc.
72
+ // import() of full URLs works regardless of base.
73
+ const baseUrl = appUrl.replace(/\/$/, '');
74
+ const iframeWin = iframe.contentWindow;
75
+ const iframeDoc = iframeWin.document;
76
+
77
+ iframeDoc.open();
78
+ iframeDoc.write(
79
+ `<!DOCTYPE html><html><head><base href="${baseUrl}/"></head><body></body></html>`
80
+ );
81
+ iframeDoc.close();
82
+
83
+ // 3. Make wu available inside iframe for wu.define()
84
+ iframeWin.wu = window.wu;
85
+
86
+ // 4. Patch document: redirect DOM operations to Shadow DOM
87
+ this._patchDocument(iframeWin, shadowContainer, shadowRoot);
88
+
89
+ // 5. Track timers for guaranteed cleanup
90
+ this._patchTimers(iframeWin);
91
+
92
+ this._active = true;
93
+ logger.wuDebug(`[IframeSandbox] Activated for ${this.appName} (base: ${baseUrl})`);
94
+ return iframeWin;
95
+ }
96
+
97
+ /**
98
+ * Import an ES module inside the iframe via real import().
99
+ * Preserves tree shaking, source maps, and Vite HMR.
100
+ *
101
+ * @param {string} url - Full module URL to import
102
+ * @param {number} [timeout=30000] - Max wait time in ms
103
+ * @returns {Promise<void>}
104
+ */
105
+ importModule(url, timeout = 30000) {
106
+ if (!this._active) {
107
+ throw new Error(`[IframeSandbox] Not active for ${this.appName}`);
108
+ }
109
+
110
+ return new Promise((resolve, reject) => {
111
+ const channelId = `wu_${this.appName}_${Date.now()}`;
112
+
113
+ // Listen for import completion via postMessage
114
+ const onMessage = (event) => {
115
+ if (event.data?.channelId !== channelId) return;
116
+ cleanup();
117
+ if (event.data.error) {
118
+ reject(new Error(event.data.error));
119
+ } else {
120
+ resolve();
121
+ }
122
+ };
123
+
124
+ const timer = setTimeout(() => {
125
+ cleanup();
126
+ reject(new Error(
127
+ `[IframeSandbox] import() timed out for ${this.appName}: ${url}`
128
+ ));
129
+ }, timeout);
130
+
131
+ const cleanup = () => {
132
+ window.removeEventListener('message', onMessage);
133
+ clearTimeout(timer);
134
+ };
135
+
136
+ window.addEventListener('message', onMessage);
137
+
138
+ // Inject module script into iframe
139
+ const iframeDoc = this.iframe.contentWindow.document;
140
+ const script = iframeDoc.createElement('script');
141
+ script.type = 'module';
142
+ script.textContent =
143
+ `import("${url.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}")` +
144
+ `.then(() => parent.postMessage({ channelId: "${channelId}", success: true }, '*'))` +
145
+ `.catch(e => parent.postMessage({ channelId: "${channelId}", error: e.message || String(e) }, '*'));`;
146
+
147
+ iframeDoc.head.appendChild(script);
148
+ logger.wuDebug(`[IframeSandbox] Importing module: ${url}`);
149
+ });
150
+ }
151
+
152
+ /**
153
+ * Patch the iframe's document to redirect DOM operations.
154
+ *
155
+ * Critical patches:
156
+ * - createElement/createTextNode → main document (avoids ownerDocument mismatch)
157
+ * React/Vue create nodes and append to Shadow DOM container.
158
+ * Nodes must belong to the main document to avoid cross-document adoption issues.
159
+ *
160
+ * - querySelector/body → Shadow DOM container
161
+ * Libraries that query the document will find app elements in the Shadow DOM.
162
+ *
163
+ * - addEventListener → tracked for cleanup
164
+ */
165
+ _patchDocument(iframeWin, shadowContainer, shadowRoot) {
166
+ const iframeDoc = iframeWin.document;
167
+ const queryTarget = shadowRoot || shadowContainer;
168
+ const mainDoc = document; // parent document
169
+
170
+ // --- Node creation: use main document to avoid ownerDocument mismatch ---
171
+ // React uses container.ownerDocument.createElement() internally,
172
+ // but other code might use document.createElement() directly.
173
+ // By redirecting to main document, all nodes belong to the same document tree.
174
+ iframeDoc.createElement = (tag, options) => mainDoc.createElement(tag, options);
175
+ iframeDoc.createElementNS = (ns, tag, options) => mainDoc.createElementNS(ns, tag, options);
176
+ iframeDoc.createTextNode = (text) => mainDoc.createTextNode(text);
177
+ iframeDoc.createComment = (text) => mainDoc.createComment(text);
178
+ iframeDoc.createDocumentFragment = () => mainDoc.createDocumentFragment();
179
+
180
+ // --- DOM queries: redirect to Shadow DOM ---
181
+ iframeDoc.querySelector = (sel) => queryTarget.querySelector(sel);
182
+ iframeDoc.querySelectorAll = (sel) => queryTarget.querySelectorAll(sel);
183
+ iframeDoc.getElementById = (id) => queryTarget.querySelector(`#${id}`);
184
+ iframeDoc.getElementsByClassName = (cls) => queryTarget.querySelectorAll(`.${cls}`);
185
+ iframeDoc.getElementsByTagName = (tag) => queryTarget.querySelectorAll(tag);
186
+
187
+ // --- document.body → shadow container ---
188
+ // Frameworks that append to document.body (portals, modals) will target the Shadow DOM.
189
+ try {
190
+ Object.defineProperty(iframeDoc, 'body', {
191
+ get: () => shadowContainer,
192
+ configurable: true
193
+ });
194
+ } catch {
195
+ // Some environments don't allow redefining body — not critical
196
+ logger.wuDebug('[IframeSandbox] Could not redefine document.body');
197
+ }
198
+
199
+ // --- document.addEventListener: track for cleanup ---
200
+ const origDocAdd = iframeDoc.addEventListener.bind(iframeDoc);
201
+ const origDocRemove = iframeDoc.removeEventListener.bind(iframeDoc);
202
+
203
+ iframeDoc.addEventListener = (event, handler, options) => {
204
+ this._listeners.push({ target: iframeDoc, event, handler, options });
205
+ origDocAdd(event, handler, options);
206
+ };
207
+
208
+ iframeDoc.removeEventListener = (event, handler, options) => {
209
+ this._listeners = this._listeners.filter(
210
+ l => !(l.target === iframeDoc && l.event === event && l.handler === handler)
211
+ );
212
+ origDocRemove(event, handler, options);
213
+ };
214
+
215
+ logger.wuDebug(`[IframeSandbox] Document patched for ${this.appName}`);
216
+ }
217
+
218
+ /**
219
+ * Patch timers in the iframe for guaranteed cleanup.
220
+ * Some browsers don't fully kill timers when an iframe is removed.
221
+ * We track all IDs and clear them explicitly on destroy.
222
+ */
223
+ _patchTimers(iframeWin) {
224
+ const origSetTimeout = iframeWin.setTimeout.bind(iframeWin);
225
+ const origClearTimeout = iframeWin.clearTimeout.bind(iframeWin);
226
+ const origSetInterval = iframeWin.setInterval.bind(iframeWin);
227
+ const origClearInterval = iframeWin.clearInterval.bind(iframeWin);
228
+
229
+ iframeWin.setTimeout = (fn, ms, ...args) => {
230
+ const id = origSetTimeout((...a) => {
231
+ this._timers.delete(id);
232
+ if (typeof fn === 'function') fn(...a);
233
+ }, ms, ...args);
234
+ this._timers.add(id);
235
+ return id;
236
+ };
237
+
238
+ iframeWin.clearTimeout = (id) => {
239
+ this._timers.delete(id);
240
+ origClearTimeout(id);
241
+ };
242
+
243
+ iframeWin.setInterval = (fn, ms, ...args) => {
244
+ const id = origSetInterval(fn, ms, ...args);
245
+ this._intervals.add(id);
246
+ return id;
247
+ };
248
+
249
+ iframeWin.clearInterval = (id) => {
250
+ this._intervals.delete(id);
251
+ origClearInterval(id);
252
+ };
253
+
254
+ // requestAnimationFrame may not exist in all iframe contexts
255
+ if (iframeWin.requestAnimationFrame) {
256
+ const origRAF = iframeWin.requestAnimationFrame.bind(iframeWin);
257
+ const origCancelRAF = iframeWin.cancelAnimationFrame.bind(iframeWin);
258
+
259
+ iframeWin.requestAnimationFrame = (fn) => {
260
+ const id = origRAF((...a) => {
261
+ this._rafs.delete(id);
262
+ fn(...a);
263
+ });
264
+ this._rafs.add(id);
265
+ return id;
266
+ };
267
+
268
+ iframeWin.cancelAnimationFrame = (id) => {
269
+ this._rafs.delete(id);
270
+ origCancelRAF(id);
271
+ };
272
+ }
273
+
274
+ logger.wuDebug(`[IframeSandbox] Timer tracking active for ${this.appName}`);
275
+ }
276
+
277
+ /**
278
+ * Destroy the iframe and all side effects.
279
+ * Nuclear cleanup: kills everything at once.
280
+ */
281
+ destroy() {
282
+ if (!this._active) return;
283
+ this._active = false;
284
+
285
+ // 1. Clear all tracked timers
286
+ for (const id of this._timers) { try { clearTimeout(id); } catch {} }
287
+ for (const id of this._intervals) { try { clearInterval(id); } catch {} }
288
+ for (const id of this._rafs) { try { cancelAnimationFrame(id); } catch {} }
289
+ this._timers.clear();
290
+ this._intervals.clear();
291
+ this._rafs.clear();
292
+
293
+ // 2. Remove all tracked event listeners
294
+ for (const { target, event, handler, options } of this._listeners) {
295
+ try { target.removeEventListener(event, handler, options); } catch {}
296
+ }
297
+ this._listeners = [];
298
+
299
+ // 3. Wipe and remove iframe
300
+ if (this.iframe) {
301
+ try {
302
+ const doc = this.iframe.contentDocument;
303
+ if (doc) {
304
+ doc.open();
305
+ doc.write('');
306
+ doc.close();
307
+ }
308
+ } catch {
309
+ // Cross-origin or already detached — ignore
310
+ }
311
+
312
+ if (this.iframe.parentNode) {
313
+ this.iframe.parentNode.removeChild(this.iframe);
314
+ }
315
+ this.iframe = null;
316
+ }
317
+
318
+ logger.wuDebug(`[IframeSandbox] Destroyed for ${this.appName}`);
319
+ }
320
+
321
+ /**
322
+ * Check if this sandbox is active.
323
+ * @returns {boolean}
324
+ */
325
+ isActive() {
326
+ return this._active;
327
+ }
328
+ }