@morojs/moro 1.8.4 → 1.8.5

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.
@@ -1142,7 +1142,9 @@ export class MoroHttpServer {
1142
1142
  httpReq.method === MoroHttpServer.METHOD_PATCH;
1143
1143
  const hookManager = this.hookManager;
1144
1144
  const hasHooks = !!(hookManager &&
1145
- (hookManager.hasHooks === undefined || hookManager.hasHooks('request')));
1145
+ (hookManager.hasHooks === undefined ||
1146
+ hookManager.hasHooks('request') ||
1147
+ hookManager.hasHooks('response')));
1146
1148
  if (needsBody || hasHooks || this.globalMiddleware.length > 0) {
1147
1149
  // Slow path: real awaits ahead (body parse / hooks / middleware).
1148
1150
  // Errors are fully handled inside - the returned promise never rejects.
@@ -1190,6 +1192,14 @@ export class MoroHttpServer {
1190
1192
  request: httpReq,
1191
1193
  response: httpRes,
1192
1194
  });
1195
+ // Post-response hooks (session persistence, auth session refresh,
1196
+ // CDN invalidation) run after the response is flushed, off the
1197
+ // request's critical path.
1198
+ if (this.hookManager.hasHooks === undefined || this.hookManager.hasHooks('response')) {
1199
+ httpRes.once('finish', () => {
1200
+ void Promise.resolve(this.hookManager.execute('response', { request: httpReq, response: httpRes })).catch((error) => this.logger?.error?.(`Response hook error: ${error?.message || error}`, 'Hooks'));
1201
+ });
1202
+ }
1193
1203
  }
1194
1204
  // executeMiddleware returns undefined when the whole chain completed
1195
1205
  // synchronously, so a sync chain costs zero promise allocations.
@@ -1273,6 +1283,13 @@ export class MoroHttpServer {
1273
1283
  // Shared error handling for all request paths (sync throw, slow-path catch,
1274
1284
  // and promise rejections from async handlers/middleware). Never throws.
1275
1285
  async onRequestError(error, httpReq, httpRes, req) {
1286
+ // Error hooks are observational (Sentry-style reporting, audit trails):
1287
+ // fire-and-forget so a slow or throwing observer can never delay or
1288
+ // alter the error response.
1289
+ if (this.hookManager &&
1290
+ (this.hookManager.hasHooks === undefined || this.hookManager.hasHooks('error'))) {
1291
+ void Promise.resolve(this.hookManager.execute('error', { request: httpReq, response: httpRes, error })).catch((hookError) => this.logger?.error?.(`Error hook error: ${hookError?.message || hookError}`, 'Hooks'));
1292
+ }
1276
1293
  try {
1277
1294
  // Debug: Log the actual error and where it came from
1278
1295
  this.logger.debug('Request error details', 'RequestHandler', {