Skip to content

Commit

Permalink
fix(instrumentation-hapi): fix this binding for plugin register method (
Browse files Browse the repository at this point in the history
  • Loading branch information
seemk authored Jan 7, 2025
1 parent 3bab134 commit f6ded2e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,13 @@ export class HapiInstrumentation extends InstrumentationBase {
private _wrapRegisterHandler<T>(plugin: Hapi.Plugin<T>): void {
const instrumentation: HapiInstrumentation = this;
const pluginName = getPluginName(plugin);
const oldHandler = plugin.register;
const oldRegister = plugin.register;
const self = this;
const newRegisterHandler = function (server: Hapi.Server, options: T) {
const newRegisterHandler = function (
this: typeof plugin,
server: Hapi.Server,
options: T
) {
self._wrap(server, 'route', original => {
return instrumentation._getServerRoutePatch.bind(instrumentation)(
original,
Expand All @@ -287,7 +291,7 @@ export class HapiInstrumentation extends InstrumentationBase {
pluginName
);
});
return oldHandler(server, options);
return oldRegister.call(this, server, options);
};
plugin.register = newRegisterHandler;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ describe('Hapi Instrumentation - Hapi.Plugin Tests', () => {
name: 'simplePlugin',
version: '1.0.0',
multiple: true,
value: 42,
register: async function (server: hapi.Server, options: any) {
server.route({
method: 'GET',
path: '/hello',
handler: function (request, h) {
return `hello, world, ${options.name}`;
handler: (request, h) => {
return `hello, world, ${this.value} ${options.name}`;
},
});
},
Expand Down

0 comments on commit f6ded2e

Please sign in to comment.