koa 2.13.4 → 2.14.1

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,16 @@
1
1
 
2
+ 2.14.1 / 2022-12-07
3
+ ==================
4
+
5
+ **fixes**
6
+ * [[`cb92bc9`](http://github.com/koajs/koa/commit/cb92bc98939bd71fd3c01e2dc681caf6545baa38)] - fix: should export createAsyncCtxStorageMiddleware function on application (#1724) (fengmk2 <<fengmk2@gmail.com>>)
7
+
8
+ 2.14.0 / 2022-12-06
9
+ ==================
10
+
11
+ **features**
12
+ * [[`a293122`](http://github.com/koajs/koa/commit/a29312212839c6f0418152d2a2cd3bf3fd3ee5e4)] - feat: support asyncLocalStorage (#1721) (fengmk2 <<fengmk2@gmail.com>>)
13
+
2
14
  2.13.4 / 2021-10-19
3
15
  ==================
4
16
 
@@ -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(this.createAsyncCtxStorageMiddleware());
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
  *
@@ -220,6 +234,15 @@ module.exports = class Application extends Emitter {
220
234
  static get default() {
221
235
  return Application;
222
236
  }
237
+
238
+ createAsyncCtxStorageMiddleware() {
239
+ const app = this;
240
+ return async function asyncCtxStorage(ctx, next) {
241
+ await app.ctxStorage.run(ctx, async() => {
242
+ return await next();
243
+ });
244
+ };
245
+ }
223
246
  };
224
247
 
225
248
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koa",
3
- "version": "2.13.4",
3
+ "version": "2.14.1",
4
4
  "description": "Koa web app framework",
5
5
  "main": "lib/application.js",
6
6
  "exports": {