TRPCWebSocketServer
All checks were successful
Lint / lint (push) Successful in 12s

This commit is contained in:
Julien Valverdé
2024-09-06 06:33:24 +02:00
parent 1b80901f04
commit 442148b64d
2 changed files with 41 additions and 20 deletions

View File

@@ -1,44 +1,64 @@
import { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Context, Effect, Layer } from "effect"
import ws from "ws"
import { ExpressHTTPServer } from "../http/ExpressHTTPServer.service"
import { ServerConfig } from "../ServerConfig"
import { TRPCContextCreator } from "../trpc/TRPCContextCreator.service"
import { RPCRouter } from "./RPCRouter.service"
import type { applyWSSHandler } from "@trpc/server/adapters/ws"
import { Config, Context, Effect, Layer } from "effect"
import type ws from "ws"
import { ImportError } from "../../ImportError"
import { ExpressNodeHTTPServer } from "../express"
import { TRPCUnknownContextCreator } from "./TRPCContextCreator"
import { TRPCAnyRouter } from "./TRPCRouter"
export class TRPCWebSocketServer extends Context.Tag("TRPCWebSocketServer")<TRPCWebSocketServer, {
export class TRPCWebSocketServer extends Context.Tag("@thilalib/TRPC/TRPCWebSocketServer")<TRPCWebSocketServer, {
wss: ws.Server
handler: ReturnType<typeof applyWSSHandler>
}>() {}
export const RPCWebSocketServerLive = Layer.effect(RPCWebSocketServer, ServerConfig.rpcHTTPRoot.pipe(
Effect.flatMap(rpcHTTPRoot => Effect.acquireRelease(
Effect.gen(function*() {
yield* Effect.logInfo(`WebSocket server started on ${ rpcHTTPRoot }`)
const wss = new ws.WebSocketServer({
server: yield* ExpressHTTPServer,
host: rpcHTTPRoot,
const importWS = Effect.tryPromise({
try: () => import("ws"),
catch: cause => new ImportError({ path: "ws", cause }),
})
const importTRPCServerWSAdapter = Effect.tryPromise({
try: () => import("@trpc/server/adapters/ws"),
catch: cause => new ImportError({ path: "@trpc/server/adapters/ws", cause }),
})
export const TRPCWebSocketServerLive = (
config: {
readonly host: Config.Config<string>
}
) => Layer.effect(TRPCWebSocketServer, Effect.gen(function*() {
const { WebSocketServer } = yield* importWS
const { applyWSSHandler } = yield* importTRPCServerWSAdapter
const host = yield* config.host
return yield* Effect.acquireRelease(
Effect.gen(function*() {
yield* Effect.logInfo(`WebSocket server started on ${ host }`)
const wss = new WebSocketServer({
server: yield* ExpressNodeHTTPServer.ExpressNodeHTTPServer,
host,
})
return {
wss,
handler: applyWSSHandler({
wss,
router: yield* RPCRouter,
createContext: (yield* TRPCContextCreator).createWebSocketContext,
router: yield* TRPCAnyRouter,
createContext: (yield* TRPCUnknownContextCreator).createWebSocketContext,
}),
}
}),
({ wss, handler }) => Effect.gen(function*() {
yield* Effect.logInfo(`WebSocket server on ${ rpcHTTPRoot } is stopping. Waiting for existing connections to end...`)
yield* Effect.logInfo(`WebSocket server on ${ host } is stopping. Waiting for existing connections to end...`)
handler.broadcastReconnectNotification()
yield* Effect.async(resume => {
wss.close(() => resume(Effect.logInfo("WebSocket server closed")))
})
}),
))
))
)
}))

View File

@@ -3,3 +3,4 @@ export * as TRPCContext from "./TRPCContext"
export * as TRPCContextCreator from "./TRPCContextCreator"
export * as TRPCExpressRoute from "./TRPCExpressRoute"
export * as TRPCRouter from "./TRPCRouter"
export * as TRPCWebSocketServer from "./TRPCWebSocketServer"