@standardagents/builder 0.11.0-next.5d2e71b → 0.11.0-next.730b472

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/plugin.js CHANGED
@@ -2246,6 +2246,32 @@ function corsHeaders(contentType) {
2246
2246
  };
2247
2247
  }
2248
2248
 
2249
+ // Helper to add CORS headers to any Response without touching the body
2250
+ function addCorsHeaders(response) {
2251
+ // Skip WebSocket upgrade responses - they can't be wrapped
2252
+ if (response.status === 101) {
2253
+ return response;
2254
+ }
2255
+
2256
+ // Skip if already has CORS headers
2257
+ if (response.headers.has("Access-Control-Allow-Origin")) {
2258
+ return response;
2259
+ }
2260
+
2261
+ // Create new headers with CORS added
2262
+ const newHeaders = new Headers(response.headers);
2263
+ for (const [key, value] of Object.entries(CORS_HEADERS)) {
2264
+ newHeaders.set(key, value);
2265
+ }
2266
+
2267
+ // Return new Response with same body stream (not cloned, just transferred)
2268
+ return new Response(response.body, {
2269
+ status: response.status,
2270
+ statusText: response.statusText,
2271
+ headers: newHeaders,
2272
+ });
2273
+ }
2274
+
2249
2275
  export async function router(request, env) {
2250
2276
  const url = new URL(request.url);
2251
2277
  const pathname = url.pathname;
@@ -2297,7 +2323,7 @@ ${threadRouteCode}
2297
2323
 
2298
2324
  // If requireAuth returns a Response, it's an error (401)
2299
2325
  if (authResult instanceof Response) {
2300
- return authResult;
2326
+ return addCorsHeaders(authResult);
2301
2327
  }
2302
2328
 
2303
2329
  authContext = authResult;
@@ -2316,11 +2342,7 @@ ${threadRouteCode}
2316
2342
  const result = await controller(context);
2317
2343
 
2318
2344
  if (result instanceof Response) {
2319
- // Return Response objects as-is - CORS headers can't be safely added
2320
- // to Response objects in the workerd environment without issues.
2321
- // Controllers that return streaming/binary responses should add
2322
- // CORS headers themselves if needed.
2323
- return result;
2345
+ return addCorsHeaders(result);
2324
2346
  }
2325
2347
  if (typeof result === "string") {
2326
2348
  return new Response(result, {