Skip to content

Commit d545e93

Browse files
authored
feat: support @hapi/hapi (#1108)
1 parent 801e495 commit d545e93

File tree

6 files changed

+47
-25
lines changed

6 files changed

+47
-25
lines changed

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ export const defaultConfig = {
334334
'generic-pool': path.join(pluginDirectory, 'plugin-generic-pool.js'),
335335
grpc: path.join(pluginDirectory, 'plugin-grpc.js'),
336336
hapi: path.join(pluginDirectory, 'plugin-hapi.js'),
337+
'@hapi/hapi': path.join(pluginDirectory, 'plugin-hapi.js'),
337338
http: path.join(pluginDirectory, 'plugin-http.js'),
338339
http2: path.join(pluginDirectory, 'plugin-http2.js'),
339340
koa: path.join(pluginDirectory, 'plugin-koa.js'),

src/plugins/plugin-hapi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ const plugin: PluginTypes.Plugin = [
133133
* available in every handler.
134134
*/
135135
{
136-
versions: '17',
136+
versions: '>=17',
137137
file: 'lib/request.js',
138138
// Request is a class name.
139139
// tslint:disable-next-line:variable-name

test/fixtures/plugin-fixtures.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@
5959
"hapi": "^17.0.0"
6060
}
6161
},
62+
"hapi18": {
63+
"dependencies": {
64+
"@hapi/hapi": "^18.0.0"
65+
}
66+
},
6267
"hapi8": {
6368
"dependencies": {
6469
"hapi": "^8.8.1"

test/test-config-plugins.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ import {PluginLoader, PluginLoaderConfig} from '../src/trace-plugin-loader';
2222

2323
import * as testTraceModule from './trace';
2424

25+
function assertPluginPath(
26+
plugins: {[pluginName: string]: string},
27+
pluginName: string
28+
) {
29+
// hapi was renamed to @hapi/hapi in v18. We still use the same plugin
30+
// filename.
31+
if (pluginName === '@hapi/hapi') {
32+
pluginName = 'hapi';
33+
}
34+
assert.ok(plugins[pluginName].includes(`plugin-${pluginName}.js`));
35+
}
36+
2537
describe('Configuration: Plugins', () => {
2638
const instrumentedModules = Object.keys(defaultConfig.plugins);
2739
let plugins: {[pluginName: string]: string} | null;
@@ -55,9 +67,7 @@ describe('Configuration: Plugins', () => {
5567
JSON.stringify(Object.keys(plugins!)),
5668
JSON.stringify(instrumentedModules)
5769
);
58-
instrumentedModules.forEach(e =>
59-
assert.ok(plugins![e].includes(`plugin-${e}.js`))
60-
);
70+
instrumentedModules.forEach(e => assertPluginPath(plugins!, e));
6171
});
6272

6373
it('should handle empty object', () => {
@@ -67,9 +77,7 @@ describe('Configuration: Plugins', () => {
6777
JSON.stringify(Object.keys(plugins!)),
6878
JSON.stringify(instrumentedModules)
6979
);
70-
instrumentedModules.forEach(e =>
71-
assert.ok(plugins![e].includes(`plugin-${e}.js`))
72-
);
80+
instrumentedModules.forEach(e => assertPluginPath(plugins!, e));
7381
});
7482

7583
it('should handle non-object', () => {
@@ -86,7 +94,7 @@ describe('Configuration: Plugins', () => {
8694
);
8795
instrumentedModules
8896
.filter(e => e !== 'express')
89-
.forEach(e => assert.ok(plugins![e].includes(`plugin-${e}.js`)));
97+
.forEach(e => assertPluginPath(plugins!, e));
9098
assert.strictEqual(plugins!.express, 'foo');
9199
});
92100
});

test/test-trace-web-frameworks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
import {WebFramework, WebFrameworkConstructor} from './web-frameworks/base';
3333
import {Connect3} from './web-frameworks/connect';
3434
import {Express4} from './web-frameworks/express';
35-
import {Hapi17} from './web-frameworks/hapi17';
35+
import {Hapi17, Hapi18} from './web-frameworks/hapi17';
3636
import {Hapi12, Hapi15, Hapi16, Hapi8} from './web-frameworks/hapi8_16';
3737
import {Koa1} from './web-frameworks/koa1';
3838
import {Koa2} from './web-frameworks/koa2';
@@ -61,6 +61,7 @@ const FRAMEWORKS: WebFrameworkConstructor[] = [
6161
Hapi15,
6262
Hapi16,
6363
Hapi17,
64+
Hapi18,
6465
Koa1,
6566
Koa2,
6667
Restify3,

test/web-frameworks/hapi17.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,26 @@ import {EventEmitter} from 'events';
1818

1919
import {hapi_17} from '../../src/plugins/types';
2020

21-
import {
22-
WebFramework,
23-
WebFrameworkAddHandlerOptions,
24-
WebFrameworkHandlerFunction,
25-
WebFrameworkResponse,
26-
} from './base';
21+
import {WebFramework, WebFrameworkAddHandlerOptions} from './base';
2722

2823
const TAIL_WORK = Symbol('tail work for hapi');
2924

3025
interface AppState {
3126
[TAIL_WORK]?: Array<Promise<void>>;
3227
}
3328

34-
export class Hapi17 extends EventEmitter implements WebFramework {
35-
static commonName = `hapi@17`;
36-
static expectedTopStackFrame = '_executeWrap';
37-
static versionRange = '>=7.5';
38-
39-
private server: hapi_17.Server;
29+
class Hapi extends EventEmitter implements WebFramework {
30+
server: hapi_17.Server;
4031
// We can't add two routes on the same path.
4132
// So instead of registering a new Hapi plugin per path,
4233
// register only the first time -- passing a function that will iterate
4334
// through a list of routes keyed under the path.
44-
private routes = new Map<string, WebFrameworkAddHandlerOptions[]>();
45-
private registering = Promise.resolve();
35+
routes = new Map<string, WebFrameworkAddHandlerOptions[]>();
36+
registering = Promise.resolve();
4637

47-
constructor() {
38+
constructor(path: string) {
4839
super();
49-
const hapi = require('../plugins/fixtures/hapi17') as typeof hapi_17;
40+
const hapi = require(path) as typeof hapi_17;
5041
this.server = new hapi.Server();
5142
this.server.events.on('response', (request: hapi_17.Request) => {
5243
Promise.all((request.app as AppState)[TAIL_WORK] || []).then(
@@ -116,3 +107,19 @@ export class Hapi17 extends EventEmitter implements WebFramework {
116107
this.server.stop();
117108
}
118109
}
110+
111+
const makeHapiClass = (version: number) =>
112+
class extends Hapi {
113+
static commonName = `hapi@${version}`;
114+
static expectedTopStackFrame = '_executeWrap';
115+
static versionRange = '>=7.5';
116+
117+
constructor() {
118+
super(`../plugins/fixtures/hapi${version}`);
119+
}
120+
};
121+
122+
// tslint:disable:variable-name (Hapi* are class names)
123+
export const Hapi17 = makeHapiClass(17);
124+
export const Hapi18 = makeHapiClass(18);
125+
// tslint:enable:variable-name

0 commit comments

Comments
 (0)