use got instead of axios, some cleanup, problem with async context being lost for...
[websub-hub] / src / service.js
index e76959978eddab7a554dbe80eb4d31ffa9c10d8a..d77d06c7794819967478df957afe0cfea7e0bbee 100644 (file)
@@ -15,12 +15,12 @@ const path = require('path');
 const _fileScope = common.fileScope(__filename);
 
 class Service extends Dingus {
-  constructor(logger, db, options) {
+  constructor(logger, db, options, asyncLocalStorage) {
     super(logger, {
       ...options.dingus,
       ignoreTrailingSlash: false,
     });
-
+    this.asyncLocalStorage = asyncLocalStorage;
     this.manager = new Manager(logger, db, options);
     this.authenticator = new Authenticator(logger, db, options);
     this.sessionManager = new SessionManager(logger, this.authenticator, options);
@@ -70,6 +70,24 @@ class Service extends Dingus {
   }
 
 
+  /**
+   * Rearrange logging data.
+   * @param {http.ClientRequest} req
+   * @param {http.ServerResponse} res
+   * @param {Object} ctx
+   */
+  async preHandler(req, res, ctx) {
+    await super.preHandler(req, res, ctx);
+    const logObject = this.asyncLocalStorage.getStore();
+    // FIXME: for some reason, returning from the super.preHandler loses async context?
+    // Workaround until cause and solution are found.
+    if (logObject) {
+      logObject.requestId = ctx.requestId;
+      delete ctx.requestId;
+    }
+  }
+
+
   /**
    * @param {http.ClientRequest} req
    * @param {http.ServerResponse} res