Skip to content

Commit

Permalink
feat: support @hapi/hapi (#1108)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjin authored Sep 3, 2019
1 parent 801e495 commit d545e93
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/plugin-hapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/plugin-fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
"hapi": "^17.0.0"
}
},
"hapi18": {
"dependencies": {
"@hapi/hapi": "^18.0.0"
}
},
"hapi8": {
"dependencies": {
"hapi": "^8.8.1"
Expand Down
22 changes: 15 additions & 7 deletions test/test-config-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand All @@ -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');
});
});
3 changes: 2 additions & 1 deletion test/test-trace-web-frameworks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -61,6 +61,7 @@ const FRAMEWORKS: WebFrameworkConstructor[] = [
Hapi15,
Hapi16,
Hapi17,
Hapi18,
Koa1,
Koa2,
Restify3,
Expand Down
39 changes: 23 additions & 16 deletions test/web-frameworks/hapi17.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,26 @@ 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');

interface AppState {
[TAIL_WORK]?: Array<Promise<void>>;
}

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<string, WebFrameworkAddHandlerOptions[]>();
private registering = Promise.resolve();
routes = new Map<string, WebFrameworkAddHandlerOptions[]>();
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(
Expand Down Expand Up @@ -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

0 comments on commit d545e93

Please sign in to comment.