@adonisjs/session 7.0.0-0 → 7.0.0-2

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.
package/README.md CHANGED
@@ -1,53 +1,36 @@
1
- <div align="center">
2
- <img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1558612869/adonis-readme_zscycu.jpg" width="600px">
3
- </div>
1
+ # @adonisjs/session
4
2
 
5
3
  <br />
6
4
 
7
- <div align="center">
8
- <h3>Sessions</h3>
9
- <p>This package adds support for sessions to AdonisJS</p>
10
- </div>
5
+ [![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] [![snyk-image]][snyk-url]
11
6
 
12
- <br />
7
+ ## Introduction
8
+ Add sessions to your AdonisJS application. Cookie, Redis, and File drivers are included out of the box.
13
9
 
14
- <div align="center">
15
-
16
- [![gh-workflow-image]][gh-workflow-url] [![typescript-image]][typescript-url] [![npm-image]][npm-url] [![license-image]][license-url] [![synk-image]][synk-url]
17
-
18
- </div>
19
-
20
- <div align="center">
21
- <h3>
22
- <a href="https://adonisjs.com">
23
- Website
24
- </a>
25
- <span> | </span>
26
- <a href="https://docs.adonisjs.com/guides/session">
27
- Guides
28
- </a>
29
- <span> | </span>
30
- <a href="CONTRIBUTING.md">
31
- Contributing
32
- </a>
33
- </h3>
34
- </div>
35
-
36
- <div align="center">
37
- <sub>Built with ❤︎ by <a href="https://twitter.com/AmanVirk1">Harminder Virk</a>
38
- </div>
39
-
40
- [gh-workflow-image]: https://img.shields.io/github/workflow/status/adonisjs/session/test?style=for-the-badge
41
- [gh-workflow-url]: https://github.com/adonisjs/session/actions/workflows/test.yml "Github action"
10
+ ## Official Documentation
11
+ The documentation is available on the [AdonisJS website](https://docs.adonisjs.com/guides/sessions)
42
12
 
43
- [typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
44
- [typescript-url]: "typescript"
13
+ ## Contributing
14
+ One of the primary goals of AdonisJS is to have a vibrant community of users and contributors who believes in the principles of the framework.
15
+
16
+ We encourage you to read the [contribution guide](https://github.com/adonisjs/.github/blob/main/docs/CONTRIBUTING.md) before contributing to the framework.
17
+
18
+ ## Code of Conduct
19
+ In order to ensure that the AdonisJS community is welcoming to all, please review and abide by the [Code of Conduct](https://github.com/adonisjs/.github/blob/main/docs/CODE_OF_CONDUCT.md).
20
+
21
+ ## License
22
+ AdonisJS ally is open-sourced software licensed under the [MIT license](LICENSE.md).
23
+
24
+ [gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/adonisjs/session/test.yml?style=for-the-badge
25
+ [gh-workflow-url]: https://github.com/adonisjs/session/actions/workflows/test.yml "Github action"
45
26
 
46
27
  [npm-image]: https://img.shields.io/npm/v/@adonisjs/session/latest.svg?style=for-the-badge&logo=npm
47
28
  [npm-url]: https://www.npmjs.com/package/@adonisjs/session/v/latest "npm"
48
29
 
49
- [license-image]: https://img.shields.io/npm/l/@adonisjs/session?color=blueviolet&style=for-the-badge
50
- [license-url]: LICENSE.md "license"
30
+ [typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
31
+
32
+ [license-url]: LICENSE.md
33
+ [license-image]: https://img.shields.io/github/license/adonisjs/session?style=for-the-badge
51
34
 
52
- [synk-image]: https://img.shields.io/snyk/vulnerabilities/github/adonisjs/session?label=Synk%20Vulnerabilities&style=for-the-badge
53
- [synk-url]: https://snyk.io/test/github/adonisjs/session?targetFile=package.json "synk"
35
+ [snyk-image]: https://img.shields.io/snyk/vulnerabilities/github/adonisjs/session?label=Snyk%20Vulnerabilities&style=for-the-badge
36
+ [snyk-url]: https://snyk.io/test/github/adonisjs/session?targetFile=package.json "snyk"
@@ -1,5 +1,6 @@
1
- import { ApplicationService } from '@adonisjs/core/types';
1
+ import type { ApplicationService } from '@adonisjs/core/types';
2
2
  export default class SessionProvider {
3
+ #private;
3
4
  protected app: ApplicationService;
4
5
  constructor(app: ApplicationService);
5
6
  /**
@@ -7,7 +7,7 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  import { extendHttpContext } from '../src/bindings/http_context.js';
10
- import { extendApiClient } from '../src/bindings/api_client.js';
10
+ import SessionMiddleware from '../src/session_middleware.js';
11
11
  export default class SessionProvider {
12
12
  app;
13
13
  constructor(app) {
@@ -24,20 +24,32 @@ export default class SessionProvider {
24
24
  const config = this.app.config.get('session', {});
25
25
  return new SessionManager(config, encryption, redis);
26
26
  });
27
+ this.app.container.bind(SessionMiddleware, async () => {
28
+ const session = await this.app.container.make('session');
29
+ return new SessionMiddleware(session);
30
+ });
31
+ }
32
+ /**
33
+ * Register Japa API Client bindings
34
+ */
35
+ async #registerApiClientBindings(session) {
36
+ if (this.app.getEnvironment() === 'test') {
37
+ const { extendApiClient } = await import('../src/bindings/api_client.js');
38
+ extendApiClient(session);
39
+ }
27
40
  }
28
41
  /**
29
42
  * Register bindings
30
43
  */
31
44
  async boot() {
32
- const sessionManager = await this.app.container.make('session');
45
+ const session = await this.app.container.make('session');
33
46
  /**
34
47
  * Add `session` getter to the HttpContext class
35
48
  */
36
- extendHttpContext(sessionManager);
49
+ extendHttpContext(session);
37
50
  /**
38
- * Add some macros and getter to japa/api-client classes for
39
- * easier testing
51
+ * Extend Japa API Client
40
52
  */
41
- extendApiClient(sessionManager);
53
+ await this.#registerApiClientBindings(session);
42
54
  }
43
55
  }
@@ -6,11 +6,10 @@
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 { join } from 'node:path';
9
+ import { dirname, join } from 'node:path';
10
+ import { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
10
11
  import { Exception } from '@poppinss/utils';
11
12
  import { MessageBuilder } from '@poppinss/utils';
12
- import { ensureFile, outputFile, remove } from 'fs-extra/esm';
13
- import { readFile } from 'node:fs/promises';
14
13
  /**
15
14
  * File driver to read/write session to filesystem
16
15
  */
@@ -28,13 +27,47 @@ export class FileDriver {
28
27
  #getFilePath(sessionId) {
29
28
  return join(this.#config.file.location, `${sessionId}.txt`);
30
29
  }
30
+ /**
31
+ * Check if the given path exists or not
32
+ */
33
+ async #pathExists(path) {
34
+ try {
35
+ await access(path);
36
+ return true;
37
+ }
38
+ catch {
39
+ return false;
40
+ }
41
+ }
42
+ /**
43
+ * Output file with contents to the given path
44
+ */
45
+ async #outputFile(path, content) {
46
+ const pathDirname = dirname(path);
47
+ const dirExists = await this.#pathExists(pathDirname);
48
+ if (!dirExists) {
49
+ await mkdir(pathDirname, { recursive: true });
50
+ }
51
+ await writeFile(path, content, 'utf-8');
52
+ }
53
+ /**
54
+ * Ensure the file exists. Create it if missing
55
+ */
56
+ async #ensureFile(path) {
57
+ const pathDirname = dirname(path);
58
+ const dirExists = await this.#pathExists(pathDirname);
59
+ if (!dirExists) {
60
+ await mkdir(pathDirname, { recursive: true });
61
+ await writeFile(path, '', 'utf-8');
62
+ }
63
+ }
31
64
  /**
32
65
  * Returns file contents. A new file will be created if it's
33
66
  * missing.
34
67
  */
35
68
  async read(sessionId) {
36
69
  const filePath = this.#getFilePath(sessionId);
37
- await ensureFile(filePath);
70
+ await this.#ensureFile(filePath);
38
71
  const contents = await readFile(filePath, 'utf-8');
39
72
  if (!contents.trim()) {
40
73
  return null;
@@ -56,13 +89,13 @@ export class FileDriver {
56
89
  throw new Error('Session file driver expects an object of values');
57
90
  }
58
91
  const message = new MessageBuilder().build(values, undefined, sessionId);
59
- await outputFile(this.#getFilePath(sessionId), message);
92
+ await this.#outputFile(this.#getFilePath(sessionId), message);
60
93
  }
61
94
  /**
62
95
  * Cleanup session file by removing it
63
96
  */
64
97
  async destroy(sessionId) {
65
- await remove(this.#getFilePath(sessionId));
98
+ await rm(this.#getFilePath(sessionId), { force: true });
66
99
  }
67
100
  /**
68
101
  * Writes the value by reading it from the store
@@ -162,11 +162,9 @@ export class Session {
162
162
  * (only when view property exists)
163
163
  */
164
164
  #shareLocalsWithView() {
165
- // @ts-ignore may need to expose ./types/extended from adonisjs/view
166
165
  if (!this.#ctx['view'] || typeof this.#ctx['view'].share !== 'function') {
167
166
  return;
168
167
  }
169
- // @ts-ignore may need to expose ./types/extended from adonisjs/view
170
168
  this.#ctx['view'].share({
171
169
  flashMessages: this.flashMessages,
172
170
  session: {
@@ -1,5 +1,8 @@
1
1
  import type { HttpContext } from '@adonisjs/core/http';
2
2
  import type { NextFn } from '@adonisjs/core/types/http';
3
+ import { SessionManager } from './session_manager.js';
3
4
  export default class SessionMiddleware {
5
+ protected session: SessionManager;
6
+ constructor(session: SessionManager);
4
7
  handle(ctx: HttpContext, next: NextFn): Promise<void>;
5
8
  }
@@ -1,7 +1,10 @@
1
1
  export default class SessionMiddleware {
2
+ session;
3
+ constructor(session) {
4
+ this.session = session;
5
+ }
2
6
  async handle(ctx, next) {
3
- const sessionManager = (await ctx.containerResolver.make('session'));
4
- if (!sessionManager.isEnabled()) {
7
+ if (!this.session.isEnabled()) {
5
8
  return;
6
9
  }
7
10
  /**
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-0",
4
+ "version": "7.0.0-2",
5
5
  "engines": {
6
6
  "node": ">=18.16.0"
7
7
  },
@@ -40,10 +40,10 @@
40
40
  },
41
41
  "devDependencies": {
42
42
  "@adonisjs/application": "7.1.2-8",
43
- "@adonisjs/core": "6.1.5-8",
43
+ "@adonisjs/core": "6.1.5-10",
44
44
  "@adonisjs/eslint-config": "^1.1.8",
45
45
  "@adonisjs/prettier-config": "^1.1.8",
46
- "@adonisjs/redis": "8.0.0-1",
46
+ "@adonisjs/redis": "8.0.0-3",
47
47
  "@adonisjs/tsconfig": "^1.1.8",
48
48
  "@adonisjs/view": "7.0.0-4",
49
49
  "@japa/api-client": "2.0.0-0",
@@ -53,7 +53,6 @@
53
53
  "@japa/runner": "3.0.0-6",
54
54
  "@poppinss/dev-utils": "^2.0.3",
55
55
  "@swc/core": "^1.3.70",
56
- "@types/fs-extra": "^11.0.1",
57
56
  "@types/node": "^20.4.2",
58
57
  "@types/set-cookie-parser": "^2.4.3",
59
58
  "@types/supertest": "^2.0.12",
@@ -71,16 +70,18 @@
71
70
  "typescript": "^5.1.6"
72
71
  },
73
72
  "dependencies": {
74
- "@poppinss/utils": "6.5.0-3",
75
- "fs-extra": "^11.1.1"
73
+ "@poppinss/utils": "6.5.0-3"
76
74
  },
77
75
  "peerDependencies": {
78
- "@adonisjs/core": "6.1.5-8",
79
- "@adonisjs/redis": "8.0.0-1"
76
+ "@adonisjs/core": "6.1.5-10",
77
+ "@adonisjs/redis": "8.0.0-3"
80
78
  },
81
79
  "peerDependenciesMeta": {
82
80
  "@adonisjs/redis": {
83
81
  "optional": true
82
+ },
83
+ "@japa/api-client": {
84
+ "optional": true
84
85
  }
85
86
  },
86
87
  "author": "virk,adonisjs",