@adonisjs/session 7.0.0-6 → 7.0.0-7

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,3 +1,4 @@
1
+ import type { Edge } from 'edge.js';
1
2
  import type { ApplicationService } from '@adonisjs/core/types';
2
3
  /**
3
4
  * Session provider configures the session management inside an
@@ -6,6 +7,18 @@ import type { ApplicationService } from '@adonisjs/core/types';
6
7
  export default class SessionProvider {
7
8
  protected app: ApplicationService;
8
9
  constructor(app: ApplicationService);
10
+ /**
11
+ * Returns edge when it's installed
12
+ */
13
+ protected getEdge(): Promise<Edge | null>;
14
+ /**
15
+ * Registering muddleware
16
+ */
9
17
  register(): void;
18
+ /**
19
+ * Registering the active driver when middleware is used
20
+ * +
21
+ * Adding edge tags (if edge is installed)
22
+ */
10
23
  boot(): Promise<void>;
11
24
  }
@@ -6,6 +6,7 @@
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 debug from '../src/debug.js';
9
10
  import { registerSessionDriver } from '../src/helpers.js';
10
11
  import SessionMiddleware from '../src/session_middleware.js';
11
12
  /**
@@ -17,6 +18,22 @@ export default class SessionProvider {
17
18
  constructor(app) {
18
19
  this.app = app;
19
20
  }
21
+ /**
22
+ * Returns edge when it's installed
23
+ */
24
+ async getEdge() {
25
+ try {
26
+ const { default: edge } = await import('edge.js');
27
+ debug('Detected edge.js package. Adding session primitives to it');
28
+ return edge;
29
+ }
30
+ catch {
31
+ return null;
32
+ }
33
+ }
34
+ /**
35
+ * Registering muddleware
36
+ */
20
37
  register() {
21
38
  this.app.container.singleton(SessionMiddleware, async (resolver) => {
22
39
  const config = this.app.config.get('session', {});
@@ -24,10 +41,20 @@ export default class SessionProvider {
24
41
  return new SessionMiddleware(config, emitter);
25
42
  });
26
43
  }
44
+ /**
45
+ * Registering the active driver when middleware is used
46
+ * +
47
+ * Adding edge tags (if edge is installed)
48
+ */
27
49
  async boot() {
28
50
  this.app.container.resolving(SessionMiddleware, async () => {
29
51
  const config = this.app.config.get('session');
30
52
  await registerSessionDriver(this.app, config.driver);
31
53
  });
54
+ const edge = await this.getEdge();
55
+ if (edge) {
56
+ const { edgePluginAdonisJSSession } = await import('../src/edge_plugin_adonisjs_session.js');
57
+ edge.use(edgePluginAdonisJSSession);
58
+ }
32
59
  }
33
60
  }
@@ -0,0 +1,6 @@
1
+ import type { PluginFn } from 'edge.js/types';
2
+ /**
3
+ * The edge plugin for AdonisJS Session adds tags to read
4
+ * flash messages
5
+ */
6
+ export declare const edgePluginAdonisJSSession: PluginFn<undefined>;
@@ -0,0 +1,92 @@
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 debug from './debug.js';
10
+ /**
11
+ * The edge plugin for AdonisJS Session adds tags to read
12
+ * flash messages
13
+ */
14
+ export const edgePluginAdonisJSSession = (edge) => {
15
+ debug('registering session tags with edge');
16
+ edge.registerTag({
17
+ tagName: 'flashMessage',
18
+ seekable: true,
19
+ block: true,
20
+ compile(parser, buffer, token) {
21
+ const expression = parser.utils.transformAst(parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser);
22
+ const key = parser.utils.stringify(expression);
23
+ /**
24
+ * Write an if statement
25
+ */
26
+ buffer.writeStatement(`if (state.flashMessages.has(${key})) {`, token.filename, token.loc.start.line);
27
+ /**
28
+ * Define a local variable
29
+ */
30
+ buffer.writeExpression(`let message = state.flashMessages.get(${key})`, token.filename, token.loc.start.line);
31
+ /**
32
+ * Create a local variables scope and tell the parser about
33
+ * the existence of the "message" variable
34
+ */
35
+ parser.stack.defineScope();
36
+ parser.stack.defineVariable('message');
37
+ /**
38
+ * Process component children using the parser
39
+ */
40
+ token.children.forEach((child) => {
41
+ parser.processToken(child, buffer);
42
+ });
43
+ /**
44
+ * Clear the scope of the local variables before we
45
+ * close the if statement
46
+ */
47
+ parser.stack.clearScope();
48
+ /**
49
+ * Close if statement
50
+ */
51
+ buffer.writeStatement(`}`, token.filename, token.loc.start.line);
52
+ },
53
+ });
54
+ edge.registerTag({
55
+ tagName: 'error',
56
+ seekable: true,
57
+ block: true,
58
+ compile(parser, buffer, token) {
59
+ const expression = parser.utils.transformAst(parser.utils.generateAST(token.properties.jsArg, token.loc, token.filename), token.filename, parser);
60
+ const key = parser.utils.stringify(expression);
61
+ /**
62
+ * Write an if statement
63
+ */
64
+ buffer.writeStatement(`if (!!state.flashMessages.get('errors')[${key}]) {`, token.filename, token.loc.start.line);
65
+ /**
66
+ * Define a local variable
67
+ */
68
+ buffer.writeExpression(`let messages = state.flashMessages.get('errors')[${key}]`, token.filename, token.loc.start.line);
69
+ /**
70
+ * Create a local variables scope and tell the parser about
71
+ * the existence of the "messages" variable
72
+ */
73
+ parser.stack.defineScope();
74
+ parser.stack.defineVariable('messages');
75
+ /**
76
+ * Process component children using the parser
77
+ */
78
+ token.children.forEach((child) => {
79
+ parser.processToken(child, buffer);
80
+ });
81
+ /**
82
+ * Clear the scope of the local variables before we
83
+ * close the if statement
84
+ */
85
+ parser.stack.clearScope();
86
+ /**
87
+ * Close if statement
88
+ */
89
+ buffer.writeStatement(`}`, token.filename, token.loc.start.line);
90
+ },
91
+ });
92
+ };
@@ -8,7 +8,7 @@
8
8
  */
9
9
  import lodash from '@poppinss/utils/lodash';
10
10
  import { cuid } from '@adonisjs/core/helpers';
11
- import { Store } from './store.js';
11
+ import { ReadOnlyStore, Store } from './store.js';
12
12
  import * as errors from './errors.js';
13
13
  import debug from './debug.js';
14
14
  /**
@@ -157,6 +157,19 @@ export class Session {
157
157
  this.flashMessages.update(this.pull(this.flashKey, null));
158
158
  }
159
159
  }
160
+ /**
161
+ * Share session with the templates. We assume the view property
162
+ * is a reference to edge templates
163
+ */
164
+ if ('view' in this.#ctx) {
165
+ this.#ctx.view.share({
166
+ session: new ReadOnlyStore(this.#store.all()),
167
+ flashMessages: new ReadOnlyStore(this.flashMessages.all()),
168
+ old: function (key, defaultValue) {
169
+ return this.flashMessages.get(key, defaultValue);
170
+ },
171
+ });
172
+ }
160
173
  this.#emitter.emit('session:initiated', { session: this });
161
174
  }
162
175
  /**
@@ -1,15 +1,50 @@
1
1
  import type { AllowedSessionValues, SessionData } from './types/main.js';
2
2
  /**
3
- * Session store encapsulates the session data and offers a
4
- * declarative API to mutate it.
3
+ * Readonly session store
5
4
  */
6
- export declare class Store {
7
- #private;
8
- constructor(values: SessionData | null);
5
+ export declare class ReadOnlyStore {
6
+ /**
7
+ * Underlying store values
8
+ */
9
+ protected values: SessionData;
9
10
  /**
10
11
  * Find if store is empty or not
11
12
  */
12
13
  get isEmpty(): boolean;
14
+ constructor(values: SessionData | null);
15
+ /**
16
+ * Get value for a given key
17
+ */
18
+ get(key: string, defaultValue?: any): any;
19
+ /**
20
+ * A boolean to know if value exists. Extra guards to check
21
+ * arrays for it's length as well.
22
+ */
23
+ has(key: string, checkForArraysLength?: boolean): boolean;
24
+ /**
25
+ * Get all values
26
+ */
27
+ all(): any;
28
+ /**
29
+ * Returns object representation of values
30
+ */
31
+ toObject(): any;
32
+ /**
33
+ * Returns the store values
34
+ */
35
+ toJSON(): any;
36
+ /**
37
+ * Returns string representation of the store
38
+ */
39
+ toString(): string;
40
+ }
41
+ /**
42
+ * Session store encapsulates the session data and offers a
43
+ * declarative API to mutate it.
44
+ */
45
+ export declare class Store extends ReadOnlyStore {
46
+ #private;
47
+ constructor(values: SessionData | null);
13
48
  /**
14
49
  * Find if the store has been modified.
15
50
  */
@@ -53,29 +88,4 @@ export declare class Store {
53
88
  * Reset store by clearing it's values.
54
89
  */
55
90
  clear(): void;
56
- /**
57
- * Get value for a given key
58
- */
59
- get(key: string, defaultValue?: any): any;
60
- /**
61
- * A boolean to know if value exists. Extra guards to check
62
- * arrays for it's length as well.
63
- */
64
- has(key: string, checkForArraysLength?: boolean): boolean;
65
- /**
66
- * Get all values
67
- */
68
- all(): any;
69
- /**
70
- * Returns object representation of values
71
- */
72
- toObject(): any;
73
- /**
74
- * Returns the store values
75
- */
76
- toJSON(): any;
77
- /**
78
- * Returns string representation of the store
79
- */
80
- toString(): string;
81
91
  }
@@ -9,27 +9,76 @@
9
9
  import lodash from '@poppinss/utils/lodash';
10
10
  import { RuntimeException } from '@poppinss/utils';
11
11
  /**
12
- * Session store encapsulates the session data and offers a
13
- * declarative API to mutate it.
12
+ * Readonly session store
14
13
  */
15
- export class Store {
14
+ export class ReadOnlyStore {
16
15
  /**
17
16
  * Underlying store values
18
17
  */
19
- #values;
18
+ values;
20
19
  /**
21
- * A boolean to know if store has been
22
- * modified
20
+ * Find if store is empty or not
23
21
  */
24
- #modified = false;
22
+ get isEmpty() {
23
+ return !this.values || Object.keys(this.values).length === 0;
24
+ }
25
25
  constructor(values) {
26
- this.#values = values || {};
26
+ this.values = values || {};
27
27
  }
28
28
  /**
29
- * Find if store is empty or not
29
+ * Get value for a given key
30
30
  */
31
- get isEmpty() {
32
- return !this.#values || Object.keys(this.#values).length === 0;
31
+ get(key, defaultValue) {
32
+ return lodash.get(this.values, key, defaultValue);
33
+ }
34
+ /**
35
+ * A boolean to know if value exists. Extra guards to check
36
+ * arrays for it's length as well.
37
+ */
38
+ has(key, checkForArraysLength = true) {
39
+ const value = this.get(key);
40
+ if (!Array.isArray(value)) {
41
+ return !!value;
42
+ }
43
+ return checkForArraysLength ? value.length > 0 : !!value;
44
+ }
45
+ /**
46
+ * Get all values
47
+ */
48
+ all() {
49
+ return this.values;
50
+ }
51
+ /**
52
+ * Returns object representation of values
53
+ */
54
+ toObject() {
55
+ return this.all();
56
+ }
57
+ /**
58
+ * Returns the store values
59
+ */
60
+ toJSON() {
61
+ return this.all();
62
+ }
63
+ /**
64
+ * Returns string representation of the store
65
+ */
66
+ toString() {
67
+ return JSON.stringify(this.all());
68
+ }
69
+ }
70
+ /**
71
+ * Session store encapsulates the session data and offers a
72
+ * declarative API to mutate it.
73
+ */
74
+ export class Store extends ReadOnlyStore {
75
+ /**
76
+ * A boolean to know if store has been
77
+ * modified
78
+ */
79
+ #modified = false;
80
+ constructor(values) {
81
+ super(values);
33
82
  }
34
83
  /**
35
84
  * Find if the store has been modified.
@@ -42,14 +91,14 @@ export class Store {
42
91
  */
43
92
  set(key, value) {
44
93
  this.#modified = true;
45
- lodash.set(this.#values, key, value);
94
+ lodash.set(this.values, key, value);
46
95
  }
47
96
  /**
48
97
  * Remove key
49
98
  */
50
99
  unset(key) {
51
100
  this.#modified = true;
52
- lodash.unset(this.#values, key);
101
+ lodash.unset(this.values, key);
53
102
  }
54
103
  /**
55
104
  * Pull value from the store. It is same as calling
@@ -88,14 +137,14 @@ export class Store {
88
137
  */
89
138
  update(values) {
90
139
  this.#modified = true;
91
- this.#values = values;
140
+ this.values = values;
92
141
  }
93
142
  /**
94
143
  * Update to merge values
95
144
  */
96
145
  merge(values) {
97
146
  this.#modified = true;
98
- lodash.merge(this.#values, values);
147
+ lodash.merge(this.values, values);
99
148
  }
100
149
  /**
101
150
  * Reset store by clearing it's values.
@@ -103,45 +152,4 @@ export class Store {
103
152
  clear() {
104
153
  this.update({});
105
154
  }
106
- /**
107
- * Get value for a given key
108
- */
109
- get(key, defaultValue) {
110
- return lodash.get(this.#values, key, defaultValue);
111
- }
112
- /**
113
- * A boolean to know if value exists. Extra guards to check
114
- * arrays for it's length as well.
115
- */
116
- has(key, checkForArraysLength = true) {
117
- const value = this.get(key);
118
- if (!Array.isArray(value)) {
119
- return !!value;
120
- }
121
- return checkForArraysLength ? value.length > 0 : !!value;
122
- }
123
- /**
124
- * Get all values
125
- */
126
- all() {
127
- return this.#values;
128
- }
129
- /**
130
- * Returns object representation of values
131
- */
132
- toObject() {
133
- return this.all();
134
- }
135
- /**
136
- * Returns the store values
137
- */
138
- toJSON() {
139
- return this.all();
140
- }
141
- /**
142
- * Returns string representation of the store
143
- */
144
- toString() {
145
- return JSON.stringify(this.all());
146
- }
147
155
  }
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-6",
4
+ "version": "7.0.0-7",
5
5
  "engines": {
6
6
  "node": ">=18.16.0"
7
7
  },
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "devDependencies": {
43
43
  "@adonisjs/assembler": "^6.1.3-18",
44
- "@adonisjs/core": "^6.1.5-19",
44
+ "@adonisjs/core": "^6.1.5-22",
45
45
  "@adonisjs/eslint-config": "^1.1.8",
46
46
  "@adonisjs/prettier-config": "^1.1.8",
47
47
  "@adonisjs/redis": "^8.0.0-10",
@@ -60,6 +60,7 @@
60
60
  "copyfiles": "^2.4.1",
61
61
  "cross-env": "^7.0.3",
62
62
  "del-cli": "^5.0.0",
63
+ "edge.js": "^6.0.0-8",
63
64
  "eslint": "^8.47.0",
64
65
  "github-label-sync": "^2.3.1",
65
66
  "husky": "^8.0.3",
@@ -74,14 +75,15 @@
74
75
  "@poppinss/utils": "^6.5.0-5"
75
76
  },
76
77
  "peerDependencies": {
77
- "@adonisjs/core": "^6.1.5-19",
78
- "@adonisjs/redis": "^8.0.0-10"
78
+ "@adonisjs/core": "^6.1.5-22",
79
+ "@adonisjs/redis": "^8.0.0-10",
80
+ "edge.js": "^6.0.0-8"
79
81
  },
80
82
  "peerDependenciesMeta": {
81
83
  "@adonisjs/redis": {
82
84
  "optional": true
83
85
  },
84
- "@japa/api-client": {
86
+ "edge.js": {
85
87
  "optional": true
86
88
  }
87
89
  },