rahulramesha / http2-express-bridge

wrapper for express app to work with http2 protocol
MIT License
31 stars 6 forks source link

HTTP/2 server crashes when using `createServer` with an express app through `http2-express-bridge` #11

Open mctrafik opened 9 months ago

mctrafik commented 9 months ago

I am trying to write an insecure HTTP2 web server. However it crashes on any inputs.

I need an insecure HTTP2 server. Here's my code:

import { createServer } from 'node:http2';

import createExpressApp from 'express';
import http2ExpressBridge from 'http2-express-bridge';

// Test with: curl --insecure -v -k --http2-prior-knowledge "http://[::]:8080"
function main(): void {
  const app = http2ExpressBridge(createExpressApp);

  app.get('*', (request, response) => {
    response.setHeader('Content-Type', 'text/plain');
    response.status(200);
    response.write('Server reached');
    response.end();
  });

  const server = createServer(app);

  server.listen({ port: 8080 });

  // Triggered by CMD/CTRL + C .
  process.on('SIGINT', () => {
    server.close();
    process.exit(0);
  });
}

try {
  main();
} catch (error) {
  console.error(error);
  process.exit(1);
}

I'm testing using curl --insecure -v -k --http2-prior-knowledge "http://[::]:8080"

Every time I execute it, the server crashes with:

> tsx src/example/http2BugRepro.ts

node:events:492
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read properties of undefined (reading 'readable')
    at IncomingMessage._read (node:_http_incoming:214:19)
    at Readable.read (node:internal/streams/readable:547:12)
    at resume_ (node:internal/streams/readable:1048:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Emitted 'error' event on IncomingMessage instance at:
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at errorOrDestroy (node:internal/streams/destroy:214:7)
    at Readable.read (node:internal/streams/readable:549:7)
    at resume_ (node:internal/streams/readable:1048:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

When I add an SSL certificate and use createSecureServer everything works fine. But when I use createServer it crashes.

I think it's a bug in express or http2-express-bridge because without specifying the app, and handling the stream directly produces no errors.

My info:

OS: MacOS
Node: 20.9.0
Express: I tried v5.0.0-beta and v4.18.2
http2-express-bridge: 1.0.7

Thank you.