spdy-http2 / node-spdy

SPDY server on Node.js
2.81k stars 196 forks source link

spdy with ReactDOMServer.renderToPipeableStream stops after streaming 2048 bytes. #398

Open EmilsWebbod opened 1 year ago

EmilsWebbod commented 1 year ago

Got a really strange issue that boggles my brain. This is only connected to spdy. If i use http2.createSecureServer it works fine.

Seems like spdy stops streaming after 2048 bytes. image image

Even more strange. It works fine when i add a simple console.log after the stream.pipe(res); function. Added a simple code to test with.

Running Node v18.16.0. "react": "^18.2.0", "react-dom": "^18.2.0", "spdy": "^4.0.2"

const React = require('react');
const ReactDOMServer = require('react-dom/server');
const spdy = require('spdy');
const fs = require("fs");
const path = require("path");

const fail = true;
// Fails if buffersize is 229. Works if buffersize is 228....
const buffer = new Buffer(fail ? 229 : 228);

const server = spdy.createServer(
  {
    key: fs.readFileSync(path.join('keys', 'server.key')),
    cert: fs.readFileSync(path.join('keys', 'server.crt')),
    ca: fs.readFileSync(path.join('keys', 'ca.crt')),
    spdy: {
      protocols: ['h2'],
    },
    requestCert: true,
    rejectUnauthorized: false,
  },
  (req, res) => {
    res.setHeader('Content-type', 'text/html');
    const stream = ReactDOMServer.renderToPipeableStream(buffer, {
      onShellReady: () => {
        stream.pipe(res);
        // Adding console.log here fixes the problem with buffer size. Then it works fine with >228
        // console.log(); // This actually fixes the problem and ends the stream.
      },
      onShellError: (err) => {
        console.error(err);
        res.send('<h1>onShellError</h1>')
      },
      onError: (err) => {
        console.error(err);
        res.send('<h1>onError</h1>')
      },
      onAllReady: () => {
        res.end();
      }
    });
  }
);

server.listen(1335, () => {
  console.log(`Server listening on port 1335`);
});