@nocobase/server 1.6.0-alpha.9 → 1.6.0-beta.2
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/lib/application.js +1 -4
- package/lib/gateway/index.js +10 -49
- package/lib/gateway/ws-server.d.ts +2 -0
- package/lib/gateway/ws-server.js +60 -20
- package/lib/plugin-manager/plugin-manager.d.ts +2 -2
- package/package.json +15 -15
package/lib/application.js
CHANGED
|
@@ -373,10 +373,7 @@ const _Application = class _Application extends import_koa.default {
|
|
|
373
373
|
this._loaded = false;
|
|
374
374
|
}
|
|
375
375
|
async createCacheManager() {
|
|
376
|
-
this._cacheManager = await (0, import_cache2.createCacheManager)(this,
|
|
377
|
-
prefix: this.name,
|
|
378
|
-
...this.options.cacheManager
|
|
379
|
-
});
|
|
376
|
+
this._cacheManager = await (0, import_cache2.createCacheManager)(this, this.options.cacheManager);
|
|
380
377
|
return this._cacheManager;
|
|
381
378
|
}
|
|
382
379
|
async load(options) {
|
package/lib/gateway/index.js
CHANGED
|
@@ -66,6 +66,14 @@ var import_ws_server = require("./ws-server");
|
|
|
66
66
|
var import_node_worker_threads = require("node:worker_threads");
|
|
67
67
|
var import_node_process = __toESM(require("node:process"));
|
|
68
68
|
const compress = (0, import_node_util.promisify)((0, import_compression.default)());
|
|
69
|
+
function getSocketPath() {
|
|
70
|
+
const { SOCKET_PATH } = import_node_process.default.env;
|
|
71
|
+
if ((0, import_path.isAbsolute)(SOCKET_PATH)) {
|
|
72
|
+
return SOCKET_PATH;
|
|
73
|
+
}
|
|
74
|
+
return (0, import_path.resolve)(import_node_process.default.cwd(), SOCKET_PATH);
|
|
75
|
+
}
|
|
76
|
+
__name(getSocketPath, "getSocketPath");
|
|
69
77
|
const _Gateway = class _Gateway extends import_events.EventEmitter {
|
|
70
78
|
/**
|
|
71
79
|
* use main app as default app to handle request
|
|
@@ -81,9 +89,7 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
|
|
|
81
89
|
constructor() {
|
|
82
90
|
super();
|
|
83
91
|
this.reset();
|
|
84
|
-
|
|
85
|
-
this.socketPath = (0, import_path.resolve)(import_node_process.default.cwd(), import_node_process.default.env.SOCKET_PATH);
|
|
86
|
-
}
|
|
92
|
+
this.socketPath = getSocketPath();
|
|
87
93
|
}
|
|
88
94
|
static getInstance(options = {}) {
|
|
89
95
|
if (!_Gateway.instance) {
|
|
@@ -92,7 +98,7 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
|
|
|
92
98
|
return _Gateway.instance;
|
|
93
99
|
}
|
|
94
100
|
static async getIPCSocketClient() {
|
|
95
|
-
const socketPath = (
|
|
101
|
+
const socketPath = getSocketPath();
|
|
96
102
|
try {
|
|
97
103
|
return await import_ipc_socket_client.IPCSocketClient.getConnection(socketPath);
|
|
98
104
|
} catch (error) {
|
|
@@ -355,51 +361,6 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
|
|
|
355
361
|
}
|
|
356
362
|
this.server = import_http.default.createServer(this.getCallback());
|
|
357
363
|
this.wsServer = new import_ws_server.WSServer();
|
|
358
|
-
this.wsServer.on("message", async ({ client, message }) => {
|
|
359
|
-
const app = await import_app_supervisor.AppSupervisor.getInstance().getApp(client.app);
|
|
360
|
-
if (!app) {
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
const parsedMessage = JSON.parse(message.toString());
|
|
364
|
-
if (!parsedMessage.type) {
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
if (!app.listenerCount(`ws:setTag`)) {
|
|
368
|
-
app.on("ws:setTag", ({ clientId, tagKey, tagValue }) => {
|
|
369
|
-
this.wsServer.setClientTag(clientId, tagKey, tagValue);
|
|
370
|
-
});
|
|
371
|
-
app.on("ws:removeTag", ({ clientId, tagKey }) => {
|
|
372
|
-
this.wsServer.removeClientTag(clientId, tagKey);
|
|
373
|
-
});
|
|
374
|
-
app.on("ws:sendToTag", ({ tagKey, tagValue, message: message2 }) => {
|
|
375
|
-
this.wsServer.sendToConnectionsByTags(
|
|
376
|
-
[
|
|
377
|
-
{ tagName: tagKey, tagValue },
|
|
378
|
-
{ tagName: "app", tagValue: app.name }
|
|
379
|
-
],
|
|
380
|
-
message2
|
|
381
|
-
);
|
|
382
|
-
});
|
|
383
|
-
app.on("ws:sendToTags", ({ tags, message: message2 }) => {
|
|
384
|
-
this.wsServer.sendToConnectionsByTags(tags, message2);
|
|
385
|
-
});
|
|
386
|
-
app.on("ws:authorized", ({ clientId, userId }) => {
|
|
387
|
-
this.wsServer.sendToConnectionsByTags(
|
|
388
|
-
[
|
|
389
|
-
{ tagName: "userId", tagValue: userId },
|
|
390
|
-
{ tagName: "app", tagValue: app.name }
|
|
391
|
-
],
|
|
392
|
-
{ type: "authorized" }
|
|
393
|
-
);
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
const eventName = `ws:message:${parsedMessage.type}`;
|
|
397
|
-
app.emit(eventName, {
|
|
398
|
-
clientId: client.id,
|
|
399
|
-
tags: [...client.tags],
|
|
400
|
-
payload: parsedMessage.payload
|
|
401
|
-
});
|
|
402
|
-
});
|
|
403
364
|
this.server.on("upgrade", (request, socket, head) => {
|
|
404
365
|
const { pathname } = (0, import_url.parse)(request.url);
|
|
405
366
|
if (pathname === import_node_process.default.env.WS_PATH) {
|
|
@@ -28,6 +28,7 @@ export declare class WSServer extends EventEmitter {
|
|
|
28
28
|
webSocketClients: Map<string, WebSocketClient>;
|
|
29
29
|
logger: Logger;
|
|
30
30
|
constructor();
|
|
31
|
+
bindAppWSEvents(app: any): void;
|
|
31
32
|
addNewConnection(ws: WebSocketWithId, request: IncomingMessage): WebSocketClient;
|
|
32
33
|
setClientTag(clientId: string, tagKey: string, tagValue: string): void;
|
|
33
34
|
removeClientTag(clientId: string, tagKey: string): void;
|
|
@@ -44,6 +45,7 @@ export declare class WSServer extends EventEmitter {
|
|
|
44
45
|
tagName: string;
|
|
45
46
|
tagValue: string;
|
|
46
47
|
}>, sendMessage: object): void;
|
|
48
|
+
sendToClient(clientId: string, sendMessage: object): void;
|
|
47
49
|
loopThroughConnections(callback: (client: WebSocketClient) => void): void;
|
|
48
50
|
close(): void;
|
|
49
51
|
}
|
package/lib/gateway/ws-server.js
CHANGED
|
@@ -133,6 +133,57 @@ const _WSServer = class _WSServer extends import_events.default {
|
|
|
133
133
|
}
|
|
134
134
|
});
|
|
135
135
|
});
|
|
136
|
+
import_app_supervisor.AppSupervisor.getInstance().on("afterAppAdded", (app) => {
|
|
137
|
+
this.bindAppWSEvents(app);
|
|
138
|
+
});
|
|
139
|
+
this.on("message", async ({ client, message }) => {
|
|
140
|
+
const app = await import_app_supervisor.AppSupervisor.getInstance().getApp(client.app);
|
|
141
|
+
if (!app) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const parsedMessage = JSON.parse(message.toString());
|
|
145
|
+
if (!parsedMessage.type) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const eventName = `ws:message:${parsedMessage.type}`;
|
|
149
|
+
app.emit(eventName, {
|
|
150
|
+
clientId: client.id,
|
|
151
|
+
tags: [...client.tags],
|
|
152
|
+
payload: parsedMessage.payload
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
bindAppWSEvents(app) {
|
|
157
|
+
if (app.listenerCount("ws:setTag") > 0) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
app.on("ws:setTag", ({ clientId, tagKey, tagValue }) => {
|
|
161
|
+
this.setClientTag(clientId, tagKey, tagValue);
|
|
162
|
+
});
|
|
163
|
+
app.on("ws:removeTag", ({ clientId, tagKey }) => {
|
|
164
|
+
this.removeClientTag(clientId, tagKey);
|
|
165
|
+
});
|
|
166
|
+
app.on("ws:sendToTag", ({ tagKey, tagValue, message }) => {
|
|
167
|
+
this.sendToConnectionsByTags(
|
|
168
|
+
[
|
|
169
|
+
{ tagName: tagKey, tagValue },
|
|
170
|
+
{ tagName: "app", tagValue: app.name }
|
|
171
|
+
],
|
|
172
|
+
message
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
app.on("ws:sendToClient", ({ clientId, message }) => {
|
|
176
|
+
this.sendToClient(clientId, message);
|
|
177
|
+
});
|
|
178
|
+
app.on("ws:sendToCurrentApp", ({ message }) => {
|
|
179
|
+
this.sendToConnectionsByTag("app", app.name, message);
|
|
180
|
+
});
|
|
181
|
+
app.on("ws:sendToTags", ({ tags, message }) => {
|
|
182
|
+
this.sendToConnectionsByTags(tags, message);
|
|
183
|
+
});
|
|
184
|
+
app.on("ws:authorized", ({ clientId, userId }) => {
|
|
185
|
+
this.sendToClient(clientId, { type: "authorized" });
|
|
186
|
+
});
|
|
136
187
|
}
|
|
137
188
|
addNewConnection(ws, request) {
|
|
138
189
|
const id = (0, import_nanoid.nanoid)();
|
|
@@ -149,6 +200,9 @@ const _WSServer = class _WSServer extends import_events.default {
|
|
|
149
200
|
}
|
|
150
201
|
setClientTag(clientId, tagKey, tagValue) {
|
|
151
202
|
const client = this.webSocketClients.get(clientId);
|
|
203
|
+
if (!client) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
152
206
|
client.tags.add(`${tagKey}#${tagValue}`);
|
|
153
207
|
console.log(`client tags: ${Array.from(client.tags)}`);
|
|
154
208
|
}
|
|
@@ -173,26 +227,6 @@ const _WSServer = class _WSServer extends import_events.default {
|
|
|
173
227
|
if (!hasApp) {
|
|
174
228
|
import_app_supervisor.AppSupervisor.getInstance().bootStrapApp(handleAppName);
|
|
175
229
|
}
|
|
176
|
-
const appStatus = import_app_supervisor.AppSupervisor.getInstance().getAppStatus(handleAppName, "initializing");
|
|
177
|
-
if (appStatus === "not_found") {
|
|
178
|
-
this.sendMessageToConnection(client, {
|
|
179
|
-
type: "maintaining",
|
|
180
|
-
payload: getPayloadByErrorCode("APP_NOT_FOUND", { appName: handleAppName })
|
|
181
|
-
});
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
if (appStatus === "initializing") {
|
|
185
|
-
this.sendMessageToConnection(client, {
|
|
186
|
-
type: "maintaining",
|
|
187
|
-
payload: getPayloadByErrorCode("APP_INITIALIZING", { appName: handleAppName })
|
|
188
|
-
});
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
const app = await import_app_supervisor.AppSupervisor.getInstance().getApp(handleAppName);
|
|
192
|
-
this.sendMessageToConnection(client, {
|
|
193
|
-
type: "maintaining",
|
|
194
|
-
payload: getPayloadByErrorCode(appStatus, { app })
|
|
195
|
-
});
|
|
196
230
|
}
|
|
197
231
|
removeConnection(id) {
|
|
198
232
|
console.log(`client disconnected ${id}`);
|
|
@@ -217,6 +251,12 @@ const _WSServer = class _WSServer extends import_events.default {
|
|
|
217
251
|
}
|
|
218
252
|
});
|
|
219
253
|
}
|
|
254
|
+
sendToClient(clientId, sendMessage) {
|
|
255
|
+
const client = this.webSocketClients.get(clientId);
|
|
256
|
+
if (client) {
|
|
257
|
+
this.sendMessageToConnection(client, sendMessage);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
220
260
|
loopThroughConnections(callback) {
|
|
221
261
|
this.webSocketClients.forEach((client) => {
|
|
222
262
|
callback(client);
|
|
@@ -89,9 +89,9 @@ export declare class PluginManager {
|
|
|
89
89
|
addPreset(plugin: string | typeof Plugin, options?: any): void;
|
|
90
90
|
getPlugins(): Map<typeof Plugin, Plugin<any>>;
|
|
91
91
|
getAliases(): IterableIterator<string>;
|
|
92
|
-
get(name: string | typeof Plugin):
|
|
92
|
+
get<T extends Plugin>(name: string | typeof Plugin | (new () => T)): T;
|
|
93
93
|
has(name: string | typeof Plugin): boolean;
|
|
94
|
-
del(name:
|
|
94
|
+
del(name: any): void;
|
|
95
95
|
create(pluginName: string, options?: {
|
|
96
96
|
forceRecreate?: boolean;
|
|
97
97
|
}): Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/server",
|
|
3
|
-
"version": "1.6.0-
|
|
3
|
+
"version": "1.6.0-beta.2",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "./lib/index.d.ts",
|
|
6
6
|
"license": "AGPL-3.0",
|
|
@@ -10,19 +10,19 @@
|
|
|
10
10
|
"@koa/cors": "^3.1.0",
|
|
11
11
|
"@koa/multer": "^3.0.2",
|
|
12
12
|
"@koa/router": "^9.4.0",
|
|
13
|
-
"@nocobase/acl": "1.6.0-
|
|
14
|
-
"@nocobase/actions": "1.6.0-
|
|
15
|
-
"@nocobase/auth": "1.6.0-
|
|
16
|
-
"@nocobase/cache": "1.6.0-
|
|
17
|
-
"@nocobase/data-source-manager": "1.6.0-
|
|
18
|
-
"@nocobase/database": "1.6.0-
|
|
19
|
-
"@nocobase/evaluators": "1.6.0-
|
|
20
|
-
"@nocobase/lock-manager": "1.6.0-
|
|
21
|
-
"@nocobase/logger": "1.6.0-
|
|
22
|
-
"@nocobase/resourcer": "1.6.0-
|
|
23
|
-
"@nocobase/sdk": "1.6.0-
|
|
24
|
-
"@nocobase/telemetry": "1.6.0-
|
|
25
|
-
"@nocobase/utils": "1.6.0-
|
|
13
|
+
"@nocobase/acl": "1.6.0-beta.2",
|
|
14
|
+
"@nocobase/actions": "1.6.0-beta.2",
|
|
15
|
+
"@nocobase/auth": "1.6.0-beta.2",
|
|
16
|
+
"@nocobase/cache": "1.6.0-beta.2",
|
|
17
|
+
"@nocobase/data-source-manager": "1.6.0-beta.2",
|
|
18
|
+
"@nocobase/database": "1.6.0-beta.2",
|
|
19
|
+
"@nocobase/evaluators": "1.6.0-beta.2",
|
|
20
|
+
"@nocobase/lock-manager": "1.6.0-beta.2",
|
|
21
|
+
"@nocobase/logger": "1.6.0-beta.2",
|
|
22
|
+
"@nocobase/resourcer": "1.6.0-beta.2",
|
|
23
|
+
"@nocobase/sdk": "1.6.0-beta.2",
|
|
24
|
+
"@nocobase/telemetry": "1.6.0-beta.2",
|
|
25
|
+
"@nocobase/utils": "1.6.0-beta.2",
|
|
26
26
|
"@types/decompress": "4.2.7",
|
|
27
27
|
"@types/ini": "^1.3.31",
|
|
28
28
|
"@types/koa-send": "^4.1.3",
|
|
@@ -56,5 +56,5 @@
|
|
|
56
56
|
"@types/serve-handler": "^6.1.1",
|
|
57
57
|
"@types/ws": "^8.5.5"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "476ab2f424d86f585c8fba1459568c8aec49a5c6"
|
|
60
60
|
}
|