Skip to content

Commit b02673a

Browse files
committed
Allow initializing connectionless. Closes #3325
1 parent 1cc8f35 commit b02673a

File tree

5 files changed

+63
-11
lines changed

5 files changed

+63
-11
lines changed

lib/server.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ internals.Server.prototype.start = function (callback) {
182182
Hoek.assert(typeof callback === 'function', 'Missing required start callback function');
183183
const nextTickCallback = Hoek.nextTick(callback);
184184

185+
if (!this.connections.length) {
186+
return nextTickCallback(new Error('No connections to start'));
187+
}
188+
185189
if (this._state === 'initialized' ||
186190
this._state === 'started') {
187191

@@ -224,10 +228,6 @@ internals.Server.prototype.initialize = function (callback) {
224228
Hoek.assert(typeof callback === 'function', 'Missing required start callback function');
225229
const nextTickCallback = Hoek.nextTick(callback);
226230

227-
if (!this.connections.length) {
228-
return nextTickCallback(new Error('No connections to start'));
229-
}
230-
231231
if (this._registring) {
232232
return nextTickCallback(new Error('Cannot start server before plugins finished registration'));
233233
}
@@ -249,12 +249,8 @@ internals.Server.prototype.initialize = function (callback) {
249249

250250
// Start cache
251251

252-
const each = (cache, next) => {
253-
254-
this._caches[cache].client.start(next);
255-
};
256-
257252
const caches = Object.keys(this._caches);
253+
const each = (cache, next) => this._caches[cache].client.start(next);
258254
Items.parallel(caches, each, (err) => {
259255

260256
if (err) {

npm-shrinkwrap.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "hapi",
33
"description": "HTTP Server framework",
44
"homepage": "http://hapijs.com",
5-
"version": "15.0.1",
5+
"version": "15.0.2",
66
"repository": {
77
"type": "git",
88
"url": "git://github.com/hapijs/hapi"

test/plugin.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2870,6 +2870,49 @@ describe('Plugin', () => {
28702870
});
28712871
});
28722872
});
2873+
2874+
it('errors when missing dependencies on connection added after start', (done) => {
2875+
2876+
const a = function (srv, options, next) {
2877+
2878+
return next();
2879+
};
2880+
2881+
a.attributes = {
2882+
name: 'a'
2883+
};
2884+
2885+
const b = function (srv, options, next) {
2886+
2887+
srv.dependency('a');
2888+
return next();
2889+
};
2890+
2891+
b.attributes = {
2892+
name: 'b'
2893+
};
2894+
2895+
const server = new Hapi.Server();
2896+
server.connection();
2897+
server.register([a, b], (err) => {
2898+
2899+
expect(err).to.not.exist();
2900+
server.start((err) => {
2901+
2902+
expect(err).to.not.exist();
2903+
const select = server.connection();
2904+
select.register(b, (err) => {
2905+
2906+
expect(err).to.not.exist();
2907+
server.start((err) => {
2908+
2909+
expect(err).to.be.an.error(`Plugin b missing dependency a in connection: ${select.info.uri}`);
2910+
server.stop(done);
2911+
});
2912+
});
2913+
});
2914+
});
2915+
});
28732916
});
28742917

28752918
describe('encoder()', () => {

test/server.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ describe('Server', () => {
5252
done();
5353
});
5454

55+
describe('initialize()', () => {
56+
57+
it('allows initializing a server without connections', (done) => {
58+
59+
const server = new Hapi.Server();
60+
server.initialize((err) => {
61+
62+
expect(err).to.not.exist();
63+
done();
64+
});
65+
});
66+
});
67+
5568
describe('start()', () => {
5669

5770
it('starts and stops', (done) => {

0 commit comments

Comments
 (0)