koa1-wrapmiddleware

1.0.2 • Public • Published

Record the time spent on each middleware for koa1

Use this data for performance optimization

Here is an example:

const Koa = require("koa");
const wrappedKoaMiddleware = require("koa1-wrapmiddleware");
const app = new Koa();

wrappedKoaMiddleware(app);

app.use(function*(next) {
  yield next;
  console.log(this.middlewareTakeTime);
  /**
   * console
  [ { num: 3, funcName: '', takeTime: 2 },
  { num: 2, funcName: 'loggerFunc', takeTime: 4 },
  { num: 1, funcName: 'xResponseTimeFunc', takeTime: 4 } ]
  **/
});
function* xResponseTimeFunc(next) {
  const start = Date.now();
  yield next;
  const ms = Date.now() - start;
  this.set("X-Response-Time", `${ms}ms`);
}
app.use(xResponseTimeFunc);

// logger
function* loggerFunc(next) {
  const start = Date.now();
  yield next;
  const ms = Date.now() - start;
  console.log(`${this.method} ${this.url} - ${ms}`);
}
app.use(loggerFunc);

// response
app.use(function*() {
  this.body = "Hello World";
});

app.listen(3000);

Implement:

function wrappedKoaMiddleware(app) {
  const appUse = app.use;
  app.use = function(fn) {
    if (isGeneratorFunction(fn)) {
      fn = convert(fn);
    }
    const middlewareLength = this.middleware.length;
    const wrapFun = async function(ctx, next) {
      const t1 = process.uptime() * 1000;
      await fn(ctx, next);
      ctx.middlewareTakeTime = ctx.middlewareTakeTime || [];
      ctx.middlewareTakeTime.push({
        num: middlewareLength,
        funcName: fn.name,
        takeTime: process.uptime() * 1000 - t1
      });
    };
    return appUse.call(this, wrapFun);
  };
}

Package Sidebar

Install

npm i koa1-wrapmiddleware

Weekly Downloads

4

Version

1.0.2

License

ISC

Unpacked Size

3.75 kB

Total Files

4

Last publish

Collaborators

  • zhoulinxiang