routup 3.3.0 → 4.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.
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import type { RequestListener } from 'node:http';
3
2
  import type { Request } from '../../request';
4
3
  import type { Response } from '../../response';
@@ -12,7 +12,7 @@ export declare class Handler implements Dispatcher {
12
12
  protected _method: MethodName | undefined;
13
13
  constructor(handler: HandlerConfig);
14
14
  get type(): "error" | "core";
15
- get path(): Path | undefined;
15
+ get path(): string | undefined;
16
16
  get method(): MethodName | undefined;
17
17
  dispatch(event: DispatchEvent): Promise<void>;
18
18
  matchPath(path: string): boolean;
package/dist/index.cjs CHANGED
@@ -64,55 +64,6 @@ function isRequestCacheable(req, modifiedTime) {
64
64
  return new Date(modifiedSince) >= modifiedTime;
65
65
  }
66
66
 
67
- const envSymbol = Symbol.for('ReqEnv');
68
- function setRequestEnv(req, key, value) {
69
- if (envSymbol in req) {
70
- if (typeof key === 'object') {
71
- if (value) {
72
- req[envSymbol] = smob.merge(req[envSymbol], key);
73
- } else {
74
- req[envSymbol] = key;
75
- }
76
- } else {
77
- req[envSymbol][key] = value;
78
- }
79
- return;
80
- }
81
- if (typeof key === 'object') {
82
- req[envSymbol] = key;
83
- return;
84
- }
85
- req[envSymbol] = {
86
- [key]: value
87
- };
88
- }
89
- function useRequestEnv(req, key) {
90
- if (envSymbol in req) {
91
- if (typeof key === 'string') {
92
- return req[envSymbol][key];
93
- }
94
- return req[envSymbol];
95
- }
96
- if (typeof key === 'string') {
97
- return undefined;
98
- }
99
- return {};
100
- }
101
- function unsetRequestEnv(req, key) {
102
- if (envSymbol in req) {
103
- if (smob.hasOwnProperty(req[envSymbol], key)) {
104
- delete req[envSymbol][key];
105
- }
106
- }
107
- }
108
-
109
- function getRequestHeader(req, name) {
110
- return req.headers[name];
111
- }
112
- function setRequestHeader(req, name, value) {
113
- req.headers[name] = value;
114
- }
115
-
116
67
  /*
117
68
  Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas
118
69
  that are within a single set-cookie field-value, such as in the Expires portion.
@@ -188,6 +139,12 @@ function setRequestHeader(req, name, value) {
188
139
  function isObject(item) {
189
140
  return !!item && typeof item === 'object' && !Array.isArray(item);
190
141
  }
142
+ function setProperty(record, property, value) {
143
+ record[property] = value;
144
+ }
145
+ function getProperty(req, property) {
146
+ return req[property];
147
+ }
191
148
 
192
149
  /**
193
150
  * Determine if object is a Stats object.
@@ -324,7 +281,7 @@ function basename(input, extension) {
324
281
  if (!lastSegment) {
325
282
  return input;
326
283
  }
327
- return extension && lastSegment.endsWith(extension) ? lastSegment.slice(0, -extension.length) : lastSegment;
284
+ return lastSegment;
328
285
  }
329
286
 
330
287
  function isPromise(p) {
@@ -380,12 +337,66 @@ function isWebResponse(input) {
380
337
  return typeof Response !== 'undefined' && input instanceof Response;
381
338
  }
382
339
 
383
- const NegotiatorSymbol = Symbol.for('ReqNegotiator');
340
+ const symbol$4 = Symbol.for('ReqEnv');
341
+ function setRequestEnv(req, key, value) {
342
+ const propertyValue = getProperty(req, symbol$4);
343
+ if (propertyValue) {
344
+ if (typeof key === 'object') {
345
+ if (value) {
346
+ setProperty(req, symbol$4, smob.merge(propertyValue, key));
347
+ } else {
348
+ setProperty(req, symbol$4, key);
349
+ }
350
+ } else {
351
+ propertyValue[key] = value;
352
+ setProperty(req, symbol$4, propertyValue);
353
+ }
354
+ return;
355
+ }
356
+ if (typeof key === 'object') {
357
+ setProperty(req, symbol$4, key);
358
+ return;
359
+ }
360
+ setProperty(req, symbol$4, {
361
+ [key]: value
362
+ });
363
+ }
364
+ function useRequestEnv(req, key) {
365
+ const propertyValue = getProperty(req, symbol$4);
366
+ if (propertyValue) {
367
+ if (typeof key !== 'undefined') {
368
+ return propertyValue[key];
369
+ }
370
+ return propertyValue;
371
+ }
372
+ if (typeof key !== 'undefined') {
373
+ return undefined;
374
+ }
375
+ return {};
376
+ }
377
+ function unsetRequestEnv(req, key) {
378
+ const propertyValue = getProperty(req, symbol$4);
379
+ if (smob.hasOwnProperty(propertyValue, key)) {
380
+ delete propertyValue[key];
381
+ }
382
+ }
383
+
384
+ function getRequestHeader(req, name) {
385
+ return req.headers[name];
386
+ }
387
+ function setRequestHeader(req, name, value) {
388
+ req.headers[name] = value;
389
+ }
390
+
391
+ const symbol$3 = Symbol.for('ReqNegotiator');
384
392
  function useRequestNegotiator(req) {
385
- if (NegotiatorSymbol in req) {
386
- return req[NegotiatorSymbol];
393
+ let value = getProperty(req, symbol$3);
394
+ if (value) {
395
+ return value;
387
396
  }
388
- return new Negotiator(req);
397
+ value = new Negotiator(req);
398
+ setProperty(req, symbol$3, value);
399
+ return value;
389
400
  }
390
401
 
391
402
  function getRequestAcceptableContentTypes(req) {
@@ -513,13 +524,10 @@ function findRouterOption(key, path) {
513
524
 
514
525
  const routerSymbol = Symbol.for('ReqRouterID');
515
526
  function setRequestRouterPath(req, path) {
516
- req[routerSymbol] = path;
527
+ setProperty(req, routerSymbol, path);
517
528
  }
518
529
  function useRequestRouterPath(req) {
519
- if (routerSymbol in req) {
520
- return req[routerSymbol];
521
- }
522
- return undefined;
530
+ return getProperty(req, routerSymbol);
523
531
  }
524
532
 
525
533
  function getRequestHostName(req, options) {
@@ -564,32 +572,23 @@ function getRequestIP(req, options) {
564
572
  return addrs[addrs.length - 1];
565
573
  }
566
574
 
567
- const ReqMountPathSymbol = Symbol.for('ReqMountPath');
575
+ const symbol$2 = Symbol.for('ReqMountPath');
568
576
  function useRequestMountPath(req) {
569
- if (ReqMountPathSymbol in req) {
570
- return req[ReqMountPathSymbol];
571
- }
572
- return '/';
577
+ return getProperty(req, symbol$2) || '/';
573
578
  }
574
579
  function setRequestMountPath(req, basePath) {
575
- req[ReqMountPathSymbol] = basePath;
580
+ setProperty(req, symbol$2, basePath);
576
581
  }
577
582
 
578
- const ParamsSymbol = Symbol.for('ReqParams');
583
+ const symbol$1 = Symbol.for('ReqParams');
579
584
  function useRequestParams(req) {
580
- /* istanbul ignore next */ if ('params' in req) {
581
- return req.params;
582
- }
583
- if (ParamsSymbol in req) {
584
- return req[ParamsSymbol];
585
- }
586
- return {};
585
+ return getProperty(req, symbol$1) || getProperty(req, 'params') || {};
587
586
  }
588
587
  function useRequestParam(req, key) {
589
588
  return useRequestParams(req)[key];
590
589
  }
591
590
  function setRequestParams(req, data) {
592
- req[ParamsSymbol] = data;
591
+ setProperty(req, symbol$1, data);
593
592
  }
594
593
  function setRequestParam(req, key, value) {
595
594
  const params = useRequestParams(req);
@@ -599,18 +598,16 @@ function setRequestParam(req, key, value) {
599
598
 
600
599
  const PathSymbol = Symbol.for('ReqPath');
601
600
  function useRequestPath(req) {
602
- if ('path' in req) {
603
- return req.path;
604
- }
605
- if (PathSymbol in req) {
606
- return req[PathSymbol];
601
+ const path = getProperty(req, 'path') || getProperty(req, PathSymbol);
602
+ if (path) {
603
+ return path;
607
604
  }
608
605
  if (typeof req.url === 'undefined') {
609
606
  return '/';
610
607
  }
611
608
  const parsed = new URL(req.url, 'http://localhost/');
612
- req[PathSymbol] = parsed.pathname;
613
- return req[PathSymbol];
609
+ setProperty(req, PathSymbol, parsed.pathname);
610
+ return parsed.pathname;
614
611
  }
615
612
 
616
613
  function getRequestProtocol(req, options) {
@@ -757,18 +754,15 @@ function setResponseCacheHeaders(res, options) {
757
754
  res.setHeader('cache-control', cacheControls.join(', '));
758
755
  }
759
756
 
760
- const GoneSymbol = Symbol.for('ResGone');
757
+ const symbol = Symbol.for('ResGone');
761
758
  function isResponseGone(res) {
762
759
  if (res.headersSent || res.writableEnded) {
763
760
  return true;
764
761
  }
765
- if (GoneSymbol in res) {
766
- return res[GoneSymbol];
767
- }
768
- return false;
762
+ return getProperty(res, symbol) ?? false;
769
763
  }
770
764
  function setResponseGone(res, value) {
771
- res[GoneSymbol] = value;
765
+ setProperty(res, symbol, value);
772
766
  }
773
767
 
774
768
  function serializeEventStreamMessage(message) {
@@ -1730,12 +1724,14 @@ class PathMatcher {
1730
1724
  this.regexpKeys = [];
1731
1725
  this.path = path;
1732
1726
  this.regexpOptions = options || {};
1733
- this.regexp = pathToRegexp.pathToRegexp(path, this.regexpKeys, options);
1727
+ const regexp = pathToRegexp.pathToRegexp(path, options);
1728
+ this.regexp = regexp;
1729
+ this.regexpKeys = regexp.keys;
1734
1730
  }
1735
1731
  }
1736
1732
 
1737
1733
  function isPath(input) {
1738
- return typeof input === 'string' || input instanceof RegExp;
1734
+ return typeof input === 'string';
1739
1735
  }
1740
1736
 
1741
1737
  class Handler {
@@ -1928,15 +1924,9 @@ class Router {
1928
1924
  this.pathMatcher = undefined;
1929
1925
  return;
1930
1926
  }
1931
- if (typeof value === 'string') {
1932
- this.pathMatcher = new PathMatcher(withLeadingSlash(withoutTrailingSlash(`${value}`)), {
1933
- end: false
1934
- });
1935
- } else {
1936
- this.pathMatcher = new PathMatcher(value, {
1937
- end: false
1938
- });
1939
- }
1927
+ this.pathMatcher = new PathMatcher(withLeadingSlash(withoutTrailingSlash(`${value}`)), {
1928
+ end: false
1929
+ });
1940
1930
  }
1941
1931
  // --------------------------------------------------
1942
1932
  async executePipelineStep(context) {
@@ -2158,11 +2148,7 @@ class Router {
2158
2148
  for(let i = 0; i < input.length; i++){
2159
2149
  const item = input[i];
2160
2150
  if (isPath(item)) {
2161
- if (typeof item === 'string') {
2162
- path = withLeadingSlash(item);
2163
- } else {
2164
- path = item;
2165
- }
2151
+ path = withLeadingSlash(item);
2166
2152
  continue;
2167
2153
  }
2168
2154
  if (isRouterInstance(item)) {