@adonisjs/session 6.4.0 → 7.0.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.
- package/build/configure.d.ts +5 -0
- package/build/configure.js +18 -0
- package/build/index.d.ts +12 -0
- package/build/index.js +12 -0
- package/build/providers/session_provider.d.ts +13 -0
- package/build/providers/session_provider.js +43 -0
- package/build/src/bindings/api_client.d.ts +2 -0
- package/build/src/{Bindings/Tests.js → bindings/api_client.js} +8 -13
- package/build/src/bindings/http_context.d.ts +5 -0
- package/build/src/bindings/http_context.js +17 -0
- package/build/{adonis-typings/tests.d.ts → src/bindings/types.d.ts} +26 -5
- package/build/{adonis-typings/session.js → src/bindings/types.js} +2 -1
- package/build/src/{Client/index.d.ts → client.d.ts} +7 -15
- package/build/src/client.js +100 -0
- package/build/src/define_config.d.ts +5 -0
- package/build/src/define_config.js +13 -0
- package/build/src/{Drivers/Cookie.d.ts → drivers/cookie.d.ts} +4 -6
- package/build/src/{Drivers/Cookie.js → drivers/cookie.js} +10 -12
- package/build/src/{Drivers/File.d.ts → drivers/file.d.ts} +3 -8
- package/build/src/{Drivers/File.js → drivers/file.js} +20 -23
- package/build/src/{Drivers/Memory.d.ts → drivers/memory.d.ts} +2 -3
- package/build/src/{Drivers/Memory.js → drivers/memory.js} +3 -7
- package/build/src/{Drivers/Redis.d.ts → drivers/redis.d.ts} +5 -15
- package/build/src/drivers/redis.js +74 -0
- package/build/src/{Session/index.d.ts → session.d.ts} +6 -67
- package/build/src/session.js +373 -0
- package/build/src/session_manager.d.ts +38 -0
- package/build/src/session_manager.js +149 -0
- package/build/src/session_middleware.d.ts +5 -0
- package/build/src/session_middleware.js +20 -0
- package/build/src/{Store/index.d.ts → store.d.ts} +3 -7
- package/build/src/{Store/index.js → store.js} +18 -18
- package/build/src/types.d.ts +61 -0
- package/build/src/types.js +1 -0
- package/build/{templates/session.txt → stubs/config.stub} +12 -15
- package/build/stubs/main.d.ts +1 -0
- package/build/{adonis-typings/tests.js → stubs/main.js} +2 -3
- package/package.json +96 -134
- package/build/adonis-typings/container.d.ts +0 -14
- package/build/adonis-typings/container.js +0 -8
- package/build/adonis-typings/context.d.ts +0 -14
- package/build/adonis-typings/context.js +0 -8
- package/build/adonis-typings/index.d.ts +0 -4
- package/build/adonis-typings/index.js +0 -12
- package/build/adonis-typings/session.d.ts +0 -265
- package/build/config.d.ts +0 -13
- package/build/config.js +0 -18
- package/build/instructions.md +0 -10
- package/build/providers/SessionProvider.d.ts +0 -31
- package/build/providers/SessionProvider.js +0 -56
- package/build/src/Bindings/Server.d.ts +0 -10
- package/build/src/Bindings/Server.js +0 -42
- package/build/src/Bindings/Tests.d.ts +0 -7
- package/build/src/Client/index.js +0 -93
- package/build/src/Drivers/Redis.js +0 -73
- package/build/src/Session/index.js +0 -352
- package/build/src/SessionManager/index.d.ts +0 -78
- package/build/src/SessionManager/index.js +0 -148
|
@@ -0,0 +1,149 @@
|
|
|
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 { Exception } from '@poppinss/utils';
|
|
11
|
+
import { Session } from './session.js';
|
|
12
|
+
import { CookieClient } from '@adonisjs/core/http';
|
|
13
|
+
import { CookieDriver } from './drivers/cookie.js';
|
|
14
|
+
import { MemoryDriver } from './drivers/memory.js';
|
|
15
|
+
import { FileDriver } from './drivers/file.js';
|
|
16
|
+
import { RedisDriver } from './drivers/redis.js';
|
|
17
|
+
import { SessionClient } from './client.js';
|
|
18
|
+
/**
|
|
19
|
+
* Session manager exposes the API to create session instance for a given
|
|
20
|
+
* request and also add new drivers.
|
|
21
|
+
*/
|
|
22
|
+
export class SessionManager {
|
|
23
|
+
/**
|
|
24
|
+
* A private map of drivers added from outside in.
|
|
25
|
+
*/
|
|
26
|
+
#extendedDrivers = new Map();
|
|
27
|
+
/**
|
|
28
|
+
* Reference to session config
|
|
29
|
+
*/
|
|
30
|
+
#config;
|
|
31
|
+
/**
|
|
32
|
+
* Reference to the encryption instance
|
|
33
|
+
*/
|
|
34
|
+
#encryption;
|
|
35
|
+
/**
|
|
36
|
+
* Reference to the redis manager
|
|
37
|
+
*/
|
|
38
|
+
#redis;
|
|
39
|
+
constructor(config, encryption, redis) {
|
|
40
|
+
this.#encryption = encryption;
|
|
41
|
+
this.#redis = redis;
|
|
42
|
+
this.#processConfig(config);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Processes the config and decides the `expires` option for the cookie
|
|
46
|
+
*/
|
|
47
|
+
#processConfig(config) {
|
|
48
|
+
/**
|
|
49
|
+
* Explicitly overwriting `cookie.expires` and `cookie.maxAge` from
|
|
50
|
+
* the user defined config
|
|
51
|
+
*/
|
|
52
|
+
const processedConfig = Object.assign({ enabled: true }, config, {
|
|
53
|
+
cookie: {
|
|
54
|
+
...config.cookie,
|
|
55
|
+
expires: undefined,
|
|
56
|
+
maxAge: undefined,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
/**
|
|
60
|
+
* Set the max age when `clearWithBrowser = false`. Otherwise cookie
|
|
61
|
+
* is a session cookie
|
|
62
|
+
*/
|
|
63
|
+
if (!processedConfig.clearWithBrowser) {
|
|
64
|
+
const age = typeof processedConfig.age === 'string'
|
|
65
|
+
? Math.round(string.milliseconds.parse(processedConfig.age) / 1000)
|
|
66
|
+
: processedConfig.age;
|
|
67
|
+
processedConfig.cookie.maxAge = age;
|
|
68
|
+
}
|
|
69
|
+
this.#config = processedConfig;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns an instance of cookie driver
|
|
73
|
+
*/
|
|
74
|
+
#createCookieDriver(ctx) {
|
|
75
|
+
return new CookieDriver(this.#config, ctx);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns an instance of the memory driver
|
|
79
|
+
*/
|
|
80
|
+
#createMemoryDriver() {
|
|
81
|
+
return new MemoryDriver();
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Returns an instance of file driver
|
|
85
|
+
*/
|
|
86
|
+
#createFileDriver() {
|
|
87
|
+
return new FileDriver(this.#config);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Returns an instance of redis driver
|
|
91
|
+
*/
|
|
92
|
+
#createRedisDriver() {
|
|
93
|
+
if (!this.#redis) {
|
|
94
|
+
throw new Error('Install "@adonisjs/redis" in order to use the redis driver for storing sessions');
|
|
95
|
+
}
|
|
96
|
+
return new RedisDriver(this.#config, this.#redis);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Creates an instance of extended driver
|
|
100
|
+
*/
|
|
101
|
+
#createExtendedDriver(ctx) {
|
|
102
|
+
if (!this.#extendedDrivers.has(this.#config.driver)) {
|
|
103
|
+
throw new Exception(`"${this.#config.driver}" is not a valid session driver`, {
|
|
104
|
+
code: 'E_INVALID_SESSION_DRIVER',
|
|
105
|
+
status: 500,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return this.#extendedDrivers.get(this.#config.driver)(this, this.#config, ctx);
|
|
109
|
+
}
|
|
110
|
+
#createDriver(ctx) {
|
|
111
|
+
switch (this.#config.driver) {
|
|
112
|
+
case 'cookie':
|
|
113
|
+
return this.#createCookieDriver(ctx);
|
|
114
|
+
case 'file':
|
|
115
|
+
return this.#createFileDriver();
|
|
116
|
+
case 'redis':
|
|
117
|
+
return this.#createRedisDriver();
|
|
118
|
+
case 'memory':
|
|
119
|
+
return this.#createMemoryDriver();
|
|
120
|
+
default:
|
|
121
|
+
return this.#createExtendedDriver(ctx);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Find if the sessions are enabled
|
|
126
|
+
*/
|
|
127
|
+
isEnabled() {
|
|
128
|
+
return this.#config.enabled;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Creates an instance of the session client
|
|
132
|
+
*/
|
|
133
|
+
client() {
|
|
134
|
+
const cookieClient = new CookieClient(this.#encryption);
|
|
135
|
+
return new SessionClient(this.#config, this.#createMemoryDriver(), cookieClient, {});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Creates a new session instance for a given HTTP request
|
|
139
|
+
*/
|
|
140
|
+
create(ctx) {
|
|
141
|
+
return new Session(ctx, this.#config, this.#createDriver(ctx));
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Extend the drivers list by adding a new one.
|
|
145
|
+
*/
|
|
146
|
+
extend(driver, callback) {
|
|
147
|
+
this.#extendedDrivers.set(driver, callback);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export default class SessionMiddleware {
|
|
2
|
+
async handle(ctx, next) {
|
|
3
|
+
const sessionManager = (await ctx.containerResolver.make('session'));
|
|
4
|
+
if (!sessionManager.isEnabled()) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Initiate session store
|
|
9
|
+
*/
|
|
10
|
+
await ctx.session.initiate(false);
|
|
11
|
+
/**
|
|
12
|
+
* Call next middlewares or route handler
|
|
13
|
+
*/
|
|
14
|
+
await next();
|
|
15
|
+
/**
|
|
16
|
+
* Commit store mutations
|
|
17
|
+
*/
|
|
18
|
+
await ctx.session.commit();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
import { AllowedSessionValues, StoreContract } from '@ioc:Adonis/Addons/Session';
|
|
1
|
+
import type { AllowedSessionValues } from './types.js';
|
|
3
2
|
/**
|
|
4
3
|
* Session store to mutate and access values from the session object
|
|
5
4
|
*/
|
|
6
|
-
export declare class Store
|
|
7
|
-
|
|
8
|
-
* Underlying store values
|
|
9
|
-
*/
|
|
10
|
-
private values;
|
|
5
|
+
export declare class Store {
|
|
6
|
+
#private;
|
|
11
7
|
constructor(values: {
|
|
12
8
|
[key: string]: any;
|
|
13
9
|
} | null);
|
|
@@ -1,46 +1,47 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/*
|
|
3
2
|
* @adonisjs/redis
|
|
4
3
|
*
|
|
5
|
-
* (c)
|
|
4
|
+
* (c) AdonisJS
|
|
6
5
|
*
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
/// <reference path="../../adonis-typings/index.ts" />
|
|
13
|
-
const utils_1 = require("@poppinss/utils");
|
|
9
|
+
import { Exception } from '@poppinss/utils';
|
|
10
|
+
import lodash from '@poppinss/utils/lodash';
|
|
14
11
|
/**
|
|
15
12
|
* Session store to mutate and access values from the session object
|
|
16
13
|
*/
|
|
17
|
-
class Store {
|
|
14
|
+
export class Store {
|
|
15
|
+
/**
|
|
16
|
+
* Underlying store values
|
|
17
|
+
*/
|
|
18
|
+
#values;
|
|
18
19
|
constructor(values) {
|
|
19
|
-
this
|
|
20
|
+
this.#values = values || {};
|
|
20
21
|
}
|
|
21
22
|
/**
|
|
22
23
|
* Find if store is empty or not
|
|
23
24
|
*/
|
|
24
25
|
get isEmpty() {
|
|
25
|
-
return !this
|
|
26
|
+
return !this.#values || Object.keys(this.#values).length === 0;
|
|
26
27
|
}
|
|
27
28
|
/**
|
|
28
29
|
* Set key/value pair
|
|
29
30
|
*/
|
|
30
31
|
set(key, value) {
|
|
31
|
-
|
|
32
|
+
lodash.set(this.#values, key, value);
|
|
32
33
|
}
|
|
33
34
|
/**
|
|
34
35
|
* Get value for a given key
|
|
35
36
|
*/
|
|
36
37
|
get(key, defaultValue) {
|
|
37
|
-
return
|
|
38
|
+
return lodash.get(this.#values, key, defaultValue);
|
|
38
39
|
}
|
|
39
40
|
/**
|
|
40
41
|
* Remove key
|
|
41
42
|
*/
|
|
42
43
|
unset(key) {
|
|
43
|
-
|
|
44
|
+
lodash.unset(this.#values, key);
|
|
44
45
|
}
|
|
45
46
|
/**
|
|
46
47
|
* Reset store by clearing it's values.
|
|
@@ -65,7 +66,7 @@ class Store {
|
|
|
65
66
|
increment(key, steps = 1) {
|
|
66
67
|
const value = this.get(key, 0);
|
|
67
68
|
if (typeof value !== 'number') {
|
|
68
|
-
throw new
|
|
69
|
+
throw new Exception(`Cannot increment "${key}", since original value is not a number`);
|
|
69
70
|
}
|
|
70
71
|
this.set(key, value + steps);
|
|
71
72
|
}
|
|
@@ -76,7 +77,7 @@ class Store {
|
|
|
76
77
|
decrement(key, steps = 1) {
|
|
77
78
|
const value = this.get(key, 0);
|
|
78
79
|
if (typeof value !== 'number') {
|
|
79
|
-
throw new
|
|
80
|
+
throw new Exception(`Cannot increment "${key}", since original value is not a number`);
|
|
80
81
|
}
|
|
81
82
|
this.set(key, value - steps);
|
|
82
83
|
}
|
|
@@ -84,13 +85,13 @@ class Store {
|
|
|
84
85
|
* Overwrite the underlying values object
|
|
85
86
|
*/
|
|
86
87
|
update(values) {
|
|
87
|
-
this
|
|
88
|
+
this.#values = values;
|
|
88
89
|
}
|
|
89
90
|
/**
|
|
90
91
|
* Update to merge values
|
|
91
92
|
*/
|
|
92
93
|
merge(values) {
|
|
93
|
-
|
|
94
|
+
lodash.merge(this.#values, values);
|
|
94
95
|
}
|
|
95
96
|
/**
|
|
96
97
|
* A boolean to know if value exists. Extra guards to check
|
|
@@ -107,7 +108,7 @@ class Store {
|
|
|
107
108
|
* Get all values
|
|
108
109
|
*/
|
|
109
110
|
all() {
|
|
110
|
-
return this
|
|
111
|
+
return this.#values;
|
|
111
112
|
}
|
|
112
113
|
/**
|
|
113
114
|
* Returns object representation of values
|
|
@@ -128,4 +129,3 @@ class Store {
|
|
|
128
129
|
return JSON.stringify(this.all());
|
|
129
130
|
}
|
|
130
131
|
}
|
|
131
|
-
exports.Store = Store;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { CookieOptions } from '@adonisjs/core/types/http';
|
|
2
|
+
import type { SessionManager } from './session_manager.js';
|
|
3
|
+
import type { HttpContext } from '@adonisjs/core/http';
|
|
4
|
+
/**
|
|
5
|
+
* The callback to be passed to the `extend` method. It is invoked
|
|
6
|
+
* for each request (if extended driver is in use).
|
|
7
|
+
*/
|
|
8
|
+
export type ExtendCallback = (manager: SessionManager, config: SessionConfig, ctx: HttpContext) => SessionDriverContract;
|
|
9
|
+
/**
|
|
10
|
+
* Shape of a driver that every session driver must have
|
|
11
|
+
*/
|
|
12
|
+
export interface SessionDriverContract {
|
|
13
|
+
read(sessionId: string): Promise<Record<string, any> | null> | Record<string, any> | null;
|
|
14
|
+
write(sessionId: string, values: Record<string, any>): Promise<void> | void;
|
|
15
|
+
destroy(sessionId: string): Promise<void> | void;
|
|
16
|
+
touch(sessionId: string): Promise<void> | void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Shape of session config.
|
|
20
|
+
*/
|
|
21
|
+
export interface SessionConfig {
|
|
22
|
+
/**
|
|
23
|
+
* Enable/disable session for the entire application lifecycle
|
|
24
|
+
*/
|
|
25
|
+
enabled: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* The driver in play
|
|
28
|
+
*/
|
|
29
|
+
driver: string;
|
|
30
|
+
/**
|
|
31
|
+
* Cookie name.
|
|
32
|
+
*/
|
|
33
|
+
cookieName: string;
|
|
34
|
+
/**
|
|
35
|
+
* Clear session when browser closes
|
|
36
|
+
*/
|
|
37
|
+
clearWithBrowser: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Age of session cookie
|
|
40
|
+
*/
|
|
41
|
+
age: string | number;
|
|
42
|
+
/**
|
|
43
|
+
* Config for the cookie driver and also the session id
|
|
44
|
+
* cookie
|
|
45
|
+
*/
|
|
46
|
+
cookie: Omit<Partial<CookieOptions>, 'maxAge' | 'expires'>;
|
|
47
|
+
/**
|
|
48
|
+
* Config for the file driver
|
|
49
|
+
*/
|
|
50
|
+
file?: {
|
|
51
|
+
location: string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* The redis connection to use from the `config/redis` file
|
|
55
|
+
*/
|
|
56
|
+
redisConnection?: string;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The values allowed by the `session.put` method
|
|
60
|
+
*/
|
|
61
|
+
export type AllowedSessionValues = string | boolean | number | object | Date | Array<any>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* Feel free to let us know via PR, if you find something broken in this config
|
|
5
|
-
* file.
|
|
6
|
-
*/
|
|
1
|
+
---
|
|
2
|
+
to: {{ app.configPath('session.ts') }}
|
|
3
|
+
---
|
|
7
4
|
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
5
|
+
import env from '#start/env'
|
|
6
|
+
import app from '@adonisjs/core/services/app'
|
|
7
|
+
import { defineConfig } from '@adonisjs/session'
|
|
11
8
|
|
|
12
|
-
export default
|
|
9
|
+
export default defineConfig({
|
|
13
10
|
/*
|
|
14
11
|
|--------------------------------------------------------------------------
|
|
15
12
|
| Enable/Disable sessions
|
|
@@ -36,7 +33,7 @@ export default sessionConfig({
|
|
|
36
33
|
| Note: Switching drivers will make existing sessions invalid.
|
|
37
34
|
|
|
|
38
35
|
*/
|
|
39
|
-
driver:
|
|
36
|
+
driver: env.get('SESSION_DRIVER'),
|
|
40
37
|
|
|
41
38
|
/*
|
|
42
39
|
|--------------------------------------------------------------------------
|
|
@@ -54,7 +51,7 @@ export default sessionConfig({
|
|
|
54
51
|
|--------------------------------------------------------------------------
|
|
55
52
|
|
|
|
56
53
|
| Whether or not you want to destroy the session when browser closes. Setting
|
|
57
|
-
| this value to
|
|
54
|
+
| this value to "true" will ignore the "age".
|
|
58
55
|
|
|
|
59
56
|
*/
|
|
60
57
|
clearWithBrowser: false,
|
|
@@ -70,7 +67,7 @@ export default sessionConfig({
|
|
|
70
67
|
| The value can be a number in milliseconds or a string that must be valid
|
|
71
68
|
| as per https://npmjs.org/package/ms package.
|
|
72
69
|
|
|
|
73
|
-
| Example:
|
|
70
|
+
| Example: "2 days", "2.5 hrs", "1y", "5s" and so on.
|
|
74
71
|
|
|
|
75
72
|
*/
|
|
76
73
|
age: '2h',
|
|
@@ -100,7 +97,7 @@ export default sessionConfig({
|
|
|
100
97
|
|
|
|
101
98
|
*/
|
|
102
99
|
file: {
|
|
103
|
-
location:
|
|
100
|
+
location: app.tmpPath('sessions'),
|
|
104
101
|
},
|
|
105
102
|
|
|
106
103
|
/*
|
|
@@ -109,7 +106,7 @@ export default sessionConfig({
|
|
|
109
106
|
|--------------------------------------------------------------------------
|
|
110
107
|
|
|
|
111
108
|
| The redis connection you want session driver to use. The same connection
|
|
112
|
-
| must be defined inside
|
|
109
|
+
| must be defined inside "config/redis.ts" file as well.
|
|
113
110
|
|
|
|
114
111
|
*/
|
|
115
112
|
redisConnection: 'local',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const stubsRoot: string;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/*
|
|
3
2
|
* @adonisjs/session
|
|
4
3
|
*
|
|
@@ -7,5 +6,5 @@
|
|
|
7
6
|
* For the full copyright and license information, please view the LICENSE
|
|
8
7
|
* file that was distributed with this source code.
|
|
9
8
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
import { getDirname } from '@poppinss/utils';
|
|
10
|
+
export const stubsRoot = getDirname(import.meta.url);
|