@adonisjs/session 7.5.1 → 8.0.0-next.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.
- package/build/{chunk-TZLOND27.js → chunk-DFXWYDMY.js} +21 -3
- package/build/chunk-HAD4PFFM.js +229 -0
- package/build/{chunk-WUWXIKIB.js → chunk-MVBWJOEG.js} +54 -14
- package/build/{chunk-ZVSEMDIC.js → chunk-SBOMJK4T.js} +1 -2
- package/build/{chunk-OCQGCVXK.js → chunk-SHD6OX52.js} +169 -75
- package/build/chunk-Y566BNUT.js +113 -0
- package/build/{cookie-WBWYVEDW.js → cookie-YBBGLCO5.js} +38 -6
- package/build/{dynamodb-3PG52TE3.js → dynamodb-PLZABBFD.js} +54 -8
- package/build/factories/main.d.ts +1 -1
- package/build/factories/main.js +5 -6
- package/build/factories/session_middleware_factory.d.ts +2 -2
- package/build/{file-B6QKOZXW.js → file-CCJ5ESE2.js} +49 -14
- package/build/index.d.ts +6 -6
- package/build/index.js +4 -5
- package/build/providers/session_provider.d.ts +1 -1
- package/build/providers/session_provider.js +5 -6
- package/build/{redis-CAY24YIA.js → redis-NXJWWWVB.js} +39 -8
- package/build/src/client.d.ts +46 -10
- package/build/src/client.js +3 -4
- package/build/src/define_config.d.ts +37 -4
- package/build/src/errors.d.ts +20 -4
- package/build/src/plugins/edge.d.ts +12 -2
- package/build/src/plugins/edge.js +1 -2
- package/build/src/plugins/japa/api_client.d.ts +22 -4
- package/build/src/plugins/japa/api_client.js +5 -5
- package/build/src/plugins/japa/browser_client.d.ts +18 -4
- package/build/src/plugins/japa/browser_client.js +5 -5
- package/build/src/session.d.ts +171 -58
- package/build/src/session_middleware.d.ts +28 -5
- package/build/src/session_middleware.js +4 -5
- package/build/src/stores/cookie.d.ts +41 -7
- package/build/src/stores/dynamodb.d.ts +44 -8
- package/build/src/stores/file.d.ts +34 -8
- package/build/src/stores/memory.d.ts +34 -5
- package/build/src/stores/redis.d.ts +37 -7
- package/build/src/types.d.ts +155 -34
- package/build/src/types.js +0 -1
- package/build/src/values_store.d.ts +113 -21
- package/package.json +43 -40
- package/build/chunk-GB5FBZCV.js +0 -85
- package/build/chunk-GB5FBZCV.js.map +0 -1
- package/build/chunk-OCQGCVXK.js.map +0 -1
- package/build/chunk-TE5JP3SX.js +0 -151
- package/build/chunk-TE5JP3SX.js.map +0 -1
- package/build/chunk-TZLOND27.js.map +0 -1
- package/build/chunk-WUWXIKIB.js.map +0 -1
- package/build/chunk-ZVSEMDIC.js.map +0 -1
- package/build/cookie-WBWYVEDW.js.map +0 -1
- package/build/dynamodb-3PG52TE3.js.map +0 -1
- package/build/factories/main.js.map +0 -1
- package/build/file-B6QKOZXW.js.map +0 -1
- package/build/index.js.map +0 -1
- package/build/providers/session_provider.js.map +0 -1
- package/build/redis-CAY24YIA.js.map +0 -1
- package/build/src/client.js.map +0 -1
- package/build/src/plugins/edge.js.map +0 -1
- package/build/src/plugins/japa/api_client.js.map +0 -1
- package/build/src/plugins/japa/browser_client.js.map +0 -1
- package/build/src/session_middleware.js.map +0 -1
- package/build/src/types.js.map +0 -1
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
ReadOnlyValuesStore,
|
|
3
3
|
ValuesStore
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-HAD4PFFM.js";
|
|
5
5
|
import {
|
|
6
6
|
__export,
|
|
7
7
|
debug_default
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-SBOMJK4T.js";
|
|
9
9
|
|
|
10
10
|
// src/errors.ts
|
|
11
11
|
var errors_exports = {};
|
|
@@ -13,7 +13,7 @@ __export(errors_exports, {
|
|
|
13
13
|
E_SESSION_NOT_MUTABLE: () => E_SESSION_NOT_MUTABLE,
|
|
14
14
|
E_SESSION_NOT_READY: () => E_SESSION_NOT_READY
|
|
15
15
|
});
|
|
16
|
-
import { createError } from "@
|
|
16
|
+
import { createError } from "@adonisjs/core/exceptions";
|
|
17
17
|
var E_SESSION_NOT_MUTABLE = createError(
|
|
18
18
|
"Session store is in readonly mode and cannot be mutated",
|
|
19
19
|
"E_SESSION_NOT_MUTABLE",
|
|
@@ -26,10 +26,18 @@ var E_SESSION_NOT_READY = createError(
|
|
|
26
26
|
);
|
|
27
27
|
|
|
28
28
|
// src/session.ts
|
|
29
|
+
import { randomUUID } from "crypto";
|
|
29
30
|
import Macroable from "@poppinss/macroable";
|
|
30
31
|
import lodash from "@poppinss/utils/lodash";
|
|
31
|
-
import { cuid } from "@adonisjs/core/helpers";
|
|
32
32
|
var Session = class extends Macroable {
|
|
33
|
+
/**
|
|
34
|
+
* Creates a new session instance
|
|
35
|
+
*
|
|
36
|
+
* @param config - Session configuration
|
|
37
|
+
* @param storeFactory - Factory function to create session store
|
|
38
|
+
* @param emitter - Event emitter service
|
|
39
|
+
* @param ctx - HTTP context
|
|
40
|
+
*/
|
|
33
41
|
constructor(config, storeFactory, emitter, ctx) {
|
|
34
42
|
super();
|
|
35
43
|
this.config = config;
|
|
@@ -37,93 +45,83 @@ var Session = class extends Macroable {
|
|
|
37
45
|
this.#emitter = emitter;
|
|
38
46
|
this.#store = storeFactory(ctx, config);
|
|
39
47
|
this.#sessionIdFromCookie = ctx.request.cookie(config.cookieName, void 0);
|
|
40
|
-
this.#sessionId = this.#sessionIdFromCookie ||
|
|
48
|
+
this.#sessionId = this.#sessionIdFromCookie || randomUUID();
|
|
41
49
|
}
|
|
42
50
|
#store;
|
|
43
51
|
#emitter;
|
|
44
52
|
#ctx;
|
|
45
53
|
#readonly = false;
|
|
46
54
|
/**
|
|
47
|
-
* Session values store
|
|
55
|
+
* Session values store that holds the actual session data
|
|
48
56
|
*/
|
|
49
57
|
#valuesStore;
|
|
50
58
|
/**
|
|
51
|
-
* Session id
|
|
52
|
-
* as a cookie during the response.
|
|
59
|
+
* Session id that will be committed as a cookie during the response
|
|
53
60
|
*/
|
|
54
61
|
#sessionId;
|
|
55
62
|
/**
|
|
56
|
-
* Session id from cookie
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* This only might not exist during the first request. Also during
|
|
60
|
-
* session id re-generation, this value will be different from
|
|
61
|
-
* the session id.
|
|
63
|
+
* Session id read from the cookie during the HTTP request.
|
|
64
|
+
* May not exist during the first request or will differ from sessionId during regeneration.
|
|
62
65
|
*/
|
|
63
66
|
#sessionIdFromCookie;
|
|
64
67
|
/**
|
|
65
|
-
* Store of flash messages that be written during the
|
|
66
|
-
* HTTP request
|
|
68
|
+
* Store of flash messages that will be written during the HTTP request
|
|
67
69
|
*/
|
|
68
70
|
responseFlashMessages = new ValuesStore({});
|
|
69
71
|
/**
|
|
70
|
-
* Store of flash messages for the current HTTP request
|
|
72
|
+
* Store of flash messages for the current HTTP request
|
|
71
73
|
*/
|
|
72
74
|
flashMessages = new ValuesStore({});
|
|
73
75
|
/**
|
|
74
|
-
* The key
|
|
75
|
-
* the session store.
|
|
76
|
+
* The key used for storing flash messages inside the session store
|
|
76
77
|
*/
|
|
77
78
|
flashKey = "__flash__";
|
|
78
79
|
/**
|
|
79
|
-
*
|
|
80
|
+
* Gets the session id for the current HTTP request
|
|
80
81
|
*/
|
|
81
82
|
get sessionId() {
|
|
82
83
|
return this.#sessionId;
|
|
83
84
|
}
|
|
84
85
|
/**
|
|
85
|
-
*
|
|
86
|
-
* the request
|
|
86
|
+
* Returns true if a fresh session was created during the request
|
|
87
87
|
*/
|
|
88
88
|
get fresh() {
|
|
89
89
|
return this.#sessionIdFromCookie === void 0;
|
|
90
90
|
}
|
|
91
91
|
/**
|
|
92
|
-
*
|
|
93
|
-
* state
|
|
92
|
+
* Returns true if the session is in readonly state
|
|
94
93
|
*/
|
|
95
94
|
get readonly() {
|
|
96
95
|
return this.#readonly;
|
|
97
96
|
}
|
|
98
97
|
/**
|
|
99
|
-
*
|
|
98
|
+
* Returns true if the session store has been initiated
|
|
100
99
|
*/
|
|
101
100
|
get initiated() {
|
|
102
101
|
return !!this.#valuesStore;
|
|
103
102
|
}
|
|
104
103
|
/**
|
|
105
|
-
*
|
|
106
|
-
* during the current request
|
|
104
|
+
* Returns true if the session id has been re-generated during the current request
|
|
107
105
|
*/
|
|
108
106
|
get hasRegeneratedSession() {
|
|
109
107
|
return !!(this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId);
|
|
110
108
|
}
|
|
111
109
|
/**
|
|
112
|
-
*
|
|
110
|
+
* Returns true if the session store is empty
|
|
113
111
|
*/
|
|
114
112
|
get isEmpty() {
|
|
115
113
|
return this.#valuesStore?.isEmpty ?? true;
|
|
116
114
|
}
|
|
117
115
|
/**
|
|
118
|
-
*
|
|
119
|
-
* modified
|
|
116
|
+
* Returns true if the session store has been modified
|
|
120
117
|
*/
|
|
121
118
|
get hasBeenModified() {
|
|
122
119
|
return this.#valuesStore?.hasBeenModified ?? false;
|
|
123
120
|
}
|
|
124
121
|
/**
|
|
125
|
-
* Returns the flash messages store for a given
|
|
126
|
-
*
|
|
122
|
+
* Returns the flash messages store for a given mode
|
|
123
|
+
*
|
|
124
|
+
* @param mode - Access mode ('write' or 'read')
|
|
127
125
|
*/
|
|
128
126
|
#getFlashStore(mode) {
|
|
129
127
|
if (!this.#valuesStore) {
|
|
@@ -136,6 +134,8 @@ var Session = class extends Macroable {
|
|
|
136
134
|
}
|
|
137
135
|
/**
|
|
138
136
|
* Returns the store instance for a given mode
|
|
137
|
+
*
|
|
138
|
+
* @param mode - Access mode ('write' or 'read')
|
|
139
139
|
*/
|
|
140
140
|
#getValuesStore(mode) {
|
|
141
141
|
if (!this.#valuesStore) {
|
|
@@ -147,8 +147,13 @@ var Session = class extends Macroable {
|
|
|
147
147
|
return this.#valuesStore;
|
|
148
148
|
}
|
|
149
149
|
/**
|
|
150
|
-
* Initiates the session store. The method results in a noop
|
|
151
|
-
*
|
|
150
|
+
* Initiates the session store. The method results in a noop when called multiple times.
|
|
151
|
+
*
|
|
152
|
+
* @param readonly - Whether to initiate the session in readonly mode
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* await session.initiate(false) // Read-write mode
|
|
156
|
+
* await session.initiate(true) // Readonly mode
|
|
152
157
|
*/
|
|
153
158
|
async initiate(readonly) {
|
|
154
159
|
if (this.#valuesStore) {
|
|
@@ -178,66 +183,113 @@ var Session = class extends Macroable {
|
|
|
178
183
|
this.#emitter.emit("session:initiated", { session: this });
|
|
179
184
|
}
|
|
180
185
|
/**
|
|
181
|
-
*
|
|
186
|
+
* Puts a key-value pair to the session data store
|
|
187
|
+
*
|
|
188
|
+
* @param key - The key to store the value under
|
|
189
|
+
* @param value - The value to store
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* session.put('username', 'john')
|
|
193
|
+
* session.put('user.preferences', { theme: 'dark' })
|
|
182
194
|
*/
|
|
183
195
|
put(key, value) {
|
|
184
196
|
this.#getValuesStore("write").set(key, value);
|
|
185
197
|
}
|
|
186
198
|
/**
|
|
187
|
-
*
|
|
199
|
+
* Checks if a key exists inside the datastore
|
|
200
|
+
*
|
|
201
|
+
* @param key - The key to check for existence
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* if (session.has('username')) {
|
|
205
|
+
* console.log('User is logged in')
|
|
206
|
+
* }
|
|
188
207
|
*/
|
|
189
208
|
has(key) {
|
|
190
209
|
return this.#getValuesStore("read").has(key);
|
|
191
210
|
}
|
|
192
211
|
/**
|
|
193
|
-
*
|
|
194
|
-
* You can specify a default value to use
|
|
195
|
-
*
|
|
212
|
+
* Gets the value of a key from the session datastore.
|
|
213
|
+
* You can specify a default value to use when key does not exist or has undefined value.
|
|
214
|
+
*
|
|
215
|
+
* @param key - The key to retrieve
|
|
216
|
+
* @param defaultValue - Default value to return if key doesn't exist
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* const username = session.get('username', 'guest')
|
|
220
|
+
* const preferences = session.get('user.preferences', {})
|
|
196
221
|
*/
|
|
197
222
|
get(key, defaultValue) {
|
|
198
223
|
return this.#getValuesStore("read").get(key, defaultValue);
|
|
199
224
|
}
|
|
200
225
|
/**
|
|
201
|
-
*
|
|
226
|
+
* Gets everything from the session store
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* const allData = session.all()
|
|
230
|
+
* console.log(allData) // { username: 'john', theme: 'dark' }
|
|
202
231
|
*/
|
|
203
232
|
all() {
|
|
204
233
|
return this.#getValuesStore("read").all();
|
|
205
234
|
}
|
|
206
235
|
/**
|
|
207
|
-
*
|
|
236
|
+
* Removes a key from the session datastore
|
|
237
|
+
*
|
|
238
|
+
* @param key - The key to remove
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* session.forget('temp_data')
|
|
242
|
+
* session.forget('user.cache')
|
|
208
243
|
*/
|
|
209
244
|
forget(key) {
|
|
210
245
|
return this.#getValuesStore("write").unset(key);
|
|
211
246
|
}
|
|
212
247
|
/**
|
|
213
|
-
*
|
|
214
|
-
*
|
|
248
|
+
* Reads value for a key from the session datastore and removes it simultaneously
|
|
249
|
+
*
|
|
250
|
+
* @param key - The key to pull
|
|
251
|
+
* @param defaultValue - Default value to return if key doesn't exist
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* const message = session.pull('notification', 'No messages')
|
|
255
|
+
* // message contains the value, and it's removed from session
|
|
215
256
|
*/
|
|
216
257
|
pull(key, defaultValue) {
|
|
217
258
|
return this.#getValuesStore("write").pull(key, defaultValue);
|
|
218
259
|
}
|
|
219
260
|
/**
|
|
220
|
-
*
|
|
221
|
-
*
|
|
261
|
+
* Increments the value of a key inside the session store.
|
|
262
|
+
* A new key will be defined if it doesn't exist already with value 1.
|
|
263
|
+
*
|
|
264
|
+
* @param key - The key to increment
|
|
265
|
+
* @param steps - Number of steps to increment (default: 1)
|
|
222
266
|
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
267
|
+
* @example
|
|
268
|
+
* session.increment('page_views') // Increments by 1
|
|
269
|
+
* session.increment('score', 10) // Increments by 10
|
|
225
270
|
*/
|
|
226
271
|
increment(key, steps = 1) {
|
|
227
272
|
return this.#getValuesStore("write").increment(key, steps);
|
|
228
273
|
}
|
|
229
274
|
/**
|
|
230
|
-
*
|
|
231
|
-
*
|
|
275
|
+
* Decrements the value of a key inside the session store.
|
|
276
|
+
* A new key will be defined if it doesn't exist already with value -1.
|
|
232
277
|
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
278
|
+
* @param key - The key to decrement
|
|
279
|
+
* @param steps - Number of steps to decrement (default: 1)
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* session.decrement('attempts') // Decrements by 1
|
|
283
|
+
* session.decrement('credits', 5) // Decrements by 5
|
|
235
284
|
*/
|
|
236
285
|
decrement(key, steps = 1) {
|
|
237
286
|
return this.#getValuesStore("write").decrement(key, steps);
|
|
238
287
|
}
|
|
239
288
|
/**
|
|
240
|
-
*
|
|
289
|
+
* Empties the session store
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* session.clear() // Removes all session data
|
|
241
293
|
*/
|
|
242
294
|
clear() {
|
|
243
295
|
return this.#getValuesStore("write").clear();
|
|
@@ -252,21 +304,34 @@ var Session = class extends Macroable {
|
|
|
252
304
|
}
|
|
253
305
|
}
|
|
254
306
|
/**
|
|
255
|
-
*
|
|
256
|
-
* errors via the "@error" tag.
|
|
257
|
-
*
|
|
307
|
+
* Flashes errors to the errorsBag. You can read these errors via the "@error" tag.
|
|
258
308
|
* Appends new messages to the existing collection.
|
|
309
|
+
*
|
|
310
|
+
* @param errorsCollection - Collection of error messages
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* session.flashErrors({
|
|
314
|
+
* general: 'Something went wrong',
|
|
315
|
+
* validation: ['Name is required', 'Email is invalid']
|
|
316
|
+
* })
|
|
259
317
|
*/
|
|
260
318
|
flashErrors(errorsCollection) {
|
|
261
319
|
this.flash({ errorsBag: errorsCollection });
|
|
262
320
|
}
|
|
263
321
|
/**
|
|
264
|
-
*
|
|
265
|
-
*
|
|
322
|
+
* Flashes validation error messages. Make sure the error is an instance of VineJS ValidationException.
|
|
323
|
+
* Overrides existing inputErrors.
|
|
266
324
|
*
|
|
267
|
-
*
|
|
268
|
-
|
|
269
|
-
|
|
325
|
+
* @param error - HTTP error containing validation messages
|
|
326
|
+
*
|
|
327
|
+
* @example
|
|
328
|
+
* try {
|
|
329
|
+
* await request.validate(schema)
|
|
330
|
+
* } catch (error) {
|
|
331
|
+
* session.flashValidationErrors(error)
|
|
332
|
+
* }
|
|
333
|
+
*/
|
|
334
|
+
flashValidationErrors(error, withInput = true) {
|
|
270
335
|
const errorsBag = error.messages.reduce((result, message) => {
|
|
271
336
|
if (result[message.field]) {
|
|
272
337
|
result[message.field].push(message.message);
|
|
@@ -275,7 +340,9 @@ var Session = class extends Macroable {
|
|
|
275
340
|
}
|
|
276
341
|
return result;
|
|
277
342
|
}, {});
|
|
278
|
-
|
|
343
|
+
if (withInput) {
|
|
344
|
+
this.flashExcept(["_csrf", "_method", "password", "password_confirmation"]);
|
|
345
|
+
}
|
|
279
346
|
let summary = "The form could not be saved. Please check the errors below.";
|
|
280
347
|
if ("i18n" in this.#ctx) {
|
|
281
348
|
summary = this.#ctx.i18n.t(
|
|
@@ -290,55 +357,83 @@ var Session = class extends Macroable {
|
|
|
290
357
|
[String(error.code)]: summary
|
|
291
358
|
});
|
|
292
359
|
this.flash("inputErrorsBag", errorsBag);
|
|
293
|
-
this.flash("errors", errorsBag);
|
|
294
360
|
}
|
|
295
361
|
/**
|
|
296
|
-
*
|
|
362
|
+
* Flashes all form input data to the flash messages store
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* session.flashAll() // Flashes all request input for next request
|
|
297
366
|
*/
|
|
298
367
|
flashAll() {
|
|
299
368
|
return this.#getFlashStore("write").set("input", this.#ctx.request.original());
|
|
300
369
|
}
|
|
301
370
|
/**
|
|
302
|
-
*
|
|
371
|
+
* Flashes form input data (except some keys) to the flash messages store
|
|
372
|
+
*
|
|
373
|
+
* @param keys - Array of keys to exclude from flashing
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* session.flashExcept(['password', '_csrf'])
|
|
303
377
|
*/
|
|
304
378
|
flashExcept(keys) {
|
|
305
379
|
this.#getFlashStore("write").set("input", lodash.omit(this.#ctx.request.original(), keys));
|
|
306
380
|
}
|
|
307
381
|
/**
|
|
308
|
-
*
|
|
382
|
+
* Flashes form input data (only some keys) to the flash messages store
|
|
383
|
+
*
|
|
384
|
+
* @param keys - Array of keys to include in flashing
|
|
385
|
+
*
|
|
386
|
+
* @example
|
|
387
|
+
* session.flashOnly(['name', 'email'])
|
|
309
388
|
*/
|
|
310
389
|
flashOnly(keys) {
|
|
311
390
|
this.#getFlashStore("write").set("input", lodash.pick(this.#ctx.request.original(), keys));
|
|
312
391
|
}
|
|
313
392
|
/**
|
|
314
|
-
*
|
|
393
|
+
* Reflashes messages from the last request in the current response
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
396
|
+
* session.reflash() // Keep all flash messages for another request
|
|
315
397
|
*/
|
|
316
398
|
reflash() {
|
|
317
399
|
this.#getFlashStore("write").set("reflashed", this.flashMessages.all());
|
|
318
400
|
}
|
|
319
401
|
/**
|
|
320
|
-
*
|
|
321
|
-
*
|
|
402
|
+
* Reflashes messages (only some keys) from the last request in the current response
|
|
403
|
+
*
|
|
404
|
+
* @param keys - Array of keys to reflash
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* session.reflashOnly(['success', 'info'])
|
|
322
408
|
*/
|
|
323
409
|
reflashOnly(keys) {
|
|
324
410
|
this.#getFlashStore("write").set("reflashed", lodash.pick(this.flashMessages.all(), keys));
|
|
325
411
|
}
|
|
326
412
|
/**
|
|
327
|
-
*
|
|
328
|
-
*
|
|
413
|
+
* Reflashes messages (except some keys) from the last request in the current response
|
|
414
|
+
*
|
|
415
|
+
* @param keys - Array of keys to exclude from reflashing
|
|
416
|
+
*
|
|
417
|
+
* @example
|
|
418
|
+
* session.reflashExcept(['error', 'warning'])
|
|
329
419
|
*/
|
|
330
420
|
reflashExcept(keys) {
|
|
331
421
|
this.#getFlashStore("write").set("reflashed", lodash.omit(this.flashMessages.all(), keys));
|
|
332
422
|
}
|
|
333
423
|
/**
|
|
334
|
-
* Re-
|
|
424
|
+
* Re-generates the session id and migrates data to it
|
|
425
|
+
*
|
|
426
|
+
* @example
|
|
427
|
+
* session.regenerate() // Generates new session ID for security
|
|
335
428
|
*/
|
|
336
429
|
regenerate() {
|
|
337
|
-
this.#sessionId =
|
|
430
|
+
this.#sessionId = randomUUID();
|
|
338
431
|
}
|
|
339
432
|
/**
|
|
340
|
-
*
|
|
341
|
-
*
|
|
433
|
+
* Commits session changes. No more mutations will be allowed after commit.
|
|
434
|
+
*
|
|
435
|
+
* @example
|
|
436
|
+
* await session.commit() // Save all changes to the session store
|
|
342
437
|
*/
|
|
343
438
|
async commit() {
|
|
344
439
|
if (!this.#valuesStore || this.readonly) {
|
|
@@ -391,4 +486,3 @@ export {
|
|
|
391
486
|
errors_exports,
|
|
392
487
|
Session
|
|
393
488
|
};
|
|
394
|
-
//# sourceMappingURL=chunk-OCQGCVXK.js.map
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ValuesStore
|
|
3
|
+
} from "./chunk-HAD4PFFM.js";
|
|
4
|
+
import {
|
|
5
|
+
debug_default
|
|
6
|
+
} from "./chunk-SBOMJK4T.js";
|
|
7
|
+
|
|
8
|
+
// src/client.ts
|
|
9
|
+
import { randomUUID } from "crypto";
|
|
10
|
+
var SessionClient = class {
|
|
11
|
+
/**
|
|
12
|
+
* Internal data store for session values
|
|
13
|
+
*/
|
|
14
|
+
#valuesStore = new ValuesStore({});
|
|
15
|
+
/**
|
|
16
|
+
* Internal store for flash messages
|
|
17
|
+
*/
|
|
18
|
+
#flashMessagesStore = new ValuesStore({});
|
|
19
|
+
/**
|
|
20
|
+
* The session store contract for reading and writing session data
|
|
21
|
+
*/
|
|
22
|
+
#store;
|
|
23
|
+
/**
|
|
24
|
+
* Session key used for storing flash messages
|
|
25
|
+
*/
|
|
26
|
+
flashKey = "__flash__";
|
|
27
|
+
/**
|
|
28
|
+
* Session ID to use when no explicit session id is defined
|
|
29
|
+
*/
|
|
30
|
+
sessionId = randomUUID();
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new session client
|
|
33
|
+
*
|
|
34
|
+
* @param store - Session store contract implementation
|
|
35
|
+
*/
|
|
36
|
+
constructor(store) {
|
|
37
|
+
this.#store = store;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Merges session data with existing values
|
|
41
|
+
*
|
|
42
|
+
* @param values - Session data to merge
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* client.merge({ userId: 123, theme: 'dark' })
|
|
46
|
+
*/
|
|
47
|
+
merge(values) {
|
|
48
|
+
this.#valuesStore.merge(values);
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Merges flash messages with existing flash data
|
|
53
|
+
*
|
|
54
|
+
* @param values - Flash message data to merge
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* client.flash({ success: 'Operation completed', info: 'Check your email' })
|
|
58
|
+
*/
|
|
59
|
+
flash(values) {
|
|
60
|
+
this.#flashMessagesStore.merge(values);
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Commits data to the session store
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* await client.commit() // Saves all changes to the store
|
|
68
|
+
*/
|
|
69
|
+
async commit() {
|
|
70
|
+
if (!this.#flashMessagesStore.isEmpty) {
|
|
71
|
+
this.#valuesStore.set(this.flashKey, this.#flashMessagesStore.toJSON());
|
|
72
|
+
}
|
|
73
|
+
debug_default("committing session data during api request");
|
|
74
|
+
if (!this.#valuesStore.isEmpty) {
|
|
75
|
+
this.#store.write(this.sessionId, this.#valuesStore.toJSON());
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Destroys the session data from the store
|
|
80
|
+
*
|
|
81
|
+
* @param sessionId - Optional session ID to destroy (defaults to current session)
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* await client.destroy() // Destroy current session
|
|
85
|
+
* await client.destroy('abc123') // Destroy specific session
|
|
86
|
+
*/
|
|
87
|
+
async destroy(sessionId) {
|
|
88
|
+
debug_default("destroying session data during api request");
|
|
89
|
+
this.#store.destroy(sessionId || this.sessionId);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Loads session data from the session store
|
|
93
|
+
*
|
|
94
|
+
* @param sessionId - Optional session ID to load (defaults to current session)
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* const { values, flashMessages } = await client.load()
|
|
98
|
+
* const data = await client.load('abc123') // Load specific session
|
|
99
|
+
*/
|
|
100
|
+
async load(sessionId) {
|
|
101
|
+
const contents = await this.#store.read(sessionId || this.sessionId);
|
|
102
|
+
const store = new ValuesStore(contents);
|
|
103
|
+
const flashMessages = store.pull(this.flashKey, {});
|
|
104
|
+
return {
|
|
105
|
+
values: store.all(),
|
|
106
|
+
flashMessages
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export {
|
|
112
|
+
SessionClient
|
|
113
|
+
};
|
|
@@ -1,18 +1,35 @@
|
|
|
1
1
|
import {
|
|
2
2
|
debug_default
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-SBOMJK4T.js";
|
|
4
4
|
|
|
5
5
|
// src/stores/cookie.ts
|
|
6
6
|
var CookieStore = class {
|
|
7
|
+
/**
|
|
8
|
+
* HTTP context for request/response operations
|
|
9
|
+
*/
|
|
7
10
|
#ctx;
|
|
11
|
+
/**
|
|
12
|
+
* Cookie configuration options
|
|
13
|
+
*/
|
|
8
14
|
#config;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new cookie store instance
|
|
17
|
+
*
|
|
18
|
+
* @param config - Cookie configuration options
|
|
19
|
+
* @param ctx - HTTP context
|
|
20
|
+
*/
|
|
9
21
|
constructor(config, ctx) {
|
|
10
22
|
this.#config = config;
|
|
11
23
|
this.#ctx = ctx;
|
|
12
24
|
debug_default("initiating cookie store %O", this.#config);
|
|
13
25
|
}
|
|
14
26
|
/**
|
|
15
|
-
*
|
|
27
|
+
* Reads session value from the encrypted cookie
|
|
28
|
+
*
|
|
29
|
+
* @param sessionId - Session identifier used as cookie name
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* const data = store.read('sess_abc123')
|
|
16
33
|
*/
|
|
17
34
|
read(sessionId) {
|
|
18
35
|
debug_default("cookie store: reading session data %s", sessionId);
|
|
@@ -23,14 +40,25 @@ var CookieStore = class {
|
|
|
23
40
|
return cookieValue;
|
|
24
41
|
}
|
|
25
42
|
/**
|
|
26
|
-
*
|
|
43
|
+
* Writes session values to an encrypted cookie
|
|
44
|
+
*
|
|
45
|
+
* @param sessionId - Session identifier used as cookie name
|
|
46
|
+
* @param values - Session data to store
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* store.write('sess_abc123', { userId: 123, theme: 'dark' })
|
|
27
50
|
*/
|
|
28
51
|
write(sessionId, values) {
|
|
29
52
|
debug_default("cookie store: writing session data %s: %O", sessionId, values);
|
|
30
53
|
this.#ctx.response.encryptedCookie(sessionId, values, this.#config);
|
|
31
54
|
}
|
|
32
55
|
/**
|
|
33
|
-
* Removes the session cookie
|
|
56
|
+
* Removes the session cookie from the client
|
|
57
|
+
*
|
|
58
|
+
* @param sessionId - Session identifier used as cookie name
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* store.destroy('sess_abc123')
|
|
34
62
|
*/
|
|
35
63
|
destroy(sessionId) {
|
|
36
64
|
debug_default("cookie store: destroying session data %s", sessionId);
|
|
@@ -39,7 +67,12 @@ var CookieStore = class {
|
|
|
39
67
|
}
|
|
40
68
|
}
|
|
41
69
|
/**
|
|
42
|
-
* Updates the cookie with existing
|
|
70
|
+
* Updates the cookie expiry by rewriting it with existing values
|
|
71
|
+
*
|
|
72
|
+
* @param sessionId - Session identifier used as cookie name
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* store.touch('sess_abc123') // Refreshes cookie expiry
|
|
43
76
|
*/
|
|
44
77
|
touch(sessionId) {
|
|
45
78
|
const value = this.read(sessionId);
|
|
@@ -53,4 +86,3 @@ var CookieStore = class {
|
|
|
53
86
|
export {
|
|
54
87
|
CookieStore
|
|
55
88
|
};
|
|
56
|
-
//# sourceMappingURL=cookie-WBWYVEDW.js.map
|