routup 1.0.0-alpha.6 → 1.0.1

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
@@ -66,7 +66,8 @@ router.listen(3000);
66
66
 
67
67
  ## Plugins
68
68
 
69
- According to the fact that routup is a minimalistic framework, it depends on plugins to cover some
69
+ According to the fact that routup is a minimalistic framework,
70
+ it depends on [plugins](https://github.com/routup/plugins) to cover some
70
71
  typically http framework functions, which are not integrated in the main package.
71
72
 
72
73
  | Name | Description |
@@ -1,4 +1,4 @@
1
- export declare enum Method {
1
+ export declare enum MethodName {
2
2
  GET = "get",
3
3
  POST = "post",
4
4
  PUT = "put",
@@ -23,6 +23,7 @@ export declare enum HeaderName {
23
23
  COOKIE = "cookie",
24
24
  ETag = "etag",
25
25
  HOST = "host",
26
+ IF_MODIFIED_SINCE = "if-modified-since",
26
27
  IF_NONE_MATCH = "if-none-match",
27
28
  LAST_MODIFIED = "last-modified",
28
29
  LOCATION = "location",
package/dist/index.cjs CHANGED
@@ -55,12 +55,7 @@ var node_http = require('node:http');
55
55
  return weak ? `W/${tag}` : tag;
56
56
  }
57
57
 
58
- /*
59
- * Copyright (c) 2022-2023.
60
- * Author Peter Placzek (tada5hi)
61
- * For the full copyright and license information,
62
- * view the LICENSE file that was distributed with this source code.
63
- */ function isObject(item) {
58
+ function isObject(item) {
64
59
  return !!item && typeof item === 'object' && !Array.isArray(item);
65
60
  }
66
61
 
@@ -106,12 +101,7 @@ function buildTrustProxyFn(input) {
106
101
  return proxyAddr.compile(input || []);
107
102
  }
108
103
 
109
- /*
110
- * Copyright (c) 2022-2022.
111
- * Author Peter Placzek (tada5hi)
112
- * For the full copyright and license information,
113
- * view the LICENSE file that was distributed with this source code.
114
- */ function isInstance(input, name) {
104
+ function isInstance(input, name) {
115
105
  return typeof input === 'object' && input !== null && input['@instanceof'] === Symbol.for(name);
116
106
  }
117
107
 
@@ -132,12 +122,7 @@ function getCharsetForMimeType(type) {
132
122
  return undefined;
133
123
  }
134
124
 
135
- /*
136
- * Copyright (c) 2022.
137
- * Author Peter Placzek (tada5hi)
138
- * For the full copyright and license information,
139
- * view the LICENSE file that was distributed with this source code.
140
- */ function isPath(input) {
125
+ function isPath(input) {
141
126
  return typeof input === 'string' || input instanceof RegExp;
142
127
  }
143
128
 
@@ -167,12 +152,7 @@ function isPromise(p) {
167
152
  });
168
153
  }
169
154
 
170
- /*
171
- * Copyright (c) 2022-2023.
172
- * Author Peter Placzek (tada5hi)
173
- * For the full copyright and license information,
174
- * view the LICENSE file that was distributed with this source code.
175
- */ const TRAILING_SLASH_RE = /\/$|\/\?/;
155
+ const TRAILING_SLASH_RE = /\/$|\/\?/;
176
156
  function hasTrailingSlash(input = '', queryParams = false) {
177
157
  if (!queryParams) {
178
158
  return input.endsWith('/');
@@ -255,21 +235,16 @@ function getConfigOption(key) {
255
235
  return config.get(key);
256
236
  }
257
237
 
258
- /*
259
- * Copyright (c) 2022-2023.
260
- * Author Peter Placzek (tada5hi)
261
- * For the full copyright and license information,
262
- * view the LICENSE file that was distributed with this source code.
263
- */ exports.Method = void 0;
264
- (function(Method) {
265
- Method["GET"] = 'get';
266
- Method["POST"] = 'post';
267
- Method["PUT"] = 'put';
268
- Method["PATCH"] = 'patch';
269
- Method["DELETE"] = 'delete';
270
- Method["OPTIONS"] = 'options';
271
- Method["HEAD"] = 'head';
272
- })(exports.Method || (exports.Method = {}));
238
+ exports.MethodName = void 0;
239
+ (function(MethodName) {
240
+ MethodName["GET"] = 'get';
241
+ MethodName["POST"] = 'post';
242
+ MethodName["PUT"] = 'put';
243
+ MethodName["PATCH"] = 'patch';
244
+ MethodName["DELETE"] = 'delete';
245
+ MethodName["OPTIONS"] = 'options';
246
+ MethodName["HEAD"] = 'head';
247
+ })(exports.MethodName || (exports.MethodName = {}));
273
248
  exports.HeaderName = void 0;
274
249
  (function(HeaderName) {
275
250
  HeaderName["ACCEPT"] = 'accept';
@@ -287,6 +262,7 @@ exports.HeaderName = void 0;
287
262
  HeaderName["COOKIE"] = 'cookie';
288
263
  HeaderName["ETag"] = 'etag';
289
264
  HeaderName["HOST"] = 'host';
265
+ HeaderName["IF_MODIFIED_SINCE"] = 'if-modified-since';
290
266
  HeaderName["IF_NONE_MATCH"] = 'if-none-match';
291
267
  HeaderName["LAST_MODIFIED"] = 'last-modified';
292
268
  HeaderName["LOCATION"] = 'location';
@@ -357,13 +333,8 @@ function extendRequestBody(req, key, value) {
357
333
  setRequestBody(req, key, value);
358
334
  }
359
335
 
360
- /*
361
- * Copyright (c) 2022.
362
- * Author Peter Placzek (tada5hi)
363
- * For the full copyright and license information,
364
- * view the LICENSE file that was distributed with this source code.
365
- */ function isRequestCacheable(req, modifiedTime) {
366
- const modifiedSince = req.headers['if-modified-since'];
336
+ function isRequestCacheable(req, modifiedTime) {
337
+ const modifiedSince = req.headers[exports.HeaderName.IF_MODIFIED_SINCE];
367
338
  if (!modifiedSince) {
368
339
  return false;
369
340
  }
@@ -454,12 +425,7 @@ function unsetRequestEnv(req, key) {
454
425
  }
455
426
  }
456
427
 
457
- /*
458
- * Copyright (c) 2022.
459
- * Author Peter Placzek (tada5hi)
460
- * For the full copyright and license information,
461
- * view the LICENSE file that was distributed with this source code.
462
- */ function getRequestHeader(req, name) {
428
+ function getRequestHeader(req, name) {
463
429
  return req.headers[name];
464
430
  }
465
431
  function setRequestHeader(req, name, value) {
@@ -612,12 +578,7 @@ function getRequestIP(req, options) {
612
578
  return proxyAddr(req, trustProxy);
613
579
  }
614
580
 
615
- /*
616
- * Copyright (c) 2022.
617
- * Author Peter Placzek (tada5hi)
618
- * For the full copyright and license information,
619
- * view the LICENSE file that was distributed with this source code.
620
- */ const ReqMountPathSymbol = Symbol.for('ReqMountPath');
581
+ const ReqMountPathSymbol = Symbol.for('ReqMountPath');
621
582
  function useRequestMountPath(req) {
622
583
  if (ReqMountPathSymbol in req) {
623
584
  return req[ReqMountPathSymbol];
@@ -628,12 +589,7 @@ function setRequestMountPath(req, basePath) {
628
589
  req[ReqMountPathSymbol] = basePath;
629
590
  }
630
591
 
631
- /*
632
- * Copyright (c) 2022.
633
- * Author Peter Placzek (tada5hi)
634
- * For the full copyright and license information,
635
- * view the LICENSE file that was distributed with this source code.
636
- */ const ParamsSymbol = Symbol.for('ReqParams');
592
+ const ParamsSymbol = Symbol.for('ReqParams');
637
593
  function useRequestParams(req) {
638
594
  /* istanbul ignore next */ if ('params' in req) {
639
595
  return req.params;
@@ -655,15 +611,20 @@ function setRequestParam(req, key, value) {
655
611
  setRequestParams(req, params);
656
612
  }
657
613
 
614
+ const PathSymbol = Symbol.for('ReqPath');
658
615
  function useRequestPath(req) {
659
616
  if ('path' in req) {
660
617
  return req.path;
661
618
  }
619
+ if (PathSymbol in req) {
620
+ return req[PathSymbol];
621
+ }
662
622
  if (typeof req.url === 'undefined') {
663
623
  return '/';
664
624
  }
665
625
  const parsed = new node_url.URL(req.url, 'http://localhost/');
666
- return parsed.pathname;
626
+ req[PathSymbol] = parsed.pathname;
627
+ return req[PathSymbol];
667
628
  }
668
629
 
669
630
  function getRequestProtocol(req, options) {
@@ -741,12 +702,7 @@ function extendRequestQuery(req, key, value) {
741
702
  setRequestQuery(req, key, value);
742
703
  }
743
704
 
744
- /*
745
- * Copyright (c) 2022.
746
- * Author Peter Placzek (tada5hi)
747
- * For the full copyright and license information,
748
- * view the LICENSE file that was distributed with this source code.
749
- */ function setResponseCacheHeaders(res, options) {
705
+ function setResponseCacheHeaders(res, options) {
750
706
  options = options || {};
751
707
  const cacheControls = [
752
708
  'public'
@@ -761,12 +717,7 @@ function extendRequestQuery(req, key, value) {
761
717
  res.setHeader('cache-control', cacheControls.join(', '));
762
718
  }
763
719
 
764
- /*
765
- * Copyright (c) 2022.
766
- * Author Peter Placzek (tada5hi)
767
- * For the full copyright and license information,
768
- * view the LICENSE file that was distributed with this source code.
769
- */ function appendResponseHeader(res, name, value) {
720
+ function appendResponseHeader(res, name, value) {
770
721
  let header = res.getHeader(name);
771
722
  if (!header) {
772
723
  res.setHeader(name, value);
@@ -958,12 +909,7 @@ function sendCreated(res, chunk) {
958
909
  return send(res, chunk);
959
910
  }
960
911
 
961
- /*
962
- * Copyright (c) 2022.
963
- * Author Peter Placzek (tada5hi)
964
- * For the full copyright and license information,
965
- * view the LICENSE file that was distributed with this source code.
966
- */ function sendStream(res, stream, fn) {
912
+ function sendStream(res, stream, fn) {
967
913
  stream.on('open', ()=>{
968
914
  stream.pipe(res);
969
915
  });
@@ -1210,16 +1156,16 @@ class Route {
1210
1156
  }
1211
1157
  matchMethod(method) {
1212
1158
  let name = method.toLowerCase();
1213
- if (name === exports.Method.HEAD && !smob.hasOwnProperty(this.layers, name)) {
1214
- name = exports.Method.GET;
1159
+ if (name === exports.MethodName.HEAD && !smob.hasOwnProperty(this.layers, name)) {
1160
+ name = exports.MethodName.GET;
1215
1161
  }
1216
1162
  return Object.prototype.hasOwnProperty.call(this.layers, name);
1217
1163
  }
1218
1164
  // --------------------------------------------------
1219
1165
  getMethods() {
1220
1166
  const keys = Object.keys(this.layers);
1221
- if (smob.hasOwnProperty(this.layers, exports.Method.GET) && !smob.hasOwnProperty(this.layers, exports.Method.HEAD)) {
1222
- keys.push(exports.Method.HEAD);
1167
+ if (smob.hasOwnProperty(this.layers, exports.MethodName.GET) && !smob.hasOwnProperty(this.layers, exports.MethodName.HEAD)) {
1168
+ keys.push(exports.MethodName.HEAD);
1223
1169
  }
1224
1170
  return keys;
1225
1171
  }
@@ -1230,8 +1176,8 @@ class Route {
1230
1176
  return;
1231
1177
  }
1232
1178
  let name = req.method.toLowerCase();
1233
- if (name === exports.Method.HEAD && !smob.hasOwnProperty(this.layers, name)) {
1234
- name = exports.Method.GET;
1179
+ if (name === exports.MethodName.HEAD && !smob.hasOwnProperty(this.layers, name)) {
1180
+ name = exports.MethodName.GET;
1235
1181
  }
1236
1182
  const layers = this.layers[name];
1237
1183
  /* istanbul ignore next */ if (typeof layers === 'undefined' || layers.length === 0 || typeof meta.path === 'undefined') {
@@ -1275,25 +1221,25 @@ class Route {
1275
1221
  }
1276
1222
  }
1277
1223
  get(...handlers) {
1278
- return this.register(exports.Method.GET, ...handlers);
1224
+ return this.register(exports.MethodName.GET, ...handlers);
1279
1225
  }
1280
1226
  post(...handlers) {
1281
- return this.register(exports.Method.POST, ...handlers);
1227
+ return this.register(exports.MethodName.POST, ...handlers);
1282
1228
  }
1283
1229
  put(...handlers) {
1284
- return this.register(exports.Method.PUT, ...handlers);
1230
+ return this.register(exports.MethodName.PUT, ...handlers);
1285
1231
  }
1286
1232
  patch(...handlers) {
1287
- return this.register(exports.Method.PATCH, ...handlers);
1233
+ return this.register(exports.MethodName.PATCH, ...handlers);
1288
1234
  }
1289
1235
  delete(...handlers) {
1290
- return this.register(exports.Method.DELETE, ...handlers);
1236
+ return this.register(exports.MethodName.DELETE, ...handlers);
1291
1237
  }
1292
1238
  head(...handlers) {
1293
- return this.register(exports.Method.HEAD, ...handlers);
1239
+ return this.register(exports.MethodName.HEAD, ...handlers);
1294
1240
  }
1295
1241
  options(...handlers) {
1296
- return this.register(exports.Method.OPTIONS, ...handlers);
1242
+ return this.register(exports.MethodName.OPTIONS, ...handlers);
1297
1243
  }
1298
1244
  // --------------------------------------------------
1299
1245
  isStrictPath() {
@@ -1378,7 +1324,7 @@ class Router {
1378
1324
  res.end();
1379
1325
  return;
1380
1326
  }
1381
- if (req.method && req.method.toLowerCase() === exports.Method.OPTIONS) {
1327
+ if (req.method && req.method.toLowerCase() === exports.MethodName.OPTIONS) {
1382
1328
  const options = allowedMethods.map((key)=>key.toUpperCase()).join(',');
1383
1329
  res.setHeader(exports.HeaderName.ALLOW, options);
1384
1330
  send(res, options);
@@ -1427,8 +1373,8 @@ class Router {
1427
1373
  match = layer.matchPath(path);
1428
1374
  if (req.method && !layer.matchMethod(req.method)) {
1429
1375
  match = false;
1430
- if (req.method.toLowerCase() === exports.Method.OPTIONS) {
1431
- allowedMethods = smob.mergeArrays(allowedMethods, layer.getMethods(), true);
1376
+ if (req.method.toLowerCase() === exports.MethodName.OPTIONS) {
1377
+ allowedMethods = smob.distinctArray(smob.merge(allowedMethods, layer.getMethods()));
1432
1378
  }
1433
1379
  }
1434
1380
  }