xypriss 9.12.11 → 9.12.13
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/dist/cjs/src/plugins/builtin/xems/XemsBuiltinPlugin.js +9 -3
- package/dist/cjs/src/plugins/builtin/xems/XemsBuiltinPlugin.js.map +1 -1
- package/dist/cjs/src/server/FastServer/index.js +4 -6
- package/dist/cjs/src/server/FastServer/index.js.map +1 -1
- package/dist/cjs/src/xhsc/UtilsApi.js +49 -2
- package/dist/cjs/src/xhsc/UtilsApi.js.map +1 -1
- package/dist/cjs/src/xhsc/utils/ObjectUtils.js +832 -0
- package/dist/cjs/src/xhsc/utils/ObjectUtils.js.map +1 -1
- package/dist/esm/src/plugins/builtin/xems/XemsBuiltinPlugin.js +9 -3
- package/dist/esm/src/plugins/builtin/xems/XemsBuiltinPlugin.js.map +1 -1
- package/dist/esm/src/server/FastServer/index.js +4 -6
- package/dist/esm/src/server/FastServer/index.js.map +1 -1
- package/dist/esm/src/xhsc/UtilsApi.js +49 -2
- package/dist/esm/src/xhsc/UtilsApi.js.map +1 -1
- package/dist/esm/src/xhsc/utils/ObjectUtils.js +832 -1
- package/dist/esm/src/xhsc/utils/ObjectUtils.js.map +1 -1
- package/dist/index.d.ts +619 -2
- package/package.json +72 -71
- package/dist/cjs/src/middleware/XemsSessionMiddleware.js +0 -115
- package/dist/cjs/src/middleware/XemsSessionMiddleware.js.map +0 -1
- package/dist/esm/src/middleware/XemsSessionMiddleware.js +0 -113
- package/dist/esm/src/middleware/XemsSessionMiddleware.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,74 +1,75 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
2
|
+
"name": "xypriss",
|
|
3
|
+
"version": "9.12.13",
|
|
4
|
+
"description": "XyPriss is a high-performance, TypeScript-first hyper-system web framework powered by a native Go core (XHSC), featuring robust multi-tenant sandboxing, secure native file streaming, and zero Express dependencies.",
|
|
5
|
+
"author": {
|
|
6
|
+
"DEV.to": "https://dev.to/nehonix",
|
|
7
|
+
"GITHUB": "https://github.com/Nehonix-Team",
|
|
8
|
+
"name": "NEHONIX",
|
|
9
|
+
"url": "https://www.nehonix.com"
|
|
10
|
+
},
|
|
11
|
+
"license": "NOSL v2.0",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "./dist/cjs/src/index.js",
|
|
14
|
+
"module": "./dist/esm/src/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/esm/src/index.js",
|
|
19
|
+
"require": "./dist/cjs/src/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist/",
|
|
25
|
+
"shared/",
|
|
26
|
+
"scripts/",
|
|
27
|
+
"README.md",
|
|
28
|
+
"LICENSE",
|
|
29
|
+
"CONTRIBUTING.md"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"XCIS:bench.all": "bash bench/xcis-bench/scripts/bench_all.sh",
|
|
33
|
+
"XCIS:bench.xstatic": "bash simulations/XCIS/bench_static.sh",
|
|
34
|
+
"benchmark": "node benchmarks/index.js",
|
|
35
|
+
"build": "rm -rf dist && NODE_OPTIONS='--max-old-space-size=4096' xfpm exec rollup -c rollup.config.js",
|
|
36
|
+
"build:memory": "NODE_OPTIONS='--max-old-space-size=6144' xfpm exec rollup -c rollup.config.js",
|
|
37
|
+
"install-memory-cli": "node scripts/install-memory-cli.js",
|
|
38
|
+
"install-xhsc": "node scripts/postinstall-xhsc.js",
|
|
39
|
+
"postinstall": "node scripts/postinstall.js",
|
|
40
|
+
"prepublishOnly": "xfpm run build",
|
|
41
|
+
"test": "jest",
|
|
42
|
+
"test:coverage": "jest --coverage",
|
|
43
|
+
"test:watch": "jest --watch"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"ioredis": "^5.10.0",
|
|
47
|
+
"nehoid": "^2.2.1",
|
|
48
|
+
"redis": "^5.11.0",
|
|
49
|
+
"reliant-type": "^2.1.5",
|
|
50
|
+
"strulink": "^1.3.0",
|
|
51
|
+
"xss": "^1.0.15",
|
|
52
|
+
"xypriss-compression": "^1.0.6"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@rollup/plugin-commonjs": "^25.0.8",
|
|
56
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
57
|
+
"@rollup/plugin-node-resolve": "^15.3.1",
|
|
58
|
+
"@rollup/plugin-terser": "^1.0.0",
|
|
59
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
60
|
+
"@types/morgan": "^1.9.10",
|
|
61
|
+
"@types/node": "^25.9.3",
|
|
62
|
+
"@typescript-eslint/eslint-plugin": "^5.62.0",
|
|
63
|
+
"@typescript-eslint/parser": "^5.62.0",
|
|
64
|
+
"eslint": "^8.57.1",
|
|
65
|
+
"rimraf": "^4.4.1",
|
|
66
|
+
"rollup": "^4.59.0",
|
|
67
|
+
"rollup-plugin-dts": "^6.3.0",
|
|
68
|
+
"ts-node": "^10.9.2",
|
|
69
|
+
"typedoc": "^0.25.13"
|
|
70
|
+
},
|
|
71
|
+
"peerDependencies": {
|
|
72
|
+
"xypriss-security": ">=2.1.17"
|
|
21
73
|
}
|
|
22
|
-
},
|
|
23
|
-
"files": [
|
|
24
|
-
"dist/",
|
|
25
|
-
"shared/",
|
|
26
|
-
"scripts/",
|
|
27
|
-
"README.md",
|
|
28
|
-
"LICENSE",
|
|
29
|
-
"CONTRIBUTING.md"
|
|
30
|
-
],
|
|
31
|
-
"scripts": {
|
|
32
|
-
"XCIS:bench.all": "bash bench/xcis-bench/scripts/bench_all.sh",
|
|
33
|
-
"XCIS:bench.xstatic": "bash simulations/XCIS/bench_static.sh",
|
|
34
|
-
"benchmark": "node benchmarks/index.js",
|
|
35
|
-
"build": "rm -rf dist && NODE_OPTIONS='--max-old-space-size=4096' xfpm exec rollup -c rollup.config.js",
|
|
36
|
-
"build:memory": "NODE_OPTIONS='--max-old-space-size=6144' xfpm exec rollup -c rollup.config.js",
|
|
37
|
-
"install-memory-cli": "node scripts/install-memory-cli.js",
|
|
38
|
-
"install-xhsc": "node scripts/postinstall-xhsc.js",
|
|
39
|
-
"postinstall": "node scripts/postinstall.js",
|
|
40
|
-
"prepublishOnly": "xfpm run build",
|
|
41
|
-
"test": "jest",
|
|
42
|
-
"test:coverage": "jest --coverage",
|
|
43
|
-
"test:watch": "jest --watch"
|
|
44
|
-
},
|
|
45
|
-
"dependencies": {
|
|
46
|
-
"ioredis": "^5.10.0",
|
|
47
|
-
"nehoid": "^2.2.1",
|
|
48
|
-
"redis": "^5.11.0",
|
|
49
|
-
"reliant-type": "^2.1.5",
|
|
50
|
-
"strulink": "^1.3.0",
|
|
51
|
-
"xss": "^1.0.15",
|
|
52
|
-
"xypriss-compression": "^1.0.6"
|
|
53
|
-
},
|
|
54
|
-
"devDependencies": {
|
|
55
|
-
"@rollup/plugin-commonjs": "^25.0.8",
|
|
56
|
-
"@rollup/plugin-json": "^6.1.0",
|
|
57
|
-
"@rollup/plugin-node-resolve": "^15.3.1",
|
|
58
|
-
"@rollup/plugin-terser": "^1.0.0",
|
|
59
|
-
"@rollup/plugin-typescript": "^11.1.6",
|
|
60
|
-
"@types/morgan": "^1.9.10",
|
|
61
|
-
"@types/node": "^25.9.3",
|
|
62
|
-
"@typescript-eslint/eslint-plugin": "^5.62.0",
|
|
63
|
-
"@typescript-eslint/parser": "^5.62.0",
|
|
64
|
-
"eslint": "^8.57.1",
|
|
65
|
-
"rimraf": "^4.4.1",
|
|
66
|
-
"rollup": "^4.59.0",
|
|
67
|
-
"rollup-plugin-dts": "^6.3.0",
|
|
68
|
-
"ts-node": "^10.9.2",
|
|
69
|
-
"typedoc": "^0.25.13"
|
|
70
|
-
},
|
|
71
|
-
"peerDependencies": {
|
|
72
|
-
"xypriss-security": ">=2.1.17"
|
|
73
|
-
}
|
|
74
74
|
}
|
|
75
|
+
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var XemsPlugin = require('../plugins/builtin/xems/XemsPlugin.js');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* XEMS Session Middleware
|
|
7
|
-
* Implements the "Moving Target Defense" for sessions by rotating tokens on every request.
|
|
8
|
-
*/
|
|
9
|
-
function xemsSession(options) {
|
|
10
|
-
// const op;
|
|
11
|
-
const sandbox = options.sandbox || "auth-session";
|
|
12
|
-
const cookieName = options.cookieName || "xems_token";
|
|
13
|
-
const headerName = options.headerName || "x-xypriss-token";
|
|
14
|
-
const ttl = options.ttl || "15m";
|
|
15
|
-
const attachTo = options.attachTo || "session";
|
|
16
|
-
const autoRotation = options.autoRotation !== false;
|
|
17
|
-
const cookieOptions = options.cookieOptions;
|
|
18
|
-
return async (req, res, next) => {
|
|
19
|
-
// 0. Get the correct XEMS runner (handles persistence from app config)
|
|
20
|
-
const runner = XemsPlugin.xems.forApp(req.app);
|
|
21
|
-
// 1. Extract token from Cookie or Header
|
|
22
|
-
let token = req.cookies[cookieName] || req.headers[headerName];
|
|
23
|
-
// 2. Add a helper to initialize a session (xLink)
|
|
24
|
-
res.xLink = async (data, linkOptions) => {
|
|
25
|
-
let actualSandbox = sandbox;
|
|
26
|
-
let actualAttachTo = attachTo;
|
|
27
|
-
let actualTtl = ttl;
|
|
28
|
-
if (typeof linkOptions === "string") {
|
|
29
|
-
actualSandbox = linkOptions;
|
|
30
|
-
}
|
|
31
|
-
else if (linkOptions) {
|
|
32
|
-
if (linkOptions.sandbox)
|
|
33
|
-
actualSandbox = linkOptions.sandbox;
|
|
34
|
-
if (linkOptions.attachTo)
|
|
35
|
-
actualAttachTo = linkOptions.attachTo;
|
|
36
|
-
if (linkOptions.ttl)
|
|
37
|
-
actualTtl = linkOptions.ttl;
|
|
38
|
-
}
|
|
39
|
-
const newToken = await runner.createSession(actualSandbox, data, {
|
|
40
|
-
ttl: actualTtl,
|
|
41
|
-
});
|
|
42
|
-
res._xemsNewToken = newToken;
|
|
43
|
-
// Apply immediately to current response
|
|
44
|
-
res.cookie(cookieName, newToken, cookieOptions);
|
|
45
|
-
res.setHeader(headerName, newToken);
|
|
46
|
-
req[actualAttachTo] = data;
|
|
47
|
-
return newToken;
|
|
48
|
-
};
|
|
49
|
-
// 2b. Add a helper to destroy a session (xUnlink)
|
|
50
|
-
res.xUnlink = async (unlinkOptions) => {
|
|
51
|
-
let actualSandbox = sandbox;
|
|
52
|
-
let actualAttachTo = attachTo;
|
|
53
|
-
if (typeof unlinkOptions === "string") {
|
|
54
|
-
actualSandbox = unlinkOptions;
|
|
55
|
-
}
|
|
56
|
-
else if (unlinkOptions) {
|
|
57
|
-
if (unlinkOptions.sandbox)
|
|
58
|
-
actualSandbox = unlinkOptions.sandbox;
|
|
59
|
-
if (unlinkOptions.attachTo)
|
|
60
|
-
actualAttachTo = unlinkOptions.attachTo;
|
|
61
|
-
}
|
|
62
|
-
const currentToken = req.cookies[cookieName] || req.headers[headerName];
|
|
63
|
-
if (currentToken) {
|
|
64
|
-
await runner.from(actualSandbox).del(currentToken);
|
|
65
|
-
}
|
|
66
|
-
res.clearCookie(cookieName);
|
|
67
|
-
res.removeHeader(headerName);
|
|
68
|
-
req[actualAttachTo] = null;
|
|
69
|
-
};
|
|
70
|
-
if (token) {
|
|
71
|
-
try {
|
|
72
|
-
// 3. Read session data and perform rotation
|
|
73
|
-
const session = await runner.resolveSession(token, {
|
|
74
|
-
sandbox,
|
|
75
|
-
rotate: autoRotation,
|
|
76
|
-
ttl,
|
|
77
|
-
});
|
|
78
|
-
if (session) {
|
|
79
|
-
// Attach data to request
|
|
80
|
-
req[attachTo] = session.data;
|
|
81
|
-
if (autoRotation && session.newToken) {
|
|
82
|
-
// Store new token for response injection
|
|
83
|
-
res._xemsNewToken = session.newToken;
|
|
84
|
-
// Intercept response methods to inject the new token
|
|
85
|
-
const originalSend = res.send;
|
|
86
|
-
res.send = function (body) {
|
|
87
|
-
if (res._xemsNewToken) {
|
|
88
|
-
res.cookie(cookieName, res._xemsNewToken, cookieOptions);
|
|
89
|
-
res.setHeader(headerName, res._xemsNewToken);
|
|
90
|
-
}
|
|
91
|
-
return originalSend.call(this, body);
|
|
92
|
-
};
|
|
93
|
-
// Also intercept json
|
|
94
|
-
const originalJson = res.json;
|
|
95
|
-
res.json = function (data) {
|
|
96
|
-
if (res._xemsNewToken) {
|
|
97
|
-
res.cookie(cookieName, res._xemsNewToken, cookieOptions);
|
|
98
|
-
res.setHeader(headerName, res._xemsNewToken);
|
|
99
|
-
}
|
|
100
|
-
return originalJson.call(this, data);
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
catch (error) {
|
|
106
|
-
// Session might be expired or invalid, we just let it pass
|
|
107
|
-
// The dev can check if req.session exists in their handlers
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
next();
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
exports.xemsSession = xemsSession;
|
|
115
|
-
//# sourceMappingURL=XemsSessionMiddleware.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"XemsSessionMiddleware.js","sources":["../../../../src/middleware/XemsSessionMiddleware.ts"],"sourcesContent":[null],"names":["xems"],"mappings":";;;;AAQA;;;AAGG;AACG,SAAU,WAAW,CAAC,OAAkB,EAAA;;AAE1C,IAAA,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,cAAc;AACjD,IAAA,MAAM,UAAU,GAAG,OA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CAAC,GAAG,OAAO,CAAC,IAAI;AAErC,oBAAA,IAAI,YAAY,IAAI,OAAO,CAAC,QAAQ,EAAE;;AAEjC,wBAAA,GAAW,CAAC,aAAa,GAAG,OAAO,CAAC,QAAQ;;AAG7C,wBAAA,MAAM,YAAY,GAAG,GAAG,CAAC,IAAI;AAC7B,wBAAA,GAAG,CAAC,IAAI,GAAG,UAAU,IAAS,EAAA;AAC1B,4BAAA,IAAK,GAAW,CAAC,aAAa,EAAE;gCAC5B,GAAG,CAAC,MAAM,CACN,UAAU,EACT,GAAW,CAAC,aAAa,EAC1B,aAAa,CAChB;gCACD,GAAG,CAAC,SAAS,CACT,UAAU,EACT,GAAW,CAAC,aAAa,CAC7B;4BACL;4BACA,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC;AACxC,wBAAA,CAAC;;AAGD,wBAAA,MAAM,YAAY,GAAG,GAAG,CAAC,IAAI;AAC7B,wBAAA,GAAG,CAAC,IAAI,GAAG,UAAU,IAAS,EAAA;AAC1B,4BAAA,IAAK,GAAW,CAAC,aAAa,EAAE;gCAC5B,GAAG,CAAC,MAAM,CACN,UAAU,EACT,GAAW,CAAC,aAAa,EAC1B,aAAa,CAChB;gCACD,GAAG,CAAC,SAAS,CACT,UAAU,EACT,GAAW,CAAC,aAAa,CAC7B;4BACL;4BACA,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC;AACxC,wBAAA,CAAC;oBACL;gBACJ;YACJ;YAAE,OAAO,KAAK,EAAE;;;YAGhB;QACJ;AAEA,QAAA,IAAI,EAAE;AACV,IAAA,CAAC;AACL;;;;"}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { xems } from '../plugins/builtin/xems/XemsPlugin.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* XEMS Session Middleware
|
|
5
|
-
* Implements the "Moving Target Defense" for sessions by rotating tokens on every request.
|
|
6
|
-
*/
|
|
7
|
-
function xemsSession(options) {
|
|
8
|
-
// const op;
|
|
9
|
-
const sandbox = options.sandbox || "auth-session";
|
|
10
|
-
const cookieName = options.cookieName || "xems_token";
|
|
11
|
-
const headerName = options.headerName || "x-xypriss-token";
|
|
12
|
-
const ttl = options.ttl || "15m";
|
|
13
|
-
const attachTo = options.attachTo || "session";
|
|
14
|
-
const autoRotation = options.autoRotation !== false;
|
|
15
|
-
const cookieOptions = options.cookieOptions;
|
|
16
|
-
return async (req, res, next) => {
|
|
17
|
-
// 0. Get the correct XEMS runner (handles persistence from app config)
|
|
18
|
-
const runner = xems.forApp(req.app);
|
|
19
|
-
// 1. Extract token from Cookie or Header
|
|
20
|
-
let token = req.cookies[cookieName] || req.headers[headerName];
|
|
21
|
-
// 2. Add a helper to initialize a session (xLink)
|
|
22
|
-
res.xLink = async (data, linkOptions) => {
|
|
23
|
-
let actualSandbox = sandbox;
|
|
24
|
-
let actualAttachTo = attachTo;
|
|
25
|
-
let actualTtl = ttl;
|
|
26
|
-
if (typeof linkOptions === "string") {
|
|
27
|
-
actualSandbox = linkOptions;
|
|
28
|
-
}
|
|
29
|
-
else if (linkOptions) {
|
|
30
|
-
if (linkOptions.sandbox)
|
|
31
|
-
actualSandbox = linkOptions.sandbox;
|
|
32
|
-
if (linkOptions.attachTo)
|
|
33
|
-
actualAttachTo = linkOptions.attachTo;
|
|
34
|
-
if (linkOptions.ttl)
|
|
35
|
-
actualTtl = linkOptions.ttl;
|
|
36
|
-
}
|
|
37
|
-
const newToken = await runner.createSession(actualSandbox, data, {
|
|
38
|
-
ttl: actualTtl,
|
|
39
|
-
});
|
|
40
|
-
res._xemsNewToken = newToken;
|
|
41
|
-
// Apply immediately to current response
|
|
42
|
-
res.cookie(cookieName, newToken, cookieOptions);
|
|
43
|
-
res.setHeader(headerName, newToken);
|
|
44
|
-
req[actualAttachTo] = data;
|
|
45
|
-
return newToken;
|
|
46
|
-
};
|
|
47
|
-
// 2b. Add a helper to destroy a session (xUnlink)
|
|
48
|
-
res.xUnlink = async (unlinkOptions) => {
|
|
49
|
-
let actualSandbox = sandbox;
|
|
50
|
-
let actualAttachTo = attachTo;
|
|
51
|
-
if (typeof unlinkOptions === "string") {
|
|
52
|
-
actualSandbox = unlinkOptions;
|
|
53
|
-
}
|
|
54
|
-
else if (unlinkOptions) {
|
|
55
|
-
if (unlinkOptions.sandbox)
|
|
56
|
-
actualSandbox = unlinkOptions.sandbox;
|
|
57
|
-
if (unlinkOptions.attachTo)
|
|
58
|
-
actualAttachTo = unlinkOptions.attachTo;
|
|
59
|
-
}
|
|
60
|
-
const currentToken = req.cookies[cookieName] || req.headers[headerName];
|
|
61
|
-
if (currentToken) {
|
|
62
|
-
await runner.from(actualSandbox).del(currentToken);
|
|
63
|
-
}
|
|
64
|
-
res.clearCookie(cookieName);
|
|
65
|
-
res.removeHeader(headerName);
|
|
66
|
-
req[actualAttachTo] = null;
|
|
67
|
-
};
|
|
68
|
-
if (token) {
|
|
69
|
-
try {
|
|
70
|
-
// 3. Read session data and perform rotation
|
|
71
|
-
const session = await runner.resolveSession(token, {
|
|
72
|
-
sandbox,
|
|
73
|
-
rotate: autoRotation,
|
|
74
|
-
ttl,
|
|
75
|
-
});
|
|
76
|
-
if (session) {
|
|
77
|
-
// Attach data to request
|
|
78
|
-
req[attachTo] = session.data;
|
|
79
|
-
if (autoRotation && session.newToken) {
|
|
80
|
-
// Store new token for response injection
|
|
81
|
-
res._xemsNewToken = session.newToken;
|
|
82
|
-
// Intercept response methods to inject the new token
|
|
83
|
-
const originalSend = res.send;
|
|
84
|
-
res.send = function (body) {
|
|
85
|
-
if (res._xemsNewToken) {
|
|
86
|
-
res.cookie(cookieName, res._xemsNewToken, cookieOptions);
|
|
87
|
-
res.setHeader(headerName, res._xemsNewToken);
|
|
88
|
-
}
|
|
89
|
-
return originalSend.call(this, body);
|
|
90
|
-
};
|
|
91
|
-
// Also intercept json
|
|
92
|
-
const originalJson = res.json;
|
|
93
|
-
res.json = function (data) {
|
|
94
|
-
if (res._xemsNewToken) {
|
|
95
|
-
res.cookie(cookieName, res._xemsNewToken, cookieOptions);
|
|
96
|
-
res.setHeader(headerName, res._xemsNewToken);
|
|
97
|
-
}
|
|
98
|
-
return originalJson.call(this, data);
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
catch (error) {
|
|
104
|
-
// Session might be expired or invalid, we just let it pass
|
|
105
|
-
// The dev can check if req.session exists in their handlers
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
next();
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export { xemsSession };
|
|
113
|
-
//# sourceMappingURL=XemsSessionMiddleware.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"XemsSessionMiddleware.js","sources":["../../../../src/middleware/XemsSessionMiddleware.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAQA;;;AAGG;AACG,SAAU,WAAW,CAAC,OAAkB,EAAA;;AAE1C,IAAA,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,IAAI,cAAc;AACjD,IAAA,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,YAAY;AACrD,IAAA,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,iBAAiB;AAC1D,IAAA,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,IAAI,KAAK;AAChC,IAAA,MAAM,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,SAAS;AAC9C,IAAA,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,KAAK,KAAK;AAEnD,IAAA,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa;IAE3C,OAAO,OACH,GAAkB,EAClB,GAAmB,EACnB,IAAkB,KAClB;;QAEA,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAU,CAAC;;AAG1C,QAAA,IAAI,KAAK,GACL,GAAG,CAAC,OAAO,CAAC,UAAU,CAA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