@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,50 +1,48 @@
1
- "use strict";
2
1
  /**
3
2
  * @adonisjs/session
4
3
  *
5
- * (c) Harminder Virk <virk@adonisjs.com>
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.FileDriver = void 0;
12
- /// <reference path="../../adonis-typings/index.ts" />
13
- const path_1 = require("path");
14
- const utils_1 = require("@poppinss/utils");
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.config = config;
23
- if (!this.config.file || !this.config.file.location) {
24
- throw new utils_1.Exception('Missing "file.location" for session file driver inside "config/session" file', 500, 'E_INVALID_SESSION_DRIVER_CONFIG');
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 (0, path_1.join)(this.config.file.location, `${sessionId}.txt`);
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.getFilePath(sessionId);
39
- await (0, fs_extra_1.ensureFile)(filePath);
40
- const contents = await (0, fs_extra_1.readFile)(filePath, 'utf-8');
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 helpers_1.MessageBuilder().verify(contents.trim(), sessionId);
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 helpers_1.MessageBuilder().build(values, undefined, sessionId);
61
- await (0, fs_extra_1.outputFile)(this.getFilePath(sessionId), message);
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 (0, fs_extra_1.remove)(this.getFilePath(sessionId));
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) Harminder Virk <virk@adonisjs.com>
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
- /// <reference path="../../adonis-typings/index.d.ts" />
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) Harminder Virk <virk@adonisjs.com>
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();
@@ -1,29 +1,19 @@
1
1
  /**
2
2
  * @adonisjs/session
3
3
  *
4
- * (c) Harminder Virk <virk@adonisjs.com>
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
- /// <reference path="../../adonis-typings/index.d.ts" />
10
- import { SessionDriverContract, SessionConfig } from '@ioc:Adonis/Addons/Session';
11
- import { RedisManagerContract } from '@ioc:Adonis/Addons/Redis';
9
+ import type { RedisManagerContract } from '@adonisjs/redis/types';
10
+ import type { SessionDriverContract, SessionConfig } from '../types.js';
12
11
  /**
13
12
  * File driver to read/write session to filesystem
14
13
  */
15
14
  export declare class RedisDriver implements SessionDriverContract {
16
- private config;
17
- private redis;
18
- /**
19
- * Convert milliseconds to seconds
20
- */
21
- private ttl;
22
- constructor(config: SessionConfig, redis: RedisManagerContract);
23
- /**
24
- * Returns instance of the redis connection
25
- */
26
- private getRedisConnection;
15
+ #private;
16
+ constructor(config: SessionConfig, redis: RedisManagerContract<any>);
27
17
  /**
28
18
  * Returns file contents. A new file will be created if it's
29
19
  * missing.
@@ -0,0 +1,74 @@
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 { Exception } from '@poppinss/utils';
10
+ import string from '@poppinss/utils/string';
11
+ import { MessageBuilder } from '@poppinss/utils';
12
+ /**
13
+ * File driver to read/write session to filesystem
14
+ */
15
+ export class RedisDriver {
16
+ #config;
17
+ #redis;
18
+ #ttl;
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'
26
+ ? string.milliseconds.parse(this.#config.age)
27
+ : this.#config.age) / 1000);
28
+ if (!this.#config.redisConnection) {
29
+ throw new Exception('Missing redisConnection for session redis driver inside "config/session" file', { code: 'E_INVALID_SESSION_DRIVER_CONFIG', status: 500 });
30
+ }
31
+ }
32
+ /**
33
+ * Returns instance of the redis connection
34
+ */
35
+ #getRedisConnection() {
36
+ return this.#redis.connection(this.#config.redisConnection);
37
+ }
38
+ /**
39
+ * Returns file contents. A new file will be created if it's
40
+ * missing.
41
+ */
42
+ async read(sessionId) {
43
+ const contents = await this.#getRedisConnection().get(sessionId);
44
+ if (!contents) {
45
+ return null;
46
+ }
47
+ const verifiedContents = new MessageBuilder().verify(contents, sessionId);
48
+ if (typeof verifiedContents !== 'object') {
49
+ return null;
50
+ }
51
+ return verifiedContents;
52
+ }
53
+ /**
54
+ * Write session values to a file
55
+ */
56
+ async write(sessionId, values) {
57
+ if (typeof values !== 'object') {
58
+ throw new Error('Session file driver expects an object of values');
59
+ }
60
+ await this.#getRedisConnection().setex(sessionId, this.#ttl, new MessageBuilder().build(values, undefined, sessionId));
61
+ }
62
+ /**
63
+ * Cleanup session file by removing it
64
+ */
65
+ async destroy(sessionId) {
66
+ await this.#getRedisConnection().del(sessionId);
67
+ }
68
+ /**
69
+ * Updates the value expiry
70
+ */
71
+ async touch(sessionId) {
72
+ await this.#getRedisConnection().expire(sessionId, this.#ttl);
73
+ }
74
+ }
@@ -1,15 +1,12 @@
1
- /// <reference path="../../adonis-typings/session.d.ts" />
2
- import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
3
- import { SessionConfig, SessionContract, AllowedSessionValues, SessionDriverContract } from '@ioc:Adonis/Addons/Session';
4
- import { Store } from '../Store';
1
+ import type { SessionConfig, SessionDriverContract, AllowedSessionValues } from './types.js';
2
+ import type { HttpContext } from '@adonisjs/core/http';
3
+ import { Store } from './store.js';
5
4
  /**
6
5
  * Session class exposes the API to read/write values to the session for
7
6
  * a given request.
8
7
  */
9
- export declare class Session implements SessionContract {
10
- private ctx;
11
- private config;
12
- private driver;
8
+ export declare class Session {
9
+ #private;
13
10
  /**
14
11
  * Set to true inside the `initiate` method
15
12
  */
@@ -34,21 +31,6 @@ export declare class Session implements SessionContract {
34
31
  * A copy of previously set flash messages
35
32
  */
36
33
  flashMessages: Store;
37
- /**
38
- * Session id for the current request. It will be different
39
- * from the "this.sessionId" when regenerate is called.
40
- */
41
- private currentSessionId;
42
- /**
43
- * A instance of store with values read from the driver. The store
44
- * in initiated inside the [[initiate]] method
45
- */
46
- private store;
47
- /**
48
- * Whether or not to re-generate the session id before comitting
49
- * session values.
50
- */
51
- private regeneratedSessionId;
52
34
  /**
53
35
  * A copy of flash messages. The `input` messages
54
36
  * are overwritten when any of the input related
@@ -57,50 +39,7 @@ export declare class Session implements SessionContract {
57
39
  * The `others` object is expanded with each call.
58
40
  */
59
41
  responseFlashMessages: Store;
60
- /**
61
- * Session key for setting flash messages
62
- */
63
- private flashMessagesKey;
64
- constructor(ctx: HttpContextContract, config: SessionConfig, driver: SessionDriverContract);
65
- /**
66
- * Returns a merged copy of flash messages or null
67
- * when nothing is set
68
- */
69
- private setFlashMessages;
70
- /**
71
- * Returns the existing session id or creates one.
72
- */
73
- private getSessionId;
74
- /**
75
- * Ensures the session store is initialized
76
- */
77
- private ensureIsReady;
78
- /**
79
- * Raises exception when session store is in readonly mode
80
- */
81
- private ensureIsMutable;
82
- /**
83
- * Touches the session cookie
84
- */
85
- private touchSessionCookie;
86
- /**
87
- * Commits the session value to the store
88
- */
89
- private commitValuesToStore;
90
- /**
91
- * Touches the driver to make sure the session values doesn't expire
92
- */
93
- private touchDriver;
94
- /**
95
- * Reading flash messages from the last HTTP request and
96
- * updating the flash messages bag
97
- */
98
- private readLastRequestFlashMessage;
99
- /**
100
- * Share flash messages & read only session's functions with views
101
- * (only when view property exists)
102
- */
103
- private shareLocalsWithView;
42
+ constructor(ctx: HttpContext, config: SessionConfig, driver: SessionDriverContract);
104
43
  /**
105
44
  * Initiating the session by reading it's value from the
106
45
  * driver and feeding it to a store.