slower 1.1.8 → 1.1.10

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/lib/router.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const http = require('http');
2
2
  const fs = require('fs');
3
- const { noop, slugify, isSparseEqual, last, renderDynamicHTML } = require('./utils');
3
+ const { noop, slugify, isSparseEqual, last, renderDynamicHTML, setSocketLocals } = require('./utils');
4
4
 
5
5
  class Route {
6
6
  constructor (path, type, callback) {
@@ -270,7 +270,17 @@ class SlowerRouter {
270
270
  return this.allowedMethods;
271
271
  }
272
272
 
273
- start = function (port = 8080, callback) {
273
+ // Starts the server - listening at <host>:<port>
274
+ /**
275
+ * @category Router
276
+ * @param {Number} port The port number the server will listen to.
277
+ * @param {String} host The host's network interface address the server will listen into (use a falsy value or '0.0.0.0' for listening on all).
278
+ * @param {Function} methods The methods that are allowed by the application. Methods that do not conform with standards are ignored.
279
+ * @returns {Object} The AllowedMethods Array object.
280
+ * @example <caption> Allowing only GET and POST:</caption>
281
+ * app.setAllowedMethods(['GET', 'POST']);
282
+ */
283
+ start = function (port = 8080, host = undefined, callback = () => {}) {
274
284
  let routes = this.routes;
275
285
  let middle = this.middleware;
276
286
  let fallback = this.fallback;
@@ -279,6 +289,8 @@ class SlowerRouter {
279
289
 
280
290
  let server = http.createServer(function (req, res) {
281
291
  try {
292
+ // Sets local req.session object
293
+ setSocketLocals(req);
282
294
  // Runs all middlewares
283
295
  for (let i = 0; i < middle.length; i++) { middle[i](req, res); }
284
296
  // Only respond to allowed methods with callbacks, else, use the default empty response.
@@ -307,7 +319,8 @@ class SlowerRouter {
307
319
  }
308
320
  });
309
321
  callback(server);
310
- return server.listen(port);
322
+ if (!host) host = undefined; // Turn falsy values into undefined, for default behaviour
323
+ return server.listen(port, host);
311
324
  }
312
325
  }
313
326
 
package/lib/utils.js CHANGED
@@ -69,6 +69,23 @@ const renderDynamicHTML = (html, patterns) => {
69
69
  return template;
70
70
  }
71
71
 
72
+ const setSocketLocals = (socket) => {
73
+ socket.session = {};
74
+ socket.session.port = socket.socket.localPort;
75
+ socket.session.rport = socket.socket.remotePort;
76
+ socket.session.host = (
77
+ socket.socket.localAddress.startsWith('::') ?
78
+ socket.socket.localAddress.substring(
79
+ socket.socketlocalAddress.indexOf(':',2)+1
80
+ ) : socket.socket.localAddress);
81
+ socket.session.rhost = (
82
+ socket.socket.remoteAddress.startsWith('::') ?
83
+ socket.socket.remoteAddress.substring(
84
+ socket.socket.remoteAddress.indexOf(':',2)+1
85
+ ) : socket.socket.remoteAddress);
86
+ return socket;
87
+ }
88
+
72
89
  const toBool = [() => true, () => false];
73
90
 
74
- module.exports = { noop, slugify, isSparseEqual, toBool, last, renderDynamicHTML };
91
+ module.exports = { noop, slugify, isSparseEqual, toBool, last, renderDynamicHTML, setSocketLocals };
package/package.json CHANGED
@@ -1,10 +1,9 @@
1
1
  {
2
2
  "name": "slower",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "directories": {
7
- "doc": "doc",
8
7
  "lib": "lib"
9
8
  },
10
9
  "scripts": {
package/readme.md CHANGED
@@ -41,8 +41,26 @@ app.setFallback((req, res) => {
41
41
  res.end();
42
42
  });
43
43
 
44
- app.start(port, () => {
44
+ // Start app listening on all interfaces (0.0.0.0)
45
+ app.start(port, null, () => {
45
46
  console.log(`Running on localhost:${port}`);
46
47
  console.log(app);
47
48
  });
48
- ```
49
+ ```
50
+ ### API modifications on 'net.Socket' instances:
51
+ - The API modifies every ```net.Socket``` instance BEFORE it is passed
52
+ to ```app.connectionListener```. This means that all events receiving
53
+ a socket will receive the modified socket instead.
54
+ - The modifications adds the following properties to the socket instance:
55
+ ```
56
+ <socket>.session: Object => A container for persistent data appended to sockets
57
+ <socket>.session.port: Number => The local port number
58
+ <socket>.session.rport: Number => The remote port number
59
+ <socket>.session.host: String => The local host interface address
60
+ <socket>.session.rhost: String => The remote host interface address
61
+ ```
62
+ - It is possible to use the ```socket.session``` object to append data that will persist
63
+ during the lifetime of a single connection. Useful for keeping short-life local variables.
64
+
65
+ - In HTTP ```http.IncomingMessage``` instances, the 'socket' instance is found over 'request'.
66
+ So, considering the common callback of ```(req, res)```, the session container will be ```req.session```