Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

refactor: migrate to hapi 18 #36

Merged
merged 3 commits into from
Jan 31, 2019
Merged

refactor: migrate to hapi 18 #36

merged 3 commits into from
Jan 31, 2019

Conversation

alanshaw
Copy link

This PR refactors the HTTP API for use with Hapi 18 in preparation for ipfs/js-ipfs#1844.

The current code works with Hapi 16. This is now two major versions behind the current version and we need to update our code to keep up to date with the latest bug and security fixes and performance improvements.

Hapi 17 introduced significant breaking changes, along with a switch from a callback API to a promises API hence the code changes to support this are also significant.

Note the following additional changes:

  1. There's no need to explicitly reply with an error response - just throw the error and the error handler will construct the appropriate response - so the instances where this was happening have been removed.
  2. Switched to using async/await with try/catch over .then/.catch for easier to understand and more succinct code - Hapi 17+ uses this syntax and it's supported in Node.js since version 8.

Alan Shaw added 3 commits January 28, 2019 12:40
This PR refactors the HTTP API for use with Hapi 18 in preparation for ipfs/js-ipfs#1844.

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
License: MIT
Signed-off-by: Alan Shaw <[email protected]>
Copy link
Collaborator

@achingbrain achingbrain left a comment

Choose a reason for hiding this comment

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

LGTM.

}
})
readableStream.once('error', (error) => {
reject(error)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What would happen here if an error occurred during writing (so we'd already resolved the promise)?

Copy link
Author

@alanshaw alanshaw Jan 31, 2019

Choose a reason for hiding this comment

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

A promise can only be resolved/rejected once, and first one wins, so nothing really. Hapi would have already started streaming to the client. At that point we've handed over responsibility of responding (by passing the stream to hapi) and my expectation would be for either Hapi or Node.js to handle an error in the stream and close the connection...

Previously we'd have called reply again, which definitely wouldn't have worked as the HTTP headers would have already been sent and possibly some data as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably at least log the error then as presumably it would get swallowed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Happy to do this later though.

@achingbrain achingbrain merged commit 68a764c into master Jan 31, 2019
@ghost ghost removed the in progress label Jan 31, 2019
@achingbrain achingbrain deleted the refactor/hapi-18 branch January 31, 2019 16:01
@achingbrain
Copy link
Collaborator

Released as v0.9.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants