@adonisjs/session 7.0.0-10 → 7.0.0-12
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/factories/session_middleware_factory.d.ts +9 -4
- package/build/factories/session_middleware_factory.js +5 -4
- package/build/index.d.ts +1 -3
- package/build/index.js +1 -3
- package/build/providers/session_provider.d.ts +22 -5
- package/build/providers/session_provider.js +21 -21
- package/build/src/client.d.ts +24 -19
- package/build/src/client.js +48 -44
- package/build/src/debug.d.ts +1 -1
- package/build/src/define_config.d.ts +24 -3
- package/build/src/define_config.js +80 -17
- package/build/src/{edge_plugin_adonisjs_session.d.ts → plugins/edge.d.ts} +1 -1
- package/build/src/{edge_plugin_adonisjs_session.js → plugins/edge.js} +2 -2
- package/build/src/plugins/japa/api_client.d.ts +75 -0
- package/build/src/plugins/japa/api_client.js +141 -0
- package/build/src/plugins/japa/browser_client.d.ts +36 -0
- package/build/src/plugins/japa/browser_client.js +115 -0
- package/build/src/session.d.ts +7 -7
- package/build/src/session.js +40 -37
- package/build/src/session_middleware.d.ts +7 -4
- package/build/src/session_middleware.js +2 -3
- package/build/src/{drivers → stores}/cookie.d.ts +4 -4
- package/build/src/{drivers → stores}/cookie.js +7 -7
- package/build/src/{drivers → stores}/file.d.ts +4 -4
- package/build/src/{drivers → stores}/file.js +8 -8
- package/build/src/{drivers → stores}/memory.d.ts +3 -3
- package/build/src/{drivers → stores}/memory.js +5 -5
- package/build/src/{drivers → stores}/redis.d.ts +5 -5
- package/build/src/{drivers → stores}/redis.js +14 -18
- package/build/src/{types/main.d.ts → types.d.ts} +11 -31
- package/build/src/{store.d.ts → values_store.d.ts} +3 -3
- package/build/src/{store.js → values_store.js} +2 -2
- package/build/stubs/config.stub +18 -18
- package/package.json +32 -20
- package/build/src/drivers_collection.d.ts +0 -22
- package/build/src/drivers_collection.js +0 -38
- package/build/src/helpers.d.ts +0 -6
- package/build/src/helpers.js +0 -43
- package/build/src/types/extended.d.ts +0 -19
- package/build/src/types/main.js +0 -9
- /package/build/src/{types/extended.js → types.js} +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Emitter } from '@adonisjs/core/events';
|
|
2
|
-
import { EventsList } from '@adonisjs/core/types';
|
|
3
|
-
import { SessionConfig } from '../src/types/main.js';
|
|
2
|
+
import type { EventsList } from '@adonisjs/core/types';
|
|
4
3
|
import SessionMiddleware from '../src/session_middleware.js';
|
|
4
|
+
import type { SessionConfig, SessionStoreFactory } from '../src/types.js';
|
|
5
5
|
/**
|
|
6
6
|
* Exposes the API to create an instance of the session middleware
|
|
7
7
|
* without additional plumbing
|
|
@@ -12,11 +12,16 @@ export declare class SessionMiddlewareFactory {
|
|
|
12
12
|
* Merge custom options
|
|
13
13
|
*/
|
|
14
14
|
merge(options: {
|
|
15
|
-
config?: Partial<SessionConfig
|
|
15
|
+
config?: Partial<SessionConfig> & {
|
|
16
|
+
store: string;
|
|
17
|
+
stores: Record<string, SessionStoreFactory>;
|
|
18
|
+
};
|
|
16
19
|
emitter?: Emitter<EventsList>;
|
|
17
20
|
}): this;
|
|
18
21
|
/**
|
|
19
22
|
* Creates an instance of the session middleware
|
|
20
23
|
*/
|
|
21
|
-
create(): Promise<SessionMiddleware
|
|
24
|
+
create(): Promise<SessionMiddleware<{
|
|
25
|
+
[x: string]: SessionStoreFactory;
|
|
26
|
+
}>>;
|
|
22
27
|
}
|
|
@@ -9,14 +9,16 @@
|
|
|
9
9
|
import { Emitter } from '@adonisjs/core/events';
|
|
10
10
|
import { AppFactory } from '@adonisjs/core/factories/app';
|
|
11
11
|
import { defineConfig } from '../index.js';
|
|
12
|
-
import { registerSessionDriver } from '../src/helpers.js';
|
|
13
12
|
import SessionMiddleware from '../src/session_middleware.js';
|
|
14
13
|
/**
|
|
15
14
|
* Exposes the API to create an instance of the session middleware
|
|
16
15
|
* without additional plumbing
|
|
17
16
|
*/
|
|
18
17
|
export class SessionMiddlewareFactory {
|
|
19
|
-
#config = {
|
|
18
|
+
#config = {
|
|
19
|
+
store: 'memory',
|
|
20
|
+
stores: {},
|
|
21
|
+
};
|
|
20
22
|
#emitter;
|
|
21
23
|
#getApp() {
|
|
22
24
|
return new AppFactory().create(new URL('./', import.meta.url), () => { });
|
|
@@ -40,8 +42,7 @@ export class SessionMiddlewareFactory {
|
|
|
40
42
|
* Creates an instance of the session middleware
|
|
41
43
|
*/
|
|
42
44
|
async create() {
|
|
43
|
-
const config = defineConfig(this.#config);
|
|
44
|
-
await registerSessionDriver(this.#getApp(), config.driver);
|
|
45
|
+
const config = await defineConfig(this.#config).resolver(this.#getApp());
|
|
45
46
|
return new SessionMiddleware(config, this.#getEmitter());
|
|
46
47
|
}
|
|
47
48
|
}
|
package/build/index.d.ts
CHANGED
|
@@ -6,9 +6,7 @@
|
|
|
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 './src/types/extended.js';
|
|
10
9
|
export * as errors from './src/errors.js';
|
|
11
10
|
export { configure } from './configure.js';
|
|
12
11
|
export { stubsRoot } from './stubs/main.js';
|
|
13
|
-
export { defineConfig } from './src/define_config.js';
|
|
14
|
-
export { default as driversList } from './src/drivers_collection.js';
|
|
12
|
+
export { defineConfig, stores } from './src/define_config.js';
|
package/build/index.js
CHANGED
|
@@ -6,9 +6,7 @@
|
|
|
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 './src/types/extended.js';
|
|
10
9
|
export * as errors from './src/errors.js';
|
|
11
10
|
export { configure } from './configure.js';
|
|
12
11
|
export { stubsRoot } from './stubs/main.js';
|
|
13
|
-
export { defineConfig } from './src/define_config.js';
|
|
14
|
-
export { default as driversList } from './src/drivers_collection.js';
|
|
12
|
+
export { defineConfig, stores } from './src/define_config.js';
|
|
@@ -1,5 +1,23 @@
|
|
|
1
|
-
import type { Edge } from 'edge.js';
|
|
2
1
|
import type { ApplicationService } from '@adonisjs/core/types';
|
|
2
|
+
import type { Session } from '../src/session.js';
|
|
3
|
+
/**
|
|
4
|
+
* Events emitted by the session class
|
|
5
|
+
*/
|
|
6
|
+
declare module '@adonisjs/core/types' {
|
|
7
|
+
interface EventsList {
|
|
8
|
+
'session:initiated': {
|
|
9
|
+
session: Session;
|
|
10
|
+
};
|
|
11
|
+
'session:committed': {
|
|
12
|
+
session: Session;
|
|
13
|
+
};
|
|
14
|
+
'session:migrated': {
|
|
15
|
+
fromSessionId: string;
|
|
16
|
+
toSessionId: string;
|
|
17
|
+
session: Session;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
3
21
|
/**
|
|
4
22
|
* Session provider configures the session management inside an
|
|
5
23
|
* AdonisJS application
|
|
@@ -8,16 +26,15 @@ export default class SessionProvider {
|
|
|
8
26
|
protected app: ApplicationService;
|
|
9
27
|
constructor(app: ApplicationService);
|
|
10
28
|
/**
|
|
11
|
-
*
|
|
29
|
+
* Registers edge plugin when edge is installed
|
|
30
|
+
* in the user application.
|
|
12
31
|
*/
|
|
13
|
-
protected
|
|
32
|
+
protected registerEdgePlugin(): Promise<void>;
|
|
14
33
|
/**
|
|
15
34
|
* Registering muddleware
|
|
16
35
|
*/
|
|
17
36
|
register(): void;
|
|
18
37
|
/**
|
|
19
|
-
* Registering the active driver when middleware is used
|
|
20
|
-
* +
|
|
21
38
|
* Adding edge tags (if edge is installed)
|
|
22
39
|
*/
|
|
23
40
|
boot(): Promise<void>;
|
|
@@ -6,8 +6,8 @@
|
|
|
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
|
|
10
|
-
import {
|
|
9
|
+
import { configProvider } from '@adonisjs/core';
|
|
10
|
+
import { RuntimeException } from '@poppinss/utils';
|
|
11
11
|
import SessionMiddleware from '../src/session_middleware.js';
|
|
12
12
|
/**
|
|
13
13
|
* Session provider configures the session management inside an
|
|
@@ -19,16 +19,19 @@ export default class SessionProvider {
|
|
|
19
19
|
this.app = app;
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* Registers edge plugin when edge is installed
|
|
23
|
+
* in the user application.
|
|
23
24
|
*/
|
|
24
|
-
async
|
|
25
|
+
async registerEdgePlugin() {
|
|
26
|
+
let edge = null;
|
|
25
27
|
try {
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
return edge;
|
|
28
|
+
const edgeExports = await import('edge.js');
|
|
29
|
+
edge = edgeExports.default;
|
|
29
30
|
}
|
|
30
|
-
catch {
|
|
31
|
-
|
|
31
|
+
catch { }
|
|
32
|
+
if (edge) {
|
|
33
|
+
const { edgePluginSession } = await import('../src/plugins/edge.js');
|
|
34
|
+
edge.use(edgePluginSession);
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
/**
|
|
@@ -36,25 +39,22 @@ export default class SessionProvider {
|
|
|
36
39
|
*/
|
|
37
40
|
register() {
|
|
38
41
|
this.app.container.singleton(SessionMiddleware, async (resolver) => {
|
|
39
|
-
const
|
|
42
|
+
const sessionConfigProvider = this.app.config.get('session', {});
|
|
43
|
+
/**
|
|
44
|
+
* Resolve config from the provider
|
|
45
|
+
*/
|
|
46
|
+
const config = await configProvider.resolve(this.app, sessionConfigProvider);
|
|
47
|
+
if (!config) {
|
|
48
|
+
throw new RuntimeException('Invalid "config/session.ts" file. Make sure you are using the "defineConfig" method');
|
|
49
|
+
}
|
|
40
50
|
const emitter = await resolver.make('emitter');
|
|
41
51
|
return new SessionMiddleware(config, emitter);
|
|
42
52
|
});
|
|
43
53
|
}
|
|
44
54
|
/**
|
|
45
|
-
* Registering the active driver when middleware is used
|
|
46
|
-
* +
|
|
47
55
|
* Adding edge tags (if edge is installed)
|
|
48
56
|
*/
|
|
49
57
|
async boot() {
|
|
50
|
-
this.
|
|
51
|
-
const config = this.app.config.get('session');
|
|
52
|
-
await registerSessionDriver(this.app, config.driver);
|
|
53
|
-
});
|
|
54
|
-
const edge = await this.getEdge();
|
|
55
|
-
if (edge) {
|
|
56
|
-
const { edgePluginAdonisJSSession } = await import('../src/edge_plugin_adonisjs_session.js');
|
|
57
|
-
edge.use(edgePluginAdonisJSSession);
|
|
58
|
-
}
|
|
58
|
+
await this.registerEdgePlugin();
|
|
59
59
|
}
|
|
60
60
|
}
|
package/build/src/client.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { SessionConfig, SessionData, SessionDriverContract } from './types/main.js';
|
|
1
|
+
import type { SessionData, SessionStoreContract } from './types.js';
|
|
3
2
|
/**
|
|
4
3
|
* Session client exposes the API to set session data as a client
|
|
5
4
|
*/
|
|
@@ -9,27 +8,33 @@ export declare class SessionClient {
|
|
|
9
8
|
* Session key for setting flash messages
|
|
10
9
|
*/
|
|
11
10
|
flashKey: string;
|
|
12
|
-
constructor(config: SessionConfig, driver: SessionDriverContract, cookieClient: CookieClient);
|
|
13
11
|
/**
|
|
14
|
-
*
|
|
12
|
+
* Session to use when no explicit session id is
|
|
13
|
+
* defined
|
|
15
14
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
session: SessionData;
|
|
19
|
-
flashMessages: SessionData;
|
|
20
|
-
}>;
|
|
15
|
+
sessionId: string;
|
|
16
|
+
constructor(store: SessionStoreContract);
|
|
21
17
|
/**
|
|
22
|
-
*
|
|
23
|
-
* the session id and cookie name for it to be accessible
|
|
24
|
-
* by the server
|
|
18
|
+
* Merge session data
|
|
25
19
|
*/
|
|
26
|
-
|
|
27
|
-
sessionId: string;
|
|
28
|
-
signedSessionId: string;
|
|
29
|
-
cookieName: string;
|
|
30
|
-
}>;
|
|
20
|
+
merge(values: SessionData): this;
|
|
31
21
|
/**
|
|
32
|
-
*
|
|
22
|
+
* Merge flash messages
|
|
33
23
|
*/
|
|
34
|
-
|
|
24
|
+
flash(values: SessionData): this;
|
|
25
|
+
/**
|
|
26
|
+
* Commits data to the session store.
|
|
27
|
+
*/
|
|
28
|
+
commit(): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Destroys the session data with the store
|
|
31
|
+
*/
|
|
32
|
+
destroy(sessionId?: string): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Loads session data from the session store
|
|
35
|
+
*/
|
|
36
|
+
load(sessionId?: string): Promise<{
|
|
37
|
+
values: any;
|
|
38
|
+
flashMessages: any;
|
|
39
|
+
}>;
|
|
35
40
|
}
|
package/build/src/client.js
CHANGED
|
@@ -7,75 +7,79 @@
|
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
9
|
import { cuid } from '@adonisjs/core/helpers';
|
|
10
|
-
import
|
|
10
|
+
import debug from './debug.js';
|
|
11
|
+
import { ValuesStore } from './values_store.js';
|
|
11
12
|
/**
|
|
12
13
|
* Session client exposes the API to set session data as a client
|
|
13
14
|
*/
|
|
14
15
|
export class SessionClient {
|
|
15
16
|
/**
|
|
16
|
-
*
|
|
17
|
+
* Data store
|
|
17
18
|
*/
|
|
18
|
-
#
|
|
19
|
+
#valuesStore = new ValuesStore({});
|
|
19
20
|
/**
|
|
20
|
-
*
|
|
21
|
+
* Flash messages store
|
|
21
22
|
*/
|
|
22
|
-
#
|
|
23
|
+
#flashMessagesStore = new ValuesStore({});
|
|
23
24
|
/**
|
|
24
|
-
*
|
|
25
|
+
* The session store to use for reading and writing session data
|
|
25
26
|
*/
|
|
26
|
-
#
|
|
27
|
+
#store;
|
|
28
|
+
/**
|
|
29
|
+
* Session key for setting flash messages
|
|
30
|
+
*/
|
|
31
|
+
flashKey = '__flash__';
|
|
27
32
|
/**
|
|
28
33
|
* Session to use when no explicit session id is
|
|
29
34
|
* defined
|
|
30
35
|
*/
|
|
31
|
-
|
|
36
|
+
sessionId = cuid();
|
|
37
|
+
constructor(store) {
|
|
38
|
+
this.#store = store;
|
|
39
|
+
}
|
|
32
40
|
/**
|
|
33
|
-
*
|
|
41
|
+
* Merge session data
|
|
34
42
|
*/
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
this
|
|
38
|
-
this.#driver = driver;
|
|
39
|
-
this.#cookieClient = cookieClient;
|
|
43
|
+
merge(values) {
|
|
44
|
+
this.#valuesStore.merge(values);
|
|
45
|
+
return this;
|
|
40
46
|
}
|
|
41
47
|
/**
|
|
42
|
-
*
|
|
48
|
+
* Merge flash messages
|
|
43
49
|
*/
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const contents = await this.#driver.read(sessId);
|
|
48
|
-
const store = new Store(contents);
|
|
49
|
-
const flashMessages = store.pull(this.flashKey, null);
|
|
50
|
-
return {
|
|
51
|
-
sessionId: sessId,
|
|
52
|
-
session: store.all(),
|
|
53
|
-
flashMessages,
|
|
54
|
-
};
|
|
50
|
+
flash(values) {
|
|
51
|
+
this.#flashMessagesStore.merge(values);
|
|
52
|
+
return this;
|
|
55
53
|
}
|
|
56
54
|
/**
|
|
57
|
-
* Commits
|
|
58
|
-
* the session id and cookie name for it to be accessible
|
|
59
|
-
* by the server
|
|
55
|
+
* Commits data to the session store.
|
|
60
56
|
*/
|
|
61
|
-
async commit(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (
|
|
67
|
-
|
|
57
|
+
async commit() {
|
|
58
|
+
if (!this.#flashMessagesStore.isEmpty) {
|
|
59
|
+
this.#valuesStore.set(this.flashKey, this.#flashMessagesStore.toJSON());
|
|
60
|
+
}
|
|
61
|
+
debug('committing session data during api request');
|
|
62
|
+
if (!this.#valuesStore.isEmpty) {
|
|
63
|
+
this.#store.write(this.sessionId, this.#valuesStore.toJSON());
|
|
68
64
|
}
|
|
69
|
-
return {
|
|
70
|
-
sessionId: sessId,
|
|
71
|
-
signedSessionId: this.#cookieClient.sign(this.#config.cookieName, sessId),
|
|
72
|
-
cookieName: this.#config.cookieName,
|
|
73
|
-
};
|
|
74
65
|
}
|
|
75
66
|
/**
|
|
76
|
-
*
|
|
67
|
+
* Destroys the session data with the store
|
|
77
68
|
*/
|
|
78
|
-
async
|
|
79
|
-
|
|
69
|
+
async destroy(sessionId) {
|
|
70
|
+
debug('destroying session data during api request');
|
|
71
|
+
this.#store.destroy(sessionId || this.sessionId);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Loads session data from the session store
|
|
75
|
+
*/
|
|
76
|
+
async load(sessionId) {
|
|
77
|
+
const contents = await this.#store.read(sessionId || this.sessionId);
|
|
78
|
+
const store = new ValuesStore(contents);
|
|
79
|
+
const flashMessages = store.pull(this.flashKey, {});
|
|
80
|
+
return {
|
|
81
|
+
values: store.all(),
|
|
82
|
+
flashMessages,
|
|
83
|
+
};
|
|
80
84
|
}
|
|
81
85
|
}
|
package/build/src/debug.d.ts
CHANGED
|
@@ -1,8 +1,29 @@
|
|
|
1
|
+
import type { ConfigProvider } from '@adonisjs/core/types';
|
|
1
2
|
import type { CookieOptions } from '@adonisjs/core/types/http';
|
|
2
|
-
import type { SessionConfig } from './types
|
|
3
|
+
import type { SessionConfig, FileStoreConfig, RedisStoreConfig, SessionStoreFactory } from './types.js';
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
+
* Resolved config with stores
|
|
5
6
|
*/
|
|
6
|
-
|
|
7
|
+
type ResolvedConfig<KnownStores extends Record<string, SessionStoreFactory>> = SessionConfig & {
|
|
8
|
+
store: keyof KnownStores;
|
|
9
|
+
stores: KnownStores;
|
|
7
10
|
cookie: Partial<CookieOptions>;
|
|
8
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* Helper to normalize session config
|
|
14
|
+
*/
|
|
15
|
+
export declare function defineConfig<KnownStores extends Record<string, SessionStoreFactory | ConfigProvider<SessionStoreFactory>>>(config: Partial<SessionConfig> & {
|
|
16
|
+
store: keyof KnownStores | 'memory';
|
|
17
|
+
stores: KnownStores;
|
|
18
|
+
}): ConfigProvider<ResolvedConfig<{
|
|
19
|
+
[K in keyof KnownStores]: SessionStoreFactory;
|
|
20
|
+
}>>;
|
|
21
|
+
/**
|
|
22
|
+
* Inbuilt stores to store the session data.
|
|
23
|
+
*/
|
|
24
|
+
export declare const stores: {
|
|
25
|
+
file: (config: FileStoreConfig) => ConfigProvider<SessionStoreFactory>;
|
|
26
|
+
redis: (config: RedisStoreConfig) => ConfigProvider<SessionStoreFactory>;
|
|
27
|
+
cookie: () => ConfigProvider<SessionStoreFactory>;
|
|
28
|
+
};
|
|
29
|
+
export {};
|
|
@@ -6,37 +6,100 @@
|
|
|
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
|
+
/// <reference types="@adonisjs/redis/redis_provider" />
|
|
9
10
|
import string from '@poppinss/utils/string';
|
|
11
|
+
import { configProvider } from '@adonisjs/core';
|
|
10
12
|
import { InvalidArgumentsException } from '@poppinss/utils';
|
|
11
13
|
import debug from './debug.js';
|
|
14
|
+
import { MemoryStore } from './stores/memory.js';
|
|
12
15
|
/**
|
|
13
16
|
* Helper to normalize session config
|
|
14
17
|
*/
|
|
15
18
|
export function defineConfig(config) {
|
|
19
|
+
debug('processing session config %O', config);
|
|
16
20
|
/**
|
|
17
|
-
* Make sure a
|
|
21
|
+
* Make sure a store is defined
|
|
18
22
|
*/
|
|
19
|
-
if (!config.
|
|
20
|
-
throw new InvalidArgumentsException('Missing "
|
|
23
|
+
if (!config.store) {
|
|
24
|
+
throw new InvalidArgumentsException('Missing "store" property inside the session config');
|
|
21
25
|
}
|
|
22
|
-
const age = config.age || '2h';
|
|
23
|
-
const clearWithBrowser = config.clearWithBrowser ?? false;
|
|
24
|
-
const cookieOptions = { ...config.cookie };
|
|
25
26
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
27
|
+
* Destructuring config with the default values. We pull out
|
|
28
|
+
* stores and cookie values, since we have to transform
|
|
29
|
+
* them in the output value.
|
|
28
30
|
*/
|
|
29
|
-
|
|
30
|
-
debug('computing maxAge for session id cookie');
|
|
31
|
-
cookieOptions.maxAge = string.seconds.parse(config.age || age);
|
|
32
|
-
}
|
|
33
|
-
return {
|
|
31
|
+
const { stores, cookie, ...rest } = {
|
|
34
32
|
enabled: true,
|
|
35
|
-
age,
|
|
36
|
-
clearWithBrowser,
|
|
33
|
+
age: '2h',
|
|
37
34
|
cookieName: 'adonis_session',
|
|
38
|
-
|
|
39
|
-
driver: config.driver,
|
|
35
|
+
clearWithBrowser: false,
|
|
40
36
|
...config,
|
|
41
37
|
};
|
|
38
|
+
const cookieOptions = { ...cookie };
|
|
39
|
+
/**
|
|
40
|
+
* Define maxAge property when session id cookie is
|
|
41
|
+
* not a session cookie.
|
|
42
|
+
*/
|
|
43
|
+
if (!rest.clearWithBrowser) {
|
|
44
|
+
cookieOptions.maxAge = string.seconds.parse(rest.age);
|
|
45
|
+
debug('computing maxAge "%s" for session id cookie', cookieOptions.maxAge);
|
|
46
|
+
}
|
|
47
|
+
return configProvider.create(async (app) => {
|
|
48
|
+
const storesNames = Object.keys(config.stores);
|
|
49
|
+
/**
|
|
50
|
+
* List of stores with memory store always configured
|
|
51
|
+
*/
|
|
52
|
+
const storesList = {
|
|
53
|
+
memory: () => new MemoryStore(),
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Looping for stores and resolving them
|
|
57
|
+
*/
|
|
58
|
+
for (let storeName of storesNames) {
|
|
59
|
+
const store = config.stores[storeName];
|
|
60
|
+
if (typeof store === 'function') {
|
|
61
|
+
storesList[storeName] = store;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
storesList[storeName] = await store.resolver(app);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const transformedConfig = {
|
|
68
|
+
...rest,
|
|
69
|
+
cookie: cookieOptions,
|
|
70
|
+
stores: storesList,
|
|
71
|
+
};
|
|
72
|
+
debug('transformed session config %O', transformedConfig);
|
|
73
|
+
return transformedConfig;
|
|
74
|
+
});
|
|
42
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Inbuilt stores to store the session data.
|
|
78
|
+
*/
|
|
79
|
+
export const stores = {
|
|
80
|
+
file: (config) => {
|
|
81
|
+
return configProvider.create(async () => {
|
|
82
|
+
const { FileStore } = await import('./stores/file.js');
|
|
83
|
+
return (_, sessionConfig) => {
|
|
84
|
+
return new FileStore(config, sessionConfig.age);
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
},
|
|
88
|
+
redis: (config) => {
|
|
89
|
+
return configProvider.create(async (app) => {
|
|
90
|
+
const { RedisStore } = await import('./stores/redis.js');
|
|
91
|
+
const redis = await app.container.make('redis');
|
|
92
|
+
return (_, sessionConfig) => {
|
|
93
|
+
return new RedisStore(redis.connection(config.connection), sessionConfig.age);
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
cookie: () => {
|
|
98
|
+
return configProvider.create(async () => {
|
|
99
|
+
const { CookieStore } = await import('./stores/cookie.js');
|
|
100
|
+
return (ctx, sessionConfig) => {
|
|
101
|
+
return new CookieStore(sessionConfig.cookie, ctx);
|
|
102
|
+
};
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
};
|
|
@@ -6,12 +6,12 @@
|
|
|
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 debug from '
|
|
9
|
+
import debug from '../debug.js';
|
|
10
10
|
/**
|
|
11
11
|
* The edge plugin for AdonisJS Session adds tags to read
|
|
12
12
|
* flash messages
|
|
13
13
|
*/
|
|
14
|
-
export const
|
|
14
|
+
export const edgePluginSession = (edge) => {
|
|
15
15
|
debug('registering session tags with edge');
|
|
16
16
|
edge.registerTag({
|
|
17
17
|
tagName: 'flashMessage',
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { PluginFn } from '@japa/runner/types';
|
|
2
|
+
import type { ApplicationService } from '@adonisjs/core/types';
|
|
3
|
+
import { SessionClient } from '../../client.js';
|
|
4
|
+
import type { SessionData } from '../../types.js';
|
|
5
|
+
declare module '@japa/api-client' {
|
|
6
|
+
interface ApiRequest {
|
|
7
|
+
sessionClient: SessionClient;
|
|
8
|
+
/**
|
|
9
|
+
* Make HTTP request along with the provided session data
|
|
10
|
+
*/
|
|
11
|
+
withSession(values: SessionData): this;
|
|
12
|
+
/**
|
|
13
|
+
* Make HTTP request along with the provided session flash
|
|
14
|
+
* messages.
|
|
15
|
+
*/
|
|
16
|
+
withFlashMessages(values: SessionData): this;
|
|
17
|
+
}
|
|
18
|
+
interface ApiResponse {
|
|
19
|
+
sessionBag: {
|
|
20
|
+
values: SessionData;
|
|
21
|
+
flashMessages: SessionData;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Get session data from the HTTP response
|
|
25
|
+
*/
|
|
26
|
+
session(key?: string): any;
|
|
27
|
+
/**
|
|
28
|
+
* Get flash messages from the HTTP response
|
|
29
|
+
*/
|
|
30
|
+
flashMessages(): SessionData;
|
|
31
|
+
/**
|
|
32
|
+
* Get flash messages for a specific key from the HTTP response
|
|
33
|
+
*/
|
|
34
|
+
flashMessage(key: string): SessionData;
|
|
35
|
+
/**
|
|
36
|
+
* Assert session key-value pair exists
|
|
37
|
+
*/
|
|
38
|
+
assertSession(key: string, value?: any): void;
|
|
39
|
+
/**
|
|
40
|
+
* Assert key is missing in session store
|
|
41
|
+
*/
|
|
42
|
+
assertSessionMissing(key: string): void;
|
|
43
|
+
/**
|
|
44
|
+
* Assert flash message key-value pair exists
|
|
45
|
+
*/
|
|
46
|
+
assertFlashMessage(key: string, value?: any): void;
|
|
47
|
+
/**
|
|
48
|
+
* Assert key is missing flash messages store
|
|
49
|
+
*/
|
|
50
|
+
assertFlashMissing(key: string): void;
|
|
51
|
+
/**
|
|
52
|
+
* Assert flash messages has validation errors for
|
|
53
|
+
* the given field
|
|
54
|
+
*/
|
|
55
|
+
assertHasValidationError(field: string): void;
|
|
56
|
+
/**
|
|
57
|
+
* Assert flash messages does not have validation errors
|
|
58
|
+
* for the given field
|
|
59
|
+
*/
|
|
60
|
+
assertDoesNotHaveValidationError(field: string): void;
|
|
61
|
+
/**
|
|
62
|
+
* Assert error message for a given field
|
|
63
|
+
*/
|
|
64
|
+
assertValidationError(field: string, message: string): void;
|
|
65
|
+
/**
|
|
66
|
+
* Assert all error messages for a given field
|
|
67
|
+
*/
|
|
68
|
+
assertValidationErrors(field: string, messages: string[]): void;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Hooks AdonisJS Session with the Japa API client
|
|
73
|
+
* plugin
|
|
74
|
+
*/
|
|
75
|
+
export declare const sessionApiClient: (app: ApplicationService) => PluginFn;
|