/
Memory.ts
46 lines (38 loc) · 1.11 KB
/
Memory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @adonisjs/session
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/// <reference path="../../adonis-typings/index.ts" />
import { SessionDriverContract } from '@ioc:Adonis/Addons/Session'
/**
* Memory driver is meant to be used for writing tests.
*/
export class MemoryDriver implements SessionDriverContract {
public static sessions: Map<string, Object> = new Map()
/**
* Read session id value from the memory
*/
public read(sessionId: string): { [key: string]: any } | null {
return MemoryDriver.sessions.get(sessionId) || null
}
/**
* Save in memory value for a given session id
*/
public write(sessionId: string, values: { [key: string]: any }): void {
if (typeof values !== 'object') {
throw new Error('Session memory driver expects an object of values')
}
MemoryDriver.sessions.set(sessionId, values)
}
/**
* Cleanup for a single session
*/
public destroy(sessionId: string): void {
MemoryDriver.sessions.delete(sessionId)
}
public touch(): void {}
}