koa 2.13.4 → 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 CHANGED
@@ -1,4 +1,10 @@
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
+
2
8
  2.13.4 / 2021-10-19
3
9
  ==================
4
10
 
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koa",
3
- "version": "2.13.4",
3
+ "version": "2.14.0",
4
4
  "description": "Koa web app framework",
5
5
  "main": "lib/application.js",
6
6
  "exports": {