| 116 | * }) |
| 117 | */ |
| 118 | export default class SessionMiddleware<KnownStores extends Record<string, SessionStoreFactory>> { |
| 119 | /** |
| 120 | * Session configuration including store settings |
| 121 | */ |
| 122 | #config: SessionConfig & { |
| 123 | store: keyof KnownStores |
| 124 | stores: KnownStores |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Event emitter service for session events |
| 129 | */ |
| 130 | #emitter: EmitterService |
| 131 | |
| 132 | /** |
| 133 | * Creates a new session middleware instance |
| 134 | * |
| 135 | * @param config - Session configuration with store settings |
| 136 | * @param emitter - Event emitter service |
| 137 | */ |
| 138 | constructor( |
| 139 | config: SessionConfig & { |
| 140 | store: keyof KnownStores |
| 141 | stores: KnownStores |
| 142 | }, |
| 143 | emitter: EmitterService |
| 144 | ) { |
| 145 | this.#config = config |
| 146 | this.#emitter = emitter |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Handles the HTTP request by initializing session and committing changes |
| 151 | * |
| 152 | * @param ctx - HTTP context |
| 153 | * @param next - Next middleware function |
| 154 | */ |
| 155 | async handle(ctx: HttpContext, next: NextFn) { |
| 156 | if (!this.#config.enabled) { |
| 157 | return next() |
| 158 | } |
| 159 | |
| 160 | ctx.session = new Session( |
| 161 | this.#config, |
| 162 | this.#config.stores[this.#config.store], // reference to store factory |
| 163 | this.#emitter, |
| 164 | ctx |
| 165 | ) |
| 166 | |
| 167 | /** |
| 168 | * Initiate session store |
| 169 | */ |
| 170 | await ctx.session.initiate(false) |
| 171 | |
| 172 | /** |
| 173 | * Call next middlewares or route handler |
| 174 | */ |
| 175 | const response = await next() |
nothing calls this directly
no outgoing calls
no test coverage detected