egg 3.8.0 → 3.9.0-beta.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/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  /// <reference types="node" />
2
2
  import accepts = require('accepts');
3
- import KoaApplication = require('koa');
4
- import KoaRouter = require('koa-router');
5
3
  import { AsyncLocalStorage } from 'async_hooks';
6
4
  import { EventEmitter } from 'events'
7
5
  import { Readable } from 'stream';
8
6
  import { Socket } from 'net';
9
7
  import { IncomingMessage, ServerResponse } from 'http';
8
+ import KoaApplication = require('koa');
9
+ import KoaRouter = require('koa-router');
10
10
  import {
11
11
  EggLogger as Logger,
12
12
  EggLoggers,
@@ -15,7 +15,9 @@ import {
15
15
  EggLoggerOptions,
16
16
  EggContextLogger
17
17
  } from 'egg-logger';
18
- import { HttpClient, RequestOptions2 as RequestOptions } from 'urllib';
18
+ import { RequestOptions2 as RequestOptions, HttpClientResponse } from 'urllib';
19
+ import { RequestURL, RequestOptions as RequestOptionsNext } from 'urllib-next/src/Request';
20
+ import { HttpClientResponse as HttpClientResponseNext } from 'urllib-next/src/Response';
19
21
  import {
20
22
  EggCoreBase,
21
23
  FileLoaderOption,
@@ -46,12 +48,18 @@ declare module 'egg' {
46
48
  // Remove specific property from the specific class
47
49
  type RemoveSpecProp<T, P> = Pick<T, Exclude<keyof T, P>>;
48
50
 
49
- export interface EggHttpClient extends HttpClient<RequestOptions> { }
51
+ // Compatible with both urllib@2 and urllib@3 RequestOptions to request
52
+ export interface EggHttpClient extends EventEmitter {
53
+ reques<T = any>(url: RequestURL): Promise<HttpClientResponse<T> | HttpClientResponseNext>;
54
+ request<T = any>(url: RequestURL, options: RequestOptions | RequestOptionsNext): Promise<HttpClientResponse<T> | HttpClientResponseNext>;
55
+ curl<T = any>(url: RequestURL): Promise<HttpClientResponse<T> | HttpClientResponseNext>;
56
+ curl<T = any>(url: RequestURL, options: RequestOptions | RequestOptionsNext): Promise<HttpClientResponse<T> | HttpClientResponseNext>;
57
+ }
50
58
  interface EggHttpConstructor {
51
59
  new(app: Application): EggHttpClient;
52
60
  }
53
61
 
54
- export interface EggContextHttpClient extends HttpClient<RequestOptions> { }
62
+ export interface EggContextHttpClient extends EggHttpClient { }
55
63
  interface EggContextHttpClientConstructor {
56
64
  new(ctx: Context): EggContextHttpClient;
57
65
  }
@@ -695,6 +703,13 @@ declare module 'egg' {
695
703
  */
696
704
  runInBackground(scope: (ctx: Context) => void): void;
697
705
 
706
+ /**
707
+ * Run async function in the anonymous context scope
708
+ * @see Context#runInAnonymousContextScope
709
+ * @param {Function} scope - the first args is an anonymous ctx, scope should be async function
710
+ */
711
+ runInAnonymousContextScope(scope: (ctx: Context) => Promise<void>): Promise<void>;
712
+
698
713
  /**
699
714
  * Get current execute ctx async local storage
700
715
  * @returns {AsyncLocalStorage} localStorage - store current execute Context
@@ -251,7 +251,22 @@ class Application extends EggApplication {
251
251
  runInBackground(scope) {
252
252
  const ctx = this.createAnonymousContext();
253
253
  if (!scope.name) scope._name = eggUtils.getCalleeFromStack(true);
254
- ctx.runInBackground(scope);
254
+ this.ctxStorage.run(ctx, () => {
255
+ ctx.runInBackground(scope);
256
+ });
257
+ }
258
+
259
+ /**
260
+ * Run async function in the anonymous context scope
261
+ * @see Context#runInAnonymousContextScope
262
+ * @param {Function} scope - the first args is an anonymous ctx, scope should be async function
263
+ */
264
+ async runInAnonymousContextScope(scope) {
265
+ const ctx = this.createAnonymousContext();
266
+ if (!scope.name) scope._name = eggUtils.getCalleeFromStack(true);
267
+ await this.ctxStorage.run(ctx, async () => {
268
+ await scope(ctx);
269
+ });
255
270
  }
256
271
 
257
272
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egg",
3
- "version": "3.8.0",
3
+ "version": "3.9.0-beta.1",
4
4
  "publishConfig": {
5
5
  "tag": "next"
6
6
  },
@@ -54,7 +54,7 @@
54
54
  "on-finished": "^2.4.1",
55
55
  "sendmessage": "^1.1.0",
56
56
  "urllib": "^2.33.0",
57
- "urllib-next": "^3.1.3",
57
+ "urllib-next": "^3.8.0",
58
58
  "utility": "^1.17.0",
59
59
  "ylru": "^1.3.2"
60
60
  },