lien 4.0.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DOCUMENTATION.md +4 -290
- package/LICENSE +1 -1
- package/README.md +14 -321
- package/assets/logo.png +0 -0
- package/example/index.js +8 -10
- package/example/login.js +23 -36
- package/example/secure.js +6 -10
- package/lib/errors.js +58 -0
- package/lib/hook-registry.js +221 -0
- package/lib/hooks.js +302 -0
- package/lib/index.js +15 -1117
- package/lib/lien.js +539 -0
- package/lib/server.js +590 -0
- package/lib/sessions.js +56 -0
- package/lib/to-buffer.js +32 -45
- package/lib/utils.js +75 -0
- package/package.json +34 -21
- package/.github/FUNDING.yml +0 -4
- package/.travis.yml +0 -3
- package/CONTRIBUTING.md +0 -66
- package/test/index.js +0 -280
- package/test/public/foo/index.html +0 -1
- package/test/public/index.html +0 -12
- package/test/public/test.html +0 -1
- package/test/views/main.ajs +0 -1
package/lib/index.js
CHANGED
|
@@ -1,1122 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
6
|
-
|
|
7
|
-
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
|
8
|
-
|
|
9
|
-
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
|
10
|
-
|
|
11
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
12
|
-
|
|
13
|
-
var events = require("events"),
|
|
14
|
-
EventEmitter = events.EventEmitter,
|
|
15
|
-
express = require("express"),
|
|
16
|
-
bodyParser = require("body-parser"),
|
|
17
|
-
ul = require("ul"),
|
|
18
|
-
http = require("http"),
|
|
19
|
-
https = require("https"),
|
|
20
|
-
fs = require("fs"),
|
|
21
|
-
path = require("path"),
|
|
22
|
-
toBuffer = require("./to-buffer"),
|
|
23
|
-
session = require("express-session"),
|
|
24
|
-
findValue = require("find-value"),
|
|
25
|
-
qs = require("querystring"),
|
|
26
|
-
Transformer = require("transformer"),
|
|
27
|
-
setOrGet = require("set-or-get"),
|
|
28
|
-
httpMethods = require("methods"),
|
|
29
|
-
_require = require("path-to-regexp"),
|
|
30
|
-
pathToRegexp = _require.pathToRegexp,
|
|
31
|
-
iterateObject = require("iterate-object"),
|
|
32
|
-
noop = require("noop6"),
|
|
33
|
-
cookieParser = require("cookie-parser"),
|
|
34
|
-
csrf = require("csurf");
|
|
35
|
-
|
|
36
|
-
var LienObj = function () {
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* LienCreator
|
|
40
|
-
* Creates the `lien` object.
|
|
41
|
-
*
|
|
42
|
-
* @name LienCreator
|
|
43
|
-
* @function
|
|
44
|
-
* @param {Object} req The request object.
|
|
45
|
-
* @param {Object} res The response object.
|
|
46
|
-
* @param {Function} next The `next` middleware function.
|
|
47
|
-
* @param {Object} server The `Lien` server instance.
|
|
48
|
-
* @return {Lien} The lien object.
|
|
49
|
-
*/
|
|
50
|
-
function LienObj(req, res, next, server) {
|
|
51
|
-
var _this = this;
|
|
52
|
-
|
|
53
|
-
_classCallCheck(this, LienObj);
|
|
54
|
-
|
|
55
|
-
this._next = next;
|
|
56
|
-
this.req = req;
|
|
57
|
-
this.res = res;
|
|
58
|
-
|
|
59
|
-
this.path = this.req.path;
|
|
60
|
-
|
|
61
|
-
if (this.path.charAt(this.path.length - 1) === "/") {
|
|
62
|
-
this.path = this.path.slice(0, -1) || "/";
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
this.accepts = req.accepts.bind(req);
|
|
66
|
-
this.host = req.hostname;
|
|
67
|
-
this.params = req.params;
|
|
68
|
-
this.query = req.query;
|
|
69
|
-
this.pathname = req.baseUrl + req.path;
|
|
70
|
-
this.method = req.method.toLowerCase();
|
|
71
|
-
this.cookies = req.cookies || {};
|
|
72
|
-
this.server = server;
|
|
73
|
-
this.data = req.body;
|
|
74
|
-
this.session = req.session;
|
|
75
|
-
Object.defineProperty(this, "csrfToken", {
|
|
76
|
-
get: function get() {
|
|
77
|
-
return _this.req.csrfToken && _this.req.csrfToken() || "";
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
Object.defineProperty(this, "csrfInput", {
|
|
81
|
-
get: function get() {
|
|
82
|
-
return _this.req.csrfToken ? "<input type=\"hidden\" name=\"_csrf\" value=\"" + _this.req.csrfToken() + "\">" : "";
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
this.full_path = req.originalUrl;
|
|
87
|
-
this.protocol = req.protocol;
|
|
88
|
-
this.domain = this.protocol + "://" + this.host + (this.host === "localhost" ? ":" + server.options.port : "");
|
|
89
|
-
this.href = "" + this.domain + req.originalUrl;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* next
|
|
94
|
-
* Go to the next middleware handler.
|
|
95
|
-
*
|
|
96
|
-
* @name next
|
|
97
|
-
* @function
|
|
98
|
-
* @returns {Lien} The `Lien` instance.
|
|
99
|
-
*/
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
_createClass(LienObj, [{
|
|
103
|
-
key: "next",
|
|
104
|
-
value: function next(err, data) {
|
|
105
|
-
this._next(err, data);
|
|
106
|
-
return this;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* redirect
|
|
111
|
-
* Redirects the client to another url.
|
|
112
|
-
*
|
|
113
|
-
* @name redirect
|
|
114
|
-
* @function
|
|
115
|
-
* @param {String} newUrl The new url to redirect to.
|
|
116
|
-
* @param {Boolean|Object} query If `true`, the request querystring parameters will be appended. If it's an object, it will be merged with the request querystring parameters.
|
|
117
|
-
*/
|
|
118
|
-
|
|
119
|
-
}, {
|
|
120
|
-
key: "redirect",
|
|
121
|
-
value: function redirect(newUrl, query) {
|
|
122
|
-
if (query) {
|
|
123
|
-
var sQuery = null;
|
|
124
|
-
sQuery = qs.stringify(query === true ? this.query : ul.merge(query, this.query));
|
|
125
|
-
sQuery && (newUrl += "?" + sQuery);
|
|
126
|
-
}
|
|
127
|
-
this.res.redirect(newUrl);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* render
|
|
132
|
-
* Renders a template to the client.
|
|
133
|
-
*
|
|
134
|
-
* @name render
|
|
135
|
-
* @function
|
|
136
|
-
* @param {String} template The template name.
|
|
137
|
-
* @param {Object} data The template data.
|
|
138
|
-
*/
|
|
139
|
-
|
|
140
|
-
}, {
|
|
141
|
-
key: "render",
|
|
142
|
-
value: function render(template, data) {
|
|
143
|
-
this.res.render(template, data);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/*!
|
|
147
|
-
* _checkSessionSupport
|
|
148
|
-
* Checks if the session is supported or not.
|
|
149
|
-
*
|
|
150
|
-
* @name _checkSessionSupport
|
|
151
|
-
* @function
|
|
152
|
-
* @returns {Boolean} `false` if session is *not* supported. `true` otherwise.
|
|
153
|
-
*/
|
|
154
|
-
|
|
155
|
-
}, {
|
|
156
|
-
key: "_checkSessionSupport",
|
|
157
|
-
value: function _checkSessionSupport() {
|
|
158
|
-
if (this.session) {
|
|
159
|
-
return true;
|
|
160
|
-
}
|
|
161
|
-
console.warn("You're trying to use a session feature, but you didn't enable the session support.");
|
|
162
|
-
return false;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* startSession
|
|
167
|
-
* Starts a session.
|
|
168
|
-
*
|
|
169
|
-
* @name startSession
|
|
170
|
-
* @function
|
|
171
|
-
* @param {Object} data The session data.
|
|
172
|
-
*/
|
|
173
|
-
|
|
174
|
-
}, {
|
|
175
|
-
key: "startSession",
|
|
176
|
-
value: function startSession(data) {
|
|
177
|
-
if (!this._checkSessionSupport()) {
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
this.setSessionData(data);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* setSessionData
|
|
185
|
-
* Sets the session data.
|
|
186
|
-
*
|
|
187
|
-
* @name setSessionData
|
|
188
|
-
* @function
|
|
189
|
-
* @param {Object} data The session data.
|
|
190
|
-
*/
|
|
191
|
-
|
|
192
|
-
}, {
|
|
193
|
-
key: "setSessionData",
|
|
194
|
-
value: function setSessionData(data) {
|
|
195
|
-
if (!this._checkSessionSupport()) {
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
this.session._sessionData = ul.deepMerge(data, this.getSessionData());
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* getSessionData
|
|
203
|
-
* Returns the session data object/specific field.
|
|
204
|
-
*
|
|
205
|
-
* @name getSessionData
|
|
206
|
-
* @function
|
|
207
|
-
* @param {Field} field A specific field to get from the session object.
|
|
208
|
-
* @returns {Value|Object} The field value. If a field is not specified,
|
|
209
|
-
* the whole session data object is returned.
|
|
210
|
-
*/
|
|
211
|
-
|
|
212
|
-
}, {
|
|
213
|
-
key: "getSessionData",
|
|
214
|
-
value: function getSessionData(field) {
|
|
215
|
-
if (!this._checkSessionSupport()) {
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
if (field) {
|
|
219
|
-
return findValue(this.session._sessionData, field) || null;
|
|
220
|
-
}
|
|
221
|
-
return this.session._sessionData;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* destroySession
|
|
226
|
-
* Destroys the session.
|
|
227
|
-
*
|
|
228
|
-
* @name destroySession
|
|
229
|
-
* @function
|
|
230
|
-
*/
|
|
231
|
-
|
|
232
|
-
}, {
|
|
233
|
-
key: "destroySession",
|
|
234
|
-
value: function destroySession() {
|
|
235
|
-
if (!this._checkSessionSupport()) {
|
|
236
|
-
return;
|
|
237
|
-
}
|
|
238
|
-
this.session.destroy();
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* header
|
|
243
|
-
* Gets/sets/deletes headers.
|
|
244
|
-
*
|
|
245
|
-
* @name header
|
|
246
|
-
* @function
|
|
247
|
-
* @param {String} name The header name.
|
|
248
|
-
* @param {String} value The header value to set. If `null`, the header will be *removed*.
|
|
249
|
-
* @returns {Lien} The Lien instance.
|
|
250
|
-
*/
|
|
251
|
-
|
|
252
|
-
}, {
|
|
253
|
-
key: "header",
|
|
254
|
-
value: function header(name, value) {
|
|
255
|
-
|
|
256
|
-
if (!name && !value) {
|
|
257
|
-
return this.req.headers;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
if (value === null) {
|
|
261
|
-
return this.res.removeHeader(name);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
if (value === undefined && typeof name === "string") {
|
|
265
|
-
return this.req.header(name);
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
if ((typeof name === "undefined" ? "undefined" : _typeof(name)) === "object") {
|
|
269
|
-
this.res.set(name);
|
|
270
|
-
} else {
|
|
271
|
-
this.res.set(name, value);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
return this;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* apiMsg
|
|
279
|
-
* Sends to the client a JSON object containing the `message` field.
|
|
280
|
-
*
|
|
281
|
-
* @name apiMsg
|
|
282
|
-
* @function
|
|
283
|
-
* @param {String} msg The API message.
|
|
284
|
-
* @param {Number} status The status code (default: `200`).
|
|
285
|
-
*/
|
|
286
|
-
|
|
287
|
-
}, {
|
|
288
|
-
key: "apiMsg",
|
|
289
|
-
value: function apiMsg(msg, status) {
|
|
290
|
-
status = status || 200;
|
|
291
|
-
this.end({ message: msg }, status);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* apiError
|
|
296
|
-
* Like `apiMsg`, but by default with a status code of `422`.
|
|
297
|
-
*
|
|
298
|
-
* @name apiError
|
|
299
|
-
* @function
|
|
300
|
-
* @param {String} msg The API message.
|
|
301
|
-
* @param {Number} status The status code (default: `422`).
|
|
302
|
-
*/
|
|
303
|
-
|
|
304
|
-
}, {
|
|
305
|
-
key: "apiError",
|
|
306
|
-
value: function apiError(msg, status) {
|
|
307
|
-
msg = msg.message || msg;
|
|
308
|
-
this.apiMsg(msg, status || 422);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* end
|
|
313
|
-
* Ends the response sending the content.
|
|
314
|
-
*
|
|
315
|
-
* @name end
|
|
316
|
-
* @function
|
|
317
|
-
* @param {Anything} content The content that should be sent to the response.
|
|
318
|
-
* @param {Number} status The status code.
|
|
319
|
-
* @param {String} contentType The content type (e.g. `"json"`).
|
|
320
|
-
* @param {Object} headers Additional headers to send in the response.
|
|
321
|
-
*/
|
|
322
|
-
|
|
323
|
-
}, {
|
|
324
|
-
key: "end",
|
|
325
|
-
value: function end(content, status, contentType, headers) {
|
|
326
|
-
var _this2 = this;
|
|
327
|
-
|
|
328
|
-
var resData = {};
|
|
329
|
-
|
|
330
|
-
// end(content, status, headers)
|
|
331
|
-
if ((typeof contentType === "undefined" ? "undefined" : _typeof(contentType)) === "object") {
|
|
332
|
-
headers = contentType;
|
|
333
|
-
contentType = null;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
// end(status, headers)
|
|
337
|
-
if (typeof content === "number") {
|
|
338
|
-
status = content;
|
|
339
|
-
content = "";
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
resData.content = content;
|
|
343
|
-
resData.status = status || 200;
|
|
344
|
-
resData.headers = headers || {};
|
|
345
|
-
resData.contentType = contentType || this.res.get("Content-Type");
|
|
346
|
-
resData.lien = this;
|
|
347
|
-
|
|
348
|
-
var trans = this.server.getHooks("after", this.path, this.method);
|
|
349
|
-
|
|
350
|
-
if (trans) {
|
|
351
|
-
trans.start(resData, function (err, data) {
|
|
352
|
-
if (err) {
|
|
353
|
-
data.content = err;
|
|
354
|
-
}
|
|
355
|
-
_this2._sendResponse(data);
|
|
356
|
-
});
|
|
357
|
-
} else {
|
|
358
|
-
this._sendResponse(resData);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
/*!
|
|
363
|
-
* _sendResponse
|
|
364
|
-
* Low level function for sending the response.
|
|
365
|
-
*
|
|
366
|
-
* @name _sendResponse
|
|
367
|
-
* @function
|
|
368
|
-
* @param {Object} resData An object containing the following fields:
|
|
369
|
-
*
|
|
370
|
-
* - `content` (Anything): The data to send to client.
|
|
371
|
-
* - `status` (Number): The response status.
|
|
372
|
-
* - `headers` (Object): The headers object.
|
|
373
|
-
* - `contentType` (String): The content type.
|
|
374
|
-
*
|
|
375
|
-
*/
|
|
376
|
-
|
|
377
|
-
}, {
|
|
378
|
-
key: "_sendResponse",
|
|
379
|
-
value: function _sendResponse(resData) {
|
|
380
|
-
|
|
381
|
-
if (this.res.headersSent) {
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
if (resData.content && resData.content.statusCode) {
|
|
386
|
-
resData.status = resData.content.statusCode;
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
var bRes = toBuffer(resData.content);
|
|
390
|
-
|
|
391
|
-
this.res.status(resData.status);
|
|
392
|
-
this.header(resData.headers);
|
|
393
|
-
|
|
394
|
-
this.res.type(resData.contentType || bRes.contentType);
|
|
395
|
-
this.res.end(bRes.buffer);
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/**
|
|
399
|
-
* cookie
|
|
400
|
-
* Sets, gets or deletes the cookie.
|
|
401
|
-
*
|
|
402
|
-
* @name cookie
|
|
403
|
-
* @function
|
|
404
|
-
* @param {String} cookie The searched cookie.
|
|
405
|
-
* @param {String} value If provided and it not `null`, the cookie will be set. If it's null, the cookie will be deleted. If `value` is not provided, the cookie value will be returned.
|
|
406
|
-
* @return {String|null|undefined} `null`, if the cookie was deleted. `undefined` if the cookie doesn't exist. The cookie value if this exists.
|
|
407
|
-
*/
|
|
408
|
-
|
|
409
|
-
}, {
|
|
410
|
-
key: "cookie",
|
|
411
|
-
value: function cookie(name, value, options) {
|
|
412
|
-
if (value === undefined) {
|
|
413
|
-
return this.cookies[name];
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
if (value === null) {
|
|
417
|
-
this.res.clearCookie(name);
|
|
418
|
-
} else {
|
|
419
|
-
this.res.cookie(name, value, options);
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
return this;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* file
|
|
427
|
-
* Serves a file to the response.
|
|
428
|
-
*
|
|
429
|
-
* @name file
|
|
430
|
-
* @function
|
|
431
|
-
* @param {String} path Relative path to the file.
|
|
432
|
-
* @param {String} customRoot Absolute path to the root directory (optional).
|
|
433
|
-
*/
|
|
434
|
-
|
|
435
|
-
}, {
|
|
436
|
-
key: "file",
|
|
437
|
-
value: function file(filePath, customRoot) {
|
|
438
|
-
if (!customRoot) {
|
|
439
|
-
customRoot = this.server.options.public[0][1];
|
|
440
|
-
}
|
|
441
|
-
filePath = path.resolve(customRoot, filePath);
|
|
442
|
-
this.res.sendFile(filePath);
|
|
443
|
-
}
|
|
444
|
-
}]);
|
|
445
|
-
|
|
446
|
-
return LienObj;
|
|
447
|
-
}();
|
|
448
|
-
|
|
449
|
-
var RESPONSE_METHODS = ["append", "attachment", "download", "json", "jsonp", "links", "vary", "status"];
|
|
450
|
-
RESPONSE_METHODS.forEach(function (method) {
|
|
451
|
-
LienObj.prototype[method] = function () {
|
|
452
|
-
this.res[method].apply(this.res, arguments);
|
|
453
|
-
return this;
|
|
454
|
-
};
|
|
455
|
-
});
|
|
1
|
+
import Lien from "./server.js";
|
|
2
|
+
import Hooks from "./hooks.js";
|
|
3
|
+
import LienObj from "./lien.js";
|
|
456
4
|
|
|
457
5
|
/**
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
*
|
|
461
|
-
* It extends the `EventEmitter` class.
|
|
462
|
-
*
|
|
463
|
-
* It emits the following events:
|
|
464
|
-
*
|
|
465
|
-
* - `load` (err): After the server is started. If there are no errors, the `err` will be null.
|
|
466
|
-
* - `serverError` (err, req, res): This is emitted when something goes wrong after the server is started.
|
|
467
|
-
* - `error` (err): Errors which may appear during the server initialization.
|
|
468
|
-
*
|
|
469
|
-
* @name Lien
|
|
470
|
-
* @function
|
|
471
|
-
* @param {Object} opt_options An object containing the following properties:
|
|
472
|
-
*
|
|
473
|
-
* - `host` (String): The server host.
|
|
474
|
-
* - `port` (Integer): The server port. `process.env.PORT` is used, default is `3000`.
|
|
475
|
-
* - `session` (Boolean|Object): Enable the session support. If it's an object, it will be merged with the following defaults and passed to [`express-session`](https://github.com/expressjs/session):
|
|
476
|
-
* - `secret` (String): This is the secret used to sign the session ID cookie (default: "lien server").
|
|
477
|
-
* - `resave` (Boolean): Forces the session to be saved back to the session store, even if the session was never modified during the request (default: false).
|
|
478
|
-
* - `saveUninitialized` (Boolean): Forces a session that is "uninitialized" to be saved to the store (default: `true`).
|
|
479
|
-
* - `cookie` (Object): The cookie [options](https://github.com/expressjs/cookie-parser).
|
|
480
|
-
* - `storeOptions` (Object): The session store options. These options are passed to the session store you choose.
|
|
481
|
-
* - `store`: (String|Function): The session store name or function. By default it's using a memory store if the session is enabled.
|
|
482
|
-
* - `public` (String|Array): The path to the public directory or an array of arrays in this format: `["/url/of/static/dir", "path/to/static/dir"]`.
|
|
483
|
-
*
|
|
484
|
-
* Example:
|
|
485
|
-
*
|
|
486
|
-
* ```js
|
|
487
|
-
* [
|
|
488
|
-
* ["/images", "path/to/images"]
|
|
489
|
-
* , ["/", "path/to/public"]
|
|
490
|
-
* ]
|
|
491
|
-
* ```
|
|
492
|
-
*
|
|
493
|
-
* - `ssl` (Object): An object containing the following fields:
|
|
494
|
-
* - `key` (String): The path to the key file.
|
|
495
|
-
* - `cert` (String): The path to the cert file.
|
|
496
|
-
* - `_key` (String|Buffer): The key file content (defaults to the key file content).
|
|
497
|
-
* - `_cert` (String|Buffer): The cert file content (defaults to the cert file content).
|
|
498
|
-
*
|
|
499
|
-
* - `views`
|
|
500
|
-
* - `path` (String): The path to the views directory.
|
|
501
|
-
* - `name` (String): The view engine name.
|
|
502
|
-
*
|
|
503
|
-
* - `errorPages` (Object):
|
|
504
|
-
* - `notFound` (String|Function): The path to a custom 404 page or a function receiving the lien object as parameter. This can be used to serve custom 404 pages.
|
|
505
|
-
* - `serverError` (String|Function): The path to a custom 500 page or a function receiving the lien object as parameter. This can be used to serve custom 500 pages.
|
|
506
|
-
* - `badCsrf` (String|Function): The path to a custom bad CSRF page or a function receiving the lien object as parameter. This can be used to serve custom bad CSRF errors.
|
|
507
|
-
*
|
|
508
|
-
* - `logErrors` (Boolean): Log the server errors (default: `true`).
|
|
509
|
-
* - `csrf` (Object): The CSRF options. These are passed to [`csurf`](https://github.com/expressjs/csurf)
|
|
510
|
-
* - `bodyParserJson` (Boolean): Whether to enable the JSON body parser (default: `true`)
|
|
511
|
-
* - `bodyParserUrlEncoded` (Boolean): Whether to enable the URL encoded body parser (default: `true`)
|
|
512
|
-
* - `cookieParser` (Boolean): Whether to enable the cookie parser (default: `true`)
|
|
513
|
-
* - `transformers` (Boolean): Whether to enable the transformers (default: `true`)
|
|
514
|
-
*
|
|
515
|
-
* @return {Object} The Lien instance.
|
|
6
|
+
* The constructor for reusable hook pipelines.
|
|
7
|
+
* @type {typeof Hooks}
|
|
516
8
|
*/
|
|
9
|
+
Lien.Hooks = Hooks;
|
|
10
|
+
/**
|
|
11
|
+
* The request object constructor, also available as a named export.
|
|
12
|
+
* @type {typeof LienObj}
|
|
13
|
+
*/
|
|
14
|
+
Lien.LienObj = LienObj;
|
|
517
15
|
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
function Lien(options) {
|
|
522
|
-
var _this3$server;
|
|
523
|
-
|
|
524
|
-
_classCallCheck(this, Lien);
|
|
525
|
-
|
|
526
|
-
var _this3 = _possibleConstructorReturn(this, (Lien.__proto__ || Object.getPrototypeOf(Lien)).call(this));
|
|
527
|
-
|
|
528
|
-
options = ul.merge(options, {
|
|
529
|
-
public: [],
|
|
530
|
-
session: false,
|
|
531
|
-
errorPages: {},
|
|
532
|
-
logErrors: true,
|
|
533
|
-
csrf: null,
|
|
534
|
-
port: process.env.PORT || 3000,
|
|
535
|
-
cookieSecret: "lien",
|
|
536
|
-
cookieOptions: {},
|
|
537
|
-
bodyParserJson: true,
|
|
538
|
-
bodyParserUrlEncoded: true,
|
|
539
|
-
cookieParser: true,
|
|
540
|
-
transformers: true
|
|
541
|
-
});
|
|
542
|
-
|
|
543
|
-
if (options.csrf === true) {
|
|
544
|
-
options.csrf = {
|
|
545
|
-
cookie: true
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
_this3.csrf = options.csrf ? csrf(options.csrf) : null;
|
|
550
|
-
_this3.express = express;
|
|
551
|
-
_this3.router = express.Router();
|
|
552
|
-
_this3.beforeRequest = express.Router();
|
|
553
|
-
_this3.staticRouter = express.Router();
|
|
554
|
-
|
|
555
|
-
_this3.transformers = {
|
|
556
|
-
before: {},
|
|
557
|
-
after: {}
|
|
558
|
-
};
|
|
559
|
-
|
|
560
|
-
var app = _this3.app = express();
|
|
561
|
-
|
|
562
|
-
if (options.views) {
|
|
563
|
-
app.set("views", options.views.path);
|
|
564
|
-
app.set("view engine", options.views.name);
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
options.bodyParserJson && app.use(bodyParser.json());
|
|
568
|
-
options.bodyParserUrlEncoded && app.use(bodyParser.urlencoded({ extended: true }));
|
|
569
|
-
|
|
570
|
-
app.disable("x-powered-by");
|
|
571
|
-
|
|
572
|
-
_this3.SessionStore = null;
|
|
573
|
-
_this3.SessionStoreModule = null;
|
|
574
|
-
|
|
575
|
-
options.cookieParser && app.use(cookieParser(options.cookieSecret, options.cookieOptions));
|
|
576
|
-
|
|
577
|
-
if (options.session) {
|
|
578
|
-
options.session = ul.merge(options.session, {
|
|
579
|
-
resave: false,
|
|
580
|
-
saveUninitialized: true,
|
|
581
|
-
store: null,
|
|
582
|
-
storeOptions: {}
|
|
583
|
-
});
|
|
584
|
-
options.session.secret = options.cookieSecret;
|
|
585
|
-
|
|
586
|
-
// Store name provided
|
|
587
|
-
if (typeof options.session.store === "string") {
|
|
588
|
-
try {
|
|
589
|
-
_this3.SessionStoreModule = require(options.session.store);
|
|
590
|
-
if (_this3.SessionStoreModule.__esModule && _this3.SessionStoreModule.default) {
|
|
591
|
-
_this3.SessionStoreModule = _this3.SessionStoreModule.default;
|
|
592
|
-
}
|
|
593
|
-
} catch (e) {
|
|
594
|
-
if (e.code === "MODULE_NOT_FOUND") {
|
|
595
|
-
_this3.emit("error", new Error("The " + options.session.store + " should be installed in your app to use it as session store."));
|
|
596
|
-
} else {
|
|
597
|
-
_this3.emit("error", e);
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
} else if (typeof options.session.store === "function") {
|
|
601
|
-
_this3.SessionStoreModule = options.session.store;
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
if (!_this3.SessionStoreModule && options.session.storeOptions) {
|
|
605
|
-
console.warn("Received store options, but no store was specified.");
|
|
606
|
-
}
|
|
607
|
-
|
|
608
|
-
if (_this3.SessionStoreModule && options.session.storeOptions) {
|
|
609
|
-
_this3.SessionStore = _this3.SessionStoreModule(session);
|
|
610
|
-
options.session.store = new _this3.SessionStore(options.session.storeOptions);
|
|
611
|
-
} else if (!_this3.SessionStoreModule && _this3.app.get("env") === "production") {
|
|
612
|
-
console.warn("You are using a memory session store in production. This may cause memory leaks. Please consider using a database store.");
|
|
613
|
-
}
|
|
614
|
-
|
|
615
|
-
_this3.session = session(options.session);
|
|
616
|
-
_this3.app.use(_this3.session);
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
// Handle ssl
|
|
620
|
-
if (options.ssl) {
|
|
621
|
-
options.ssl._key = options.ssl._key || fs.readFileSync(options.ssl.key);
|
|
622
|
-
options.ssl._cert = options.ssl._cert || fs.readFileSync(options.ssl.cert);
|
|
623
|
-
_this3.server = https.createServer({
|
|
624
|
-
key: options.ssl._key,
|
|
625
|
-
cert: options.ssl._cert
|
|
626
|
-
}, _this3.app);
|
|
627
|
-
} else {
|
|
628
|
-
_this3.server = http.createServer(_this3.app);
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
if (!Array.isArray(options.public)) {
|
|
632
|
-
options.public = [["/", options.public]];
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
_this3.options = options;
|
|
636
|
-
|
|
637
|
-
app.use(_this3.beforeRequest);
|
|
638
|
-
app.use(_this3.staticRouter);
|
|
639
|
-
app.use(_this3.router);
|
|
640
|
-
|
|
641
|
-
// Public directorie
|
|
642
|
-
options.public.forEach(function (c) {
|
|
643
|
-
_this3.addStaticPath(c[0], c[1]);
|
|
644
|
-
});
|
|
645
|
-
|
|
646
|
-
var listenArgs = [options.port];
|
|
647
|
-
|
|
648
|
-
if (options.host) {
|
|
649
|
-
listenArgs.push(options.host);
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
listenArgs.push(function (err) {
|
|
653
|
-
_this3.emit("load", err);
|
|
654
|
-
});
|
|
655
|
-
|
|
656
|
-
["all"].concat(httpMethods).forEach(function (method) {
|
|
657
|
-
if (_this3[method]) {
|
|
658
|
-
return;
|
|
659
|
-
}
|
|
660
|
-
_this3[method] = function (url, output) {
|
|
661
|
-
_this3.addPage(url, method, output);
|
|
662
|
-
};
|
|
663
|
-
});
|
|
664
|
-
|
|
665
|
-
// Start listening on host:port
|
|
666
|
-
(_this3$server = _this3.server).listen.apply(_this3$server, listenArgs);
|
|
667
|
-
|
|
668
|
-
if (options.errorPages) {
|
|
669
|
-
process.nextTick(function () {
|
|
670
|
-
_this3.errorPages(options.errorPages);
|
|
671
|
-
});
|
|
672
|
-
}
|
|
673
|
-
return _this3;
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
/**
|
|
677
|
-
* addStaticPath
|
|
678
|
-
* Adds a new static path to the server.
|
|
679
|
-
*
|
|
680
|
-
* @name addStaticPath
|
|
681
|
-
* @function
|
|
682
|
-
* @static
|
|
683
|
-
* @param {String} url The static path url endpoint.
|
|
684
|
-
* @param {String} localPath The local path to the directory.
|
|
685
|
-
*/
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
_createClass(Lien, [{
|
|
689
|
-
key: "addStaticPath",
|
|
690
|
-
value: function addStaticPath(url, localPath) {
|
|
691
|
-
this.staticRouter.use(url, express.static(localPath));
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
/*!
|
|
695
|
-
* _handleRoute
|
|
696
|
-
* Calls a function or serves a static file.
|
|
697
|
-
*
|
|
698
|
-
* @name _handleRoute
|
|
699
|
-
* @function
|
|
700
|
-
* @param {Request} req The request object.
|
|
701
|
-
* @param {Response} res The response object.
|
|
702
|
-
* @param {String|Function} output The path to a public file or a function.
|
|
703
|
-
* @param {Array} args An array of custom arguments to be used in the function call.
|
|
704
|
-
*/
|
|
705
|
-
|
|
706
|
-
}, {
|
|
707
|
-
key: "_handleRoute",
|
|
708
|
-
value: function _handleRoute(req, res, output, args, next) {
|
|
709
|
-
var lien = new LienObj(req, res, next || noop, this);
|
|
710
|
-
|
|
711
|
-
if (typeof output === "string") {
|
|
712
|
-
return lien.file(output);
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
if (Array.isArray(args)) {
|
|
716
|
-
args.unshift(lien);
|
|
717
|
-
} else {
|
|
718
|
-
args = [lien];
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
output.apply(this, args);
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
/*!
|
|
725
|
-
* _handleLienRoute
|
|
726
|
-
*
|
|
727
|
-
* @name _handleLienRoute
|
|
728
|
-
* @function
|
|
729
|
-
* @param {Lien} lien The Lien instance.
|
|
730
|
-
* @param {Function} output The route handler.
|
|
731
|
-
*/
|
|
732
|
-
|
|
733
|
-
}, {
|
|
734
|
-
key: "_handleLienRoute",
|
|
735
|
-
value: function _handleLienRoute(lien, output) {
|
|
736
|
-
if (typeof output === "string") {
|
|
737
|
-
return lien.file(output);
|
|
738
|
-
}
|
|
739
|
-
output.apply(this, [lien]);
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
/**
|
|
743
|
-
* addPage
|
|
744
|
-
* Adds a new page to be handled.
|
|
745
|
-
*
|
|
746
|
-
* @name addPage
|
|
747
|
-
* @function
|
|
748
|
-
* @param {String} url The page url.
|
|
749
|
-
* @param {String|Object} method The request methods to be handled (default: `"all"`) or an object:
|
|
750
|
-
*
|
|
751
|
-
* - `method` (String): The HTTP method.
|
|
752
|
-
* - `before` (Array|Function): A function or an array of middleware functions to be executed *before* the main function.
|
|
753
|
-
* - `after` (Array|Function): A function or an array of middleware functions to be executed *after* the main function.
|
|
754
|
-
*
|
|
755
|
-
* @param {Function} output A function receiving the `lien` object as parameter. If can be a path serving a public file.
|
|
756
|
-
*/
|
|
757
|
-
|
|
758
|
-
}, {
|
|
759
|
-
key: "addPage",
|
|
760
|
-
value: function addPage(url, method, output) {
|
|
761
|
-
var _this4 = this;
|
|
762
|
-
|
|
763
|
-
if (output === undefined) {
|
|
764
|
-
output = method;
|
|
765
|
-
method = "all";
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
var router = typeof url === "string" && ~url.indexOf(":") ? this.router : this.beforeRequest;
|
|
769
|
-
var before = [this.csrf];
|
|
770
|
-
var after = [];
|
|
771
|
-
if (method && method.method) {
|
|
772
|
-
before = method.before || [];
|
|
773
|
-
if (!Array.isArray(before)) {
|
|
774
|
-
before = [before];
|
|
775
|
-
}
|
|
776
|
-
after = method.after || [];
|
|
777
|
-
if (!Array.isArray(after)) {
|
|
778
|
-
after = [after];
|
|
779
|
-
}
|
|
780
|
-
method = method.method;
|
|
781
|
-
}
|
|
782
|
-
var funcs = [].concat(before);
|
|
783
|
-
if (this.options.transformers) {
|
|
784
|
-
funcs.push(function (req, res, next) {
|
|
785
|
-
var trans = _this4.getHooks("before", req.path, req.method.toLowerCase());
|
|
786
|
-
if (trans) {
|
|
787
|
-
var lien = new LienObj(req, res, next, _this4);
|
|
788
|
-
trans.start(lien, function (err, data) {
|
|
789
|
-
if (err) {
|
|
790
|
-
return lien.end(err);
|
|
791
|
-
}
|
|
792
|
-
_this4._handleLienRoute(lien, output);
|
|
793
|
-
});
|
|
794
|
-
} else {
|
|
795
|
-
_this4._handleRoute(req, res, output, [], next);
|
|
796
|
-
}
|
|
797
|
-
});
|
|
798
|
-
} else {
|
|
799
|
-
funcs.push(function (req, res, next) {
|
|
800
|
-
_this4._handleRoute(req, res, output, [], next);
|
|
801
|
-
});
|
|
802
|
-
}
|
|
803
|
-
funcs = funcs.concat(after);
|
|
804
|
-
|
|
805
|
-
router[method].apply(router, [url].concat(funcs.filter(Boolean)));
|
|
806
|
-
}
|
|
807
|
-
|
|
808
|
-
/**
|
|
809
|
-
* errorPages
|
|
810
|
-
* Handle the error pages.
|
|
811
|
-
*
|
|
812
|
-
* @name errorPages
|
|
813
|
-
* @function
|
|
814
|
-
* @param {Object} options An object containing the following fields:
|
|
815
|
-
*
|
|
816
|
-
* - `notFound` (String|Function): The path to a custom 404 page or a function receiving the lien object as parameter. This can be used to serve custom 404 pages.
|
|
817
|
-
* - `serverError` (String|Function): The path to a custom 500 page or a function receiving the lien object as parameter. This can be used to serve custom 500 pages.
|
|
818
|
-
*/
|
|
819
|
-
|
|
820
|
-
}, {
|
|
821
|
-
key: "errorPages",
|
|
822
|
-
value: function errorPages(options) {
|
|
823
|
-
var _this5 = this;
|
|
824
|
-
|
|
825
|
-
var sendResp = function sendResp(lien, msg, status) {
|
|
826
|
-
if (lien.accepts("html")) {
|
|
827
|
-
return lien.end(msg, status);
|
|
828
|
-
} else if (lien.accepts("json")) {
|
|
829
|
-
return lien.apiError(msg, status);
|
|
830
|
-
}
|
|
831
|
-
lien.end(msg, status, "txt");
|
|
832
|
-
};
|
|
833
|
-
options = ul.merge(options, {
|
|
834
|
-
notFound: function notFound(lien) {
|
|
835
|
-
return sendResp(lien, "404 — Not found.", 404);
|
|
836
|
-
},
|
|
837
|
-
serverError: function serverError(lien) {
|
|
838
|
-
return sendResp(lien, "500 — Internal Server Error", 500);
|
|
839
|
-
},
|
|
840
|
-
badCsrf: function badCsrf(lien) {
|
|
841
|
-
return sendResp(lien, "What‽ Your browser did something unexpected.", 422);
|
|
842
|
-
}
|
|
843
|
-
});
|
|
844
|
-
this.app.use(function (req, res, next) {
|
|
845
|
-
_this5._handleRoute(req, res, options.notFound, [], next);
|
|
846
|
-
});
|
|
847
|
-
this.app.use(function (err, req, res, next) {
|
|
848
|
-
if (err.code === "ENOENT" || err.statusCode === 404) {
|
|
849
|
-
return _this5._handleRoute(req, res, options.notFound, [err], next);
|
|
850
|
-
}
|
|
851
|
-
if (err.code === "EBADCSRFTOKEN") {
|
|
852
|
-
return _this5._handleRoute(req, res, options.badCsrf, [err], next);
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
_this5.emit("serverError", err, req, res);
|
|
856
|
-
if (_this5.options.logErrors) {
|
|
857
|
-
console.error(err.stack);
|
|
858
|
-
}
|
|
859
|
-
_this5._handleRoute(req, res, options.serverError, [err], next);
|
|
860
|
-
});
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
/**
|
|
864
|
-
* getHooks
|
|
865
|
-
* Gets the transformer for a url.
|
|
866
|
-
*
|
|
867
|
-
* @name getHooks
|
|
868
|
-
* @function
|
|
869
|
-
* @param {String} type The hook type (`before` or `after`).
|
|
870
|
-
* @param {String} url The url.
|
|
871
|
-
* @param {String} method The method.
|
|
872
|
-
* @returns {Transformer|null} The transformer (if it exists) or `null`.
|
|
873
|
-
*/
|
|
874
|
-
|
|
875
|
-
}, {
|
|
876
|
-
key: "getHooks",
|
|
877
|
-
value: function getHooks(type, url, method) {
|
|
878
|
-
var typ = this.transformers[type] = this.transformers[type] || {},
|
|
879
|
-
all = setOrGet(typ, method, []),
|
|
880
|
-
hooks = [];
|
|
881
|
-
|
|
882
|
-
for (var i = 0, c; i < all.length; ++i) {
|
|
883
|
-
c = all[i];
|
|
884
|
-
if (c.re.test(url)) {
|
|
885
|
-
hooks.push(c.trans);
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
if (hooks.length) {
|
|
890
|
-
if (hooks.length === 1) {
|
|
891
|
-
return hooks[0];
|
|
892
|
-
}
|
|
893
|
-
return new Transformer({}, { autostart: false }).add(hooks, Transformer.PARALLEL);
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
return null;
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
/**
|
|
900
|
-
* getHooksStrict
|
|
901
|
-
* Similar to `getHooks`, but doesn't concat hooks based on the regex
|
|
902
|
-
* matching but only if they are the same regex.
|
|
903
|
-
*
|
|
904
|
-
* @name getHooksStrict
|
|
905
|
-
* @function
|
|
906
|
-
* @param {String} type The hook type (`before` or `after`).
|
|
907
|
-
* @param {String} url The url.
|
|
908
|
-
* @param {String} method The method.
|
|
909
|
-
* @returns {Transformer|null} The transformer (if it exists) or `null`.
|
|
910
|
-
*/
|
|
911
|
-
|
|
912
|
-
}, {
|
|
913
|
-
key: "getHooksStrict",
|
|
914
|
-
value: function getHooksStrict(type, url, method) {
|
|
915
|
-
if (url === "*") {
|
|
916
|
-
url = "(.*)";
|
|
917
|
-
}
|
|
918
|
-
var reUrl = pathToRegexp(url),
|
|
919
|
-
typ = this.transformers[type] = this.transformers[type] || {},
|
|
920
|
-
all = setOrGet(typ, method, []),
|
|
921
|
-
hooks = [];
|
|
922
|
-
|
|
923
|
-
for (var i = 0, c; i < all.length; ++i) {
|
|
924
|
-
c = all[i];
|
|
925
|
-
if (c.re.toString() === reUrl.toString()) {
|
|
926
|
-
hooks.push(c.trans);
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
if (hooks.length) {
|
|
931
|
-
if (hooks.length === 1) {
|
|
932
|
-
return hooks[0];
|
|
933
|
-
}
|
|
934
|
-
return new Transformer({}, { autostart: false }).add(hooks, Transformer.PARALLEL);
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
return null;
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
/**
|
|
941
|
-
* insertHook
|
|
942
|
-
* Inserts a new hook.
|
|
943
|
-
*
|
|
944
|
-
* @name insertHook
|
|
945
|
-
* @function
|
|
946
|
-
* @param {String} type The hook type (`before`, `after`, `custom:name`).
|
|
947
|
-
* @param {String} url The url.
|
|
948
|
-
* @param {String} method The method.
|
|
949
|
-
* @param {Transformer} trans The transformer to insert.
|
|
950
|
-
* @returns {Transformer} The inserted transformer.
|
|
951
|
-
*/
|
|
952
|
-
|
|
953
|
-
}, {
|
|
954
|
-
key: "insertHook",
|
|
955
|
-
value: function insertHook(type, url, method, trans) {
|
|
956
|
-
|
|
957
|
-
if (url === "*") {
|
|
958
|
-
url = "(.*)";
|
|
959
|
-
}
|
|
960
|
-
|
|
961
|
-
var all = setOrGet(this.transformers[type], method, []);
|
|
962
|
-
|
|
963
|
-
all.push({
|
|
964
|
-
re: pathToRegexp(url),
|
|
965
|
-
trans: trans
|
|
966
|
-
});
|
|
967
|
-
|
|
968
|
-
return trans;
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
/**
|
|
972
|
-
* hook
|
|
973
|
-
* Adds a new hook.
|
|
974
|
-
*
|
|
975
|
-
* @name hook
|
|
976
|
-
* @function
|
|
977
|
-
* @param {String} where The hook type (`before` or `after`).
|
|
978
|
-
* @param {String} url The route url.
|
|
979
|
-
* @param {String} method The HTTP method.
|
|
980
|
-
* @param {Function} cb The callback function.
|
|
981
|
-
* @param {Number} transType The transformer type.
|
|
982
|
-
*/
|
|
983
|
-
|
|
984
|
-
}, {
|
|
985
|
-
key: "hook",
|
|
986
|
-
value: function hook(where, url, method, cb, transType) {
|
|
987
|
-
var _this6 = this;
|
|
988
|
-
|
|
989
|
-
if (!this.options.transformers) {
|
|
990
|
-
console.warn("Transformers are disabled.");
|
|
991
|
-
}
|
|
992
|
-
|
|
993
|
-
if (typeof url === "string" && url.charAt(0) === ":") {
|
|
994
|
-
url = "/" + url;
|
|
995
|
-
}
|
|
996
|
-
if (typeof method === "function") {
|
|
997
|
-
cb = method;
|
|
998
|
-
method = "all";
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
if (method === "all") {
|
|
1002
|
-
httpMethods.forEach(function (c) {
|
|
1003
|
-
return _this6.hook(where, url, c, cb, transType);
|
|
1004
|
-
});
|
|
1005
|
-
return this;
|
|
1006
|
-
}
|
|
1007
|
-
|
|
1008
|
-
if (Array.isArray(url)) {
|
|
1009
|
-
url.forEach(function (u) {
|
|
1010
|
-
return _this6.hook(where, u, method, cb, transType);
|
|
1011
|
-
});
|
|
1012
|
-
return this;
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
var customName = where.split(":");
|
|
1016
|
-
|
|
1017
|
-
var trans = {};
|
|
1018
|
-
setOrGet(this.transformers, where, {});
|
|
1019
|
-
switch (customName[0]) {
|
|
1020
|
-
case "before":
|
|
1021
|
-
case "after":
|
|
1022
|
-
case "custom":
|
|
1023
|
-
trans = this.getHooksStrict(where, url, method) || this.insertHook(where, url, method, new Transformer({}, { autostart: false }));
|
|
1024
|
-
break;
|
|
1025
|
-
default:
|
|
1026
|
-
throw new Error("The hook type should be 'before', 'after' or 'custom'.");
|
|
1027
|
-
break;
|
|
1028
|
-
}
|
|
1029
|
-
|
|
1030
|
-
trans.add(cb, transType);
|
|
1031
|
-
return this;
|
|
1032
|
-
}
|
|
1033
|
-
|
|
1034
|
-
/**
|
|
1035
|
-
* before
|
|
1036
|
-
* Adds a before hook. It will handle all the subroutes of the `url`.
|
|
1037
|
-
*
|
|
1038
|
-
* @name before
|
|
1039
|
-
* @function
|
|
1040
|
-
* @param {String} url The route url.
|
|
1041
|
-
* @param {String} method The HTTP method.
|
|
1042
|
-
* @param {Function} cb The callback function.
|
|
1043
|
-
* @param {Number} transType The transformer type.
|
|
1044
|
-
*/
|
|
1045
|
-
|
|
1046
|
-
}, {
|
|
1047
|
-
key: "before",
|
|
1048
|
-
value: function before(url, method, cb, transType) {
|
|
1049
|
-
this.hook("before", url, method, cb, transType);
|
|
1050
|
-
}
|
|
1051
|
-
|
|
1052
|
-
/**
|
|
1053
|
-
* after
|
|
1054
|
-
* Adds a before hook. It will handle all the subroutes of the `url`.
|
|
1055
|
-
*
|
|
1056
|
-
* @name after
|
|
1057
|
-
* @function
|
|
1058
|
-
* @param {String} url The route url.
|
|
1059
|
-
* @param {String} method The HTTP method.
|
|
1060
|
-
* @param {Function} cb The callback function.
|
|
1061
|
-
* @param {Number} transType The transformer type.
|
|
1062
|
-
*/
|
|
1063
|
-
|
|
1064
|
-
}, {
|
|
1065
|
-
key: "after",
|
|
1066
|
-
value: function after(url, method, cb, transType) {
|
|
1067
|
-
this.hook("after", url, method, cb, transType);
|
|
1068
|
-
}
|
|
1069
|
-
|
|
1070
|
-
/**
|
|
1071
|
-
* use
|
|
1072
|
-
* Use this function to add middleware handlers.
|
|
1073
|
-
*
|
|
1074
|
-
* @name use
|
|
1075
|
-
* @function
|
|
1076
|
-
* @param {String} url The route url.
|
|
1077
|
-
* @param {String} method The HTTP method.
|
|
1078
|
-
* @param {Function} cb The callback function.
|
|
1079
|
-
* @param {Number} transType The transformer type.
|
|
1080
|
-
*/
|
|
1081
|
-
|
|
1082
|
-
}, {
|
|
1083
|
-
key: "use",
|
|
1084
|
-
value: function use(url, method, cb, transType) {
|
|
1085
|
-
if (url.charAt(url.length - 1) === "/") {
|
|
1086
|
-
url = url.slice(0, -1);
|
|
1087
|
-
}
|
|
1088
|
-
this.before([url, url + "/(.*)"], method, cb, transType);
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1091
|
-
/**
|
|
1092
|
-
* add
|
|
1093
|
-
* Adds a new middleware. **Note**: This will *not* trigger the hooks.
|
|
1094
|
-
*
|
|
1095
|
-
* @name add
|
|
1096
|
-
* @function
|
|
1097
|
-
* @param {String} url The endpoint url.
|
|
1098
|
-
* @param {String} method The HTTP method (default: `all`).
|
|
1099
|
-
* @param {Function} cb The callback function.
|
|
1100
|
-
*/
|
|
1101
|
-
|
|
1102
|
-
}, {
|
|
1103
|
-
key: "add",
|
|
1104
|
-
value: function add(url, method, cb) {
|
|
1105
|
-
var _this7 = this;
|
|
1106
|
-
|
|
1107
|
-
if (typeof method === "function") {
|
|
1108
|
-
cb = method;
|
|
1109
|
-
method = "all";
|
|
1110
|
-
}
|
|
1111
|
-
this.router[method].call(this.router, url, function (req, res, next) {
|
|
1112
|
-
var lien = new LienObj(req, res, next, _this7);
|
|
1113
|
-
cb(lien);
|
|
1114
|
-
});
|
|
1115
|
-
}
|
|
1116
|
-
}]);
|
|
1117
|
-
|
|
1118
|
-
return Lien;
|
|
1119
|
-
}(EventEmitter);
|
|
16
|
+
export { Lien, Hooks, LienObj };
|
|
17
|
+
export default Lien;
|
|
1120
18
|
|
|
1121
|
-
|
|
1122
|
-
module.exports
|
|
19
|
+
// Keep require("lien") returning the constructor.
|
|
20
|
+
export { Lien as "module.exports" };
|