Skip to content

Commit

Permalink
fix circular deps
Browse files Browse the repository at this point in the history
  • Loading branch information
tudddorrr committed Dec 7, 2024
1 parent e594fb5 commit 3ed7267
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@ export default async function init(): Promise<Koa> {
app.use(bodyParser())
app.use(helmet())
app.use(corsMiddleware)
app.use(requestContextMiddleware)
app.use(devDataMiddleware)
app.use(requestContextMiddleware)

configureProtectedRoutes(app)
configurePublicRoutes(app)
configureAPIRoutes(app)

app.use(cleanupMiddleware)

const server = createServer(app.callback())
app.context.wss = new Socket(server, app.context.em)

if (!isTest) {
const server = createServer(app.callback())
app.context.wss = new Socket(server, app.context.em)
server.listen(80, () => console.info('Listening on port 80'))
}

Expand Down
4 changes: 2 additions & 2 deletions src/socket/listeners/gameChannelListeners.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { z, ZodType } from 'zod'
import { SocketMessageListener } from '../router/socketRoutes'
import { createListener } from '../router/socketRouter'
import { SocketMessageListener } from '../router/createListener'
import createListener from '../router/createListener'
import { RequestContext } from '@mikro-orm/core'
import GameChannel from '../../entities/game-channel'
import { sendMessages } from '../messages/socketMessage'
Expand Down
4 changes: 2 additions & 2 deletions src/socket/listeners/playerListeners.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { z, ZodType } from 'zod'
import { createListener } from '../router/socketRouter'
import createListener from '../router/createListener'
import { sendMessage } from '../messages/socketMessage'
import Redis from 'ioredis'
import redisConfig from '../../config/redis.config'
import { RequestContext } from '@mikro-orm/core'
import PlayerAlias from '../../entities/player-alias'
import { SocketMessageListener } from '../router/socketRoutes'
import { SocketMessageListener } from '../router/createListener'
import SocketError, { sendError } from '../messages/socketError'

const playerListeners: SocketMessageListener<ZodType>[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { z, ZodType } from 'zod'
import { SocketMessageRequest } from '../messages/socketMessage'
import SocketConnection from '../socketConnection'
import Socket from '..'
import playerListeners from '../listeners/playerListeners'
import gameChannelListeners from '../listeners/gameChannelListeners'

type SocketMessageListenerHandlerParams<T> = {
conn: SocketConnection
Expand All @@ -12,8 +10,8 @@ type SocketMessageListenerHandlerParams<T> = {
socket: Socket
}

export type SocketMessageListenerHandler<T> = (params: SocketMessageListenerHandlerParams<T>) => void | Promise<void>
export type SocketMessageListenerOptions = {
type SocketMessageListenerHandler<T> = (params: SocketMessageListenerHandlerParams<T>) => void | Promise<void>
type SocketMessageListenerOptions = {
requirePlayer?: boolean
apiKeyScopes?: string[]
}
Expand All @@ -25,9 +23,16 @@ export type SocketMessageListener<T extends ZodType> = {
options: SocketMessageListenerOptions
}

const routes: SocketMessageListener<ZodType>[][] = [
playerListeners,
gameChannelListeners
]

export default routes
export default function createListener<T extends ZodType>(
req: SocketMessageRequest,
validator: T,
handler: SocketMessageListenerHandler<z.infer<T>>,
options?: SocketMessageListenerOptions
): SocketMessageListener<T> {
return {
req,
validator,
handler,
options
}
}
25 changes: 9 additions & 16 deletions src/socket/router/socketRouter.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import { z, ZodError, ZodType } from 'zod'
import Socket from '..'
import { SocketMessageRequest, requests } from '../messages/socketMessage'
import { requests } from '../messages/socketMessage'
import SocketConnection from '../socketConnection'
import { RawData } from 'ws'
import { addBreadcrumb } from '@sentry/node'
import routes, { SocketMessageListener, SocketMessageListenerHandler, SocketMessageListenerOptions } from './socketRoutes'
import { SocketMessageListener } from './createListener'
import SocketError, { sendError } from '../messages/socketError'
import { APIKeyScope } from '../../entities/api-key'

export function createListener<T extends ZodType>(
req: SocketMessageRequest,
validator: T,
handler: SocketMessageListenerHandler<z.infer<T>>,
options?: SocketMessageListenerOptions
): SocketMessageListener<T> {
return {
req,
validator,
handler,
options
}
}
import playerListeners from '../listeners/playerListeners'
import gameChannelListeners from '../listeners/gameChannelListeners'

const socketMessageValidator = z.object({
req: z.enum(requests),
Expand All @@ -29,6 +17,11 @@ const socketMessageValidator = z.object({

type SocketMessage = z.infer<typeof socketMessageValidator>

const routes: SocketMessageListener<ZodType>[][] = [
playerListeners,
gameChannelListeners
]

export default class SocketRouter {
constructor(readonly socket: Socket) {}

Expand Down

0 comments on commit 3ed7267

Please sign in to comment.