@nocobase/server 1.5.0-beta.1 → 1.6.0-alpha.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.d.ts +13 -2
- package/lib/application.js +21 -3
- package/lib/audit-manager/index.d.ts +115 -0
- package/lib/audit-manager/index.js +326 -0
- package/lib/helper.js +1 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.js +4 -2
- package/lib/locale/locale.d.ts +1 -0
- package/lib/locale/locale.js +12 -1
- package/lib/plugin-manager/deps.js +1 -1
- package/lib/plugin-manager/plugin-manager.js +31 -0
- package/lib/plugin.d.ts +3 -12
- package/lib/plugin.js +6 -11
- package/lib/pub-sub-manager/handler-manager.d.ts +34 -0
- package/lib/pub-sub-manager/handler-manager.js +149 -0
- package/lib/pub-sub-manager/index.d.ts +11 -0
- package/lib/pub-sub-manager/index.js +34 -0
- package/lib/pub-sub-manager/pub-sub-manager.d.ts +27 -0
- package/lib/pub-sub-manager/pub-sub-manager.js +116 -0
- package/lib/pub-sub-manager/types.d.ts +27 -0
- package/lib/pub-sub-manager/types.js +24 -0
- package/lib/sync-message-manager.d.ts +25 -0
- package/lib/sync-message-manager.js +105 -0
- package/package.json +16 -14
- package/lib/sync-manager.d.ts +0 -46
- package/lib/sync-manager.js +0 -137
|
@@ -421,6 +421,37 @@ const _PluginManager = class _PluginManager {
|
|
|
421
421
|
plugin.state.loaded = true;
|
|
422
422
|
await this.app.emitAsync("afterLoadPlugin", plugin, options);
|
|
423
423
|
}
|
|
424
|
+
const getSourceAndTargetForAddAction = /* @__PURE__ */ __name(async (ctx) => {
|
|
425
|
+
const { packageName } = ctx.action.params;
|
|
426
|
+
return {
|
|
427
|
+
targetCollection: "applicationPlugins",
|
|
428
|
+
targetRecordUK: packageName
|
|
429
|
+
};
|
|
430
|
+
}, "getSourceAndTargetForAddAction");
|
|
431
|
+
const getSourceAndTargetForUpdateAction = /* @__PURE__ */ __name(async (ctx) => {
|
|
432
|
+
let { packageName } = ctx.action.params;
|
|
433
|
+
if (ctx.file) {
|
|
434
|
+
packageName = ctx.request.body.packageName;
|
|
435
|
+
}
|
|
436
|
+
return {
|
|
437
|
+
targetCollection: "applicationPlugins",
|
|
438
|
+
targetRecordUK: packageName
|
|
439
|
+
};
|
|
440
|
+
}, "getSourceAndTargetForUpdateAction");
|
|
441
|
+
const getSourceAndTargetForOtherActions = /* @__PURE__ */ __name(async (ctx) => {
|
|
442
|
+
const { filterByTk } = ctx.action.params;
|
|
443
|
+
return {
|
|
444
|
+
targetCollection: "applicationPlugins",
|
|
445
|
+
targetRecordUK: filterByTk
|
|
446
|
+
};
|
|
447
|
+
}, "getSourceAndTargetForOtherActions");
|
|
448
|
+
this.app.auditManager.registerActions([
|
|
449
|
+
{ name: "pm:add", getSourceAndTarget: getSourceAndTargetForAddAction },
|
|
450
|
+
{ name: "pm:update", getSourceAndTarget: getSourceAndTargetForUpdateAction },
|
|
451
|
+
{ name: "pm:enable", getSourceAndTarget: getSourceAndTargetForOtherActions },
|
|
452
|
+
{ name: "pm:disable", getSourceAndTarget: getSourceAndTargetForOtherActions },
|
|
453
|
+
{ name: "pm:remove", getSourceAndTarget: getSourceAndTargetForOtherActions }
|
|
454
|
+
]);
|
|
424
455
|
this.app.log.debug("plugins loaded");
|
|
425
456
|
this.app.setMaintainingMessage("plugins loaded");
|
|
426
457
|
}
|
package/lib/plugin.d.ts
CHANGED
|
@@ -6,12 +6,11 @@
|
|
|
6
6
|
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
|
-
import { Model } from '@nocobase/database';
|
|
9
|
+
import { Model, Transactionable } from '@nocobase/database';
|
|
10
10
|
import { LoggerOptions } from '@nocobase/logger';
|
|
11
11
|
import type { TFuncKey, TOptions } from 'i18next';
|
|
12
12
|
import { Application } from './application';
|
|
13
13
|
import { InstallOptions } from './plugin-manager';
|
|
14
|
-
import { SyncMessageData } from './sync-manager';
|
|
15
14
|
export interface PluginInterface {
|
|
16
15
|
beforeLoad?: () => void;
|
|
17
16
|
load(): any;
|
|
@@ -66,16 +65,8 @@ export declare abstract class Plugin<O = any> implements PluginInterface {
|
|
|
66
65
|
afterDisable(): Promise<void>;
|
|
67
66
|
beforeRemove(): Promise<void>;
|
|
68
67
|
afterRemove(): Promise<void>;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
* @experimental
|
|
72
|
-
*/
|
|
73
|
-
onSync(message?: SyncMessageData): Promise<void> | void;
|
|
74
|
-
/**
|
|
75
|
-
* Publish a sync message.
|
|
76
|
-
* @experimental
|
|
77
|
-
*/
|
|
78
|
-
sync(message?: SyncMessageData): void;
|
|
68
|
+
handleSyncMessage(message: any): Promise<void>;
|
|
69
|
+
sendSyncMessage(message: any, options?: Transactionable): Promise<void>;
|
|
79
70
|
/**
|
|
80
71
|
* @deprecated
|
|
81
72
|
*/
|
package/lib/plugin.js
CHANGED
|
@@ -124,18 +124,13 @@ const _Plugin = class _Plugin {
|
|
|
124
124
|
}
|
|
125
125
|
async afterRemove() {
|
|
126
126
|
}
|
|
127
|
-
|
|
128
|
-
* Fired when a sync message is received.
|
|
129
|
-
* @experimental
|
|
130
|
-
*/
|
|
131
|
-
onSync(message = {}) {
|
|
127
|
+
async handleSyncMessage(message) {
|
|
132
128
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
this.app.syncManager.publish(this.name, message);
|
|
129
|
+
async sendSyncMessage(message, options) {
|
|
130
|
+
if (!this.name) {
|
|
131
|
+
throw new Error(`plugin name invalid`);
|
|
132
|
+
}
|
|
133
|
+
await this.app.syncMessageManager.publish(this.name, message, options);
|
|
139
134
|
}
|
|
140
135
|
/**
|
|
141
136
|
* @deprecated
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { type PubSubManagerSubscribeOptions } from './types';
|
|
10
|
+
export declare class HandlerManager {
|
|
11
|
+
protected publisherId: string;
|
|
12
|
+
handlers: Map<any, any>;
|
|
13
|
+
uniqueMessageHandlers: Map<any, any>;
|
|
14
|
+
constructor(publisherId: string);
|
|
15
|
+
protected getMessageHash(message: any): Promise<string>;
|
|
16
|
+
protected verifyMessage({ onlySelf, skipSelf, publisherId }: {
|
|
17
|
+
onlySelf: any;
|
|
18
|
+
skipSelf: any;
|
|
19
|
+
publisherId: any;
|
|
20
|
+
}): boolean;
|
|
21
|
+
protected debounce(func: any, wait: number): any;
|
|
22
|
+
handleMessage({ channel, message, callback, debounce }: {
|
|
23
|
+
channel: any;
|
|
24
|
+
message: any;
|
|
25
|
+
callback: any;
|
|
26
|
+
debounce: any;
|
|
27
|
+
}): Promise<void>;
|
|
28
|
+
wrapper(channel: any, callback: any, options: any): (wrappedMessage: any) => Promise<void>;
|
|
29
|
+
set(channel: string, callback: any, options: PubSubManagerSubscribeOptions): (wrappedMessage: any) => Promise<void>;
|
|
30
|
+
get(channel: string, callback: any): any;
|
|
31
|
+
delete(channel: string, callback: any): any;
|
|
32
|
+
reset(): void;
|
|
33
|
+
each(callback: any): Promise<void>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
30
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
31
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
32
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
33
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
34
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
35
|
+
mod
|
|
36
|
+
));
|
|
37
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
38
|
+
var handler_manager_exports = {};
|
|
39
|
+
__export(handler_manager_exports, {
|
|
40
|
+
HandlerManager: () => HandlerManager
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(handler_manager_exports);
|
|
43
|
+
var import_node_crypto = __toESM(require("node:crypto"));
|
|
44
|
+
var import_lodash = __toESM(require("lodash"));
|
|
45
|
+
const _HandlerManager = class _HandlerManager {
|
|
46
|
+
constructor(publisherId) {
|
|
47
|
+
this.publisherId = publisherId;
|
|
48
|
+
this.reset();
|
|
49
|
+
}
|
|
50
|
+
handlers;
|
|
51
|
+
uniqueMessageHandlers;
|
|
52
|
+
async getMessageHash(message) {
|
|
53
|
+
const encoder = new TextEncoder();
|
|
54
|
+
const data = encoder.encode(JSON.stringify(message));
|
|
55
|
+
const hashBuffer = await import_node_crypto.default.subtle.digest("SHA-256", data);
|
|
56
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
57
|
+
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
58
|
+
return hashHex;
|
|
59
|
+
}
|
|
60
|
+
verifyMessage({ onlySelf, skipSelf, publisherId }) {
|
|
61
|
+
if (onlySelf && publisherId !== this.publisherId) {
|
|
62
|
+
return;
|
|
63
|
+
} else if (!onlySelf && skipSelf && publisherId === this.publisherId) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
debounce(func, wait) {
|
|
69
|
+
if (wait) {
|
|
70
|
+
return import_lodash.default.debounce(func, wait);
|
|
71
|
+
}
|
|
72
|
+
return func;
|
|
73
|
+
}
|
|
74
|
+
async handleMessage({ channel, message, callback, debounce }) {
|
|
75
|
+
if (!debounce) {
|
|
76
|
+
await callback(message);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const messageHash = channel + await this.getMessageHash(message);
|
|
80
|
+
if (!this.uniqueMessageHandlers.has(messageHash)) {
|
|
81
|
+
this.uniqueMessageHandlers.set(messageHash, this.debounce(callback, debounce));
|
|
82
|
+
}
|
|
83
|
+
const handler = this.uniqueMessageHandlers.get(messageHash);
|
|
84
|
+
try {
|
|
85
|
+
await handler(message);
|
|
86
|
+
setTimeout(() => {
|
|
87
|
+
this.uniqueMessageHandlers.delete(messageHash);
|
|
88
|
+
}, debounce);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
this.uniqueMessageHandlers.delete(messageHash);
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
wrapper(channel, callback, options) {
|
|
95
|
+
const { debounce = 0 } = options;
|
|
96
|
+
return async (wrappedMessage) => {
|
|
97
|
+
const json = JSON.parse(wrappedMessage);
|
|
98
|
+
if (!this.verifyMessage(json)) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
await this.handleMessage({ channel, message: json.message, debounce, callback });
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
set(channel, callback, options) {
|
|
105
|
+
if (!this.handlers.has(channel)) {
|
|
106
|
+
this.handlers.set(channel, /* @__PURE__ */ new Map());
|
|
107
|
+
}
|
|
108
|
+
const headlerMap = this.handlers.get(channel);
|
|
109
|
+
const headler = this.wrapper(channel, callback, options);
|
|
110
|
+
headlerMap.set(callback, headler);
|
|
111
|
+
return headler;
|
|
112
|
+
}
|
|
113
|
+
get(channel, callback) {
|
|
114
|
+
const headlerMap = this.handlers.get(channel);
|
|
115
|
+
if (!headlerMap) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
return headlerMap.get(callback);
|
|
119
|
+
}
|
|
120
|
+
delete(channel, callback) {
|
|
121
|
+
if (!callback) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const headlerMap = this.handlers.get(channel);
|
|
125
|
+
if (!headlerMap) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const headler = headlerMap.get(callback);
|
|
129
|
+
headlerMap.delete(callback);
|
|
130
|
+
return headler;
|
|
131
|
+
}
|
|
132
|
+
reset() {
|
|
133
|
+
this.handlers = /* @__PURE__ */ new Map();
|
|
134
|
+
this.uniqueMessageHandlers = /* @__PURE__ */ new Map();
|
|
135
|
+
}
|
|
136
|
+
async each(callback) {
|
|
137
|
+
for (const [channel, headlerMap] of this.handlers) {
|
|
138
|
+
for (const headler of headlerMap.values()) {
|
|
139
|
+
await callback(channel, headler);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
__name(_HandlerManager, "HandlerManager");
|
|
145
|
+
let HandlerManager = _HandlerManager;
|
|
146
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
147
|
+
0 && (module.exports = {
|
|
148
|
+
HandlerManager
|
|
149
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
export * from './handler-manager';
|
|
10
|
+
export * from './pub-sub-manager';
|
|
11
|
+
export * from './types';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var pub_sub_manager_exports = {};
|
|
25
|
+
module.exports = __toCommonJS(pub_sub_manager_exports);
|
|
26
|
+
__reExport(pub_sub_manager_exports, require("./handler-manager"), module.exports);
|
|
27
|
+
__reExport(pub_sub_manager_exports, require("./pub-sub-manager"), module.exports);
|
|
28
|
+
__reExport(pub_sub_manager_exports, require("./types"), module.exports);
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
...require("./handler-manager"),
|
|
32
|
+
...require("./pub-sub-manager"),
|
|
33
|
+
...require("./types")
|
|
34
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import Application from '../application';
|
|
10
|
+
import { HandlerManager } from './handler-manager';
|
|
11
|
+
import { PubSubCallback, type IPubSubAdapter, type PubSubManagerOptions, type PubSubManagerPublishOptions, type PubSubManagerSubscribeOptions } from './types';
|
|
12
|
+
export declare const createPubSubManager: (app: Application, options: PubSubManagerOptions) => PubSubManager;
|
|
13
|
+
export declare class PubSubManager {
|
|
14
|
+
protected options: PubSubManagerOptions;
|
|
15
|
+
protected publisherId: string;
|
|
16
|
+
protected adapter: IPubSubAdapter;
|
|
17
|
+
protected handlerManager: HandlerManager;
|
|
18
|
+
constructor(options?: PubSubManagerOptions);
|
|
19
|
+
get channelPrefix(): string;
|
|
20
|
+
setAdapter(adapter: IPubSubAdapter): void;
|
|
21
|
+
isConnected(): Promise<boolean>;
|
|
22
|
+
connect(): Promise<void>;
|
|
23
|
+
close(): Promise<any>;
|
|
24
|
+
subscribe(channel: string, callback: PubSubCallback, options?: PubSubManagerSubscribeOptions): Promise<void>;
|
|
25
|
+
unsubscribe(channel: string, callback: PubSubCallback): Promise<any>;
|
|
26
|
+
publish(channel: string, message: any, options?: PubSubManagerPublishOptions): Promise<any>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
+
var __export = (target, all) => {
|
|
16
|
+
for (var name in all)
|
|
17
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var pub_sub_manager_exports = {};
|
|
29
|
+
__export(pub_sub_manager_exports, {
|
|
30
|
+
PubSubManager: () => PubSubManager,
|
|
31
|
+
createPubSubManager: () => createPubSubManager
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(pub_sub_manager_exports);
|
|
34
|
+
var import_utils = require("@nocobase/utils");
|
|
35
|
+
var import_handler_manager = require("./handler-manager");
|
|
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
|
+
});
|
|
44
|
+
return pubSubManager;
|
|
45
|
+
}, "createPubSubManager");
|
|
46
|
+
const _PubSubManager = class _PubSubManager {
|
|
47
|
+
constructor(options = {}) {
|
|
48
|
+
this.options = options;
|
|
49
|
+
this.publisherId = (0, import_utils.uid)();
|
|
50
|
+
this.handlerManager = new import_handler_manager.HandlerManager(this.publisherId);
|
|
51
|
+
}
|
|
52
|
+
publisherId;
|
|
53
|
+
adapter;
|
|
54
|
+
handlerManager;
|
|
55
|
+
get channelPrefix() {
|
|
56
|
+
var _a;
|
|
57
|
+
return ((_a = this.options) == null ? void 0 : _a.channelPrefix) ? `${this.options.channelPrefix}.` : "";
|
|
58
|
+
}
|
|
59
|
+
setAdapter(adapter) {
|
|
60
|
+
this.adapter = adapter;
|
|
61
|
+
}
|
|
62
|
+
async isConnected() {
|
|
63
|
+
if (this.adapter) {
|
|
64
|
+
return this.adapter.isConnected();
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
async connect() {
|
|
69
|
+
if (!this.adapter) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
await this.adapter.connect();
|
|
73
|
+
await this.handlerManager.each(async (channel, headler) => {
|
|
74
|
+
await this.adapter.subscribe(`${this.channelPrefix}${channel}`, headler);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
async close() {
|
|
78
|
+
if (!this.adapter) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
return await this.adapter.close();
|
|
82
|
+
}
|
|
83
|
+
async subscribe(channel, callback, options = {}) {
|
|
84
|
+
await this.unsubscribe(channel, callback);
|
|
85
|
+
const handler = this.handlerManager.set(channel, callback, options);
|
|
86
|
+
if (await this.isConnected()) {
|
|
87
|
+
await this.adapter.subscribe(`${this.channelPrefix}${channel}`, handler);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async unsubscribe(channel, callback) {
|
|
91
|
+
const handler = this.handlerManager.delete(channel, callback);
|
|
92
|
+
if (!this.adapter || !handler) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
return this.adapter.unsubscribe(`${this.channelPrefix}${channel}`, handler);
|
|
96
|
+
}
|
|
97
|
+
async publish(channel, message, options) {
|
|
98
|
+
var _a;
|
|
99
|
+
if (!((_a = this.adapter) == null ? void 0 : _a.isConnected())) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const wrappedMessage = JSON.stringify({
|
|
103
|
+
publisherId: this.publisherId,
|
|
104
|
+
...options,
|
|
105
|
+
message
|
|
106
|
+
});
|
|
107
|
+
return this.adapter.publish(`${this.channelPrefix}${channel}`, wrappedMessage);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
__name(_PubSubManager, "PubSubManager");
|
|
111
|
+
let PubSubManager = _PubSubManager;
|
|
112
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
113
|
+
0 && (module.exports = {
|
|
114
|
+
PubSubManager,
|
|
115
|
+
createPubSubManager
|
|
116
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
export interface PubSubManagerOptions {
|
|
10
|
+
channelPrefix?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface PubSubManagerPublishOptions {
|
|
13
|
+
skipSelf?: boolean;
|
|
14
|
+
onlySelf?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface PubSubManagerSubscribeOptions {
|
|
17
|
+
debounce?: number;
|
|
18
|
+
}
|
|
19
|
+
export type PubSubCallback = (message: any) => Promise<void>;
|
|
20
|
+
export interface IPubSubAdapter {
|
|
21
|
+
isConnected(): Promise<boolean> | boolean;
|
|
22
|
+
connect(): Promise<any>;
|
|
23
|
+
close(): Promise<any>;
|
|
24
|
+
subscribe(channel: string, callback: PubSubCallback): Promise<any>;
|
|
25
|
+
unsubscribe(channel: string, callback: PubSubCallback): Promise<any>;
|
|
26
|
+
publish(channel: string, message: string): Promise<any>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
|
+
var types_exports = {};
|
|
24
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
import { Transactionable } from '@nocobase/database';
|
|
10
|
+
import Application from './application';
|
|
11
|
+
import { PubSubCallback, PubSubManager, PubSubManagerPublishOptions } from './pub-sub-manager';
|
|
12
|
+
export declare class SyncMessageManager {
|
|
13
|
+
protected app: Application;
|
|
14
|
+
protected options: any;
|
|
15
|
+
protected versionManager: SyncMessageVersionManager;
|
|
16
|
+
protected pubSubManager: PubSubManager;
|
|
17
|
+
constructor(app: Application, options?: any);
|
|
18
|
+
get debounce(): any;
|
|
19
|
+
publish(channel: string, message: any, options?: PubSubManagerPublishOptions & Transactionable): Promise<any>;
|
|
20
|
+
subscribe(channel: string, callback: PubSubCallback): Promise<void>;
|
|
21
|
+
unsubscribe(channel: string, callback: PubSubCallback): Promise<any>;
|
|
22
|
+
sync(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export declare class SyncMessageVersionManager {
|
|
25
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
15
|
+
var __export = (target, all) => {
|
|
16
|
+
for (var name in all)
|
|
17
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
18
|
+
};
|
|
19
|
+
var __copyProps = (to, from, except, desc) => {
|
|
20
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
21
|
+
for (let key of __getOwnPropNames(from))
|
|
22
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
23
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
24
|
+
}
|
|
25
|
+
return to;
|
|
26
|
+
};
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var sync_message_manager_exports = {};
|
|
29
|
+
__export(sync_message_manager_exports, {
|
|
30
|
+
SyncMessageManager: () => SyncMessageManager,
|
|
31
|
+
SyncMessageVersionManager: () => SyncMessageVersionManager
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(sync_message_manager_exports);
|
|
34
|
+
const _SyncMessageManager = class _SyncMessageManager {
|
|
35
|
+
constructor(app, options = {}) {
|
|
36
|
+
this.app = app;
|
|
37
|
+
this.options = options;
|
|
38
|
+
this.versionManager = new SyncMessageVersionManager();
|
|
39
|
+
app.on("beforeLoadPlugin", async (plugin) => {
|
|
40
|
+
if (!plugin.name) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
await this.subscribe(plugin.name, plugin.handleSyncMessage.bind(plugin));
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
versionManager;
|
|
47
|
+
pubSubManager;
|
|
48
|
+
get debounce() {
|
|
49
|
+
return this.options.debounce || 1e3;
|
|
50
|
+
}
|
|
51
|
+
async publish(channel, message, options) {
|
|
52
|
+
const { transaction, ...others } = options || {};
|
|
53
|
+
if (transaction) {
|
|
54
|
+
return await new Promise((resolve, reject) => {
|
|
55
|
+
const timer = setTimeout(() => {
|
|
56
|
+
reject(
|
|
57
|
+
new Error(
|
|
58
|
+
`Publish message to ${channel} timeout, channel: ${channel}, message: ${JSON.stringify(message)}`
|
|
59
|
+
)
|
|
60
|
+
);
|
|
61
|
+
}, 5e4);
|
|
62
|
+
transaction.afterCommit(async () => {
|
|
63
|
+
try {
|
|
64
|
+
const r = await this.app.pubSubManager.publish(`${this.app.name}.sync.${channel}`, message, {
|
|
65
|
+
skipSelf: true,
|
|
66
|
+
...others
|
|
67
|
+
});
|
|
68
|
+
resolve(r);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
reject(error);
|
|
71
|
+
} finally {
|
|
72
|
+
clearTimeout(timer);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
} else {
|
|
77
|
+
return await this.app.pubSubManager.publish(`${this.app.name}.sync.${channel}`, message, {
|
|
78
|
+
skipSelf: true,
|
|
79
|
+
...options
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async subscribe(channel, callback) {
|
|
84
|
+
return await this.app.pubSubManager.subscribe(`${this.app.name}.sync.${channel}`, callback, {
|
|
85
|
+
debounce: this.debounce
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
async unsubscribe(channel, callback) {
|
|
89
|
+
return this.app.pubSubManager.unsubscribe(`${this.app.name}.sync.${channel}`, callback);
|
|
90
|
+
}
|
|
91
|
+
async sync() {
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
__name(_SyncMessageManager, "SyncMessageManager");
|
|
95
|
+
let SyncMessageManager = _SyncMessageManager;
|
|
96
|
+
const _SyncMessageVersionManager = class _SyncMessageVersionManager {
|
|
97
|
+
// TODO
|
|
98
|
+
};
|
|
99
|
+
__name(_SyncMessageVersionManager, "SyncMessageVersionManager");
|
|
100
|
+
let SyncMessageVersionManager = _SyncMessageVersionManager;
|
|
101
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
102
|
+
0 && (module.exports = {
|
|
103
|
+
SyncMessageManager,
|
|
104
|
+
SyncMessageVersionManager
|
|
105
|
+
});
|