Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/koajs/middleware-hook
low-level hooks for your middleware
https://github.com/koajs/middleware-hook
Last synced: about 1 month ago
JSON representation
low-level hooks for your middleware
- Host: GitHub
- URL: https://github.com/koajs/middleware-hook
- Owner: koajs
- Created: 2015-10-15T05:23:49.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-01-01T07:07:41.000Z (almost 9 years ago)
- Last Synced: 2024-04-14T13:08:40.623Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 5
- Watchers: 9
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
- awesome-koa - middleware-hook - low-level hooks for your middleware (Middleware)
README
# koa-middleware-hook
low-level hooks for your middleware. this is mostly an experiment,
but it lets you hook into the following spots:1. Downstream: Beginning of the middleware
2. Downstream: Before yielding to the next middleware in the stack
3. Upstream: Beginning on the way back up the stack
4. Upstream: When the middleware finishsuseful for profiling your middleware, logging your middleware, timing your middleware, etc.
probably not suitable for production, though I haven't profiled it or anything.
## Install
```
npm install koa-middleware-hook
```## Usage
##### `hook = Hook(function: marker, function: reduce)`
`marker` is a function called during each point in time where you'd return what you want to track. Here's an example:
```js
function marker () {
return new Date()
}
````reduce` takes all 4 hooks and allows you to merge them together in some meaningful way. Something to keep in mind is that the innermost middleware will only execute 2 hooks:
```js
function reduce (middleware_name, downstream_start, downstream_end | upstream_end, upstream_start | null, upstream_end | null) {
if (arguments.length === 3) {
debug('%s: %sms', middleware_name, downstream_end - downstream_start)
} else {
debug('%s: %sms', middleware_name, (downstream_end - downstream_start) + (upstream_end - upstream_start))
}
}
```Here's how you use hook:
```
app.use(hook(bodyparser()))
```## TODO
- solicit ideas for a better API
- some more usage examples## License
MIT