egg 4.0.0-beta.6 → 4.0.0-beta.8

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.
Files changed (49) hide show
  1. package/dist/commonjs/app/extend/context.d.ts +6 -3
  2. package/dist/commonjs/app/extend/context.js +2 -1
  3. package/dist/commonjs/app/extend/context.types.d.ts +16 -0
  4. package/dist/commonjs/app/extend/context.types.js +3 -0
  5. package/dist/commonjs/app/middleware/site_file.d.ts +3 -1
  6. package/dist/commonjs/app/middleware/site_file.js +2 -2
  7. package/dist/commonjs/index.d.ts +2 -0
  8. package/dist/commonjs/index.js +7 -2
  9. package/dist/commonjs/lib/core/messenger/IMessenger.d.ts +2 -2
  10. package/dist/commonjs/lib/core/messenger/index.js +2 -2
  11. package/dist/commonjs/lib/core/messenger/ipc.d.ts +5 -3
  12. package/dist/commonjs/lib/core/messenger/ipc.js +23 -16
  13. package/dist/commonjs/lib/core/messenger/local.d.ts +2 -2
  14. package/dist/commonjs/lib/core/messenger/local.js +14 -11
  15. package/dist/commonjs/lib/egg.d.ts +1 -0
  16. package/dist/commonjs/lib/egg.js +2 -1
  17. package/dist/commonjs/lib/egg.types.d.ts +6 -0
  18. package/dist/commonjs/lib/egg.types.js +3 -0
  19. package/dist/esm/app/extend/context.d.ts +6 -3
  20. package/dist/esm/app/extend/context.js +2 -1
  21. package/dist/esm/app/extend/context.types.d.ts +16 -0
  22. package/dist/esm/app/extend/context.types.js +2 -0
  23. package/dist/esm/app/middleware/site_file.d.ts +3 -1
  24. package/dist/esm/app/middleware/site_file.js +2 -2
  25. package/dist/esm/index.d.ts +2 -0
  26. package/dist/esm/index.js +4 -1
  27. package/dist/esm/lib/core/messenger/IMessenger.d.ts +2 -2
  28. package/dist/esm/lib/core/messenger/index.js +2 -2
  29. package/dist/esm/lib/core/messenger/ipc.d.ts +5 -3
  30. package/dist/esm/lib/core/messenger/ipc.js +23 -16
  31. package/dist/esm/lib/core/messenger/local.d.ts +2 -2
  32. package/dist/esm/lib/core/messenger/local.js +14 -11
  33. package/dist/esm/lib/egg.d.ts +1 -0
  34. package/dist/esm/lib/egg.js +2 -1
  35. package/dist/esm/lib/egg.types.d.ts +6 -0
  36. package/dist/esm/lib/egg.types.js +2 -0
  37. package/dist/package.json +1 -1
  38. package/package.json +20 -11
  39. package/src/app/extend/context.ts +10 -4
  40. package/src/app/extend/context.types.ts +24 -0
  41. package/src/app/middleware/site_file.ts +1 -1
  42. package/src/index.ts +4 -0
  43. package/src/lib/core/messenger/IMessenger.ts +2 -2
  44. package/src/lib/core/messenger/index.ts +1 -1
  45. package/src/lib/core/messenger/ipc.ts +23 -16
  46. package/src/lib/core/messenger/local.ts +12 -10
  47. package/src/lib/egg.ts +2 -0
  48. package/src/lib/egg.types.ts +6 -0
  49. package/src/lib/type.ts +3 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egg",
3
- "version": "4.0.0-beta.6",
3
+ "version": "4.0.0-beta.8",
4
4
  "engines": {
5
5
  "node": ">= 18.19.0"
6
6
  },
@@ -21,10 +21,10 @@
21
21
  "dependencies": {
22
22
  "@eggjs/cluster": "beta",
23
23
  "@eggjs/cookies": "^3.0.0",
24
- "@eggjs/core": "^6.2.3",
25
- "@eggjs/schedule": "^5.0.1",
26
- "@eggjs/utils": "^4.0.2",
27
- "@eggjs/watcher": "^4.0.0",
24
+ "@eggjs/core": "beta",
25
+ "@eggjs/schedule": "^5.0.2",
26
+ "@eggjs/utils": "^4.1.5",
27
+ "@eggjs/watcher": "^4.0.1",
28
28
  "circular-json-for-egg": "^1.0.0",
29
29
  "cluster-client": "^3.7.0",
30
30
  "delegates": "^1.0.0",
@@ -55,7 +55,9 @@
55
55
  },
56
56
  "devDependencies": {
57
57
  "@arethetypeswrong/cli": "^0.17.1",
58
+ "@eggjs/bin": "^7.0.0",
58
59
  "@eggjs/koa": "^2.19.1",
60
+ "@eggjs/mock": "beta",
59
61
  "@eggjs/tsconfig": "1",
60
62
  "@types/delegates": "^1.0.3",
61
63
  "@types/koa-bodyparser": "^4.3.12",
@@ -67,8 +69,6 @@
67
69
  "assert-file": "1",
68
70
  "coffee": "5",
69
71
  "cross-env": "7",
70
- "egg-bin": "beta",
71
- "egg-mock": "beta",
72
72
  "egg-plugin-puml": "^2.4.0",
73
73
  "egg-tracer": "^2.1.0",
74
74
  "egg-view-nunjucks": "^2.3.0",
@@ -76,8 +76,10 @@
76
76
  "eslint-config-egg": "14",
77
77
  "formstream": "^1.5.1",
78
78
  "koa-static": "^5.0.0",
79
+ "mm": "^3.4.0",
79
80
  "pedding": "^1.1.0",
80
81
  "prettier": "^2.7.1",
82
+ "rimraf": "6",
81
83
  "runscript": "^2.0.1",
82
84
  "sdk-base": "^4.2.1",
83
85
  "spy": "^1.0.0",
@@ -87,13 +89,16 @@
87
89
  "typescript": "5"
88
90
  },
89
91
  "scripts": {
92
+ "clean": "rimraf dist",
90
93
  "lint": "eslint src test --ext .ts",
91
- "pretest": "npm run lint -- --fix && npm run prepublishOnly",
94
+ "pretest": "npm run clean && npm run lint -- --fix",
92
95
  "test": "egg-bin test",
96
+ "test-local": "egg-bin test",
93
97
  "test:changed": "egg-bin test --changed",
94
- "preci": "npm run lint && npm run prepublishOnly && attw --pack",
98
+ "preci": "npm run clean && npm run lint",
95
99
  "ci": "egg-bin cov",
96
- "prepublishOnly": "tshy && tshy-after",
100
+ "postci": "npm run prepublishOnly && npm run clean",
101
+ "prepublishOnly": "tshy && tshy-after && attw --pack",
97
102
  "site:dev": "cross-env NODE_OPTIONS=--openssl-legacy-provider APP_ROOT=./site dumi dev",
98
103
  "site:devWithNode14-16": "cross-env APP_ROOT=./site dumi dev",
99
104
  "site:build": "cross-env NODE_OPTIONS=--openssl-legacy-provider APP_ROOT=./site dumi build",
@@ -107,11 +112,15 @@
107
112
  "url": "git://github.com/eggjs/egg.git"
108
113
  },
109
114
  "license": "MIT",
115
+ "tnpm": {
116
+ "mode": "npm"
117
+ },
110
118
  "egg": {
111
119
  "framework": true,
112
120
  "exports": {
113
121
  "import": "./dist/esm",
114
- "require": "./dist/commonjs"
122
+ "require": "./dist/commonjs",
123
+ "typescript": "./src"
115
124
  }
116
125
  },
117
126
  "files": [
@@ -6,12 +6,18 @@ import {
6
6
  type ContextDelegation as EggCoreContextDelegation,
7
7
  } from '@eggjs/core';
8
8
  import type { Cookies as ContextCookies } from '@eggjs/cookies';
9
+ import { EggLogger } from 'egg-logger';
9
10
  import type { Application } from '../../lib/application.js';
11
+ import type {
12
+ HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
13
+ } from '../../lib/core/httpclient.js';
10
14
  import type { ContextHttpClient } from '../../lib/core/context_httpclient.js';
11
15
  import type { BaseContextClass } from '../../lib//core/base_context_class.js';
12
16
  import Request from './request.js';
13
17
  import Response from './response.js';
14
- import { EggLogger } from 'egg-logger';
18
+ import type Helper from './helper.js';
19
+
20
+ import './context.types.js';
15
21
 
16
22
  const HELPER = Symbol('ctx helper');
17
23
  const LOCALS = Symbol('ctx locals');
@@ -79,7 +85,7 @@ export default class Context extends EggCoreContext {
79
85
  * @param {Object} [options] - options for request.
80
86
  * @return {Object} see {@link ContextHttpClient#curl}
81
87
  */
82
- async curl(url: string, options?: object): ReturnType<ContextHttpClient['curl']> {
88
+ async curl(url: HttpClientRequestURL, options?: HttpClientRequestOptions): ReturnType<HttpClient['request']> {
83
89
  return await this.httpclient.curl(url, options);
84
90
  }
85
91
 
@@ -114,11 +120,11 @@ export default class Context extends EggCoreContext {
114
120
  * @member {Helper} Context#helper
115
121
  * @since 1.0.0
116
122
  */
117
- get helper() {
123
+ get helper(): Helper {
118
124
  if (!this[HELPER]) {
119
125
  this[HELPER] = new this.app.Helper(this as any);
120
126
  }
121
- return this[HELPER];
127
+ return this[HELPER] as Helper;
122
128
  }
123
129
 
124
130
  /**
@@ -0,0 +1,24 @@
1
+ import type {
2
+ Router,
3
+ } from '@eggjs/core';
4
+ import type {
5
+ HttpClientRequestURL, HttpClientRequestOptions, HttpClient,
6
+ } from '../../lib/core/httpclient.js';
7
+ import type {
8
+ ContextHttpClient,
9
+ } from '../../lib/core/context_httpclient.js';
10
+ import type Helper from './helper.js';
11
+ import type { EggLogger } from 'egg-logger';
12
+
13
+ declare module '@eggjs/core' {
14
+ // add Context overrides types
15
+ interface Context {
16
+ curl(url: HttpClientRequestURL, options?: HttpClientRequestOptions): ReturnType<HttpClient['request']>;
17
+ get router(): Router;
18
+ set router(val: Router);
19
+ get helper(): Helper;
20
+ get httpclient(): ContextHttpClient;
21
+ get httpClient(): ContextHttpClient;
22
+ getLogger(name: string): EggLogger;
23
+ }
24
+ }
@@ -13,7 +13,7 @@ export interface SiteFileMiddlewareOptions {
13
13
 
14
14
  const BUFFER_CACHE = Symbol('siteFile URL buffer cache');
15
15
 
16
- module.exports = (options: SiteFileMiddlewareOptions) => {
16
+ export default (options: SiteFileMiddlewareOptions) => {
17
17
  return async function siteFile(ctx: ContextDelegation, next: Next) {
18
18
  if (ctx.method !== 'HEAD' && ctx.method !== 'GET') {
19
19
  return next();
package/src/index.ts CHANGED
@@ -4,6 +4,10 @@
4
4
 
5
5
  import { BaseContextClass } from './lib/core/base_context_class.js';
6
6
  import { startEgg } from './lib/start.js';
7
+ import Helper from './app/extend/helper.js';
8
+
9
+ // export extends
10
+ export { Helper };
7
11
 
8
12
  // export types
9
13
  export * from './lib/egg.js';
@@ -11,12 +11,12 @@ export interface IMessenger extends EventEmitter {
11
11
 
12
12
  /**
13
13
  * send message to the specified process
14
- * @param {String} pid - the process id of the receiver
14
+ * @param {String} workerId - the workerId of the receiver
15
15
  * @param {String} action - message key
16
16
  * @param {Object} data - message value
17
17
  * @return {Messenger} this
18
18
  */
19
- sendTo(pid: string, action: string, data?: unknown): IMessenger;
19
+ sendTo(workerId: string, action: string, data?: unknown): IMessenger;
20
20
 
21
21
  /**
22
22
  * send message to one app worker by random
@@ -11,5 +11,5 @@ export type { IMessenger } from './IMessenger.js';
11
11
  export function create(egg: EggApplicationCore): IMessenger {
12
12
  return egg.options.mode === 'single'
13
13
  ? new LocalMessenger(egg)
14
- : new IPCMessenger();
14
+ : new IPCMessenger(egg);
15
15
  }
@@ -3,24 +3,28 @@ import { debuglog } from 'node:util';
3
3
  import workerThreads from 'node:worker_threads';
4
4
  import { sendmessage } from 'sendmessage';
5
5
  import type { IMessenger } from './IMessenger.js';
6
+ import type { EggApplicationCore } from '../../egg.js';
6
7
 
7
- const debug = debuglog('egg:lib:core:messenger:ipc');
8
+ const debug = debuglog('egg/lib/core/messenger/ipc');
8
9
 
9
10
  /**
10
11
  * Communication between app worker and agent worker by IPC channel
11
12
  */
12
13
  export class Messenger extends EventEmitter implements IMessenger {
13
14
  readonly pid: string;
15
+ readonly egg: EggApplicationCore;
14
16
  opids: string[] = [];
15
17
 
16
- constructor() {
18
+ constructor(egg: EggApplicationCore) {
17
19
  super();
18
20
  this.pid = String(process.pid);
21
+ this.egg = egg;
19
22
  // pids of agent or app managed by master
20
23
  // - retrieve app worker pids when it's an agent worker
21
24
  // - retrieve agent worker pids when it's an app worker
22
- this.on('egg-pids', pids => {
23
- this.opids = pids;
25
+ this.on('egg-pids', workerIds => {
26
+ debug('[%s:%s] got egg-pids %j', this.egg.type, this.pid, workerIds);
27
+ this.opids = workerIds.map((workerId: number) => String(workerId));
24
28
  });
25
29
  this.onMessage = this.onMessage.bind(this);
26
30
  process.on('message', this.onMessage);
@@ -36,7 +40,7 @@ export class Messenger extends EventEmitter implements IMessenger {
36
40
  * @return {Messenger} this
37
41
  */
38
42
  broadcast(action: string, data?: unknown): Messenger {
39
- debug('[%s] broadcast %s with %j', this.pid, action, data);
43
+ debug('[%s:%s] broadcast %s with %j', this.egg.type, this.pid, action, data);
40
44
  this.send(action, data, 'app');
41
45
  this.send(action, data, 'agent');
42
46
  return this;
@@ -44,21 +48,21 @@ export class Messenger extends EventEmitter implements IMessenger {
44
48
 
45
49
  /**
46
50
  * send message to the specified process
47
- * @param {String} pid - the process id of the receiver
51
+ * @param {String} workerId - the workerId of the receiver
48
52
  * @param {String} action - message key
49
53
  * @param {Object} data - message value
50
54
  * @return {Messenger} this
51
55
  */
52
- sendTo(pid: string, action: string, data?: unknown): Messenger {
53
- debug('[%s] send %s with %j to %s', this.pid, action, data, pid);
56
+ sendTo(workerId: string, action: string, data?: unknown): Messenger {
57
+ debug('[%s:%s] send %s with %j to workerId:%s', this.egg.type, this.pid, action, data, workerId);
54
58
  sendmessage(process, {
55
59
  action,
56
60
  data,
57
61
  /**
58
62
  * @deprecated Keep compatible, please use receiverWorkerId instead
59
63
  */
60
- receiverPid: String(pid),
61
- receiverWorkerId: String(pid),
64
+ receiverPid: String(workerId),
65
+ receiverWorkerId: String(workerId),
62
66
  });
63
67
  return this;
64
68
  }
@@ -73,11 +77,12 @@ export class Messenger extends EventEmitter implements IMessenger {
73
77
  */
74
78
  sendRandom(action: string, data?: unknown): Messenger {
75
79
  if (this.opids.length === 0) {
80
+ debug('[%s:%s] no pids, ignore sendRandom %s with %j', this.egg.type, this.pid, action, data);
76
81
  return this;
77
82
  }
78
83
  const index = Math.floor(Math.random() * this.opids.length);
79
- const pid = this.opids[index];
80
- this.sendTo(String(pid), action, data);
84
+ const workerId = this.opids[index];
85
+ this.sendTo(workerId, action, data);
81
86
  return this;
82
87
  }
83
88
 
@@ -88,7 +93,7 @@ export class Messenger extends EventEmitter implements IMessenger {
88
93
  * @return {Messenger} this
89
94
  */
90
95
  sendToApp(action: string, data?: unknown): Messenger {
91
- debug('[%s] send %s with %j to all app', this.pid, action, data);
96
+ debug('[%s:%s] send %s with %j to all app', this.egg.type, this.pid, action, data);
92
97
  this.send(action, data, 'app');
93
98
  return this;
94
99
  }
@@ -100,7 +105,7 @@ export class Messenger extends EventEmitter implements IMessenger {
100
105
  * @return {Messenger} this
101
106
  */
102
107
  sendToAgent(action: string, data?: unknown): Messenger {
103
- debug('[%s] send %s with %j to all agent', this.pid, action, data);
108
+ debug('[%s:%s] send %s with %j to all agent', this.egg.type, this.pid, action, data);
104
109
  this.send(action, data, 'agent');
105
110
  return this;
106
111
  }
@@ -122,9 +127,11 @@ export class Messenger extends EventEmitter implements IMessenger {
122
127
 
123
128
  onMessage(message: any) {
124
129
  if (typeof message?.action === 'string') {
125
- debug('[%s] got message %s with %j, receiverWorkerId: %s',
126
- this.pid, message.action, message.data, message.receiverWorkerId ?? message.receiverPid);
130
+ debug('[%s:%s] got message %s with %j, receiverWorkerId: %s',
131
+ this.egg.type, this.pid, message.action, message.data, message.receiverWorkerId ?? message.receiverPid);
127
132
  this.emit(message.action, message.data);
133
+ } else {
134
+ debug('[%s:%s] got an invalid message %j', this.egg.type, this.pid, message);
128
135
  }
129
136
  }
130
137
 
@@ -3,7 +3,7 @@ import EventEmitter from 'node:events';
3
3
  import type { IMessenger } from './IMessenger.js';
4
4
  import type { EggApplicationCore } from '../../egg.js';
5
5
 
6
- const debug = debuglog('egg:lib:core:messenger:local');
6
+ const debug = debuglog('egg/lib/core/messenger/local');
7
7
 
8
8
  /**
9
9
  * Communication between app worker and agent worker with EventEmitter
@@ -25,7 +25,7 @@ export class Messenger extends EventEmitter implements IMessenger {
25
25
  * @return {Messenger} this
26
26
  */
27
27
  broadcast(action: string, data?: unknown): Messenger {
28
- debug('[%s] broadcast %s with %j', this.pid, action, data);
28
+ debug('[%s:%s] broadcast %s with %j', this.egg.type, this.pid, action, data);
29
29
  this.send(action, data, 'both');
30
30
  return this;
31
31
  }
@@ -34,14 +34,14 @@ export class Messenger extends EventEmitter implements IMessenger {
34
34
  * send message to the specified process
35
35
  * Notice: in single process mode, it only can send to self process,
36
36
  * and it will send to both agent and app's messengers.
37
- * @param {String} pid - the process id of the receiver
37
+ * @param {String} workerId - the workerId of the receiver
38
38
  * @param {String} action - message key
39
39
  * @param {Object} data - message value
40
40
  * @return {Messenger} this
41
41
  */
42
- sendTo(pid: string, action: string, data?: unknown): Messenger {
43
- debug('[%s] send %s with %j to %s', this.pid, action, data, pid);
44
- if (String(pid) !== this.pid) {
42
+ sendTo(workerId: string, action: string, data?: unknown): Messenger {
43
+ debug('[%s:%s] send %s with %j to %s', this.egg.type, this.pid, action, data, workerId);
44
+ if (String(workerId) !== this.pid) {
45
45
  return this;
46
46
  }
47
47
  this.send(action, data, 'both');
@@ -58,7 +58,7 @@ export class Messenger extends EventEmitter implements IMessenger {
58
58
  * @return {Messenger} this
59
59
  */
60
60
  sendRandom(action: string, data?: unknown): Messenger {
61
- debug('[%s] send %s with %j to opposite', this.pid, action, data);
61
+ debug('[%s:%s] send %s with %j to opposite', this.egg.type, this.pid, action, data);
62
62
  this.send(action, data, 'opposite');
63
63
  return this;
64
64
  }
@@ -70,7 +70,7 @@ export class Messenger extends EventEmitter implements IMessenger {
70
70
  * @return {Messenger} this
71
71
  */
72
72
  sendToApp(action: string, data?: unknown): Messenger {
73
- debug('[%s] send %s with %j to all app', this.pid, action, data);
73
+ debug('[%s:%s] send %s with %j to all app', this.egg.type, this.pid, action, data);
74
74
  this.send(action, data, 'application');
75
75
  return this;
76
76
  }
@@ -82,7 +82,7 @@ export class Messenger extends EventEmitter implements IMessenger {
82
82
  * @return {Messenger} this
83
83
  */
84
84
  sendToAgent(action: string, data?: unknown): Messenger {
85
- debug('[%s] send %s with %j to all agent', this.pid, action, data);
85
+ debug('[%s:%s] send %s with %j to all agent', this.egg.type, this.pid, action, data);
86
86
  this.send(action, data, 'agent');
87
87
  return this;
88
88
  }
@@ -130,8 +130,10 @@ export class Messenger extends EventEmitter implements IMessenger {
130
130
 
131
131
  onMessage(message: any) {
132
132
  if (typeof message?.action === 'string') {
133
- debug('[%s] got message %s with %j', this.pid, message.action, message.data);
133
+ debug('[%s:%s] got message %s with %j', this.egg.type, this.pid, message.action, message.data);
134
134
  this.emit(message.action, message.data);
135
+ } else {
136
+ debug('[%s:%s] got an invalid message %j', this.egg.type, this.pid, message);
135
137
  }
136
138
  }
137
139
 
package/src/lib/egg.ts CHANGED
@@ -43,6 +43,8 @@ import { BaseHookClass } from './core/base_hook_class.js';
43
43
  import type { EggApplicationLoader } from './loader/index.js';
44
44
  import { getSourceDirname } from './utils.js';
45
45
 
46
+ import './egg.types.js';
47
+
46
48
  const EGG_PATH = Symbol.for('egg#eggPath');
47
49
 
48
50
  export interface EggApplicationCoreOptions extends Omit<EggCoreOptions, 'baseDir'> {
@@ -0,0 +1,6 @@
1
+ declare module '@eggjs/core' {
2
+ // add EggApplicationCore overrides types
3
+ interface EggCore {
4
+ inspect(): any;
5
+ }
6
+ }
package/src/lib/type.ts CHANGED
@@ -15,6 +15,9 @@ import type { MetaMiddlewareOptions } from '../app/middleware/meta.js';
15
15
  import type { NotFoundMiddlewareOptions } from '../app/middleware/notfound.js';
16
16
  import type { SiteFileMiddlewareOptions } from '../app/middleware/site_file.js';
17
17
 
18
+ // import @eggjs/watcher types
19
+ // import '@eggjs/watcher';
20
+
18
21
  type IgnoreItem = string | RegExp | ((ctx: ContextDelegation) => boolean);
19
22
  type IgnoreOrMatch = IgnoreItem | IgnoreItem[];
20
23