Skip to content

Track the current ctx via egg's app, especially in plugin's callbacks.

License

Notifications You must be signed in to change notification settings

Claude-Ray/egg-current-ctx

Repository files navigation

egg-current-ctx

Build Status codecov

Track the current ctx via egg's app, especially in plugin's callbacks.

Based on async_hooks.

Install

$ npm i egg-current-ctx --save

Usage

// {app_root}/config/plugin.js
exports.currentCtx = {
  enable: true,
  package: 'egg-current-ctx',
};

Example

For example, if you want to use dubbo2.js in egg.

The following code was written according to the starting and middleware guides of dubbo2.js.

// {plugin_root} ./app.js
module.exports = app => {
  const dubbo = Dubbo.from({....});
  app.beforeStart(async () => {
    dubbo.use(async (ctx, next) => {
      const startTime = Date.now();
      await next();
      const endTime = Date.now();
      console.log('costtime: %d', endTime - startTime);
    });
    await dubbo.ready();
    console.log('dubbo was ready...');
  })
}

The above ctx which belongs to dubbo2.js isn't equal to ctx created by egg.

You could use app.currentCtx to operate the ctx of egg.

// {plugin_root} ./app.js
module.exports = app => {
  const dubbo = Dubbo.from({....});
  app.beforeStart(async () => {
    dubbo.use(async (ctx, next) => {
      const startTime = Date.now();
      // get ctx from current async context
      const eggCtx = app.currentCtx;
      console.log('', eggCtx.query);
      await next();
      const endTime = Date.now();
      console.log('costtime: %d', endTime - startTime);
    });
    await dubbo.ready();
    console.log('dubbo was ready...');
  })
}

Here I omitted a part of calling process of dubbo2.js. For more details, you can follow its own documentation.

It can be very useful in rare conditions, such as mounting the properties of dubbo2.js ctx to egg ctx.

License

MIT

About

Track the current ctx via egg's app, especially in plugin's callbacks.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published