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

Allows middlewares to preserve & modify input stream #351

Merged
merged 2 commits into from
Oct 5, 2016

Conversation

botic
Copy link
Member

@botic botic commented Aug 20, 2016

This would enable JSGI middlewares to read the input and reset the input stream to the initial state after. This is needed to add support for X-Hub-Signature like available for the Github and Facebook API. Otherwise someone needs to clone the whole params middleware to avoid that it consumes the input stream before the verification middleware.

@mention-bot
Copy link

@botic, thanks for your PR! By analyzing the annotation information on this pull request, we identified @grob and @oberhamsi to be potential reviewers

@botic
Copy link
Member Author

botic commented Aug 22, 2016

I looked for other solutions for this problem. One would be to directly read into a MemoryStream at the beginning of the JSGI connector and closing the original java.io.InputStream after it. Since a MemoryStream has a position which can be reset to 0, consumers of the body can read the stream and then leave it in the original state.

exports.middleware = function consumingMiddleware(next, app) {
  return function (req) {
    const input = req.input.read();
    req.input.position = 0;

    if (input ...) {
       // process input
    }

    return next(req);
  };
};

Changing it to a MemoryStream instead of a simple Stream might introduces consequences which are not on our radar yet. The simpler solution is like proposed in this PR, which just adds an additional setter method for the input, which will never be used in existing applications.

Since an application can be composed and it never knows which middleware chain might be present, I think we should solve this problem for the upcoming release.

@botic botic added this to the 1.0 milestone Oct 3, 2016
@botic botic merged commit 0e8c281 into master Oct 5, 2016
@botic botic deleted the jsgi-connector-setinput branch August 7, 2020 11:11
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.

2 participants