@adonisjs/session 6.4.0 → 7.0.0-1

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 (59) hide show
  1. package/README.md +25 -42
  2. package/build/configure.d.ts +5 -0
  3. package/build/configure.js +18 -0
  4. package/build/index.d.ts +12 -0
  5. package/build/index.js +12 -0
  6. package/build/providers/session_provider.d.ts +13 -0
  7. package/build/providers/session_provider.js +43 -0
  8. package/build/src/bindings/api_client.d.ts +2 -0
  9. package/build/src/{Bindings/Tests.js → bindings/api_client.js} +8 -13
  10. package/build/src/bindings/http_context.d.ts +5 -0
  11. package/build/src/bindings/http_context.js +17 -0
  12. package/build/{adonis-typings/tests.d.ts → src/bindings/types.d.ts} +26 -5
  13. package/build/{adonis-typings/session.js → src/bindings/types.js} +2 -1
  14. package/build/src/{Client/index.d.ts → client.d.ts} +7 -15
  15. package/build/src/client.js +100 -0
  16. package/build/src/define_config.d.ts +5 -0
  17. package/build/src/define_config.js +13 -0
  18. package/build/src/{Drivers/Cookie.d.ts → drivers/cookie.d.ts} +4 -6
  19. package/build/src/{Drivers/Cookie.js → drivers/cookie.js} +10 -12
  20. package/build/src/{Drivers/File.d.ts → drivers/file.d.ts} +3 -8
  21. package/build/src/{Drivers/File.js → drivers/file.js} +20 -23
  22. package/build/src/{Drivers/Memory.d.ts → drivers/memory.d.ts} +2 -3
  23. package/build/src/{Drivers/Memory.js → drivers/memory.js} +3 -7
  24. package/build/src/{Drivers/Redis.d.ts → drivers/redis.d.ts} +5 -15
  25. package/build/src/drivers/redis.js +74 -0
  26. package/build/src/{Session/index.d.ts → session.d.ts} +6 -67
  27. package/build/src/session.js +371 -0
  28. package/build/src/session_manager.d.ts +38 -0
  29. package/build/src/session_manager.js +149 -0
  30. package/build/src/session_middleware.d.ts +5 -0
  31. package/build/src/session_middleware.js +20 -0
  32. package/build/src/{Store/index.d.ts → store.d.ts} +3 -7
  33. package/build/src/{Store/index.js → store.js} +18 -18
  34. package/build/src/types.d.ts +61 -0
  35. package/build/src/types.js +1 -0
  36. package/build/{templates/session.txt → stubs/config.stub} +12 -15
  37. package/build/stubs/main.d.ts +1 -0
  38. package/build/{adonis-typings/tests.js → stubs/main.js} +2 -3
  39. package/package.json +96 -134
  40. package/build/adonis-typings/container.d.ts +0 -14
  41. package/build/adonis-typings/container.js +0 -8
  42. package/build/adonis-typings/context.d.ts +0 -14
  43. package/build/adonis-typings/context.js +0 -8
  44. package/build/adonis-typings/index.d.ts +0 -4
  45. package/build/adonis-typings/index.js +0 -12
  46. package/build/adonis-typings/session.d.ts +0 -265
  47. package/build/config.d.ts +0 -13
  48. package/build/config.js +0 -18
  49. package/build/instructions.md +0 -10
  50. package/build/providers/SessionProvider.d.ts +0 -31
  51. package/build/providers/SessionProvider.js +0 -56
  52. package/build/src/Bindings/Server.d.ts +0 -10
  53. package/build/src/Bindings/Server.js +0 -42
  54. package/build/src/Bindings/Tests.d.ts +0 -7
  55. package/build/src/Client/index.js +0 -93
  56. package/build/src/Drivers/Redis.js +0 -73
  57. package/build/src/Session/index.js +0 -352
  58. package/build/src/SessionManager/index.d.ts +0 -78
  59. package/build/src/SessionManager/index.js +0 -148
@@ -1,265 +0,0 @@
1
- /// <reference types="@adonisjs/http-server/build/adonis-typings" />
2
- /// <reference types="@adonisjs/application/build/adonis-typings" />
3
- declare module '@ioc:Adonis/Addons/Session' {
4
- import { CookieOptions } from '@ioc:Adonis/Core/Response';
5
- import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
6
- import { ApplicationContract } from '@ioc:Adonis/Core/Application';
7
- /**
8
- * Shape of session config.
9
- */
10
- export interface SessionConfig {
11
- /**
12
- * Enable/disable session for the entire application lifecycle
13
- */
14
- enabled: boolean;
15
- /**
16
- * The driver in play
17
- */
18
- driver: string;
19
- /**
20
- * Cookie name.
21
- */
22
- cookieName: string;
23
- /**
24
- * Clear session when browser closes
25
- */
26
- clearWithBrowser: boolean;
27
- /**
28
- * Age of session cookie
29
- */
30
- age: string | number;
31
- /**
32
- * Config for the cookie driver and also the session id
33
- * cookie
34
- */
35
- cookie: Omit<Partial<CookieOptions>, 'maxAge' | 'expires'>;
36
- /**
37
- * Config for the file driver
38
- */
39
- file?: {
40
- location: string;
41
- };
42
- /**
43
- * The redis connection to use from the `config/redis` file
44
- */
45
- redisConnection?: string;
46
- }
47
- /**
48
- * Shape of a driver that every session driver must have
49
- */
50
- export interface SessionDriverContract {
51
- read(sessionId: string): Promise<Record<string, any> | null> | Record<string, any> | null;
52
- write(sessionId: string, values: Record<string, any>): Promise<void> | void;
53
- destroy(sessionId: string): Promise<void> | void;
54
- touch(sessionId: string): Promise<void> | void;
55
- }
56
- /**
57
- * The callback to be passed to the `extend` method. It is invoked
58
- * for each request (if extended driver is in use).
59
- */
60
- export type ExtendCallback = (manager: SessionManagerContract, config: SessionConfig, ctx: HttpContextContract) => SessionDriverContract;
61
- /**
62
- * The values allowed by the `session.put` method
63
- */
64
- export type AllowedSessionValues = string | boolean | number | object | Date | Array<any>;
65
- /**
66
- * Store used for storing session values + flash messages
67
- */
68
- export interface StoreContract {
69
- /**
70
- * A boolean to know if store is empty
71
- */
72
- isEmpty: boolean;
73
- /**
74
- * Set value for a key
75
- */
76
- set(key: string, value: AllowedSessionValues): void;
77
- /**
78
- * Increment value for a key. An exception is raised when existing
79
- * value is not a number
80
- */
81
- increment(key: string, steps?: number): void;
82
- /**
83
- * Decrement value for a key. An exception is raised when existing
84
- * value is not a number
85
- */
86
- decrement(key: string, steps?: number): void;
87
- /**
88
- * Replace existing values with new ones
89
- */
90
- update(values: Record<string, any>): void;
91
- /**
92
- * Merge values with existing ones
93
- */
94
- merge(values: Record<string, any>): any;
95
- /**
96
- * Get all values
97
- */
98
- all(): any;
99
- /**
100
- * Get value for a given key or use the default value
101
- */
102
- get(key: string, defaultValue?: any): any;
103
- /**
104
- * Find if a value exists. Optionally you can also check arrays
105
- * to have length too
106
- */
107
- has(key: string, checkForArraysLength?: boolean): boolean;
108
- /**
109
- * Unset value
110
- */
111
- unset(key: string): void;
112
- /**
113
- * Clear all values
114
- */
115
- clear(): void;
116
- /**
117
- * Read value and then unset it at the same time
118
- */
119
- pull(key: string, defaultValue?: any): any;
120
- /**
121
- * Convert store values toObject
122
- */
123
- toObject(): any;
124
- /**
125
- * Convert store values to toJSON
126
- */
127
- toJSON(): any;
128
- }
129
- /**
130
- * Shape of the actual session store
131
- */
132
- export interface SessionContract {
133
- /**
134
- * Has the store being initiated
135
- */
136
- initiated: boolean;
137
- /**
138
- * Is session store readonly. Will be during Websockets
139
- * request
140
- */
141
- readonly: boolean;
142
- /**
143
- * Is session just created or we read received the
144
- * session id from the request
145
- */
146
- fresh: boolean;
147
- /**
148
- * Session id
149
- */
150
- sessionId: string;
151
- /**
152
- * Previous request flash messages
153
- */
154
- flashMessages: StoreContract;
155
- /**
156
- * Flash messages that will be sent in the current
157
- * request response
158
- */
159
- responseFlashMessages: StoreContract;
160
- /**
161
- * Initiate session store
162
- */
163
- initiate(readonly: boolean): Promise<void>;
164
- /**
165
- * Commit session mutations
166
- */
167
- commit(): Promise<void>;
168
- /**
169
- * Re-generate session id. This help avoid session
170
- * replay attacks.
171
- */
172
- regenerate(): void;
173
- /**
174
- * Store API
175
- */
176
- has(key: string): boolean;
177
- put(key: string, value: AllowedSessionValues): void;
178
- get(key: string, defaultValue?: any): any;
179
- all(): any;
180
- forget(key: string): void;
181
- pull(key: string, defaultValue?: any): any;
182
- increment(key: string, steps?: number): any;
183
- decrement(key: string, steps?: number): any;
184
- clear(): void;
185
- /**
186
- * Flash a key-value pair
187
- */
188
- flash(values: {
189
- [key: string]: AllowedSessionValues;
190
- }): void;
191
- flash(key: string, value: AllowedSessionValues): void;
192
- /**
193
- * Flash request body
194
- */
195
- flashAll(): void;
196
- /**
197
- * Flash selected keys from the request body
198
- */
199
- flashOnly(keys: string[]): void;
200
- /**
201
- * Omit selected keys from the request data and flash
202
- * the rest of values
203
- */
204
- flashExcept(keys: string[]): void;
205
- /**
206
- * Reflash existing flash messages
207
- */
208
- reflash(): void;
209
- /**
210
- * Reflash selected keys from the existing flash messages
211
- */
212
- reflashOnly(keys: string[]): void;
213
- /**
214
- * Omit selected keys from the existing flash messages
215
- * and flash the rest of values
216
- */
217
- reflashExcept(keys: string[]): void;
218
- }
219
- /**
220
- * SessionClient exposes the API to set session data as a client
221
- */
222
- export interface SessionClientContract extends StoreContract {
223
- /**
224
- * Find if the sessions are enabled
225
- */
226
- isEnabled(): boolean;
227
- /**
228
- * Flash messages store to set flash messages
229
- */
230
- flashMessages: StoreContract;
231
- /**
232
- * Load session data from the driver
233
- */
234
- load(cookies: Record<string, any>): Promise<{
235
- session: Record<string, any>;
236
- flashMessages: Record<string, any> | null;
237
- }>;
238
- /**
239
- * Commits the session data to the session store and returns
240
- * the session id and cookie name for it to be accessible
241
- * by the server
242
- */
243
- commit(): Promise<{
244
- cookieName: string;
245
- sessionId: string;
246
- signedSessionId: string;
247
- }>;
248
- /**
249
- * Forget the session data.
250
- */
251
- forget(): Promise<void>;
252
- }
253
- /**
254
- * Session manager shape
255
- */
256
- export interface SessionManagerContract {
257
- isEnabled(): boolean;
258
- application: ApplicationContract;
259
- client(): SessionClientContract;
260
- create(ctx: HttpContextContract): SessionContract;
261
- extend(driver: string, callback: ExtendCallback): void;
262
- }
263
- const Session: SessionManagerContract;
264
- export default Session;
265
- }
package/build/config.d.ts DELETED
@@ -1,13 +0,0 @@
1
- /**
2
- * @adonisjs/session
3
- *
4
- * (c) Harminder Virk <virk@adonisjs.com>
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 { SessionConfig } from '@ioc:Adonis/Addons/Session';
10
- /**
11
- * Helper to define session config
12
- */
13
- export declare function sessionConfig<Config extends SessionConfig>(config: Config): Config;
package/build/config.js DELETED
@@ -1,18 +0,0 @@
1
- "use strict";
2
- /**
3
- * @adonisjs/session
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.sessionConfig = void 0;
12
- /**
13
- * Helper to define session config
14
- */
15
- function sessionConfig(config) {
16
- return config;
17
- }
18
- exports.sessionConfig = sessionConfig;
@@ -1,10 +0,0 @@
1
- The package has been configured successfully. The session configuration stored inside `config/session.ts` file relies on the following environment variables and hence we recommend validating them.
2
-
3
- Open the `env.ts` file and paste the following code inside the `Env.rules` object.
4
-
5
- ```ts
6
- SESSION_DRIVER: Env.schema.string()
7
- ```
8
-
9
- - Here we expect the `SESSION_DRIVER` environment variable to be always present
10
- - And should be a valid string
@@ -1,31 +0,0 @@
1
- /**
2
- * @adonisjs/session
3
- *
4
- * (c) Harminder Virk <virk@adonisjs.com>
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- /// <reference types="@adonisjs/application/build/adonis-typings" />
10
- import { ApplicationContract } from '@ioc:Adonis/Core/Application';
11
- /**
12
- * Session provider for AdonisJS
13
- */
14
- export default class SessionProvider {
15
- protected app: ApplicationContract;
16
- constructor(app: ApplicationContract);
17
- static needsApplication: boolean;
18
- /**
19
- * Register Session Manager
20
- */
21
- register(): void;
22
- /**
23
- * Register bindings for tests
24
- */
25
- protected registerTestsBindings(): void;
26
- /**
27
- * Register server bindings
28
- */
29
- protected registerServerBindings(): void;
30
- boot(): void;
31
- }
@@ -1,56 +0,0 @@
1
- "use strict";
2
- /**
3
- * @adonisjs/session
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- /**
12
- * Session provider for AdonisJS
13
- */
14
- class SessionProvider {
15
- constructor(app) {
16
- this.app = app;
17
- }
18
- /**
19
- * Register Session Manager
20
- */
21
- register() {
22
- this.app.container.singleton('Adonis/Addons/Session', () => {
23
- const { SessionManager } = require('../src/SessionManager');
24
- return new SessionManager(this.app, this.app.config.get('session', {}));
25
- });
26
- }
27
- /**
28
- * Register bindings for tests
29
- */
30
- registerTestsBindings() {
31
- this.app.container.withBindings([
32
- 'Japa/Preset/ApiRequest',
33
- 'Japa/Preset/ApiResponse',
34
- 'Japa/Preset/ApiClient',
35
- 'Adonis/Addons/Session',
36
- ], (ApiRequest, ApiResponse, ApiClient, Session) => {
37
- const { defineTestsBindings } = require('../src/Bindings/Tests');
38
- defineTestsBindings(ApiRequest, ApiResponse, ApiClient, Session);
39
- });
40
- }
41
- /**
42
- * Register server bindings
43
- */
44
- registerServerBindings() {
45
- this.app.container.withBindings(['Adonis/Core/Server', 'Adonis/Core/HttpContext', 'Adonis/Addons/Session'], (Server, HttpContext, Session) => {
46
- const { defineServerBindings } = require('../src/Bindings/Server');
47
- defineServerBindings(HttpContext, Server, Session);
48
- });
49
- }
50
- boot() {
51
- this.registerServerBindings();
52
- this.registerTestsBindings();
53
- }
54
- }
55
- exports.default = SessionProvider;
56
- SessionProvider.needsApplication = true;
@@ -1,10 +0,0 @@
1
- /// <reference path="../../adonis-typings/index.d.ts" />
2
- /// <reference types="@adonisjs/http-server/build/adonis-typings" />
3
- import { ServerContract } from '@ioc:Adonis/Core/Server';
4
- import { SessionManagerContract } from '@ioc:Adonis/Addons/Session';
5
- import { HttpContextConstructorContract } from '@ioc:Adonis/Core/HttpContext';
6
- /**
7
- * Share "session" with the HTTP context. Define hooks to initiate and
8
- * commit session when sessions are enabled.
9
- */
10
- export declare function defineServerBindings(HttpContext: HttpContextConstructorContract, Server: ServerContract, Session: SessionManagerContract): void;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/session
4
- *
5
- * (c) AdonisJS
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.defineServerBindings = void 0;
12
- /**
13
- * Share "session" with the HTTP context. Define hooks to initiate and
14
- * commit session when sessions are enabled.
15
- */
16
- function defineServerBindings(HttpContext, Server, Session) {
17
- /**
18
- * Sharing session with the context
19
- */
20
- HttpContext.getter('session', function session() {
21
- return Session.create(this);
22
- }, true);
23
- /**
24
- * Do not register hooks when sessions are disabled
25
- */
26
- if (!Session.isEnabled()) {
27
- return;
28
- }
29
- /**
30
- * Initiate session store
31
- */
32
- Server.hooks.before(async (ctx) => {
33
- await ctx.session.initiate(false);
34
- });
35
- /**
36
- * Commit store mutations
37
- */
38
- Server.hooks.after(async (ctx) => {
39
- await ctx.session.commit();
40
- });
41
- }
42
- exports.defineServerBindings = defineServerBindings;
@@ -1,7 +0,0 @@
1
- /// <reference path="../../adonis-typings/index.d.ts" />
2
- import { ContainerBindings } from '@ioc:Adonis/Core/Application';
3
- import { SessionManagerContract } from '@ioc:Adonis/Addons/Session';
4
- /**
5
- * Define test bindings
6
- */
7
- export declare function defineTestsBindings(ApiRequest: ContainerBindings['Japa/Preset/ApiRequest'], ApiResponse: ContainerBindings['Japa/Preset/ApiResponse'], ApiClient: ContainerBindings['Japa/Preset/ApiClient'], SessionManager: SessionManagerContract): void;
@@ -1,93 +0,0 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/session
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.SessionClient = void 0;
12
- /// <reference path="../../adonis-typings/index.ts" />
13
- const helpers_1 = require("@poppinss/utils/build/helpers");
14
- const Store_1 = require("../Store");
15
- /**
16
- * SessionClient exposes the API to set session data as a client
17
- */
18
- class SessionClient extends Store_1.Store {
19
- constructor(config, driver, cookieClient, values) {
20
- super(values);
21
- this.config = config;
22
- this.driver = driver;
23
- this.cookieClient = cookieClient;
24
- /**
25
- * Each instance of client works on a single session id. Generate
26
- * multiple client instances for a different session id
27
- */
28
- this.sessionId = (0, helpers_1.cuid)();
29
- /**
30
- * Session key for setting flash messages
31
- */
32
- this.flashMessagesKey = '__flash__';
33
- /**
34
- * Flash messages store. They are merged with the session data during
35
- * commit
36
- */
37
- this.flashMessages = new Store_1.Store({});
38
- }
39
- /**
40
- * Find if the sessions are enabled
41
- */
42
- isEnabled() {
43
- return this.config.enabled;
44
- }
45
- /**
46
- * Load session from the driver
47
- */
48
- async load(cookies) {
49
- const sessionIdCookie = cookies[this.config.cookieName];
50
- const sessionId = sessionIdCookie ? sessionIdCookie.value : this.sessionId;
51
- const contents = await this.driver.read(sessionId);
52
- const store = new Store_1.Store(contents);
53
- const flashMessages = store.pull(this.flashMessagesKey, null);
54
- return {
55
- session: store.all(),
56
- flashMessages,
57
- };
58
- }
59
- /**
60
- * Commits the session data to the session store and returns
61
- * the session id and cookie name for it to be accessible
62
- * by the server
63
- */
64
- async commit() {
65
- this.set(this.flashMessagesKey, this.flashMessages.all());
66
- await this.driver.write(this.sessionId, this.toJSON());
67
- /**
68
- * Clear from the session client memory
69
- */
70
- this.clear();
71
- this.flashMessages.clear();
72
- return {
73
- sessionId: this.sessionId,
74
- signedSessionId: this.cookieClient.sign(this.config.cookieName, this.sessionId),
75
- cookieName: this.config.cookieName,
76
- };
77
- }
78
- /**
79
- * Clear the session store
80
- */
81
- async forget() {
82
- /**
83
- * Clear from the session client memory
84
- */
85
- this.clear();
86
- this.flashMessages.clear();
87
- /**
88
- * Clear with the driver
89
- */
90
- await this.driver.destroy(this.sessionId);
91
- }
92
- }
93
- exports.SessionClient = SessionClient;
@@ -1,73 +0,0 @@
1
- "use strict";
2
- /**
3
- * @adonisjs/session
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.RedisDriver = void 0;
12
- /// <reference path="../../adonis-typings/index.ts" />
13
- const utils_1 = require("@poppinss/utils");
14
- const helpers_1 = require("@poppinss/utils/build/helpers");
15
- /**
16
- * File driver to read/write session to filesystem
17
- */
18
- class RedisDriver {
19
- constructor(config, redis) {
20
- this.config = config;
21
- this.redis = redis;
22
- /**
23
- * Convert milliseconds to seconds
24
- */
25
- this.ttl = Math.round((typeof this.config.age === 'string' ? helpers_1.string.toMs(this.config.age) : this.config.age) / 1000);
26
- if (!this.config.redisConnection) {
27
- throw new utils_1.Exception('Missing redisConnection for session redis driver inside "config/session" file', 500, 'E_INVALID_SESSION_DRIVER_CONFIG');
28
- }
29
- }
30
- /**
31
- * Returns instance of the redis connection
32
- */
33
- getRedisConnection() {
34
- return this.redis.connection(this.config.redisConnection);
35
- }
36
- /**
37
- * Returns file contents. A new file will be created if it's
38
- * missing.
39
- */
40
- async read(sessionId) {
41
- const contents = await this.getRedisConnection().get(sessionId);
42
- if (!contents) {
43
- return null;
44
- }
45
- const verifiedContents = new helpers_1.MessageBuilder().verify(contents, sessionId);
46
- if (typeof verifiedContents !== 'object') {
47
- return null;
48
- }
49
- return verifiedContents;
50
- }
51
- /**
52
- * Write session values to a file
53
- */
54
- async write(sessionId, values) {
55
- if (typeof values !== 'object') {
56
- throw new Error('Session file driver expects an object of values');
57
- }
58
- await this.getRedisConnection().setex(sessionId, this.ttl, new helpers_1.MessageBuilder().build(values, undefined, sessionId));
59
- }
60
- /**
61
- * Cleanup session file by removing it
62
- */
63
- async destroy(sessionId) {
64
- await this.getRedisConnection().del(sessionId);
65
- }
66
- /**
67
- * Updates the value expiry
68
- */
69
- async touch(sessionId) {
70
- await this.getRedisConnection().expire(sessionId, this.ttl);
71
- }
72
- }
73
- exports.RedisDriver = RedisDriver;