Repository navigation
Expand file tree
/
Copy pathreply.js
More file actions
executable file
·168 lines (107 loc) · 3.97 KB
/
Copy pathreply.js
File metadata and controls
executable file
·168 lines (107 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Load modules
var Hoek = require('hoek');
var Response = require('./response');
// Declare internals
var internals = {};
exports = module.exports = internals.Reply = function () {
this._decorations = null;
};
internals.Reply.prototype.decorate = function (property, method) {
Hoek.assert(!this._decorations || !this._decorations[property], 'Reply interface decoration already defined:', property);
Hoek.assert(['request', 'response', 'close', 'state', 'unstate', 'redirect', 'continue'].indexOf(property) === -1, 'Cannot override built-in reply interface decoration:', property);
this._decorations = this._decorations || {};
this._decorations[property] = method;
};
/*
var handler = function (request, reply) {
reply(error, result, ignore); -> error || result (continue)
reply(...).takeover(); -> ... (continue)
reply.continue(ignore); -> null (continue)
};
var ext = function (request, reply) {
reply(error, result, ignore); -> error || result (respond)
reply(...).takeover(); -> ... (respond)
reply.continue(ignore); -> (continue)
};
var pre = function (request, reply) {
reply(error); -> error (respond) // failAction override
reply(null, result, ignore); -> result (continue)
reply(...).takeover(); -> ... (respond)
reply.continue(ignore); -> null (continue)
};
var auth = function (request, reply) {
reply(error, result, data); -> error || result (respond) + data
reply(...).takeover(); -> ... (respond) + data
reply.continue(data); -> (continue) + data
};
*/
internals.Reply.prototype.interface = function (request, realm, next) { // next(err || response, data);
var reply = function (err, response, data) {
reply._data = data; // Held for later
return reply.response(err !== null && err !== undefined ? err : response);
};
reply._replied = false;
reply._next = Hoek.once(next);
reply.realm = realm;
reply.request = request;
reply.response = internals.response;
reply.close = internals.close;
reply.state = internals.state;
reply.unstate = internals.unstate;
reply.redirect = internals.redirect;
reply.continue = internals.continue;
if (this._decorations) {
var methods = Object.keys(this._decorations);
for (var i = 0, il = methods.length; i < il; ++i) {
var method = methods[i];
reply[method] = this._decorations[method];
}
}
return reply;
};
internals.close = function (options) {
options = options || {};
this._next({ closed: true, end: options.end !== false });
};
internals.continue = function (data) {
this._next(null, data);
this._next = null;
};
internals.state = function (name, value, options) {
this.request._setState(name, value, options);
};
internals.unstate = function (name) {
this.request._clearState(name);
};
internals.redirect = function (location) {
return this.response('').redirect(location);
};
internals.response = function (result) {
var self = this;
Hoek.assert(!this._replied, 'reply interface called twice');
this._replied = true;
var response = Response.wrap(result, this.request);
if (response.isBoom) {
this._next(response, this._data);
this._next = null;
return response;
}
response.hold = function () {
this.hold = undefined;
this.send = function () {
this.send = undefined;
this._prepare(self._data, self._next);
this._next = null;
};
return this;
};
process.nextTick(function () {
response.hold = undefined;
if (!response.send &&
self._next) {
response._prepare(self._data, self._next);
self._next = null;
}
});
return response;
};