@adonisjs/session 7.0.0-11 → 7.0.0-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.
Files changed (40) hide show
  1. package/build/configure.js +1 -1
  2. package/build/factories/session_middleware_factory.d.ts +9 -4
  3. package/build/factories/session_middleware_factory.js +5 -4
  4. package/build/index.d.ts +1 -3
  5. package/build/index.js +1 -3
  6. package/build/providers/session_provider.d.ts +22 -5
  7. package/build/providers/session_provider.js +21 -21
  8. package/build/src/client.d.ts +2 -2
  9. package/build/src/client.js +14 -14
  10. package/build/src/debug.d.ts +1 -1
  11. package/build/src/define_config.d.ts +24 -3
  12. package/build/src/define_config.js +80 -17
  13. package/build/src/plugins/japa/api_client.d.ts +2 -2
  14. package/build/src/plugins/japa/api_client.js +8 -12
  15. package/build/src/plugins/japa/browser_client.d.ts +2 -2
  16. package/build/src/plugins/japa/browser_client.js +8 -12
  17. package/build/src/session.d.ts +7 -7
  18. package/build/src/session.js +40 -37
  19. package/build/src/session_middleware.d.ts +7 -4
  20. package/build/src/session_middleware.js +2 -3
  21. package/build/src/{drivers → stores}/cookie.d.ts +4 -4
  22. package/build/src/{drivers → stores}/cookie.js +7 -7
  23. package/build/src/{drivers → stores}/file.d.ts +4 -4
  24. package/build/src/{drivers → stores}/file.js +8 -8
  25. package/build/src/{drivers → stores}/memory.d.ts +3 -3
  26. package/build/src/{drivers → stores}/memory.js +5 -5
  27. package/build/src/{drivers → stores}/redis.d.ts +5 -5
  28. package/build/src/{drivers → stores}/redis.js +14 -18
  29. package/build/src/{types/main.d.ts → types.d.ts} +11 -31
  30. package/build/src/{store.d.ts → values_store.d.ts} +3 -3
  31. package/build/src/{store.js → values_store.js} +2 -2
  32. package/build/stubs/config.stub +18 -18
  33. package/package.json +20 -20
  34. package/build/src/drivers_collection.d.ts +0 -21
  35. package/build/src/drivers_collection.js +0 -38
  36. package/build/src/helpers.d.ts +0 -6
  37. package/build/src/helpers.js +0 -43
  38. package/build/src/types/extended.d.ts +0 -19
  39. package/build/src/types/main.js +0 -9
  40. /package/build/src/{types/extended.js → types.js} +0 -0
@@ -8,9 +8,9 @@
8
8
  */
9
9
  import lodash from '@poppinss/utils/lodash';
10
10
  import { cuid } from '@adonisjs/core/helpers';
11
- import { ReadOnlyStore, Store } from './store.js';
12
- import * as errors from './errors.js';
13
11
  import debug from './debug.js';
12
+ import * as errors from './errors.js';
13
+ import { ReadOnlyValuesStore, ValuesStore } from './values_store.js';
14
14
  /**
15
15
  * The session class exposes the API to read and write values to
16
16
  * the session store.
@@ -20,11 +20,14 @@ import debug from './debug.js';
20
20
  */
21
21
  export class Session {
22
22
  #config;
23
- #driver;
23
+ #store;
24
24
  #emitter;
25
25
  #ctx;
26
26
  #readonly = false;
27
- #store;
27
+ /**
28
+ * Session values store
29
+ */
30
+ #valuesStore;
28
31
  /**
29
32
  * Session id refers to the session id that will be committed
30
33
  * as a cookie during the response.
@@ -43,11 +46,11 @@ export class Session {
43
46
  * Store of flash messages that be written during the
44
47
  * HTTP request
45
48
  */
46
- responseFlashMessages = new Store({});
49
+ responseFlashMessages = new ValuesStore({});
47
50
  /**
48
51
  * Store of flash messages for the current HTTP request.
49
52
  */
50
- flashMessages = new Store({});
53
+ flashMessages = new ValuesStore({});
51
54
  /**
52
55
  * The key to use for storing flash messages inside
53
56
  * the session store.
@@ -77,7 +80,7 @@ export class Session {
77
80
  * A boolean to know if session store has been initiated
78
81
  */
79
82
  get initiated() {
80
- return !!this.#store;
83
+ return !!this.#valuesStore;
81
84
  }
82
85
  /**
83
86
  * A boolean to know if the session id has been re-generated
@@ -90,20 +93,20 @@ export class Session {
90
93
  * A boolean to know if the session store is empty
91
94
  */
92
95
  get isEmpty() {
93
- return this.#store?.isEmpty ?? true;
96
+ return this.#valuesStore?.isEmpty ?? true;
94
97
  }
95
98
  /**
96
99
  * A boolean to know if the session store has been
97
100
  * modified
98
101
  */
99
102
  get hasBeenModified() {
100
- return this.#store?.hasBeenModified ?? false;
103
+ return this.#valuesStore?.hasBeenModified ?? false;
101
104
  }
102
- constructor(config, driver, emitter, ctx) {
105
+ constructor(config, storeFactory, emitter, ctx) {
103
106
  this.#ctx = ctx;
104
107
  this.#config = config;
105
- this.#driver = driver;
106
108
  this.#emitter = emitter;
109
+ this.#store = storeFactory(ctx, config);
107
110
  this.#sessionIdFromCookie = ctx.request.cookie(config.cookieName, undefined);
108
111
  this.#sessionId = this.#sessionIdFromCookie || cuid();
109
112
  }
@@ -112,7 +115,7 @@ export class Session {
112
115
  * mode
113
116
  */
114
117
  #getFlashStore(mode) {
115
- if (!this.#store) {
118
+ if (!this.#valuesStore) {
116
119
  throw new errors.E_SESSION_NOT_READY();
117
120
  }
118
121
  if (mode === 'write' && this.readonly) {
@@ -123,27 +126,27 @@ export class Session {
123
126
  /**
124
127
  * Returns the store instance for a given mode
125
128
  */
126
- #getStore(mode) {
127
- if (!this.#store) {
129
+ #getValuesStore(mode) {
130
+ if (!this.#valuesStore) {
128
131
  throw new errors.E_SESSION_NOT_READY();
129
132
  }
130
133
  if (mode === 'write' && this.readonly) {
131
134
  throw new errors.E_SESSION_NOT_MUTABLE();
132
135
  }
133
- return this.#store;
136
+ return this.#valuesStore;
134
137
  }
135
138
  /**
136
139
  * Initiates the session store. The method results in a noop
137
140
  * when called multiple times
138
141
  */
139
142
  async initiate(readonly) {
140
- if (this.#store) {
143
+ if (this.#valuesStore) {
141
144
  return;
142
145
  }
143
146
  debug('initiating session (readonly: %s)', readonly);
144
147
  this.#readonly = readonly;
145
- const contents = await this.#driver.read(this.#sessionId);
146
- this.#store = new Store(contents);
148
+ const contents = await this.#store.read(this.#sessionId);
149
+ this.#valuesStore = new ValuesStore(contents);
147
150
  /**
148
151
  * Extract flash messages from the store and keep a local
149
152
  * copy of it.
@@ -163,8 +166,8 @@ export class Session {
163
166
  */
164
167
  if ('view' in this.#ctx) {
165
168
  this.#ctx.view.share({
166
- session: new ReadOnlyStore(this.#store.all()),
167
- flashMessages: new ReadOnlyStore(this.flashMessages.all()),
169
+ session: new ReadOnlyValuesStore(this.#valuesStore.all()),
170
+ flashMessages: new ReadOnlyValuesStore(this.flashMessages.all()),
168
171
  old: function (key, defaultValue) {
169
172
  return this.flashMessages.get(key, defaultValue);
170
173
  },
@@ -176,13 +179,13 @@ export class Session {
176
179
  * Put a key-value pair to the session data store
177
180
  */
178
181
  put(key, value) {
179
- this.#getStore('write').set(key, value);
182
+ this.#getValuesStore('write').set(key, value);
180
183
  }
181
184
  /**
182
185
  * Check if a key exists inside the datastore
183
186
  */
184
187
  has(key) {
185
- return this.#getStore('read').has(key);
188
+ return this.#getValuesStore('read').has(key);
186
189
  }
187
190
  /**
188
191
  * Get the value of a key from the session datastore.
@@ -190,26 +193,26 @@ export class Session {
190
193
  * does not exists or has undefined value.
191
194
  */
192
195
  get(key, defaultValue) {
193
- return this.#getStore('read').get(key, defaultValue);
196
+ return this.#getValuesStore('read').get(key, defaultValue);
194
197
  }
195
198
  /**
196
199
  * Get everything from the session store
197
200
  */
198
201
  all() {
199
- return this.#getStore('read').all();
202
+ return this.#getValuesStore('read').all();
200
203
  }
201
204
  /**
202
205
  * Remove a key from the session datastore
203
206
  */
204
207
  forget(key) {
205
- return this.#getStore('write').unset(key);
208
+ return this.#getValuesStore('write').unset(key);
206
209
  }
207
210
  /**
208
211
  * Read value for a key from the session datastore
209
212
  * and remove it simultaneously.
210
213
  */
211
214
  pull(key, defaultValue) {
212
- return this.#getStore('write').pull(key, defaultValue);
215
+ return this.#getValuesStore('write').pull(key, defaultValue);
213
216
  }
214
217
  /**
215
218
  * Increment the value of a key inside the session
@@ -219,7 +222,7 @@ export class Session {
219
222
  * The value of a new key will be 1
220
223
  */
221
224
  increment(key, steps = 1) {
222
- return this.#getStore('write').increment(key, steps);
225
+ return this.#getValuesStore('write').increment(key, steps);
223
226
  }
224
227
  /**
225
228
  * Increment the value of a key inside the session
@@ -229,13 +232,13 @@ export class Session {
229
232
  * The value of a new key will be -1
230
233
  */
231
234
  decrement(key, steps = 1) {
232
- return this.#getStore('write').decrement(key, steps);
235
+ return this.#getValuesStore('write').decrement(key, steps);
233
236
  }
234
237
  /**
235
238
  * Empty the session store
236
239
  */
237
240
  clear() {
238
- return this.#getStore('write').clear();
241
+ return this.#getValuesStore('write').clear();
239
242
  }
240
243
  /**
241
244
  * Flash validation error messages. Make sure the error
@@ -313,7 +316,7 @@ export class Session {
313
316
  * allowed after commit.
314
317
  */
315
318
  async commit() {
316
- if (!this.#store || this.readonly) {
319
+ if (!this.#valuesStore || this.readonly) {
317
320
  return;
318
321
  }
319
322
  /**
@@ -339,7 +342,7 @@ export class Session {
339
342
  */
340
343
  if (this.isEmpty) {
341
344
  if (this.#sessionIdFromCookie) {
342
- await this.#driver.destroy(this.#sessionIdFromCookie);
345
+ await this.#store.destroy(this.#sessionIdFromCookie);
343
346
  }
344
347
  this.#emitter.emit('session:committed', { session: this });
345
348
  return;
@@ -350,8 +353,8 @@ export class Session {
350
353
  */
351
354
  if (!this.hasBeenModified) {
352
355
  if (this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId) {
353
- await this.#driver.destroy(this.#sessionIdFromCookie);
354
- await this.#driver.write(this.#sessionId, this.#store.toJSON());
356
+ await this.#store.destroy(this.#sessionIdFromCookie);
357
+ await this.#store.write(this.#sessionId, this.#valuesStore.toJSON());
355
358
  this.#emitter.emit('session:migrated', {
356
359
  fromSessionId: this.#sessionIdFromCookie,
357
360
  toSessionId: this.sessionId,
@@ -359,7 +362,7 @@ export class Session {
359
362
  });
360
363
  }
361
364
  else {
362
- await this.#driver.touch(this.#sessionId);
365
+ await this.#store.touch(this.#sessionId);
363
366
  }
364
367
  this.#emitter.emit('session:committed', { session: this });
365
368
  return;
@@ -368,8 +371,8 @@ export class Session {
368
371
  * Otherwise commit to the session store
369
372
  */
370
373
  if (this.#sessionIdFromCookie && this.#sessionIdFromCookie !== this.#sessionId) {
371
- await this.#driver.destroy(this.#sessionIdFromCookie);
372
- await this.#driver.write(this.#sessionId, this.#store.toJSON());
374
+ await this.#store.destroy(this.#sessionIdFromCookie);
375
+ await this.#store.write(this.#sessionId, this.#valuesStore.toJSON());
373
376
  this.#emitter.emit('session:migrated', {
374
377
  fromSessionId: this.#sessionIdFromCookie,
375
378
  toSessionId: this.sessionId,
@@ -377,7 +380,7 @@ export class Session {
377
380
  });
378
381
  }
379
382
  else {
380
- await this.#driver.write(this.#sessionId, this.#store.toJSON());
383
+ await this.#store.write(this.#sessionId, this.#valuesStore.toJSON());
381
384
  }
382
385
  this.#emitter.emit('session:committed', { session: this });
383
386
  }
@@ -2,11 +2,11 @@ import { EmitterService } from '@adonisjs/core/types';
2
2
  import type { NextFn } from '@adonisjs/core/types/http';
3
3
  import { HttpContext } from '@adonisjs/core/http';
4
4
  import { Session } from './session.js';
5
- import type { SessionConfig } from './types/main.js';
5
+ import type { SessionConfig, SessionStoreFactory } from './types.js';
6
6
  /**
7
7
  * HttpContext augmentations
8
8
  */
9
- declare module '@adonisjs/http-server' {
9
+ declare module '@adonisjs/core/http' {
10
10
  interface HttpContext {
11
11
  session: Session;
12
12
  }
@@ -15,8 +15,11 @@ declare module '@adonisjs/http-server' {
15
15
  * Session middleware is used to initiate the session store
16
16
  * and commit its values during an HTTP request
17
17
  */
18
- export default class SessionMiddleware {
18
+ export default class SessionMiddleware<KnownStores extends Record<string, SessionStoreFactory>> {
19
19
  #private;
20
- constructor(config: SessionConfig, emitter: EmitterService);
20
+ constructor(config: SessionConfig & {
21
+ store: keyof KnownStores;
22
+ stores: KnownStores;
23
+ }, emitter: EmitterService);
21
24
  handle(ctx: HttpContext, next: NextFn): Promise<any>;
22
25
  }
@@ -8,7 +8,6 @@
8
8
  */
9
9
  import { ExceptionHandler } from '@adonisjs/core/http';
10
10
  import { Session } from './session.js';
11
- import sessionDriversList from './drivers_collection.js';
12
11
  /**
13
12
  * Overwriting validation exception renderer
14
13
  */
@@ -37,8 +36,8 @@ export default class SessionMiddleware {
37
36
  if (!this.#config.enabled) {
38
37
  return next();
39
38
  }
40
- const driver = sessionDriversList.create(this.#config.driver, this.#config, ctx);
41
- ctx.session = new Session(this.#config, driver, this.#emitter, ctx);
39
+ ctx.session = new Session(this.#config, this.#config.stores[this.#config.store], // reference to store factory
40
+ this.#emitter, ctx);
42
41
  /**
43
42
  * Initiate session store
44
43
  */
@@ -1,11 +1,11 @@
1
1
  import type { HttpContext } from '@adonisjs/core/http';
2
- import { CookieOptions } from '@adonisjs/core/types/http';
3
- import type { SessionData, SessionDriverContract } from '../types/main.js';
2
+ import type { CookieOptions } from '@adonisjs/core/types/http';
3
+ import type { SessionData, SessionStoreContract } from '../types.js';
4
4
  /**
5
- * Cookie driver stores the session data inside an encrypted
5
+ * Cookie store stores the session data inside an encrypted
6
6
  * cookie.
7
7
  */
8
- export declare class CookieDriver implements SessionDriverContract {
8
+ export declare class CookieStore implements SessionStoreContract {
9
9
  #private;
10
10
  constructor(config: Partial<CookieOptions>, ctx: HttpContext);
11
11
  /**
@@ -8,22 +8,22 @@
8
8
  */
9
9
  import debug from '../debug.js';
10
10
  /**
11
- * Cookie driver stores the session data inside an encrypted
11
+ * Cookie store stores the session data inside an encrypted
12
12
  * cookie.
13
13
  */
14
- export class CookieDriver {
14
+ export class CookieStore {
15
15
  #ctx;
16
16
  #config;
17
17
  constructor(config, ctx) {
18
18
  this.#config = config;
19
19
  this.#ctx = ctx;
20
- debug('initiating cookie driver %O', this.#config);
20
+ debug('initiating cookie store %O', this.#config);
21
21
  }
22
22
  /**
23
23
  * Read session value from the cookie
24
24
  */
25
25
  read(sessionId) {
26
- debug('cookie driver: reading session data %s', sessionId);
26
+ debug('cookie store: reading session data %s', sessionId);
27
27
  const cookieValue = this.#ctx.request.encryptedCookie(sessionId);
28
28
  if (typeof cookieValue !== 'object') {
29
29
  return null;
@@ -34,14 +34,14 @@ export class CookieDriver {
34
34
  * Write session values to the cookie
35
35
  */
36
36
  write(sessionId, values) {
37
- debug('cookie driver: writing session data %s: %O', sessionId, values);
37
+ debug('cookie store: writing session data %s: %O', sessionId, values);
38
38
  this.#ctx.response.encryptedCookie(sessionId, values, this.#config);
39
39
  }
40
40
  /**
41
41
  * Removes the session cookie
42
42
  */
43
43
  destroy(sessionId) {
44
- debug('cookie driver: destroying session data %s', sessionId);
44
+ debug('cookie store: destroying session data %s', sessionId);
45
45
  if (this.#ctx.request.cookiesList()[sessionId]) {
46
46
  this.#ctx.response.clearCookie(sessionId);
47
47
  }
@@ -51,7 +51,7 @@ export class CookieDriver {
51
51
  */
52
52
  touch(sessionId) {
53
53
  const value = this.read(sessionId);
54
- debug('cookie driver: touching session data %s', sessionId);
54
+ debug('cookie store: touching session data %s', sessionId);
55
55
  if (!value) {
56
56
  return;
57
57
  }
@@ -6,14 +6,14 @@
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
- import type { FileDriverConfig, SessionData, SessionDriverContract } from '../types/main.js';
9
+ import type { FileStoreConfig, SessionData, SessionStoreContract } from '../types.js';
10
10
  /**
11
- * File driver writes the session data on the file system as. Each session
11
+ * File store writes the session data on the file system as. Each session
12
12
  * id gets its own file.
13
13
  */
14
- export declare class FileDriver implements SessionDriverContract {
14
+ export declare class FileStore implements SessionStoreContract {
15
15
  #private;
16
- constructor(config: FileDriverConfig, age: string | number);
16
+ constructor(config: FileStoreConfig, age: string | number);
17
17
  /**
18
18
  * Reads the session data from the disk.
19
19
  */
@@ -12,16 +12,16 @@ import { MessageBuilder } from '@adonisjs/core/helpers';
12
12
  import { access, mkdir, readFile, rm, writeFile, utimes, stat } from 'node:fs/promises';
13
13
  import debug from '../debug.js';
14
14
  /**
15
- * File driver writes the session data on the file system as. Each session
15
+ * File store writes the session data on the file system as. Each session
16
16
  * id gets its own file.
17
17
  */
18
- export class FileDriver {
18
+ export class FileStore {
19
19
  #config;
20
20
  #age;
21
21
  constructor(config, age) {
22
22
  this.#config = config;
23
23
  this.#age = age;
24
- debug('initiating file driver %O', this.#config);
24
+ debug('initiating file store %O', this.#config);
25
25
  }
26
26
  /**
27
27
  * Returns an absolute path to the session id file
@@ -70,7 +70,7 @@ export class FileDriver {
70
70
  */
71
71
  async read(sessionId) {
72
72
  const filePath = this.#getFilePath(sessionId);
73
- debug('file driver: reading session data %', sessionId);
73
+ debug('file store: reading session data %', sessionId);
74
74
  /**
75
75
  * Return null when no session id file exists in first
76
76
  * place
@@ -84,7 +84,7 @@ export class FileDriver {
84
84
  */
85
85
  const sessionWillExpireAt = stats.mtimeMs + string.milliseconds.parse(this.#age);
86
86
  if (Date.now() > sessionWillExpireAt) {
87
- debug('file driver: expired session data %s', sessionId);
87
+ debug('file store: expired session data %s', sessionId);
88
88
  return null;
89
89
  }
90
90
  /**
@@ -110,7 +110,7 @@ export class FileDriver {
110
110
  * Writes the session data to the disk as a string
111
111
  */
112
112
  async write(sessionId, values) {
113
- debug('file driver: writing session data %s: %O', sessionId, values);
113
+ debug('file store: writing session data %s: %O', sessionId, values);
114
114
  const filePath = this.#getFilePath(sessionId);
115
115
  const message = new MessageBuilder().build(values, undefined, sessionId);
116
116
  await this.#outputFile(filePath, message);
@@ -119,7 +119,7 @@ export class FileDriver {
119
119
  * Removes the session file from the disk
120
120
  */
121
121
  async destroy(sessionId) {
122
- debug('file driver: destroying session data %s', sessionId);
122
+ debug('file store: destroying session data %s', sessionId);
123
123
  await rm(this.#getFilePath(sessionId), { force: true });
124
124
  }
125
125
  /**
@@ -127,7 +127,7 @@ export class FileDriver {
127
127
  * persistence store
128
128
  */
129
129
  async touch(sessionId) {
130
- debug('file driver: touching session data %s', sessionId);
130
+ debug('file store: touching session data %s', sessionId);
131
131
  await utimes(this.#getFilePath(sessionId), new Date(), new Date());
132
132
  }
133
133
  }
@@ -6,11 +6,11 @@
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
- import type { SessionData, SessionDriverContract } from '../types/main.js';
9
+ import type { SessionData, SessionStoreContract } from '../types.js';
10
10
  /**
11
- * Memory driver is meant to be used for writing tests.
11
+ * Memory store is meant to be used for writing tests.
12
12
  */
13
- export declare class MemoryDriver implements SessionDriverContract {
13
+ export declare class MemoryStore implements SessionStoreContract {
14
14
  static sessions: Map<string, SessionData>;
15
15
  /**
16
16
  * Read session id value from the memory
@@ -7,27 +7,27 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  /**
10
- * Memory driver is meant to be used for writing tests.
10
+ * Memory store is meant to be used for writing tests.
11
11
  */
12
- export class MemoryDriver {
12
+ export class MemoryStore {
13
13
  static sessions = new Map();
14
14
  /**
15
15
  * Read session id value from the memory
16
16
  */
17
17
  read(sessionId) {
18
- return MemoryDriver.sessions.get(sessionId) || null;
18
+ return MemoryStore.sessions.get(sessionId) || null;
19
19
  }
20
20
  /**
21
21
  * Save in memory value for a given session id
22
22
  */
23
23
  write(sessionId, values) {
24
- MemoryDriver.sessions.set(sessionId, values);
24
+ MemoryStore.sessions.set(sessionId, values);
25
25
  }
26
26
  /**
27
27
  * Cleanup for a single session
28
28
  */
29
29
  destroy(sessionId) {
30
- MemoryDriver.sessions.delete(sessionId);
30
+ MemoryStore.sessions.delete(sessionId);
31
31
  }
32
32
  touch() { }
33
33
  }
@@ -6,14 +6,14 @@
6
6
  * For the full copyright and license information, please view the LICENSE
7
7
  * file that was distributed with this source code.
8
8
  */
9
- import type { RedisService } from '@adonisjs/redis/types';
10
- import type { SessionDriverContract, RedisDriverConfig, SessionData } from '../types/main.js';
9
+ import type { Connection } from '@adonisjs/redis/types';
10
+ import type { SessionStoreContract, SessionData } from '../types.js';
11
11
  /**
12
- * File driver to read/write session to filesystem
12
+ * File store to read/write session to filesystem
13
13
  */
14
- export declare class RedisDriver implements SessionDriverContract {
14
+ export declare class RedisStore implements SessionStoreContract {
15
15
  #private;
16
- constructor(redis: RedisService, config: RedisDriverConfig, age: string | number);
16
+ constructor(connection: Connection, age: string | number);
17
17
  /**
18
18
  * Returns file contents. A new file will be created if it's
19
19
  * missing.
@@ -10,25 +10,23 @@ import string from '@poppinss/utils/string';
10
10
  import { MessageBuilder } from '@adonisjs/core/helpers';
11
11
  import debug from '../debug.js';
12
12
  /**
13
- * File driver to read/write session to filesystem
13
+ * File store to read/write session to filesystem
14
14
  */
15
- export class RedisDriver {
16
- #config;
17
- #redis;
15
+ export class RedisStore {
16
+ #connection;
18
17
  #ttlSeconds;
19
- constructor(redis, config, age) {
20
- this.#config = config;
21
- this.#redis = redis;
18
+ constructor(connection, age) {
19
+ this.#connection = connection;
22
20
  this.#ttlSeconds = string.seconds.parse(age);
23
- debug('initiating redis driver %O', this.#config);
21
+ debug('initiating redis store');
24
22
  }
25
23
  /**
26
24
  * Returns file contents. A new file will be created if it's
27
25
  * missing.
28
26
  */
29
27
  async read(sessionId) {
30
- debug('redis driver: reading session data %s', sessionId);
31
- const contents = await this.#redis.connection(this.#config.connection).get(sessionId);
28
+ debug('redis store: reading session data %s', sessionId);
29
+ const contents = await this.#connection.get(sessionId);
32
30
  if (!contents) {
33
31
  return null;
34
32
  }
@@ -47,24 +45,22 @@ export class RedisDriver {
47
45
  * Write session values to a file
48
46
  */
49
47
  async write(sessionId, values) {
50
- debug('redis driver: writing session data %s, %O', sessionId, values);
48
+ debug('redis store: writing session data %s, %O', sessionId, values);
51
49
  const message = new MessageBuilder().build(values, undefined, sessionId);
52
- await this.#redis
53
- .connection(this.#config.connection)
54
- .setex(sessionId, this.#ttlSeconds, message);
50
+ await this.#connection.setex(sessionId, this.#ttlSeconds, message);
55
51
  }
56
52
  /**
57
53
  * Cleanup session file by removing it
58
54
  */
59
55
  async destroy(sessionId) {
60
- debug('redis driver: destroying session data %s', sessionId);
61
- await this.#redis.connection(this.#config.connection).del(sessionId);
56
+ debug('redis store: destroying session data %s', sessionId);
57
+ await this.#connection.del(sessionId);
62
58
  }
63
59
  /**
64
60
  * Updates the value expiry
65
61
  */
66
62
  async touch(sessionId) {
67
- debug('redis driver: touching session data %s', sessionId);
68
- await this.#redis.connection(this.#config.connection).expire(sessionId, this.#ttlSeconds);
63
+ debug('redis store: touching session data %s', sessionId);
64
+ await this.#connection.expire(sessionId, this.#ttlSeconds);
69
65
  }
70
66
  }