routup 3.0.0-alpha.1 → 3.0.0-alpha.3

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/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) {
@@ -1791,14 +1649,17 @@ function isLayerInstance(input) {
1791
1649
  return isInstance(input, LayerSymbol);
1792
1650
  }
1793
1651
 
1794
- function isPluginInstallContext(input) {
1795
- if (!isObject(input) || !isObject(input.options)) {
1652
+ function isPlugin(input) {
1653
+ if (!isObject(input)) {
1796
1654
  return false;
1797
1655
  }
1798
1656
  if (typeof input.name !== 'undefined' && typeof input.name !== 'string') {
1799
1657
  return false;
1800
1658
  }
1801
- return typeof input.path === 'undefined' || isPath(input.path);
1659
+ if (typeof input.install !== 'function' || input.install.length !== 1) {
1660
+ return false;
1661
+ }
1662
+ return typeof input.version === 'undefined' || typeof input.version === 'string';
1802
1663
  }
1803
1664
 
1804
1665
  function transformRouterOptions(input) {
@@ -2026,9 +1887,6 @@ class Router {
2026
1887
  return this;
2027
1888
  }
2028
1889
  use(...input) {
2029
- /* istanbul ignore next */ if (input.length === 0) {
2030
- return this;
2031
- }
2032
1890
  const modifyPath = (input)=>{
2033
1891
  if (typeof input === 'string') {
2034
1892
  return withLeadingSlash(input);
@@ -2053,33 +1911,32 @@ class Router {
2053
1911
  item.path = path || modifyPath(item.path);
2054
1912
  this.stack.push(new Layer(item));
2055
1913
  }
1914
+ if (isPlugin(item)) {
1915
+ if (path) {
1916
+ this.install(item, {
1917
+ path
1918
+ });
1919
+ } else {
1920
+ this.install(item);
1921
+ }
1922
+ }
2056
1923
  }
2057
1924
  return this;
2058
1925
  }
2059
1926
  // --------------------------------------------------
2060
- install(plugin, context) {
2061
- if (isPluginInstallContext(context)) {
2062
- const name = context.name || plugin.name;
2063
- if (context.path) {
2064
- const router = new Router({
2065
- name
2066
- });
2067
- plugin.install(router, context.options);
2068
- this.use(context.path, router);
2069
- return this;
2070
- }
2071
- plugin.install(this, context.options);
1927
+ install(plugin, context = {}) {
1928
+ const name = context.name || plugin.name;
1929
+ if (context.path) {
1930
+ const router = new Router({
1931
+ name
1932
+ });
1933
+ plugin.install(router);
1934
+ this.use(context.path, router);
2072
1935
  return this;
2073
1936
  }
2074
- plugin.install(this, context);
1937
+ plugin.install(this);
2075
1938
  return this;
2076
1939
  }
2077
- uninstall(name) {
2078
- const index = this.stack.findIndex((el)=>isRouterInstance(el) && el.name === name);
2079
- if (index !== -1) {
2080
- this.stack.splice(index, 1);
2081
- }
2082
- }
2083
1940
  // --------------------------------------------------
2084
1941
  constructor(options = {}){
2085
1942
  this['@instanceof'] = RouterSymbol;
@@ -2115,9 +1972,6 @@ exports.dispatchNodeRequest = dispatchNodeRequest;
2115
1972
  exports.dispatchRawRequest = dispatchRawRequest;
2116
1973
  exports.dispatchWebRequest = dispatchWebRequest;
2117
1974
  exports.errorHandler = errorHandler;
2118
- exports.extendRequestBody = extendRequestBody;
2119
- exports.extendRequestCookies = extendRequestCookies;
2120
- exports.extendRequestQuery = extendRequestQuery;
2121
1975
  exports.getRequestAcceptableCharset = getRequestAcceptableCharset;
2122
1976
  exports.getRequestAcceptableCharsets = getRequestAcceptableCharsets;
2123
1977
  exports.getRequestAcceptableContentType = getRequestAcceptableContentType;
@@ -2130,14 +1984,11 @@ exports.getRequestHeader = getRequestHeader;
2130
1984
  exports.getRequestHostName = getRequestHostName;
2131
1985
  exports.getRequestIP = getRequestIP;
2132
1986
  exports.getRequestProtocol = getRequestProtocol;
2133
- exports.hasRequestBody = hasRequestBody;
2134
- exports.hasRequestCookies = hasRequestCookies;
2135
- exports.hasRequestQuery = hasRequestQuery;
2136
1987
  exports.isError = isError;
2137
1988
  exports.isHandler = isHandler;
2138
1989
  exports.isLayerInstance = isLayerInstance;
2139
1990
  exports.isPath = isPath;
2140
- exports.isPluginInstallContext = isPluginInstallContext;
1991
+ exports.isPlugin = isPlugin;
2141
1992
  exports.isRequestCacheable = isRequestCacheable;
2142
1993
  exports.isResponseGone = isResponseGone;
2143
1994
  exports.isRouterInstance = isRouterInstance;
@@ -2152,29 +2003,22 @@ exports.sendRedirect = sendRedirect;
2152
2003
  exports.sendStream = sendStream;
2153
2004
  exports.sendWebBlob = sendWebBlob;
2154
2005
  exports.sendWebResponse = sendWebResponse;
2155
- exports.setRequestBody = setRequestBody;
2156
- exports.setRequestCookies = setRequestCookies;
2157
2006
  exports.setRequestEnv = setRequestEnv;
2158
2007
  exports.setRequestHeader = setRequestHeader;
2159
2008
  exports.setRequestMountPath = setRequestMountPath;
2160
2009
  exports.setRequestParam = setRequestParam;
2161
2010
  exports.setRequestParams = setRequestParams;
2162
- exports.setRequestQuery = setRequestQuery;
2163
2011
  exports.setRequestRouterPath = setRequestRouterPath;
2164
2012
  exports.setResponseCacheHeaders = setResponseCacheHeaders;
2165
2013
  exports.setResponseContentTypeByFileName = setResponseContentTypeByFileName;
2166
2014
  exports.setResponseHeaderAttachment = setResponseHeaderAttachment;
2167
2015
  exports.setResponseHeaderContentType = setResponseHeaderContentType;
2168
2016
  exports.unsetRequestEnv = unsetRequestEnv;
2169
- exports.useRequestBody = useRequestBody;
2170
- exports.useRequestCookie = useRequestCookie;
2171
- exports.useRequestCookies = useRequestCookies;
2172
2017
  exports.useRequestEnv = useRequestEnv;
2173
2018
  exports.useRequestMountPath = useRequestMountPath;
2174
2019
  exports.useRequestNegotiator = useRequestNegotiator;
2175
2020
  exports.useRequestParam = useRequestParam;
2176
2021
  exports.useRequestParams = useRequestParams;
2177
2022
  exports.useRequestPath = useRequestPath;
2178
- exports.useRequestQuery = useRequestQuery;
2179
2023
  exports.useRequestRouterPath = useRequestRouterPath;
2180
2024
  //# sourceMappingURL=index.cjs.map