-
Notifications
You must be signed in to change notification settings - Fork 1
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
adapt plugin to hapi v17 #16
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
81bcf99
adapt plugin to hapi v17
maoeye18 392d0d1
move hapi 17 changes to extra file
maoeye18 038601f
repeat plugin descriptions to files at root
maoeye18 e8d632d
fixes after review
maoeye18 59438f5
move HapiPlugin, clean tests
maoeye18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"use strict"; | ||
|
||
var _index = require("./dist/index.js"); | ||
|
||
module.exports = _index.express; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"use strict"; | ||
|
||
var _index = require("./dist/index.js"); | ||
|
||
module.exports = _index.hapi17; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"use strict"; | ||
|
||
var _index = require("./dist/index.js"); | ||
|
||
module.exports.register = _index.register; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Syslog from '../adapter/syslog'; | ||
import Console from '../adapter/console'; | ||
import HapiPlugin from './hapi-plugin.js'; | ||
|
||
/** | ||
* | ||
* @param {Object} Logger | ||
* @param {Server} server | ||
* @param {Object} options | ||
*/ | ||
export function setupLogger(Logger, server, options) { | ||
const hapiPlugin = new HapiPlugin(Logger); | ||
|
||
// always add console as adapter | ||
Logger.setParameters(options); | ||
Logger.addAdapter(Console, options.console || {}); | ||
|
||
// if syslog object is provided, add syslog adapter | ||
if (options.syslog) { | ||
Logger.addAdapter(Syslog, options.syslog); | ||
} | ||
|
||
// | ||
// expose logger everywhere in hapi | ||
// | ||
server.app.logger = Logger; | ||
server.decorate('server', 'logger', Logger); | ||
|
||
// | ||
// logging requested via `server.log` | ||
// possible calls: | ||
// - `server.log('error', data);` | ||
// - `server.log(['error', 'database'], data);` | ||
// | ||
server.events.on('log', (...args) => { | ||
hapiPlugin.onServerLog(server, ...args); | ||
}); | ||
|
||
// | ||
// logging requested via `request.log` | ||
// possible calls: | ||
// - `request.log('error', data);` | ||
// - `request.log(['error', 'database'], data);` | ||
// | ||
server.events.on('request', (...args) => { | ||
hapiPlugin.onRequestLog(...args); | ||
}); | ||
|
||
// | ||
// error during route handler processing | ||
// | ||
server.events.on({ name: 'request', channels: 'error' }, (...args) => { | ||
hapiPlugin.onRequestError(...args); | ||
}); | ||
|
||
// | ||
// internal hapi event related to request | ||
// currently we only interested in received sub-event | ||
// to render request before any processing will start | ||
// | ||
server.events.on({ name: 'request', channels: 'internal' }, (...args) => { | ||
hapiPlugin.onRequestInternal(...args); | ||
}); | ||
|
||
// | ||
// just handler to notify that server is started | ||
// | ||
server.ext({ | ||
type: 'onPostStart', | ||
method: (server) => hapiPlugin.onServerStart(server) | ||
}); | ||
} | ||
|
||
module.exports.HapiPlugin = HapiPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { processLogData } from '../helpers'; | ||
|
||
/** | ||
* HapiPlugin | ||
* | ||
*/ | ||
export default class HapiPlugin { | ||
|
||
/** | ||
* | ||
* @param {Logger} Logger | ||
* @constructor | ||
*/ | ||
constructor(Logger) { | ||
this._logger = Logger; | ||
} | ||
|
||
/** | ||
* | ||
* @param {Server} server | ||
* @param next | ||
*/ | ||
async onServerStart(server, next) { | ||
this._logger.info('server started:', server.info.uri); | ||
|
||
if (server.version < '17.0.0') { | ||
return next(); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {Server} server | ||
* @param {Object} event | ||
* @param {Array} tags | ||
*/ | ||
onServerLog(server, event, tags) { | ||
const data = event.error || event.data; | ||
const { severity, message } = processLogData(data, tags); | ||
this._logger[severity](message); | ||
} | ||
|
||
/** | ||
* | ||
* @param {Request} request | ||
* @param {Object} event | ||
* @param {Array} tags | ||
*/ | ||
onRequestLog(request, event, tags) { | ||
const data = event.error || event.data; | ||
const { severity, message } = processLogData(data, tags); | ||
this._logger[severity](message); | ||
} | ||
|
||
/** | ||
* | ||
* @param {Request} request | ||
* @param {error} err | ||
*/ | ||
onRequestError(request, err) { | ||
this._logger.error(err); | ||
} | ||
|
||
/** | ||
* | ||
* @param {Request} request | ||
* @param {Object} event | ||
* @param {Object} tags | ||
*/ | ||
onRequestInternal(request, event, tags) { | ||
// new request just received | ||
if (tags.received) { | ||
const method = request.method.toUpperCase(); | ||
const proto = (request.headers['x-forwarded-proto'] || request.connection.info.protocol); | ||
const url = `${proto}://${request.info.host}${request.url.path}`; | ||
this._logger.debug(method, url); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async
безawait
'ов.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
И, кстати, как hapi < 17 отреагирует на то, что
register
вернёт промис?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hapi < 17 нормально это воспринимает. А для 17+ это может быть критично, он await-ит регистрацию плагина.