structured-fw 1.7.2 → 1.7.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.
package/README.md CHANGED
@@ -142,6 +142,14 @@ new Application(config);
142
142
  - `beforeAssetAccess` - runs when assets are being accessed, before response is sent. Callback receives `RequestContext` as the first argument
143
143
  - `afterAssetAccess` - runs when assets are being accessed, after response is sent. Callback receives `RequestContext` as the first argument
144
144
  - `pageNotFound` - runs when a request is received for which there is no registered request handler (route), and the requested URL is not an asset. Callback's result is sent as a response - a good use case is showing a 404 page. Callback receives `RequestContext` as the first argument
145
+ - `sessionsStart` - runs after session handling is enabled, payload is instance of `Session`
146
+ - `sessionsStop` - runs after session handling is disabled, payload is instance of `Session`
147
+ - `sessionCreated` - runs after a new session is created, payload is `SessionEntry`
148
+ - `sessionExpired` - runs after a session expires, payload is `sessionId`
149
+ - `sessionExtended` - runs after client with an existing session makes a request, payload is `sessionId`
150
+ - `sessionValueSet` - runs after a value is set in session, payload is `[sessionId, key, value]`
151
+ - `sessionValueRemove` - runs after a value is removed from session, payload is `[sessionId, key]`
152
+ - `sessionClear` - runs after session data is cleared, payload is `string` (sessionId)
145
153
  - **Callback to any of the `ApplicationEvents` is expected to be an async function**
146
154
  - `importEnv<T extends LooseObject>(smartPrimitives: boolean = true): T` - import ENV variables that start with `StructuredConfig.envPrefix`_ (if envPrefix is omitted from config, all ENV variables are returned). It is a generic method so that you can specify the expected return type. If `smartPrimitives = true` importEnv will convert the ENV values to type it feels is appropriate:
147
155
  - numeric values -> `number`
@@ -9,6 +9,7 @@ import { Request } from './Request.js';
9
9
  import { Handlebars } from './Handlebars.js';
10
10
  import { Cookies } from './Cookies.js';
11
11
  import { RequestContext } from './RequestContext.js';
12
+ import { SessionEntry } from '../Types.js';
12
13
  export declare class Application {
13
14
  readonly config: StructuredConfig;
14
15
  initialized: boolean;
@@ -25,7 +26,7 @@ export declare class Application {
25
26
  constructor(config: StructuredConfig);
26
27
  init(): Promise<void>;
27
28
  private start;
28
- on<E extends ApplicationEvents>(evt: E, callback: (payload: E extends 'beforeRequestHandler' | 'afterRequestHandler' | 'beforeAssetAccess' | 'afterAssetAccess' | 'pageNotFound' | 'requestHandleError' ? RequestContext<RequestContextData> : E extends 'documentCreated' ? Document : E extends 'afterComponentsLoaded' ? Components : E extends 'serverStarted' ? Server : undefined) => void): void;
29
+ on<E extends ApplicationEvents>(evt: E, callback: (payload: E extends 'beforeRequestHandler' | 'afterRequestHandler' | 'beforeAssetAccess' | 'afterAssetAccess' | 'pageNotFound' | 'requestHandleError' ? RequestContext<RequestContextData> : E extends 'documentCreated' ? Document : E extends 'afterComponentsLoaded' ? Components : E extends 'serverStarted' ? Server : E extends 'sessionsStart' ? Session : E extends 'sessionsStop' ? Session : E extends 'sessionCreated' ? SessionEntry : E extends 'sessionExpired' ? string : E extends 'sessionValueSet' ? [string, string, any] : E extends 'sessionValueRemove' ? [string, string] : E extends 'sessionClear' ? string : undefined) => void): void;
29
30
  emit(eventName: ApplicationEvents, payload?: any): Promise<Array<any>>;
30
31
  importEnv<T extends LooseObject>(smartPrimitives?: boolean): T;
31
32
  exportContextFields(...fields: Array<keyof RequestContextData>): void;
@@ -29,7 +29,6 @@ export class Application {
29
29
  this.session = new Session(this);
30
30
  this.request = new Request(this);
31
31
  this.components = new Components(this);
32
- this.session.start();
33
32
  if (this.config.autoInit) {
34
33
  this.init();
35
34
  }
@@ -73,6 +72,7 @@ export class Application {
73
72
  return '';
74
73
  }, this, true);
75
74
  await this.start();
75
+ this.session.start();
76
76
  this.initialized = true;
77
77
  }
78
78
  start() {
@@ -10,6 +10,7 @@ export declare class Session {
10
10
  constructor(app: Application);
11
11
  start(): void;
12
12
  stop(): void;
13
+ load(sessions: Record<string, SessionEntry>): void;
13
14
  private sessionInit;
14
15
  private generateId;
15
16
  private garbageCollect;
@@ -17,6 +17,7 @@ export class Session {
17
17
  if (ctx.sessionId) {
18
18
  this.application.cookies.set(ctx.response, this.application.config.session.cookieName, ctx.sessionId, this.application.config.session.durationSeconds);
19
19
  this.sessions[ctx.sessionId].lastRequest = new Date().getTime();
20
+ this.application.emit('sessionExtended', ctx.sessionId);
20
21
  }
21
22
  }
22
23
  }
@@ -25,9 +26,14 @@ export class Session {
25
26
  }
26
27
  start() {
27
28
  this.enabled = true;
29
+ this.application.emit('sessionsStart', this);
28
30
  }
29
31
  stop() {
30
32
  this.enabled = false;
33
+ this.application.emit('sessionsStop', this);
34
+ }
35
+ load(sessions) {
36
+ this.sessions = sessions;
31
37
  }
32
38
  sessionInit(ctx) {
33
39
  ctx.sessionId = this.generateId();
@@ -38,6 +44,7 @@ export class Session {
38
44
  data: {}
39
45
  };
40
46
  this.sessions[ctx.sessionId] = sessionEntry;
47
+ this.application.emit('sessionCreated', sessionEntry);
41
48
  }
42
49
  generateId() {
43
50
  return randomString(this.application.config.session.keyLength);
@@ -49,6 +56,7 @@ export class Session {
49
56
  const sess = this.sessions[sessionId];
50
57
  if (time - sess.lastRequest > sessDurationMilliseconds) {
51
58
  delete this.sessions[sessionId];
59
+ this.application.emit('sessionExpired', sessionId);
52
60
  }
53
61
  }
54
62
  setTimeout(() => {
@@ -62,6 +70,7 @@ export class Session {
62
70
  if (this.sessions[sessionId]) {
63
71
  const session = this.sessions[sessionId];
64
72
  session.data[key] = value;
73
+ this.application.emit('sessionValueSet', [sessionId, key, value]);
65
74
  }
66
75
  }
67
76
  getValue(sessionId, key) {
@@ -85,6 +94,7 @@ export class Session {
85
94
  }
86
95
  if (this.sessions[sessionId] && this.sessions[sessionId].data[key]) {
87
96
  delete this.sessions[sessionId].data[key];
97
+ this.application.emit('sessionValueRemove', [sessionId, key]);
88
98
  }
89
99
  }
90
100
  clear(sessionId) {
@@ -93,6 +103,7 @@ export class Session {
93
103
  }
94
104
  if (this.sessions[sessionId]) {
95
105
  this.sessions[sessionId].data = {};
106
+ this.application.emit('sessionClear', sessionId);
96
107
  }
97
108
  }
98
109
  extract(sessionId, keys) {
@@ -1 +1 @@
1
- export type ApplicationEvents = 'serverStarted' | 'beforeRequestHandler' | 'afterRequestHandler' | 'requestHandleError' | 'beforeRoutes' | 'afterRoutes' | 'beforeComponentsLoad' | 'afterComponentsLoaded' | 'documentCreated' | 'beforeAssetAccess' | 'afterAssetAccess' | 'pageNotFound';
1
+ export type ApplicationEvents = 'serverStarted' | 'beforeRequestHandler' | 'afterRequestHandler' | 'requestHandleError' | 'beforeRoutes' | 'afterRoutes' | 'beforeComponentsLoad' | 'afterComponentsLoaded' | 'documentCreated' | 'beforeAssetAccess' | 'afterAssetAccess' | 'pageNotFound' | 'sessionsStart' | 'sessionsStop' | 'sessionCreated' | 'sessionExtended' | 'sessionExpired' | 'sessionValueSet' | 'sessionValueRemove' | 'sessionClear';
package/package.json CHANGED
@@ -19,7 +19,7 @@
19
19
  "license": "MIT",
20
20
  "type": "module",
21
21
  "main": "build/index",
22
- "version": "1.7.2",
22
+ "version": "1.7.4",
23
23
  "scripts": {
24
24
  "develop": "tsc --watch",
25
25
  "startDev": "cd build && nodemon --watch '../app/**/*' --watch '../build/**/*' -e js,html,hbs,css index.js",