@shware/security 2.5.3 → 3.0.0

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.
Files changed (225) hide show
  1. package/dist/auth/index.cjs +519 -561
  2. package/dist/auth/index.cjs.map +1 -1
  3. package/dist/auth/index.d.cts +104 -106
  4. package/dist/auth/index.d.cts.map +1 -0
  5. package/dist/auth/index.d.mts +109 -0
  6. package/dist/auth/index.d.mts.map +1 -0
  7. package/dist/auth/index.mjs +512 -536
  8. package/dist/auth/index.mjs.map +1 -1
  9. package/dist/auth/types.cjs +0 -19
  10. package/dist/auth/types.d.cts +44 -44
  11. package/dist/auth/types.d.cts.map +1 -0
  12. package/dist/auth/types.d.mts +50 -0
  13. package/dist/auth/types.d.mts.map +1 -0
  14. package/dist/auth/types.mjs +1 -1
  15. package/dist/core/index.cjs +18 -59
  16. package/dist/core/index.cjs.map +1 -1
  17. package/dist/core/index.d.cts +13 -24
  18. package/dist/core/index.d.cts.map +1 -0
  19. package/dist/core/index.d.mts +21 -0
  20. package/dist/core/index.d.mts.map +1 -0
  21. package/dist/core/index.mjs +17 -33
  22. package/dist/core/index.mjs.map +1 -1
  23. package/dist/email/schema.cjs +13 -38
  24. package/dist/email/schema.cjs.map +1 -1
  25. package/dist/email/schema.d.cts +11 -12
  26. package/dist/email/schema.d.cts.map +1 -0
  27. package/dist/email/schema.d.mts +12 -0
  28. package/dist/email/schema.d.mts.map +1 -0
  29. package/dist/email/schema.mjs +10 -11
  30. package/dist/email/schema.mjs.map +1 -1
  31. package/dist/index.cjs +18 -54
  32. package/dist/index.d.cts +10 -15
  33. package/dist/index.d.mts +10 -0
  34. package/dist/index.mjs +5 -17
  35. package/dist/oauth2/client.cjs +109 -145
  36. package/dist/oauth2/client.cjs.map +1 -1
  37. package/dist/oauth2/client.d.cts +55 -56
  38. package/dist/oauth2/client.d.cts.map +1 -0
  39. package/dist/oauth2/client.d.mts +58 -0
  40. package/dist/oauth2/client.d.mts.map +1 -0
  41. package/dist/oauth2/client.mjs +104 -116
  42. package/dist/oauth2/client.mjs.map +1 -1
  43. package/dist/oauth2/error.cjs +19 -44
  44. package/dist/oauth2/error.cjs.map +1 -1
  45. package/dist/oauth2/error.d.cts +8 -6
  46. package/dist/oauth2/error.d.cts.map +1 -0
  47. package/dist/oauth2/{error.d.ts → error.d.mts} +8 -6
  48. package/dist/oauth2/error.d.mts.map +1 -0
  49. package/dist/oauth2/error.mjs +18 -19
  50. package/dist/oauth2/error.mjs.map +1 -1
  51. package/dist/oauth2/provider/apple.cjs +75 -88
  52. package/dist/oauth2/provider/apple.cjs.map +1 -1
  53. package/dist/oauth2/provider/apple.d.cts +31 -30
  54. package/dist/oauth2/provider/apple.d.cts.map +1 -0
  55. package/dist/oauth2/provider/apple.d.mts +42 -0
  56. package/dist/oauth2/provider/apple.d.mts.map +1 -0
  57. package/dist/oauth2/provider/apple.mjs +71 -60
  58. package/dist/oauth2/provider/apple.mjs.map +1 -1
  59. package/dist/oauth2/provider/common.cjs +86 -109
  60. package/dist/oauth2/provider/common.cjs.map +1 -1
  61. package/dist/oauth2/provider/common.d.cts +19 -18
  62. package/dist/oauth2/provider/common.d.cts.map +1 -0
  63. package/dist/oauth2/provider/common.d.mts +30 -0
  64. package/dist/oauth2/provider/common.d.mts.map +1 -0
  65. package/dist/oauth2/provider/common.mjs +80 -79
  66. package/dist/oauth2/provider/common.mjs.map +1 -1
  67. package/dist/oauth2/provider/facebook.cjs +93 -109
  68. package/dist/oauth2/provider/facebook.cjs.map +1 -1
  69. package/dist/oauth2/provider/facebook.d.cts +29 -28
  70. package/dist/oauth2/provider/facebook.d.cts.map +1 -0
  71. package/dist/oauth2/provider/facebook.d.mts +45 -0
  72. package/dist/oauth2/provider/facebook.d.mts.map +1 -0
  73. package/dist/oauth2/provider/facebook.mjs +89 -81
  74. package/dist/oauth2/provider/facebook.mjs.map +1 -1
  75. package/dist/oauth2/provider/github.cjs +64 -91
  76. package/dist/oauth2/provider/github.cjs.map +1 -1
  77. package/dist/oauth2/provider/github.d.cts +52 -51
  78. package/dist/oauth2/provider/github.d.cts.map +1 -0
  79. package/dist/oauth2/provider/github.d.mts +60 -0
  80. package/dist/oauth2/provider/github.d.mts.map +1 -0
  81. package/dist/oauth2/provider/github.mjs +60 -63
  82. package/dist/oauth2/provider/github.mjs.map +1 -1
  83. package/dist/oauth2/provider/google.cjs +105 -122
  84. package/dist/oauth2/provider/google.cjs.map +1 -1
  85. package/dist/oauth2/provider/google.d.cts +34 -33
  86. package/dist/oauth2/provider/google.d.cts.map +1 -0
  87. package/dist/oauth2/provider/google.d.mts +44 -0
  88. package/dist/oauth2/provider/google.d.mts.map +1 -0
  89. package/dist/oauth2/provider/google.mjs +101 -94
  90. package/dist/oauth2/provider/google.mjs.map +1 -1
  91. package/dist/oauth2/provider/index.cjs +13 -46
  92. package/dist/oauth2/provider/index.d.cts +7 -7
  93. package/dist/oauth2/provider/index.d.mts +7 -0
  94. package/dist/oauth2/provider/index.mjs +1 -10
  95. package/dist/oauth2/provider/wechat.cjs +77 -99
  96. package/dist/oauth2/provider/wechat.cjs.map +1 -1
  97. package/dist/oauth2/provider/wechat.d.cts +28 -27
  98. package/dist/oauth2/provider/wechat.d.cts.map +1 -0
  99. package/dist/oauth2/provider/wechat.d.mts +39 -0
  100. package/dist/oauth2/provider/wechat.d.mts.map +1 -0
  101. package/dist/oauth2/provider/wechat.mjs +74 -72
  102. package/dist/oauth2/provider/wechat.mjs.map +1 -1
  103. package/dist/oauth2/provider/x.cjs +102 -125
  104. package/dist/oauth2/provider/x.cjs.map +1 -1
  105. package/dist/oauth2/provider/x.d.cts +51 -50
  106. package/dist/oauth2/provider/x.d.cts.map +1 -0
  107. package/dist/oauth2/provider/x.d.mts +69 -0
  108. package/dist/oauth2/provider/x.d.mts.map +1 -0
  109. package/dist/oauth2/provider/x.mjs +99 -103
  110. package/dist/oauth2/provider/x.mjs.map +1 -1
  111. package/dist/oauth2/types.cjs +12 -37
  112. package/dist/oauth2/types.cjs.map +1 -1
  113. package/dist/oauth2/types.d.cts +176 -180
  114. package/dist/oauth2/types.d.cts.map +1 -0
  115. package/dist/oauth2/types.d.mts +207 -0
  116. package/dist/oauth2/types.d.mts.map +1 -0
  117. package/dist/oauth2/types.mjs +12 -13
  118. package/dist/oauth2/types.mjs.map +1 -1
  119. package/dist/session/common.cjs +17 -43
  120. package/dist/session/common.cjs.map +1 -1
  121. package/dist/session/common.d.cts +4 -4
  122. package/dist/session/common.d.cts.map +1 -0
  123. package/dist/session/{common.d.ts → common.d.mts} +4 -4
  124. package/dist/session/common.d.mts.map +1 -0
  125. package/dist/session/common.mjs +14 -16
  126. package/dist/session/common.mjs.map +1 -1
  127. package/dist/session/db.cjs +88 -123
  128. package/dist/session/db.cjs.map +1 -1
  129. package/dist/session/db.d.cts +36 -36
  130. package/dist/session/db.d.cts.map +1 -0
  131. package/dist/session/db.d.mts +44 -0
  132. package/dist/session/db.d.mts.map +1 -0
  133. package/dist/session/db.mjs +84 -95
  134. package/dist/session/db.mjs.map +1 -1
  135. package/dist/session/map-session.cjs +71 -103
  136. package/dist/session/map-session.cjs.map +1 -1
  137. package/dist/session/map-session.d.cts +24 -24
  138. package/dist/session/map-session.d.cts.map +1 -0
  139. package/dist/session/map-session.d.mts +27 -0
  140. package/dist/session/map-session.d.mts.map +1 -0
  141. package/dist/session/map-session.mjs +70 -78
  142. package/dist/session/map-session.mjs.map +1 -1
  143. package/dist/session/redis.cjs +270 -319
  144. package/dist/session/redis.cjs.map +1 -1
  145. package/dist/session/redis.d.cts +51 -51
  146. package/dist/session/redis.d.cts.map +1 -0
  147. package/dist/session/redis.d.mts +58 -0
  148. package/dist/session/redis.d.mts.map +1 -0
  149. package/dist/session/redis.mjs +266 -291
  150. package/dist/session/redis.mjs.map +1 -1
  151. package/dist/session/types.cjs +0 -19
  152. package/dist/session/types.d.cts +34 -33
  153. package/dist/session/types.d.cts.map +1 -0
  154. package/dist/session/types.d.mts +45 -0
  155. package/dist/session/types.d.mts.map +1 -0
  156. package/dist/session/types.mjs +1 -1
  157. package/dist/utils/http.cjs +31 -55
  158. package/dist/utils/http.cjs.map +1 -1
  159. package/dist/utils/http.d.cts +7 -10
  160. package/dist/utils/http.d.cts.map +1 -0
  161. package/dist/utils/{http.d.ts → http.d.mts} +7 -10
  162. package/dist/utils/http.d.mts.map +1 -0
  163. package/dist/utils/http.mjs +24 -24
  164. package/dist/utils/http.mjs.map +1 -1
  165. package/dist/utils/oauth2.cjs +6 -30
  166. package/dist/utils/oauth2.cjs.map +1 -1
  167. package/dist/utils/oauth2.d.cts +3 -1
  168. package/dist/utils/oauth2.d.cts.map +1 -0
  169. package/dist/utils/oauth2.d.mts +5 -0
  170. package/dist/utils/oauth2.d.mts.map +1 -0
  171. package/dist/utils/oauth2.mjs +5 -5
  172. package/dist/utils/oauth2.mjs.map +1 -1
  173. package/dist/utils/timing.cjs +49 -70
  174. package/dist/utils/timing.cjs.map +1 -1
  175. package/dist/utils/timing.d.cts +10 -8
  176. package/dist/utils/timing.d.cts.map +1 -0
  177. package/dist/utils/timing.d.mts +15 -0
  178. package/dist/utils/timing.d.mts.map +1 -0
  179. package/dist/utils/timing.mjs +48 -45
  180. package/dist/utils/timing.mjs.map +1 -1
  181. package/dist/utils/valid.cjs +50 -71
  182. package/dist/utils/valid.cjs.map +1 -1
  183. package/dist/utils/valid.d.cts +17 -16
  184. package/dist/utils/valid.d.cts.map +1 -0
  185. package/dist/utils/valid.d.mts +28 -0
  186. package/dist/utils/valid.d.mts.map +1 -0
  187. package/dist/utils/valid.mjs +47 -44
  188. package/dist/utils/valid.mjs.map +1 -1
  189. package/package.json +22 -12
  190. package/dist/auth/index.d.ts +0 -111
  191. package/dist/auth/types.cjs.map +0 -1
  192. package/dist/auth/types.d.ts +0 -50
  193. package/dist/auth/types.mjs.map +0 -1
  194. package/dist/core/index.d.ts +0 -32
  195. package/dist/email/schema.d.ts +0 -13
  196. package/dist/index.cjs.map +0 -1
  197. package/dist/index.d.ts +0 -15
  198. package/dist/index.mjs.map +0 -1
  199. package/dist/oauth2/client.d.ts +0 -59
  200. package/dist/oauth2/provider/apple.d.ts +0 -41
  201. package/dist/oauth2/provider/common.d.ts +0 -29
  202. package/dist/oauth2/provider/facebook.d.ts +0 -44
  203. package/dist/oauth2/provider/github.d.ts +0 -59
  204. package/dist/oauth2/provider/google.d.ts +0 -43
  205. package/dist/oauth2/provider/index.cjs.map +0 -1
  206. package/dist/oauth2/provider/index.d.ts +0 -7
  207. package/dist/oauth2/provider/index.mjs.map +0 -1
  208. package/dist/oauth2/provider/wechat.d.ts +0 -38
  209. package/dist/oauth2/provider/x.d.ts +0 -68
  210. package/dist/oauth2/types.d.ts +0 -211
  211. package/dist/session/__tests__/redis.test.cjs +0 -247
  212. package/dist/session/__tests__/redis.test.cjs.map +0 -1
  213. package/dist/session/__tests__/redis.test.d.cts +0 -2
  214. package/dist/session/__tests__/redis.test.d.ts +0 -2
  215. package/dist/session/__tests__/redis.test.mjs +0 -252
  216. package/dist/session/__tests__/redis.test.mjs.map +0 -1
  217. package/dist/session/db.d.ts +0 -44
  218. package/dist/session/map-session.d.ts +0 -27
  219. package/dist/session/redis.d.ts +0 -58
  220. package/dist/session/types.cjs.map +0 -1
  221. package/dist/session/types.d.ts +0 -44
  222. package/dist/session/types.mjs.map +0 -1
  223. package/dist/utils/oauth2.d.ts +0 -3
  224. package/dist/utils/timing.d.ts +0 -13
  225. package/dist/utils/valid.d.ts +0 -27
@@ -1,328 +1,279 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/session/redis.ts
21
- var redis_exports = {};
22
- __export(redis_exports, {
23
- RedisIndexedSessionRepository: () => RedisIndexedSessionRepository,
24
- RedisKVRepository: () => RedisKVRepository
25
- });
26
- module.exports = __toCommonJS(redis_exports);
27
- var import_common = require("./common.cjs");
28
- var import_map_session = require("./map-session.cjs");
29
- var CREATION_TIME_KEY = "creationTime";
30
- var LAST_ACCESSED_TIME_KEY = "lastAccessedTime";
31
- var MAX_INACTIVE_INTERVAL_KEY = "maxInactiveInterval";
32
- var ATTRIBUTE_PREFIX = "sessionAttr:";
33
- var SESSION_EXPIRES_PREFIX = "expires:";
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_session_common = require("./common.cjs");
3
+ const require_session_map_session = require("./map-session.cjs");
4
+ //#region src/session/redis.ts
5
+ const CREATION_TIME_KEY = "creationTime";
6
+ const LAST_ACCESSED_TIME_KEY = "lastAccessedTime";
7
+ const MAX_INACTIVE_INTERVAL_KEY = "maxInactiveInterval";
8
+ const ATTRIBUTE_PREFIX = "sessionAttr:";
9
+ const SESSION_EXPIRES_PREFIX = "expires:";
34
10
  var RedisSession = class {
35
- cached;
36
- isNew;
37
- delta = /* @__PURE__ */ new Map();
38
- originalSessionId;
39
- originalPrincipalName;
40
- originalLastAccessTime = null;
41
- constructor(cached, isNew) {
42
- this.cached = cached;
43
- this.isNew = isNew;
44
- this.originalSessionId = cached.getId();
45
- this.originalPrincipalName = (0, import_common.resolveIndexesFor)(this);
46
- if (isNew) {
47
- this.delta.set(CREATION_TIME_KEY, cached.getCreationTime());
48
- this.delta.set(LAST_ACCESSED_TIME_KEY, cached.getLastAccessedTime());
49
- this.delta.set(MAX_INACTIVE_INTERVAL_KEY, cached.getMaxInactiveInterval());
50
- this.cached.getAttributeNames().forEach((name) => {
51
- const value = this.cached.getAttribute(name);
52
- if (value) this.delta.set(this.getSessionAttrNameKey(name), value);
53
- });
54
- }
55
- }
56
- getSessionAttrNameKey(name) {
57
- return ATTRIBUTE_PREFIX + name;
58
- }
59
- // override
60
- setLastAccessedTime(lastAccessedTime) {
61
- this.cached.setLastAccessedTime(lastAccessedTime);
62
- this.delta.set(LAST_ACCESSED_TIME_KEY, lastAccessedTime);
63
- }
64
- isExpired() {
65
- return this.cached.isExpired();
66
- }
67
- getCreationTime() {
68
- return this.cached.getCreationTime();
69
- }
70
- getId() {
71
- return this.cached.getId();
72
- }
73
- changeSessionId() {
74
- return this.cached.changeSessionId();
75
- }
76
- getLastAccessedTime() {
77
- return this.cached.getLastAccessedTime();
78
- }
79
- setMaxInactiveInterval(interval) {
80
- this.cached.setMaxInactiveInterval(interval);
81
- this.delta.set(MAX_INACTIVE_INTERVAL_KEY, interval);
82
- }
83
- getMaxInactiveInterval() {
84
- return this.cached.getMaxInactiveInterval();
85
- }
86
- getAttribute(name) {
87
- return this.cached.getAttribute(name);
88
- }
89
- getAttributeNames() {
90
- return this.cached.getAttributeNames();
91
- }
92
- setAttribute(name, value) {
93
- this.cached.setAttribute(name, value);
94
- this.delta.set(this.getSessionAttrNameKey(name), value);
95
- }
96
- removeAttribute(name) {
97
- this.cached.removeAttribute(name);
98
- this.delta.set(this.getSessionAttrNameKey(name), null);
99
- }
11
+ cached;
12
+ isNew;
13
+ delta = /* @__PURE__ */ new Map();
14
+ originalSessionId;
15
+ originalPrincipalName;
16
+ originalLastAccessTime = null;
17
+ constructor(cached, isNew) {
18
+ this.cached = cached;
19
+ this.isNew = isNew;
20
+ this.originalSessionId = cached.getId();
21
+ this.originalPrincipalName = require_session_common.resolveIndexesFor(this);
22
+ if (isNew) {
23
+ this.delta.set(CREATION_TIME_KEY, cached.getCreationTime());
24
+ this.delta.set(LAST_ACCESSED_TIME_KEY, cached.getLastAccessedTime());
25
+ this.delta.set(MAX_INACTIVE_INTERVAL_KEY, cached.getMaxInactiveInterval());
26
+ this.cached.getAttributeNames().forEach((name) => {
27
+ const value = this.cached.getAttribute(name);
28
+ if (value) this.delta.set(this.getSessionAttrNameKey(name), value);
29
+ });
30
+ }
31
+ }
32
+ getSessionAttrNameKey(name) {
33
+ return ATTRIBUTE_PREFIX + name;
34
+ }
35
+ setLastAccessedTime(lastAccessedTime) {
36
+ this.cached.setLastAccessedTime(lastAccessedTime);
37
+ this.delta.set(LAST_ACCESSED_TIME_KEY, lastAccessedTime);
38
+ }
39
+ isExpired() {
40
+ return this.cached.isExpired();
41
+ }
42
+ getCreationTime() {
43
+ return this.cached.getCreationTime();
44
+ }
45
+ getId() {
46
+ return this.cached.getId();
47
+ }
48
+ changeSessionId() {
49
+ return this.cached.changeSessionId();
50
+ }
51
+ getLastAccessedTime() {
52
+ return this.cached.getLastAccessedTime();
53
+ }
54
+ setMaxInactiveInterval(interval) {
55
+ this.cached.setMaxInactiveInterval(interval);
56
+ this.delta.set(MAX_INACTIVE_INTERVAL_KEY, interval);
57
+ }
58
+ getMaxInactiveInterval() {
59
+ return this.cached.getMaxInactiveInterval();
60
+ }
61
+ getAttribute(name) {
62
+ return this.cached.getAttribute(name);
63
+ }
64
+ getAttributeNames() {
65
+ return this.cached.getAttributeNames();
66
+ }
67
+ setAttribute(name, value) {
68
+ this.cached.setAttribute(name, value);
69
+ this.delta.set(this.getSessionAttrNameKey(name), value);
70
+ }
71
+ removeAttribute(name) {
72
+ this.cached.removeAttribute(name);
73
+ this.delta.set(this.getSessionAttrNameKey(name), null);
74
+ }
100
75
  };
101
76
  var SortedSetRedisSessionExpirationStore = class {
102
- redis;
103
- namespace;
104
- expirationsKey;
105
- constructor(redis, namespace) {
106
- this.redis = redis;
107
- this.namespace = namespace;
108
- this.expirationsKey = `${this.namespace}:sessions:expirations`;
109
- }
110
- getSessionKey(sessionId) {
111
- return `${this.namespace}:sessions:${sessionId}`;
112
- }
113
- async save(session) {
114
- const expirationInMillis = session.getLastAccessedTime() + session.getMaxInactiveInterval() * 1e3;
115
- await this.redis.zadd(this.expirationsKey, expirationInMillis, session.getId());
116
- }
117
- async remove(sessionId) {
118
- await this.redis.zrem(this.expirationsKey, sessionId);
119
- }
120
- async rename(session) {
121
- if (session.originalSessionId === null || session.originalSessionId === session.getId()) return;
122
- await this.remove(session.originalSessionId);
123
- await this.save(session);
124
- }
125
- async cleanupExpiredSessions(cleanupCount = 100) {
126
- const key = this.expirationsKey;
127
- const score = Date.now();
128
- const sessionIds = await this.redis.zrevrangebyscore(key, score, 0, "LIMIT", 0, cleanupCount);
129
- if (sessionIds.length === 0) return;
130
- for (const sessionId of sessionIds) {
131
- const sessionKey = this.getSessionKey(sessionId);
132
- await this.redis.exists(sessionKey);
133
- }
134
- }
77
+ redis;
78
+ namespace;
79
+ expirationsKey;
80
+ constructor(redis, namespace) {
81
+ this.redis = redis;
82
+ this.namespace = namespace;
83
+ this.expirationsKey = `${this.namespace}:sessions:expirations`;
84
+ }
85
+ getSessionKey(sessionId) {
86
+ return `${this.namespace}:sessions:${sessionId}`;
87
+ }
88
+ async save(session) {
89
+ const expirationInMillis = session.getLastAccessedTime() + session.getMaxInactiveInterval() * 1e3;
90
+ await this.redis.zadd(this.expirationsKey, expirationInMillis, session.getId());
91
+ }
92
+ async remove(sessionId) {
93
+ await this.redis.zrem(this.expirationsKey, sessionId);
94
+ }
95
+ async rename(session) {
96
+ if (session.originalSessionId === null || session.originalSessionId === session.getId()) return;
97
+ await this.remove(session.originalSessionId);
98
+ await this.save(session);
99
+ }
100
+ async cleanupExpiredSessions(cleanupCount = 100) {
101
+ const key = this.expirationsKey;
102
+ const score = Date.now();
103
+ const sessionIds = await this.redis.zrevrangebyscore(key, score, 0, "LIMIT", 0, cleanupCount);
104
+ if (sessionIds.length === 0) return;
105
+ for (const sessionId of sessionIds) {
106
+ const sessionKey = this.getSessionKey(sessionId);
107
+ await this.redis.exists(sessionKey);
108
+ }
109
+ }
135
110
  };
136
111
  var RedisIndexedSessionRepository = class {
137
- redis;
138
- namespace;
139
- expirationStore;
140
- defaultMaxInactiveInterval = 1800;
141
- constructor(redis, namespace = "spring:session") {
142
- this.redis = redis;
143
- this.namespace = namespace;
144
- this.expirationStore = new SortedSetRedisSessionExpirationStore(redis, namespace);
145
- }
146
- setDefaultMaxInactiveInterval(interval) {
147
- this.defaultMaxInactiveInterval = interval;
148
- }
149
- getSessionKey(sessionId) {
150
- return `${this.namespace}:sessions:${sessionId}`;
151
- }
152
- getExpiredKey(sessionId) {
153
- return `${this.namespace}:sessions:expires:${sessionId}`;
154
- }
155
- getSessionAttrNameKey(name) {
156
- return ATTRIBUTE_PREFIX + name;
157
- }
158
- getPrincipalKey(principalName) {
159
- return `${this.namespace}:index:${import_common.PRINCIPAL_NAME_INDEX_NAME}:${principalName}`;
160
- }
161
- async getSession(id, allowExpired) {
162
- const key = this.getSessionKey(id);
163
- const entries = await this.redis.hgetall(key);
164
- if (!entries || Object.keys(entries).length === 0) return null;
165
- const loaded = new import_map_session.MapSession(id);
166
- if (!entries.creationTime) throw new Error("creationTime attribute not found");
167
- if (!entries.lastAccessedTime) throw new Error("lastAccessedTime attribute not found");
168
- if (!entries.maxInactiveInterval) throw new Error("maxInactiveInterval attribute not found");
169
- loaded.setCreationTime(Number(entries.creationTime));
170
- loaded.setLastAccessedTime(Number(entries.lastAccessedTime));
171
- loaded.setMaxInactiveInterval(Number(entries.maxInactiveInterval));
172
- for (const [key2, value] of Object.entries(entries)) {
173
- if (key2.startsWith(ATTRIBUTE_PREFIX)) {
174
- loaded.setAttribute(key2.slice(ATTRIBUTE_PREFIX.length), value);
175
- }
176
- }
177
- if (!allowExpired && loaded.isExpired()) return null;
178
- const result = new RedisSession(loaded, false);
179
- result.originalLastAccessTime = loaded.getLastAccessedTime();
180
- return result;
181
- }
182
- async saveDelta(session) {
183
- if (session.delta.size === 0) return;
184
- const sessionId = session.getId();
185
- const delta = /* @__PURE__ */ new Map();
186
- const removes = [];
187
- session.delta.forEach((value, key) => {
188
- if (value !== null) delta.set(key, value);
189
- else removes.push(key);
190
- });
191
- if (delta.size !== 0) {
192
- await this.redis.hset(this.getSessionKey(sessionId), delta);
193
- }
194
- if (removes.length !== 0) {
195
- await this.redis.hdel(this.getSessionKey(sessionId), ...removes);
196
- }
197
- const principalSessionKey = this.getSessionAttrNameKey(import_common.PRINCIPAL_NAME_INDEX_NAME);
198
- const securityPrincipalSessionKey = this.getSessionAttrNameKey(import_common.SPRING_SECURITY_CONTEXT);
199
- if (session.delta.has(principalSessionKey) || session.delta.has(securityPrincipalSessionKey)) {
200
- if (session.originalPrincipalName !== null) {
201
- const originalPrincipalRedisKey = this.getPrincipalKey(session.originalPrincipalName);
202
- await this.redis.srem(originalPrincipalRedisKey, sessionId);
203
- }
204
- const principal = (0, import_common.resolveIndexesFor)(session);
205
- session.originalPrincipalName = principal;
206
- if (principal !== null) {
207
- const principalRedisKey = this.getPrincipalKey(principal);
208
- await this.redis.sadd(principalRedisKey, sessionId);
209
- }
210
- }
211
- if (session.isNew) {
212
- session.isNew = false;
213
- }
214
- const sessionExpireInSeconds = session.getMaxInactiveInterval();
215
- const keyToExpire = SESSION_EXPIRES_PREFIX + session.getId();
216
- const sessionKey = this.getSessionKey(keyToExpire);
217
- if (sessionExpireInSeconds < 0) {
218
- await this.redis.append(sessionKey, "");
219
- await this.redis.persist(sessionKey);
220
- await this.redis.persist(this.getSessionKey(session.getId()));
221
- } else if (sessionExpireInSeconds === 0) {
222
- await this.redis.del(sessionKey);
223
- } else {
224
- await this.redis.append(sessionKey, "");
225
- await this.redis.expire(sessionKey, sessionExpireInSeconds);
226
- }
227
- if (sessionExpireInSeconds > 0) {
228
- const fiveMinutesAfterExpires = sessionExpireInSeconds + 5 * 60;
229
- await this.redis.expire(this.getSessionKey(session.getId()), fiveMinutesAfterExpires);
230
- }
231
- await this.expirationStore.save(session);
232
- session.delta.clear();
233
- }
234
- async saveChangeSessionId(session) {
235
- const sessionId = session.getId();
236
- if (sessionId === session.originalSessionId) return;
237
- if (!session.isNew) {
238
- const originalSessionIdKey = this.getSessionKey(session.originalSessionId);
239
- const sessionIdKey = this.getSessionKey(sessionId);
240
- const originalExpiredKey = this.getExpiredKey(session.originalSessionId);
241
- const expiredKey = this.getExpiredKey(sessionId);
242
- await this.redis.rename(originalSessionIdKey, sessionIdKey);
243
- await this.redis.rename(originalExpiredKey, expiredKey);
244
- if (session.originalPrincipalName !== null) {
245
- const originalPrincipalRedisKey = this.getPrincipalKey(session.originalPrincipalName);
246
- await this.redis.srem(originalPrincipalRedisKey, session.originalSessionId);
247
- await this.redis.sadd(originalPrincipalRedisKey, sessionId);
248
- }
249
- await this.expirationStore.rename(session);
250
- }
251
- session.originalSessionId = sessionId;
252
- }
253
- // override
254
- createSession() {
255
- const cached = new import_map_session.MapSession();
256
- cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
257
- return new RedisSession(cached, true);
258
- }
259
- async save(session) {
260
- await this.saveChangeSessionId(session);
261
- await this.saveDelta(session);
262
- }
263
- async findById(sessionId) {
264
- return this.getSession(sessionId, false);
265
- }
266
- async deleteById(sessionId) {
267
- const session = await this.getSession(sessionId, true);
268
- if (!session) return;
269
- const principal = (0, import_common.resolveIndexesFor)(session);
270
- if (principal !== null) {
271
- await this.redis.srem(this.getPrincipalKey(principal), sessionId);
272
- }
273
- await this.expirationStore.remove(sessionId);
274
- const expiredKey = this.getExpiredKey(sessionId);
275
- await this.redis.del(expiredKey);
276
- session.setMaxInactiveInterval(0);
277
- await this.save(session);
278
- }
279
- async findByIndexNameAndIndexValue(indexName, indexValue) {
280
- if (indexName !== import_common.PRINCIPAL_NAME_INDEX_NAME) {
281
- return /* @__PURE__ */ new Map();
282
- }
283
- const principalKey = this.getPrincipalKey(indexValue);
284
- const sessionIds = await this.redis.smembers(principalKey);
285
- if (!sessionIds || sessionIds.length === 0) {
286
- return /* @__PURE__ */ new Map();
287
- }
288
- const sessions = /* @__PURE__ */ new Map();
289
- for (const sessionId of sessionIds) {
290
- const session = await this.findById(sessionId);
291
- if (session !== null) {
292
- sessions.set(sessionId, session);
293
- }
294
- }
295
- return sessions;
296
- }
297
- async findByPrincipalName(principalName) {
298
- return this.findByIndexNameAndIndexValue(import_common.PRINCIPAL_NAME_INDEX_NAME, principalName);
299
- }
300
- async cleanupExpiredSessions(cleanupCount) {
301
- await this.expirationStore.cleanupExpiredSessions(cleanupCount);
302
- }
112
+ redis;
113
+ namespace;
114
+ expirationStore;
115
+ defaultMaxInactiveInterval = 1800;
116
+ constructor(redis, namespace = "spring:session") {
117
+ this.redis = redis;
118
+ this.namespace = namespace;
119
+ this.expirationStore = new SortedSetRedisSessionExpirationStore(redis, namespace);
120
+ }
121
+ setDefaultMaxInactiveInterval(interval) {
122
+ this.defaultMaxInactiveInterval = interval;
123
+ }
124
+ getSessionKey(sessionId) {
125
+ return `${this.namespace}:sessions:${sessionId}`;
126
+ }
127
+ getExpiredKey(sessionId) {
128
+ return `${this.namespace}:sessions:expires:${sessionId}`;
129
+ }
130
+ getSessionAttrNameKey(name) {
131
+ return ATTRIBUTE_PREFIX + name;
132
+ }
133
+ getPrincipalKey(principalName) {
134
+ return `${this.namespace}:index:${require_session_common.PRINCIPAL_NAME_INDEX_NAME}:${principalName}`;
135
+ }
136
+ async getSession(id, allowExpired) {
137
+ const key = this.getSessionKey(id);
138
+ const entries = await this.redis.hgetall(key);
139
+ if (!entries || Object.keys(entries).length === 0) return null;
140
+ const loaded = new require_session_map_session.MapSession(id);
141
+ if (!entries.creationTime) throw new Error("creationTime attribute not found");
142
+ if (!entries.lastAccessedTime) throw new Error("lastAccessedTime attribute not found");
143
+ if (!entries.maxInactiveInterval) throw new Error("maxInactiveInterval attribute not found");
144
+ loaded.setCreationTime(Number(entries.creationTime));
145
+ loaded.setLastAccessedTime(Number(entries.lastAccessedTime));
146
+ loaded.setMaxInactiveInterval(Number(entries.maxInactiveInterval));
147
+ for (const [key, value] of Object.entries(entries)) if (key.startsWith(ATTRIBUTE_PREFIX)) loaded.setAttribute(key.slice(12), value);
148
+ if (!allowExpired && loaded.isExpired()) return null;
149
+ const result = new RedisSession(loaded, false);
150
+ result.originalLastAccessTime = loaded.getLastAccessedTime();
151
+ return result;
152
+ }
153
+ async saveDelta(session) {
154
+ if (session.delta.size === 0) return;
155
+ const sessionId = session.getId();
156
+ const delta = /* @__PURE__ */ new Map();
157
+ const removes = [];
158
+ session.delta.forEach((value, key) => {
159
+ if (value !== null) delta.set(key, value);
160
+ else removes.push(key);
161
+ });
162
+ if (delta.size !== 0) await this.redis.hset(this.getSessionKey(sessionId), delta);
163
+ if (removes.length !== 0) await this.redis.hdel(this.getSessionKey(sessionId), ...removes);
164
+ const principalSessionKey = this.getSessionAttrNameKey(require_session_common.PRINCIPAL_NAME_INDEX_NAME);
165
+ const securityPrincipalSessionKey = this.getSessionAttrNameKey(require_session_common.SPRING_SECURITY_CONTEXT);
166
+ if (session.delta.has(principalSessionKey) || session.delta.has(securityPrincipalSessionKey)) {
167
+ if (session.originalPrincipalName !== null) {
168
+ const originalPrincipalRedisKey = this.getPrincipalKey(session.originalPrincipalName);
169
+ await this.redis.srem(originalPrincipalRedisKey, sessionId);
170
+ }
171
+ const principal = require_session_common.resolveIndexesFor(session);
172
+ session.originalPrincipalName = principal;
173
+ if (principal !== null) {
174
+ const principalRedisKey = this.getPrincipalKey(principal);
175
+ await this.redis.sadd(principalRedisKey, sessionId);
176
+ }
177
+ }
178
+ if (session.isNew) session.isNew = false;
179
+ const sessionExpireInSeconds = session.getMaxInactiveInterval();
180
+ const keyToExpire = SESSION_EXPIRES_PREFIX + session.getId();
181
+ const sessionKey = this.getSessionKey(keyToExpire);
182
+ if (sessionExpireInSeconds < 0) {
183
+ await this.redis.append(sessionKey, "");
184
+ await this.redis.persist(sessionKey);
185
+ await this.redis.persist(this.getSessionKey(session.getId()));
186
+ } else if (sessionExpireInSeconds === 0) await this.redis.del(sessionKey);
187
+ else {
188
+ await this.redis.append(sessionKey, "");
189
+ await this.redis.expire(sessionKey, sessionExpireInSeconds);
190
+ }
191
+ if (sessionExpireInSeconds > 0) {
192
+ const fiveMinutesAfterExpires = sessionExpireInSeconds + 300;
193
+ await this.redis.expire(this.getSessionKey(session.getId()), fiveMinutesAfterExpires);
194
+ }
195
+ await this.expirationStore.save(session);
196
+ session.delta.clear();
197
+ }
198
+ async saveChangeSessionId(session) {
199
+ const sessionId = session.getId();
200
+ if (sessionId === session.originalSessionId) return;
201
+ if (!session.isNew) {
202
+ const originalSessionIdKey = this.getSessionKey(session.originalSessionId);
203
+ const sessionIdKey = this.getSessionKey(sessionId);
204
+ const originalExpiredKey = this.getExpiredKey(session.originalSessionId);
205
+ const expiredKey = this.getExpiredKey(sessionId);
206
+ await this.redis.rename(originalSessionIdKey, sessionIdKey);
207
+ await this.redis.rename(originalExpiredKey, expiredKey);
208
+ if (session.originalPrincipalName !== null) {
209
+ const originalPrincipalRedisKey = this.getPrincipalKey(session.originalPrincipalName);
210
+ await this.redis.srem(originalPrincipalRedisKey, session.originalSessionId);
211
+ await this.redis.sadd(originalPrincipalRedisKey, sessionId);
212
+ }
213
+ await this.expirationStore.rename(session);
214
+ }
215
+ session.originalSessionId = sessionId;
216
+ }
217
+ createSession() {
218
+ const cached = new require_session_map_session.MapSession();
219
+ cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
220
+ return new RedisSession(cached, true);
221
+ }
222
+ async save(session) {
223
+ await this.saveChangeSessionId(session);
224
+ await this.saveDelta(session);
225
+ }
226
+ async findById(sessionId) {
227
+ return this.getSession(sessionId, false);
228
+ }
229
+ async deleteById(sessionId) {
230
+ const session = await this.getSession(sessionId, true);
231
+ if (!session) return;
232
+ const principal = require_session_common.resolveIndexesFor(session);
233
+ if (principal !== null) await this.redis.srem(this.getPrincipalKey(principal), sessionId);
234
+ await this.expirationStore.remove(sessionId);
235
+ const expiredKey = this.getExpiredKey(sessionId);
236
+ await this.redis.del(expiredKey);
237
+ session.setMaxInactiveInterval(0);
238
+ await this.save(session);
239
+ }
240
+ async findByIndexNameAndIndexValue(indexName, indexValue) {
241
+ if (indexName !== require_session_common.PRINCIPAL_NAME_INDEX_NAME) return /* @__PURE__ */ new Map();
242
+ const principalKey = this.getPrincipalKey(indexValue);
243
+ const sessionIds = await this.redis.smembers(principalKey);
244
+ if (!sessionIds || sessionIds.length === 0) return /* @__PURE__ */ new Map();
245
+ const sessions = /* @__PURE__ */ new Map();
246
+ for (const sessionId of sessionIds) {
247
+ const session = await this.findById(sessionId);
248
+ if (session !== null) sessions.set(sessionId, session);
249
+ }
250
+ return sessions;
251
+ }
252
+ async findByPrincipalName(principalName) {
253
+ return this.findByIndexNameAndIndexValue(require_session_common.PRINCIPAL_NAME_INDEX_NAME, principalName);
254
+ }
255
+ async cleanupExpiredSessions(cleanupCount) {
256
+ await this.expirationStore.cleanupExpiredSessions(cleanupCount);
257
+ }
303
258
  };
304
259
  var RedisKVRepository = class {
305
- redis;
306
- constructor(redis) {
307
- this.redis = redis;
308
- }
309
- async setItem(key, value, expiresIn) {
310
- if (expiresIn) {
311
- await this.redis.set(key, value, "EX", expiresIn);
312
- } else {
313
- await this.redis.set(key, value);
314
- }
315
- }
316
- async getItem(key) {
317
- return await this.redis.get(key);
318
- }
319
- async removeItem(key) {
320
- await this.redis.del(key);
321
- }
260
+ redis;
261
+ constructor(redis) {
262
+ this.redis = redis;
263
+ }
264
+ async setItem(key, value, expiresIn) {
265
+ if (expiresIn) await this.redis.set(key, value, "EX", expiresIn);
266
+ else await this.redis.set(key, value);
267
+ }
268
+ async getItem(key) {
269
+ return await this.redis.get(key);
270
+ }
271
+ async removeItem(key) {
272
+ await this.redis.del(key);
273
+ }
322
274
  };
323
- // Annotate the CommonJS export names for ESM import in node:
324
- 0 && (module.exports = {
325
- RedisIndexedSessionRepository,
326
- RedisKVRepository
327
- });
275
+ //#endregion
276
+ exports.RedisIndexedSessionRepository = RedisIndexedSessionRepository;
277
+ exports.RedisKVRepository = RedisKVRepository;
278
+
328
279
  //# sourceMappingURL=redis.cjs.map