koa 2.13.3 → 2.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/History.md +12 -0
- package/lib/application.js +22 -0
- package/lib/response.js +3 -1
- package/package.json +1 -1
package/History.md
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
|
|
2
|
+
2.14.0 / 2022-12-06
|
|
3
|
+
==================
|
|
4
|
+
|
|
5
|
+
**features**
|
|
6
|
+
* [[`a293122`](http://github.com/koajs/koa/commit/a29312212839c6f0418152d2a2cd3bf3fd3ee5e4)] - feat: support asyncLocalStorage (#1721) (fengmk2 <<fengmk2@gmail.com>>)
|
|
7
|
+
|
|
8
|
+
2.13.4 / 2021-10-19
|
|
9
|
+
==================
|
|
10
|
+
|
|
11
|
+
**fixes**
|
|
12
|
+
* [[`dbc9c5a`](http://github.com/koajs/koa/commit/dbc9c5a47e2b2799cab403186fdb010df5df6f67)] - fix: Do not response Content-Length if Transfer-Encoding is defined (#1602) (Yiyu He <<dead_horse@qq.com>>)
|
|
13
|
+
|
|
2
14
|
2.13.3 / 2021-09-24
|
|
3
15
|
==================
|
|
4
16
|
|
package/lib/application.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
const isGeneratorFunction = require('is-generator-function');
|
|
9
9
|
const debug = require('debug')('koa:application');
|
|
10
10
|
const onFinished = require('on-finished');
|
|
11
|
+
const assert = require('assert');
|
|
11
12
|
const response = require('./response');
|
|
12
13
|
const compose = require('koa-compose');
|
|
13
14
|
const context = require('./context');
|
|
@@ -64,6 +65,12 @@ module.exports = class Application extends Emitter {
|
|
|
64
65
|
if (util.inspect.custom) {
|
|
65
66
|
this[util.inspect.custom] = this.inspect;
|
|
66
67
|
}
|
|
68
|
+
if (options.asyncLocalStorage) {
|
|
69
|
+
const { AsyncLocalStorage } = require('async_hooks');
|
|
70
|
+
assert(AsyncLocalStorage, 'Requires node 12.17.0 or higher to enable asyncLocalStorage');
|
|
71
|
+
this.ctxStorage = new AsyncLocalStorage();
|
|
72
|
+
this.use(createAsyncCtxStorage(this));
|
|
73
|
+
}
|
|
67
74
|
}
|
|
68
75
|
|
|
69
76
|
/**
|
|
@@ -153,6 +160,13 @@ module.exports = class Application extends Emitter {
|
|
|
153
160
|
return handleRequest;
|
|
154
161
|
}
|
|
155
162
|
|
|
163
|
+
/**
|
|
164
|
+
* return currnect contenxt from async local storage
|
|
165
|
+
*/
|
|
166
|
+
get currentContext() {
|
|
167
|
+
if (this.ctxStorage) return this.ctxStorage.getStore();
|
|
168
|
+
}
|
|
169
|
+
|
|
156
170
|
/**
|
|
157
171
|
* Handle request in callback.
|
|
158
172
|
*
|
|
@@ -222,6 +236,14 @@ module.exports = class Application extends Emitter {
|
|
|
222
236
|
}
|
|
223
237
|
};
|
|
224
238
|
|
|
239
|
+
function createAsyncCtxStorage(app) {
|
|
240
|
+
return async function asyncCtxStorage(ctx, next) {
|
|
241
|
+
await app.ctxStorage.run(ctx, async() => {
|
|
242
|
+
return await next();
|
|
243
|
+
});
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
225
247
|
/**
|
|
226
248
|
* Response helper.
|
|
227
249
|
*/
|
package/lib/response.js
CHANGED