@adonisjs/session 7.0.0-13 → 7.0.0-15
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-2X5L327N.js +28 -0
- package/build/chunk-2X5L327N.js.map +1 -0
- package/build/chunk-HIQQAMXD.js +133 -0
- package/build/chunk-HIQQAMXD.js.map +1 -0
- package/build/chunk-K4OSGJVW.js +402 -0
- package/build/chunk-K4OSGJVW.js.map +1 -0
- package/build/chunk-S6P3TBEK.js +85 -0
- package/build/chunk-S6P3TBEK.js.map +1 -0
- package/build/chunk-TE5JP3SX.js +151 -0
- package/build/chunk-TE5JP3SX.js.map +1 -0
- package/build/chunk-WBAYBMJJ.js +15 -0
- package/build/chunk-WBAYBMJJ.js.map +1 -0
- package/build/{stubs/config.stub → config/session.stub} +3 -1
- package/build/cookie-H7KRZB4T.js +56 -0
- package/build/cookie-H7KRZB4T.js.map +1 -0
- package/build/factories/main.js +50 -9
- package/build/factories/main.js.map +1 -0
- package/build/file-YO7C2QWO.js +112 -0
- package/build/file-YO7C2QWO.js.map +1 -0
- package/build/index.d.ts +0 -1
- package/build/index.js +16 -12
- package/build/index.js.map +1 -0
- package/build/providers/session_provider.js +51 -59
- package/build/providers/session_provider.js.map +1 -0
- package/build/redis-KDWIBKUQ.js +58 -0
- package/build/redis-KDWIBKUQ.js.map +1 -0
- package/build/src/client.js +9 -85
- package/build/src/client.js.map +1 -0
- package/build/src/plugins/edge.js +101 -91
- package/build/src/plugins/edge.js.map +1 -0
- package/build/src/plugins/japa/api_client.js +99 -140
- package/build/src/plugins/japa/api_client.js.map +1 -0
- package/build/src/plugins/japa/browser_client.js +82 -109
- package/build/src/plugins/japa/browser_client.js.map +1 -0
- package/build/src/session.d.ts +14 -5
- package/build/src/session_middleware.js +10 -58
- package/build/src/session_middleware.js.map +1 -0
- package/build/src/values_store.d.ts +7 -7
- package/package.json +60 -43
- package/build/configure.js +0 -45
- package/build/factories/session_middleware_factory.js +0 -48
- package/build/src/debug.js +0 -10
- package/build/src/define_config.js +0 -105
- package/build/src/errors.js +0 -17
- package/build/src/session.js +0 -387
- package/build/src/stores/cookie.js +0 -60
- package/build/src/stores/file.js +0 -133
- package/build/src/stores/memory.js +0 -33
- package/build/src/stores/redis.js +0 -66
- package/build/src/types.js +0 -9
- package/build/src/values_store.js +0 -159
- package/build/stubs/main.js +0 -10
package/build/src/session.js
DELETED
|
@@ -1,387 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/session
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import lodash from '@poppinss/utils/lodash';
|
|
10
|
-
import { cuid } from '@adonisjs/core/helpers';
|
|
11
|
-
import debug from './debug.js';
|
|
12
|
-
import * as errors from './errors.js';
|
|
13
|
-
import { ReadOnlyValuesStore, ValuesStore } from './values_store.js';
|
|
14
|
-
/**
|
|
15
|
-
* The session class exposes the API to read and write values to
|
|
16
|
-
* the session store.
|
|
17
|
-
*
|
|
18
|
-
* A session instance is isolated between requests but
|
|
19
|
-
* uses a centralized persistence store and
|
|
20
|
-
*/
|
|
21
|
-
export class Session {
|
|
22
|
-
#config;
|
|
23
|
-
#store;
|
|
24
|
-
#emitter;
|
|
25
|
-
#ctx;
|
|
26
|
-
#readonly = false;
|
|
27
|
-
/**
|
|
28
|
-
* Session values store
|
|
29
|
-
*/
|
|
30
|
-
#valuesStore;
|
|
31
|
-
/**
|
|
32
|
-
* Session id refers to the session id that will be committed
|
|
33
|
-
* as a cookie during the response.
|
|
34
|
-
*/
|
|
35
|
-
#sessionId;
|
|
36
|
-
/**
|
|
37
|
-
* Session id from cookie refers to the value we read from the
|
|
38
|
-
* cookie during the HTTP request.
|
|
39
|
-
*
|
|
40
|
-
* This only might not exist during the first request. Also during
|
|
41
|
-
* session id re-generation, this value will be different from
|
|
42
|
-
* the session id.
|
|
43
|
-
*/
|
|
44
|
-
#sessionIdFromCookie;
|
|
45
|
-
/**
|
|
46
|
-
* Store of flash messages that be written during the
|
|
47
|
-
* HTTP request
|
|
48
|
-
*/
|
|
49
|
-
responseFlashMessages = new ValuesStore({});
|
|
50
|
-
/**
|
|
51
|
-
* Store of flash messages for the current HTTP request.
|
|
52
|
-
*/
|
|
53
|
-
flashMessages = new ValuesStore({});
|
|
54
|
-
/**
|
|
55
|
-
* The key to use for storing flash messages inside
|
|
56
|
-
* the session store.
|
|
57
|
-
*/
|
|
58
|
-
flashKey = '__flash__';
|
|
59
|
-
/**
|
|
60
|
-
* Session id for the current HTTP request
|
|
61
|
-
*/
|
|
62
|
-
get sessionId() {
|
|
63
|
-
return this.#sessionId;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* A boolean to know if a fresh session is created during
|
|
67
|
-
* the request
|
|
68
|
-
*/
|
|
69
|
-
get fresh() {
|
|
70
|
-
return this.#sessionIdFromCookie === undefined;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* A boolean to know if session is in readonly
|
|
74
|
-
* state
|
|
75
|
-
*/
|
|
76
|
-
get readonly() {
|
|
77
|
-
return this.#readonly;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* A boolean to know if session store has been initiated
|
|
81
|
-
*/
|
|
82
|
-
get initiated() {
|
|
83
|
-
return !!this.#valuesStore;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* A boolean to know if the session id has been re-generated
|
|
87
|
-
* during the current request
|
|
88
|
-
*/
|
|
89
|
-
get hasRegeneratedSession() {
|
|
90
|
-
return !!(this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId);
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* A boolean to know if the session store is empty
|
|
94
|
-
*/
|
|
95
|
-
get isEmpty() {
|
|
96
|
-
return this.#valuesStore?.isEmpty ?? true;
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* A boolean to know if the session store has been
|
|
100
|
-
* modified
|
|
101
|
-
*/
|
|
102
|
-
get hasBeenModified() {
|
|
103
|
-
return this.#valuesStore?.hasBeenModified ?? false;
|
|
104
|
-
}
|
|
105
|
-
constructor(config, storeFactory, emitter, ctx) {
|
|
106
|
-
this.#ctx = ctx;
|
|
107
|
-
this.#config = config;
|
|
108
|
-
this.#emitter = emitter;
|
|
109
|
-
this.#store = storeFactory(ctx, config);
|
|
110
|
-
this.#sessionIdFromCookie = ctx.request.cookie(config.cookieName, undefined);
|
|
111
|
-
this.#sessionId = this.#sessionIdFromCookie || cuid();
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Returns the flash messages store for a given
|
|
115
|
-
* mode
|
|
116
|
-
*/
|
|
117
|
-
#getFlashStore(mode) {
|
|
118
|
-
if (!this.#valuesStore) {
|
|
119
|
-
throw new errors.E_SESSION_NOT_READY();
|
|
120
|
-
}
|
|
121
|
-
if (mode === 'write' && this.readonly) {
|
|
122
|
-
throw new errors.E_SESSION_NOT_MUTABLE();
|
|
123
|
-
}
|
|
124
|
-
return this.responseFlashMessages;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Returns the store instance for a given mode
|
|
128
|
-
*/
|
|
129
|
-
#getValuesStore(mode) {
|
|
130
|
-
if (!this.#valuesStore) {
|
|
131
|
-
throw new errors.E_SESSION_NOT_READY();
|
|
132
|
-
}
|
|
133
|
-
if (mode === 'write' && this.readonly) {
|
|
134
|
-
throw new errors.E_SESSION_NOT_MUTABLE();
|
|
135
|
-
}
|
|
136
|
-
return this.#valuesStore;
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Initiates the session store. The method results in a noop
|
|
140
|
-
* when called multiple times
|
|
141
|
-
*/
|
|
142
|
-
async initiate(readonly) {
|
|
143
|
-
if (this.#valuesStore) {
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
debug('initiating session (readonly: %s)', readonly);
|
|
147
|
-
this.#readonly = readonly;
|
|
148
|
-
const contents = await this.#store.read(this.#sessionId);
|
|
149
|
-
this.#valuesStore = new ValuesStore(contents);
|
|
150
|
-
/**
|
|
151
|
-
* Extract flash messages from the store and keep a local
|
|
152
|
-
* copy of it.
|
|
153
|
-
*/
|
|
154
|
-
if (this.has(this.flashKey)) {
|
|
155
|
-
debug('reading flash data');
|
|
156
|
-
if (this.#readonly) {
|
|
157
|
-
this.flashMessages.update(this.get(this.flashKey, null));
|
|
158
|
-
}
|
|
159
|
-
else {
|
|
160
|
-
this.flashMessages.update(this.pull(this.flashKey, null));
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Share session with the templates. We assume the view property
|
|
165
|
-
* is a reference to edge templates
|
|
166
|
-
*/
|
|
167
|
-
if ('view' in this.#ctx) {
|
|
168
|
-
this.#ctx.view.share({
|
|
169
|
-
session: new ReadOnlyValuesStore(this.#valuesStore.all()),
|
|
170
|
-
flashMessages: new ReadOnlyValuesStore(this.flashMessages.all()),
|
|
171
|
-
old: function (key, defaultValue) {
|
|
172
|
-
return this.flashMessages.get(key, defaultValue);
|
|
173
|
-
},
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
this.#emitter.emit('session:initiated', { session: this });
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Put a key-value pair to the session data store
|
|
180
|
-
*/
|
|
181
|
-
put(key, value) {
|
|
182
|
-
this.#getValuesStore('write').set(key, value);
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Check if a key exists inside the datastore
|
|
186
|
-
*/
|
|
187
|
-
has(key) {
|
|
188
|
-
return this.#getValuesStore('read').has(key);
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* Get the value of a key from the session datastore.
|
|
192
|
-
* You can specify a default value to use, when key
|
|
193
|
-
* does not exists or has undefined value.
|
|
194
|
-
*/
|
|
195
|
-
get(key, defaultValue) {
|
|
196
|
-
return this.#getValuesStore('read').get(key, defaultValue);
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Get everything from the session store
|
|
200
|
-
*/
|
|
201
|
-
all() {
|
|
202
|
-
return this.#getValuesStore('read').all();
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Remove a key from the session datastore
|
|
206
|
-
*/
|
|
207
|
-
forget(key) {
|
|
208
|
-
return this.#getValuesStore('write').unset(key);
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Read value for a key from the session datastore
|
|
212
|
-
* and remove it simultaneously.
|
|
213
|
-
*/
|
|
214
|
-
pull(key, defaultValue) {
|
|
215
|
-
return this.#getValuesStore('write').pull(key, defaultValue);
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Increment the value of a key inside the session
|
|
219
|
-
* store.
|
|
220
|
-
*
|
|
221
|
-
* A new key will be defined if does not exists already.
|
|
222
|
-
* The value of a new key will be 1
|
|
223
|
-
*/
|
|
224
|
-
increment(key, steps = 1) {
|
|
225
|
-
return this.#getValuesStore('write').increment(key, steps);
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Increment the value of a key inside the session
|
|
229
|
-
* store.
|
|
230
|
-
*
|
|
231
|
-
* A new key will be defined if does not exists already.
|
|
232
|
-
* The value of a new key will be -1
|
|
233
|
-
*/
|
|
234
|
-
decrement(key, steps = 1) {
|
|
235
|
-
return this.#getValuesStore('write').decrement(key, steps);
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Empty the session store
|
|
239
|
-
*/
|
|
240
|
-
clear() {
|
|
241
|
-
return this.#getValuesStore('write').clear();
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Flash validation error messages. Make sure the error
|
|
245
|
-
* is an instance of VineJS ValidationException
|
|
246
|
-
*/
|
|
247
|
-
flashValidationErrors(error) {
|
|
248
|
-
const errorsBag = error.messages.reduce((result, message) => {
|
|
249
|
-
if (result[message.field]) {
|
|
250
|
-
result[message.field].push(message.message);
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
result[message.field] = [message.message];
|
|
254
|
-
}
|
|
255
|
-
return result;
|
|
256
|
-
}, {});
|
|
257
|
-
this.flashExcept(['_csrf', '_method']);
|
|
258
|
-
this.flash('errors', errorsBag);
|
|
259
|
-
}
|
|
260
|
-
flash(key, value) {
|
|
261
|
-
if (typeof key === 'string') {
|
|
262
|
-
if (value) {
|
|
263
|
-
this.#getFlashStore('write').set(key, value);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
else {
|
|
267
|
-
this.#getFlashStore('write').merge(key);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Flash form input data to the flash messages store
|
|
272
|
-
*/
|
|
273
|
-
flashAll() {
|
|
274
|
-
return this.#getFlashStore('write').set('input', this.#ctx.request.original());
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Flash form input data (except some keys) to the flash messages store
|
|
278
|
-
*/
|
|
279
|
-
flashExcept(keys) {
|
|
280
|
-
this.#getFlashStore('write').set('input', lodash.omit(this.#ctx.request.original(), keys));
|
|
281
|
-
}
|
|
282
|
-
/**
|
|
283
|
-
* Flash form input data (only some keys) to the flash messages store
|
|
284
|
-
*/
|
|
285
|
-
flashOnly(keys) {
|
|
286
|
-
this.#getFlashStore('write').set('input', lodash.pick(this.#ctx.request.original(), keys));
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Reflash messages from the last request in the current response
|
|
290
|
-
*/
|
|
291
|
-
reflash() {
|
|
292
|
-
this.#getFlashStore('write').set('reflashed', this.flashMessages.all());
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* Reflash messages (only some keys) from the last
|
|
296
|
-
* request in the current response
|
|
297
|
-
*/
|
|
298
|
-
reflashOnly(keys) {
|
|
299
|
-
this.#getFlashStore('write').set('reflashed', lodash.pick(this.flashMessages.all(), keys));
|
|
300
|
-
}
|
|
301
|
-
/**
|
|
302
|
-
* Reflash messages (except some keys) from the last
|
|
303
|
-
* request in the current response
|
|
304
|
-
*/
|
|
305
|
-
reflashExcept(keys) {
|
|
306
|
-
this.#getFlashStore('write').set('reflashed', lodash.omit(this.flashMessages.all(), keys));
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Re-generate the session id and migrate data to it.
|
|
310
|
-
*/
|
|
311
|
-
regenerate() {
|
|
312
|
-
this.#sessionId = cuid();
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Commit session changes. No more mutations will be
|
|
316
|
-
* allowed after commit.
|
|
317
|
-
*/
|
|
318
|
-
async commit() {
|
|
319
|
-
if (!this.#valuesStore || this.readonly) {
|
|
320
|
-
return;
|
|
321
|
-
}
|
|
322
|
-
/**
|
|
323
|
-
* If the flash messages store is not empty, we should put
|
|
324
|
-
* its messages inside main session store.
|
|
325
|
-
*/
|
|
326
|
-
if (!this.responseFlashMessages.isEmpty) {
|
|
327
|
-
const { input, reflashed, ...others } = this.responseFlashMessages.all();
|
|
328
|
-
this.put(this.flashKey, { ...reflashed, ...input, ...others });
|
|
329
|
-
}
|
|
330
|
-
debug('committing session data');
|
|
331
|
-
/**
|
|
332
|
-
* Touch the session id cookie to stay alive
|
|
333
|
-
*/
|
|
334
|
-
this.#ctx.response.cookie(this.#config.cookieName, this.#sessionId, this.#config.cookie);
|
|
335
|
-
/**
|
|
336
|
-
* Delete the session data when the session store
|
|
337
|
-
* is empty.
|
|
338
|
-
*
|
|
339
|
-
* Also we only destroy the session id we read from the cookie.
|
|
340
|
-
* If there was no session id in the cookie, there won't be
|
|
341
|
-
* any data inside the store either.
|
|
342
|
-
*/
|
|
343
|
-
if (this.isEmpty) {
|
|
344
|
-
if (this.#sessionIdFromCookie) {
|
|
345
|
-
await this.#store.destroy(this.#sessionIdFromCookie);
|
|
346
|
-
}
|
|
347
|
-
this.#emitter.emit('session:committed', { session: this });
|
|
348
|
-
return;
|
|
349
|
-
}
|
|
350
|
-
/**
|
|
351
|
-
* Touch the store expiry when the session store was
|
|
352
|
-
* not modified.
|
|
353
|
-
*/
|
|
354
|
-
if (!this.hasBeenModified) {
|
|
355
|
-
if (this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId) {
|
|
356
|
-
await this.#store.destroy(this.#sessionIdFromCookie);
|
|
357
|
-
await this.#store.write(this.#sessionId, this.#valuesStore.toJSON());
|
|
358
|
-
this.#emitter.emit('session:migrated', {
|
|
359
|
-
fromSessionId: this.#sessionIdFromCookie,
|
|
360
|
-
toSessionId: this.sessionId,
|
|
361
|
-
session: this,
|
|
362
|
-
});
|
|
363
|
-
}
|
|
364
|
-
else {
|
|
365
|
-
await this.#store.touch(this.#sessionId);
|
|
366
|
-
}
|
|
367
|
-
this.#emitter.emit('session:committed', { session: this });
|
|
368
|
-
return;
|
|
369
|
-
}
|
|
370
|
-
/**
|
|
371
|
-
* Otherwise commit to the session store
|
|
372
|
-
*/
|
|
373
|
-
if (this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId) {
|
|
374
|
-
await this.#store.destroy(this.#sessionIdFromCookie);
|
|
375
|
-
await this.#store.write(this.#sessionId, this.#valuesStore.toJSON());
|
|
376
|
-
this.#emitter.emit('session:migrated', {
|
|
377
|
-
fromSessionId: this.#sessionIdFromCookie,
|
|
378
|
-
toSessionId: this.sessionId,
|
|
379
|
-
session: this,
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
else {
|
|
383
|
-
await this.#store.write(this.#sessionId, this.#valuesStore.toJSON());
|
|
384
|
-
}
|
|
385
|
-
this.#emitter.emit('session:committed', { session: this });
|
|
386
|
-
}
|
|
387
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/session
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import debug from '../debug.js';
|
|
10
|
-
/**
|
|
11
|
-
* Cookie store stores the session data inside an encrypted
|
|
12
|
-
* cookie.
|
|
13
|
-
*/
|
|
14
|
-
export class CookieStore {
|
|
15
|
-
#ctx;
|
|
16
|
-
#config;
|
|
17
|
-
constructor(config, ctx) {
|
|
18
|
-
this.#config = config;
|
|
19
|
-
this.#ctx = ctx;
|
|
20
|
-
debug('initiating cookie store %O', this.#config);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Read session value from the cookie
|
|
24
|
-
*/
|
|
25
|
-
read(sessionId) {
|
|
26
|
-
debug('cookie store: reading session data %s', sessionId);
|
|
27
|
-
const cookieValue = this.#ctx.request.encryptedCookie(sessionId);
|
|
28
|
-
if (typeof cookieValue !== 'object') {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
return cookieValue;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Write session values to the cookie
|
|
35
|
-
*/
|
|
36
|
-
write(sessionId, values) {
|
|
37
|
-
debug('cookie store: writing session data %s: %O', sessionId, values);
|
|
38
|
-
this.#ctx.response.encryptedCookie(sessionId, values, this.#config);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Removes the session cookie
|
|
42
|
-
*/
|
|
43
|
-
destroy(sessionId) {
|
|
44
|
-
debug('cookie store: destroying session data %s', sessionId);
|
|
45
|
-
if (this.#ctx.request.cookiesList()[sessionId]) {
|
|
46
|
-
this.#ctx.response.clearCookie(sessionId);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Updates the cookie with existing cookie values
|
|
51
|
-
*/
|
|
52
|
-
touch(sessionId) {
|
|
53
|
-
const value = this.read(sessionId);
|
|
54
|
-
debug('cookie store: touching session data %s', sessionId);
|
|
55
|
-
if (!value) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
this.write(sessionId, value);
|
|
59
|
-
}
|
|
60
|
-
}
|
package/build/src/stores/file.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @adonisjs/session
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import { dirname, join } from 'node:path';
|
|
10
|
-
import string from '@poppinss/utils/string';
|
|
11
|
-
import { MessageBuilder } from '@adonisjs/core/helpers';
|
|
12
|
-
import { access, mkdir, readFile, rm, writeFile, utimes, stat } from 'node:fs/promises';
|
|
13
|
-
import debug from '../debug.js';
|
|
14
|
-
/**
|
|
15
|
-
* File store writes the session data on the file system as. Each session
|
|
16
|
-
* id gets its own file.
|
|
17
|
-
*/
|
|
18
|
-
export class FileStore {
|
|
19
|
-
#config;
|
|
20
|
-
#age;
|
|
21
|
-
constructor(config, age) {
|
|
22
|
-
this.#config = config;
|
|
23
|
-
this.#age = age;
|
|
24
|
-
debug('initiating file store %O', this.#config);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Returns an absolute path to the session id file
|
|
28
|
-
*/
|
|
29
|
-
#getFilePath(sessionId) {
|
|
30
|
-
return join(this.#config.location, `${sessionId}.txt`);
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Check if a file exists at a given path or not
|
|
34
|
-
*/
|
|
35
|
-
async #pathExists(path) {
|
|
36
|
-
try {
|
|
37
|
-
await access(path);
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
catch {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Returns stats for a file and ignoring missing
|
|
46
|
-
* files.
|
|
47
|
-
*/
|
|
48
|
-
async #stats(path) {
|
|
49
|
-
try {
|
|
50
|
-
const stats = await stat(path);
|
|
51
|
-
return stats;
|
|
52
|
-
}
|
|
53
|
-
catch {
|
|
54
|
-
return null;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Output file with contents to the given path
|
|
59
|
-
*/
|
|
60
|
-
async #outputFile(path, contents) {
|
|
61
|
-
const pathDirname = dirname(path);
|
|
62
|
-
const dirExists = await this.#pathExists(pathDirname);
|
|
63
|
-
if (!dirExists) {
|
|
64
|
-
await mkdir(pathDirname, { recursive: true });
|
|
65
|
-
}
|
|
66
|
-
await writeFile(path, contents, 'utf-8');
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Reads the session data from the disk.
|
|
70
|
-
*/
|
|
71
|
-
async read(sessionId) {
|
|
72
|
-
const filePath = this.#getFilePath(sessionId);
|
|
73
|
-
debug('file store: reading session data %', sessionId);
|
|
74
|
-
/**
|
|
75
|
-
* Return null when no session id file exists in first
|
|
76
|
-
* place
|
|
77
|
-
*/
|
|
78
|
-
const stats = await this.#stats(filePath);
|
|
79
|
-
if (!stats) {
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Check if the file has been expired and return null (if expired)
|
|
84
|
-
*/
|
|
85
|
-
const sessionWillExpireAt = stats.mtimeMs + string.milliseconds.parse(this.#age);
|
|
86
|
-
if (Date.now() > sessionWillExpireAt) {
|
|
87
|
-
debug('file store: expired session data %s', sessionId);
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Reading the file contents if the file exists
|
|
92
|
-
*/
|
|
93
|
-
let contents = await readFile(filePath, 'utf-8');
|
|
94
|
-
contents = contents.trim();
|
|
95
|
-
if (!contents) {
|
|
96
|
-
return null;
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Verify contents with the session id and return them as an object. The verify
|
|
100
|
-
* method can fail when the contents is not JSON>
|
|
101
|
-
*/
|
|
102
|
-
try {
|
|
103
|
-
return new MessageBuilder().verify(contents, sessionId);
|
|
104
|
-
}
|
|
105
|
-
catch {
|
|
106
|
-
return null;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Writes the session data to the disk as a string
|
|
111
|
-
*/
|
|
112
|
-
async write(sessionId, values) {
|
|
113
|
-
debug('file store: writing session data %s: %O', sessionId, values);
|
|
114
|
-
const filePath = this.#getFilePath(sessionId);
|
|
115
|
-
const message = new MessageBuilder().build(values, undefined, sessionId);
|
|
116
|
-
await this.#outputFile(filePath, message);
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Removes the session file from the disk
|
|
120
|
-
*/
|
|
121
|
-
async destroy(sessionId) {
|
|
122
|
-
debug('file store: destroying session data %s', sessionId);
|
|
123
|
-
await rm(this.#getFilePath(sessionId), { force: true });
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Updates the session expiry by rewriting it to the
|
|
127
|
-
* persistence store
|
|
128
|
-
*/
|
|
129
|
-
async touch(sessionId) {
|
|
130
|
-
debug('file store: touching session data %s', sessionId);
|
|
131
|
-
await utimes(this.#getFilePath(sessionId), new Date(), new Date());
|
|
132
|
-
}
|
|
133
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @adonisjs/session
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Memory store is meant to be used for writing tests.
|
|
11
|
-
*/
|
|
12
|
-
export class MemoryStore {
|
|
13
|
-
static sessions = new Map();
|
|
14
|
-
/**
|
|
15
|
-
* Read session id value from the memory
|
|
16
|
-
*/
|
|
17
|
-
read(sessionId) {
|
|
18
|
-
return MemoryStore.sessions.get(sessionId) || null;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Save in memory value for a given session id
|
|
22
|
-
*/
|
|
23
|
-
write(sessionId, values) {
|
|
24
|
-
MemoryStore.sessions.set(sessionId, values);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Cleanup for a single session
|
|
28
|
-
*/
|
|
29
|
-
destroy(sessionId) {
|
|
30
|
-
MemoryStore.sessions.delete(sessionId);
|
|
31
|
-
}
|
|
32
|
-
touch() { }
|
|
33
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @adonisjs/session
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import string from '@poppinss/utils/string';
|
|
10
|
-
import { MessageBuilder } from '@adonisjs/core/helpers';
|
|
11
|
-
import debug from '../debug.js';
|
|
12
|
-
/**
|
|
13
|
-
* File store to read/write session to filesystem
|
|
14
|
-
*/
|
|
15
|
-
export class RedisStore {
|
|
16
|
-
#connection;
|
|
17
|
-
#ttlSeconds;
|
|
18
|
-
constructor(connection, age) {
|
|
19
|
-
this.#connection = connection;
|
|
20
|
-
this.#ttlSeconds = string.seconds.parse(age);
|
|
21
|
-
debug('initiating redis store');
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Returns file contents. A new file will be created if it's
|
|
25
|
-
* missing.
|
|
26
|
-
*/
|
|
27
|
-
async read(sessionId) {
|
|
28
|
-
debug('redis store: reading session data %s', sessionId);
|
|
29
|
-
const contents = await this.#connection.get(sessionId);
|
|
30
|
-
if (!contents) {
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Verify contents with the session id and return them as an object. The verify
|
|
35
|
-
* method can fail when the contents is not JSON>
|
|
36
|
-
*/
|
|
37
|
-
try {
|
|
38
|
-
return new MessageBuilder().verify(contents, sessionId);
|
|
39
|
-
}
|
|
40
|
-
catch {
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Write session values to a file
|
|
46
|
-
*/
|
|
47
|
-
async write(sessionId, values) {
|
|
48
|
-
debug('redis store: writing session data %s, %O', sessionId, values);
|
|
49
|
-
const message = new MessageBuilder().build(values, undefined, sessionId);
|
|
50
|
-
await this.#connection.setex(sessionId, this.#ttlSeconds, message);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Cleanup session file by removing it
|
|
54
|
-
*/
|
|
55
|
-
async destroy(sessionId) {
|
|
56
|
-
debug('redis store: destroying session data %s', sessionId);
|
|
57
|
-
await this.#connection.del(sessionId);
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Updates the value expiry
|
|
61
|
-
*/
|
|
62
|
-
async touch(sessionId) {
|
|
63
|
-
debug('redis store: touching session data %s', sessionId);
|
|
64
|
-
await this.#connection.expire(sessionId, this.#ttlSeconds);
|
|
65
|
-
}
|
|
66
|
-
}
|