session-flash 1.0.0 → 1.0.1
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/.travis.yml +5 -0
- package/lib/index.cjs +62 -0
- package/lib/{index.js → index.mjs} +54 -56
- package/package.json +19 -5
package/.travis.yml
ADDED
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { format } = require('node:util');
|
|
4
|
+
|
|
5
|
+
function flash(options = {}) {
|
|
6
|
+
const safe = options.unsafe === undefined ? true : !options.unsafe;
|
|
7
|
+
|
|
8
|
+
return function flashMiddleware(req, res, next) {
|
|
9
|
+
// if (typeof req.flash === 'function' && safe) return next();
|
|
10
|
+
if (typeof req.flash === 'function' && safe) {
|
|
11
|
+
return next();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
Object.defineProperty(req, 'flash', {
|
|
15
|
+
configurable: true,
|
|
16
|
+
enumerable: false,
|
|
17
|
+
writable: false,
|
|
18
|
+
value: flashHandler.bind(req)
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
next();
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function flashHandler(type, msg) {
|
|
26
|
+
if (!this.session) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
'req.flash() requires session middleware (express-session)'
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const store = (this.session.flash ??= Object.create(null));
|
|
33
|
+
|
|
34
|
+
// ADD message(s)
|
|
35
|
+
if (type && arguments.length > 1) {
|
|
36
|
+
if (arguments.length > 2) {
|
|
37
|
+
msg = format(...Array.prototype.slice.call(arguments, 1));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (Array.isArray(msg)) {
|
|
41
|
+
store[type] = store[type] || [];
|
|
42
|
+
store[type].push(...msg);
|
|
43
|
+
return store[type].length;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return (store[type] = store[type] || []).push(msg);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// GET messages of a type
|
|
50
|
+
if (type) {
|
|
51
|
+
const messages = store[type] || [];
|
|
52
|
+
delete store[type];
|
|
53
|
+
return messages;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// GET all messages
|
|
57
|
+
this.session.flash = Object.create(null);
|
|
58
|
+
return store;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = flash;
|
|
62
|
+
module.exports.default = flash;
|
|
@@ -1,56 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return store;
|
|
56
|
-
}
|
|
1
|
+
import { format } from 'node:util';
|
|
2
|
+
|
|
3
|
+
export default function flash(options = {}) {
|
|
4
|
+
const safe = options.unsafe === undefined ? true : !options.unsafe;
|
|
5
|
+
|
|
6
|
+
return function flashMiddleware(req, res, next) {
|
|
7
|
+
if (req.flash && safe) return next();
|
|
8
|
+
|
|
9
|
+
Object.defineProperty(req, 'flash', {
|
|
10
|
+
configurable: true,
|
|
11
|
+
enumerable: false,
|
|
12
|
+
writable: false,
|
|
13
|
+
value: flashHandler.bind(req)
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
next();
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function flashHandler(type, msg) {
|
|
21
|
+
if (!this.session) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
'req.flash() requires session middleware (express-session)'
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const store = (this.session.flash ??= Object.create(null));
|
|
28
|
+
|
|
29
|
+
// ADD message(s)
|
|
30
|
+
if (type && arguments.length > 1) {
|
|
31
|
+
if (arguments.length > 2) {
|
|
32
|
+
msg = format(...Array.prototype.slice.call(arguments, 1));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (Array.isArray(msg)) {
|
|
36
|
+
store[type] = store[type] || [];
|
|
37
|
+
store[type].push(...msg);
|
|
38
|
+
return store[type].length;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return (store[type] = store[type] || []).push(msg);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// GET messages of a type
|
|
45
|
+
if (type) {
|
|
46
|
+
const messages = store[type] || [];
|
|
47
|
+
delete store[type];
|
|
48
|
+
return messages;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// GET all messages
|
|
52
|
+
this.session.flash = Object.create(null);
|
|
53
|
+
return store;
|
|
54
|
+
}
|
package/package.json
CHANGED
|
@@ -1,18 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "session-flash",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Flash message middleware for express.",
|
|
5
|
-
"
|
|
5
|
+
"main": "./lib/index.cjs",
|
|
6
|
+
"module": "./lib/index.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
"require": "./lib/index.cjs",
|
|
9
|
+
"import": "./lib/index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"session-flash",
|
|
13
|
+
"express",
|
|
14
|
+
"flash",
|
|
15
|
+
"messages"
|
|
16
|
+
],
|
|
6
17
|
"author": {
|
|
7
18
|
"name": "Andrew Khabweri",
|
|
8
19
|
"email": "andrewkhabweri@gmail.com"
|
|
9
20
|
},
|
|
10
21
|
"license": "MIT",
|
|
11
|
-
"
|
|
12
|
-
"
|
|
22
|
+
"dependencies": {},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"vows": "0.8.x"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
|
|
13
28
|
},
|
|
14
29
|
"engines": {
|
|
15
30
|
"node": ">= 18"
|
|
16
31
|
}
|
|
17
|
-
|
|
18
32
|
}
|