This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import type { AnyRouter } from "@trpc/server"
|
||||
import { Config, Context, Effect, Layer } from "effect"
|
||||
import { Config, Effect, Layer } from "effect"
|
||||
import { ExpressApp } from "../Express"
|
||||
import { ImportError } from "../ImportError"
|
||||
import { TRPCUnknownContextCreator } from "./TRPCContextCreator"
|
||||
import { TRPCAnyRouter } from "./TRPCRouter"
|
||||
|
||||
|
||||
const importTRPCServerExpressAdapter = Effect.tryPromise({
|
||||
@@ -10,22 +10,17 @@ const importTRPCServerExpressAdapter = Effect.tryPromise({
|
||||
catch: cause => new ImportError({ path: "@trpc/server/adapters/express", cause }),
|
||||
})
|
||||
|
||||
export const TRPCExpressRouteLive = <
|
||||
Tag,
|
||||
TagShape extends AnyRouter,
|
||||
>(
|
||||
routerTag: Context.TagClass<Tag, "@thilalib/TRCP/TRPCRouter", TagShape>,
|
||||
|
||||
export const TRPCExpressRouteLive = (
|
||||
config: {
|
||||
readonly root: Config.Config<string>
|
||||
},
|
||||
}
|
||||
) => Layer.effectDiscard(Effect.gen(function*() {
|
||||
const { createExpressMiddleware } = yield* importTRPCServerExpressAdapter
|
||||
const app = yield* ExpressApp.ExpressApp
|
||||
|
||||
app.use(yield* config.root,
|
||||
createExpressMiddleware({
|
||||
router: yield* routerTag,
|
||||
router: yield* TRPCAnyRouter,
|
||||
createContext: (yield* TRPCUnknownContextCreator).createExpressContext,
|
||||
})
|
||||
)
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
import type { AnyRouter } from "@trpc/server"
|
||||
import type { applyWSSHandler } from "@trpc/server/adapters/ws"
|
||||
import { Config, Context, Effect, Layer } from "effect"
|
||||
import type ws from "ws"
|
||||
import { ExpressNodeHTTPServer } from "../Express"
|
||||
import { ImportError } from "../ImportError"
|
||||
import { TRPCUnknownContextCreator } from "./TRPCContextCreator"
|
||||
import { TRPCAnyRouter } from "./TRPCRouter"
|
||||
|
||||
|
||||
export class TRPCWebSocketServer extends Context.Tag("@thilalib/TRPC/TRPCWebSocketServer")<TRPCWebSocketServer, TRPCWebSocketServerService>() {}
|
||||
|
||||
export interface TRPCWebSocketServerService {
|
||||
wss: ws.Server
|
||||
handler: ReturnType<typeof applyWSSHandler>
|
||||
}
|
||||
|
||||
|
||||
const importWS = Effect.tryPromise({
|
||||
@@ -17,57 +25,42 @@ const importTRPCServerWSAdapter = Effect.tryPromise({
|
||||
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
|
||||
|
||||
export const make = <
|
||||
Tag,
|
||||
TagShape extends AnyRouter,
|
||||
>(
|
||||
routerTag: Context.TagClass<Tag, "@thilalib/TRCP/TRPCRouter", TagShape>
|
||||
) => {
|
||||
class TRPCWebSocketServer extends Context.Tag("@thilalib/TRPC/TRPCWebSocketServer")<TRPCWebSocketServer, {
|
||||
wss: ws.Server
|
||||
handler: ReturnType<typeof applyWSSHandler<TagShape>>
|
||||
}>() {}
|
||||
const host = yield* config.host
|
||||
|
||||
const TRPCWebSocketServerLive = (
|
||||
config: {
|
||||
readonly host: Config.Config<string>
|
||||
}
|
||||
) => Layer.effect(TRPCWebSocketServer, Effect.gen(function*() {
|
||||
const { WebSocketServer } = yield* importWS
|
||||
const { applyWSSHandler } = yield* importTRPCServerWSAdapter
|
||||
return yield* Effect.acquireRelease(
|
||||
Effect.gen(function*() {
|
||||
yield* Effect.logInfo(`WebSocket server started on ${ host }`)
|
||||
|
||||
const host = yield* config.host
|
||||
const wss = new WebSocketServer({
|
||||
server: yield* ExpressNodeHTTPServer.ExpressNodeHTTPServer,
|
||||
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,
|
||||
handler: applyWSSHandler({
|
||||
wss,
|
||||
handler: applyWSSHandler({
|
||||
wss,
|
||||
router: yield* routerTag,
|
||||
createContext: (yield* TRPCUnknownContextCreator).createWebSocketContext,
|
||||
}),
|
||||
}
|
||||
}),
|
||||
router: yield* TRPCAnyRouter,
|
||||
createContext: (yield* TRPCUnknownContextCreator).createWebSocketContext,
|
||||
}),
|
||||
}
|
||||
}),
|
||||
|
||||
({ wss, handler }) => Effect.gen(function*() {
|
||||
yield* Effect.logInfo(`WebSocket server on ${ host } is stopping. Waiting for existing connections to end...`)
|
||||
({ wss, handler }) => Effect.gen(function*() {
|
||||
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")))
|
||||
})
|
||||
}),
|
||||
)
|
||||
}))
|
||||
|
||||
return { TRPCWebSocketServer, TRPCWebSocketServerLive }
|
||||
}
|
||||
handler.broadcastReconnectNotification()
|
||||
yield* Effect.async(resume => {
|
||||
wss.close(() => resume(Effect.logInfo("WebSocket server closed")))
|
||||
})
|
||||
}),
|
||||
)
|
||||
}))
|
||||
|
||||
@@ -24,14 +24,11 @@ const router = TRPCBuilder.pipe(Effect.map(t => t.router({
|
||||
const { TRPCRouter, TRPCRouterLive } = TRPC.TRPCRouter.make(router)
|
||||
|
||||
|
||||
const { TRPCWebSocketServer, TRPCWebSocketServerLive } = TRPC.TRPCWebSocketServer.make(TRPCRouter)
|
||||
|
||||
|
||||
const ServerLive = Layer.empty.pipe(
|
||||
Layer.provideMerge(TRPC.TRPCExpressRoute.TRPCExpressRouteLive(TRPCRouter, {
|
||||
Layer.provideMerge(TRPC.TRPCExpressRoute.TRPCExpressRouteLive({
|
||||
root: Config.succeed("/rpc")
|
||||
})),
|
||||
Layer.provideMerge(TRPCWebSocketServerLive({
|
||||
Layer.provideMerge(TRPC.TRPCWebSocketServer.TRPCWebSocketServerLive({
|
||||
host: Config.succeed("/rpc")
|
||||
})),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user