slower 2.1.4 → 2.1.7

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "path-to-regexp": "^6.2.2"
4
4
  },
5
5
  "name": "slower",
6
- "version": "2.1.4",
6
+ "version": "2.1.7",
7
7
  "main": "index.js",
8
8
  "devDependencies": {},
9
9
  "scripts": {
package/src/decorators.js CHANGED
@@ -191,6 +191,9 @@ function setupRequest (request) {
191
191
  host: utils.normalizeAddress(request.socket.localAddress),
192
192
  rhost: utils.normalizeAddress(request.socket.remoteAddress)
193
193
  };
194
+
195
+ // Set express-compatible locals
196
+ request.ip = request.session.rhost;
194
197
 
195
198
 
196
199
  /**
package/src/slower.js CHANGED
@@ -34,20 +34,24 @@ class SlowerRouter {
34
34
  this.layers = new Map();
35
35
 
36
36
  // Create basic route shortcuts
37
- // get(), post(), put(), delete()
38
- // They can be used with either 1 or 2 params
37
+ // get(), post(), put(), delete(), ...
38
+ // They can be used with either 1 or more params
39
39
  /**
40
- * app.get({path}, {handler}) -> apply handler for a specific route
40
+ * app.get({path}, {handler}) -> apply handlers for a specific route
41
+ * app.get({path}, {handler}, {handler}, ...) -> apply handlers for a specific route
41
42
  * or
42
- * app.get({handler}) -> apply handler for any route for a specific method
43
+ * app.get({handler}) -> apply handlers for any route for a specific method
44
+ * app.get({handler}, {handler}, ...) -> apply handlers for any route for a specific method
43
45
  *
44
46
  */
45
47
  for (let verb of HTTP_VERBS) {
46
48
  this.layers.set(verb, new Map());
47
- this[verb] = function (path, callback) {
49
+ this[verb] = function (path, ...callbacks) {
48
50
  if (typeof path === 'function')
49
- return this.#setRoute(verb, MATCH_ANY, path);
50
- return this.#setRoute(verb, path, callback);
51
+ this.#setRoute(verb, MATCH_ANY, path);
52
+ for (let callback of callbacks)
53
+ this.#setRoute(verb, path, callback);
54
+ return this;
51
55
  };
52
56
  }
53
57
 
@@ -111,7 +115,7 @@ class SlowerRouter {
111
115
  * Create a middleware for all HTTP methods, for a specific path
112
116
  * @overload
113
117
  * @param {String} path
114
- * @param {Function} handler
118
+ * @param {...Function} handlers
115
119
  * @returns {SlowerRouter}
116
120
  * @example Applies a middleware for all methods, for a specific path
117
121
  * app.all('/img/:id', (req, res, next) => {console.log(...); next()})
@@ -121,7 +125,7 @@ class SlowerRouter {
121
125
  * @info This is the same as using "app[method]({callback})", without a path. Like: app.get(() => {});
122
126
  * @overload
123
127
  * @param {string} verb (one of the HTTP verbs)
124
- * @param {Function} handler
128
+ * @param {...Function} handlers
125
129
  * @returns {SlowerRouter}
126
130
  * @example Applies a middleware for all GET requests
127
131
  * app.all('get', (req, res, next) => {console.log(...); next()})
@@ -129,28 +133,39 @@ class SlowerRouter {
129
133
  /**
130
134
  * Create a global middleware (all paths and all HTTP methods)
131
135
  * @overload
132
- * @param {Function} handler
136
+ * @param {...Function} handlers
133
137
  * @returns {SlowerRouter}
134
138
  * @example Applies a middleware for all HTTP requests (of any method)
135
139
  * app.all((req, res, next) => {console.log(...); next()})
136
140
  */
137
- all (path, handler) {
138
- // function signature: app.all({METHOD}, {HANDLER})
141
+ all (path, ...handlers) {
142
+
143
+ // function signature: app.all({METHOD}, ...{HANDLERS})
139
144
  // example: app.all('post', () => {});
140
145
  if (typeof path === 'string' && HTTP_VERBS.includes(path.toLowerCase()))
141
- this.#setRoute(path, MATCH_ANY, handler);
146
+ for (let handler of handlers)
147
+ this.#setRoute(path, MATCH_ANY, handler);
148
+
142
149
 
143
- // function signature: app.all({PATH}, {HANDLER})
150
+ // function signature: app.all({PATH}, ...{HANDLERS})
144
151
  // example: app.all('/api', () => {});
145
152
  else if (typeof path === 'string')
146
- for (let verb of HTTP_VERBS) this.#setRoute(verb, path, handler);
147
-
148
- // function signature: app.all({HANDLER})
153
+ for (let handler of handlers)
154
+ for (let verb of HTTP_VERBS)
155
+ this.#setRoute(verb, path, handler);
156
+
157
+ // function signature: app.all(...{HANDLERS})
149
158
  // example: app.all(() => {});
150
159
  else if (typeof path === 'function')
151
- for (let verb of HTTP_VERBS) this.#setRoute(verb, MATCH_ANY, path);
160
+ for (let verb of HTTP_VERBS) {
161
+ this.#setRoute(verb, MATCH_ANY, path);
162
+ for (let handler of handlers)
163
+ this.#setRoute(verb, MATCH_ANY, handler);
164
+ }
152
165
 
166
+
153
167
  else throw new Error('<SlowerRouter>.use :: "handler" parameter must be of type Function');
168
+
154
169
  return this;
155
170
  }
156
171
  // Just a more comprehensive call to app.all for defining middlewares
@@ -251,11 +266,34 @@ class SlowerSubRouter {
251
266
  this.METHODS = HTTP_VERBS;
252
267
  this.layers = new Array();
253
268
 
269
+ // OLD version: (Do not delete until new version is properly tested)
270
+ // Create basic route shortcuts
271
+ // get(), post(), put(), delete()
272
+ // for (let verb of HTTP_VERBS) {
273
+ // this[verb] = function (path, callback) {
274
+ // return this.#setRoute(verb, path, callback);
275
+ // };
276
+ // }
277
+
278
+
254
279
  // Create basic route shortcuts
255
- // get(), post(), put(), delete()
280
+ // get(), post(), put(), delete(), ...
281
+ // They can be used with either 1 or more params
282
+ /**
283
+ * app.get({path}, {handler}) -> apply handlers for a specific route
284
+ * app.get({path}, {handler}, {handler}, ...) -> apply handlers for a specific route
285
+ * or
286
+ * app.get({handler}) -> apply handlers for any route for a specific method
287
+ * app.get({handler}, {handler}, ...) -> apply handlers for any route for a specific method
288
+ *
289
+ */
256
290
  for (let verb of HTTP_VERBS) {
257
- this[verb] = function (path, callback) {
258
- return this.#setRoute(verb, path, callback);
291
+ this[verb] = function (path, ...callbacks) {
292
+ if (typeof path === 'function')
293
+ this.#setRoute(verb, MATCH_ANY, path);
294
+ for (let callback of callbacks)
295
+ this.#setRoute(verb, path, callback);
296
+ return this;
259
297
  };
260
298
  }
261
299
  }
@@ -271,12 +309,62 @@ class SlowerSubRouter {
271
309
  return this;
272
310
  }
273
311
 
274
- all (path, handler) {
275
- if (typeof path === 'string')
276
- for (let verb of HTTP_VERBS) this.#setRoute(verb, path, handler);
277
- else if (typeof path !== 'function')
278
- throw new Error('<SlowerSubRouter>.use :: "handler" parameter must be of type Function');
279
- else for (let verb of HTTP_VERBS) this.#setRoute(verb, '/(.{0,})', path);
312
+ // Add middleware
313
+ /**
314
+ * Create a middleware for all HTTP methods, for a specific path
315
+ * @overload
316
+ * @param {String} path
317
+ * @param {...Function} handlers
318
+ * @returns {SlowerRouter}
319
+ * @example Applies a middleware for all methods, for a specific path
320
+ * app.all('/img/:id', (req, res, next) => {console.log(...); next()})
321
+ */
322
+ /**
323
+ * Create a global middleware for a specific verb (all paths for that method)
324
+ * @info This is the same as using "app[method]({callback})", without a path. Like: app.get(() => {});
325
+ * @overload
326
+ * @param {string} verb (one of the HTTP verbs)
327
+ * @param {...Function} handlers
328
+ * @returns {SlowerRouter}
329
+ * @example Applies a middleware for all GET requests
330
+ * app.all('get', (req, res, next) => {console.log(...); next()})
331
+ */
332
+ /**
333
+ * Create a global middleware (all paths and all HTTP methods)
334
+ * @overload
335
+ * @param {...Function} handlers
336
+ * @returns {SlowerRouter}
337
+ * @example Applies a middleware for all HTTP requests (of any method)
338
+ * app.all((req, res, next) => {console.log(...); next()})
339
+ */
340
+ all (path, ...handlers) {
341
+
342
+ // function signature: app.all({METHOD}, ...{HANDLERS})
343
+ // example: app.all('post', () => {});
344
+ if (typeof path === 'string' && HTTP_VERBS.includes(path.toLowerCase()))
345
+ for (let handler of handlers)
346
+ this.#setRoute(path, MATCH_ANY, handler);
347
+
348
+
349
+ // function signature: app.all({PATH}, ...{HANDLERS})
350
+ // example: app.all('/api', () => {});
351
+ else if (typeof path === 'string')
352
+ for (let handler of handlers)
353
+ for (let verb of HTTP_VERBS)
354
+ this.#setRoute(verb, path, handler);
355
+
356
+ // function signature: app.all(...{HANDLERS})
357
+ // example: app.all(() => {});
358
+ else if (typeof path === 'function')
359
+ for (let verb of HTTP_VERBS) {
360
+ this.#setRoute(verb, MATCH_ANY, path);
361
+ for (let handler of handlers)
362
+ this.#setRoute(verb, MATCH_ANY, handler);
363
+ }
364
+
365
+
366
+ else throw new Error('<SlowerSubRouter>.use :: "handler" parameter must be of type Function');
367
+
280
368
  return this;
281
369
  }
282
370
  // Just a more comprehensive call to app.all for defining middlewares
@@ -321,8 +409,10 @@ class SlowerMicroRouter {
321
409
  // Create basic route shortcuts
322
410
  // get(), post(), put(), delete()
323
411
  for (let verb of HTTP_VERBS) {
324
- this[verb] = function (callback) {
325
- return this.#setRoute(verb, callback);
412
+ this[verb] = function (...callbacks) {
413
+ for (let callback of callbacks)
414
+ this.#setRoute(verb, callback);
415
+ return this;
326
416
  };
327
417
  }
328
418
  }
@@ -330,22 +420,71 @@ class SlowerMicroRouter {
330
420
  #setRoute (method, handler) {
331
421
  const path = this.path;
332
422
  if (typeof method !== 'string')
333
- throw new Error('<SlowerSubRouter>.route :: "method" parameter must be of type String');
423
+ throw new Error('<SlowerMicroRouter>.route :: "method" parameter must be of type String');
334
424
  if (typeof path !== 'string' && path?.constructor?.name !== 'RegExp')
335
- throw new Error('<SlowerSubRouter>.route :: "path" parameter must be of type Function, String or RegExp');
425
+ throw new Error('<SlowerMicroRouter>.route :: "path" parameter must be of type Function, String or RegExp');
336
426
  if (typeof handler !== 'function')
337
- throw new Error('<SlowerSubRouter>.route :: "handler" parameter must be of type Function');
427
+ throw new Error('<SlowerMicroRouter>.route :: "handler" parameter must be of type Function');
338
428
  this.layers.push({ method, path, handler });
339
429
  return this;
340
430
  }
341
431
 
342
- all (handler) {
343
- const path = this.path;
344
- if (typeof path === 'string')
345
- for (let verb of HTTP_VERBS) this.#setRoute(verb, path, handler);
346
- else if (typeof path !== 'function')
347
- throw new Error('<SlowerSubRouter>.use :: "handler" parameter must be of type Function');
348
- else for (let verb of HTTP_VERBS) this.#setRoute(verb, '/(.{0,})', path);
432
+ // Add middleware
433
+ /**
434
+ * Create a middleware for all HTTP methods, for a specific path
435
+ * @overload
436
+ * @param {String} path
437
+ * @param {...Function} handlers
438
+ * @returns {SlowerRouter}
439
+ * @example Applies a middleware for all methods, for a specific path
440
+ * app.all('/img/:id', (req, res, next) => {console.log(...); next()})
441
+ */
442
+ /**
443
+ * Create a global middleware for a specific verb (all paths for that method)
444
+ * @info This is the same as using "app[method]({callback})", without a path. Like: app.get(() => {});
445
+ * @overload
446
+ * @param {string} verb (one of the HTTP verbs)
447
+ * @param {...Function} handlers
448
+ * @returns {SlowerRouter}
449
+ * @example Applies a middleware for all GET requests
450
+ * app.all('get', (req, res, next) => {console.log(...); next()})
451
+ */
452
+ /**
453
+ * Create a global middleware (all paths and all HTTP methods)
454
+ * @overload
455
+ * @param {...Function} handlers
456
+ * @returns {SlowerRouter}
457
+ * @example Applies a middleware for all HTTP requests (of any method)
458
+ * app.all((req, res, next) => {console.log(...); next()})
459
+ */
460
+ all (path, ...handlers) {
461
+
462
+ // function signature: app.all({METHOD}, ...{HANDLERS})
463
+ // example: app.all('post', () => {});
464
+ if (typeof path === 'string' && HTTP_VERBS.includes(path.toLowerCase()))
465
+ for (let handler of handlers)
466
+ this.#setRoute(path, MATCH_ANY, handler);
467
+
468
+
469
+ // function signature: app.all({PATH}, ...{HANDLERS})
470
+ // example: app.all('/api', () => {});
471
+ else if (typeof path === 'string')
472
+ for (let handler of handlers)
473
+ for (let verb of HTTP_VERBS)
474
+ this.#setRoute(verb, path, handler);
475
+
476
+ // function signature: app.all(...{HANDLERS})
477
+ // example: app.all(() => {});
478
+ else if (typeof path === 'function')
479
+ for (let verb of HTTP_VERBS) {
480
+ this.#setRoute(verb, MATCH_ANY, path);
481
+ for (let handler of handlers)
482
+ this.#setRoute(verb, MATCH_ANY, handler);
483
+ }
484
+
485
+
486
+ else throw new Error('<SlowerMicroRouter>.use :: "handler" parameter must be of type Function');
487
+
349
488
  return this;
350
489
  }
351
490
  // Just a more comprehensive call to app.all for defining middlewares