session-flash 1.0.2 → 1.0.4

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
@@ -35,6 +35,12 @@ app.use(session({
35
35
 
36
36
  app.use(flash());
37
37
 
38
+ //HTML escaping enabled by default
39
+ //Disable escaping only if you trust input:
40
+
41
+ app.use(flash({ unsafe: true }));
42
+
43
+
38
44
  ```
39
45
 
40
46
  With the `flash` middleware in place, all requests will have a `req.flash()` function
package/lib/index.js CHANGED
@@ -1,66 +1,65 @@
1
- /**
2
- * Simple session flash middleware
3
- * Requires express-session to be set up first
4
- */
1
+ 'use strict';
5
2
 
6
- module.exports = function sessionFlash(options) {
7
- options = options || {};
8
- const unsafe = options.unsafe === true;
3
+ const { format } = require('util');
9
4
 
10
- return function (req, res, next) {
11
- if (!req.session) {
12
- throw new Error("session-flash requires express-session");
13
- }
5
+ /**
6
+ * Expose `flash()` function on requests.
7
+ *
8
+ * @param {Object} options
9
+ * @return {Function}
10
+ * @api public
11
+ */
12
+ module.exports = function flash(options = {}) {
13
+ const safe = options.unsafe === undefined ? true : !options.unsafe;
14
14
 
15
- if (!req.session.flash) {
16
- req.session.flash = {};
17
- }
15
+ return function flashMiddleware(req, res, next) {
16
+ if (req.flash && safe) return next();
17
+ req.flash = _flash;
18
+ next();
19
+ };
20
+ };
18
21
 
19
- /**
20
- * flash()
21
- * flash(type)
22
- * flash(type, message)
23
- * flash(type, format, ...args)
24
- */
25
- req.flash = function (type, message) {
26
- // flash() → return all messages
27
- if (arguments.length === 0) {
28
- const flash = req.session.flash;
29
- req.session.flash = {};
30
- return flash;
31
- }
22
+ /**
23
+ * Queue or retrieve flash messages.
24
+ *
25
+ * @param {String} type
26
+ * @param {String|Array} msg
27
+ * @return {Array|Object|Number}
28
+ * @api public
29
+ */
30
+ function _flash(type, msg) {
31
+ if (!this.session) {
32
+ throw new Error('req.flash() requires sessions');
33
+ }
32
34
 
33
- // flash(type) return messages for type
34
- if (arguments.length === 1) {
35
- const messages = req.session.flash[type] || [];
36
- delete req.session.flash[type];
37
- return messages;
38
- }
35
+ const flashStore = (this.session.flash ||= {});
39
36
 
40
- // flash(type, message | format, ...args)
41
- let msg;
37
+ // SET
38
+ if (type && msg !== undefined) {
39
+ // Support util.format
40
+ if (arguments.length > 2) {
41
+ msg = format.apply(null, Array.prototype.slice.call(arguments, 1));
42
+ }
42
43
 
43
- if (typeof message === "string" && arguments.length > 2) {
44
- // format string with args
45
- const args = Array.prototype.slice.call(arguments, 2);
46
- msg = message.replace(/%s/g, () => args.shift());
47
- } else {
48
- msg = message;
49
- }
44
+ // Support array of messages
45
+ if (Array.isArray(msg)) {
46
+ msg.forEach(val => {
47
+ (flashStore[type] ||= []).push(val);
48
+ });
49
+ return flashStore[type].length;
50
+ }
50
51
 
51
- if (!Array.isArray(req.session.flash[type])) {
52
- req.session.flash[type] = [];
53
- }
52
+ return (flashStore[type] ||= []).push(msg);
53
+ }
54
54
 
55
- if (Array.isArray(msg)) {
56
- req.session.flash[type].push.apply(req.session.flash[type], msg);
57
- return msg.length;
58
- } else {
59
- req.session.flash[type].push(msg);
60
- return 1;
61
- }
62
- };
55
+ // GET by type
56
+ if (type) {
57
+ const messages = flashStore[type] || [];
58
+ delete flashStore[type];
59
+ return messages;
60
+ }
63
61
 
64
- next();
65
- };
66
- };
62
+ // GET all
63
+ this.session.flash = {};
64
+ return flashStore;
65
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "session-flash",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Flash message middleware for express.",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -22,7 +22,7 @@
22
22
  "devDependencies": {
23
23
  "@types/node": "^25.0.3",
24
24
  "typescript": "^5.9.3",
25
- "vows": "0.8.x"
25
+ "vows": "^0.8.3"
26
26
  },
27
27
  "scripts": {
28
28
  "test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js",
package/tsconfig.json DELETED
@@ -1,25 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "allowJs": true,
4
- "checkJs": false,
5
- "declaration": true,
6
- "emitDeclarationOnly": true,
7
-
8
- "rootDir": ".",
9
- "outDir": ".",
10
-
11
- "esModuleInterop": true,
12
- "moduleResolution": "node",
13
- "target": "ES2020",
14
- "module": "CommonJS",
15
-
16
- "types": ["node"],
17
- "skipLibCheck": true
18
- },
19
- "include": [
20
- "lib/index.js"
21
- ],
22
- "exclude": [
23
- "node_modules"
24
- ]
25
- }