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

adding failing test for request.body #23

Merged
merged 6 commits into from
Nov 28, 2018
Merged

Conversation

dylancarlone
Copy link
Contributor

@dylancarlone dylancarlone commented Apr 3, 2018

This test shows that the middleware will hang when request.body is created by consuming the request data stream.

closes #40

@dylancarlone dylancarlone mentioned this pull request Apr 3, 2018
@dylancarlone
Copy link
Contributor Author

I was actually able to get this to work just fine by wrapping the middleware and firing events after the middleware has subscribed:

function (request, response) {
  webhooks.middleware(request, response);

  if (request.body) {
    request.emit('data', Buffer.from(JSON.stringify(request.body)));
    request.emit('end');
  }
}

This would be an easy fix, whether or not it's implemented inside the middleware itself.

@gr2m
Copy link
Contributor

gr2m commented Apr 6, 2018

sounds good to me 👍 sorry for the long response times, I’m mostly out until April 22nd

@dylancarlone
Copy link
Contributor Author

dylancarlone commented Apr 16, 2018

I added the fix and the tests are now passing. If you don't think this is the way to do it or would prefer to hold off, I'm happy just refiring the events in my code and leaving the package be.

@gr2m gr2m self-assigned this Aug 8, 2018
@BerkeleyTrue
Copy link

Well, just opened an issue for this. (#40)

ping @dylancarlone

@dylancarlone
Copy link
Contributor Author

@BerkeleyTrue I had success duplicating the logic I added to this PR in my own app:

if (request.body) {

My problem initially came from FireBase parsing the request body before any of my own code ran.

@nbransby
Copy link
Contributor

@dylancarlone do you mean firebase functions? Im getting a timeout in firebase functions on calling verifyAndReceive, is this related?

@gr2m
Copy link
Contributor

gr2m commented Nov 26, 2018

I’ve rebased the PR, so this is ready to merge. I can’t think of any disadvantages of merging this. Does anyone have any objections or concerns?

@ci-reporter

This comment has been minimized.

@nbransby
Copy link
Contributor

Not from me.. I'm waiting for it!

if (request.body) {
request.emit('data', Buffer.from(JSON.stringify(request.body)))
request.emit('end')
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder, is there a need to emit the events, why not just call verifyAndReceive directly?

  if (request.body) {
    verifyAndReceive(state, {
      id: id,
      name: eventName,
      payload: request.body,
      signature
    })

      .then(() => {
        response.end('ok\n')
      })

      .catch(error => {
        response.statusCode = error.status || 500
        response.end(error.toString())
      })
  }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my change at b380f87 which omits re-emitting the "data" event on the request object. Test is still passing, so unless anyone objects I’ll go ahead with that :)

@gr2m
Copy link
Contributor

gr2m commented Nov 27, 2018

@dylancarlone @nbransby I’ve updated the implementation. Could you check if it works with Firebase functions now?

@nbransby
Copy link
Contributor

Works for me

@gr2m gr2m merged commit c0fade7 into octokit:master Nov 28, 2018
@nbransby
Copy link
Contributor

nbransby commented Dec 9, 2018

Sorry but it would appear its still hanging on master for me my firebase function is defined as:

export const webhook = https.onRequest((req, resp) => webhooks.verifyAndReceive({
    id: req.headers['x-github-delivery'],
    name: req.headers['x-github-event'],
    payload: req.body,
    signature: req.headers['x-hub-signature']
}));

results in

Function execution took 60002 ms, finished with status: 'timeout'

@gr2m
Copy link
Contributor

gr2m commented Dec 9, 2018

This seems unrelated to this pull request. You use the . verifyAndReceive method directly, while this PR was about the http middleware.

I don’t know firebase functions, but from these docs: https://firebase.google.com/docs/functions/http-events it looks like you still have to send a response, in your code example you don’t do anything with the resp parameter.

Maybe try this instead:

export const webhook = https.onRequest(webhooks.middleware);

@octokitbot
Copy link
Collaborator

🎉 This PR is included in version 5.0.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

webhook hangs when used in combination to bodyparser.json
5 participants