mock-socket icon indicating copy to clipboard operation
mock-socket copied to clipboard

Fix: pass Buffer data type as ArrayBuffer instead of string

Open yannbriancon opened this issue 3 years ago • 3 comments

From what I experienced with web sockets, Buffer messages are transmitted as ArrayBuffer in the message event.

This modification may break some people implementations but should be closer to real behaviour.

Happy to have your feedback.

yannbriancon avatar Aug 23 '22 07:08 yannbriancon

Buffer is a Node module, but this library is supposed to mock the client web socket and is not supposed to create a copy of the Node WebSocket implementation (Anyway the server can be written in any language). The normalizeSendData function is called both on the client and server, but the only thing it should do right is to mimic the client's behavior. So if you want to pass the data to the WebSocket client in the test I think you can just pass the string or blob/arraybuffer data as the client expects (not the Buffer instance), as you test the client, not the server.

Atrue avatar Aug 29 '22 09:08 Atrue

Meet the same problem, we use some external npm package which will generate Buffer and we transfer them by the websocket.

bytemain avatar Dec 18 '23 14:12 bytemain

@bytemain Can you share an example of the issue? According to the spec the client WebSocket can accept/send only these types: string, blob and ArrayBuffer, (see https://websockets.spec.whatwg.org/#feedback-from-the-protocol p3 and p4). All these types should be handled properly in the mock-socket

Atrue avatar Dec 20 '23 07:12 Atrue