forked from hapijs/hapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.js
More file actions
executable file
·199 lines (135 loc) · 5.41 KB
/
Copy pathmethods.js
File metadata and controls
executable file
·199 lines (135 loc) · 5.41 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
'use strict';
// Load modules
const Boom = require('boom');
const Hoek = require('hoek');
const Schema = require('./schema');
// Declare internals
const internals = {};
exports = module.exports = internals.Methods = function (server) {
this.server = server;
this.methods = {};
this._normalized = {};
};
internals.Methods.prototype.add = function (name, method, options, realm) {
if (typeof name !== 'object') {
return this._add(name, method, options, realm);
}
// {} or [{}, {}]
const items = [].concat(name);
for (let i = 0; i < items.length; ++i) {
const item = Schema.apply('methodObject', items[i]);
this._add(item.name, item.method, item.options, realm);
}
};
exports.methodNameRx = /^[_$a-zA-Z][$\w]*(?:\.[_$a-zA-Z][$\w]*)*$/;
internals.Methods.prototype._add = function (name, method, options, realm) {
Hoek.assert(typeof method === 'function', 'method must be a function');
Hoek.assert(typeof name === 'string', 'name must be a string');
Hoek.assert(name.match(exports.methodNameRx), 'Invalid name:', name);
Hoek.assert(!Hoek.reach(this.methods, name, { functions: false }), 'Server method function name already exists:', name);
options = Schema.apply('method', options || {}, name);
const settings = Hoek.cloneWithShallow(options, ['bind']);
settings.generateKey = settings.generateKey || internals.generateKey;
const bind = settings.bind || realm.settings.bind || null;
const apply = function () {
return method.apply(bind, arguments);
};
const bound = bind ? apply : method;
// Normalize methods
let normalized = bound;
if (settings.callback === false) { // Defaults to true
normalized = function (/* arg1, arg2, ..., argn, methodNext */) {
const args = [];
for (let i = 0; i < arguments.length - 1; ++i) {
args.push(arguments[i]);
}
const methodNext = arguments[arguments.length - 1];
let result = null;
let error = null;
try {
result = method.apply(bind, args);
}
catch (err) {
error = err;
}
if (result instanceof Error) {
error = result;
result = null;
}
if (error ||
typeof result !== 'object' ||
typeof result.then !== 'function') {
return methodNext(error, result);
}
// Promise object
const onFulfilled = (outcome) => methodNext(null, outcome);
const onRejected = (err) => methodNext(err);
result.then(onFulfilled, onRejected);
};
}
// Not cached
if (!settings.cache) {
return this._assign(name, bound, normalized);
}
// Cached
Hoek.assert(!settings.cache.generateFunc, 'Cannot set generateFunc with method caching:', name);
Hoek.assert(settings.cache.generateTimeout !== undefined, 'Method caching requires a timeout value in generateTimeout:', name);
settings.cache.generateFunc = (id, next) => {
id.args.push(next); // function (err, result, ttl)
normalized.apply(bind, id.args);
};
const cache = this.server.cache(settings.cache, '#' + name);
const func = function (/* arguments, methodNext */) {
const args = [];
for (let i = 0; i < arguments.length - 1; ++i) {
args.push(arguments[i]);
}
const methodNext = arguments[arguments.length - 1];
const key = settings.generateKey.apply(bind, args);
if (key === null || // Value can be ''
typeof key !== 'string') { // When using custom generateKey
return Hoek.nextTick(methodNext)(Boom.badImplementation('Invalid method key when invoking: ' + name, { name, args }));
}
cache.get({ id: key, args }, methodNext);
};
func.cache = {
drop: function (/* arguments, callback */) {
const args = [];
for (let i = 0; i < arguments.length - 1; ++i) {
args.push(arguments[i]);
}
const methodNext = arguments[arguments.length - 1];
const key = settings.generateKey.apply(null, args);
if (key === null) { // Value can be ''
return Hoek.nextTick(methodNext)(Boom.badImplementation('Invalid method key'));
}
return cache.drop(key, methodNext);
},
stats: cache.stats
};
this._assign(name, func, func);
};
internals.Methods.prototype._assign = function (name, method, normalized) {
const path = name.split('.');
let ref = this.methods;
for (let i = 0; i < path.length; ++i) {
if (!ref[path[i]]) {
ref[path[i]] = (i + 1 === path.length ? method : {});
}
ref = ref[path[i]];
}
this._normalized[name] = normalized;
};
internals.generateKey = function () {
let key = '';
for (let i = 0; i < arguments.length; ++i) {
const arg = arguments[i];
if (typeof arg !== 'string' &&
typeof arg !== 'number' &&
typeof arg !== 'boolean') {
return null;
}
key = key + (i ? ':' : '') + encodeURIComponent(arg.toString());
}
return key;
};