Skip to content
Source
Sign inSign up
Open History to see the last commit

upgrade.ts

28 lines1.1 KB

1import { upgradeWebSocket } from "@neon/functions";
2import type { RuntimeContext } from "alchemy/RuntimeContext";
3import * as Effect from "effect/Effect";
4import * as HttpServerResponse from "effect/http/HttpServerResponse";
5import { FunctionRequest } from "./FunctionEnvironment.ts";
6import { FunctionUpgradeSockets } from "./FunctionUpgrade.ts";
7​
8/**
9 * Upgrade the current request using Neon's native WebSocket implementation.
10 * Return `response` unchanged; rebuilding it discards the native upgrade metadata.
11 * Authenticate the caller before accepting a public Function connection.
12 */
13export const upgrade = (options?: {
14 protocol?: string;
15}): Effect.Effect<
16 {
17 socket: WebSocket;
18 response: HttpServerResponse.HttpServerResponse;
19 },
20 never,
21 RuntimeContext | FunctionRequest
22> =>
23 Effect.gen(function* () {
24 const request = yield* FunctionRequest;
25 const { socket, response } = yield* Effect.sync(() => upgradeWebSocket(request, options));
26 yield* Effect.sync(() => FunctionUpgradeSockets.set(response, socket));
27 return { socket, response: HttpServerResponse.raw(response) };
28 });