Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http module emits 'end' event after 'error' event being called #2980

Closed
megothub opened this issue Feb 24, 2020 · 0 comments
Closed

http module emits 'end' event after 'error' event being called #2980

megothub opened this issue Feb 24, 2020 · 0 comments

Comments

@megothub
Copy link

megothub commented Feb 24, 2020

Update2: moved to the corresponding Issue tracker. Closing the ticket.

Hello

Update: Found another similar topic:
nodejs/node#2156

Couldn't find solution, but, found similar issue related to TCP sockets
nodejs/node#6083

  • OS: Ubuntu 18.04.3 LTS
  • node v12.16.1

What I need is to receive Github webhook request and handle it within 'End' event as 'Data' Event might not contain all data (as I understood the process). And, if there is a DoS attempt detected from a 3rd party just want to drop that connection to prevent node process to grow.

Code to reproduce:

const http = require('http');

console.log("Webhook Server: STARTING")

http.createServer((request, response) => {
  const { headers, method, url } = request;
  let body = [];

  request.on('error', (err) => {
        console.log("Webhook Server: Request Handler Error event: " + err);
  }).on('data', (chunk) => {
        console.log("Webhook Server: Data event");
        body.push(chunk);

        console.log("Webhook Server: Emitting Error");
        request.emit('error', "Error message");
  }).on('end', () => {
        console.log("Webhook Server: End event");
        response.on('error', (err) => {
             console.log("Webhook Server: Response Error")
             console.error(err);
        });

        response.writeHead(200, {'Content-Type': 'application/json'})
        const responseBody = { headers, method, url, body };

        response.end(JSON.stringify(responseBody))

    });
}).listen(3001, '127.0.0.1', function() {
    console.log("Webhook Server: LISTENING")
});

Getting:
Webhook Server: STARTING
Webhook Server: LISTENING
Webhook Server: Data event
Webhook Server: Emitting Error
Webhook Server: Request Handler Error event: Error message
Webhook Server: End event

@megothub megothub changed the title http module emit 'end' event after 'error' event being called http module emits 'end' event after 'error' event being called Apr 13, 2020
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

No branches or pull requests

1 participant