@nocobase/server 1.9.0-beta.5 → 1.9.0-beta.6

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.
@@ -279,6 +279,9 @@ const _Application = class _Application extends import_koa.default {
279
279
  if (!WORKER_MODE) {
280
280
  return true;
281
281
  }
282
+ if (WORKER_MODE === "-") {
283
+ return false;
284
+ }
282
285
  const topics = WORKER_MODE.trim().split(",");
283
286
  if (key) {
284
287
  if (WORKER_MODE === "*") {
@@ -252,6 +252,9 @@ const _AuditManager = class _AuditManager {
252
252
  async output(ctx, reqId, metadata) {
253
253
  var _a;
254
254
  try {
255
+ if (!ctx.action) {
256
+ return;
257
+ }
255
258
  const { resourceName, actionName } = ctx.action;
256
259
  const action = this.getAction(actionName, resourceName);
257
260
  if (!action) {
@@ -195,6 +195,7 @@ const _MemoryEventQueueAdapter = class _MemoryEventQueueAdapter {
195
195
  publish(channel, content, options = { timestamp: Date.now() }) {
196
196
  const event = this.events.get(channel);
197
197
  if (!event) {
198
+ console.debug(`memory queue (${channel}) not subscribed, skip`);
198
199
  return;
199
200
  }
200
201
  if (!this.queues.get(channel)) {
@@ -265,14 +266,16 @@ const _EventQueue = class _EventQueue {
265
266
  constructor(app, options = {}) {
266
267
  this.app = app;
267
268
  this.options = options;
268
- this.setAdapter(new MemoryEventQueueAdapter({ appName: this.app.name }));
269
- app.on("afterStart", async () => {
270
- await this.connect();
271
- });
272
- app.on("beforeStop", async () => {
273
- app.logger.info("[queue] gracefully shutting down...");
274
- await this.close();
275
- });
269
+ if (app.serving()) {
270
+ this.setAdapter(new MemoryEventQueueAdapter({ appName: this.app.name }));
271
+ app.on("afterStart", async () => {
272
+ await this.connect();
273
+ });
274
+ app.on("beforeStop", async () => {
275
+ app.logger.info("[queue] gracefully shutting down...");
276
+ await this.close();
277
+ });
278
+ }
276
279
  }
277
280
  adapter;
278
281
  events = /* @__PURE__ */ new Map();
@@ -296,7 +299,12 @@ const _EventQueue = class _EventQueue {
296
299
  if (!this.adapter) {
297
300
  throw new Error("no adapter set, cannot connect");
298
301
  }
302
+ if (!this.app.serving()) {
303
+ this.app.logger.warn("app is not serving, will not connect to event queue");
304
+ return;
305
+ }
299
306
  await this.adapter.connect();
307
+ this.app.logger.debug(`connected to adapter, using memory? ${this.adapter instanceof MemoryEventQueueAdapter}`);
300
308
  for (const [channel, event] of this.events.entries()) {
301
309
  this.adapter.subscribe(this.getFullChannel(channel), event);
302
310
  }
@@ -337,7 +345,7 @@ const _EventQueue = class _EventQueue {
337
345
  throw new Error("event queue not connected, cannot publish");
338
346
  }
339
347
  const c = this.getFullChannel(channel);
340
- this.app.logger.debug("event queue publishing:", { channel: c, message });
348
+ this.app.logger.debug(`event queue publishing to channel(${c})`, { message });
341
349
  await this.adapter.publish(c, message, {
342
350
  timeout: QUEUE_DEFAULT_ACK_TIMEOUT,
343
351
  ...options,
@@ -172,6 +172,20 @@ const _WSServer = class _WSServer extends import_events.default {
172
172
  message
173
173
  );
174
174
  });
175
+ app.on("ws:sendToUser", ({ userId, message }) => {
176
+ this.sendToAppUser(app.name, userId, message);
177
+ app.logger.trace(`[broadcasting message] ws:sendToUser for user ${userId}`, { message });
178
+ app.pubSubManager.publish(
179
+ "ws:sendToUser",
180
+ {
181
+ userId,
182
+ message
183
+ },
184
+ {
185
+ skipSelf: true
186
+ }
187
+ );
188
+ });
175
189
  app.on("ws:sendToClient", ({ clientId, message }) => {
176
190
  this.sendToClient(clientId, message);
177
191
  });
@@ -184,6 +198,12 @@ const _WSServer = class _WSServer extends import_events.default {
184
198
  app.on("ws:authorized", ({ clientId, userId }) => {
185
199
  this.sendToClient(clientId, { type: "authorized" });
186
200
  });
201
+ app.on("afterLoad", () => {
202
+ app.pubSubManager.subscribe("ws:sendToUser", ({ userId, message }) => {
203
+ app.logger.debug(`[receive broadcasting message] ws:sendToUser for user ${userId}`, { message });
204
+ this.sendToAppUser(app.name, userId, message);
205
+ });
206
+ });
187
207
  }
188
208
  addNewConnection(ws, request) {
189
209
  const id = (0, import_nanoid.nanoid)();
@@ -11,11 +11,12 @@ import { HandlerManager } from './handler-manager';
11
11
  import { PubSubCallback, type IPubSubAdapter, type PubSubManagerOptions, type PubSubManagerPublishOptions, type PubSubManagerSubscribeOptions } from './types';
12
12
  export declare const createPubSubManager: (app: Application, options: PubSubManagerOptions) => PubSubManager;
13
13
  export declare class PubSubManager {
14
+ protected app: Application;
14
15
  protected options: PubSubManagerOptions;
15
16
  protected publisherId: string;
16
17
  protected adapter: IPubSubAdapter;
17
18
  protected handlerManager: HandlerManager;
18
- constructor(options?: PubSubManagerOptions);
19
+ constructor(app: Application, options?: PubSubManagerOptions);
19
20
  get channelPrefix(): string;
20
21
  setAdapter(adapter: IPubSubAdapter): void;
21
22
  isConnected(): Promise<boolean>;
@@ -23,5 +24,5 @@ export declare class PubSubManager {
23
24
  close(): Promise<any>;
24
25
  subscribe(channel: string, callback: PubSubCallback, options?: PubSubManagerSubscribeOptions): Promise<void>;
25
26
  unsubscribe(channel: string, callback: PubSubCallback): Promise<any>;
26
- publish(channel: string, message: any, options?: PubSubManagerPublishOptions): Promise<any>;
27
+ publish(channel: string, message: any, options?: PubSubManagerPublishOptions): Promise<void>;
27
28
  }
@@ -34,17 +34,20 @@ module.exports = __toCommonJS(pub_sub_manager_exports);
34
34
  var import_utils = require("@nocobase/utils");
35
35
  var import_handler_manager = require("./handler-manager");
36
36
  const createPubSubManager = /* @__PURE__ */ __name((app, options) => {
37
- const pubSubManager = new PubSubManager(options);
38
- app.on("afterStart", async () => {
39
- await pubSubManager.connect();
40
- });
41
- app.on("afterStop", async () => {
42
- await pubSubManager.close();
43
- });
37
+ const pubSubManager = new PubSubManager(app, options);
38
+ if (app.serving()) {
39
+ app.on("afterStart", async () => {
40
+ await pubSubManager.connect();
41
+ });
42
+ app.on("afterStop", async () => {
43
+ await pubSubManager.close();
44
+ });
45
+ }
44
46
  return pubSubManager;
45
47
  }, "createPubSubManager");
46
48
  const _PubSubManager = class _PubSubManager {
47
- constructor(options = {}) {
49
+ constructor(app, options = {}) {
50
+ this.app = app;
48
51
  this.options = options;
49
52
  this.publisherId = (0, import_utils.uid)();
50
53
  this.handlerManager = new import_handler_manager.HandlerManager(this.publisherId);
@@ -69,8 +72,13 @@ const _PubSubManager = class _PubSubManager {
69
72
  if (!this.adapter) {
70
73
  return;
71
74
  }
75
+ if (!this.app.serving()) {
76
+ this.app.logger.warn("app is not serving, will not connect to event queue");
77
+ return;
78
+ }
72
79
  await this.adapter.connect();
73
80
  await this.handlerManager.each(async (channel, headler) => {
81
+ this.app.logger.debug(`[PubSubManager] subscribe ${channel} added before connected`);
74
82
  await this.adapter.subscribe(`${this.channelPrefix}${channel}`, headler);
75
83
  });
76
84
  }
@@ -84,6 +92,7 @@ const _PubSubManager = class _PubSubManager {
84
92
  await this.unsubscribe(channel, callback);
85
93
  const handler = this.handlerManager.set(channel, callback, options);
86
94
  if (await this.isConnected()) {
95
+ this.app.logger.debug(`[PubSubManager] subscribe ${channel} added after connected`);
87
96
  await this.adapter.subscribe(`${this.channelPrefix}${channel}`, handler);
88
97
  }
89
98
  }
@@ -97,6 +106,9 @@ const _PubSubManager = class _PubSubManager {
97
106
  async publish(channel, message, options) {
98
107
  var _a;
99
108
  if (!((_a = this.adapter) == null ? void 0 : _a.isConnected())) {
109
+ this.app.logger.warn(
110
+ `[PubSubManager] adapter is not exist or not connected, cannot publish message to channel ${channel}`
111
+ );
100
112
  return;
101
113
  }
102
114
  const wrappedMessage = JSON.stringify({
@@ -104,7 +116,8 @@ const _PubSubManager = class _PubSubManager {
104
116
  ...options,
105
117
  message
106
118
  });
107
- return this.adapter.publish(`${this.channelPrefix}${channel}`, wrappedMessage);
119
+ await this.adapter.publish(`${this.channelPrefix}${channel}`, wrappedMessage);
120
+ this.app.logger.trace(`[PubSubManager] published message to channel ${channel}`);
108
121
  }
109
122
  };
110
123
  __name(_PubSubManager, "PubSubManager");
@@ -16,7 +16,7 @@ export declare class SyncMessageManager {
16
16
  protected pubSubManager: PubSubManager;
17
17
  constructor(app: Application, options?: any);
18
18
  get debounce(): any;
19
- publish(channel: string, message: any, options?: PubSubManagerPublishOptions & Transactionable): Promise<any>;
19
+ publish(channel: string, message: any, options?: PubSubManagerPublishOptions & Transactionable): Promise<unknown>;
20
20
  subscribe(channel: string, callback: PubSubCallback): Promise<void>;
21
21
  unsubscribe(channel: string, callback: PubSubCallback): Promise<any>;
22
22
  sync(): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/server",
3
- "version": "1.9.0-beta.5",
3
+ "version": "1.9.0-beta.6",
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": "^5.0.0",
11
11
  "@koa/multer": "^3.1.0",
12
12
  "@koa/router": "^13.1.0",
13
- "@nocobase/acl": "1.9.0-beta.5",
14
- "@nocobase/actions": "1.9.0-beta.5",
15
- "@nocobase/auth": "1.9.0-beta.5",
16
- "@nocobase/cache": "1.9.0-beta.5",
17
- "@nocobase/data-source-manager": "1.9.0-beta.5",
18
- "@nocobase/database": "1.9.0-beta.5",
19
- "@nocobase/evaluators": "1.9.0-beta.5",
20
- "@nocobase/lock-manager": "1.9.0-beta.5",
21
- "@nocobase/logger": "1.9.0-beta.5",
22
- "@nocobase/resourcer": "1.9.0-beta.5",
23
- "@nocobase/sdk": "1.9.0-beta.5",
24
- "@nocobase/telemetry": "1.9.0-beta.5",
25
- "@nocobase/utils": "1.9.0-beta.5",
13
+ "@nocobase/acl": "1.9.0-beta.6",
14
+ "@nocobase/actions": "1.9.0-beta.6",
15
+ "@nocobase/auth": "1.9.0-beta.6",
16
+ "@nocobase/cache": "1.9.0-beta.6",
17
+ "@nocobase/data-source-manager": "1.9.0-beta.6",
18
+ "@nocobase/database": "1.9.0-beta.6",
19
+ "@nocobase/evaluators": "1.9.0-beta.6",
20
+ "@nocobase/lock-manager": "1.9.0-beta.6",
21
+ "@nocobase/logger": "1.9.0-beta.6",
22
+ "@nocobase/resourcer": "1.9.0-beta.6",
23
+ "@nocobase/sdk": "1.9.0-beta.6",
24
+ "@nocobase/telemetry": "1.9.0-beta.6",
25
+ "@nocobase/utils": "1.9.0-beta.6",
26
26
  "@types/decompress": "4.2.7",
27
27
  "@types/ini": "^1.3.31",
28
28
  "@types/koa-send": "^4.1.3",
@@ -57,5 +57,5 @@
57
57
  "@types/serve-handler": "^6.1.1",
58
58
  "@types/ws": "^8.5.5"
59
59
  },
60
- "gitHead": "9a61c60dd3db5af64244e82447309b0cb17aabb6"
60
+ "gitHead": "ada5e359a91135cb9baf605c04c5c380f067a046"
61
61
  }