routup 0.11.0 → 0.13.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/README.md CHANGED
@@ -62,16 +62,17 @@ router.listen(3000);
62
62
  According to the fact that routup is a minimalistic framework, it depends on plugins to cover some
63
63
  typically http framework functions, which are not integrated in the main package.
64
64
 
65
- | Name | Description |
66
- |----------------------------------------------------------------|------------------------------------------------------------------------|
67
- | [body](https://www.npmjs.com/package/@routup/body) | Read and parse the request body. |
68
- | [cookie](https://www.npmjs.com/package/@routup/cookie) | Read and parse request cookies and serialize cookies for the response. |
69
- | [decorators](https://www.npmjs.com/package/@routup/decorators) | Create request handlers with class-, method- & parameter-decorators. |
70
- | [prometheus](https://www.npmjs.com/package/@routup/prometheus) | Collect and serve metrics for prometheus. |
71
- | [query](https://www.npmjs.com/package/@routup/query) | Read and parse the query string of the request url. |
72
- | [rate-limit](https://www.npmjs.com/package/@routup/rate-limit) | Rate limit incoming requests. |
73
- | [static](https://www.npmjs.com/package/@routup/static) | Serve static files from a directory. |
74
- | [swagger](https://www.npmjs.com/package/@routup/swagger) | Serve generated docs from URL or based on a JSON file. |
65
+ | Name | Description |
66
+ |----------------------------------------------------------------------------|------------------------------------------------------------------------|
67
+ | [body](https://www.npmjs.com/package/@routup/body) | Read and parse the request body. |
68
+ | [cookie](https://www.npmjs.com/package/@routup/cookie) | Read and parse request cookies and serialize cookies for the response. |
69
+ | [decorators](https://www.npmjs.com/package/@routup/decorators) | Create request handlers with class-, method- & parameter-decorators. |
70
+ | [prometheus](https://www.npmjs.com/package/@routup/prometheus) | Collect and serve metrics for prometheus. |
71
+ | [query](https://www.npmjs.com/package/@routup/query) | Read and parse the query string of the request url. |
72
+ | [rate-limit](https://www.npmjs.com/package/@routup/rate-limit) | Rate limit incoming requests. |
73
+ | [rate-limit-redis](https://www.npmjs.com/package/@routup/rate-limit-redis) | Redis adapter for the rate-limit plugin. |
74
+ | [static](https://www.npmjs.com/package/@routup/static) | Serve static files from a directory. |
75
+ | [swagger](https://www.npmjs.com/package/@routup/swagger) | Serve generated docs from URL or based on a JSON file. |
75
76
 
76
77
  ## License
77
78
 
@@ -1,2 +1,2 @@
1
- export * from './utils';
1
+ export * from './utils';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1,3 +1,3 @@
1
- import { Next, Response } from '@routup/core';
2
- export declare function processHandlerExecutionOutput(res: Response, next: Next, output?: unknown): void;
1
+ import { Next, Response } from '@routup/core';
2
+ export declare function processHandlerExecutionOutput(res: Response, next: Next, output?: unknown): void;
3
3
  //# sourceMappingURL=utils.d.ts.map
package/dist/index.cjs CHANGED
@@ -1,2 +1,580 @@
1
- "use strict";var a=require("@routup/core"),f=require("smob"),l=require("@ebec/http"),k=require("path-to-regexp"),D=require("http");function g(r,t){return typeof r=="object"&&r!==null&&r["@instanceof"]===Symbol.for(t)}function b(r){return typeof r=="string"||r instanceof RegExp}function v(r){return f.isObject(r)&&(r instanceof Promise||typeof r.then=="function")}function T(r,t,e){const s=setTimeout(()=>{r.statusCode=l.GatewayTimeoutErrorOptions.statusCode,r.statusMessage=l.GatewayTimeoutErrorOptions.message,r.end()},t);r.once("close",()=>{clearTimeout(s),typeof e=="function"&&e()}),r.once("error",h=>{clearTimeout(s),typeof e=="function"&&e(h)})}function I(r,t,e){if(v(e)){e.then(s=>(typeof s<"u"&&a.send(r,s),s)).catch(t);return}typeof e<"u"&&a.send(r,e)}function S(r){return typeof r!="string"||r.length===0?r:decodeURIComponent(r)}class M{constructor(t,e){this.regexpKeys=[],this.path=t,this.regexpOptions=e||{},t instanceof RegExp?this.regexp=t:this.regexp=k.pathToRegexp(t,this.regexpKeys,e)}test(t){return this.path==="/"&&this.regexpOptions.end===!1?!0:this.regexp.test(t)}exec(t){let e=null;if(this.path==="/"&&this.regexpOptions.end===!1)return{path:"/",params:{}};if(e=this.regexp.exec(t),!e)return;if(this.path instanceof RegExp)return{path:t,params:{0:S(e[0])}};const s={};for(let h=1;h<e.length;h++){const p=this.regexpKeys[h-1].name,i=S(e[h]);typeof i<"u"&&(s[p]=i)}return{path:e[0],params:s}}}class E{constructor(t,e){this["@instanceof"]=Symbol.for("Layer"),this.pathMatcher=new M(t.path,t.pathMatcher),this.fn=e}isError(){return this.fn.length===4}dispatch(t,e,s,h,p){if(a.setRequestParams(t,s.params||{}),a.setRequestMountPath(t,s.mountPath||"/"),typeof p<"u"){if(this.fn.length===4){try{this.fn(p,t,e,h)}catch(i){i instanceof Error?h(i):h(new l.BadRequestError({message:"The request could not be processed by the error handler."}))}return}h(p);return}if(this.fn.length>3){h();return}try{const i=this.fn(t,e,h);I(e,h,i)}catch(i){i instanceof Error?h(i):h(new l.BadRequestError({message:"The request could not be processed by the handler."}))}}matchPath(t){return this.pathMatcher.test(t)}exec(t){return this.pathMatcher.exec(t)}}function O(r){return g(r,"Layer")}var $=Object.defineProperty,L=Object.getOwnPropertySymbols,G=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable,q=(r,t,e)=>t in r?$(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,w=(r,t)=>{for(var e in t||(t={}))G.call(t,e)&&q(r,e,t[e]);if(L)for(var e of L(t))N.call(t,e)&&q(r,e,t[e]);return r};class j{constructor(t){this["@instanceof"]=Symbol.for("Route"),this.layers={},this.path=t.path,this.pathMatcherOptions=w({end:!0,strict:this.isStrictPath()},t.pathMatcher),this.pathMatcher=new M(this.path,this.pathMatcherOptions)}matchPath(t){return this.pathMatcher.test(t)}matchMethod(t){let e=t.toLowerCase();return e===a.Method.HEAD&&!f.hasOwnProperty(this.layers,e)&&(e=a.Method.GET),Object.prototype.hasOwnProperty.call(this.layers,e)}getMethods(){const t=Object.keys(this.layers);return f.hasOwnProperty(this.layers,a.Method.GET)&&!f.hasOwnProperty(this.layers,a.Method.HEAD)&&t.push(a.Method.HEAD),t}dispatch(t,e,s,h){if(!t.method){h();return}let p=t.method.toLowerCase();p===a.Method.HEAD&&!f.hasOwnProperty(this.layers,p)&&(p=a.Method.GET);const i=this.layers[p];if(typeof i>"u"||i.length===0||typeof s.path>"u"){h();return}const y=w({},s),c=this.pathMatcher.exec(s.path);c&&(y.params=f.merge({},s.params||{},c.params));let d=-1;const o=n=>{if(d++,d>=i.length){setImmediate(h,n);return}const u=i[d];if(n&&!u.isError()){o(n);return}u.dispatch(t,e,w({},y),o)};o()}register(t,...e){this.layers[t]=[];for(let s=0;s<e.length;s++){const h=new E({path:this.path,pathMatcher:this.pathMatcherOptions},e[s]);this.layers[t].push(h)}}get(...t){return this.register(a.Method.GET,...t)}post(...t){return this.register(a.Method.POST,...t)}put(...t){return this.register(a.Method.PUT,...t)}patch(...t){return this.register(a.Method.PATCH,...t)}delete(...t){return this.register(a.Method.DELETE,...t)}head(...t){return this.register(a.Method.HEAD,...t)}options(...t){return this.register(a.Method.OPTIONS,...t)}isStrictPath(){return typeof this.path!="string"||this.path!=="/"&&this.path.length!==0}}function R(r){return g(r,"Route")}var B=Object.defineProperty,C=Object.getOwnPropertySymbols,K=Object.prototype.hasOwnProperty,U=Object.prototype.propertyIsEnumerable,H=(r,t,e)=>t in r?B(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,A=(r,t)=>{for(var e in t||(t={}))K.call(t,e)&&H(r,e,t[e]);if(C)for(var e of C(t))U.call(t,e)&&H(r,e,t[e]);return r};function x(r){return g(r,"Router")}class F{constructor(t){this["@instanceof"]=Symbol.for("Router"),this.stack=[],t=t||{},this.pathMatcherOptions=A({end:!1,sensitive:!0},t.pathMatcher||{}),this.timeout=t.timeout,this.setPath(t.path||"/")}setPathMatcherOptions(t){this.pathMatcherOptions=t,this.pathMatcher&&(this.pathMatcher.regexpOptions=this.pathMatcherOptions)}setPath(t){if(t==="/"||!b(t)){this.path="/";return}typeof t=="string"?this.path=a.withLeadingSlash(a.withoutTrailingSlash(`${t}`)):this.path=t,this.pathMatcher=new M(this.path,this.pathMatcherOptions)}createListener(){return this.isRoot=!0,(t,e)=>{this.dispatch(t,e)}}listen(t){return D.createServer(this.createListener()).listen(t)}matchPath(t){return this.pathMatcher?this.pathMatcher.test(t):!0}dispatch(t,e,s,h){let p=-1;s=s||{};let i=[];this.isRoot&&typeof this.timeout=="number"&&T(e,this.timeout,h);const y=o=>{if(!this.isRoot){typeof h<"u"&&setImmediate(()=>h(o));return}if(typeof o<"u"){e.statusCode=l.BadRequestErrorOptions.statusCode,e.statusMessage=l.BadRequestErrorOptions.message,e.end();return}if(t.method&&t.method.toLowerCase()===a.Method.OPTIONS){const n=i.map(u=>u.toUpperCase()).join(",");e.setHeader(a.HeaderName.ALLOW,n),a.send(e,n);return}e.statusCode=l.NotFoundErrorOptions.statusCode,e.end()};let c=s.path||a.useRequestPath(t);if(this.pathMatcher){const o=this.pathMatcher.exec(c);typeof o<"u"&&(s.mountPath=a.cleanDoubleSlashes(`${s.mountPath||""}/${o.path}`),c===o.path?c="/":c=a.withLeadingSlash(c.substring(o.path.length)),s.params=f.merge(s.params||{},o.params))}s.path=c,s.mountPath||(s.mountPath="/");const d=o=>{if(p>=this.stack.length){setImmediate(y,o);return}let n,u=!1;for(;!u&&p<this.stack.length;){if(p++,n=this.stack[p],O(n)){if(!n.isError()&&o)continue;u=n.matchPath(c)}x(n)&&(u=n.matchPath(c)),R(n)&&(u=n.matchPath(c),t.method&&!n.matchMethod(t.method)&&(u=!1,t.method.toLowerCase()===a.Method.OPTIONS&&(i=f.mergeArrays(i,n.getMethods(),!0))))}if(!u||!n){setImmediate(y,o);return}const m=A({},s);if(O(n)){const P=n.exec(c);P&&(m.params=f.merge(P.params,m.params||{}),m.mountPath=a.cleanDoubleSlashes(`${m.mountPath||""}/${P.path}`))}if(o){if(O(n)&&n.isError()){n.dispatch(t,e,m,d,o);return}setImmediate(d,o);return}n.dispatch(t,e,m,d)};d()}dispatchAsync(t,e){return new Promise((s,h)=>{this.dispatch(t,e,{},p=>{if(p){h(p);return}s()})})}route(t){typeof t=="string"&&t.length>0&&(t=a.withLeadingSlash(t));const e=this.stack.findIndex(h=>R(h)&&h.path===t);if(e!==-1)return this.stack[e];const s=new j({path:t,pathMatcher:{sensitive:this.pathMatcherOptions.sensitive}});return this.stack.push(s),s}delete(t,...e){return this.route(t).delete(...e),this}get(t,...e){return this.route(t).get(...e),this}post(t,...e){return this.route(t).post(...e),this}put(t,...e){return this.route(t).put(...e),this}patch(t,...e){return this.route(t).patch(...e),this}head(t,...e){return this.route(t).head(...e),this}options(t,...e){return this.route(t).options(...e),this}use(...t){if(t.length===0)return this;let e;b(t[0])&&(e=t.shift());for(let s=0;s<t.length;s++){const h=t[s];if(x(h)){e&&h.setPath(e),h.setPathMatcherOptions(this.pathMatcherOptions),this.stack.push(h);continue}typeof h=="function"&&this.stack.push(new E({path:e||"/",pathMatcher:{strict:!1,end:!1,sensitive:this.pathMatcherOptions.sensitive}},h))}return this}}exports.Layer=E,exports.PathMatcher=M,exports.Route=j,exports.Router=F,exports.createRequestTimeout=T,exports.isInstance=g,exports.isLayerInstance=O,exports.isPath=b,exports.isPromise=v,exports.isRouteInstance=R,exports.isRouterInstance=x,exports.processHandlerExecutionOutput=I,Object.keys(a).forEach(function(r){r!=="default"&&!exports.hasOwnProperty(r)&&Object.defineProperty(exports,r,{enumerable:!0,get:function(){return a[r]}})});
1
+ 'use strict';
2
+
3
+ var core = require('@routup/core');
4
+ var smob = require('smob');
5
+ var http = require('@ebec/http');
6
+ var pathToRegexp = require('path-to-regexp');
7
+ var node_http = require('node:http');
8
+
9
+ /*
10
+ * Copyright (c) 2022-2022.
11
+ * Author Peter Placzek (tada5hi)
12
+ * For the full copyright and license information,
13
+ * view the LICENSE file that was distributed with this source code.
14
+ */ function isInstance(input, name) {
15
+ return typeof input === 'object' && input !== null && input['@instanceof'] === Symbol.for(name);
16
+ }
17
+
18
+ /*
19
+ * Copyright (c) 2022.
20
+ * Author Peter Placzek (tada5hi)
21
+ * For the full copyright and license information,
22
+ * view the LICENSE file that was distributed with this source code.
23
+ */ function isPath(input) {
24
+ return typeof input === 'string' || input instanceof RegExp;
25
+ }
26
+
27
+ function isPromise(p) {
28
+ return smob.isObject(p) && (p instanceof Promise || // eslint-disable-next-line @typescript-eslint/ban-ts-comment
29
+ // @ts-ignore
30
+ typeof p.then === 'function');
31
+ }
32
+
33
+ /* istanbul ignore next */ function createRequestTimeout(res, timeout, done) {
34
+ const instance = setTimeout(()=>{
35
+ res.statusCode = http.GatewayTimeoutErrorOptions.statusCode;
36
+ res.statusMessage = http.GatewayTimeoutErrorOptions.message;
37
+ res.end();
38
+ }, timeout);
39
+ res.once('close', ()=>{
40
+ clearTimeout(instance);
41
+ if (typeof done === 'function') {
42
+ done();
43
+ }
44
+ });
45
+ res.once('error', (e)=>{
46
+ clearTimeout(instance);
47
+ if (typeof done === 'function') {
48
+ done(e);
49
+ }
50
+ });
51
+ }
52
+
53
+ function processHandlerExecutionOutput(res, next, output) {
54
+ if (isPromise(output)) {
55
+ output.then((r)=>{
56
+ if (typeof r !== 'undefined') {
57
+ core.send(res, r);
58
+ }
59
+ return r;
60
+ }).catch(next);
61
+ return;
62
+ }
63
+ if (typeof output !== 'undefined') {
64
+ core.send(res, output);
65
+ }
66
+ }
67
+
68
+ function decodeParam(val) {
69
+ /* istanbul ignore next */ if (typeof val !== 'string' || val.length === 0) {
70
+ return val;
71
+ }
72
+ return decodeURIComponent(val);
73
+ }
74
+ class PathMatcher {
75
+ test(path) {
76
+ const fastSlash = this.path === '/' && this.regexpOptions.end === false;
77
+ if (fastSlash) {
78
+ return true;
79
+ }
80
+ return this.regexp.test(path);
81
+ }
82
+ exec(path) {
83
+ let match = null;
84
+ const fastSlash = this.path === '/' && this.regexpOptions.end === false;
85
+ if (fastSlash) {
86
+ return {
87
+ path: '/',
88
+ params: {}
89
+ };
90
+ }
91
+ match = this.regexp.exec(path);
92
+ if (!match) {
93
+ return undefined;
94
+ }
95
+ if (this.path instanceof RegExp) {
96
+ return {
97
+ path,
98
+ params: {
99
+ 0: decodeParam(match[0])
100
+ }
101
+ };
102
+ }
103
+ const output = {};
104
+ for(let i = 1; i < match.length; i++){
105
+ const key = this.regexpKeys[i - 1];
106
+ const prop = key.name;
107
+ const val = decodeParam(match[i]);
108
+ if (typeof val !== 'undefined') {
109
+ output[prop] = val;
110
+ }
111
+ }
112
+ return {
113
+ path: match[0],
114
+ params: output
115
+ };
116
+ }
117
+ constructor(path, options){
118
+ this.regexpKeys = [];
119
+ this.path = path;
120
+ this.regexpOptions = options || {};
121
+ if (path instanceof RegExp) {
122
+ this.regexp = path;
123
+ } else {
124
+ this.regexp = pathToRegexp.pathToRegexp(path, this.regexpKeys, options);
125
+ }
126
+ }
127
+ }
128
+
129
+ class Layer {
130
+ // --------------------------------------------------
131
+ isError() {
132
+ return this.fn.length === 4;
133
+ }
134
+ dispatch(req, res, meta, next, err) {
135
+ core.setRequestParams(req, meta.params || {});
136
+ core.setRequestMountPath(req, meta.mountPath || '/');
137
+ if (typeof err !== 'undefined') {
138
+ if (this.fn.length === 4) {
139
+ try {
140
+ this.fn(err, req, res, next);
141
+ } catch (e) {
142
+ /* istanbul ignore next */ /* istanbul ignore next */ if (e instanceof Error) {
143
+ next(e);
144
+ } else {
145
+ next(new http.BadRequestError({
146
+ message: 'The request could not be processed by the error handler.'
147
+ }));
148
+ }
149
+ }
150
+ return;
151
+ }
152
+ /* istanbul ignore next */ next(err);
153
+ /* istanbul ignore next */ return;
154
+ }
155
+ /* istanbul ignore next */ if (this.fn.length > 3) {
156
+ next();
157
+ return;
158
+ }
159
+ try {
160
+ const output = this.fn(req, res, next);
161
+ processHandlerExecutionOutput(res, next, output);
162
+ } catch (e) {
163
+ /* istanbul ignore next */ if (e instanceof Error) {
164
+ next(e);
165
+ } else {
166
+ next(new http.BadRequestError({
167
+ message: 'The request could not be processed by the handler.'
168
+ }));
169
+ }
170
+ }
171
+ }
172
+ // --------------------------------------------------
173
+ matchPath(path) {
174
+ return this.pathMatcher.test(path);
175
+ }
176
+ exec(path) {
177
+ return this.pathMatcher.exec(path);
178
+ }
179
+ // --------------------------------------------------
180
+ constructor(options, fn){
181
+ this['@instanceof'] = Symbol.for('Layer');
182
+ this.pathMatcher = new PathMatcher(options.path, options.pathMatcher);
183
+ this.fn = fn;
184
+ }
185
+ }
186
+
187
+ function isLayerInstance(input) {
188
+ return isInstance(input, 'Layer');
189
+ }
190
+
191
+ class Route {
192
+ // --------------------------------------------------
193
+ matchPath(path) {
194
+ return this.pathMatcher.test(path);
195
+ }
196
+ matchMethod(method) {
197
+ let name = method.toLowerCase();
198
+ if (name === core.Method.HEAD && !smob.hasOwnProperty(this.layers, name)) {
199
+ name = core.Method.GET;
200
+ }
201
+ return Object.prototype.hasOwnProperty.call(this.layers, name);
202
+ }
203
+ // --------------------------------------------------
204
+ getMethods() {
205
+ const keys = Object.keys(this.layers);
206
+ if (smob.hasOwnProperty(this.layers, core.Method.GET) && !smob.hasOwnProperty(this.layers, core.Method.HEAD)) {
207
+ keys.push(core.Method.HEAD);
208
+ }
209
+ return keys;
210
+ }
211
+ // --------------------------------------------------
212
+ dispatch(req, res, meta, done) {
213
+ /* istanbul ignore next */ if (!req.method) {
214
+ done();
215
+ return;
216
+ }
217
+ let name = req.method.toLowerCase();
218
+ if (name === core.Method.HEAD && !smob.hasOwnProperty(this.layers, name)) {
219
+ name = core.Method.GET;
220
+ }
221
+ const layers = this.layers[name];
222
+ /* istanbul ignore next */ if (typeof layers === 'undefined' || layers.length === 0 || typeof meta.path === 'undefined') {
223
+ done();
224
+ return;
225
+ }
226
+ const layerMeta = {
227
+ ...meta
228
+ };
229
+ const output = this.pathMatcher.exec(meta.path);
230
+ if (output) {
231
+ layerMeta.params = smob.merge({}, meta.params || {}, output.params);
232
+ }
233
+ let index = -1;
234
+ const next = (err)=>{
235
+ index++;
236
+ if (index >= layers.length) {
237
+ setImmediate(done, err);
238
+ return;
239
+ }
240
+ const layer = layers[index];
241
+ if (err && !layer.isError()) {
242
+ next(err);
243
+ return;
244
+ }
245
+ layer.dispatch(req, res, {
246
+ ...layerMeta
247
+ }, next);
248
+ };
249
+ next();
250
+ }
251
+ // --------------------------------------------------
252
+ register(method, ...handlers) {
253
+ this.layers[method] = [];
254
+ for(let i = 0; i < handlers.length; i++){
255
+ const layer = new Layer({
256
+ path: this.path,
257
+ pathMatcher: this.pathMatcherOptions
258
+ }, handlers[i]);
259
+ this.layers[method].push(layer);
260
+ }
261
+ }
262
+ get(...handlers) {
263
+ return this.register(core.Method.GET, ...handlers);
264
+ }
265
+ post(...handlers) {
266
+ return this.register(core.Method.POST, ...handlers);
267
+ }
268
+ put(...handlers) {
269
+ return this.register(core.Method.PUT, ...handlers);
270
+ }
271
+ patch(...handlers) {
272
+ return this.register(core.Method.PATCH, ...handlers);
273
+ }
274
+ delete(...handlers) {
275
+ return this.register(core.Method.DELETE, ...handlers);
276
+ }
277
+ head(...handlers) {
278
+ return this.register(core.Method.HEAD, ...handlers);
279
+ }
280
+ options(...handlers) {
281
+ return this.register(core.Method.OPTIONS, ...handlers);
282
+ }
283
+ // --------------------------------------------------
284
+ isStrictPath() {
285
+ return typeof this.path !== 'string' || this.path !== '/' && this.path.length !== 0;
286
+ }
287
+ // --------------------------------------------------
288
+ constructor(options){
289
+ this['@instanceof'] = Symbol.for('Route');
290
+ this.layers = {};
291
+ this.path = options.path;
292
+ this.pathMatcherOptions = {
293
+ end: true,
294
+ strict: this.isStrictPath(),
295
+ ...options.pathMatcher
296
+ };
297
+ this.pathMatcher = new PathMatcher(this.path, this.pathMatcherOptions);
298
+ }
299
+ }
300
+
301
+ function isRouteInstance(input) {
302
+ return isInstance(input, 'Route');
303
+ }
304
+
305
+ function isRouterInstance(input) {
306
+ return isInstance(input, 'Router');
307
+ }
308
+ class Router {
309
+ // --------------------------------------------------
310
+ setPathMatcherOptions(input) {
311
+ this.pathMatcherOptions = input;
312
+ if (this.pathMatcher) {
313
+ this.pathMatcher.regexpOptions = this.pathMatcherOptions;
314
+ }
315
+ }
316
+ setPath(value) {
317
+ if (value === '/' || !isPath(value)) {
318
+ this.path = '/';
319
+ return;
320
+ }
321
+ if (typeof value === 'string') {
322
+ this.path = core.withLeadingSlash(core.withoutTrailingSlash(`${value}`));
323
+ } else {
324
+ this.path = value;
325
+ }
326
+ this.pathMatcher = new PathMatcher(this.path, this.pathMatcherOptions);
327
+ }
328
+ // --------------------------------------------------
329
+ createListener() {
330
+ this.isRoot = true;
331
+ return (req, res)=>{
332
+ this.dispatch(req, res);
333
+ };
334
+ }
335
+ /* istanbul ignore next */ listen(port) {
336
+ const server = node_http.createServer(this.createListener());
337
+ return server.listen(port);
338
+ }
339
+ // --------------------------------------------------
340
+ matchPath(path) {
341
+ if (this.pathMatcher) {
342
+ return this.pathMatcher.test(path);
343
+ }
344
+ return true;
345
+ }
346
+ // --------------------------------------------------
347
+ dispatch(req, res, meta, done) {
348
+ let index = -1;
349
+ meta = meta || {};
350
+ let allowedMethods = [];
351
+ if (this.isRoot && typeof this.timeout === 'number') {
352
+ createRequestTimeout(res, this.timeout, done);
353
+ }
354
+ const fn = (err)=>{
355
+ /* istanbul ignore if */ if (!this.isRoot) {
356
+ if (typeof done !== 'undefined') {
357
+ setImmediate(()=>done(err));
358
+ }
359
+ return;
360
+ }
361
+ if (typeof err !== 'undefined') {
362
+ res.statusCode = http.BadRequestErrorOptions.statusCode;
363
+ res.statusMessage = http.BadRequestErrorOptions.message;
364
+ res.end();
365
+ return;
366
+ }
367
+ if (req.method && req.method.toLowerCase() === core.Method.OPTIONS) {
368
+ const options = allowedMethods.map((key)=>key.toUpperCase()).join(',');
369
+ res.setHeader(core.HeaderName.ALLOW, options);
370
+ core.send(res, options);
371
+ return;
372
+ }
373
+ res.statusCode = http.NotFoundErrorOptions.statusCode;
374
+ res.end();
375
+ };
376
+ let path = meta.path || core.useRequestPath(req);
377
+ if (this.pathMatcher) {
378
+ const output = this.pathMatcher.exec(path);
379
+ if (typeof output !== 'undefined') {
380
+ meta.mountPath = core.cleanDoubleSlashes(`${meta.mountPath || ''}/${output.path}`);
381
+ if (path === output.path) {
382
+ path = '/';
383
+ } else {
384
+ path = core.withLeadingSlash(path.substring(output.path.length));
385
+ }
386
+ meta.params = smob.merge(meta.params || {}, output.params);
387
+ }
388
+ }
389
+ meta.path = path;
390
+ if (!meta.mountPath) {
391
+ meta.mountPath = '/';
392
+ }
393
+ const next = (err)=>{
394
+ if (index >= this.stack.length) {
395
+ setImmediate(fn, err);
396
+ return;
397
+ }
398
+ let layer;
399
+ let match = false;
400
+ while(!match && index < this.stack.length){
401
+ index++;
402
+ layer = this.stack[index];
403
+ if (isLayerInstance(layer)) {
404
+ if (!layer.isError() && err) {
405
+ continue;
406
+ }
407
+ match = layer.matchPath(path);
408
+ }
409
+ if (isRouterInstance(layer)) {
410
+ match = layer.matchPath(path);
411
+ }
412
+ if (isRouteInstance(layer)) {
413
+ match = layer.matchPath(path);
414
+ if (req.method && !layer.matchMethod(req.method)) {
415
+ match = false;
416
+ if (req.method.toLowerCase() === core.Method.OPTIONS) {
417
+ allowedMethods = smob.mergeArrays(allowedMethods, layer.getMethods(), true);
418
+ }
419
+ }
420
+ }
421
+ }
422
+ if (!match || !layer) {
423
+ setImmediate(fn, err);
424
+ return;
425
+ }
426
+ const layerMeta = {
427
+ ...meta
428
+ };
429
+ if (isLayerInstance(layer)) {
430
+ const output = layer.exec(path);
431
+ if (output) {
432
+ layerMeta.params = smob.merge(output.params, layerMeta.params || {});
433
+ layerMeta.mountPath = core.cleanDoubleSlashes(`${layerMeta.mountPath || ''}/${output.path}`);
434
+ }
435
+ }
436
+ if (err) {
437
+ if (isLayerInstance(layer) && layer.isError()) {
438
+ layer.dispatch(req, res, layerMeta, next, err);
439
+ return;
440
+ }
441
+ /* istanbul ignore next */ setImmediate(next, err);
442
+ return;
443
+ }
444
+ layer.dispatch(req, res, layerMeta, next);
445
+ };
446
+ next();
447
+ }
448
+ /* istanbul ignore next */ dispatchAsync(req, res) {
449
+ return new Promise((resolve, reject)=>{
450
+ this.dispatch(req, res, {}, (err)=>{
451
+ if (err) {
452
+ reject(err);
453
+ return;
454
+ }
455
+ resolve();
456
+ });
457
+ });
458
+ }
459
+ // --------------------------------------------------
460
+ route(path) {
461
+ if (typeof path === 'string' && path.length > 0) {
462
+ path = core.withLeadingSlash(path);
463
+ }
464
+ const index = this.stack.findIndex((item)=>isRouteInstance(item) && item.path === path);
465
+ if (index !== -1) {
466
+ return this.stack[index];
467
+ }
468
+ const route = new Route({
469
+ path,
470
+ pathMatcher: {
471
+ sensitive: this.pathMatcherOptions.sensitive
472
+ }
473
+ });
474
+ this.stack.push(route);
475
+ return route;
476
+ }
477
+ delete(path, ...handlers) {
478
+ const route = this.route(path);
479
+ route.delete(...handlers);
480
+ return this;
481
+ }
482
+ get(path, ...handlers) {
483
+ const route = this.route(path);
484
+ route.get(...handlers);
485
+ return this;
486
+ }
487
+ post(path, ...handlers) {
488
+ const route = this.route(path);
489
+ route.post(...handlers);
490
+ return this;
491
+ }
492
+ put(path, ...handlers) {
493
+ const route = this.route(path);
494
+ route.put(...handlers);
495
+ return this;
496
+ }
497
+ patch(path, ...handlers) {
498
+ const route = this.route(path);
499
+ route.patch(...handlers);
500
+ return this;
501
+ }
502
+ head(path, ...handlers) {
503
+ const route = this.route(path);
504
+ route.head(...handlers);
505
+ return this;
506
+ }
507
+ options(path, ...handlers) {
508
+ const route = this.route(path);
509
+ route.options(...handlers);
510
+ return this;
511
+ }
512
+ use(...input) {
513
+ /* istanbul ignore next */ if (input.length === 0) {
514
+ return this;
515
+ }
516
+ let path;
517
+ if (isPath(input[0])) {
518
+ path = input.shift();
519
+ }
520
+ for(let i = 0; i < input.length; i++){
521
+ const item = input[i];
522
+ if (isRouterInstance(item)) {
523
+ if (path) {
524
+ item.setPath(path);
525
+ }
526
+ item.setPathMatcherOptions(this.pathMatcherOptions);
527
+ this.stack.push(item);
528
+ continue;
529
+ }
530
+ if (typeof item === 'function') {
531
+ this.stack.push(new Layer({
532
+ path: path || '/',
533
+ pathMatcher: {
534
+ strict: false,
535
+ end: false,
536
+ sensitive: this.pathMatcherOptions.sensitive
537
+ }
538
+ }, item));
539
+ }
540
+ }
541
+ return this;
542
+ }
543
+ // --------------------------------------------------
544
+ constructor(ctx){
545
+ this['@instanceof'] = Symbol.for('Router');
546
+ /**
547
+ * Array of mounted layers, routes & routers.
548
+ *
549
+ * @protected
550
+ */ this.stack = [];
551
+ ctx = ctx || {};
552
+ this.pathMatcherOptions = {
553
+ end: false,
554
+ sensitive: true,
555
+ ...ctx.pathMatcher || {}
556
+ };
557
+ this.timeout = ctx.timeout;
558
+ this.setPath(ctx.path || '/');
559
+ }
560
+ }
561
+
562
+ exports.Layer = Layer;
563
+ exports.PathMatcher = PathMatcher;
564
+ exports.Route = Route;
565
+ exports.Router = Router;
566
+ exports.createRequestTimeout = createRequestTimeout;
567
+ exports.isInstance = isInstance;
568
+ exports.isLayerInstance = isLayerInstance;
569
+ exports.isPath = isPath;
570
+ exports.isPromise = isPromise;
571
+ exports.isRouteInstance = isRouteInstance;
572
+ exports.isRouterInstance = isRouterInstance;
573
+ exports.processHandlerExecutionOutput = processHandlerExecutionOutput;
574
+ Object.keys(core).forEach(function (k) {
575
+ if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
576
+ enumerable: true,
577
+ get: function () { return core[k]; }
578
+ });
579
+ });
2
580
  //# sourceMappingURL=index.cjs.map