[WIP] refactor(hapi): Upgrade to hapi 17#334
Conversation
vladikoff
left a comment
There was a problem hiding this comment.
I'll check 'static' in a bit
| @@ -91,7 +91,7 @@ module.exports = function profileCache(server, options, next) { | |||
| scope: scope | |||
| }}}; | |||
| return P.fromCallback(cb => { | |||
There was a problem hiding this comment.
there is a cb here, should that be removed and function below needs to 'return'?
There was a problem hiding this comment.
we should check if cache.drop still uses callbacks or not
| handler: function avatar(req, reply) { | ||
| handler: async function avatar(req) { | ||
| var uid = req.auth.credentials.user; | ||
| db.getSelectedAvatar(uid) |
There was a problem hiding this comment.
does this need a 'return'?
| @@ -63,8 +63,7 @@ module.exports = { | |||
| .then(() => { | |||
| notifyProfileUpdated(uid); // Don't wait on promise | |||
| return EMPTY; | |||
There was a problem hiding this comment.
need to check 'EMPTY' vs the {} above
| handler: function upload(req, reply) { | ||
| handler: async function upload(req, h) { | ||
| const uid = req.auth.credentials.user; | ||
| req.server.methods.profileCache.drop(uid, () => { |
There was a problem hiding this comment.
does this need to return?, also no more callbacks?
| @@ -26,20 +26,20 @@ module.exports = { | |||
| credentials: req.auth.credentials | |||
| }, res => { | |||
| cors: true, | ||
| security: { | ||
| hsts: { | ||
| maxAge: 15552000, |
There was a problem hiding this comment.
I think we had a different fix for this to make sure these headers are set ?
| handler: function heartbeat(req, reply) { | ||
| db.ping().done(reply, reply); | ||
| handler: async function heartbeat() { | ||
| db.ping().then(() => {}); |
| server.route(routes); | ||
|
|
||
| server.ext('onPreAuth', function (request, reply) { | ||
| server.ext('onPreAuth', function (request, h) { |
There was a problem hiding this comment.
are these stilp callbacks? need to check..
| config: { | ||
| handler: function upload(req, reply) { | ||
| reply({}); | ||
| handler: async function upload(req) { |
There was a problem hiding this comment.
function name 'upload' is wrong here, lets fix
| const uid = req.auth.credentials.user; | ||
| req.server.methods.profileCache.drop(uid, () => { | ||
| const payload = req.payload; | ||
| db.setDisplayName(uid, payload.displayName) |
| async function start(){ | ||
| const server = await require('../lib/server/worker').create(); | ||
|
|
||
| server.start(function() { |
There was a problem hiding this comment.
I don't think there is a callback anymore on .start, You need to do await server.start()
and surround it with the a try and catch to throw an error if it fails to .start
| server.start(function() { | ||
| logger.info('listening', server.info.uri); | ||
| }); | ||
| async function start(){ |
There was a problem hiding this comment.
style: start() { (space before {
| cache: { | ||
| expiresIn: options.expiresIn, | ||
| generateTimeout: options.generateTimeout | ||
| generateTimeout: options.generateTimeout || 100 |
There was a problem hiding this comment.
I think we change that in the tests instead and should not do this here
There was a problem hiding this comment.
it was throwing error here generateTimeout is required so i added it temporarily
| method: 'POST', | ||
| path: v('/display_name'), | ||
| config: require('./routes/display_name/post') | ||
| options: require('./routes/display_name/post') |
| host: config.server.host, | ||
| port: config.server.port + 1, | ||
| debug: { request: ['error'] } | ||
| debug: false |
There was a problem hiding this comment.
hm not sure about this change, I wonder what { request: ['error'] } used to do
There was a problem hiding this comment.
it was originally false only
| port: config.server.port, | ||
| cache: cache, | ||
| debug: false, | ||
| debug: { request: ['error'] }, |
| // server method for caching profile | ||
| try { | ||
| await server.register({ | ||
| name: 'fxa-profile-server', |
There was a problem hiding this comment.
maybe we can call this just profileCache
| }); | ||
|
|
||
| server.ext('onPreResponse', function(request, next) { | ||
| server.ext('onPreResponse', function(request, h) { |
There was a problem hiding this comment.
we have h.continue below
|
@vladikoff still i am missing something :( |
|
|
||
| async function create() { | ||
| const server = await require('../lib/server/_static').create(); | ||
| server.start().then(() => { |
There was a problem hiding this comment.
should use await server.start(); here , move the log out of then. Just like below: https://github.com/mozilla/fxa-profile-server/pull/334/files#diff-8d44631bfd02d64ee75348ab92f466c0R27
| return workers.delete(avatar.id); | ||
| } | ||
| }) | ||
| req.server.methods.profileCache.drop(uid) |
There was a problem hiding this comment.
looks like this will fail because this doesn't return
| // precaution to avoid the default id from being overwritten | ||
| assert(id !== DEFAULT_AVATAR_ID); | ||
| const url = avatarShared.fxaUrl(id); | ||
| workers.upload(id, req.payload, req.headers) |
There was a problem hiding this comment.
this also needs to return otherwise response will be lost
| }) | ||
| .done(reply, reply); | ||
| }); | ||
| req.server.methods.profileCache.drop(uid) |
There was a problem hiding this comment.
this will fail, no return
| req.server.methods.profileCache.drop(uid) | ||
| .then(() => { | ||
| const payload = req.payload; | ||
| db.setDisplayName(uid, payload.displayName) |
| switch (request.params.type) { | ||
| case '': | ||
| reply.file(DEFAULT_AVATAR); | ||
| h.file(DEFAULT_AVATAR); |
There was a problem hiding this comment.
should we make h.file a return h.file ?
| return next(null, result, ttl); | ||
| }) | ||
| .catch(next); | ||
| .then(result => { |
There was a problem hiding this comment.
@vladikoff , I have fixed the server stop function but it still didn't work. This method seems to be failing.
| server.methods.profileCache.get.cache.drop(req, cb); | ||
| }); | ||
| }).asCallback(next); | ||
| await server.methods.profileCache.get.cache.drop(req); |
There was a problem hiding this comment.
@vladikoff , this method is not right yet, how do we change it correctly ?
| @@ -90,7 +90,10 @@ module.exports = function profileCache(server, options) { | |||
| scope: scope | |||
| }}}; | |||
| return await server.methods.profileCache.get.cache.drop(req); | |||
There was a problem hiding this comment.
@vladikoff this line is throwing a 500 error, am I missing something ?
There was a problem hiding this comment.
@deeptibaghel do you get this when running the tests?
| semi: [2, "always"] | ||
|
|
||
| parserOptions: | ||
| ecmaVersion: 2017 |
There was a problem hiding this comment.
let's make this 2018 here
| }, | ||
| serverCache: { | ||
| redis: { | ||
| // name: 'redisCache', |
There was a problem hiding this comment.
what happens here? is this not named anymore? do we delete it?
| const url = avatarShared.fxaUrl(id); | ||
| await workers.upload(id, req.payload, req.headers); | ||
| await db.addAvatar(id, uid, url, FXA_PROVIDER); | ||
| await notifyProfileUpdated(uid); // Don't wait on promise |
There was a problem hiding this comment.
per // Don't wait on promise , we don't want an await here
| }) | ||
| .done(reply, reply); | ||
| }); | ||
| return req.server.methods.profileCache.drop(uid) |
There was a problem hiding this comment.
if we are changing the upload.js above to use async await, then we should do it here as well. or we can use the .then structure above. Pick the one you like.
| // we should always get an email field back. | ||
| if (! res.result.email) { | ||
| logger.error('request.auth_server.fail', res.result); | ||
| return new AppError({ |
| handler: function index(req, reply) { | ||
| function sendReply() { | ||
| reply({ | ||
| // response: { |
| @@ -90,7 +90,10 @@ module.exports = function profileCache(server, options) { | |||
| scope: scope | |||
| }}}; | |||
| return await server.methods.profileCache.get.cache.drop(req); | |||
There was a problem hiding this comment.
@deeptibaghel do you get this when running the tests?
| scope: scope | ||
| }}}; | ||
| return await server.methods.profileCache.get.cache.drop(req); | ||
| return server.methods.profileCache.get.cache.drop(req); |
There was a problem hiding this comment.
@vladikoff the test to upload avatar fails at this point
|
I'm leaving this PR open because we still want this work! We will need to migrate this to the new mono-repo: github.com/mozilla/fxa |
Fixes #333