@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,12 +1,44 @@
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 { ExceptionHandler } from '@adonisjs/core/http';
10
+ import { Session } from './session.js';
11
+ import sessionDriversList from './drivers_collection.js';
12
+ /**
13
+ * Overwriting validation exception renderer
14
+ */
15
+ const originalErrorHandler = ExceptionHandler.prototype.renderValidationErrorAsHTML;
16
+ ExceptionHandler.macro('renderValidationErrorAsHTML', async function (error, ctx) {
17
+ if (ctx.session) {
18
+ ctx.session.flashValidationErrors(error);
19
+ ctx.response.redirect('back', true);
20
+ }
21
+ else {
22
+ return originalErrorHandler(error, ctx);
23
+ }
24
+ });
25
+ /**
26
+ * Session middleware is used to initiate the session store
27
+ * and commit its values during an HTTP request
28
+ */
1
29
  export default class SessionMiddleware {
2
- session;
3
- constructor(session) {
4
- this.session = session;
30
+ #config;
31
+ #emitter;
32
+ constructor(config, emitter) {
33
+ this.#config = config;
34
+ this.#emitter = emitter;
5
35
  }
6
36
  async handle(ctx, next) {
7
- if (!this.session.isEnabled()) {
8
- return;
37
+ if (!this.#config.enabled) {
38
+ return next();
9
39
  }
40
+ const driver = sessionDriversList.create(this.#config.driver, this.#config, ctx);
41
+ ctx.session = new Session(this.#config, driver, this.#emitter, ctx);
10
42
  /**
11
43
  * Initiate session store
12
44
  */
@@ -14,10 +46,14 @@ export default class SessionMiddleware {
14
46
  /**
15
47
  * Call next middlewares or route handler
16
48
  */
17
- await next();
49
+ const response = await next();
18
50
  /**
19
51
  * Commit store mutations
20
52
  */
21
53
  await ctx.session.commit();
54
+ /**
55
+ * Return response
56
+ */
57
+ return response;
22
58
  }
23
59
  }
@@ -1,32 +1,27 @@
1
- import type { AllowedSessionValues } from './types.js';
1
+ import type { AllowedSessionValues, SessionData } from './types/main.js';
2
2
  /**
3
- * Session store to mutate and access values from the session object
3
+ * Session store encapsulates the session data and offers a
4
+ * declarative API to mutate it.
4
5
  */
5
6
  export declare class Store {
6
7
  #private;
7
- constructor(values: {
8
- [key: string]: any;
9
- } | null);
8
+ constructor(values: SessionData | null);
10
9
  /**
11
10
  * Find if store is empty or not
12
11
  */
13
12
  get isEmpty(): boolean;
14
13
  /**
15
- * Set key/value pair
14
+ * Find if the store has been modified.
16
15
  */
17
- set(key: string, value: AllowedSessionValues): void;
16
+ get hasBeenModified(): boolean;
18
17
  /**
19
- * Get value for a given key
18
+ * Set key/value pair
20
19
  */
21
- get(key: string, defaultValue?: any): any;
20
+ set(key: string, value: AllowedSessionValues): void;
22
21
  /**
23
22
  * Remove key
24
23
  */
25
24
  unset(key: string): void;
26
- /**
27
- * Reset store by clearing it's values.
28
- */
29
- clear(): void;
30
25
  /**
31
26
  * Pull value from the store. It is same as calling
32
27
  * store.get and then store.unset
@@ -43,7 +38,7 @@ export declare class Store {
43
38
  */
44
39
  decrement(key: string, steps?: number): void;
45
40
  /**
46
- * Overwrite the underlying values object
41
+ * Overwrite existing store data with new values.
47
42
  */
48
43
  update(values: {
49
44
  [key: string]: any;
@@ -54,6 +49,14 @@ export declare class Store {
54
49
  merge(values: {
55
50
  [key: string]: any;
56
51
  }): any;
52
+ /**
53
+ * Reset store by clearing it's values.
54
+ */
55
+ clear(): void;
56
+ /**
57
+ * Get value for a given key
58
+ */
59
+ get(key: string, defaultValue?: any): any;
57
60
  /**
58
61
  * A boolean to know if value exists. Extra guards to check
59
62
  * arrays for it's length as well.
@@ -6,16 +6,22 @@
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 { Exception } from '@poppinss/utils';
10
9
  import lodash from '@poppinss/utils/lodash';
10
+ import { RuntimeException } from '@poppinss/utils';
11
11
  /**
12
- * Session store to mutate and access values from the session object
12
+ * Session store encapsulates the session data and offers a
13
+ * declarative API to mutate it.
13
14
  */
14
15
  export class Store {
15
16
  /**
16
17
  * Underlying store values
17
18
  */
18
19
  #values;
20
+ /**
21
+ * A boolean to know if store has been
22
+ * modified
23
+ */
24
+ #modified = false;
19
25
  constructor(values) {
20
26
  this.#values = values || {};
21
27
  }
@@ -26,29 +32,25 @@ export class Store {
26
32
  return !this.#values || Object.keys(this.#values).length === 0;
27
33
  }
28
34
  /**
29
- * Set key/value pair
35
+ * Find if the store has been modified.
30
36
  */
31
- set(key, value) {
32
- lodash.set(this.#values, key, value);
37
+ get hasBeenModified() {
38
+ return this.#modified;
33
39
  }
34
40
  /**
35
- * Get value for a given key
41
+ * Set key/value pair
36
42
  */
37
- get(key, defaultValue) {
38
- return lodash.get(this.#values, key, defaultValue);
43
+ set(key, value) {
44
+ this.#modified = true;
45
+ lodash.set(this.#values, key, value);
39
46
  }
40
47
  /**
41
48
  * Remove key
42
49
  */
43
50
  unset(key) {
51
+ this.#modified = true;
44
52
  lodash.unset(this.#values, key);
45
53
  }
46
- /**
47
- * Reset store by clearing it's values.
48
- */
49
- clear() {
50
- this.update({});
51
- }
52
54
  /**
53
55
  * Pull value from the store. It is same as calling
54
56
  * store.get and then store.unset
@@ -66,7 +68,7 @@ export class Store {
66
68
  increment(key, steps = 1) {
67
69
  const value = this.get(key, 0);
68
70
  if (typeof value !== 'number') {
69
- throw new Exception(`Cannot increment "${key}", since original value is not a number`);
71
+ throw new RuntimeException(`Cannot increment "${key}". Existing value is not a number`);
70
72
  }
71
73
  this.set(key, value + steps);
72
74
  }
@@ -77,22 +79,36 @@ export class Store {
77
79
  decrement(key, steps = 1) {
78
80
  const value = this.get(key, 0);
79
81
  if (typeof value !== 'number') {
80
- throw new Exception(`Cannot increment "${key}", since original value is not a number`);
82
+ throw new RuntimeException(`Cannot decrement "${key}". Existing value is not a number`);
81
83
  }
82
84
  this.set(key, value - steps);
83
85
  }
84
86
  /**
85
- * Overwrite the underlying values object
87
+ * Overwrite existing store data with new values.
86
88
  */
87
89
  update(values) {
90
+ this.#modified = true;
88
91
  this.#values = values;
89
92
  }
90
93
  /**
91
94
  * Update to merge values
92
95
  */
93
96
  merge(values) {
97
+ this.#modified = true;
94
98
  lodash.merge(this.#values, values);
95
99
  }
100
+ /**
101
+ * Reset store by clearing it's values.
102
+ */
103
+ clear() {
104
+ this.update({});
105
+ }
106
+ /**
107
+ * Get value for a given key
108
+ */
109
+ get(key, defaultValue) {
110
+ return lodash.get(this.#values, key, defaultValue);
111
+ }
96
112
  /**
97
113
  * A boolean to know if value exists. Extra guards to check
98
114
  * arrays for it's length as well.
@@ -0,0 +1,19 @@
1
+ import type { Session } from '../session.js';
2
+ /**
3
+ * Events emitted by the session class
4
+ */
5
+ declare module '@adonisjs/core/types' {
6
+ interface EventsList {
7
+ 'session:initiated': {
8
+ session: Session;
9
+ };
10
+ 'session:committed': {
11
+ session: Session;
12
+ };
13
+ 'session:migrated': {
14
+ fromSessionId: string;
15
+ toSessionId: string;
16
+ session: Session;
17
+ };
18
+ }
19
+ }
@@ -0,0 +1,106 @@
1
+ import type { HttpContext } from '@adonisjs/core/http';
2
+ import { RedisConnections } from '@adonisjs/redis/types';
3
+ import type { CookieOptions } from '@adonisjs/core/types/http';
4
+ import type { FileDriver } from '../drivers/file.js';
5
+ import type { RedisDriver } from '../drivers/redis.js';
6
+ import type { MemoryDriver } from '../drivers/memory.js';
7
+ import type { CookieDriver } from '../drivers/cookie.js';
8
+ /**
9
+ * The values allowed by the `session.put` method
10
+ */
11
+ export type AllowedSessionValues = string | boolean | number | object | Date | Array<any>;
12
+ export type SessionData = Record<string, AllowedSessionValues>;
13
+ /**
14
+ * Session drivers must implement the session driver contract.
15
+ */
16
+ export interface SessionDriverContract {
17
+ /**
18
+ * The read method is used to read the data from the persistence
19
+ * store and return it back as an object
20
+ */
21
+ read(sessionId: string): Promise<SessionData | null> | SessionData | null;
22
+ /**
23
+ * The write method is used to write the session data into the
24
+ * persistence store.
25
+ */
26
+ write(sessionId: string, data: SessionData): Promise<void> | void;
27
+ /**
28
+ * The destroy method is used to destroy the session by removing
29
+ * its data from the persistence store
30
+ */
31
+ destroy(sessionId: string): Promise<void> | void;
32
+ /**
33
+ * The touch method should update the lifetime of session id without
34
+ * making changes to the session data.
35
+ */
36
+ touch(sessionId: string): Promise<void> | void;
37
+ }
38
+ /**
39
+ * Shape of session config.
40
+ */
41
+ export interface SessionConfig {
42
+ /**
43
+ * Enable/disable sessions temporarily
44
+ */
45
+ enabled: boolean;
46
+ /**
47
+ * The drivers to use
48
+ */
49
+ driver: keyof SessionDriversList;
50
+ /**
51
+ * The name of the cookie for storing the session id.
52
+ */
53
+ cookieName: string;
54
+ /**
55
+ * When set to true, the session id cookie will be removed
56
+ * when the user closes the browser.
57
+ *
58
+ * However, the persisted data will continue to exist until
59
+ * it gets expired.
60
+ */
61
+ clearWithBrowser: boolean;
62
+ /**
63
+ * How long the session data should be kept alive without any
64
+ * activity.
65
+ *
66
+ * The session id cookie will also live for the same duration, unless
67
+ * "clearWithBrowser" is enabled
68
+ *
69
+ * The value should be a time expression or a number in seconds
70
+ */
71
+ age: string | number;
72
+ /**
73
+ * Configuration used by the cookie driver and for storing the
74
+ * session id cookie.
75
+ */
76
+ cookie: Omit<Partial<CookieOptions>, 'maxAge' | 'expires'>;
77
+ }
78
+ /**
79
+ * Configuration used by the file driver.
80
+ */
81
+ export type FileDriverConfig = {
82
+ location: string;
83
+ };
84
+ /**
85
+ * Configuration used by the redis driver.
86
+ */
87
+ export type RedisDriverConfig = {
88
+ connection: keyof RedisConnections;
89
+ };
90
+ /**
91
+ * Extending session config with the drivers config
92
+ */
93
+ export interface SessionConfig {
94
+ file?: FileDriverConfig;
95
+ redis?: RedisDriverConfig;
96
+ }
97
+ /**
98
+ * List of the session drivers. The list can be extended using
99
+ * declaration merging
100
+ */
101
+ export interface SessionDriversList {
102
+ file: (config: SessionConfig, ctx: HttpContext) => FileDriver;
103
+ cookie: (config: SessionConfig, ctx: HttpContext) => CookieDriver;
104
+ redis: (config: SessionConfig, ctx: HttpContext) => RedisDriver;
105
+ memory: (config: SessionConfig, ctx: HttpContext) => MemoryDriver;
106
+ }
@@ -0,0 +1,9 @@
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
+ export {};
@@ -7,107 +7,45 @@ import app from '@adonisjs/core/services/app'
7
7
  import { defineConfig } from '@adonisjs/session'
8
8
 
9
9
  export default defineConfig({
10
- /*
11
- |--------------------------------------------------------------------------
12
- | Enable/Disable sessions
13
- |--------------------------------------------------------------------------
14
- |
15
- | Setting the following property to "false" will disable the session for the
16
- | entire application
17
- |
18
- */
19
10
  enabled: true,
20
-
21
- /*
22
- |--------------------------------------------------------------------------
23
- | Driver
24
- |--------------------------------------------------------------------------
25
- |
26
- | The session driver to use. You can choose between one of the following
27
- | drivers.
28
- |
29
- | - cookie (Uses signed cookies to store session values)
30
- | - file (Uses filesystem to store session values)
31
- | - redis (Uses redis. Make sure to install "@adonisjs/redis" as well)
32
- |
33
- | Note: Switching drivers will make existing sessions invalid.
34
- |
35
- */
36
- driver: env.get('SESSION_DRIVER'),
37
-
38
- /*
39
- |--------------------------------------------------------------------------
40
- | Cookie name
41
- |--------------------------------------------------------------------------
42
- |
43
- | The name of the cookie that will hold the session id.
44
- |
45
- */
46
11
  cookieName: 'adonis-session',
47
12
 
48
- /*
49
- |--------------------------------------------------------------------------
50
- | Clear session when browser closes
51
- |--------------------------------------------------------------------------
52
- |
53
- | Whether or not you want to destroy the session when browser closes. Setting
54
- | this value to "true" will ignore the "age".
55
- |
56
- */
13
+ /**
14
+ * When set to true, the session id cookie will be deleted
15
+ * once the user closes the browser.
16
+ */
57
17
  clearWithBrowser: false,
58
18
 
59
- /*
60
- |--------------------------------------------------------------------------
61
- | Session age
62
- |--------------------------------------------------------------------------
63
- |
64
- | The duration for which session stays active after no activity. A new HTTP
65
- | request to the server is considered as activity.
66
- |
67
- | The value can be a number in milliseconds or a string that must be valid
68
- | as per https://npmjs.org/package/ms package.
69
- |
70
- | Example: "2 days", "2.5 hrs", "1y", "5s" and so on.
71
- |
72
- */
19
+ /**
20
+ * Define how long to keep the session data alive without
21
+ * any activity.
22
+ */
73
23
  age: '2h',
74
24
 
75
- /*
76
- |--------------------------------------------------------------------------
77
- | Cookie values
78
- |--------------------------------------------------------------------------
79
- |
80
- | The cookie settings are used to setup the session id cookie and also the
81
- | driver will use the same values.
82
- |
83
- */
25
+ /**
26
+ * The driver to use. Make sure to validate the environment
27
+ * variable in order to infer the driver name without any
28
+ * errors.
29
+ */
30
+ driver: env.get('SESSION_DRIVER'),
31
+
84
32
  cookie: {
85
33
  path: '/',
86
34
  httpOnly: true,
87
35
  sameSite: false,
88
36
  },
89
37
 
90
- /*
91
- |--------------------------------------------------------------------------
92
- | Configuration for the file driver
93
- |--------------------------------------------------------------------------
94
- |
95
- | The file driver needs absolute path to the directory in which sessions
96
- | must be stored.
97
- |
98
- */
99
- file: {
100
- location: app.tmpPath('sessions'),
101
- },
102
-
103
- /*
104
- |--------------------------------------------------------------------------
105
- | Redis driver
106
- |--------------------------------------------------------------------------
107
- |
108
- | The redis connection you want session driver to use. The same connection
109
- | must be defined inside "config/redis.ts" file as well.
110
- |
111
- */
112
- redisConnection: 'local',
38
+ /**
39
+ * Settings for the file driver
40
+ */
41
+ // file: {
42
+ // location: app.tmpPath('sessions'),
43
+ // },
44
+
45
+ /**
46
+ * Settings for the redis driver
47
+ */
48
+ // redis: {
49
+ // connection: 'main'
50
+ // },
113
51
  })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@adonisjs/session",
3
3
  "description": "Session provider for AdonisJS",
4
- "version": "7.0.0-2",
4
+ "version": "7.0.0-4",
5
5
  "engines": {
6
6
  "node": ">=18.16.0"
7
7
  },
@@ -11,20 +11,21 @@
11
11
  "build/src",
12
12
  "build/stubs",
13
13
  "build/providers",
14
- "build/index.d.ts",
15
- "build/index.js",
16
14
  "build/configure.d.ts",
17
- "build/configure.js"
15
+ "build/configure.js",
16
+ "build/index.d.ts",
17
+ "build/index.js"
18
18
  ],
19
19
  "exports": {
20
20
  ".": "./build/index.js",
21
21
  "./session_provider": "./build/providers/session_provider.js",
22
22
  "./session_middleware": "./build/src/session_middleware.js",
23
+ "./client": "./build/src/client.js",
23
24
  "./types": "./build/src/types.js"
24
25
  },
25
26
  "scripts": {
26
27
  "pretest": "npm run lint",
27
- "test": "c8 npm run quick:test",
28
+ "test": "cross-env NODE_DEBUG=adonisjs:session c8 npm run quick:test",
28
29
  "clean": "del-cli build",
29
30
  "typecheck": "tsc --noEmit",
30
31
  "copy:templates": "copyfiles \"stubs/**/*.stub\" build",
@@ -35,29 +36,29 @@
35
36
  "format": "prettier --write .",
36
37
  "release": "np",
37
38
  "version": "npm run build",
38
- "sync-labels": "github-label-sync --labels .github/labels.json adonisjs/ally",
39
+ "sync-labels": "github-label-sync --labels .github/labels.json adonisjs/session",
39
40
  "quick:test": "node --enable-source-maps --loader=ts-node/esm bin/test.ts"
40
41
  },
41
42
  "devDependencies": {
42
- "@adonisjs/application": "7.1.2-8",
43
- "@adonisjs/core": "6.1.5-10",
43
+ "@adonisjs/core": "^6.1.5-15",
44
44
  "@adonisjs/eslint-config": "^1.1.8",
45
45
  "@adonisjs/prettier-config": "^1.1.8",
46
- "@adonisjs/redis": "8.0.0-3",
46
+ "@adonisjs/redis": "^8.0.0-8",
47
47
  "@adonisjs/tsconfig": "^1.1.8",
48
- "@adonisjs/view": "7.0.0-4",
49
- "@japa/api-client": "2.0.0-0",
50
- "@japa/assert": "2.0.0-1",
51
- "@japa/file-system": "2.0.0-1",
52
- "@japa/plugin-adonisjs": "2.0.0-1",
53
- "@japa/runner": "3.0.0-6",
54
- "@poppinss/dev-utils": "^2.0.3",
48
+ "@japa/api-client": "^2.0.0-0",
49
+ "@japa/assert": "^2.0.0-1",
50
+ "@japa/file-system": "^2.0.0-1",
51
+ "@japa/plugin-adonisjs": "^2.0.0-1",
52
+ "@japa/runner": "^3.0.0-6",
53
+ "@japa/snapshot": "^2.0.0-1",
55
54
  "@swc/core": "^1.3.70",
56
55
  "@types/node": "^20.4.2",
57
56
  "@types/set-cookie-parser": "^2.4.3",
58
57
  "@types/supertest": "^2.0.12",
58
+ "@vinejs/vine": "^1.6.0",
59
59
  "c8": "^8.0.0",
60
60
  "copyfiles": "^2.4.1",
61
+ "cross-env": "^7.0.3",
61
62
  "del-cli": "^5.0.0",
62
63
  "eslint": "^8.45.0",
63
64
  "github-label-sync": "^2.3.1",
@@ -70,11 +71,11 @@
70
71
  "typescript": "^5.1.6"
71
72
  },
72
73
  "dependencies": {
73
- "@poppinss/utils": "6.5.0-3"
74
+ "@poppinss/utils": "^6.5.0-5"
74
75
  },
75
76
  "peerDependencies": {
76
- "@adonisjs/core": "6.1.5-10",
77
- "@adonisjs/redis": "8.0.0-3"
77
+ "@adonisjs/core": "^6.1.5-15",
78
+ "@adonisjs/redis": "^8.0.0-8"
78
79
  },
79
80
  "peerDependenciesMeta": {
80
81
  "@adonisjs/redis": {
@@ -124,7 +125,9 @@
124
125
  ],
125
126
  "exclude": [
126
127
  "tests/**",
127
- "stubs/**"
128
+ "stubs/**",
129
+ "factories/**",
130
+ "bin/**"
128
131
  ]
129
132
  }
130
133
  }
@@ -1,2 +0,0 @@
1
- import { SessionManager } from '../session_manager.js';
2
- export declare function extendApiClient(sessionManager: SessionManager): void;