@adonisjs/session 7.0.0-2 → 7.0.0-4

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 (46) hide show
  1. package/README.md +5 -8
  2. package/build/index.d.ts +5 -3
  3. package/build/index.js +5 -3
  4. package/build/providers/session_provider.d.ts +5 -8
  5. package/build/providers/session_provider.js +13 -35
  6. package/build/src/client.d.ts +13 -25
  7. package/build/src/client.js +24 -43
  8. package/build/src/debug.d.ts +3 -0
  9. package/build/src/debug.js +10 -0
  10. package/build/src/define_config.d.ts +6 -3
  11. package/build/src/define_config.js +34 -5
  12. package/build/src/drivers/cookie.d.ts +7 -9
  13. package/build/src/drivers/cookie.js +10 -6
  14. package/build/src/drivers/file.d.ts +11 -14
  15. package/build/src/drivers/file.js +65 -42
  16. package/build/src/drivers/memory.d.ts +4 -8
  17. package/build/src/drivers/memory.js +0 -3
  18. package/build/src/drivers/redis.d.ts +4 -6
  19. package/build/src/drivers/redis.js +25 -29
  20. package/build/src/drivers_collection.d.ts +22 -0
  21. package/build/src/drivers_collection.js +38 -0
  22. package/build/src/errors.d.ts +8 -0
  23. package/build/src/errors.js +17 -0
  24. package/build/src/helpers.d.ts +6 -0
  25. package/build/src/helpers.js +43 -0
  26. package/build/src/session.d.ts +86 -59
  27. package/build/src/session.js +221 -221
  28. package/build/src/session_middleware.d.ts +19 -5
  29. package/build/src/session_middleware.js +42 -6
  30. package/build/src/store.d.ts +17 -14
  31. package/build/src/store.js +33 -17
  32. package/build/src/types/extended.d.ts +19 -0
  33. package/build/src/types/main.d.ts +106 -0
  34. package/build/src/types/main.js +9 -0
  35. package/build/stubs/config.stub +28 -90
  36. package/package.json +23 -20
  37. package/build/src/bindings/api_client.d.ts +0 -2
  38. package/build/src/bindings/api_client.js +0 -135
  39. package/build/src/bindings/http_context.d.ts +0 -5
  40. package/build/src/bindings/http_context.js +0 -17
  41. package/build/src/bindings/types.d.ts +0 -77
  42. package/build/src/session_manager.d.ts +0 -38
  43. package/build/src/session_manager.js +0 -149
  44. package/build/src/types.d.ts +0 -61
  45. package/build/src/types.js +0 -1
  46. /package/build/src/{bindings/types.js → types/extended.js} +0 -0
@@ -1,135 +0,0 @@
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 { ApiRequest, ApiResponse, ApiClient } from '@japa/api-client';
10
- import { inspect } from 'node:util';
11
- export function extendApiClient(sessionManager) {
12
- /**
13
- * Set "sessionClient" on the api request
14
- */
15
- ApiRequest.getter('sessionClient', function () {
16
- return sessionManager.client();
17
- }, true);
18
- /**
19
- * Send session values in the request
20
- */
21
- ApiRequest.macro('session', function (session) {
22
- if (!this.sessionClient.isEnabled()) {
23
- throw new Error('Cannot set session. Make sure to enable it inside "config/session" file');
24
- }
25
- this.sessionClient.merge(session);
26
- return this;
27
- });
28
- /**
29
- * Send flash messages in the request
30
- */
31
- ApiRequest.macro('flashMessages', function (messages) {
32
- if (!this.sessionClient.isEnabled()) {
33
- throw new Error('Cannot set flash messages. Make sure to enable the session inside "config/session" file');
34
- }
35
- this.sessionClient.flashMessages.merge(messages);
36
- return this;
37
- });
38
- /**
39
- * Returns reference to the session data from the session
40
- * jar
41
- */
42
- ApiResponse.macro('session', function () {
43
- return this.sessionJar.session;
44
- });
45
- /**
46
- * Returns reference to the flash messages from the session
47
- * jar
48
- */
49
- ApiResponse.macro('flashMessages', function () {
50
- return this.sessionJar.flashMessages || {};
51
- });
52
- /**
53
- * Assert response to contain a given session and optionally
54
- * has the expected value
55
- */
56
- ApiResponse.macro('assertSession', function (name, value) {
57
- this.ensureHasAssert();
58
- this.assert.property(this.session(), name);
59
- if (value !== undefined) {
60
- this.assert.deepEqual(this.session()[name], value);
61
- }
62
- });
63
- /**
64
- * Assert response to not contain a given session
65
- */
66
- ApiResponse.macro('assertSessionMissing', function (name) {
67
- this.ensureHasAssert();
68
- this.assert.notProperty(this.session(), name);
69
- });
70
- /**
71
- * Assert response to contain a given flash message and optionally
72
- * has the expected value
73
- */
74
- ApiResponse.macro('assertFlashMessage', function (name, value) {
75
- this.ensureHasAssert();
76
- this.assert.property(this.flashMessages(), name);
77
- if (value !== undefined) {
78
- this.assert.deepEqual(this.flashMessages()[name], value);
79
- }
80
- });
81
- /**
82
- * Assert response to not contain a given session
83
- */
84
- ApiResponse.macro('assertFlashMissing', function (name) {
85
- this.ensureHasAssert();
86
- this.assert.notProperty(this.flashMessages(), name);
87
- });
88
- /**
89
- * Dump session to the console
90
- */
91
- ApiResponse.macro('dumpSession', function (options) {
92
- const inspectOptions = { depth: 2, showHidden: false, colors: true, ...options };
93
- console.log(`"session" => ${inspect(this.session(), inspectOptions)}`);
94
- console.log(`"flashMessages" => ${inspect(this.flashMessages(), inspectOptions)}`);
95
- return this;
96
- });
97
- /**
98
- * Adding hooks directly on the request object moves the hooks to
99
- * the end of the queue (basically after the globally hooks)
100
- */
101
- ApiClient.onRequest((req) => {
102
- /**
103
- * Hook into request and persist session data to be available
104
- * on the server during the request.
105
- */
106
- req.setup(async (request) => {
107
- /**
108
- * Persist session data and set the session id within the
109
- * cookie
110
- */
111
- const { cookieName, sessionId } = await request.sessionClient.commit();
112
- request.withCookie(cookieName, sessionId);
113
- /**
114
- * Cleanup if request has error. Otherwise the teardown
115
- * hook will clear
116
- */
117
- return async (error) => {
118
- if (error) {
119
- await request.sessionClient.forget();
120
- }
121
- };
122
- });
123
- /**
124
- * Load messages from the session store and keep a reference to it
125
- * inside the response object.
126
- *
127
- * We also destroy the session after getting a copy of the session
128
- * data
129
- */
130
- req.teardown(async (response) => {
131
- response.sessionJar = await response.request.sessionClient.load(response.cookies());
132
- await response.request.sessionClient.forget();
133
- });
134
- });
135
- }
@@ -1,5 +0,0 @@
1
- import { SessionManager } from '../session_manager.js';
2
- /**
3
- * Extends HttpContext class with the ally getter
4
- */
5
- export declare function extendHttpContext(session: SessionManager): void;
@@ -1,17 +0,0 @@
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,77 +0,0 @@
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
- */
26
- declare module '@japa/api-client' {
27
- interface ApiRequest {
28
- sessionClient: SessionClient;
29
- /**
30
- * Send session values in the request
31
- */
32
- session(session: Record<string, AllowedSessionValues>): this;
33
- /**
34
- * Send flash messages in the request
35
- */
36
- flashMessages(messages: Record<string, AllowedSessionValues>): this;
37
- }
38
- interface ApiResponse {
39
- /**
40
- * A copy of session data loaded from the driver
41
- */
42
- sessionJar: {
43
- session: Record<string, any>;
44
- flashMessages: Record<string, any> | null;
45
- };
46
- /**
47
- * Get session data
48
- */
49
- session(): Record<string, any>;
50
- /**
51
- * Dump session
52
- */
53
- dumpSession(options?: InspectOptions): this;
54
- /**
55
- * Get flash messages set by the server
56
- */
57
- flashMessages(): Record<string, any>;
58
- /**
59
- * Assert response to contain a given session and optionally
60
- * has the expected value
61
- */
62
- assertSession(key: string, value?: any): void;
63
- /**
64
- * Assert response to not contain a given session
65
- */
66
- assertSessionMissing(key: string): void;
67
- /**
68
- * Assert response to contain a given flash message and optionally
69
- * has the expected value
70
- */
71
- assertFlashMessage(key: string, value?: any): void;
72
- /**
73
- * Assert response to not contain a given session
74
- */
75
- assertFlashMissing(key: string): void;
76
- }
77
- }
@@ -1,38 +0,0 @@
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 { Session } from './session.js';
10
- import { HttpContext } from '@adonisjs/core/http';
11
- import { ExtendCallback, SessionConfig } from './types.js';
12
- import { RedisManagerContract } from '@adonisjs/redis/types';
13
- import { Encryption } from '@adonisjs/core/encryption';
14
- import { SessionClient } from './client.js';
15
- /**
16
- * Session manager exposes the API to create session instance for a given
17
- * request and also add new drivers.
18
- */
19
- export declare class SessionManager {
20
- #private;
21
- constructor(config: SessionConfig, encryption: Encryption, redis?: RedisManagerContract<any>);
22
- /**
23
- * Find if the sessions are enabled
24
- */
25
- isEnabled(): boolean;
26
- /**
27
- * Creates an instance of the session client
28
- */
29
- client(): SessionClient;
30
- /**
31
- * Creates a new session instance for a given HTTP request
32
- */
33
- create(ctx: HttpContext): Session;
34
- /**
35
- * Extend the drivers list by adding a new one.
36
- */
37
- extend(driver: string, callback: ExtendCallback): void;
38
- }
@@ -1,149 +0,0 @@
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
- }
@@ -1,61 +0,0 @@
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>;
@@ -1 +0,0 @@
1
- export {};