From 3baf3a8347e0786837f86015dfbedeacd057371d Mon Sep 17 00:00:00 2001 From: Kelvin Jin Date: Mon, 26 Aug 2019 11:11:39 -0700 Subject: [PATCH] feat: support @hapi/hapi --- src/config.ts | 1 + src/plugins/plugin-hapi.ts | 2 +- test/fixtures/plugin-fixtures.json | 5 ++++ test/test-config-plugins.ts | 22 +++++++++++------ test/test-trace-web-frameworks.ts | 3 ++- test/web-frameworks/hapi17.ts | 39 ++++++++++++++++++------------ 6 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/config.ts b/src/config.ts index 305d06be1..cff135718 100644 --- a/src/config.ts +++ b/src/config.ts @@ -334,6 +334,7 @@ export const defaultConfig = { 'generic-pool': path.join(pluginDirectory, 'plugin-generic-pool.js'), grpc: path.join(pluginDirectory, 'plugin-grpc.js'), hapi: path.join(pluginDirectory, 'plugin-hapi.js'), + '@hapi/hapi': path.join(pluginDirectory, 'plugin-hapi.js'), http: path.join(pluginDirectory, 'plugin-http.js'), http2: path.join(pluginDirectory, 'plugin-http2.js'), koa: path.join(pluginDirectory, 'plugin-koa.js'), diff --git a/src/plugins/plugin-hapi.ts b/src/plugins/plugin-hapi.ts index d4ecc8859..c73a88ce0 100644 --- a/src/plugins/plugin-hapi.ts +++ b/src/plugins/plugin-hapi.ts @@ -133,7 +133,7 @@ const plugin: PluginTypes.Plugin = [ * available in every handler. */ { - versions: '17', + versions: '>=17', file: 'lib/request.js', // Request is a class name. // tslint:disable-next-line:variable-name diff --git a/test/fixtures/plugin-fixtures.json b/test/fixtures/plugin-fixtures.json index 98597d1c9..4d89fada5 100644 --- a/test/fixtures/plugin-fixtures.json +++ b/test/fixtures/plugin-fixtures.json @@ -59,6 +59,11 @@ "hapi": "^17.0.0" } }, + "hapi18": { + "dependencies": { + "@hapi/hapi": "^18.0.0" + } + }, "hapi8": { "dependencies": { "hapi": "^8.8.1" diff --git a/test/test-config-plugins.ts b/test/test-config-plugins.ts index 22b22f6cc..d5f30a4a1 100644 --- a/test/test-config-plugins.ts +++ b/test/test-config-plugins.ts @@ -22,6 +22,18 @@ import {PluginLoader, PluginLoaderConfig} from '../src/trace-plugin-loader'; import * as testTraceModule from './trace'; +function assertPluginPath( + plugins: {[pluginName: string]: string}, + pluginName: string +) { + // hapi was renamed to @hapi/hapi in v18. We still use the same plugin + // filename. + if (pluginName === '@hapi/hapi') { + pluginName = 'hapi'; + } + assert.ok(plugins[pluginName].includes(`plugin-${pluginName}.js`)); +} + describe('Configuration: Plugins', () => { const instrumentedModules = Object.keys(defaultConfig.plugins); let plugins: {[pluginName: string]: string} | null; @@ -55,9 +67,7 @@ describe('Configuration: Plugins', () => { JSON.stringify(Object.keys(plugins!)), JSON.stringify(instrumentedModules) ); - instrumentedModules.forEach(e => - assert.ok(plugins![e].includes(`plugin-${e}.js`)) - ); + instrumentedModules.forEach(e => assertPluginPath(plugins!, e)); }); it('should handle empty object', () => { @@ -67,9 +77,7 @@ describe('Configuration: Plugins', () => { JSON.stringify(Object.keys(plugins!)), JSON.stringify(instrumentedModules) ); - instrumentedModules.forEach(e => - assert.ok(plugins![e].includes(`plugin-${e}.js`)) - ); + instrumentedModules.forEach(e => assertPluginPath(plugins!, e)); }); it('should handle non-object', () => { @@ -86,7 +94,7 @@ describe('Configuration: Plugins', () => { ); instrumentedModules .filter(e => e !== 'express') - .forEach(e => assert.ok(plugins![e].includes(`plugin-${e}.js`))); + .forEach(e => assertPluginPath(plugins!, e)); assert.strictEqual(plugins!.express, 'foo'); }); }); diff --git a/test/test-trace-web-frameworks.ts b/test/test-trace-web-frameworks.ts index d5a650b46..b74bec475 100644 --- a/test/test-trace-web-frameworks.ts +++ b/test/test-trace-web-frameworks.ts @@ -32,7 +32,7 @@ import { import {WebFramework, WebFrameworkConstructor} from './web-frameworks/base'; import {Connect3} from './web-frameworks/connect'; import {Express4} from './web-frameworks/express'; -import {Hapi17} from './web-frameworks/hapi17'; +import {Hapi17, Hapi18} from './web-frameworks/hapi17'; import {Hapi12, Hapi15, Hapi16, Hapi8} from './web-frameworks/hapi8_16'; import {Koa1} from './web-frameworks/koa1'; import {Koa2} from './web-frameworks/koa2'; @@ -61,6 +61,7 @@ const FRAMEWORKS: WebFrameworkConstructor[] = [ Hapi15, Hapi16, Hapi17, + Hapi18, Koa1, Koa2, Restify3, diff --git a/test/web-frameworks/hapi17.ts b/test/web-frameworks/hapi17.ts index 66359b54b..98581304e 100644 --- a/test/web-frameworks/hapi17.ts +++ b/test/web-frameworks/hapi17.ts @@ -18,12 +18,7 @@ import {EventEmitter} from 'events'; import {hapi_17} from '../../src/plugins/types'; -import { - WebFramework, - WebFrameworkAddHandlerOptions, - WebFrameworkHandlerFunction, - WebFrameworkResponse, -} from './base'; +import {WebFramework, WebFrameworkAddHandlerOptions} from './base'; const TAIL_WORK = Symbol('tail work for hapi'); @@ -31,22 +26,18 @@ interface AppState { [TAIL_WORK]?: Array>; } -export class Hapi17 extends EventEmitter implements WebFramework { - static commonName = `hapi@17`; - static expectedTopStackFrame = '_executeWrap'; - static versionRange = '>=7.5'; - - private server: hapi_17.Server; +class Hapi extends EventEmitter implements WebFramework { + server: hapi_17.Server; // We can't add two routes on the same path. // So instead of registering a new Hapi plugin per path, // register only the first time -- passing a function that will iterate // through a list of routes keyed under the path. - private routes = new Map(); - private registering = Promise.resolve(); + routes = new Map(); + registering = Promise.resolve(); - constructor() { + constructor(path: string) { super(); - const hapi = require('../plugins/fixtures/hapi17') as typeof hapi_17; + const hapi = require(path) as typeof hapi_17; this.server = new hapi.Server(); this.server.events.on('response', (request: hapi_17.Request) => { Promise.all((request.app as AppState)[TAIL_WORK] || []).then( @@ -116,3 +107,19 @@ export class Hapi17 extends EventEmitter implements WebFramework { this.server.stop(); } } + +const makeHapiClass = (version: number) => + class extends Hapi { + static commonName = `hapi@${version}`; + static expectedTopStackFrame = '_executeWrap'; + static versionRange = '>=7.5'; + + constructor() { + super(`../plugins/fixtures/hapi${version}`); + } + }; + +// tslint:disable:variable-name (Hapi* are class names) +export const Hapi17 = makeHapiClass(17); +export const Hapi18 = makeHapiClass(18); +// tslint:enable:variable-name