Skip to content
Merged
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
25 changes: 25 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [Server properties](#server-properties)
- [`server.app`](#serverapp)
- [`server.connections`](#serverconnections)
- [`server.decorations`](#serverdecorations)
- [`server.info`](#serverinfo)
- [`server.load`](#serverload)
- [`server.listener`](#serverlistener)
Expand Down Expand Up @@ -272,6 +273,30 @@ Each connection object contains:
- `lookup()` -
- `match()` -

### `server.decorations`

Provides access to the decorations already applied to various framework interfaces. The object
must not be modified directly, but only through [`server.decorate`](#serverdecoratetype-property-method-options)
- `type` - the decorated interface. Supported types:
- `'request'` - decorations on the [Request object](#request-object).
- `'reply'` - decorations on the [reply interface](#reply-interface).
- `'server'` - decorations on the [Server](#server) object.

```js
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ port: 80 });

const success = function () {

return this.response({ status: 'ok' });
};

server.decorate('reply', 'success', success);
return server.decorations.reply;
// returns ['success']
```


#### `server.info`

Expand Down
8 changes: 5 additions & 3 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,17 @@ internals.Plugin.prototype.decorate = function (type, property, method, options)
// Request

if (type === 'request') {
return this.root._requestor.decorate(property, method, options);
this.root._requestor.decorate(property, method, options);
return this.root.decorations[type].push(property);
}

Hoek.assert(!options, 'Cannot specify options for non-request decoration');

// Reply

if (type === 'reply') {
return this.root._replier.decorate(property, method);
this.root._replier.decorate(property, method);
return this.root.decorations[type].push(property);
}

// Server
Expand All @@ -383,6 +385,7 @@ internals.Plugin.prototype.decorate = function (type, property, method, options)
Hoek.assert(this[property] === undefined && this.root[property] === undefined, 'Cannot override the built-in server interface method:', property);

this.root._decorations[property] = method;
this.root.decorations[type].push(property);

this[property] = method;
let parent = this._parent;
Expand All @@ -392,7 +395,6 @@ internals.Plugin.prototype.decorate = function (type, property, method, options)
}
};


internals.Plugin.prototype.dependency = function (dependencies, after) {

Hoek.assert(this.realm.plugin, 'Cannot call dependency() outside of a plugin');
Expand Down
1 change: 0 additions & 1 deletion lib/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ internals.Reply.prototype.decorate = function (property, method) {
this._decorations[property] = method;
};


/*
const handler = function (request, reply) {

Expand Down
5 changes: 5 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ exports = module.exports = internals.Server = function (options) {
this._replier = new Reply();
this._requestor = new Request();
this._decorations = {};
this.decorations = {
request: [],
reply: [],
server: []
};
this._plugins = {}; // Exposed plugin properties by name
this._app = {};
this._registring = false; // true while register() is waiting for plugin callbacks
Expand Down
152 changes: 152 additions & 0 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2594,6 +2594,158 @@ describe('Plugin', () => {
});
});

describe('decorations ()', () => {

it('shows decorations on request (empty array)', (done) => {

const server = new Hapi.Server();
server.connection();

expect(server.decorations.request).to.be.empty();
done();
});

it('shows decorations on request (single)', (done) => {

const server = new Hapi.Server();
server.connection();

server.decorate('request', 'a', () => { });

expect(server.decorations.request).to.equal(['a']);
done();
});

it('shows decorations on request (many)', (done) => {

const server = new Hapi.Server();
server.connection();

server.decorate('request', 'a', () => { });
server.decorate('request', 'b', () => { });

expect(server.decorations.request).to.equal(['a', 'b']);
done();
});

it('shows decorations on request (multiple connections)', (done) => {

const server = new Hapi.Server();

server.connection({ labels: ['alpha'] });
server.connection({ labels: ['beta'] });

const conn1 = server.select('alpha');
const conn2 = server.select('beta');

conn1.decorate('request', 'a', () => { });
conn2.decorate('request', 'b', () => { });

expect(server.decorations.request).to.equal(['a', 'b']);
done();
});

it('shows decorations on reply (empty array)', (done) => {

const server = new Hapi.Server();
server.connection();

expect(server.decorations.reply).to.be.empty();
done();
});

it('shows decorations on reply (single)', (done) => {

const server = new Hapi.Server();
server.connection();

server.decorate('reply', 'a', () => { });

expect(server.decorations.reply).to.equal(['a']);
done();
});

it('shows decorations on reply (many)', (done) => {

const server = new Hapi.Server();
server.connection();

server.decorate('reply', 'a', () => { });
server.decorate('reply', 'b', () => { });

expect(server.decorations.reply).to.equal(['a', 'b']);
done();
});

it('shows decorations on reply (multiple connections)', (done) => {

const server = new Hapi.Server();

server.connection({ labels: ['alpha'] });
server.connection({ labels: ['beta'] });

expect(server.connections.length).to.equal(2);

const conn1 = server.select('alpha');
const conn2 = server.select('beta');

conn1.decorate('reply', 'a', () => { });
conn2.decorate('reply', 'b', () => { });

expect(server.decorations.reply).to.equal(['a', 'b']);
done();
});

it('shows decorations on server (empty array)', (done) => {

const server = new Hapi.Server();
server.connection();

expect(server.decorations.server).to.be.empty();
done();
});

it('shows decorations on server (single)', (done) => {

const server = new Hapi.Server();
server.connection();

server.decorate('server', 'a', () => { });

expect(server.decorations.server).to.equal(['a']);
done();
});

it('shows decorations on server (many)', (done) => {

const server = new Hapi.Server();
server.connection();

server.decorate('server', 'a', () => { });
server.decorate('server', 'b', () => { });

expect(server.decorations.server).to.equal(['a', 'b']);
done();
});

it('shows decorations on server (multiple connections)', (done) => {

const server = new Hapi.Server();

server.connection({ labels: ['alpha'] });
server.connection({ labels: ['beta'] });

const conn1 = server.select('alpha');
const conn2 = server.select('beta');

conn1.decorate('server', 'a', () => { });
conn2.decorate('server', 'b', () => { });

expect(server.decorations.server).to.equal(['a', 'b']);
done();
});
});

describe('dependency()', () => {

it('fails to register single plugin with dependencies', (done) => {
Expand Down