@adonisjs/session 7.0.0-2 → 7.0.0-3

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 (45) 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 +64 -41
  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 +24 -28
  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 +37 -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/package.json +23 -20
  36. package/build/src/bindings/api_client.d.ts +0 -2
  37. package/build/src/bindings/api_client.js +0 -135
  38. package/build/src/bindings/http_context.d.ts +0 -5
  39. package/build/src/bindings/http_context.js +0 -17
  40. package/build/src/bindings/types.d.ts +0 -77
  41. package/build/src/session_manager.d.ts +0 -38
  42. package/build/src/session_manager.js +0 -149
  43. package/build/src/types.d.ts +0 -61
  44. package/build/src/types.js +0 -1
  45. /package/build/src/{bindings/types.js → types/extended.js} +0 -0
@@ -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 {};