diff --git a/src/als.js b/src/als.js new file mode 100644 index 0000000000..a3aec0220f --- /dev/null +++ b/src/als.js @@ -0,0 +1,7 @@ +'use strict'; + +const { AsyncLocalStorage } = require('async_hooks'); + +const asyncLocalStorage = new AsyncLocalStorage(); + +module.exports = asyncLocalStorage; diff --git a/src/plugins/hooks.js b/src/plugins/hooks.js index 59f904d186..d46a1fcdb0 100644 --- a/src/plugins/hooks.js +++ b/src/plugins/hooks.js @@ -96,6 +96,10 @@ Hooks.fire = async function (hook, params) { winston.warn(`[plugins] Unknown hookType: ${hookType}, hook : ${hook}`); return; } + if (params && typeof params === 'object' && !params.hasOwnProperty('caller')) { + const als = require('../als'); + params.caller = als.getStore(); + } const result = await hookTypeToMethod[hookType](hook, hookList, params); if (hook !== 'action:plugins.firehook' && hook !== 'filter:plugins.firehook') { diff --git a/src/socket.io/index.js b/src/socket.io/index.js index aaaceb2010..773f885791 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -73,7 +73,8 @@ function onConnection(socket) { onConnect(socket); socket.onAny((event, ...args) => { const payload = { data: [event].concat(args) }; - onMessage(socket, payload); + const als = require('../als'); + als.run({ uid: socket.uid }, onMessage, socket, payload); }); socket.on('disconnect', () => { diff --git a/src/webserver.js b/src/webserver.js index ca488e9c03..22343f6036 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -171,6 +171,10 @@ function setupExpressApp(app) { app.use(middleware.addHeaders); app.use(middleware.processRender); auth.initialize(app, middleware); + const als = require('./als'); + app.use((req, res, next) => { + als.run({ uid: req.uid }, next); + }); app.use(middleware.autoLocale); // must be added after auth middlewares are added const toobusy = require('toobusy-js'); diff --git a/test/database/sorted.js b/test/database/sorted.js index 9548b602bd..8b348961ee 100644 --- a/test/database/sorted.js +++ b/test/database/sorted.js @@ -334,7 +334,7 @@ describe('Sorted Set methods', () => { }); it('should work with big arrays (length > 100) ', async function () { - this.timeout(50000); + this.timeout(100000); const keys = []; for (let i = 0; i < 400; i++) { /* eslint-disable no-await-in-loop */ diff --git a/test/notifications.js b/test/notifications.js index 7ff53f3c60..96894e7606 100644 --- a/test/notifications.js +++ b/test/notifications.js @@ -359,7 +359,7 @@ describe('Notifications', () => { assert(nids.includes(nid)); done(); }); - }, 1500); + }, 3000); }); }); });