-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtoolkit.js
More file actions
executable file
·234 lines (163 loc) · 6.21 KB
/
toolkit.js
File metadata and controls
executable file
·234 lines (163 loc) · 6.21 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'use strict';
const Boom = require('@hapi/boom');
const Bounce = require('@hapi/bounce');
const Hoek = require('@hapi/hoek');
const Response = require('./response');
const internals = {
reserved: ['abandon', 'authenticated', 'close', 'context', 'continue', 'entity', 'redirect', 'realm', 'request', 'response', 'state', 'unauthenticated', 'unstate']
};
exports = module.exports = internals.Manager = class {
constructor() {
this.abandon = Symbol('abandon');
this.close = Symbol('close');
this.continue = Symbol('continue');
this.reserved = internals.reserved;
}
async execute(method, request, options) {
const h = new internals.Toolkit(request, this, options);
const bind = options.bind || null;
try {
let operation;
if (bind) {
operation = method.call(bind, request, h);
}
else {
if (options.args) {
operation = method(request, h, ...options.args);
}
else {
operation = method(request, h);
}
}
var response = await internals.Manager.timed(operation, options);
}
catch (err) {
if (Bounce.isSystem(err)) {
response = Boom.badImplementation(err);
}
else if (!Bounce.isError(err)) {
response = Boom.badImplementation('Cannot throw non-error object', err);
}
else {
response = Boom.boomify(err);
}
}
// Process response
if (response === undefined) {
response = Boom.badImplementation(`${method.name} method did not return a value, a promise, or throw an error`);
}
if (options.continue &&
response === this.continue) {
if (options.continue === 'undefined') {
return;
}
// 'null'
response = null;
}
if (options.auth &&
response instanceof internals.Auth) {
return response;
}
if (typeof response !== 'symbol') {
response = Response.wrap(response, request);
if (!response.isBoom) {
response = await response._prepare();
}
}
return response;
}
failAction(request, failAction, err, options) {
const retain = options.retain ? err : undefined;
if (failAction === 'ignore') {
return retain;
}
if (failAction === 'log') {
request._log(options.tags, err);
return retain;
}
if (failAction === 'error') {
throw err;
}
return this.execute(failAction, request, { realm: request.route.realm, args: [options.details || err] });
}
static timed(method, options) {
if (!options.timeout) {
return method;
}
const timer = new Promise((resolve, reject) => {
const handler = () => {
reject(Boom.internal(`${options.name} timed out`));
};
setTimeout(handler, options.timeout);
});
return Promise.race([timer, method]);
}
};
/*
const handler = function (request, h) {
result / h.response(result) -> result // Not allowed before handler
h.response(result).takeover() -> result (respond)
h.continue -> null // Defaults to null only in handler and pre, not allowed in auth
throw error / h.response(error) -> error (respond) // failAction override in pre
<undefined> -> badImplementation (respond)
// Auth only (scheme.payload and scheme.response use the same interface as pre-handler extension methods)
h.unauthenticated(error, data) -> error (respond) + data
h.authenticated(data ) -> (continue) + data
};
*/
internals.Toolkit = class {
constructor(request, manager, options) {
this.abandon = manager.abandon;
this.close = manager.close;
this.continue = manager.continue;
this.context = options.bind;
this.realm = options.realm;
this.request = request;
if (options.auth) {
this.authenticated = internals.authenticated;
this.unauthenticated = internals.unauthenticated;
}
for (const method of request._core.decorations.toolkit) {
this[method] = request._core._decorations.toolkit[method];
}
}
response(result) {
Hoek.assert(!result || typeof result !== 'object' || typeof result.then !== 'function', 'Cannot wrap a promise');
Hoek.assert(result instanceof Error === false, 'Cannot wrap an error');
Hoek.assert(typeof result !== 'symbol', 'Cannot wrap a symbol');
return Response.wrap(result, this.request);
}
redirect(location) {
return this.response('').redirect(location);
}
entity(options) {
Hoek.assert(options, 'Entity method missing required options');
Hoek.assert(options.etag || options.modified, 'Entity methods missing required options key');
this.request._entity = options;
const entity = Response.entity(options.etag, options);
if (Response.unmodified(this.request, entity)) {
return this.response().code(304).takeover();
}
}
state(name, value, options) {
this.request._setState(name, value, options);
}
unstate(name, options) {
this.request._clearState(name, options);
}
};
internals.authenticated = function (data) {
Hoek.assert(data && data.credentials, 'Authentication data missing credentials information');
return new internals.Auth(null, data);
};
internals.unauthenticated = function (error, data) {
Hoek.assert(!data || data.credentials, 'Authentication data missing credentials information');
return new internals.Auth(error, data);
};
internals.Auth = class {
constructor(error, data) {
this.isAuth = true;
this.error = error;
this.data = data;
}
};