https://github.com/snowyu/zone-koa.js
Persisting the koa session across async task execution via zone.js
https://github.com/snowyu/zone-koa.js
koa session zone
Last synced: 4 months ago
JSON representation
Persisting the koa session across async task execution via zone.js
- Host: GitHub
- URL: https://github.com/snowyu/zone-koa.js
- Owner: snowyu
- License: mit
- Created: 2017-03-27T06:01:09.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-14T07:42:52.000Z (over 7 years ago)
- Last Synced: 2025-01-31T09:17:48.262Z (5 months ago)
- Topics: koa, session, zone
- Language: CoffeeScript
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-MIT
Awesome Lists containing this project
README
# zone-koa
Each request as a zone session patch to Koa.
### Installation
* Nodejs v7.6.0 above
* zone.js v0.8.5 above
* koa v2.4.1 above
* (optional) coffeescript v2.0install npm package:
```bash
npm install zone.js koa zone-koa coffeescript
```### Usage
```coffee
require('zone.js/dist/zone-node.js')
require('zone.js/dist/long-stack-trace-zone')
require('zone-koa')Koa = require('koa')
app = new Koa()
expect = chai.expectapp.use (ctx, next)->
req = ctx.req
vSessionName = req.socket.remoteFamily + '|' + req.socket.remoteAddress + '|'+ req.socket.remotePort
expect(Zone.current.name).to.be.equal vSessionName
vSession = Zone.current.get 'session'
expect(vSession).to.be.equal ctx
start = new Date()
await next()
ms = new Date() - start
console.log("#{ctx.method} #{ctx.url} - #{ms}ms")app.use (ctx)->
ctx.body = ctx.request.url
```## History
## refs
* issue using async/await with [target ES2017](https://github.com/angular/zone.js/issues/740). with target ES2016 works good.
* [async_hooks](https://github.com/nodejs/node/blob/master/doc/api/async_hooks.md) API on above [email protected]
* [cls-hooked](https://github.com/jeff-lewis/cls-hooked) Continuation-Local Storage ( Hooked )
* [node-async-context](https://github.com/gms1/node-async-context)(based on typescript) a continuation local storage / asynchronous execution context for node.js via async_hooks