@nocobase/server 2.0.0-alpha.7 → 2.0.0-alpha.71
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/acl/available-action.js +1 -1
- package/lib/aes-encryptor.d.ts +1 -0
- package/lib/aes-encryptor.js +12 -5
- package/lib/app-command.d.ts +1 -0
- package/lib/app-command.js +3 -0
- package/lib/app-supervisor/app-options-factory.d.ts +80 -0
- package/lib/app-supervisor/app-options-factory.js +91 -0
- package/lib/app-supervisor/condition-registry.d.ts +18 -0
- package/lib/app-supervisor/condition-registry.js +60 -0
- package/lib/app-supervisor/db-creator.d.ts +16 -0
- package/lib/app-supervisor/db-creator.js +163 -0
- package/lib/app-supervisor/db-drivers.d.ts +11 -0
- package/lib/app-supervisor/db-drivers.js +52 -0
- package/lib/app-supervisor/index.d.ts +161 -0
- package/lib/app-supervisor/index.js +690 -0
- package/lib/app-supervisor/main-only-adapter.d.ts +37 -0
- package/lib/app-supervisor/main-only-adapter.js +156 -0
- package/lib/app-supervisor/types.d.ts +161 -0
- package/lib/app-supervisor/types.js +24 -0
- package/lib/application.d.ts +10 -7
- package/lib/application.js +30 -18
- package/lib/commands/index.js +2 -0
- package/lib/commands/pm.js +11 -0
- package/lib/commands/repair.d.ts +11 -0
- package/lib/commands/repair.js +43 -0
- package/lib/commands/start.js +1 -1
- package/lib/event-queue.d.ts +8 -1
- package/lib/event-queue.js +25 -22
- package/lib/gateway/errors.js +50 -12
- package/lib/gateway/index.d.ts +8 -0
- package/lib/gateway/index.js +80 -16
- package/lib/gateway/ipc-socket-server.js +1 -1
- package/lib/gateway/ws-server.js +6 -2
- package/lib/helper.d.ts +359 -0
- package/lib/helper.js +78 -3
- package/lib/index.d.ts +2 -1
- package/lib/index.js +6 -3
- package/lib/locale/locale.js +1 -1
- package/lib/locale/resource.js +6 -9
- package/lib/main-data-source.d.ts +11 -0
- package/lib/main-data-source.js +128 -0
- package/lib/middlewares/data-template.js +1 -6
- package/lib/middlewares/parse-variables.js +2 -49
- package/lib/plugin-manager/deps.js +2 -2
- package/lib/plugin-manager/options/resource.js +52 -25
- package/lib/plugin-manager/plugin-manager.d.ts +1 -0
- package/lib/plugin-manager/plugin-manager.js +36 -1
- package/lib/pub-sub-manager/pub-sub-manager.d.ts +1 -1
- package/lib/pub-sub-manager/pub-sub-manager.js +14 -20
- package/lib/redis-connection-manager.d.ts +15 -5
- package/lib/redis-connection-manager.js +117 -24
- package/lib/snowflake-id-field.d.ts +2 -1
- package/lib/snowflake-id-field.js +2 -2
- package/package.json +18 -17
- package/lib/app-supervisor.d.ts +0 -69
- package/lib/app-supervisor.js +0 -299
- package/lib/background-job-manager.d.ts +0 -40
- package/lib/background-job-manager.js +0 -111
package/lib/main-data-source.js
CHANGED
|
@@ -32,6 +32,7 @@ __export(main_data_source_exports, {
|
|
|
32
32
|
module.exports = __toCommonJS(main_data_source_exports);
|
|
33
33
|
var import_data_source_manager = require("@nocobase/data-source-manager");
|
|
34
34
|
const _MainDataSource = class _MainDataSource extends import_data_source_manager.SequelizeDataSource {
|
|
35
|
+
status = "loaded";
|
|
35
36
|
init(options = {}) {
|
|
36
37
|
const { acl, resourceManager, database } = options;
|
|
37
38
|
this.acl = acl;
|
|
@@ -48,6 +49,133 @@ const _MainDataSource = class _MainDataSource extends import_data_source_manager
|
|
|
48
49
|
this.resourceManager.use(this.acl.middleware(), { group: "acl", after: "auth" });
|
|
49
50
|
}
|
|
50
51
|
}
|
|
52
|
+
async readTables() {
|
|
53
|
+
const allTables = await this.introspector.getTableList();
|
|
54
|
+
const existsCollections = this.collectionManager.db.collections;
|
|
55
|
+
const existsTables = Array.from(existsCollections.values()).map(
|
|
56
|
+
(collection) => collection.model.tableName
|
|
57
|
+
);
|
|
58
|
+
const diffTables = allTables.filter((table) => !existsTables.includes(table));
|
|
59
|
+
return diffTables.map((name) => ({ name }));
|
|
60
|
+
}
|
|
61
|
+
async tables2Collections(tableNames) {
|
|
62
|
+
const db = this.collectionManager.db;
|
|
63
|
+
const results = await Promise.all(
|
|
64
|
+
tableNames.map(async (tableName) => {
|
|
65
|
+
let tableInfo;
|
|
66
|
+
if (typeof tableName === "string") {
|
|
67
|
+
tableInfo = { tableName };
|
|
68
|
+
if (db.options.schema) {
|
|
69
|
+
tableInfo.schema = db.options.schema;
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
tableInfo = tableName;
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
return await this.introspector.getCollection({ tableInfo });
|
|
76
|
+
} catch (e) {
|
|
77
|
+
if (e.message.includes("No description found for")) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
throw e;
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
);
|
|
84
|
+
return results.filter(Boolean);
|
|
85
|
+
}
|
|
86
|
+
async loadTables(ctx, tables) {
|
|
87
|
+
const repo = this.collectionManager.db.getRepository("collections");
|
|
88
|
+
const existsCollections = this.collectionManager.db.collections;
|
|
89
|
+
const existsTables = Array.from(existsCollections.values()).map(
|
|
90
|
+
(collection) => collection.model.tableName
|
|
91
|
+
);
|
|
92
|
+
const toAddTables = tables.filter((table) => !existsTables.includes(table));
|
|
93
|
+
if (toAddTables.length) {
|
|
94
|
+
try {
|
|
95
|
+
this.status = "loading";
|
|
96
|
+
const results = await this.tables2Collections(toAddTables);
|
|
97
|
+
const values = results.map((result) => ({
|
|
98
|
+
...result,
|
|
99
|
+
underscored: false
|
|
100
|
+
}));
|
|
101
|
+
await repo.create({ values, context: ctx });
|
|
102
|
+
} catch (e) {
|
|
103
|
+
throw e;
|
|
104
|
+
} finally {
|
|
105
|
+
this.status = "loaded";
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async getLoadedCollections(filter) {
|
|
110
|
+
const db = this.collectionManager.db;
|
|
111
|
+
const loadedCollections = await db.getRepository("collections").find({
|
|
112
|
+
appends: ["fields"],
|
|
113
|
+
filter: {
|
|
114
|
+
hidden: false,
|
|
115
|
+
...filter
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
const collections = loadedCollections.filter((collection) => {
|
|
119
|
+
var _a;
|
|
120
|
+
return ((_a = collection.options) == null ? void 0 : _a.from) !== "db2cm";
|
|
121
|
+
});
|
|
122
|
+
const loadedData = {};
|
|
123
|
+
for (const collection of collections) {
|
|
124
|
+
const c = db.getCollection(collection.name);
|
|
125
|
+
loadedData[c.tableName()] = {
|
|
126
|
+
...collection.toJSON(),
|
|
127
|
+
fields: collection.fields.map((field) => {
|
|
128
|
+
const f = c.getField(field.name);
|
|
129
|
+
return {
|
|
130
|
+
columnName: f == null ? void 0 : f.columnName(),
|
|
131
|
+
...field.toJSON()
|
|
132
|
+
};
|
|
133
|
+
})
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
return loadedData;
|
|
137
|
+
}
|
|
138
|
+
async syncFieldsFromDatabase(ctx, collectionNames) {
|
|
139
|
+
let filter = {};
|
|
140
|
+
if (collectionNames == null ? void 0 : collectionNames.length) {
|
|
141
|
+
filter = {
|
|
142
|
+
name: collectionNames
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const db = this.collectionManager.db;
|
|
146
|
+
const loadedCollections = await this.getLoadedCollections(filter);
|
|
147
|
+
const tableNames = Object.values(loadedCollections).map(({ name }) => {
|
|
148
|
+
const collection = db.getCollection(name);
|
|
149
|
+
return collection.getTableNameWithSchema();
|
|
150
|
+
});
|
|
151
|
+
let collections = [];
|
|
152
|
+
try {
|
|
153
|
+
collections = await this.tables2Collections(tableNames);
|
|
154
|
+
} catch (err) {
|
|
155
|
+
ctx.log.error(err);
|
|
156
|
+
}
|
|
157
|
+
const toLoadCollections = this.mergeWithLoadedCollections(collections, loadedCollections);
|
|
158
|
+
for (const values of toLoadCollections) {
|
|
159
|
+
const existsFields = loadedCollections[values.tableName].fields;
|
|
160
|
+
const deletedFields = existsFields.filter((field) => !values.fields.find((f) => f.name === field.name));
|
|
161
|
+
await db.sequelize.transaction(async (transaction) => {
|
|
162
|
+
for (const field of deletedFields) {
|
|
163
|
+
await db.getRepository("fields").destroy({
|
|
164
|
+
filterByTk: field.key,
|
|
165
|
+
context: ctx,
|
|
166
|
+
transaction
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
await db.getRepository("collections").update({
|
|
170
|
+
filterByTk: values.name,
|
|
171
|
+
values,
|
|
172
|
+
updateAssociationValues: ["fields"],
|
|
173
|
+
context: ctx,
|
|
174
|
+
transaction
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
51
179
|
};
|
|
52
180
|
__name(_MainDataSource, "MainDataSource");
|
|
53
181
|
let MainDataSource = _MainDataSource;
|
|
@@ -126,12 +126,7 @@ const traverseJSON = /* @__PURE__ */ __name((data, options) => {
|
|
|
126
126
|
include: subInclude
|
|
127
127
|
});
|
|
128
128
|
} else if (field.type === "belongsTo") {
|
|
129
|
-
result[key] =
|
|
130
|
-
collection: collection.db.getCollection(field.target),
|
|
131
|
-
// exclude: [field.foreignKey],
|
|
132
|
-
include: subInclude,
|
|
133
|
-
excludePk: false
|
|
134
|
-
});
|
|
129
|
+
result[key] = data[key];
|
|
135
130
|
} else if (field.type === "belongsToMany") {
|
|
136
131
|
result[key] = traverseBelongsToMany(data[key], {
|
|
137
132
|
collection: collection.db.getCollection(field.target),
|
|
@@ -31,60 +31,13 @@ __export(parse_variables_exports, {
|
|
|
31
31
|
});
|
|
32
32
|
module.exports = __toCommonJS(parse_variables_exports);
|
|
33
33
|
var import_utils = require("@nocobase/utils");
|
|
34
|
-
|
|
35
|
-
return async ({ fields }) => {
|
|
36
|
-
var _a, _b;
|
|
37
|
-
const userFields = fields.filter((f) => f && ctx.db.getFieldByPath("users." + f));
|
|
38
|
-
(_a = ctx.logger) == null ? void 0 : _a.info("filter-parse: ", { userFields });
|
|
39
|
-
if (!ctx.state.currentUser) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (!userFields.length) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const user = await ctx.db.getRepository("users").findOne({
|
|
46
|
-
filterByTk: ctx.state.currentUser.id,
|
|
47
|
-
fields: userFields
|
|
48
|
-
});
|
|
49
|
-
(_b = ctx.logger) == null ? void 0 : _b.info("filter-parse: ", {
|
|
50
|
-
$user: user == null ? void 0 : user.toJSON()
|
|
51
|
-
});
|
|
52
|
-
return user;
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
__name(getUser, "getUser");
|
|
56
|
-
function isNumeric(str) {
|
|
57
|
-
if (typeof str === "number") return true;
|
|
58
|
-
if (typeof str != "string") return false;
|
|
59
|
-
return !isNaN(str) && !isNaN(parseFloat(str));
|
|
60
|
-
}
|
|
61
|
-
__name(isNumeric, "isNumeric");
|
|
34
|
+
var import_helper = require("../helper");
|
|
62
35
|
async function parseVariables(ctx, next) {
|
|
63
36
|
const filter = ctx.action.params.filter;
|
|
64
37
|
if (!filter) {
|
|
65
38
|
return next();
|
|
66
39
|
}
|
|
67
|
-
ctx.action.params.filter = await (0, import_utils.parseFilter)(filter,
|
|
68
|
-
timezone: ctx.get("x-timezone"),
|
|
69
|
-
now: (/* @__PURE__ */ new Date()).toISOString(),
|
|
70
|
-
getField: /* @__PURE__ */ __name((path) => {
|
|
71
|
-
const fieldPath = path.split(".").filter((p) => !p.startsWith("$") && !isNumeric(p)).join(".");
|
|
72
|
-
const { resourceName } = ctx.action;
|
|
73
|
-
return ctx.db.getFieldByPath(`${resourceName}.${fieldPath}`);
|
|
74
|
-
}, "getField"),
|
|
75
|
-
vars: {
|
|
76
|
-
// @deprecated
|
|
77
|
-
$system: {
|
|
78
|
-
now: (/* @__PURE__ */ new Date()).toISOString()
|
|
79
|
-
},
|
|
80
|
-
// @deprecated
|
|
81
|
-
$date: (0, import_utils.getDateVars)(),
|
|
82
|
-
// 新的命名方式,防止和 formily 内置变量冲突
|
|
83
|
-
$nDate: (0, import_utils.getDateVars)(),
|
|
84
|
-
$user: getUser(ctx),
|
|
85
|
-
$nRole: ctx.state.currentRole === "__union__" ? ctx.state.currentRoles : ctx.state.currentRole
|
|
86
|
-
}
|
|
87
|
-
});
|
|
40
|
+
ctx.action.params.filter = await (0, import_utils.parseFilter)(filter, (0, import_helper.createContextVariablesScope)(ctx));
|
|
88
41
|
await next();
|
|
89
42
|
}
|
|
90
43
|
__name(parseVariables, "parseVariables");
|
|
@@ -41,7 +41,7 @@ const deps = {
|
|
|
41
41
|
umzug: "3.x",
|
|
42
42
|
"async-mutex": "0.5.x",
|
|
43
43
|
"@formulajs/formulajs": "4.x",
|
|
44
|
-
mathjs: "
|
|
44
|
+
mathjs: "15.x",
|
|
45
45
|
winston: "3.x",
|
|
46
46
|
"winston-daily-rotate-file": "4.x",
|
|
47
47
|
koa: "2.x",
|
|
@@ -76,6 +76,6 @@ const deps = {
|
|
|
76
76
|
lodash: "4.x",
|
|
77
77
|
"china-division": "2.x",
|
|
78
78
|
cronstrue: "2.x",
|
|
79
|
-
"@nocobase/license-kit": "0.
|
|
79
|
+
"@nocobase/license-kit": "0.3.x"
|
|
80
80
|
};
|
|
81
81
|
var deps_default = deps;
|
|
@@ -13,6 +13,8 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
13
13
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
14
|
var __getProtoOf = Object.getPrototypeOf;
|
|
15
15
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
17
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
16
18
|
var __export = (target, all) => {
|
|
17
19
|
for (var name in all)
|
|
18
20
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -34,6 +36,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
34
36
|
mod
|
|
35
37
|
));
|
|
36
38
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
39
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
37
40
|
var resource_exports = {};
|
|
38
41
|
__export(resource_exports, {
|
|
39
42
|
default: () => resource_default
|
|
@@ -43,6 +46,34 @@ var import_utils = require("@nocobase/utils");
|
|
|
43
46
|
var import_fs = __toESM(require("fs"));
|
|
44
47
|
var import_fs_extra = __toESM(require("fs-extra"));
|
|
45
48
|
var import_path = __toESM(require("path"));
|
|
49
|
+
const _PackageUrls = class _PackageUrls {
|
|
50
|
+
static async get(packageName) {
|
|
51
|
+
if (!this.items[packageName]) {
|
|
52
|
+
this.items[packageName] = await this.fetch(packageName);
|
|
53
|
+
}
|
|
54
|
+
return this.items[packageName];
|
|
55
|
+
}
|
|
56
|
+
static async fetch(packageName) {
|
|
57
|
+
const PLUGIN_CLIENT_ENTRY_FILE = "dist/client/index.js";
|
|
58
|
+
const pkgPath = import_path.default.resolve(process.env.NODE_MODULES_PATH, packageName);
|
|
59
|
+
const r = await import_fs_extra.default.exists(pkgPath);
|
|
60
|
+
if (r) {
|
|
61
|
+
let t = "";
|
|
62
|
+
const dist = import_path.default.resolve(pkgPath, PLUGIN_CLIENT_ENTRY_FILE);
|
|
63
|
+
const distExists = await import_fs_extra.default.exists(dist);
|
|
64
|
+
if (distExists) {
|
|
65
|
+
const fsState = await import_fs_extra.default.stat(distExists ? dist : pkgPath);
|
|
66
|
+
t = `?t=${fsState.mtime.getTime()}`;
|
|
67
|
+
}
|
|
68
|
+
const cdnBaseUrl = process.env.CDN_BASE_URL.replace(/\/+$/, "");
|
|
69
|
+
const url = `${cdnBaseUrl}${"/static/plugins/"}${packageName}/${PLUGIN_CLIENT_ENTRY_FILE}${t}`;
|
|
70
|
+
return url;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
__name(_PackageUrls, "PackageUrls");
|
|
75
|
+
__publicField(_PackageUrls, "items", {});
|
|
76
|
+
let PackageUrls = _PackageUrls;
|
|
46
77
|
var resource_default = {
|
|
47
78
|
name: "pm",
|
|
48
79
|
actions: {
|
|
@@ -152,33 +183,29 @@ var resource_default = {
|
|
|
152
183
|
await next();
|
|
153
184
|
},
|
|
154
185
|
async listEnabled(ctx, next) {
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
186
|
+
const toArr = /* @__PURE__ */ __name(async () => {
|
|
187
|
+
const pm = ctx.db.getRepository("applicationPlugins");
|
|
188
|
+
const items = await pm.find({
|
|
189
|
+
filter: {
|
|
190
|
+
enabled: true
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
const arr = [];
|
|
194
|
+
for (const item of items) {
|
|
195
|
+
const url = await PackageUrls.get(item.packageName);
|
|
196
|
+
const { name, packageName, options } = item.toJSON();
|
|
197
|
+
if (url) {
|
|
198
|
+
arr.push({
|
|
199
|
+
name,
|
|
200
|
+
packageName,
|
|
201
|
+
options,
|
|
202
|
+
url
|
|
203
|
+
});
|
|
173
204
|
}
|
|
174
|
-
const url = `${process.env.APP_SERVER_BASE_URL}${process.env.PLUGIN_STATICS_PATH}${item.packageName}/${PLUGIN_CLIENT_ENTRY_FILE}?version=${item.version}${t}`;
|
|
175
|
-
arr.push({
|
|
176
|
-
...item.toJSON(),
|
|
177
|
-
url
|
|
178
|
-
});
|
|
179
205
|
}
|
|
180
|
-
|
|
181
|
-
|
|
206
|
+
return arr;
|
|
207
|
+
}, "toArr");
|
|
208
|
+
ctx.body = await toArr();
|
|
182
209
|
await next();
|
|
183
210
|
},
|
|
184
211
|
async get(ctx, next) {
|
|
@@ -90,7 +90,7 @@ const _PluginManager = class _PluginManager {
|
|
|
90
90
|
namespace: "core/pm",
|
|
91
91
|
directory: (0, import_path.resolve)(__dirname, "../migrations")
|
|
92
92
|
});
|
|
93
|
-
this.app.
|
|
93
|
+
this.app.resourceManager.use(import_middleware.uploadMiddleware, { tag: "upload", after: "acl" });
|
|
94
94
|
}
|
|
95
95
|
/**
|
|
96
96
|
* @internal
|
|
@@ -718,6 +718,41 @@ const _PluginManager = class _PluginManager {
|
|
|
718
718
|
await removeDir();
|
|
719
719
|
}
|
|
720
720
|
}
|
|
721
|
+
async pull(urlOrName, options, emitStartedEvent = true) {
|
|
722
|
+
if (Array.isArray(urlOrName)) {
|
|
723
|
+
for (const packageName of urlOrName) {
|
|
724
|
+
await this.addViaCLI(packageName, import_lodash.default.omit(options, "name"), false);
|
|
725
|
+
}
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
if ((0, import_utils.isURL)(urlOrName)) {
|
|
729
|
+
await this.addByCompressedFileUrl(
|
|
730
|
+
{
|
|
731
|
+
...options,
|
|
732
|
+
compressedFileUrl: urlOrName
|
|
733
|
+
},
|
|
734
|
+
emitStartedEvent
|
|
735
|
+
);
|
|
736
|
+
} else if (await import_fs_extra.default.exists(urlOrName)) {
|
|
737
|
+
await this.addByCompressedFileUrl(
|
|
738
|
+
{
|
|
739
|
+
...options,
|
|
740
|
+
compressedFileUrl: urlOrName
|
|
741
|
+
},
|
|
742
|
+
emitStartedEvent
|
|
743
|
+
);
|
|
744
|
+
} else if (options == null ? void 0 : options.registry) {
|
|
745
|
+
const { name, packageName } = await _PluginManager.parseName(urlOrName);
|
|
746
|
+
options["name"] = name;
|
|
747
|
+
await this.addByNpm(
|
|
748
|
+
{
|
|
749
|
+
...options,
|
|
750
|
+
packageName
|
|
751
|
+
},
|
|
752
|
+
emitStartedEvent
|
|
753
|
+
);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
721
756
|
/**
|
|
722
757
|
* @internal
|
|
723
758
|
*/
|
|
@@ -17,8 +17,8 @@ export declare class PubSubManager {
|
|
|
17
17
|
protected adapter: IPubSubAdapter;
|
|
18
18
|
protected handlerManager: HandlerManager;
|
|
19
19
|
constructor(app: Application, options?: PubSubManagerOptions);
|
|
20
|
-
get channelPrefix(): string;
|
|
21
20
|
setAdapter(adapter: IPubSubAdapter): void;
|
|
21
|
+
getFullChannel(channel: string): string;
|
|
22
22
|
isConnected(): Promise<boolean>;
|
|
23
23
|
connect(): Promise<void>;
|
|
24
24
|
close(): Promise<any>;
|
|
@@ -35,14 +35,12 @@ var import_utils = require("@nocobase/utils");
|
|
|
35
35
|
var import_handler_manager = require("./handler-manager");
|
|
36
36
|
const createPubSubManager = /* @__PURE__ */ __name((app, options) => {
|
|
37
37
|
const pubSubManager = new PubSubManager(app, options);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
});
|
|
45
|
-
}
|
|
38
|
+
app.on("afterStart", async () => {
|
|
39
|
+
await pubSubManager.connect();
|
|
40
|
+
});
|
|
41
|
+
app.on("afterStop", async () => {
|
|
42
|
+
await pubSubManager.close();
|
|
43
|
+
});
|
|
46
44
|
return pubSubManager;
|
|
47
45
|
}, "createPubSubManager");
|
|
48
46
|
const _PubSubManager = class _PubSubManager {
|
|
@@ -55,13 +53,13 @@ const _PubSubManager = class _PubSubManager {
|
|
|
55
53
|
publisherId;
|
|
56
54
|
adapter;
|
|
57
55
|
handlerManager;
|
|
58
|
-
get channelPrefix() {
|
|
59
|
-
var _a;
|
|
60
|
-
return ((_a = this.options) == null ? void 0 : _a.channelPrefix) ? `${this.options.channelPrefix}.` : "";
|
|
61
|
-
}
|
|
62
56
|
setAdapter(adapter) {
|
|
63
57
|
this.adapter = adapter;
|
|
64
58
|
}
|
|
59
|
+
getFullChannel(channel) {
|
|
60
|
+
var _a;
|
|
61
|
+
return [this.app.name, (_a = this.options) == null ? void 0 : _a.channelPrefix, channel].filter(Boolean).join(".");
|
|
62
|
+
}
|
|
65
63
|
async isConnected() {
|
|
66
64
|
if (this.adapter) {
|
|
67
65
|
return this.adapter.isConnected();
|
|
@@ -72,14 +70,10 @@ const _PubSubManager = class _PubSubManager {
|
|
|
72
70
|
if (!this.adapter) {
|
|
73
71
|
return;
|
|
74
72
|
}
|
|
75
|
-
if (!this.app.serving()) {
|
|
76
|
-
this.app.logger.warn("app is not serving, will not connect to event queue");
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
73
|
await this.adapter.connect();
|
|
80
74
|
await this.handlerManager.each(async (channel, headler) => {
|
|
81
75
|
this.app.logger.debug(`[PubSubManager] subscribe ${channel} added before connected`);
|
|
82
|
-
await this.adapter.subscribe(
|
|
76
|
+
await this.adapter.subscribe(this.getFullChannel(channel), headler);
|
|
83
77
|
});
|
|
84
78
|
}
|
|
85
79
|
async close() {
|
|
@@ -93,7 +87,7 @@ const _PubSubManager = class _PubSubManager {
|
|
|
93
87
|
const handler = this.handlerManager.set(channel, callback, options);
|
|
94
88
|
if (await this.isConnected()) {
|
|
95
89
|
this.app.logger.debug(`[PubSubManager] subscribe ${channel} added after connected`);
|
|
96
|
-
await this.adapter.subscribe(
|
|
90
|
+
await this.adapter.subscribe(this.getFullChannel(channel), handler);
|
|
97
91
|
}
|
|
98
92
|
}
|
|
99
93
|
async unsubscribe(channel, callback) {
|
|
@@ -101,7 +95,7 @@ const _PubSubManager = class _PubSubManager {
|
|
|
101
95
|
if (!this.adapter || !handler) {
|
|
102
96
|
return;
|
|
103
97
|
}
|
|
104
|
-
return this.adapter.unsubscribe(
|
|
98
|
+
return this.adapter.unsubscribe(this.getFullChannel(channel), handler);
|
|
105
99
|
}
|
|
106
100
|
async publish(channel, message, options) {
|
|
107
101
|
var _a;
|
|
@@ -116,7 +110,7 @@ const _PubSubManager = class _PubSubManager {
|
|
|
116
110
|
...options,
|
|
117
111
|
message
|
|
118
112
|
});
|
|
119
|
-
await this.adapter.publish(
|
|
113
|
+
await this.adapter.publish(this.getFullChannel(channel), wrappedMessage);
|
|
120
114
|
this.app.logger.trace(`[PubSubManager] published message to channel ${channel}`);
|
|
121
115
|
}
|
|
122
116
|
};
|
|
@@ -6,23 +6,33 @@
|
|
|
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 Redis from 'ioredis';
|
|
10
9
|
import { Logger } from '@nocobase/logger';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
import { createClient } from 'redis';
|
|
11
|
+
type RedisClientOptions = NonNullable<Parameters<typeof createClient>[0]>;
|
|
12
|
+
export type Redis = ReturnType<typeof createClient>;
|
|
13
|
+
export interface RedisConfig extends RedisClientOptions {
|
|
14
|
+
connectionString?: string;
|
|
14
15
|
}
|
|
15
16
|
export declare class RedisConnectionManager {
|
|
16
17
|
private logger;
|
|
17
18
|
private config;
|
|
18
19
|
private connections;
|
|
20
|
+
private connectionConfigs;
|
|
21
|
+
private connecting;
|
|
19
22
|
constructor(config: {
|
|
20
|
-
redisConfig
|
|
23
|
+
redisConfig?: RedisConfig;
|
|
21
24
|
logger: Logger;
|
|
22
25
|
});
|
|
23
26
|
private bindEvents;
|
|
27
|
+
private mergeConfig;
|
|
28
|
+
private hasConnectionOptions;
|
|
29
|
+
private buildClientOptions;
|
|
30
|
+
private ensureConnected;
|
|
24
31
|
private getClient;
|
|
25
32
|
getConnection(key?: string, config?: RedisConfig): Redis | null;
|
|
33
|
+
private waitUntilReady;
|
|
26
34
|
getConnectionSync(key?: string, config?: RedisConfig): Promise<Redis>;
|
|
35
|
+
private closeConnection;
|
|
27
36
|
close(): Promise<void>;
|
|
28
37
|
}
|
|
38
|
+
export {};
|