@tego/core 1.3.44
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/LICENSE +201 -0
- package/lib/acl/available-action.d.ts +5 -0
- package/lib/acl/available-action.js +62 -0
- package/lib/acl/index.d.ts +2 -0
- package/lib/acl/index.js +49 -0
- package/lib/aes-encryptor.d.ts +11 -0
- package/lib/aes-encryptor.js +116 -0
- package/lib/app-command.d.ts +12 -0
- package/lib/app-command.js +76 -0
- package/lib/app-supervisor.d.ts +61 -0
- package/lib/app-supervisor.js +299 -0
- package/lib/application.d.ts +354 -0
- package/lib/application.js +878 -0
- package/lib/cache/index.d.ts +3 -0
- package/lib/cache/index.js +35 -0
- package/lib/commands/console.d.ts +3 -0
- package/lib/commands/console.js +48 -0
- package/lib/commands/create-migration.d.ts +3 -0
- package/lib/commands/create-migration.js +80 -0
- package/lib/commands/db-auth.d.ts +3 -0
- package/lib/commands/db-auth.js +28 -0
- package/lib/commands/db-clean.d.ts +3 -0
- package/lib/commands/db-clean.js +31 -0
- package/lib/commands/db-sync.d.ts +3 -0
- package/lib/commands/db-sync.js +40 -0
- package/lib/commands/destroy.d.ts +3 -0
- package/lib/commands/destroy.js +30 -0
- package/lib/commands/index.d.ts +2 -0
- package/lib/commands/index.js +71 -0
- package/lib/commands/install.d.ts +3 -0
- package/lib/commands/install.js +33 -0
- package/lib/commands/migrator.d.ts +3 -0
- package/lib/commands/migrator.js +31 -0
- package/lib/commands/pm.d.ts +3 -0
- package/lib/commands/pm.js +75 -0
- package/lib/commands/refresh.d.ts +3 -0
- package/lib/commands/refresh.js +31 -0
- package/lib/commands/restart.d.ts +3 -0
- package/lib/commands/restart.js +35 -0
- package/lib/commands/start.d.ts +3 -0
- package/lib/commands/start.js +74 -0
- package/lib/commands/stop.d.ts +3 -0
- package/lib/commands/stop.js +34 -0
- package/lib/commands/upgrade.d.ts +3 -0
- package/lib/commands/upgrade.js +29 -0
- package/lib/cron/cron-job-manager.d.ts +25 -0
- package/lib/cron/cron-job-manager.js +89 -0
- package/lib/environment.d.ts +10 -0
- package/lib/environment.js +73 -0
- package/lib/errors/application-not-install.d.ts +4 -0
- package/lib/errors/application-not-install.js +35 -0
- package/lib/errors/handler.d.ts +3 -0
- package/lib/errors/handler.js +41 -0
- package/lib/errors/plugin-command-error.d.ts +2 -0
- package/lib/errors/plugin-command-error.js +31 -0
- package/lib/errors/plugin-not-exist.d.ts +2 -0
- package/lib/errors/plugin-not-exist.js +31 -0
- package/lib/gateway/errors.d.ts +22 -0
- package/lib/gateway/errors.js +138 -0
- package/lib/gateway/gateway.d.ts +56 -0
- package/lib/gateway/gateway.js +408 -0
- package/lib/gateway/index.d.ts +2 -0
- package/lib/gateway/index.js +30 -0
- package/lib/gateway/ipc-socket-client.d.ts +17 -0
- package/lib/gateway/ipc-socket-client.js +99 -0
- package/lib/gateway/ipc-socket-server.d.ts +12 -0
- package/lib/gateway/ipc-socket-server.js +140 -0
- package/lib/gateway/types.d.ts +25 -0
- package/lib/gateway/types.js +15 -0
- package/lib/gateway/ws-server.d.ts +63 -0
- package/lib/gateway/ws-server.js +335 -0
- package/lib/helper.d.ts +11 -0
- package/lib/helper.js +193 -0
- package/lib/helpers/application-version.d.ts +10 -0
- package/lib/helpers/application-version.js +77 -0
- package/lib/helpers/multiple-instance-manager.d.ts +6 -0
- package/lib/helpers/multiple-instance-manager.js +40 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +57 -0
- package/lib/locale/index.d.ts +1 -0
- package/lib/locale/index.js +21 -0
- package/lib/locale/locale.d.ts +22 -0
- package/lib/locale/locale.js +166 -0
- package/lib/locale/resource.d.ts +1 -0
- package/lib/locale/resource.js +74 -0
- package/lib/main-data-source.d.ts +4 -0
- package/lib/main-data-source.js +45 -0
- package/lib/middlewares/data-template.d.ts +2 -0
- package/lib/middlewares/data-template.js +39 -0
- package/lib/middlewares/data-wrapping.d.ts +3 -0
- package/lib/middlewares/data-wrapping.js +90 -0
- package/lib/middlewares/db2resource.d.ts +6 -0
- package/lib/middlewares/db2resource.js +76 -0
- package/lib/middlewares/extract-client-ip.d.ts +2 -0
- package/lib/middlewares/extract-client-ip.js +37 -0
- package/lib/middlewares/i18n.d.ts +1 -0
- package/lib/middlewares/i18n.js +44 -0
- package/lib/middlewares/index.d.ts +3 -0
- package/lib/middlewares/index.js +32 -0
- package/lib/middlewares/parse-variables.d.ts +1 -0
- package/lib/middlewares/parse-variables.js +84 -0
- package/lib/migration.d.ts +12 -0
- package/lib/migration.js +47 -0
- package/lib/migrations/20230912193824-package-name-unique.d.ts +6 -0
- package/lib/migrations/20230912193824-package-name-unique.js +52 -0
- package/lib/migrations/20230912294620-update-pkg.d.ts +6 -0
- package/lib/migrations/20230912294620-update-pkg.js +58 -0
- package/lib/migrations/20240106082756-update-plugins.d.ts +6 -0
- package/lib/migrations/20240106082756-update-plugins.js +44 -0
- package/lib/migrations/20240705000001-remove-pkgs-approval.d.ts +6 -0
- package/lib/migrations/20240705000001-remove-pkgs-approval.js +47 -0
- package/lib/notice/index.d.ts +25 -0
- package/lib/notice/index.js +91 -0
- package/lib/plugin-manager/clientStaticUtils.d.ts +24 -0
- package/lib/plugin-manager/clientStaticUtils.js +156 -0
- package/lib/plugin-manager/constants.d.ts +7 -0
- package/lib/plugin-manager/constants.js +120 -0
- package/lib/plugin-manager/deps.d.ts +2 -0
- package/lib/plugin-manager/deps.js +68 -0
- package/lib/plugin-manager/index.d.ts +2 -0
- package/lib/plugin-manager/index.js +23 -0
- package/lib/plugin-manager/middleware.d.ts +2 -0
- package/lib/plugin-manager/middleware.js +36 -0
- package/lib/plugin-manager/options/collection.d.ts +2 -0
- package/lib/plugin-manager/options/collection.js +39 -0
- package/lib/plugin-manager/options/resource.d.ts +15 -0
- package/lib/plugin-manager/options/resource.js +185 -0
- package/lib/plugin-manager/plugin-manager-repository.d.ts +27 -0
- package/lib/plugin-manager/plugin-manager-repository.js +136 -0
- package/lib/plugin-manager/plugin-manager.d.ts +205 -0
- package/lib/plugin-manager/plugin-manager.js +1032 -0
- package/lib/plugin-manager/types.d.ts +15 -0
- package/lib/plugin-manager/types.js +15 -0
- package/lib/plugin-manager/utils.d.ts +101 -0
- package/lib/plugin-manager/utils.js +513 -0
- package/lib/plugin-manager/web-service.d.ts +6 -0
- package/lib/plugin-manager/web-service.js +116 -0
- package/lib/plugin.d.ts +125 -0
- package/lib/plugin.js +359 -0
- package/lib/pub-sub-manager/handler-manager.d.ts +28 -0
- package/lib/pub-sub-manager/handler-manager.js +149 -0
- package/lib/pub-sub-manager/index.d.ts +3 -0
- package/lib/pub-sub-manager/index.js +25 -0
- package/lib/pub-sub-manager/memory-pub-sub-adapter.d.ts +21 -0
- package/lib/pub-sub-manager/memory-pub-sub-adapter.js +82 -0
- package/lib/pub-sub-manager/pub-sub-manager.d.ts +19 -0
- package/lib/pub-sub-manager/pub-sub-manager.js +108 -0
- package/lib/pub-sub-manager/types.d.ts +20 -0
- package/lib/pub-sub-manager/types.js +15 -0
- package/lib/sync-message-manager.d.ts +16 -0
- package/lib/sync-message-manager.js +112 -0
- package/package.json +58 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var handler_manager_exports = {};
|
|
30
|
+
__export(handler_manager_exports, {
|
|
31
|
+
HandlerManager: () => HandlerManager
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(handler_manager_exports);
|
|
34
|
+
var import_node_crypto = __toESM(require("node:crypto"));
|
|
35
|
+
var import_lodash = __toESM(require("lodash"));
|
|
36
|
+
const _HandlerManager = class _HandlerManager {
|
|
37
|
+
constructor(publisherId) {
|
|
38
|
+
this.publisherId = publisherId;
|
|
39
|
+
this.reset();
|
|
40
|
+
}
|
|
41
|
+
async getMessageHash(message) {
|
|
42
|
+
const encoder = new TextEncoder();
|
|
43
|
+
const data = encoder.encode(JSON.stringify(message));
|
|
44
|
+
const hashBuffer = await import_node_crypto.default.subtle.digest("SHA-256", data);
|
|
45
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
46
|
+
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
47
|
+
return hashHex;
|
|
48
|
+
}
|
|
49
|
+
verifyMessage({ onlySelf, skipSelf, publisherId }) {
|
|
50
|
+
if (onlySelf && publisherId !== this.publisherId) {
|
|
51
|
+
return;
|
|
52
|
+
} else if (!onlySelf && skipSelf && publisherId === this.publisherId) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
debounce(func, wait) {
|
|
58
|
+
if (wait) {
|
|
59
|
+
return import_lodash.default.debounce(func, wait);
|
|
60
|
+
}
|
|
61
|
+
return func;
|
|
62
|
+
}
|
|
63
|
+
async handleMessage({
|
|
64
|
+
channel,
|
|
65
|
+
message,
|
|
66
|
+
callback,
|
|
67
|
+
debounce,
|
|
68
|
+
callbackCaller
|
|
69
|
+
}) {
|
|
70
|
+
if (!debounce) {
|
|
71
|
+
await callback.bind(callbackCaller)(message);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const messageHash = channel + await this.getMessageHash(message);
|
|
75
|
+
if (!this.uniqueMessageHandlers.has(messageHash)) {
|
|
76
|
+
this.uniqueMessageHandlers.set(messageHash, this.debounce(callback.bind(callbackCaller), debounce));
|
|
77
|
+
}
|
|
78
|
+
const handler = this.uniqueMessageHandlers.get(messageHash);
|
|
79
|
+
try {
|
|
80
|
+
await handler(message);
|
|
81
|
+
setTimeout(() => {
|
|
82
|
+
this.uniqueMessageHandlers.delete(messageHash);
|
|
83
|
+
}, debounce);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
this.uniqueMessageHandlers.delete(messageHash);
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
wrapper(channel, callback, options) {
|
|
90
|
+
const { debounce = 0, callbackCaller } = options;
|
|
91
|
+
return async (wrappedMessage) => {
|
|
92
|
+
let json;
|
|
93
|
+
if (this.adapterType !== "MemoryPubSubAdapter") {
|
|
94
|
+
json = JSON.parse(wrappedMessage);
|
|
95
|
+
} else {
|
|
96
|
+
json = wrappedMessage;
|
|
97
|
+
}
|
|
98
|
+
if (!this.verifyMessage(json)) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
await this.handleMessage({ channel, message: json.message, debounce, callback, callbackCaller });
|
|
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,25 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var pub_sub_manager_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(pub_sub_manager_exports);
|
|
17
|
+
__reExport(pub_sub_manager_exports, require("./handler-manager"), module.exports);
|
|
18
|
+
__reExport(pub_sub_manager_exports, require("./pub-sub-manager"), module.exports);
|
|
19
|
+
__reExport(pub_sub_manager_exports, require("./types"), module.exports);
|
|
20
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
21
|
+
0 && (module.exports = {
|
|
22
|
+
...require("./handler-manager"),
|
|
23
|
+
...require("./pub-sub-manager"),
|
|
24
|
+
...require("./types")
|
|
25
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { IPubSubAdapter } from './types';
|
|
3
|
+
declare class TestEventEmitter extends EventEmitter {
|
|
4
|
+
emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
|
5
|
+
}
|
|
6
|
+
export declare class MemoryPubSubAdapter implements IPubSubAdapter {
|
|
7
|
+
protected options: any;
|
|
8
|
+
protected emitter: TestEventEmitter;
|
|
9
|
+
connected: boolean;
|
|
10
|
+
static instances: Map<string, MemoryPubSubAdapter>;
|
|
11
|
+
static create(name?: string, options?: any): MemoryPubSubAdapter;
|
|
12
|
+
constructor(options?: any);
|
|
13
|
+
connect(): Promise<void>;
|
|
14
|
+
close(): Promise<void>;
|
|
15
|
+
isConnected(): Promise<boolean>;
|
|
16
|
+
subscribe(channel: any, callback: any): Promise<void>;
|
|
17
|
+
unsubscribe(channel: any, callback: any): Promise<void>;
|
|
18
|
+
publish(channel: any, message: any): Promise<void>;
|
|
19
|
+
subscribeAll(callback: any): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var memory_pub_sub_adapter_exports = {};
|
|
20
|
+
__export(memory_pub_sub_adapter_exports, {
|
|
21
|
+
MemoryPubSubAdapter: () => MemoryPubSubAdapter
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(memory_pub_sub_adapter_exports);
|
|
24
|
+
var import_node_events = require("node:events");
|
|
25
|
+
var import_utils = require("@tachybase/utils");
|
|
26
|
+
const sleep = /* @__PURE__ */ __name((ms) => new Promise((resolve) => setTimeout(resolve, ms)), "sleep");
|
|
27
|
+
const _TestEventEmitter = class _TestEventEmitter extends import_node_events.EventEmitter {
|
|
28
|
+
};
|
|
29
|
+
__name(_TestEventEmitter, "TestEventEmitter");
|
|
30
|
+
let TestEventEmitter = _TestEventEmitter;
|
|
31
|
+
(0, import_utils.applyMixins)(TestEventEmitter, [import_utils.AsyncEmitter]);
|
|
32
|
+
const _MemoryPubSubAdapter = class _MemoryPubSubAdapter {
|
|
33
|
+
constructor(options = {}) {
|
|
34
|
+
this.options = options;
|
|
35
|
+
this.connected = false;
|
|
36
|
+
this.emitter = new TestEventEmitter();
|
|
37
|
+
}
|
|
38
|
+
static create(name, options) {
|
|
39
|
+
if (!name) {
|
|
40
|
+
name = (0, import_utils.uid)();
|
|
41
|
+
}
|
|
42
|
+
if (!this.instances.has(name)) {
|
|
43
|
+
this.instances.set(name, new _MemoryPubSubAdapter(options));
|
|
44
|
+
}
|
|
45
|
+
return this.instances.get(name);
|
|
46
|
+
}
|
|
47
|
+
async connect() {
|
|
48
|
+
this.connected = true;
|
|
49
|
+
}
|
|
50
|
+
async close() {
|
|
51
|
+
this.connected = false;
|
|
52
|
+
}
|
|
53
|
+
async isConnected() {
|
|
54
|
+
return this.connected;
|
|
55
|
+
}
|
|
56
|
+
async subscribe(channel, callback) {
|
|
57
|
+
this.emitter.on(channel, callback);
|
|
58
|
+
}
|
|
59
|
+
async unsubscribe(channel, callback) {
|
|
60
|
+
this.emitter.off(channel, callback);
|
|
61
|
+
}
|
|
62
|
+
async publish(channel, message) {
|
|
63
|
+
if (!this.connected) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
await this.emitter.emitAsync(channel, message);
|
|
67
|
+
await this.emitter.emitAsync("__publish__", channel, message);
|
|
68
|
+
if (this.options.debounce) {
|
|
69
|
+
await sleep(Number(this.options.debounce));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async subscribeAll(callback) {
|
|
73
|
+
this.emitter.on("__publish__", callback);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
__name(_MemoryPubSubAdapter, "MemoryPubSubAdapter");
|
|
77
|
+
_MemoryPubSubAdapter.instances = /* @__PURE__ */ new Map();
|
|
78
|
+
let MemoryPubSubAdapter = _MemoryPubSubAdapter;
|
|
79
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
80
|
+
0 && (module.exports = {
|
|
81
|
+
MemoryPubSubAdapter
|
|
82
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import Application from '../application';
|
|
2
|
+
import { HandlerManager } from './handler-manager';
|
|
3
|
+
import { PubSubCallback, type IPubSubAdapter, type PubSubManagerOptions, type PubSubManagerPublishOptions, type PubSubManagerSubscribeOptions } from './types';
|
|
4
|
+
export declare const createPubSubManager: (app: Application, options: PubSubManagerOptions) => PubSubManager;
|
|
5
|
+
export declare class PubSubManager {
|
|
6
|
+
protected options: PubSubManagerOptions;
|
|
7
|
+
protected publisherId: string;
|
|
8
|
+
adapter: IPubSubAdapter;
|
|
9
|
+
protected handlerManager: HandlerManager;
|
|
10
|
+
constructor(options?: PubSubManagerOptions);
|
|
11
|
+
get channelPrefix(): string;
|
|
12
|
+
setAdapter(adapter: IPubSubAdapter): void;
|
|
13
|
+
isConnected(): Promise<boolean>;
|
|
14
|
+
connect(): Promise<void>;
|
|
15
|
+
close(): Promise<any>;
|
|
16
|
+
subscribe(channel: string, callback: PubSubCallback, options?: PubSubManagerSubscribeOptions): Promise<void>;
|
|
17
|
+
unsubscribe(channel: string, callback: PubSubCallback): Promise<any>;
|
|
18
|
+
publish(channel: string, message: any, options?: PubSubManagerPublishOptions): Promise<any>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var pub_sub_manager_exports = {};
|
|
20
|
+
__export(pub_sub_manager_exports, {
|
|
21
|
+
PubSubManager: () => PubSubManager,
|
|
22
|
+
createPubSubManager: () => createPubSubManager
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(pub_sub_manager_exports);
|
|
25
|
+
var import_utils = require("@tachybase/utils");
|
|
26
|
+
var import_handler_manager = require("./handler-manager");
|
|
27
|
+
var import_memory_pub_sub_adapter = require("./memory-pub-sub-adapter");
|
|
28
|
+
const createPubSubManager = /* @__PURE__ */ __name((app, options) => {
|
|
29
|
+
const pubSubManager = new PubSubManager(options);
|
|
30
|
+
pubSubManager.setAdapter(import_memory_pub_sub_adapter.MemoryPubSubAdapter.create());
|
|
31
|
+
app.on("afterStart", async () => {
|
|
32
|
+
await pubSubManager.connect();
|
|
33
|
+
});
|
|
34
|
+
app.on("afterStop", async () => {
|
|
35
|
+
await pubSubManager.close();
|
|
36
|
+
});
|
|
37
|
+
return pubSubManager;
|
|
38
|
+
}, "createPubSubManager");
|
|
39
|
+
const _PubSubManager = class _PubSubManager {
|
|
40
|
+
constructor(options = {}) {
|
|
41
|
+
this.options = options;
|
|
42
|
+
this.publisherId = (0, import_utils.uid)();
|
|
43
|
+
this.handlerManager = new import_handler_manager.HandlerManager(this.publisherId);
|
|
44
|
+
}
|
|
45
|
+
get channelPrefix() {
|
|
46
|
+
var _a;
|
|
47
|
+
return ((_a = this.options) == null ? void 0 : _a.channelPrefix) ? `${this.options.channelPrefix}.` : "";
|
|
48
|
+
}
|
|
49
|
+
setAdapter(adapter) {
|
|
50
|
+
this.adapter = adapter;
|
|
51
|
+
this.handlerManager.adapterType = adapter.constructor.name;
|
|
52
|
+
}
|
|
53
|
+
async isConnected() {
|
|
54
|
+
if (this.adapter) {
|
|
55
|
+
return this.adapter.isConnected();
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
async connect() {
|
|
60
|
+
if (!this.adapter) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
await this.adapter.connect();
|
|
64
|
+
await this.handlerManager.each(async (channel, headler) => {
|
|
65
|
+
await this.adapter.subscribe(`${this.channelPrefix}${channel}`, headler);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
async close() {
|
|
69
|
+
if (!this.adapter) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
return await this.adapter.close();
|
|
73
|
+
}
|
|
74
|
+
async subscribe(channel, callback, options = {}) {
|
|
75
|
+
await this.unsubscribe(channel, callback);
|
|
76
|
+
const handler = this.handlerManager.set(channel, callback, options);
|
|
77
|
+
if (await this.isConnected()) {
|
|
78
|
+
await this.adapter.subscribe(`${this.channelPrefix}${channel}`, handler);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async unsubscribe(channel, callback) {
|
|
82
|
+
const handler = this.handlerManager.delete(channel, callback);
|
|
83
|
+
if (!this.adapter || !handler) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
return this.adapter.unsubscribe(`${this.channelPrefix}${channel}`, handler);
|
|
87
|
+
}
|
|
88
|
+
async publish(channel, message, options) {
|
|
89
|
+
var _a;
|
|
90
|
+
if (!((_a = this.adapter) == null ? void 0 : _a.isConnected())) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const messageRow = {
|
|
94
|
+
publisherId: this.publisherId,
|
|
95
|
+
...options,
|
|
96
|
+
message
|
|
97
|
+
};
|
|
98
|
+
const wrappedMessage = this.adapter.constructor.name === "MemoryPubSubAdapter" ? messageRow : JSON.stringify(messageRow);
|
|
99
|
+
return this.adapter.publish(`${this.channelPrefix}${channel}`, wrappedMessage);
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
__name(_PubSubManager, "PubSubManager");
|
|
103
|
+
let PubSubManager = _PubSubManager;
|
|
104
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
105
|
+
0 && (module.exports = {
|
|
106
|
+
PubSubManager,
|
|
107
|
+
createPubSubManager
|
|
108
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface PubSubManagerOptions {
|
|
2
|
+
channelPrefix?: string;
|
|
3
|
+
}
|
|
4
|
+
export interface PubSubManagerPublishOptions {
|
|
5
|
+
skipSelf?: boolean;
|
|
6
|
+
onlySelf?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface PubSubManagerSubscribeOptions {
|
|
9
|
+
debounce?: number;
|
|
10
|
+
callbackCaller?: any;
|
|
11
|
+
}
|
|
12
|
+
export type PubSubCallback = (message: any) => Promise<void>;
|
|
13
|
+
export interface IPubSubAdapter {
|
|
14
|
+
isConnected(): Promise<boolean> | boolean;
|
|
15
|
+
connect(): Promise<any>;
|
|
16
|
+
close(): Promise<any>;
|
|
17
|
+
subscribe(channel: string, callback: PubSubCallback): Promise<any>;
|
|
18
|
+
unsubscribe(channel: string, callback: PubSubCallback): Promise<any>;
|
|
19
|
+
publish(channel: string, message: string | object): Promise<any>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var types_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(types_exports);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Transactionable } from '@tachybase/database';
|
|
2
|
+
import Application from './application';
|
|
3
|
+
import { PubSubCallback, PubSubManagerPublishOptions } from './pub-sub-manager';
|
|
4
|
+
export declare class SyncMessageManager {
|
|
5
|
+
protected app: Application;
|
|
6
|
+
protected options: any;
|
|
7
|
+
protected versionManager: SyncMessageVersionManager;
|
|
8
|
+
constructor(app: Application, options?: any);
|
|
9
|
+
get debounce(): any;
|
|
10
|
+
publish(channel: string, message: any, options?: PubSubManagerPublishOptions & Transactionable): Promise<any>;
|
|
11
|
+
subscribe(channel: string, callback: PubSubCallback, callbackCaller: any): Promise<void>;
|
|
12
|
+
unsubscribe(channel: string, callback: PubSubCallback): Promise<any>;
|
|
13
|
+
sync(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export declare class SyncMessageVersionManager {
|
|
16
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var sync_message_manager_exports = {};
|
|
20
|
+
__export(sync_message_manager_exports, {
|
|
21
|
+
SyncMessageManager: () => SyncMessageManager,
|
|
22
|
+
SyncMessageVersionManager: () => SyncMessageVersionManager
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(sync_message_manager_exports);
|
|
25
|
+
const _SyncMessageManager = class _SyncMessageManager {
|
|
26
|
+
// protected pubSubManager: PubSubManager;
|
|
27
|
+
constructor(app, options = {}) {
|
|
28
|
+
this.app = app;
|
|
29
|
+
this.options = options;
|
|
30
|
+
this.versionManager = new SyncMessageVersionManager();
|
|
31
|
+
app.on("beforeLoadPlugin", async (plugin) => {
|
|
32
|
+
if (!plugin.name) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
await this.subscribe(plugin.name, plugin.handleSyncMessage, plugin);
|
|
36
|
+
});
|
|
37
|
+
app.on("beforeStop", async () => {
|
|
38
|
+
const promises = [];
|
|
39
|
+
for (const [P, plugin] of app.pm.getPlugins()) {
|
|
40
|
+
if (!plugin.name) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
promises.push(this.unsubscribe(plugin.name, plugin.handleSyncMessage));
|
|
44
|
+
}
|
|
45
|
+
await Promise.all(promises);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
get debounce() {
|
|
49
|
+
let defaultDebounce;
|
|
50
|
+
if (this.app.pubSubManager.adapter.constructor.name === "MemoryPubSubAdapter") {
|
|
51
|
+
defaultDebounce = 0;
|
|
52
|
+
} else {
|
|
53
|
+
defaultDebounce = 1e3;
|
|
54
|
+
}
|
|
55
|
+
return this.options.debounce || defaultDebounce;
|
|
56
|
+
}
|
|
57
|
+
async publish(channel, message, options) {
|
|
58
|
+
const { transaction, ...others } = options || {};
|
|
59
|
+
if (transaction) {
|
|
60
|
+
return await new Promise((resolve, reject) => {
|
|
61
|
+
const timer = setTimeout(() => {
|
|
62
|
+
reject(
|
|
63
|
+
new Error(
|
|
64
|
+
`Publish message to ${channel} timeout, channel: ${channel}, message: ${JSON.stringify(message)}`
|
|
65
|
+
)
|
|
66
|
+
);
|
|
67
|
+
}, 5e4);
|
|
68
|
+
transaction.afterCommit(async () => {
|
|
69
|
+
try {
|
|
70
|
+
const r = await this.app.pubSubManager.publish(`${this.app.name}.sync.${channel}`, message, {
|
|
71
|
+
skipSelf: true,
|
|
72
|
+
...others
|
|
73
|
+
});
|
|
74
|
+
resolve(r);
|
|
75
|
+
} catch (error) {
|
|
76
|
+
reject(error);
|
|
77
|
+
} finally {
|
|
78
|
+
clearTimeout(timer);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
} else {
|
|
83
|
+
return await this.app.pubSubManager.publish(`${this.app.name}.sync.${channel}`, message, {
|
|
84
|
+
skipSelf: true,
|
|
85
|
+
...options
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async subscribe(channel, callback, callbackCaller) {
|
|
90
|
+
return await this.app.pubSubManager.subscribe(`${this.app.name}.sync.${channel}`, callback, {
|
|
91
|
+
debounce: this.debounce,
|
|
92
|
+
callbackCaller
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async unsubscribe(channel, callback) {
|
|
96
|
+
return this.app.pubSubManager.unsubscribe(`${this.app.name}.sync.${channel}`, callback);
|
|
97
|
+
}
|
|
98
|
+
async sync() {
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
__name(_SyncMessageManager, "SyncMessageManager");
|
|
102
|
+
let SyncMessageManager = _SyncMessageManager;
|
|
103
|
+
const _SyncMessageVersionManager = class _SyncMessageVersionManager {
|
|
104
|
+
// TODO
|
|
105
|
+
};
|
|
106
|
+
__name(_SyncMessageVersionManager, "SyncMessageVersionManager");
|
|
107
|
+
let SyncMessageVersionManager = _SyncMessageVersionManager;
|
|
108
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
109
|
+
0 && (module.exports = {
|
|
110
|
+
SyncMessageManager,
|
|
111
|
+
SyncMessageVersionManager
|
|
112
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tego/core",
|
|
3
|
+
"version": "1.3.44",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "./lib/index.d.ts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"@koa/cors": "^5.0.0",
|
|
9
|
+
"@types/decompress": "4.2.7",
|
|
10
|
+
"@types/ini": "^1.3.34",
|
|
11
|
+
"async-mutex": "^0.5.0",
|
|
12
|
+
"axios": "0.29.0",
|
|
13
|
+
"commander": "^9.5.0",
|
|
14
|
+
"compression": "^1.7.5",
|
|
15
|
+
"dayjs": "1.11.13",
|
|
16
|
+
"decompress": "4.2.1",
|
|
17
|
+
"execa": "^5.1.1",
|
|
18
|
+
"fast-glob": "^3.3.2",
|
|
19
|
+
"fs-extra": "^11.3.0",
|
|
20
|
+
"glob": "11.0.0",
|
|
21
|
+
"i18next": "23.16.8",
|
|
22
|
+
"ini": "^4.1.3",
|
|
23
|
+
"koa": "^2.15.3",
|
|
24
|
+
"koa-bodyparser": "^4.4.1",
|
|
25
|
+
"koa-compose": "^4.1.0",
|
|
26
|
+
"lodash": "^4.17.21",
|
|
27
|
+
"nanoid": "3.3.8",
|
|
28
|
+
"qs": "^6.14.0",
|
|
29
|
+
"semver": "7.7.2",
|
|
30
|
+
"serve-handler": "^6.1.6",
|
|
31
|
+
"winston": "^3.17.0",
|
|
32
|
+
"ws": "^8.18.0",
|
|
33
|
+
"xpipe": "^1.0.8",
|
|
34
|
+
"@tachybase/acl": "1.3.44",
|
|
35
|
+
"@tachybase/auth": "1.3.44",
|
|
36
|
+
"@tachybase/actions": "1.3.44",
|
|
37
|
+
"@tachybase/database": "1.3.44",
|
|
38
|
+
"@tachybase/data-source": "1.3.44",
|
|
39
|
+
"@tachybase/cache": "1.3.44",
|
|
40
|
+
"@tachybase/di": "1.3.44",
|
|
41
|
+
"@tachybase/globals": "1.3.44",
|
|
42
|
+
"@tachybase/resourcer": "1.3.44",
|
|
43
|
+
"@tachybase/logger": "1.3.44",
|
|
44
|
+
"@tachybase/utils": "1.3.44"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/fs-extra": "11.0.4",
|
|
48
|
+
"@types/koa": "^2.15.0",
|
|
49
|
+
"@types/koa__cors": "^5.0.0",
|
|
50
|
+
"@types/koa-bodyparser": "^4.3.12",
|
|
51
|
+
"@types/koa-compose": "3.2.8",
|
|
52
|
+
"@types/lodash": "4.17.20",
|
|
53
|
+
"@types/qs": "^6.14.0",
|
|
54
|
+
"@types/semver": "^7.5.8",
|
|
55
|
+
"@types/serve-handler": "^6.1.4",
|
|
56
|
+
"@types/ws": "^8.5.13"
|
|
57
|
+
}
|
|
58
|
+
}
|