Skip to content

Add breakpoints around `yield` expression especially for koa@1.

Notifications You must be signed in to change notification settings

shimohq/koa-yield-breakpoint

Repository files navigation

koa-yield-breakpoint

Add breakpoints around yield expression especially for koa@1.

Install

$ npm i koa-yield-breakpoint --save

Example

$ DEBUG=koa-yield-breakpoint node example/app
$ curl -XPOST localhost:3000/users

Usage

// Generally, on top of the main file
const koaYieldBreakpoint = require('koa-yield-breakpoint')({
  name: 'api',
  files: ['./routes/*.js'],
  // store: new require('koa-yield-breakpoint-mongodb')({
  //   url: 'mongodb://localhost:27017/test',
  //   coll: 'koa-yield-breakpoint-loggers'
  // })
})

const koa = require('koa')
const routes = require('./routes')
const app = koa()

// Generally, above other middlewares
app.use(koaYieldBreakpoint)

routes(app)

app.listen(3000, () => {
  console.log('listening on 3000')
})

NB: You'd better put require('koa-yield-breakpoint') on the top of the main file, because koa-yield-breakpoint rewrite Module.prototype._compile.

koa-yield-breakpoint will wrap YieldExpression with:

global.logger(
  this,
  function*(){
    return yield YieldExpression
  },
  YieldExpressionString,
  filename
)

log like:

{
  "name": "api",
  "requestId": "222f66ec-7259-4d20-930f-2ac035c16e7b",
  "timestamp": "2018-01-15T05:02:18.827Z",
  "this": {
    "state": {},
    "params": {},
    "request": {
      "method": "POST",
      "path": "/users",
      "header": {
        "host": "localhost:3000",
        "user-agent": "curl/7.54.0",
        "accept": "*/*"
      },
      "query": {}
    },
    "response": {
      "status": 404
    }
  },
  "type": "start",
  "step": 1,
  "take": 0
}

koa-yield-breakpoint will print logs to console by default, if you want to save these logs to db, set store option, eg: koa-yield-breakpoint-mongodb.

NB: type in ['start', 'beforeYield', 'afterYield', 'error', 'end'], take is ms.

SourceMap

After v1.1.0, koa-yield-breakpoint support source map:

example/routes/users.js

const Mongolass = require('mongolass')
const mongolass = new Mongolass()
mongolass.connect('mongodb://localhost:27017/test')

exports.getUsers = function* getUsers() {
  yield mongolass.model('users').create({
    name: 'xx',
    age: 18
  })

  const users = yield mongolass.model('users').find()


  console.log(haha)
  this.body = users
}

Will output:

ReferenceError: haha is not defined
    at Object.getUsers (/Users/nswbmw/node/koa-yield-breakpoint/example/routes/users.js:16:15)
    at next (native)
    at Object.<anonymous> (/Users/nswbmw/node/koa-yield-breakpoint/node_modules/koa-route/index.js:34:19)
    at next (native)
    at onFulfilled (/Users/nswbmw/node/koa-yield-breakpoint/node_modules/koa/node_modules/co/index.js:65:19)

Options

require('koa-yield-breakpoint')(option)

  • name{String}: service name added to log.
  • sourcemap{Boolean}: whether open sourcemap, default: true, will increase memory usage.
  • files{String[]}: files pattern, see glob, required.
  • exclude_files{String[]}: exclude files pattern, default [].
  • store{Object}: backend store instance, see koa-yield-breakpoint-mongodb, default print to console.
  • filter{Object}: reserved field in koa's this, default:
{
  ctx: ['state', 'params'],
  request: ['method', 'path', 'header', 'query', 'body'],
  response: ['status', 'body']
}
  • loggerName{String}: global logger name, default logger.
  • requestIdPath{String}: requestId path in this, default requestId.
  • yieldCondition{Function}: parameters (filename, yieldExpression, parsedYieldExpression), return a object:
    • wrapYield{Boolean}: if true return wraped yieldExpression, default true.
    • deep{Boolean}: if true deep wrap yieldExpression, default true.
  • others: see glob.

About

Add breakpoints around `yield` expression especially for koa@1.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published