@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,18 @@
|
|
|
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
|
+
* Configures the package
|
|
11
|
+
*/
|
|
12
|
+
export async function configure(command) {
|
|
13
|
+
await command.publishStub('config.stub');
|
|
14
|
+
await command.defineEnvVariables({ SESSION_DRIVER: 'cookie' });
|
|
15
|
+
await command.updateRcFile((rcFile) => {
|
|
16
|
+
rcFile.addProvider('@adonisjs/session/session_provider');
|
|
17
|
+
});
|
|
18
|
+
}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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 './src/bindings/types.js';
|
|
10
|
+
export { defineConfig } from './src/define_config.js';
|
|
11
|
+
export { stubsRoot } from './stubs/main.js';
|
|
12
|
+
export { configure } from './configure.js';
|
package/build/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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 './src/bindings/types.js';
|
|
10
|
+
export { defineConfig } from './src/define_config.js';
|
|
11
|
+
export { stubsRoot } from './stubs/main.js';
|
|
12
|
+
export { configure } from './configure.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ApplicationService } from '@adonisjs/core/types';
|
|
2
|
+
export default class SessionProvider {
|
|
3
|
+
protected app: ApplicationService;
|
|
4
|
+
constructor(app: ApplicationService);
|
|
5
|
+
/**
|
|
6
|
+
* Register Session Manager in the container
|
|
7
|
+
*/
|
|
8
|
+
register(): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Register bindings
|
|
11
|
+
*/
|
|
12
|
+
boot(): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
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 { extendHttpContext } from '../src/bindings/http_context.js';
|
|
10
|
+
import { extendApiClient } from '../src/bindings/api_client.js';
|
|
11
|
+
export default class SessionProvider {
|
|
12
|
+
app;
|
|
13
|
+
constructor(app) {
|
|
14
|
+
this.app = app;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Register Session Manager in the container
|
|
18
|
+
*/
|
|
19
|
+
async register() {
|
|
20
|
+
this.app.container.singleton('session', async () => {
|
|
21
|
+
const { SessionManager } = await import('../src/session_manager.js');
|
|
22
|
+
const encryption = await this.app.container.make('encryption');
|
|
23
|
+
const redis = await this.app.container.make('redis').catch(() => undefined);
|
|
24
|
+
const config = this.app.config.get('session', {});
|
|
25
|
+
return new SessionManager(config, encryption, redis);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Register bindings
|
|
30
|
+
*/
|
|
31
|
+
async boot() {
|
|
32
|
+
const sessionManager = await this.app.container.make('session');
|
|
33
|
+
/**
|
|
34
|
+
* Add `session` getter to the HttpContext class
|
|
35
|
+
*/
|
|
36
|
+
extendHttpContext(sessionManager);
|
|
37
|
+
/**
|
|
38
|
+
* Add some macros and getter to japa/api-client classes for
|
|
39
|
+
* easier testing
|
|
40
|
+
*/
|
|
41
|
+
extendApiClient(sessionManager);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/*
|
|
3
2
|
* @adonisjs/session
|
|
4
3
|
*
|
|
@@ -7,18 +6,14 @@
|
|
|
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
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Define test bindings
|
|
15
|
-
*/
|
|
16
|
-
function defineTestsBindings(ApiRequest, ApiResponse, ApiClient, SessionManager) {
|
|
9
|
+
import { ApiRequest, ApiResponse, ApiClient } from '@japa/api-client';
|
|
10
|
+
import { inspect } from 'node:util';
|
|
11
|
+
export function extendApiClient(sessionManager) {
|
|
17
12
|
/**
|
|
18
13
|
* Set "sessionClient" on the api request
|
|
19
14
|
*/
|
|
20
15
|
ApiRequest.getter('sessionClient', function () {
|
|
21
|
-
return
|
|
16
|
+
return sessionManager.client();
|
|
22
17
|
}, true);
|
|
23
18
|
/**
|
|
24
19
|
* Send session values in the request
|
|
@@ -95,8 +90,9 @@ function defineTestsBindings(ApiRequest, ApiResponse, ApiClient, SessionManager)
|
|
|
95
90
|
*/
|
|
96
91
|
ApiResponse.macro('dumpSession', function (options) {
|
|
97
92
|
const inspectOptions = { depth: 2, showHidden: false, colors: true, ...options };
|
|
98
|
-
console.log(`"session" => ${
|
|
99
|
-
console.log(`"flashMessages" => ${
|
|
93
|
+
console.log(`"session" => ${inspect(this.session(), inspectOptions)}`);
|
|
94
|
+
console.log(`"flashMessages" => ${inspect(this.flashMessages(), inspectOptions)}`);
|
|
95
|
+
return this;
|
|
100
96
|
});
|
|
101
97
|
/**
|
|
102
98
|
* Adding hooks directly on the request object moves the hooks to
|
|
@@ -113,7 +109,7 @@ function defineTestsBindings(ApiRequest, ApiResponse, ApiClient, SessionManager)
|
|
|
113
109
|
* cookie
|
|
114
110
|
*/
|
|
115
111
|
const { cookieName, sessionId } = await request.sessionClient.commit();
|
|
116
|
-
request.
|
|
112
|
+
request.withCookie(cookieName, sessionId);
|
|
117
113
|
/**
|
|
118
114
|
* Cleanup if request has error. Otherwise the teardown
|
|
119
115
|
* hook will clear
|
|
@@ -137,4 +133,3 @@ function defineTestsBindings(ApiRequest, ApiResponse, ApiClient, SessionManager)
|
|
|
137
133
|
});
|
|
138
134
|
});
|
|
139
135
|
}
|
|
140
|
-
exports.defineTestsBindings = defineTestsBindings;
|
|
@@ -0,0 +1,17 @@
|
|
|
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 { HttpContext } from '@adonisjs/core/http';
|
|
10
|
+
/**
|
|
11
|
+
* Extends HttpContext class with the ally getter
|
|
12
|
+
*/
|
|
13
|
+
export function extendHttpContext(session) {
|
|
14
|
+
HttpContext.getter('session', function () {
|
|
15
|
+
return session.create(this);
|
|
16
|
+
}, true);
|
|
17
|
+
}
|
|
@@ -1,10 +1,31 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import '
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
/// <reference types="@types/node" resolution-mode="require"/>
|
|
2
|
+
import { InspectOptions } from 'node:util';
|
|
3
|
+
import type { SessionClient } from '../client.js';
|
|
4
|
+
import type { Session } from '../session.js';
|
|
5
|
+
import type { SessionManager } from '../session_manager.js';
|
|
6
|
+
import type { AllowedSessionValues } from '../types.js';
|
|
7
|
+
/**
|
|
8
|
+
* HttpContext augmentations
|
|
9
|
+
*/
|
|
10
|
+
declare module '@adonisjs/core/http' {
|
|
11
|
+
interface HttpContext {
|
|
12
|
+
session: Session;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Container augmentations
|
|
17
|
+
*/
|
|
18
|
+
declare module '@adonisjs/core/types' {
|
|
19
|
+
interface ContainerBindings {
|
|
20
|
+
session: SessionManager;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Japa api client augmentations
|
|
25
|
+
*/
|
|
5
26
|
declare module '@japa/api-client' {
|
|
6
27
|
interface ApiRequest {
|
|
7
|
-
sessionClient:
|
|
28
|
+
sessionClient: SessionClient;
|
|
8
29
|
/**
|
|
9
30
|
* Send session values in the request
|
|
10
31
|
*/
|
|
@@ -1,30 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import { CookieClientContract } from '@ioc:Adonis/Core/CookieClient';
|
|
5
|
-
import { Store } from '../Store';
|
|
1
|
+
import { Store } from './store.js';
|
|
2
|
+
import type { SessionConfig, SessionDriverContract } from './types.js';
|
|
3
|
+
import type { CookieClient } from '@adonisjs/core/http';
|
|
6
4
|
/**
|
|
7
5
|
* SessionClient exposes the API to set session data as a client
|
|
8
6
|
*/
|
|
9
|
-
export declare class SessionClient extends Store
|
|
10
|
-
private
|
|
11
|
-
private driver;
|
|
12
|
-
private cookieClient;
|
|
7
|
+
export declare class SessionClient extends Store {
|
|
8
|
+
#private;
|
|
13
9
|
/**
|
|
14
10
|
* Each instance of client works on a single session id. Generate
|
|
15
11
|
* multiple client instances for a different session id
|
|
16
12
|
*/
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Session key for setting flash messages
|
|
20
|
-
*/
|
|
21
|
-
private flashMessagesKey;
|
|
13
|
+
sessionId: string;
|
|
22
14
|
/**
|
|
23
15
|
* Flash messages store. They are merged with the session data during
|
|
24
16
|
* commit
|
|
25
17
|
*/
|
|
26
18
|
flashMessages: Store;
|
|
27
|
-
constructor(config: SessionConfig, driver: SessionDriverContract, cookieClient:
|
|
19
|
+
constructor(config: SessionConfig, driver: SessionDriverContract, cookieClient: CookieClient, values: {
|
|
28
20
|
[key: string]: any;
|
|
29
21
|
} | null);
|
|
30
22
|
/**
|
|
@@ -0,0 +1,100 @@
|
|
|
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 { cuid } from '@adonisjs/core/helpers';
|
|
10
|
+
import { Store } from './store.js';
|
|
11
|
+
/**
|
|
12
|
+
* SessionClient exposes the API to set session data as a client
|
|
13
|
+
*/
|
|
14
|
+
export class SessionClient extends Store {
|
|
15
|
+
/**
|
|
16
|
+
* Session configuration
|
|
17
|
+
*/
|
|
18
|
+
#config;
|
|
19
|
+
/**
|
|
20
|
+
* The session driver used to read and write session data
|
|
21
|
+
*/
|
|
22
|
+
#driver;
|
|
23
|
+
/**
|
|
24
|
+
* Cookie client contract to sign and unsign cookies
|
|
25
|
+
*/
|
|
26
|
+
#cookieClient;
|
|
27
|
+
/**
|
|
28
|
+
* Each instance of client works on a single session id. Generate
|
|
29
|
+
* multiple client instances for a different session id
|
|
30
|
+
*/
|
|
31
|
+
sessionId = cuid();
|
|
32
|
+
/**
|
|
33
|
+
* Session key for setting flash messages
|
|
34
|
+
*/
|
|
35
|
+
#flashMessagesKey = '__flash__';
|
|
36
|
+
/**
|
|
37
|
+
* Flash messages store. They are merged with the session data during
|
|
38
|
+
* commit
|
|
39
|
+
*/
|
|
40
|
+
flashMessages = new Store({});
|
|
41
|
+
constructor(config, driver, cookieClient, values) {
|
|
42
|
+
super(values);
|
|
43
|
+
this.#config = config;
|
|
44
|
+
this.#driver = driver;
|
|
45
|
+
this.#cookieClient = cookieClient;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Find if the sessions are enabled
|
|
49
|
+
*/
|
|
50
|
+
isEnabled() {
|
|
51
|
+
return this.#config.enabled;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Load session from the driver
|
|
55
|
+
*/
|
|
56
|
+
async load(cookies) {
|
|
57
|
+
const sessionIdCookie = cookies[this.#config.cookieName];
|
|
58
|
+
const sessionId = sessionIdCookie ? sessionIdCookie.value : this.sessionId;
|
|
59
|
+
const contents = await this.#driver.read(sessionId);
|
|
60
|
+
const store = new Store(contents);
|
|
61
|
+
const flashMessages = store.pull(this.#flashMessagesKey, null);
|
|
62
|
+
return {
|
|
63
|
+
session: store.all(),
|
|
64
|
+
flashMessages,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Commits the session data to the session store and returns
|
|
69
|
+
* the session id and cookie name for it to be accessible
|
|
70
|
+
* by the server
|
|
71
|
+
*/
|
|
72
|
+
async commit() {
|
|
73
|
+
this.set(this.#flashMessagesKey, this.flashMessages.all());
|
|
74
|
+
await this.#driver.write(this.sessionId, this.toJSON());
|
|
75
|
+
/**
|
|
76
|
+
* Clear from the session client memory
|
|
77
|
+
*/
|
|
78
|
+
this.clear();
|
|
79
|
+
this.flashMessages.clear();
|
|
80
|
+
return {
|
|
81
|
+
sessionId: this.sessionId,
|
|
82
|
+
signedSessionId: this.#cookieClient.sign(this.#config.cookieName, this.sessionId),
|
|
83
|
+
cookieName: this.#config.cookieName,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Clear the session store
|
|
88
|
+
*/
|
|
89
|
+
async forget() {
|
|
90
|
+
/**
|
|
91
|
+
* Clear from the session client memory
|
|
92
|
+
*/
|
|
93
|
+
this.clear();
|
|
94
|
+
this.flashMessages.clear();
|
|
95
|
+
/**
|
|
96
|
+
* Clear with the driver
|
|
97
|
+
*/
|
|
98
|
+
await this.#driver.destroy(this.sessionId);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { InvalidArgumentsException } from '@poppinss/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Helper to define session config
|
|
4
|
+
*/
|
|
5
|
+
export function defineConfig(config) {
|
|
6
|
+
if (!config.cookieName) {
|
|
7
|
+
throw new InvalidArgumentsException('Missing "cookieName" property inside the session config');
|
|
8
|
+
}
|
|
9
|
+
if (!config.driver) {
|
|
10
|
+
throw new InvalidArgumentsException('Missing "driver" property inside the session config');
|
|
11
|
+
}
|
|
12
|
+
return config;
|
|
13
|
+
}
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import { SessionDriverContract, SessionConfig } from '@ioc:Adonis/Addons/Session';
|
|
1
|
+
import type { HttpContext } from '@adonisjs/core/http';
|
|
2
|
+
import type { SessionConfig, SessionDriverContract } from '../types.js';
|
|
4
3
|
/**
|
|
5
4
|
* Cookie driver utilizes the encrypted HTTP cookies to write session value.
|
|
6
5
|
*/
|
|
7
6
|
export declare class CookieDriver implements SessionDriverContract {
|
|
8
|
-
private
|
|
9
|
-
|
|
10
|
-
constructor(config: SessionConfig, ctx: HttpContextContract);
|
|
7
|
+
#private;
|
|
8
|
+
constructor(config: SessionConfig, ctx: HttpContext);
|
|
11
9
|
/**
|
|
12
10
|
* Read session value from the cookie
|
|
13
11
|
*/
|
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/*
|
|
3
2
|
* @adonisjs/session
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.CookieDriver = void 0;
|
|
12
9
|
/**
|
|
13
10
|
* Cookie driver utilizes the encrypted HTTP cookies to write session value.
|
|
14
11
|
*/
|
|
15
|
-
class CookieDriver {
|
|
12
|
+
export class CookieDriver {
|
|
13
|
+
#config;
|
|
14
|
+
#ctx;
|
|
16
15
|
constructor(config, ctx) {
|
|
17
|
-
this
|
|
18
|
-
this
|
|
16
|
+
this.#config = config;
|
|
17
|
+
this.#ctx = ctx;
|
|
19
18
|
}
|
|
20
19
|
/**
|
|
21
20
|
* Read session value from the cookie
|
|
22
21
|
*/
|
|
23
22
|
read(sessionId) {
|
|
24
|
-
const cookieValue = this
|
|
23
|
+
const cookieValue = this.#ctx.request.encryptedCookie(sessionId);
|
|
25
24
|
if (typeof cookieValue !== 'object') {
|
|
26
25
|
return null;
|
|
27
26
|
}
|
|
@@ -34,14 +33,14 @@ class CookieDriver {
|
|
|
34
33
|
if (typeof values !== 'object') {
|
|
35
34
|
throw new Error('Session cookie driver expects an object of values');
|
|
36
35
|
}
|
|
37
|
-
this
|
|
36
|
+
this.#ctx.response.encryptedCookie(sessionId, values, this.#config.cookie);
|
|
38
37
|
}
|
|
39
38
|
/**
|
|
40
39
|
* Removes the session cookie
|
|
41
40
|
*/
|
|
42
41
|
destroy(sessionId) {
|
|
43
|
-
if (this
|
|
44
|
-
this
|
|
42
|
+
if (this.#ctx.request.cookiesList()[sessionId]) {
|
|
43
|
+
this.#ctx.response.clearCookie(sessionId);
|
|
45
44
|
}
|
|
46
45
|
}
|
|
47
46
|
/**
|
|
@@ -55,4 +54,3 @@ class CookieDriver {
|
|
|
55
54
|
this.write(sessionId, value);
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
|
-
exports.CookieDriver = CookieDriver;
|
|
@@ -1,23 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @adonisjs/session
|
|
3
3
|
*
|
|
4
|
-
* (c)
|
|
4
|
+
* (c) AdonisJS
|
|
5
5
|
*
|
|
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
|
-
|
|
10
|
-
import { SessionDriverContract, SessionConfig } from '@ioc:Adonis/Addons/Session';
|
|
9
|
+
import { SessionConfig, SessionDriverContract } from '../types.js';
|
|
11
10
|
/**
|
|
12
11
|
* File driver to read/write session to filesystem
|
|
13
12
|
*/
|
|
14
13
|
export declare class FileDriver implements SessionDriverContract {
|
|
15
|
-
private
|
|
14
|
+
#private;
|
|
16
15
|
constructor(config: SessionConfig);
|
|
17
|
-
/**
|
|
18
|
-
* Returns complete path to the session file
|
|
19
|
-
*/
|
|
20
|
-
private getFilePath;
|
|
21
16
|
/**
|
|
22
17
|
* Returns file contents. A new file will be created if it's
|
|
23
18
|
* missing.
|
|
@@ -1,50 +1,48 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @adonisjs/session
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const helpers_1 = require("@poppinss/utils/build/helpers");
|
|
16
|
-
const fs_extra_1 = require("fs-extra");
|
|
9
|
+
import { join } from 'node:path';
|
|
10
|
+
import { Exception } from '@poppinss/utils';
|
|
11
|
+
import { MessageBuilder } from '@poppinss/utils';
|
|
12
|
+
import { ensureFile, outputFile, remove } from 'fs-extra/esm';
|
|
13
|
+
import { readFile } from 'node:fs/promises';
|
|
17
14
|
/**
|
|
18
15
|
* File driver to read/write session to filesystem
|
|
19
16
|
*/
|
|
20
|
-
class FileDriver {
|
|
17
|
+
export class FileDriver {
|
|
18
|
+
#config;
|
|
21
19
|
constructor(config) {
|
|
22
|
-
this
|
|
23
|
-
if (!this
|
|
24
|
-
throw new
|
|
20
|
+
this.#config = config;
|
|
21
|
+
if (!this.#config.file || !this.#config.file.location) {
|
|
22
|
+
throw new Exception('Missing "file.location" for session file driver inside "config/session" file', { code: 'E_INVALID_SESSION_DRIVER_CONFIG', status: 500 });
|
|
25
23
|
}
|
|
26
24
|
}
|
|
27
25
|
/**
|
|
28
26
|
* Returns complete path to the session file
|
|
29
27
|
*/
|
|
30
|
-
getFilePath(sessionId) {
|
|
31
|
-
return
|
|
28
|
+
#getFilePath(sessionId) {
|
|
29
|
+
return join(this.#config.file.location, `${sessionId}.txt`);
|
|
32
30
|
}
|
|
33
31
|
/**
|
|
34
32
|
* Returns file contents. A new file will be created if it's
|
|
35
33
|
* missing.
|
|
36
34
|
*/
|
|
37
35
|
async read(sessionId) {
|
|
38
|
-
const filePath = this
|
|
39
|
-
await
|
|
40
|
-
const contents = await
|
|
36
|
+
const filePath = this.#getFilePath(sessionId);
|
|
37
|
+
await ensureFile(filePath);
|
|
38
|
+
const contents = await readFile(filePath, 'utf-8');
|
|
41
39
|
if (!contents.trim()) {
|
|
42
40
|
return null;
|
|
43
41
|
}
|
|
44
42
|
/**
|
|
45
43
|
* Verify contents with the session id and return them as an object.
|
|
46
44
|
*/
|
|
47
|
-
const verifiedContents = new
|
|
45
|
+
const verifiedContents = new MessageBuilder().verify(contents.trim(), sessionId);
|
|
48
46
|
if (typeof verifiedContents !== 'object') {
|
|
49
47
|
return null;
|
|
50
48
|
}
|
|
@@ -57,14 +55,14 @@ class FileDriver {
|
|
|
57
55
|
if (typeof values !== 'object') {
|
|
58
56
|
throw new Error('Session file driver expects an object of values');
|
|
59
57
|
}
|
|
60
|
-
const message = new
|
|
61
|
-
await
|
|
58
|
+
const message = new MessageBuilder().build(values, undefined, sessionId);
|
|
59
|
+
await outputFile(this.#getFilePath(sessionId), message);
|
|
62
60
|
}
|
|
63
61
|
/**
|
|
64
62
|
* Cleanup session file by removing it
|
|
65
63
|
*/
|
|
66
64
|
async destroy(sessionId) {
|
|
67
|
-
await
|
|
65
|
+
await remove(this.#getFilePath(sessionId));
|
|
68
66
|
}
|
|
69
67
|
/**
|
|
70
68
|
* Writes the value by reading it from the store
|
|
@@ -77,4 +75,3 @@ class FileDriver {
|
|
|
77
75
|
await this.write(sessionId, value);
|
|
78
76
|
}
|
|
79
77
|
}
|
|
80
|
-
exports.FileDriver = FileDriver;
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @adonisjs/session
|
|
3
3
|
*
|
|
4
|
-
* (c)
|
|
4
|
+
* (c) AdonisJS
|
|
5
5
|
*
|
|
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
|
-
|
|
10
|
-
import { SessionDriverContract } from '@ioc:Adonis/Addons/Session';
|
|
9
|
+
import { SessionDriverContract } from '../types.js';
|
|
11
10
|
/**
|
|
12
11
|
* Memory driver is meant to be used for writing tests.
|
|
13
12
|
*/
|
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @adonisjs/session
|
|
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
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
-
exports.MemoryDriver = void 0;
|
|
12
9
|
/**
|
|
13
10
|
* Memory driver is meant to be used for writing tests.
|
|
14
11
|
*/
|
|
15
|
-
class MemoryDriver {
|
|
12
|
+
export class MemoryDriver {
|
|
13
|
+
static sessions = new Map();
|
|
16
14
|
/**
|
|
17
15
|
* Read session id value from the memory
|
|
18
16
|
*/
|
|
@@ -36,5 +34,3 @@ class MemoryDriver {
|
|
|
36
34
|
}
|
|
37
35
|
touch() { }
|
|
38
36
|
}
|
|
39
|
-
exports.MemoryDriver = MemoryDriver;
|
|
40
|
-
MemoryDriver.sessions = new Map();
|