Skip to content

Commit

Permalink
fix undefined params (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci authored Oct 15, 2023
1 parent 81419ad commit dcecaac
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/chopsticks/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { z } from 'zod'
import WebSocket, { AddressInfo, WebSocketServer } from 'ws'

import { ResponseError, SubscriptionManager } from './rpc/shared'
import { defaultLogger, truncate } from './logger'

const logger = defaultLogger.child({ name: 'ws' })

const requestSchema = z.object({
id: z.number(),
jsonrpc: z.literal('2.0'),
method: z.string(),
params: z.array(z.any()).default([]),
})

export type Handler = (
data: { method: string; params: string[] },
subscriptionManager: SubscriptionManager,
Expand Down Expand Up @@ -103,8 +111,8 @@ export const createServer = async (handler: Handler, port?: number) => {
})

ws.on('message', async (message) => {
const req = parseRequest(message.toString())
if (!req || req.id == null || req.method == null) {
const parsed = await requestSchema.safeParseAsync(parseRequest(message.toString()))
if (!parsed.success) {
logger.info('Invalid request: %s', message)
send({
id: null,
Expand All @@ -117,6 +125,7 @@ export const createServer = async (handler: Handler, port?: number) => {
return
}

const { data: req } = parsed
logger.trace(
{
id: req.id,
Expand Down

0 comments on commit dcecaac

Please sign in to comment.