Skip to content

Commit eff33ec

Browse files
committed
feat: update WebSocket request handling to use containerFetch
- Enhanced the request processing logic to utilize the switchPort function for WebSocket connections, aligning with the @cloudflare/containers approach. - Simplified the request creation by directly using the original request and preserving headers, eliminating the need for manual URL construction.
1 parent da04b97 commit eff33ec

1 file changed

Lines changed: 5 additions & 11 deletions

File tree

worker/effect/services.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Effect, Layer } from 'effect';
2-
import { getContainer } from '@cloudflare/containers';
2+
import { getContainer, switchPort } from '@cloudflare/containers';
33
import {
44
getUserSessionContainerId,
55
} from '../auth';
@@ -875,16 +875,10 @@ const ContainerRuntimeLive = Layer.effect(
875875
headers.set('X-User', input.username);
876876
headers.set('X-Session-Id', input.sessionId);
877877
headers.set('X-Tab-Id', input.tabId);
878-
// Match Cloudflare's container WebSocket example: synthetic http://host (not the browser's https worker URL).
879-
// https://developers.cloudflare.com/containers/examples/websocket/
880-
const u = new URL(input.request.url);
881-
const internal = new URL(`${u.pathname}${u.search}`, 'http://localhost:8080');
882-
return ready.container.fetch(
883-
new Request(internal, {
884-
method: input.request.method,
885-
headers,
886-
})
887-
);
878+
// Match @cloudflare/containers: Container.fetch → containerFetch uses getTcpPort + request.url (https→http).
879+
// containers-demos/terminal uses containerFetch(request, defaultPort); WebSockets need that path, not a manual localhost URL.
880+
const proxied = new Request(input.request, { headers });
881+
return ready.container.fetch(switchPort(proxied, 8080));
888882
},
889883
catch: toContainerFailure('Container error, please retry in a moment.', true),
890884
})

0 commit comments

Comments
 (0)