agent-web-os 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/dist/__sw__.js ADDED
@@ -0,0 +1,421 @@
1
+ /**
2
+ * Service Worker for Mini WebContainers
3
+ * Intercepts fetch requests and routes them to virtual servers
4
+ * Version: 15 - cleanup: extract helpers, gate debug logs, remove test endpoints
5
+ */
6
+
7
+ const DEBUG = false;
8
+
9
+ // Communication port with main thread
10
+ let mainPort = null;
11
+
12
+ // Pending requests waiting for response
13
+ const pendingRequests = new Map();
14
+ let requestId = 0;
15
+
16
+ // Registered virtual server ports
17
+ const registeredPorts = new Set();
18
+
19
+ /**
20
+ * Decode base64 string to Uint8Array
21
+ */
22
+ function base64ToBytes(base64) {
23
+ const binary = atob(base64);
24
+ const bytes = new Uint8Array(binary.length);
25
+ for (let i = 0; i < binary.length; i++) {
26
+ bytes[i] = binary.charCodeAt(i);
27
+ }
28
+ return bytes;
29
+ }
30
+
31
+ /**
32
+ * Handle messages from main thread
33
+ */
34
+ self.addEventListener('message', (event) => {
35
+ const { type, data } = event.data;
36
+
37
+ DEBUG && console.log('[SW] Received message:', type, 'hasPort in event.ports:', event.ports?.length > 0);
38
+
39
+ // When a MessagePort is transferred, it's in event.ports[0], not event.data.port
40
+ if (type === 'init' && event.ports && event.ports[0]) {
41
+ // Initialize communication channel
42
+ mainPort = event.ports[0];
43
+ mainPort.onmessage = handleMainMessage;
44
+ DEBUG && console.log('[SW] Initialized communication channel with transferred port');
45
+ // Re-claim clients so that pages opened after SW activation get controlled.
46
+ // Without this, controllerchange never fires for late-arriving pages.
47
+ self.clients.claim();
48
+ }
49
+
50
+ if (type === 'server-registered' && data) {
51
+ registeredPorts.add(data.port);
52
+ DEBUG && console.log(`[SW] Server registered on port ${data.port}`);
53
+ }
54
+
55
+ if (type === 'server-unregistered' && data) {
56
+ registeredPorts.delete(data.port);
57
+ DEBUG && console.log(`[SW] Server unregistered from port ${data.port}`);
58
+ }
59
+ });
60
+
61
+ /**
62
+ * Handle response messages from main thread
63
+ */
64
+ function handleMainMessage(event) {
65
+ const { type, id, data, error } = event.data;
66
+
67
+ DEBUG && console.log('[SW] Received message from main:', type, 'id:', id);
68
+
69
+ if (type === 'response') {
70
+ const pending = pendingRequests.get(id);
71
+ DEBUG && console.log('[SW] Looking for pending request:', id, 'found:', !!pending);
72
+
73
+ if (pending) {
74
+ pendingRequests.delete(id);
75
+
76
+ if (error) {
77
+ DEBUG && console.log('[SW] Response error:', error);
78
+ pending.reject(new Error(error));
79
+ } else {
80
+ DEBUG && console.log('[SW] Response data:', {
81
+ statusCode: data?.statusCode,
82
+ statusMessage: data?.statusMessage,
83
+ headers: data?.headers,
84
+ bodyType: data?.body?.constructor?.name,
85
+ bodyLength: data?.body?.length || data?.body?.byteLength,
86
+ });
87
+ pending.resolve(data);
88
+ }
89
+ }
90
+ }
91
+
92
+ // Handle streaming responses
93
+ if (type === 'stream-start') {
94
+ DEBUG && console.log('[SW] stream-start received, id:', id);
95
+ const pending = pendingRequests.get(id);
96
+ if (pending && pending.streamController) {
97
+ // Store headers/status for the streaming response
98
+ pending.streamData = data;
99
+ pending.resolveHeaders(data);
100
+ DEBUG && console.log('[SW] headers resolved for stream', id);
101
+ } else {
102
+ DEBUG && console.log('[SW] No pending request or controller for stream-start', id, !!pending, pending?.streamController);
103
+ }
104
+ }
105
+
106
+ if (type === 'stream-chunk') {
107
+ DEBUG && console.log('[SW] stream-chunk received, id:', id, 'size:', data?.chunkBase64?.length);
108
+ const pending = pendingRequests.get(id);
109
+ if (pending && pending.streamController) {
110
+ try {
111
+ // Decode base64 chunk and enqueue
112
+ if (data.chunkBase64) {
113
+ const bytes = base64ToBytes(data.chunkBase64);
114
+ pending.streamController.enqueue(bytes);
115
+ DEBUG && console.log('[SW] chunk enqueued, bytes:', bytes.length);
116
+ }
117
+ } catch (e) {
118
+ console.error('[SW] Error enqueueing chunk:', e);
119
+ }
120
+ } else {
121
+ DEBUG && console.log('[SW] No pending request or controller for stream-chunk', id);
122
+ }
123
+ }
124
+
125
+ if (type === 'stream-end') {
126
+ DEBUG && console.log('[SW] stream-end received, id:', id);
127
+ const pending = pendingRequests.get(id);
128
+ if (pending && pending.streamController) {
129
+ try {
130
+ pending.streamController.close();
131
+ DEBUG && console.log('[SW] stream closed');
132
+ } catch (e) {
133
+ DEBUG && console.log('[SW] stream already closed');
134
+ }
135
+ pendingRequests.delete(id);
136
+ }
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Send request to main thread and wait for response
142
+ */
143
+ async function sendRequest(port, method, url, headers, body) {
144
+ DEBUG && console.log('[SW] sendRequest called, mainPort:', !!mainPort, 'url:', url);
145
+
146
+ if (!mainPort) {
147
+ // Ask all clients to re-send the init message
148
+ const allClients = await self.clients.matchAll({ type: 'window' });
149
+ for (const client of allClients) {
150
+ client.postMessage({ type: 'sw-needs-init' });
151
+ }
152
+ // Wait up to 5s for a client to re-initialize the port
153
+ // (main thread may be busy with heavy operations like CLI execution)
154
+ await new Promise(resolve => {
155
+ const check = setInterval(() => { if (mainPort) { clearInterval(check); resolve(); } }, 50);
156
+ setTimeout(() => { clearInterval(check); resolve(); }, 5000);
157
+ });
158
+ if (!mainPort) {
159
+ throw new Error('Service Worker not initialized - no connection to main thread');
160
+ }
161
+ }
162
+
163
+ const id = ++requestId;
164
+
165
+ return new Promise((resolve, reject) => {
166
+ pendingRequests.set(id, { resolve, reject });
167
+
168
+ // Set timeout for request
169
+ setTimeout(() => {
170
+ if (pendingRequests.has(id)) {
171
+ pendingRequests.delete(id);
172
+ reject(new Error('Request timeout'));
173
+ }
174
+ }, 30000);
175
+
176
+ mainPort.postMessage({
177
+ type: 'request',
178
+ id,
179
+ data: { port, method, url, headers, body },
180
+ });
181
+ });
182
+ }
183
+
184
+ /**
185
+ * Send streaming request to main thread
186
+ * Returns a ReadableStream that receives chunks from main thread
187
+ */
188
+ async function sendStreamingRequest(port, method, url, headers, body) {
189
+ DEBUG && console.log('[SW] sendStreamingRequest called, url:', url);
190
+
191
+ if (!mainPort) {
192
+ // Ask all clients to re-send the init message
193
+ const allClients = await self.clients.matchAll({ type: 'window' });
194
+ for (const client of allClients) {
195
+ client.postMessage({ type: 'sw-needs-init' });
196
+ }
197
+ await new Promise(resolve => {
198
+ const check = setInterval(() => { if (mainPort) { clearInterval(check); resolve(); } }, 50);
199
+ setTimeout(() => { clearInterval(check); resolve(); }, 5000);
200
+ });
201
+ if (!mainPort) {
202
+ throw new Error('Service Worker not initialized');
203
+ }
204
+ }
205
+
206
+ const id = ++requestId;
207
+
208
+ let streamController;
209
+ let resolveHeaders;
210
+ const headersPromise = new Promise(resolve => { resolveHeaders = resolve; });
211
+
212
+ const stream = new ReadableStream({
213
+ start(controller) {
214
+ streamController = controller;
215
+
216
+ // Store in pending requests so handleMainMessage can find it
217
+ pendingRequests.set(id, {
218
+ resolve: () => {},
219
+ reject: (err) => controller.error(err),
220
+ streamController: controller,
221
+ resolveHeaders,
222
+ });
223
+
224
+ // Send request to main thread with streaming flag
225
+ mainPort.postMessage({
226
+ type: 'request',
227
+ id,
228
+ data: { port, method, url, headers, body, streaming: true },
229
+ });
230
+ },
231
+ cancel() {
232
+ pendingRequests.delete(id);
233
+ }
234
+ });
235
+
236
+ return { stream, headersPromise, id };
237
+ }
238
+
239
+ /**
240
+ * Intercept fetch requests
241
+ */
242
+ self.addEventListener('fetch', (event) => {
243
+ const url = new URL(event.request.url);
244
+
245
+ DEBUG && console.log('[SW] Fetch:', url.pathname, 'mainPort:', !!mainPort);
246
+
247
+ // Check if this is a virtual server request
248
+ const match = url.pathname.match(/^\/__virtual__\/(\d+)(\/.*)?$/);
249
+
250
+ if (!match) {
251
+ // Not a virtual request - but check if it's from a virtual context
252
+ // This handles plain <a href="/about"> links and asset requests (images, scripts)
253
+ // that should stay within the virtual server
254
+ const referer = event.request.referrer;
255
+ if (referer) {
256
+ try {
257
+ const refererUrl = new URL(referer);
258
+ const refererMatch = refererUrl.pathname.match(/^\/__virtual__\/(\d+)/);
259
+ if (refererMatch) {
260
+ // Request from within a virtual server context
261
+ const virtualPrefix = refererMatch[0];
262
+ const virtualPort = parseInt(refererMatch[1], 10);
263
+ const targetPath = url.pathname + url.search;
264
+
265
+ if (event.request.mode === 'navigate') {
266
+ // Navigation requests: redirect to include the virtual prefix
267
+ const redirectUrl = url.origin + virtualPrefix + targetPath;
268
+ DEBUG && console.log('[SW] Redirecting navigation from virtual context:', url.pathname, '->', redirectUrl);
269
+ event.respondWith(Response.redirect(redirectUrl, 302));
270
+ return;
271
+ } else {
272
+ // Non-navigation requests (images, scripts, etc.): forward to virtual server
273
+ DEBUG && console.log('[SW] Forwarding resource from virtual context:', url.pathname);
274
+ event.respondWith(handleVirtualRequest(event.request, virtualPort, targetPath));
275
+ return;
276
+ }
277
+ }
278
+ } catch (e) {
279
+ // Invalid referer URL, ignore
280
+ }
281
+ }
282
+ // Not a virtual request, let it pass through
283
+ return;
284
+ }
285
+
286
+ DEBUG && console.log('[SW] Virtual request:', url.pathname);
287
+
288
+ const port = parseInt(match[1], 10);
289
+ const path = match[2] || '/';
290
+
291
+ event.respondWith(handleVirtualRequest(event.request, port, path + url.search));
292
+ });
293
+
294
+ /**
295
+ * Handle a request to a virtual server
296
+ */
297
+ async function handleVirtualRequest(request, port, path) {
298
+ try {
299
+ // Build headers object
300
+ const headers = {};
301
+ request.headers.forEach((value, key) => {
302
+ headers[key] = value;
303
+ });
304
+
305
+ // Get body if present
306
+ let body = null;
307
+ if (request.method !== 'GET' && request.method !== 'HEAD') {
308
+ body = await request.arrayBuffer();
309
+ }
310
+
311
+ // Check if this is an API route that might stream (POST to /api/*)
312
+ const isStreamingCandidate = request.method === 'POST' && path.startsWith('/api/');
313
+
314
+ if (isStreamingCandidate) {
315
+ DEBUG && console.log('[SW] Using streaming mode for:', path);
316
+ return handleStreamingRequest(port, request.method, path, headers, body);
317
+ }
318
+ DEBUG && console.log('[SW] Using non-streaming mode for:', request.method, path);
319
+
320
+ // Send to main thread
321
+ const response = await sendRequest(port, request.method, path, headers, body);
322
+
323
+ DEBUG && console.log('[SW] Got response from main thread:', {
324
+ statusCode: response.statusCode,
325
+ headersKeys: response.headers ? Object.keys(response.headers) : [],
326
+ bodyBase64Length: response.bodyBase64?.length,
327
+ });
328
+
329
+ // Decode base64 body and create response
330
+ let finalResponse;
331
+ if (response.bodyBase64 && response.bodyBase64.length > 0) {
332
+ try {
333
+ const bytes = base64ToBytes(response.bodyBase64);
334
+ DEBUG && console.log('[SW] Decoded body length:', bytes.length);
335
+
336
+ // Use Blob to ensure proper body handling
337
+ const blob = new Blob([bytes], { type: response.headers['Content-Type'] || 'application/octet-stream' });
338
+ DEBUG && console.log('[SW] Created blob size:', blob.size);
339
+
340
+ // Merge response headers with CORP/COEP headers to allow iframe embedding
341
+ // The parent page has COEP: credentialless, so we need matching headers
342
+ const respHeaders = new Headers(response.headers);
343
+ respHeaders.set('Cross-Origin-Embedder-Policy', 'credentialless');
344
+ respHeaders.set('Cross-Origin-Opener-Policy', 'same-origin');
345
+ respHeaders.set('Cross-Origin-Resource-Policy', 'cross-origin');
346
+ // Remove any headers that might block iframe loading
347
+ respHeaders.delete('X-Frame-Options');
348
+
349
+ finalResponse = new Response(blob, {
350
+ status: response.statusCode,
351
+ statusText: response.statusMessage,
352
+ headers: respHeaders,
353
+ });
354
+ } catch (decodeError) {
355
+ console.error('[SW] Failed to decode base64 body:', decodeError);
356
+ finalResponse = new Response(`Decode error: ${decodeError.message}`, {
357
+ status: 500,
358
+ headers: { 'Content-Type': 'text/plain' },
359
+ });
360
+ }
361
+ } else {
362
+ finalResponse = new Response(null, {
363
+ status: response.statusCode,
364
+ statusText: response.statusMessage,
365
+ headers: response.headers,
366
+ });
367
+ }
368
+
369
+ DEBUG && console.log('[SW] Final Response created, status:', finalResponse.status);
370
+
371
+ return finalResponse;
372
+ } catch (error) {
373
+ console.error('[SW] Error handling virtual request:', error);
374
+ return new Response(`Service Worker Error: ${error.message}`, {
375
+ status: 500,
376
+ statusText: 'Internal Server Error',
377
+ headers: { 'Content-Type': 'text/plain' },
378
+ });
379
+ }
380
+ }
381
+
382
+ /**
383
+ * Handle a streaming request
384
+ */
385
+ async function handleStreamingRequest(port, method, path, headers, body) {
386
+ const { stream, headersPromise, id } = await sendStreamingRequest(port, method, path, headers, body);
387
+
388
+ // Wait for headers to arrive
389
+ const responseData = await headersPromise;
390
+
391
+ DEBUG && console.log('[SW] Streaming response started:', responseData?.statusCode);
392
+
393
+ // Build response headers
394
+ const respHeaders = new Headers(responseData?.headers || {});
395
+ respHeaders.set('Cross-Origin-Embedder-Policy', 'credentialless');
396
+ respHeaders.set('Cross-Origin-Opener-Policy', 'same-origin');
397
+ respHeaders.set('Cross-Origin-Resource-Policy', 'cross-origin');
398
+ respHeaders.delete('X-Frame-Options');
399
+
400
+ return new Response(stream, {
401
+ status: responseData?.statusCode || 200,
402
+ statusText: responseData?.statusMessage || 'OK',
403
+ headers: respHeaders,
404
+ });
405
+ }
406
+
407
+ /**
408
+ * Activate immediately
409
+ */
410
+ self.addEventListener('install', (event) => {
411
+ DEBUG && console.log('[SW] Installing...');
412
+ event.waitUntil(self.skipWaiting());
413
+ });
414
+
415
+ /**
416
+ * Claim all clients immediately
417
+ */
418
+ self.addEventListener('activate', (event) => {
419
+ DEBUG && console.log('[SW] Activated');
420
+ event.waitUntil(self.clients.claim());
421
+ });
@@ -0,0 +1,43 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
8
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
9
+ }) : x)(function(x) {
10
+ if (typeof require !== "undefined") return require.apply(this, arguments);
11
+ throw Error('Dynamic require of "' + x + '" is not supported');
12
+ });
13
+ var __commonJS = (cb, mod) => function __require2() {
14
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
15
+ };
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
36
+
37
+ export {
38
+ __require,
39
+ __commonJS,
40
+ __export,
41
+ __toESM
42
+ };
43
+ //# sourceMappingURL=chunk-PR4QN5HX.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}