Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/channels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

exports = module.exports = {};

try {
const DiagnosticsChannel = require('diagnostics_channel');

exports = module.exports = {
serverChannel: DiagnosticsChannel.channel('hapi:server')
};
}
catch {
// diagnostics_channel is not supported (e.g. node 12)
}

5 changes: 5 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Shot = require('@hapi/shot');
const Somever = require('@hapi/somever');
const Teamwork = require('@hapi/teamwork');

const Channels = require('./channels');
const Config = require('./config');
const Core = require('./core');
const Cors = require('./cors');
Expand Down Expand Up @@ -85,6 +86,10 @@ internals.Server = class {
}

core.registerServer(this);

if (Channels.serverChannel && Channels.serverChannel.hasSubscribers) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasSubscribers should only be checked when it is expensive to prepare the object that is published.

Channels.serverChannel.publish(this);
}
}

_clone(name) {
Expand Down
28 changes: 28 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2941,6 +2941,34 @@ describe('Server', () => {
expect(res3.result).to.equal('<h1>grabbed</h1>');
});
});

describe('Diagnostics channel', () => {

it('Fires server event on server creation', async () => {

// If diagnostics_channel is not supported (node 12), skip test
try {
require('diagnostics_channel');
}
catch {
return Promise.resolve();
}

const DiagnosticsChannel = require('diagnostics_channel');

const serverChannel = DiagnosticsChannel.channel('hapi:server');
return await new Promise((resolve) => {

serverChannel.subscribe((server) => {

expect(server).to.exist();
resolve();
});

new Hapi.Server();
});
});
});
});


Expand Down