feat: add custom asyncLocalStorage support
This commit is contained in:
@@ -4,17 +4,10 @@ const { describe, it } = require('node:test')
|
||||
const request = require('supertest')
|
||||
const assert = require('node:assert/strict')
|
||||
const Koa = require('../..')
|
||||
const { AsyncLocalStorage } = require('async_hooks')
|
||||
|
||||
describe('app.currentContext', () => {
|
||||
it('should throw error if AsyncLocalStorage not support', () => {
|
||||
if (require('async_hooks').AsyncLocalStorage) return
|
||||
assert.throws(() => new Koa({ asyncLocalStorage: true }),
|
||||
/Requires node 12\.17\.0 or higher to enable asyncLocalStorage/)
|
||||
})
|
||||
|
||||
it('should get currentContext return context when asyncLocalStorage enable', async () => {
|
||||
if (!require('async_hooks').AsyncLocalStorage) return
|
||||
|
||||
const app = new Koa({ asyncLocalStorage: true })
|
||||
|
||||
app.use(async ctx => {
|
||||
@@ -107,4 +100,17 @@ describe('app.currentContext', () => {
|
||||
await request(app.callback()).get('/').expect('Internal Server Error')
|
||||
await handleError
|
||||
})
|
||||
|
||||
it('should support a custom asyncLocalStorage', async () => {
|
||||
const asyncLocalStorage = new AsyncLocalStorage()
|
||||
const app = new Koa({ asyncLocalStorage })
|
||||
assert(app.currentContext === undefined)
|
||||
app.use(async ctx => {
|
||||
assert(ctx === app.currentContext)
|
||||
assert(asyncLocalStorage.getStore() === ctx)
|
||||
ctx.body = 'ok'
|
||||
})
|
||||
await request(app.callback()).get('/').expect('ok')
|
||||
assert(app.currentContext === undefined)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -93,7 +93,7 @@ app.listen(3000);
|
||||
- `app.subdomainOffset` offset of `.subdomains` to ignore, default to 2
|
||||
- `app.proxyIpHeader` proxy ip header, default to `X-Forwarded-For`
|
||||
- `app.maxIpsCount` max ips read from proxy ip header, default to 0 (means infinity)
|
||||
- `app.asyncLocalStorage`
|
||||
- `app.asyncLocalStorage<Boolean|asyncLocalStorage>` - pass `true` or an instance of `AsyncLocalStorage` to enable async local storage. See below for details.
|
||||
|
||||
You can pass the settings to the constructor:
|
||||
```js
|
||||
@@ -214,6 +214,41 @@ Note:
|
||||
- Many properties on `ctx` are defined using getters, setters, and `Object.defineProperty()`. You can only edit these properties (not recommended) by using `Object.defineProperty()` on `app.context`. See https://github.com/koajs/koa/issues/652.
|
||||
- Mounted apps currently use their parent's `ctx` and settings. Thus, mounted apps are really just groups of middleware.
|
||||
|
||||
## app.currentContext
|
||||
|
||||
> New in Koa v3.
|
||||
|
||||
If `asyncLocalStorage` is enabled, this will return the current context for the request. For example:
|
||||
|
||||
```js
|
||||
const app = new Koa({ asyncLocalStorage: true })
|
||||
|
||||
app.use(async (ctx, next) => {
|
||||
callSomeFunction()
|
||||
})
|
||||
|
||||
function callSomeFunction () {
|
||||
app.currentContext = {} /* ctx of the middleware above */
|
||||
}
|
||||
```
|
||||
|
||||
Starting in v3.1.0, you can also pass your own AsyncLocalStorage instance:
|
||||
|
||||
```js
|
||||
const asyncLocalStorage = new AsyncLocalStorage()
|
||||
const app = new Koa({ asyncLocalStorage })
|
||||
|
||||
app.use(async (ctx, next) => {
|
||||
callSomeFunction()
|
||||
})
|
||||
|
||||
function callSomeFunction () {
|
||||
const ctx = asyncLocalStorage.getStore()
|
||||
}
|
||||
```
|
||||
|
||||
Use this to pass key request details like request ID or the user to your internal services.
|
||||
|
||||
## Error Handling
|
||||
|
||||
By default outputs all errors to stderr unless `app.silent` is `true`.
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
*/
|
||||
|
||||
const debug = require('debug')('koa:application')
|
||||
const assert = require('assert')
|
||||
const onFinished = require('on-finished')
|
||||
const response = require('./response')
|
||||
const compose = require('koa-compose')
|
||||
@@ -19,6 +18,7 @@ const http = require('http')
|
||||
const isStream = require('./is-stream.js')
|
||||
const only = require('./only.js')
|
||||
const { HttpError } = require('http-errors')
|
||||
const { AsyncLocalStorage } = require('async_hooks')
|
||||
|
||||
/** @typedef {typeof import ('./context') & {
|
||||
* app: Application
|
||||
@@ -80,9 +80,11 @@ module.exports = class Application extends Emitter {
|
||||
this[util.inspect.custom] = this.inspect
|
||||
}
|
||||
if (options.asyncLocalStorage) {
|
||||
const { AsyncLocalStorage } = require('async_hooks')
|
||||
assert(AsyncLocalStorage, 'Requires node 12.17.0 or higher to enable asyncLocalStorage')
|
||||
this.ctxStorage = new AsyncLocalStorage()
|
||||
if (options.asyncLocalStorage instanceof AsyncLocalStorage) {
|
||||
this.ctxStorage = options.asyncLocalStorage
|
||||
} else {
|
||||
this.ctxStorage = new AsyncLocalStorage()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user