Merge "LOG SQL dump files getting installed"
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / express / lib / router / layer.js
1 /**
2  * Module dependencies.
3  */
4
5 var pathRegexp = require('path-to-regexp');
6 var debug = require('debug')('express:router:layer');
7
8 /**
9  * Module variables.
10  */
11
12 var hasOwnProperty = Object.prototype.hasOwnProperty;
13
14 /**
15  * Expose `Layer`.
16  */
17
18 module.exports = Layer;
19
20 function Layer(path, options, fn) {
21   if (!(this instanceof Layer)) {
22     return new Layer(path, options, fn);
23   }
24
25   debug('new %s', path);
26   options = options || {};
27
28   this.handle = fn;
29   this.name = fn.name || '<anonymous>';
30   this.params = undefined;
31   this.path = undefined;
32   this.regexp = pathRegexp(path, this.keys = [], options);
33
34   if (path === '/' && options.end === false) {
35     this.regexp.fast_slash = true;
36   }
37 }
38
39 /**
40  * Handle the error for the layer.
41  *
42  * @param {Error} error
43  * @param {Request} req
44  * @param {Response} res
45  * @param {function} next
46  * @api private
47  */
48
49 Layer.prototype.handle_error = function handle_error(error, req, res, next) {
50   var fn = this.handle;
51
52   if (fn.length !== 4) {
53     // not a standard error handler
54     return next(error);
55   }
56
57   try {
58     fn(error, req, res, next);
59   } catch (err) {
60     next(err);
61   }
62 };
63
64 /**
65  * Handle the request for the layer.
66  *
67  * @param {Request} req
68  * @param {Response} res
69  * @param {function} next
70  * @api private
71  */
72
73 Layer.prototype.handle_request = function handle(req, res, next) {
74   var fn = this.handle;
75
76   if (fn.length > 3) {
77     // not a standard request handler
78     return next();
79   }
80
81   try {
82     fn(req, res, next);
83   } catch (err) {
84     next(err);
85   }
86 };
87
88 /**
89  * Check if this route matches `path`, if so
90  * populate `.params`.
91  *
92  * @param {String} path
93  * @return {Boolean}
94  * @api private
95  */
96
97 Layer.prototype.match = function match(path) {
98   if (path == null) {
99     // no path, nothing matches
100     this.params = undefined;
101     this.path = undefined;
102     return false;
103   }
104
105   if (this.regexp.fast_slash) {
106     // fast path non-ending match for / (everything matches)
107     this.params = {};
108     this.path = '';
109     return true;
110   }
111
112   var m = this.regexp.exec(path);
113
114   if (!m) {
115     this.params = undefined;
116     this.path = undefined;
117     return false;
118   }
119
120   // store values
121   this.params = {};
122   this.path = m[0];
123
124   var keys = this.keys;
125   var params = this.params;
126   var prop;
127   var n = 0;
128   var key;
129   var val;
130
131   for (var i = 1, len = m.length; i < len; ++i) {
132     key = keys[i - 1];
133     prop = key
134       ? key.name
135       : n++;
136     val = decode_param(m[i]);
137
138     if (val !== undefined || !(hasOwnProperty.call(params, prop))) {
139       params[prop] = val;
140     }
141   }
142
143   return true;
144 };
145
146 /**
147  * Decode param value.
148  *
149  * @param {string} val
150  * @return {string}
151  * @api private
152  */
153
154 function decode_param(val){
155   if (typeof val !== 'string') {
156     return val;
157   }
158
159   try {
160     return decodeURIComponent(val);
161   } catch (e) {
162     var err = new TypeError("Failed to decode param '" + val + "'");
163     err.status = 400;
164     throw err;
165   }
166 }