routup 3.0.0-alpha.2 → 3.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/README.md CHANGED
@@ -61,6 +61,8 @@ as all concepts and basics are taught there.
61
61
  ### Handlers
62
62
 
63
63
  Both core and error handlers, can be defined in two different ways.
64
+ Core handler functions can have up to 3 arguments (req, res, next) whereas error handler functions can have up to 4 arguments (err, req, res, next).
65
+ This should be familiar to anyone who has used express before.
64
66
 
65
67
  **`Shorthand`**
66
68
 
@@ -187,17 +189,17 @@ According to the fact that routup is a minimalistic framework,
187
189
  it depends on [plugins](https://github.com/routup/plugins) to cover some
188
190
  typically http framework functions, which are not integrated in the main package.
189
191
 
190
- | Name | Description |
191
- |-----------------------------------------------------------------------------------------------|------------------------------------------------------------------------|
192
- | [body](https://github.com/routup/plugins/tree/master/packages/body) | Read and parse the request body. |
193
- | [cookie](https://github.com/routup/plugins/tree/master/packages/cookie) | Read and parse request cookies and serialize cookies for the response. |
194
- | [decorators](https://github.com/routup/plugins/tree/master/packages/decorators) | Create request handlers with class-, method- & parameter-decorators. |
195
- | [prometheus](https://github.com/routup/plugins/tree/master/packages/prometheus) | Collect and serve metrics for prometheus. |
196
- | [query](https://github.com/routup/plugins/tree/master/packages/query) | Read and parse the query string of the request url. |
197
- | [rate-limit](https://github.com/routup/plugins/tree/master/packages/rate-limit) | Rate limit incoming requests. |
198
- | [rate-limit-redis](https://github.com/routup/plugins/tree/master/packages/rate-limit-redis) | Redis adapter for the rate-limit plugin. |
199
- | [static](https://github.com/routup/plugins/tree/master/packages/static) | Serve static files from a directory. |
200
- | [swagger](https://github.com/routup/plugins/tree/master/packages/swagger) | Serve generated docs from URL or based on a JSON file. |
192
+ | Name | Description |
193
+ |---------------------------------------------------------------------------------------------|------------------------------------------------------------------------|
194
+ | [assets](https://github.com/routup/plugins/tree/master/packages/assets) | Serve static files from a directory. |
195
+ | [body](https://github.com/routup/plugins/tree/master/packages/body) | Read and parse the request body. |
196
+ | [cookie](https://github.com/routup/plugins/tree/master/packages/cookie) | Read and parse request cookies and serialize cookies for the response. |
197
+ | [decorators](https://github.com/routup/plugins/tree/master/packages/decorators) | Create request handlers with class-, method- & parameter-decorators. |
198
+ | [prometheus](https://github.com/routup/plugins/tree/master/packages/prometheus) | Collect and serve metrics for prometheus. |
199
+ | [query](https://github.com/routup/plugins/tree/master/packages/query) | Read and parse the query string of the request url. |
200
+ | [rate-limit](https://github.com/routup/plugins/tree/master/packages/rate-limit) | Rate limit incoming requests. |
201
+ | [rate-limit-redis](https://github.com/routup/plugins/tree/master/packages/rate-limit-redis) | Redis adapter for the rate-limit plugin. |
202
+ | [swagger](https://github.com/routup/plugins/tree/master/packages/swagger) | Serve generated docs from URL or based on a JSON file. |
201
203
 
202
204
  ## Benchmarks
203
205
 
package/dist/index.cjs CHANGED
@@ -79,6 +79,64 @@ function isError(input) {
79
79
  }, input);
80
80
  }
81
81
 
82
+ function isRequestCacheable(req, modifiedTime) {
83
+ const modifiedSince = req.headers[exports.HeaderName.IF_MODIFIED_SINCE];
84
+ if (!modifiedSince) {
85
+ return false;
86
+ }
87
+ modifiedTime = typeof modifiedTime === 'string' ? new Date(modifiedTime) : modifiedTime;
88
+ return new Date(modifiedSince) >= modifiedTime;
89
+ }
90
+
91
+ const envSymbol = Symbol.for('ReqEnv');
92
+ function setRequestEnv(req, key, value) {
93
+ if (envSymbol in req) {
94
+ if (typeof key === 'object') {
95
+ if (value) {
96
+ req[envSymbol] = smob.merge(req[envSymbol], key);
97
+ } else {
98
+ req[envSymbol] = key;
99
+ }
100
+ } else {
101
+ req[envSymbol][key] = value;
102
+ }
103
+ return;
104
+ }
105
+ if (typeof key === 'object') {
106
+ req[envSymbol] = key;
107
+ return;
108
+ }
109
+ req[envSymbol] = {
110
+ [key]: value
111
+ };
112
+ }
113
+ function useRequestEnv(req, key) {
114
+ if (envSymbol in req) {
115
+ if (typeof key === 'string') {
116
+ return req[envSymbol][key];
117
+ }
118
+ return req[envSymbol];
119
+ }
120
+ if (typeof key === 'string') {
121
+ return undefined;
122
+ }
123
+ return {};
124
+ }
125
+ function unsetRequestEnv(req, key) {
126
+ if (envSymbol in req) {
127
+ if (smob.hasOwnProperty(req[envSymbol], key)) {
128
+ delete req[envSymbol][key];
129
+ }
130
+ }
131
+ }
132
+
133
+ function getRequestHeader(req, name) {
134
+ return req.headers[name];
135
+ }
136
+ function setRequestHeader(req, name, value) {
137
+ req.headers[name] = value;
138
+ }
139
+
82
140
  /*
83
141
  Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
84
142
  that are within a single set-cookie field-value, such as in the Expires portion.
@@ -364,160 +422,6 @@ function isWebResponse(input) {
364
422
  return typeof Response !== 'undefined' && input instanceof Response;
365
423
  }
366
424
 
367
- const BodySymbol = Symbol.for('ReqBody');
368
- function useRequestBody(req, key) {
369
- let body;
370
- /* istanbul ignore next */ if ('body' in req) {
371
- body = req.body;
372
- }
373
- if (BodySymbol in req) {
374
- if (body) {
375
- body = smob.merge({}, req[BodySymbol], body);
376
- } else {
377
- body = req[BodySymbol];
378
- }
379
- }
380
- if (body) {
381
- if (typeof key === 'string') {
382
- return body[key];
383
- }
384
- return body;
385
- }
386
- return typeof key === 'string' ? undefined : {};
387
- }
388
- function hasRequestBody(req) {
389
- return 'body' in req || BodySymbol in req;
390
- }
391
- function setRequestBody(req, key, value) {
392
- if (isObject(key)) {
393
- req[BodySymbol] = key;
394
- return;
395
- }
396
- req[BodySymbol] = {
397
- [key]: value
398
- };
399
- }
400
- function extendRequestBody(req, key, value) {
401
- if (hasRequestBody(req)) {
402
- const body = useRequestBody(req);
403
- // body can not be merged :/
404
- if (!isObject(body)) {
405
- return;
406
- }
407
- if (isObject(key)) {
408
- req[BodySymbol] = smob.merge({}, key, body);
409
- } else {
410
- body[key] = value;
411
- req[BodySymbol] = body;
412
- }
413
- return;
414
- }
415
- if (isObject(key)) {
416
- setRequestBody(req, key);
417
- return;
418
- }
419
- setRequestBody(req, key, value);
420
- }
421
-
422
- function isRequestCacheable(req, modifiedTime) {
423
- const modifiedSince = req.headers[exports.HeaderName.IF_MODIFIED_SINCE];
424
- if (!modifiedSince) {
425
- return false;
426
- }
427
- modifiedTime = typeof modifiedTime === 'string' ? new Date(modifiedTime) : modifiedTime;
428
- return new Date(modifiedSince) >= modifiedTime;
429
- }
430
-
431
- const CookieSymbol = Symbol.for('ReqCookie');
432
- function useRequestCookies(req) {
433
- if (CookieSymbol in req) {
434
- return req[CookieSymbol];
435
- }
436
- return {};
437
- }
438
- function hasRequestCookies(req) {
439
- return CookieSymbol in req && isObject(req[CookieSymbol]);
440
- }
441
- function useRequestCookie(req, name) {
442
- return useRequestCookies(req)[name];
443
- }
444
- function setRequestCookies(req, key, value) {
445
- if (isObject(key)) {
446
- req[CookieSymbol] = key;
447
- return;
448
- }
449
- req[CookieSymbol] = {
450
- [key]: value
451
- };
452
- }
453
- function extendRequestCookies(req, key, value) {
454
- if (hasRequestCookies(req)) {
455
- const cookies = useRequestCookies(req);
456
- if (isObject(key)) {
457
- req[CookieSymbol] = smob.merge({}, key, cookies);
458
- } else {
459
- cookies[key] = value;
460
- req[CookieSymbol] = cookies;
461
- }
462
- req[CookieSymbol] = smob.merge(req[CookieSymbol], cookies);
463
- return;
464
- }
465
- if (isObject(key)) {
466
- setRequestCookies(req, key);
467
- return;
468
- }
469
- setRequestCookies(req, key, value);
470
- }
471
-
472
- const envSymbol = Symbol.for('ReqEnv');
473
- function setRequestEnv(req, key, value) {
474
- if (envSymbol in req) {
475
- if (typeof key === 'object') {
476
- if (value) {
477
- req[envSymbol] = smob.merge(req[envSymbol], key);
478
- } else {
479
- req[envSymbol] = key;
480
- }
481
- } else {
482
- req[envSymbol][key] = value;
483
- }
484
- return;
485
- }
486
- if (typeof key === 'object') {
487
- req[envSymbol] = key;
488
- return;
489
- }
490
- req[envSymbol] = {
491
- [key]: value
492
- };
493
- }
494
- function useRequestEnv(req, key) {
495
- if (envSymbol in req) {
496
- if (typeof key === 'string') {
497
- return req[envSymbol][key];
498
- }
499
- return req[envSymbol];
500
- }
501
- if (typeof key === 'string') {
502
- return undefined;
503
- }
504
- return {};
505
- }
506
- function unsetRequestEnv(req, key) {
507
- if (envSymbol in req) {
508
- if (smob.hasOwnProperty(req[envSymbol], key)) {
509
- delete req[envSymbol][key];
510
- }
511
- }
512
- }
513
-
514
- function getRequestHeader(req, name) {
515
- return req.headers[name];
516
- }
517
- function setRequestHeader(req, name, value) {
518
- req.headers[name] = value;
519
- }
520
-
521
425
  const NegotiatorSymbol = Symbol.for('ReqNegotiator');
522
426
  function useRequestNegotiator(req) {
523
427
  if (NegotiatorSymbol in req) {
@@ -775,52 +679,6 @@ function getRequestProtocol(req, options) {
775
679
  return index !== -1 ? header.substring(0, index).trim() : header.trim();
776
680
  }
777
681
 
778
- const QuerySymbol = Symbol.for('ReqQuery');
779
- function useRequestQuery(req, key) {
780
- /* istanbul ignore if */ if ('query' in req) {
781
- if (typeof key === 'string') {
782
- return req.query[key];
783
- }
784
- return req.query;
785
- }
786
- if (QuerySymbol in req) {
787
- if (typeof key === 'string') {
788
- return req[QuerySymbol][key];
789
- }
790
- return req[QuerySymbol];
791
- }
792
- return typeof key === 'string' ? undefined : {};
793
- }
794
- function hasRequestQuery(req) {
795
- return QuerySymbol in req && isObject(req[QuerySymbol]) || 'query' in req && isObject(req.query);
796
- }
797
- function setRequestQuery(req, key, value) {
798
- if (isObject(key)) {
799
- req[QuerySymbol] = key;
800
- return;
801
- }
802
- req[QuerySymbol] = {
803
- [key]: value
804
- };
805
- }
806
- function extendRequestQuery(req, key, value) {
807
- if (hasRequestQuery(req)) {
808
- const query = useRequestQuery(req);
809
- if (isObject(key)) {
810
- req[QuerySymbol] = smob.merge({}, key, query);
811
- } else {
812
- query[key] = value;
813
- req[QuerySymbol] = query;
814
- }
815
- return;
816
- }
817
- if (isObject(key)) {
818
- setRequestQuery(req, key);
819
- return;
820
- }
821
- setRequestQuery(req, key, value);
822
- }
823
-
824
682
  function createRequest(context) {
825
683
  let readable;
826
684
  if (context.body) {
@@ -1798,10 +1656,7 @@ function isPlugin(input) {
1798
1656
  if (typeof input.name !== 'undefined' && typeof input.name !== 'string') {
1799
1657
  return false;
1800
1658
  }
1801
- if (typeof input.install !== 'function' || input.install.length !== 1) {
1802
- return false;
1803
- }
1804
- return typeof input.version === 'undefined' || typeof input.version === 'string';
1659
+ return typeof input.install === 'function' && input.install.length === 1;
1805
1660
  }
1806
1661
 
1807
1662
  function transformRouterOptions(input) {
@@ -1923,110 +1778,50 @@ class Router {
1923
1778
  }
1924
1779
  return false;
1925
1780
  }
1926
- delete(path, handler) {
1927
- if (isPath(path)) {
1928
- this.use({
1929
- ...handler,
1930
- method: exports.MethodName.DELETE,
1931
- path
1932
- });
1933
- return this;
1934
- }
1935
- this.use({
1936
- ...path,
1937
- method: exports.MethodName.DELETE
1938
- });
1781
+ delete(...input) {
1782
+ this.useForMethod(exports.MethodName.DELETE, ...input);
1939
1783
  return this;
1940
1784
  }
1941
- get(path, handler) {
1942
- if (isPath(path)) {
1943
- this.use({
1944
- ...handler,
1945
- method: exports.MethodName.GET,
1946
- path
1947
- });
1948
- return this;
1949
- }
1950
- this.use({
1951
- ...path,
1952
- method: exports.MethodName.GET
1953
- });
1785
+ get(...input) {
1786
+ this.useForMethod(exports.MethodName.GET, ...input);
1954
1787
  return this;
1955
1788
  }
1956
- post(path, handler) {
1957
- if (isPath(path)) {
1958
- this.use({
1959
- ...handler,
1960
- method: exports.MethodName.POST,
1961
- path
1962
- });
1963
- return this;
1964
- }
1965
- this.use({
1966
- ...path,
1967
- method: exports.MethodName.POST
1968
- });
1789
+ post(...input) {
1790
+ this.useForMethod(exports.MethodName.POST, ...input);
1969
1791
  return this;
1970
1792
  }
1971
- put(path, handler) {
1972
- if (isPath(path)) {
1973
- this.use({
1974
- ...handler,
1975
- method: exports.MethodName.PUT,
1976
- path
1977
- });
1978
- return this;
1979
- }
1980
- this.use({
1981
- ...path,
1982
- method: exports.MethodName.PUT
1983
- });
1793
+ put(...input) {
1794
+ this.useForMethod(exports.MethodName.PUT, ...input);
1984
1795
  return this;
1985
1796
  }
1986
- patch(path, handler) {
1987
- if (isPath(path)) {
1988
- this.use({
1989
- ...handler,
1990
- method: exports.MethodName.PATCH,
1991
- path
1992
- });
1993
- return this;
1994
- }
1995
- this.use({
1996
- ...path,
1997
- method: exports.MethodName.PATCH
1998
- });
1797
+ patch(...input) {
1798
+ this.useForMethod(exports.MethodName.PATCH, ...input);
1999
1799
  return this;
2000
1800
  }
2001
- head(path, handler) {
2002
- if (isPath(path)) {
2003
- this.use({
2004
- ...handler,
2005
- method: exports.MethodName.HEAD,
2006
- path
2007
- });
2008
- return this;
2009
- }
2010
- this.use({
2011
- ...path,
2012
- method: exports.MethodName.HEAD
2013
- });
1801
+ head(...input) {
1802
+ this.useForMethod(exports.MethodName.HEAD, ...input);
1803
+ return this;
1804
+ }
1805
+ options(...input) {
1806
+ this.useForMethod(exports.MethodName.OPTIONS, ...input);
2014
1807
  return this;
2015
1808
  }
2016
- options(path, handler) {
2017
- if (isPath(path)) {
1809
+ // --------------------------------------------------
1810
+ useForMethod(method, ...input) {
1811
+ const base = {
1812
+ method
1813
+ };
1814
+ for(let i = 0; i < input.length; i++){
1815
+ const element = input[i];
1816
+ if (isPath(element)) {
1817
+ base.path = element;
1818
+ continue;
1819
+ }
2018
1820
  this.use({
2019
- ...handler,
2020
- method: exports.MethodName.OPTIONS,
2021
- path
1821
+ ...base,
1822
+ ...element
2022
1823
  });
2023
- return this;
2024
1824
  }
2025
- this.use({
2026
- ...path,
2027
- method: exports.MethodName.OPTIONS
2028
- });
2029
- return this;
2030
1825
  }
2031
1826
  use(...input) {
2032
1827
  const modifyPath = (input)=>{
@@ -2052,6 +1847,7 @@ class Router {
2052
1847
  if (isHandler(item)) {
2053
1848
  item.path = path || modifyPath(item.path);
2054
1849
  this.stack.push(new Layer(item));
1850
+ continue;
2055
1851
  }
2056
1852
  if (isPlugin(item)) {
2057
1853
  if (path) {
@@ -2068,15 +1864,15 @@ class Router {
2068
1864
  // --------------------------------------------------
2069
1865
  install(plugin, context = {}) {
2070
1866
  const name = context.name || plugin.name;
1867
+ const router = new Router({
1868
+ name
1869
+ });
1870
+ plugin.install(router);
2071
1871
  if (context.path) {
2072
- const router = new Router({
2073
- name
2074
- });
2075
- plugin.install(router);
2076
1872
  this.use(context.path, router);
2077
- return this;
1873
+ } else {
1874
+ this.use(router);
2078
1875
  }
2079
- plugin.install(this);
2080
1876
  return this;
2081
1877
  }
2082
1878
  // --------------------------------------------------
@@ -2114,9 +1910,6 @@ exports.dispatchNodeRequest = dispatchNodeRequest;
2114
1910
  exports.dispatchRawRequest = dispatchRawRequest;
2115
1911
  exports.dispatchWebRequest = dispatchWebRequest;
2116
1912
  exports.errorHandler = errorHandler;
2117
- exports.extendRequestBody = extendRequestBody;
2118
- exports.extendRequestCookies = extendRequestCookies;
2119
- exports.extendRequestQuery = extendRequestQuery;
2120
1913
  exports.getRequestAcceptableCharset = getRequestAcceptableCharset;
2121
1914
  exports.getRequestAcceptableCharsets = getRequestAcceptableCharsets;
2122
1915
  exports.getRequestAcceptableContentType = getRequestAcceptableContentType;
@@ -2129,9 +1922,6 @@ exports.getRequestHeader = getRequestHeader;
2129
1922
  exports.getRequestHostName = getRequestHostName;
2130
1923
  exports.getRequestIP = getRequestIP;
2131
1924
  exports.getRequestProtocol = getRequestProtocol;
2132
- exports.hasRequestBody = hasRequestBody;
2133
- exports.hasRequestCookies = hasRequestCookies;
2134
- exports.hasRequestQuery = hasRequestQuery;
2135
1925
  exports.isError = isError;
2136
1926
  exports.isHandler = isHandler;
2137
1927
  exports.isLayerInstance = isLayerInstance;
@@ -2151,29 +1941,22 @@ exports.sendRedirect = sendRedirect;
2151
1941
  exports.sendStream = sendStream;
2152
1942
  exports.sendWebBlob = sendWebBlob;
2153
1943
  exports.sendWebResponse = sendWebResponse;
2154
- exports.setRequestBody = setRequestBody;
2155
- exports.setRequestCookies = setRequestCookies;
2156
1944
  exports.setRequestEnv = setRequestEnv;
2157
1945
  exports.setRequestHeader = setRequestHeader;
2158
1946
  exports.setRequestMountPath = setRequestMountPath;
2159
1947
  exports.setRequestParam = setRequestParam;
2160
1948
  exports.setRequestParams = setRequestParams;
2161
- exports.setRequestQuery = setRequestQuery;
2162
1949
  exports.setRequestRouterPath = setRequestRouterPath;
2163
1950
  exports.setResponseCacheHeaders = setResponseCacheHeaders;
2164
1951
  exports.setResponseContentTypeByFileName = setResponseContentTypeByFileName;
2165
1952
  exports.setResponseHeaderAttachment = setResponseHeaderAttachment;
2166
1953
  exports.setResponseHeaderContentType = setResponseHeaderContentType;
2167
1954
  exports.unsetRequestEnv = unsetRequestEnv;
2168
- exports.useRequestBody = useRequestBody;
2169
- exports.useRequestCookie = useRequestCookie;
2170
- exports.useRequestCookies = useRequestCookies;
2171
1955
  exports.useRequestEnv = useRequestEnv;
2172
1956
  exports.useRequestMountPath = useRequestMountPath;
2173
1957
  exports.useRequestNegotiator = useRequestNegotiator;
2174
1958
  exports.useRequestParam = useRequestParam;
2175
1959
  exports.useRequestParams = useRequestParams;
2176
1960
  exports.useRequestPath = useRequestPath;
2177
- exports.useRequestQuery = useRequestQuery;
2178
1961
  exports.useRequestRouterPath = useRequestRouterPath;
2179
1962
  //# sourceMappingURL=index.cjs.map