0.1.17 #18
@@ -1,44 +1,64 @@
|
|||||||
import { applyWSSHandler } from "@trpc/server/adapters/ws"
|
import type { applyWSSHandler } from "@trpc/server/adapters/ws"
|
||||||
import { Context, Effect, Layer } from "effect"
|
import { Config, Context, Effect, Layer } from "effect"
|
||||||
import ws from "ws"
|
import type ws from "ws"
|
||||||
import { ExpressHTTPServer } from "../http/ExpressHTTPServer.service"
|
import { ImportError } from "../../ImportError"
|
||||||
import { ServerConfig } from "../ServerConfig"
|
import { ExpressNodeHTTPServer } from "../express"
|
||||||
import { TRPCContextCreator } from "../trpc/TRPCContextCreator.service"
|
import { TRPCUnknownContextCreator } from "./TRPCContextCreator"
|
||||||
import { RPCRouter } from "./RPCRouter.service"
|
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
|
wss: ws.Server
|
||||||
handler: ReturnType<typeof applyWSSHandler>
|
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({
|
const importWS = Effect.tryPromise({
|
||||||
server: yield* ExpressHTTPServer,
|
try: () => import("ws"),
|
||||||
host: rpcHTTPRoot,
|
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 {
|
return {
|
||||||
wss,
|
wss,
|
||||||
handler: applyWSSHandler({
|
handler: applyWSSHandler({
|
||||||
wss,
|
wss,
|
||||||
router: yield* RPCRouter,
|
router: yield* TRPCAnyRouter,
|
||||||
createContext: (yield* TRPCContextCreator).createWebSocketContext,
|
createContext: (yield* TRPCUnknownContextCreator).createWebSocketContext,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|
||||||
({ wss, handler }) => Effect.gen(function*() {
|
({ 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()
|
handler.broadcastReconnectNotification()
|
||||||
yield* Effect.async(resume => {
|
yield* Effect.async(resume => {
|
||||||
wss.close(() => resume(Effect.logInfo("WebSocket server closed")))
|
wss.close(() => resume(Effect.logInfo("WebSocket server closed")))
|
||||||
})
|
})
|
||||||
}),
|
}),
|
||||||
))
|
)
|
||||||
))
|
}))
|
||||||
|
|||||||
@@ -3,3 +3,4 @@ export * as TRPCContext from "./TRPCContext"
|
|||||||
export * as TRPCContextCreator from "./TRPCContextCreator"
|
export * as TRPCContextCreator from "./TRPCContextCreator"
|
||||||
export * as TRPCExpressRoute from "./TRPCExpressRoute"
|
export * as TRPCExpressRoute from "./TRPCExpressRoute"
|
||||||
export * as TRPCRouter from "./TRPCRouter"
|
export * as TRPCRouter from "./TRPCRouter"
|
||||||
|
export * as TRPCWebSocketServer from "./TRPCWebSocketServer"
|
||||||
|
|||||||
Reference in New Issue
Block a user