Skip to content

fix: handle WebSocket RawData correctly for compression support#499

Merged
max246 merged 3 commits intobropat:developfrom
temp-droid:fix/websocket-rawdata-handling
Feb 1, 2026
Merged

fix: handle WebSocket RawData correctly for compression support#499
max246 merged 3 commits intobropat:developfrom
temp-droid:fix/websocket-rawdata-handling

Conversation

@temp-droid
Copy link
Copy Markdown
Contributor

@temp-droid temp-droid commented Jan 14, 2026

Summary

  • Fix WebSocket message handling to properly support clients using permessage-deflate compression
  • The ws library's message event provides RawData, not string

Problem

Clients using WebSocket compression (e.g., aiohttp with compress=9) experienced silent message drops. The connection established successfully, but messages were not processed.

Root Cause

The ws library types show the message event provides RawData, not string:

// node_modules/@types/ws/index.d.ts
on(event: "message", listener: (data: WebSocket.RawData, isBinary: boolean) => void): this;
type RawData = Buffer | ArrayBuffer | Buffer[];

The old code incorrectly typed the parameter as string.

Test plan

  • Build passes: npm run build
  • Manual test with compression-enabled client:
# Save as test-compression.mjs and run: node test-compression.mjs
import { WebSocketServer, WebSocket } from 'ws';
import { createServer } from 'http';

const httpServer = createServer();
const wsServer = new WebSocketServer({ server: httpServer });

wsServer.on('connection', (socket) => {
  console.log('[Server] Client connected');
  socket.on('message', (data) => {
    console.log('[Server] Data type:', data.constructor.name, '| Is Buffer:', Buffer.isBuffer(data));
    socket.send(JSON.stringify({ type: 'result', success: true }));
  });
});

httpServer.listen(3000, () => {
  console.log('[Server] Listening on port 3000');
  const client = new WebSocket('ws://127.0.0.1:3000', { perMessageDeflate: true });
  client.on('open', () => {
    console.log('[Client] Connected with compression');
    client.send(JSON.stringify({ command: 'test', messageId: '1' }));
  });
  client.on('message', (data) => {
    console.log('[Client] Response:', data.toString());
    client.close();
    httpServer.close();
  });
});

Expected output:

[Server] Listening on port 3000
[Server] Client connected
[Client] Connected with compression
[Server] Data type: Buffer | Is Buffer: true   <-- confirms compression sends Buffer
[Client] Response: {"type":"result","success":true}

The `ws` library's message event provides `RawData` (Buffer | ArrayBuffer | Buffer[]),
not `string`. This was causing issues with clients using permessage-deflate compression
(e.g., aiohttp with compress=9) where messages appeared to be silently dropped.

From @types/ws:
```typescript
on(event: "message", listener: (data: WebSocket.RawData, isBinary: boolean) => void): this;
type RawData = Buffer | ArrayBuffer | Buffer[];
```
@temp-droid temp-droid marked this pull request as ready for review January 14, 2026 08:30
@max246
Copy link
Copy Markdown
Collaborator

max246 commented Jan 20, 2026

@temp-droid thank you! go ahead and merge

@temp-droid
Copy link
Copy Markdown
Contributor Author

@max246 The ESLint check is failing due to a version mismatch in the workflow ([email protected] vs the project's [email protected]).
Could you merge this?

@max246
Copy link
Copy Markdown
Collaborator

max246 commented Jan 24, 2026

@max246 The ESLint check is failing due to a version mismatch in the workflow ([email protected] vs the project's [email protected]). Could you merge this?

yeah thats fine. just ... need some love on that task too.
Introduced eslint and prettier but might have misconfigured the actions on github

Bit busy to catch up on stuff and refactory the client, feel free to make a PR to fix it.

@max246 max246 merged commit 6f8c771 into bropat:develop Feb 1, 2026
3 of 4 checks passed
@max246 max246 mentioned this pull request Feb 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants