diff --git a/src/adapter/bun/index.ts b/src/adapter/bun/index.ts index f5157d08d..1fe7d3beb 100644 --- a/src/adapter/bun/index.ts +++ b/src/adapter/bun/index.ts @@ -506,7 +506,7 @@ export const BunAdapter: ElysiaAdapter = { ) as any const handleResponse = createHandleWSResponse(responseValidator) - const parseMessage = createWSMessageParser(parse as any) + const parseMessage = createWSMessageParser(parse as any, !!messageValidator) let _id: string | undefined diff --git a/src/ws/index.ts b/src/ws/index.ts index 7c7aaaa5b..5f264cf0b 100644 --- a/src/ws/index.ts +++ b/src/ws/index.ts @@ -209,12 +209,13 @@ export class ElysiaWS } export const createWSMessageParser = ( - parse: MaybeArray> + parse: MaybeArray>, + hasSchema = false ) => { const parsers = typeof parse === 'function' ? [parse] : parse return async function parseMessage(ws: ServerWebSocket, message: any) { - if (typeof message === 'string') { + if (hasSchema && typeof message === 'string') { const start = message?.charCodeAt(0) if (start === 34 || start === 47 || start === 91 || start === 123) diff --git a/test/ws/message.test.ts b/test/ws/message.test.ts index 014901b96..461f55c27 100644 --- a/test/ws/message.test.ts +++ b/test/ws/message.test.ts @@ -249,6 +249,7 @@ describe('WebSocket message', () => { it('should parse objects', async () => { const app = new Elysia() .ws('/ws', { + body: t.Any(), message(ws, raw) { ws.send(raw) } @@ -274,6 +275,7 @@ describe('WebSocket message', () => { it('should parse arrays', async () => { const app = new Elysia() .ws('/ws', { + body: t.Any(), message(ws, raw) { ws.send(JSON.stringify(raw)) } @@ -299,6 +301,7 @@ describe('WebSocket message', () => { it('should parse strings', async () => { const app = new Elysia() .ws('/ws', { + body: t.Any(), message(ws, raw) { ws.send(JSON.stringify(raw)) } @@ -324,6 +327,7 @@ describe('WebSocket message', () => { it('should parse numbers', async () => { const app = new Elysia() .ws('/ws', { + body: t.Any(), message(ws, raw) { ws.send(JSON.stringify(raw)) } @@ -349,6 +353,7 @@ describe('WebSocket message', () => { it('should parse true', async () => { const app = new Elysia() .ws('/ws', { + body: t.Any(), message(ws, raw) { ws.send(JSON.stringify(raw)) } @@ -374,6 +379,7 @@ describe('WebSocket message', () => { it('should parse false', async () => { const app = new Elysia() .ws('/ws', { + body: t.Any(), message(ws, raw) { ws.send(JSON.stringify(raw)) } @@ -399,6 +405,7 @@ describe('WebSocket message', () => { it('should parse null', async () => { const app = new Elysia() .ws('/ws', { + body: t.Any(), message(ws, raw) { ws.send(JSON.stringify(raw)) } @@ -424,6 +431,7 @@ describe('WebSocket message', () => { it('should parse not parse /hello', async () => { const app = new Elysia() .ws('/ws', { + body: t.Any(), message(ws, raw) { ws.send(JSON.stringify(raw)) } @@ -446,6 +454,33 @@ describe('WebSocket message', () => { app.stop() }) + it('should not parse json-like text frames without a body schema', async () => { + const app = new Elysia() + .ws('/ws', { + message(ws, raw) { + ws.send(raw as string) + } + }) + .listen(0) + + const ws = newWebsocket(app.server!) + + await wsOpen(ws) + + const m1 = wsMessage(ws) + ws.send('["ping"]') + const { data: d1 } = await m1 + expect(d1).toBe('["ping"]') + + const m2 = wsMessage(ws) + ws.send('{"key":"val"}') + const { data: d2 } = await m2 + expect(d2).toBe('{"key":"val"}') + + await wsClosed(ws) + app.stop() + }) + it('should send from plugin', async () => { const plugin = new Elysia().ws('/ws', { message(ws, message) {