egg 4.0.2 → 4.0.4
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/dist/commonjs/app/extend/context.d.ts +2 -0
- package/dist/commonjs/index.d.ts +1 -1
- package/dist/commonjs/index.js +5 -3
- package/dist/commonjs/lib/application.d.ts +0 -7
- package/dist/commonjs/lib/application.js +1 -16
- package/dist/commonjs/lib/egg.d.ts +9 -10
- package/dist/commonjs/lib/egg.js +17 -21
- package/dist/esm/app/extend/context.d.ts +2 -0
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +3 -2
- package/dist/esm/lib/application.d.ts +0 -7
- package/dist/esm/lib/application.js +1 -16
- package/dist/esm/lib/egg.d.ts +9 -10
- package/dist/esm/lib/egg.js +17 -21
- package/dist/package.json +1 -1
- package/package.json +8 -3
- package/src/app/extend/context.ts +2 -0
- package/src/index.ts +7 -1
- package/src/lib/application.ts +0 -16
- package/src/lib/egg.ts +19 -26
- package/dist/commonjs/lib/core/singleton.d.ts +0 -29
- package/dist/commonjs/lib/core/singleton.js +0 -125
- package/dist/esm/lib/core/singleton.d.ts +0 -29
- package/dist/esm/lib/core/singleton.js +0 -118
- package/src/lib/core/singleton.ts +0 -149
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert';
|
|
2
|
-
import { isAsyncFunction } from 'is-type-of';
|
|
3
|
-
import type { EggCore } from '@eggjs/core';
|
|
4
|
-
|
|
5
|
-
export type SingletonCreateMethod =
|
|
6
|
-
(config: Record<string, any>, app: EggCore, clientName: string) => unknown | Promise<unknown>;
|
|
7
|
-
|
|
8
|
-
export interface SingletonOptions {
|
|
9
|
-
name: string;
|
|
10
|
-
app: EggCore;
|
|
11
|
-
create: SingletonCreateMethod;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class Singleton<T = any> {
|
|
15
|
-
readonly clients = new Map<string, T>();
|
|
16
|
-
readonly app: EggCore;
|
|
17
|
-
readonly create: SingletonCreateMethod;
|
|
18
|
-
readonly name: string;
|
|
19
|
-
readonly options: Record<string, any>;
|
|
20
|
-
|
|
21
|
-
constructor(options: SingletonOptions) {
|
|
22
|
-
assert(options.name, '[egg:singleton] Singleton#constructor options.name is required');
|
|
23
|
-
assert(options.app, '[egg:singleton] Singleton#constructor options.app is required');
|
|
24
|
-
assert(options.create, '[egg:singleton] Singleton#constructor options.create is required');
|
|
25
|
-
assert(!(options.name in options.app), `[egg:singleton] ${options.name} is already exists in app`);
|
|
26
|
-
this.app = options.app;
|
|
27
|
-
this.name = options.name;
|
|
28
|
-
this.create = options.create;
|
|
29
|
-
this.options = options.app.config[this.name] ?? {};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
init() {
|
|
33
|
-
return isAsyncFunction(this.create) ? this.initAsync() : this.initSync();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
initSync() {
|
|
37
|
-
const options = this.options;
|
|
38
|
-
assert(!(options.client && options.clients),
|
|
39
|
-
`[egg:singleton] ${this.name} can not set options.client and options.clients both`);
|
|
40
|
-
|
|
41
|
-
// alias app[name] as client, but still support createInstance method
|
|
42
|
-
if (options.client) {
|
|
43
|
-
const client = this.createInstance(options.client, options.name);
|
|
44
|
-
this.#setClientToApp(client);
|
|
45
|
-
this.#extendDynamicMethods(client);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// multi client, use app[name].getSingletonInstance(id)
|
|
50
|
-
if (options.clients) {
|
|
51
|
-
Object.keys(options.clients).forEach(id => {
|
|
52
|
-
const client = this.createInstance(options.clients[id], id);
|
|
53
|
-
this.clients.set(id, client);
|
|
54
|
-
});
|
|
55
|
-
this.#setClientToApp(this);
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// no config.clients and config.client
|
|
60
|
-
this.#setClientToApp(this);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
async initAsync() {
|
|
64
|
-
const options = this.options;
|
|
65
|
-
assert(!(options.client && options.clients),
|
|
66
|
-
`[egg:singleton] ${this.name} can not set options.client and options.clients both`);
|
|
67
|
-
|
|
68
|
-
// alias app[name] as client, but still support createInstance method
|
|
69
|
-
if (options.client) {
|
|
70
|
-
const client = await this.createInstanceAsync(options.client, options.name);
|
|
71
|
-
this.#setClientToApp(client);
|
|
72
|
-
this.#extendDynamicMethods(client);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// multi client, use app[name].getInstance(id)
|
|
77
|
-
if (options.clients) {
|
|
78
|
-
await Promise.all(Object.keys(options.clients).map((id: string) => {
|
|
79
|
-
return this.createInstanceAsync(options.clients[id], id)
|
|
80
|
-
.then(client => this.clients.set(id, client));
|
|
81
|
-
}));
|
|
82
|
-
this.#setClientToApp(this);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// no config.clients and config.client
|
|
87
|
-
this.#setClientToApp(this);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
#setClientToApp(client: unknown) {
|
|
91
|
-
Reflect.set(this.app, this.name, client);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* @deprecated please use `getSingletonInstance(id)` instead
|
|
96
|
-
*/
|
|
97
|
-
get(id: string) {
|
|
98
|
-
return this.clients.get(id)!;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Get singleton instance by id
|
|
103
|
-
*/
|
|
104
|
-
getSingletonInstance(id: string) {
|
|
105
|
-
return this.clients.get(id)!;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
createInstance(config: Record<string, any>, clientName: string) {
|
|
109
|
-
// async creator only support createInstanceAsync
|
|
110
|
-
assert(!isAsyncFunction(this.create),
|
|
111
|
-
`egg:singleton ${this.name} only support create asynchronous, please use createInstanceAsync`);
|
|
112
|
-
// options.default will be merge in to options.clients[id]
|
|
113
|
-
config = {
|
|
114
|
-
...this.options.default,
|
|
115
|
-
...config,
|
|
116
|
-
};
|
|
117
|
-
return (this.create as SingletonCreateMethod)(config, this.app, clientName) as T;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async createInstanceAsync(config: Record<string, any>, clientName: string) {
|
|
121
|
-
// options.default will be merge in to options.clients[id]
|
|
122
|
-
config = {
|
|
123
|
-
...this.options.default,
|
|
124
|
-
...config,
|
|
125
|
-
};
|
|
126
|
-
return await this.create(config, this.app, clientName) as T;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
#extendDynamicMethods(client: any) {
|
|
130
|
-
assert(!client.createInstance, 'singleton instance should not have createInstance method');
|
|
131
|
-
assert(!client.createInstanceAsync, 'singleton instance should not have createInstanceAsync method');
|
|
132
|
-
|
|
133
|
-
try {
|
|
134
|
-
let extendable = client;
|
|
135
|
-
// Object.preventExtensions() or Object.freeze()
|
|
136
|
-
if (!Object.isExtensible(client) || Object.isFrozen(client)) {
|
|
137
|
-
// eslint-disable-next-line no-proto
|
|
138
|
-
extendable = client.__proto__ || client;
|
|
139
|
-
}
|
|
140
|
-
extendable.createInstance = this.createInstance.bind(this);
|
|
141
|
-
extendable.createInstanceAsync = this.createInstanceAsync.bind(this);
|
|
142
|
-
} catch (err) {
|
|
143
|
-
this.app.coreLogger.warn(
|
|
144
|
-
'[egg:singleton] %s dynamic create is disabled because of client is un-extendable',
|
|
145
|
-
this.name);
|
|
146
|
-
this.app.coreLogger.warn(err);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|