Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/adapter/bun/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions src/ws/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,13 @@ export class ElysiaWS<Context = unknown, Route extends RouteSchema = {}>
}

export const createWSMessageParser = (
parse: MaybeArray<WSParseHandler<any>>
parse: MaybeArray<WSParseHandler<any>>,
hasSchema = false
) => {
const parsers = typeof parse === 'function' ? [parse] : parse

return async function parseMessage(ws: ServerWebSocket<any>, 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)
Expand Down
35 changes: 35 additions & 0 deletions test/ws/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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))
}
Expand All @@ -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) {
Expand Down