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 +6 -0
- package/lib/index.js +55 -56
- package/package.json +2 -2
- package/tsconfig.json +0 -25
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
|
-
|
|
7
|
-
options = options || {};
|
|
8
|
-
const unsafe = options.unsafe === true;
|
|
3
|
+
const { format } = require('util');
|
|
9
4
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
16
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
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
|
-
|
|
41
|
-
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
52
|
+
return (flashStore[type] ||= []).push(msg);
|
|
53
|
+
}
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
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
|
-
}
|