fastify/fastify-http-proxy

Websocket proxying changes message format from text to binary

bergkvist opened this issue · 1 comments

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.29.0

Plugin version

7.1.0 (@fastify/http-proxy)

Node.js version

16.10

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

5.10.96-1-MANJARO x86_64

Description

When proxying websocket messages, they change type from string to binary/Blob for me

Without proxying:
image

Routing through Fastify proxy:
image

This causes issues with create-react-app - since it tries to use JSON.parse(data), which only works if data is a String, but throws an error if it is a Blob. JSON.parse attempts to cooerce data into a String, resulting in "[object Blob]":

Uncaught SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at :3001/static/js/bundle.js:65554:24
    at WebSocket.client.onmessage (:3001/static/js/bundle.js:63696:9)

Steps to Reproduce

Step 1: Run create-react-app dev server on http://localhost:3002.

Step 2: Run fastify:

const app = require('fastify')({ logger: true })
const proxy = require('@fastify/http-proxy')
app.register(proxy, {
  upstream: 'http://localhost:3002',
  prefix: '/',
  websocket: true
})
app.listen({ port: 3001 })

Expected Behavior

Proxying websocket messages should not change message format to binary, but keep the format as text.

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.