slower 1.1.9 → 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) {
@@ -289,6 +289,8 @@ class SlowerRouter {
289
289
 
290
290
  let server = http.createServer(function (req, res) {
291
291
  try {
292
+ // Sets local req.session object
293
+ setSocketLocals(req);
292
294
  // Runs all middlewares
293
295
  for (let i = 0; i < middle.length; i++) { middle[i](req, res); }
294
296
  // Only respond to allowed methods with callbacks, else, use the default empty response.
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,6 +1,6 @@
1
1
  {
2
2
  "name": "slower",
3
- "version": "1.1.9",
3
+ "version": "1.1.10",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "directories": {
package/readme.md CHANGED
@@ -46,4 +46,21 @@ app.start(port, null, () => {
46
46
  console.log(`Running on localhost:${port}`);
47
47
  console.log(app);
48
48
  });
49
- ```
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```