Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shimohq/koa-yield-breakpoint
Add breakpoints around `yield` expression especially for koa@1.
https://github.com/shimohq/koa-yield-breakpoint
koa latency nodejs performance profiling
Last synced: 3 months ago
JSON representation
Add breakpoints around `yield` expression especially for koa@1.
- Host: GitHub
- URL: https://github.com/shimohq/koa-yield-breakpoint
- Owner: shimohq
- Created: 2016-10-24T06:34:53.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-24T07:28:54.000Z (about 7 years ago)
- Last Synced: 2024-10-03T03:15:32.620Z (4 months ago)
- Topics: koa, latency, nodejs, performance, profiling
- Language: JavaScript
- Size: 34.2 KB
- Stars: 17
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
Awesome Lists containing this project
README
## koa-yield-breakpoint
Add breakpoints around `yield` expression especially for koa@1.
### Install
```sh
$ npm i koa-yield-breakpoint --save
```### Example
```sh
$ DEBUG=koa-yield-breakpoint node example/app
$ curl -XPOST localhost:3000/users
```### Usage
```js
// 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:
```js
global.logger(
this,
function*(){
return yield YieldExpression
},
YieldExpressionString,
filename
)
```log like:
```json
{
"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](https://github.com/nswbmw/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**
```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:
```js
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. (/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](https://github.com/isaacs/node-glob), required.
- exclude_files{String[]}: exclude files pattern, default `[]`.
- store{Object}: backend store instance, see [koa-yield-breakpoint-mongodb](https://github.com/nswbmw/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](https://github.com/isaacs/node-glob#options).