@adonisjs/session 7.0.0-1 → 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.
@@ -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
@@ -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-1",
4
+ "version": "7.0.0-2",
5
5
  "engines": {
6
6
  "node": ">=18.16.0"
7
7
  },
@@ -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,8 +70,7 @@
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
76
  "@adonisjs/core": "6.1.5-10",
@@ -81,6 +79,9 @@
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",