@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.
Files changed (152) hide show
  1. package/LICENSE +201 -0
  2. package/lib/acl/available-action.d.ts +5 -0
  3. package/lib/acl/available-action.js +62 -0
  4. package/lib/acl/index.d.ts +2 -0
  5. package/lib/acl/index.js +49 -0
  6. package/lib/aes-encryptor.d.ts +11 -0
  7. package/lib/aes-encryptor.js +116 -0
  8. package/lib/app-command.d.ts +12 -0
  9. package/lib/app-command.js +76 -0
  10. package/lib/app-supervisor.d.ts +61 -0
  11. package/lib/app-supervisor.js +299 -0
  12. package/lib/application.d.ts +354 -0
  13. package/lib/application.js +878 -0
  14. package/lib/cache/index.d.ts +3 -0
  15. package/lib/cache/index.js +35 -0
  16. package/lib/commands/console.d.ts +3 -0
  17. package/lib/commands/console.js +48 -0
  18. package/lib/commands/create-migration.d.ts +3 -0
  19. package/lib/commands/create-migration.js +80 -0
  20. package/lib/commands/db-auth.d.ts +3 -0
  21. package/lib/commands/db-auth.js +28 -0
  22. package/lib/commands/db-clean.d.ts +3 -0
  23. package/lib/commands/db-clean.js +31 -0
  24. package/lib/commands/db-sync.d.ts +3 -0
  25. package/lib/commands/db-sync.js +40 -0
  26. package/lib/commands/destroy.d.ts +3 -0
  27. package/lib/commands/destroy.js +30 -0
  28. package/lib/commands/index.d.ts +2 -0
  29. package/lib/commands/index.js +71 -0
  30. package/lib/commands/install.d.ts +3 -0
  31. package/lib/commands/install.js +33 -0
  32. package/lib/commands/migrator.d.ts +3 -0
  33. package/lib/commands/migrator.js +31 -0
  34. package/lib/commands/pm.d.ts +3 -0
  35. package/lib/commands/pm.js +75 -0
  36. package/lib/commands/refresh.d.ts +3 -0
  37. package/lib/commands/refresh.js +31 -0
  38. package/lib/commands/restart.d.ts +3 -0
  39. package/lib/commands/restart.js +35 -0
  40. package/lib/commands/start.d.ts +3 -0
  41. package/lib/commands/start.js +74 -0
  42. package/lib/commands/stop.d.ts +3 -0
  43. package/lib/commands/stop.js +34 -0
  44. package/lib/commands/upgrade.d.ts +3 -0
  45. package/lib/commands/upgrade.js +29 -0
  46. package/lib/cron/cron-job-manager.d.ts +25 -0
  47. package/lib/cron/cron-job-manager.js +89 -0
  48. package/lib/environment.d.ts +10 -0
  49. package/lib/environment.js +73 -0
  50. package/lib/errors/application-not-install.d.ts +4 -0
  51. package/lib/errors/application-not-install.js +35 -0
  52. package/lib/errors/handler.d.ts +3 -0
  53. package/lib/errors/handler.js +41 -0
  54. package/lib/errors/plugin-command-error.d.ts +2 -0
  55. package/lib/errors/plugin-command-error.js +31 -0
  56. package/lib/errors/plugin-not-exist.d.ts +2 -0
  57. package/lib/errors/plugin-not-exist.js +31 -0
  58. package/lib/gateway/errors.d.ts +22 -0
  59. package/lib/gateway/errors.js +138 -0
  60. package/lib/gateway/gateway.d.ts +56 -0
  61. package/lib/gateway/gateway.js +408 -0
  62. package/lib/gateway/index.d.ts +2 -0
  63. package/lib/gateway/index.js +30 -0
  64. package/lib/gateway/ipc-socket-client.d.ts +17 -0
  65. package/lib/gateway/ipc-socket-client.js +99 -0
  66. package/lib/gateway/ipc-socket-server.d.ts +12 -0
  67. package/lib/gateway/ipc-socket-server.js +140 -0
  68. package/lib/gateway/types.d.ts +25 -0
  69. package/lib/gateway/types.js +15 -0
  70. package/lib/gateway/ws-server.d.ts +63 -0
  71. package/lib/gateway/ws-server.js +335 -0
  72. package/lib/helper.d.ts +11 -0
  73. package/lib/helper.js +193 -0
  74. package/lib/helpers/application-version.d.ts +10 -0
  75. package/lib/helpers/application-version.js +77 -0
  76. package/lib/helpers/multiple-instance-manager.d.ts +6 -0
  77. package/lib/helpers/multiple-instance-manager.js +40 -0
  78. package/lib/index.d.ts +10 -0
  79. package/lib/index.js +57 -0
  80. package/lib/locale/index.d.ts +1 -0
  81. package/lib/locale/index.js +21 -0
  82. package/lib/locale/locale.d.ts +22 -0
  83. package/lib/locale/locale.js +166 -0
  84. package/lib/locale/resource.d.ts +1 -0
  85. package/lib/locale/resource.js +74 -0
  86. package/lib/main-data-source.d.ts +4 -0
  87. package/lib/main-data-source.js +45 -0
  88. package/lib/middlewares/data-template.d.ts +2 -0
  89. package/lib/middlewares/data-template.js +39 -0
  90. package/lib/middlewares/data-wrapping.d.ts +3 -0
  91. package/lib/middlewares/data-wrapping.js +90 -0
  92. package/lib/middlewares/db2resource.d.ts +6 -0
  93. package/lib/middlewares/db2resource.js +76 -0
  94. package/lib/middlewares/extract-client-ip.d.ts +2 -0
  95. package/lib/middlewares/extract-client-ip.js +37 -0
  96. package/lib/middlewares/i18n.d.ts +1 -0
  97. package/lib/middlewares/i18n.js +44 -0
  98. package/lib/middlewares/index.d.ts +3 -0
  99. package/lib/middlewares/index.js +32 -0
  100. package/lib/middlewares/parse-variables.d.ts +1 -0
  101. package/lib/middlewares/parse-variables.js +84 -0
  102. package/lib/migration.d.ts +12 -0
  103. package/lib/migration.js +47 -0
  104. package/lib/migrations/20230912193824-package-name-unique.d.ts +6 -0
  105. package/lib/migrations/20230912193824-package-name-unique.js +52 -0
  106. package/lib/migrations/20230912294620-update-pkg.d.ts +6 -0
  107. package/lib/migrations/20230912294620-update-pkg.js +58 -0
  108. package/lib/migrations/20240106082756-update-plugins.d.ts +6 -0
  109. package/lib/migrations/20240106082756-update-plugins.js +44 -0
  110. package/lib/migrations/20240705000001-remove-pkgs-approval.d.ts +6 -0
  111. package/lib/migrations/20240705000001-remove-pkgs-approval.js +47 -0
  112. package/lib/notice/index.d.ts +25 -0
  113. package/lib/notice/index.js +91 -0
  114. package/lib/plugin-manager/clientStaticUtils.d.ts +24 -0
  115. package/lib/plugin-manager/clientStaticUtils.js +156 -0
  116. package/lib/plugin-manager/constants.d.ts +7 -0
  117. package/lib/plugin-manager/constants.js +120 -0
  118. package/lib/plugin-manager/deps.d.ts +2 -0
  119. package/lib/plugin-manager/deps.js +68 -0
  120. package/lib/plugin-manager/index.d.ts +2 -0
  121. package/lib/plugin-manager/index.js +23 -0
  122. package/lib/plugin-manager/middleware.d.ts +2 -0
  123. package/lib/plugin-manager/middleware.js +36 -0
  124. package/lib/plugin-manager/options/collection.d.ts +2 -0
  125. package/lib/plugin-manager/options/collection.js +39 -0
  126. package/lib/plugin-manager/options/resource.d.ts +15 -0
  127. package/lib/plugin-manager/options/resource.js +185 -0
  128. package/lib/plugin-manager/plugin-manager-repository.d.ts +27 -0
  129. package/lib/plugin-manager/plugin-manager-repository.js +136 -0
  130. package/lib/plugin-manager/plugin-manager.d.ts +205 -0
  131. package/lib/plugin-manager/plugin-manager.js +1032 -0
  132. package/lib/plugin-manager/types.d.ts +15 -0
  133. package/lib/plugin-manager/types.js +15 -0
  134. package/lib/plugin-manager/utils.d.ts +101 -0
  135. package/lib/plugin-manager/utils.js +513 -0
  136. package/lib/plugin-manager/web-service.d.ts +6 -0
  137. package/lib/plugin-manager/web-service.js +116 -0
  138. package/lib/plugin.d.ts +125 -0
  139. package/lib/plugin.js +359 -0
  140. package/lib/pub-sub-manager/handler-manager.d.ts +28 -0
  141. package/lib/pub-sub-manager/handler-manager.js +149 -0
  142. package/lib/pub-sub-manager/index.d.ts +3 -0
  143. package/lib/pub-sub-manager/index.js +25 -0
  144. package/lib/pub-sub-manager/memory-pub-sub-adapter.d.ts +21 -0
  145. package/lib/pub-sub-manager/memory-pub-sub-adapter.js +82 -0
  146. package/lib/pub-sub-manager/pub-sub-manager.d.ts +19 -0
  147. package/lib/pub-sub-manager/pub-sub-manager.js +108 -0
  148. package/lib/pub-sub-manager/types.d.ts +20 -0
  149. package/lib/pub-sub-manager/types.js +15 -0
  150. package/lib/sync-message-manager.d.ts +16 -0
  151. package/lib/sync-message-manager.js +112 -0
  152. package/package.json +58 -0
package/lib/helper.js ADDED
@@ -0,0 +1,193 @@
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 helper_exports = {};
30
+ __export(helper_exports, {
31
+ createAppProxy: () => createAppProxy,
32
+ createI18n: () => createI18n,
33
+ createResourcer: () => createResourcer,
34
+ enablePerfHooks: () => enablePerfHooks,
35
+ getCommandFullName: () => getCommandFullName,
36
+ registerMiddlewares: () => registerMiddlewares,
37
+ tsxRerunning: () => tsxRerunning
38
+ });
39
+ module.exports = __toCommonJS(helper_exports);
40
+ var import_node_crypto = require("node:crypto");
41
+ var import_node_fs = __toESM(require("node:fs"));
42
+ var import_node_path = require("node:path");
43
+ var import_node_perf_hooks = require("node:perf_hooks");
44
+ var import_logger = require("@tachybase/logger");
45
+ var import_resourcer = require("@tachybase/resourcer");
46
+ var import_utils = require("@tachybase/utils");
47
+ var import_cors = __toESM(require("@koa/cors"));
48
+ var import_i18next = __toESM(require("i18next"));
49
+ var import_koa_bodyparser = __toESM(require("koa-bodyparser"));
50
+ var import_data_wrapping = require("./middlewares/data-wrapping");
51
+ var import_db2resource = require("./middlewares/db2resource");
52
+ var import_extract_client_ip = require("./middlewares/extract-client-ip");
53
+ var import_i18n = require("./middlewares/i18n");
54
+ function createI18n(options) {
55
+ const instance = import_i18next.default.createInstance();
56
+ instance.init({
57
+ lng: "en-US",
58
+ resources: {},
59
+ keySeparator: false,
60
+ nsSeparator: false,
61
+ ...options.i18n
62
+ });
63
+ return instance;
64
+ }
65
+ __name(createI18n, "createI18n");
66
+ function createResourcer(options) {
67
+ return new import_resourcer.Resourcer({ ...options.resourcer });
68
+ }
69
+ __name(createResourcer, "createResourcer");
70
+ function registerMiddlewares(app, options) {
71
+ var _a;
72
+ app.use(
73
+ async (ctx, next) => {
74
+ app.context.reqId = (0, import_node_crypto.randomUUID)();
75
+ await next();
76
+ },
77
+ { tag: "UUID" }
78
+ );
79
+ app.use((0, import_logger.requestLogger)(app.name, (_a = options.logger) == null ? void 0 : _a.request), { tag: "logger" });
80
+ app.use(
81
+ (0, import_cors.default)({
82
+ exposeHeaders: ["content-disposition"],
83
+ ...options.cors
84
+ }),
85
+ {
86
+ tag: "cors",
87
+ after: "bodyParser"
88
+ }
89
+ );
90
+ if (options.bodyParser !== false) {
91
+ const bodyLimit = "10mb";
92
+ app.use(
93
+ (0, import_koa_bodyparser.default)({
94
+ enableTypes: ["json", "form", "xml"],
95
+ jsonLimit: bodyLimit,
96
+ formLimit: bodyLimit,
97
+ textLimit: bodyLimit,
98
+ ...options.bodyParser
99
+ }),
100
+ {
101
+ tag: "bodyParser",
102
+ after: "logger"
103
+ }
104
+ );
105
+ }
106
+ app.use(
107
+ async (ctx, next) => {
108
+ ctx.getBearerToken = () => {
109
+ const token = ctx.get("Authorization").replace(/^Bearer\s+/gi, "");
110
+ return token || ctx.query.token;
111
+ };
112
+ await next();
113
+ },
114
+ { tag: "authorization" }
115
+ );
116
+ app.use(import_i18n.i18n, { tag: "i18n", after: "cors" });
117
+ if (options.dataWrapping !== false) {
118
+ app.use((0, import_data_wrapping.dataWrapping)(), { tag: "dataWrapping", after: "i18n" });
119
+ }
120
+ app.use(import_db2resource.db2resource, { tag: "db2resource", after: "dataWrapping" });
121
+ app.use(app.dataSourceManager.middleware(), { tag: "dataSource", after: "dataWrapping" });
122
+ app.use((0, import_extract_client_ip.extractClientIp)(), { tag: "extractClientIp", before: "cors" });
123
+ }
124
+ __name(registerMiddlewares, "registerMiddlewares");
125
+ const createAppProxy = /* @__PURE__ */ __name((app) => {
126
+ return new Proxy(app, {
127
+ get(target, prop, ...args) {
128
+ if (typeof prop === "string" && ["on", "once", "addListener"].includes(prop)) {
129
+ return (eventName, listener) => {
130
+ listener["_reinitializable"] = true;
131
+ return target[prop](eventName, listener);
132
+ };
133
+ }
134
+ return Reflect.get(target, prop, ...args);
135
+ }
136
+ });
137
+ }, "createAppProxy");
138
+ const getCommandFullName = /* @__PURE__ */ __name((command) => {
139
+ const names = [];
140
+ names.push(command.name());
141
+ let parent = command == null ? void 0 : command.parent;
142
+ while (parent) {
143
+ if (!(parent == null ? void 0 : parent.parent)) {
144
+ break;
145
+ }
146
+ names.unshift(parent.name());
147
+ parent = parent.parent;
148
+ }
149
+ return names.join(".");
150
+ }, "getCommandFullName");
151
+ const tsxRerunning = /* @__PURE__ */ __name(async () => {
152
+ const file = (0, import_node_path.resolve)(process.cwd(), "storage/app.watch.ts");
153
+ await import_node_fs.default.promises.writeFile(file, `export const watchId = '${(0, import_utils.uid)()}';`, "utf-8");
154
+ }, "tsxRerunning");
155
+ const enablePerfHooks = /* @__PURE__ */ __name((app) => {
156
+ app.context.getPerfHistogram = (name) => {
157
+ if (!app.perfHistograms.has(name)) {
158
+ app.perfHistograms.set(name, (0, import_node_perf_hooks.createHistogram)());
159
+ }
160
+ return app.perfHistograms.get(name);
161
+ };
162
+ app.resourcer.define({
163
+ name: "perf",
164
+ actions: {
165
+ view: /* @__PURE__ */ __name(async (ctx, next) => {
166
+ const result = {};
167
+ const histograms = ctx.app.perfHistograms;
168
+ const sortedHistograms = [...histograms.entries()].sort(([i, a], [j, b]) => b.mean - a.mean);
169
+ sortedHistograms.forEach(([name, histogram]) => {
170
+ result[name] = histogram;
171
+ });
172
+ ctx.body = result;
173
+ await next();
174
+ }, "view"),
175
+ reset: /* @__PURE__ */ __name(async (ctx, next) => {
176
+ const histograms = ctx.app.perfHistograms;
177
+ histograms.forEach((histogram) => histogram.reset());
178
+ await next();
179
+ }, "reset")
180
+ }
181
+ });
182
+ app.acl.allow("perf", "*", "public");
183
+ }, "enablePerfHooks");
184
+ // Annotate the CommonJS export names for ESM import in node:
185
+ 0 && (module.exports = {
186
+ createAppProxy,
187
+ createI18n,
188
+ createResourcer,
189
+ enablePerfHooks,
190
+ getCommandFullName,
191
+ registerMiddlewares,
192
+ tsxRerunning
193
+ });
@@ -0,0 +1,10 @@
1
+ import { Collection } from '@tachybase/database';
2
+ import Application from '../application';
3
+ export declare class ApplicationVersion {
4
+ protected app: Application;
5
+ protected collection: Collection;
6
+ constructor(app: Application);
7
+ get(): Promise<any>;
8
+ update(version?: string): Promise<void>;
9
+ satisfies(range: string): Promise<boolean>;
10
+ }
@@ -0,0 +1,77 @@
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 application_version_exports = {};
30
+ __export(application_version_exports, {
31
+ ApplicationVersion: () => ApplicationVersion
32
+ });
33
+ module.exports = __toCommonJS(application_version_exports);
34
+ var import_semver = __toESM(require("semver"));
35
+ const _ApplicationVersion = class _ApplicationVersion {
36
+ constructor(app) {
37
+ this.app = app;
38
+ app.db.collection({
39
+ origin: "@tego/core",
40
+ name: "applicationVersion",
41
+ dataType: "meta",
42
+ timestamps: false,
43
+ dumpRules: "required",
44
+ fields: [{ name: "value", type: "string" }]
45
+ });
46
+ this.collection = this.app.db.getCollection("applicationVersion");
47
+ }
48
+ async get() {
49
+ const model = await this.collection.model.findOne();
50
+ if (!model) {
51
+ return null;
52
+ }
53
+ return model.get("value");
54
+ }
55
+ async update(version) {
56
+ await this.collection.model.destroy({
57
+ truncate: true
58
+ });
59
+ await this.collection.model.create({
60
+ value: version || this.app.getVersion()
61
+ });
62
+ }
63
+ async satisfies(range) {
64
+ const model = await this.collection.model.findOne();
65
+ const version = model == null ? void 0 : model.value;
66
+ if (!version) {
67
+ return true;
68
+ }
69
+ return import_semver.default.satisfies(version, range, { includePrerelease: true });
70
+ }
71
+ };
72
+ __name(_ApplicationVersion, "ApplicationVersion");
73
+ let ApplicationVersion = _ApplicationVersion;
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ ApplicationVersion
77
+ });
@@ -0,0 +1,6 @@
1
+ export declare class MultipleInstanceManager<Item> {
2
+ map: Map<string, Item>;
3
+ constructor();
4
+ get(key: string): Item;
5
+ set(key: string, value: Item): void;
6
+ }
@@ -0,0 +1,40 @@
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 multiple_instance_manager_exports = {};
20
+ __export(multiple_instance_manager_exports, {
21
+ MultipleInstanceManager: () => MultipleInstanceManager
22
+ });
23
+ module.exports = __toCommonJS(multiple_instance_manager_exports);
24
+ const _MultipleInstanceManager = class _MultipleInstanceManager {
25
+ constructor() {
26
+ this.map = /* @__PURE__ */ new Map();
27
+ }
28
+ get(key) {
29
+ return this.map.get(key);
30
+ }
31
+ set(key, value) {
32
+ this.map.set(key, value);
33
+ }
34
+ };
35
+ __name(_MultipleInstanceManager, "MultipleInstanceManager");
36
+ let MultipleInstanceManager = _MultipleInstanceManager;
37
+ // Annotate the CommonJS export names for ESM import in node:
38
+ 0 && (module.exports = {
39
+ MultipleInstanceManager
40
+ });
package/lib/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export * from './application';
2
+ export { Application as default } from './application';
3
+ export * as middlewares from './middlewares';
4
+ export * from './migration';
5
+ export * from './plugin';
6
+ export * from './plugin-manager';
7
+ export * from './gateway';
8
+ export * from './app-supervisor';
9
+ export * from './notice';
10
+ export { AesEncryptor } from './aes-encryptor';
package/lib/index.js ADDED
@@ -0,0 +1,57 @@
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 __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
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 index_exports = {};
30
+ __export(index_exports, {
31
+ AesEncryptor: () => import_aes_encryptor.AesEncryptor,
32
+ default: () => import_application.Application,
33
+ middlewares: () => middlewares
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+ __reExport(index_exports, require("./application"), module.exports);
37
+ var import_application = require("./application");
38
+ var middlewares = __toESM(require("./middlewares"));
39
+ __reExport(index_exports, require("./migration"), module.exports);
40
+ __reExport(index_exports, require("./plugin"), module.exports);
41
+ __reExport(index_exports, require("./plugin-manager"), module.exports);
42
+ __reExport(index_exports, require("./gateway"), module.exports);
43
+ __reExport(index_exports, require("./app-supervisor"), module.exports);
44
+ __reExport(index_exports, require("./notice"), module.exports);
45
+ var import_aes_encryptor = require("./aes-encryptor");
46
+ // Annotate the CommonJS export names for ESM import in node:
47
+ 0 && (module.exports = {
48
+ AesEncryptor,
49
+ middlewares,
50
+ ...require("./application"),
51
+ ...require("./migration"),
52
+ ...require("./plugin"),
53
+ ...require("./plugin-manager"),
54
+ ...require("./gateway"),
55
+ ...require("./app-supervisor"),
56
+ ...require("./notice")
57
+ });
@@ -0,0 +1 @@
1
+ export * from './locale';
@@ -0,0 +1,21 @@
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 locale_exports = {};
16
+ module.exports = __toCommonJS(locale_exports);
17
+ __reExport(locale_exports, require("./locale"), module.exports);
18
+ // Annotate the CommonJS export names for ESM import in node:
19
+ 0 && (module.exports = {
20
+ ...require("./locale")
21
+ });
@@ -0,0 +1,22 @@
1
+ import { Cache } from '@tachybase/cache';
2
+ import Application from '../application';
3
+ export declare class Locale {
4
+ app: Application;
5
+ cache: Cache;
6
+ defaultLang: string;
7
+ localeFn: Map<any, any>;
8
+ resourceCached: Map<any, any>;
9
+ i18nInstances: Map<any, any>;
10
+ constructor(app: Application);
11
+ load(): Promise<void>;
12
+ setLocaleFn(name: string, fn: (lang: string) => Promise<any>): void;
13
+ getETag(lang: string): Promise<any>;
14
+ get(lang: string): Promise<{
15
+ resources: any;
16
+ }>;
17
+ wrapCache(key: string, fn: () => any): Promise<any>;
18
+ loadResourcesByLang(lang: string): Promise<void>;
19
+ getCacheResources(lang: string): Promise<any>;
20
+ getResources(lang: string): {};
21
+ getI18nInstance(lang: string): Promise<any>;
22
+ }
@@ -0,0 +1,166 @@
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 locale_exports = {};
30
+ __export(locale_exports, {
31
+ Locale: () => Locale
32
+ });
33
+ module.exports = __toCommonJS(locale_exports);
34
+ var import_node_crypto = require("node:crypto");
35
+ var import_lodash = __toESM(require("lodash"));
36
+ var import_resource = require("./resource");
37
+ const _Locale = class _Locale {
38
+ constructor(app) {
39
+ this.defaultLang = "en-US";
40
+ this.localeFn = /* @__PURE__ */ new Map();
41
+ this.resourceCached = /* @__PURE__ */ new Map();
42
+ this.i18nInstances = /* @__PURE__ */ new Map();
43
+ this.app = app;
44
+ this.app.on("afterLoad", async () => {
45
+ this.app.logger.debug("load locale resource", { submodule: "locale", method: "onAfterLoad" });
46
+ this.app.setMaintainingMessage("load locale resource");
47
+ await this.load();
48
+ this.app.logger.debug("locale resource loaded", { submodule: "locale", method: "onAfterLoad" });
49
+ this.app.setMaintainingMessage("locale resource loaded");
50
+ });
51
+ }
52
+ async load() {
53
+ this.cache = await this.app.cacheManager.createCache({
54
+ name: "locale",
55
+ prefix: "locale",
56
+ store: "memory"
57
+ });
58
+ await this.get(this.defaultLang);
59
+ }
60
+ setLocaleFn(name, fn) {
61
+ this.localeFn.set(name, fn);
62
+ }
63
+ async getETag(lang) {
64
+ if (process.env.APP_ENV !== "production" && !process.env.FORCE_LOCALE_CACHE) {
65
+ await this.cache.del(`eTag:${lang}`);
66
+ }
67
+ return await this.wrapCache(`eTag:${lang}`, async () => {
68
+ return (0, import_node_crypto.randomUUID)();
69
+ });
70
+ }
71
+ async get(lang) {
72
+ const defaults = {
73
+ resources: await this.getCacheResources(lang)
74
+ };
75
+ for (const [name, fn] of this.localeFn) {
76
+ const result = await this.wrapCache(`${name}:${lang}`, async () => {
77
+ this.cache.del(`eTag:${lang}`);
78
+ return await fn(lang);
79
+ });
80
+ if (result) {
81
+ defaults[name] = result;
82
+ }
83
+ }
84
+ return defaults;
85
+ }
86
+ async wrapCache(key, fn) {
87
+ return await this.cache.wrapWithCondition(key, fn, {
88
+ isCacheable: /* @__PURE__ */ __name((val) => !import_lodash.default.isEmpty(val), "isCacheable")
89
+ });
90
+ }
91
+ async loadResourcesByLang(lang) {
92
+ if (!this.cache) {
93
+ return;
94
+ }
95
+ if (!this.resourceCached.has(lang)) {
96
+ await this.getCacheResources(lang);
97
+ }
98
+ }
99
+ async getCacheResources(lang) {
100
+ this.resourceCached.set(lang, true);
101
+ if (process.env.APP_ENV !== "production" && !process.env.FORCE_LOCALE_CACHE) {
102
+ await this.cache.del(`resources:${lang}`);
103
+ }
104
+ return await this.wrapCache(`resources:${lang}`, () => this.getResources(lang));
105
+ }
106
+ getResources(lang) {
107
+ var _a;
108
+ const resources = {};
109
+ const names = this.app.pm.getAliases();
110
+ if (process.env.APP_ENV !== "production") {
111
+ const keys = Object.keys(require.cache);
112
+ const regex = new RegExp(`((plugin|module)-[a-zA-Z0-9\\-]+|client)/(dist|lib|src)/locale/${lang}`);
113
+ const matched = keys.filter((path) => regex.test(path));
114
+ if (matched.length > 0) {
115
+ this.app.logger.debug("clear locale resource cache", { submodule: "locale", matched });
116
+ }
117
+ matched.forEach((key) => delete require.cache[key]);
118
+ }
119
+ for (const name of names) {
120
+ try {
121
+ const p = this.app.pm.get(name);
122
+ if (!p) {
123
+ continue;
124
+ }
125
+ const packageName = (_a = p.options) == null ? void 0 : _a.packageName;
126
+ if (!packageName) {
127
+ continue;
128
+ }
129
+ const res = (0, import_resource.getResource)(packageName, lang);
130
+ if (res) {
131
+ resources[packageName] = { ...res };
132
+ if (packageName.includes("@tachybase/plugin-")) {
133
+ resources[packageName.substring("@tachybase/plugin-".length)] = { ...res };
134
+ }
135
+ if (packageName.includes("@tachybase/module-")) {
136
+ resources[packageName.substring("@tachybase/module-".length)] = { ...res };
137
+ }
138
+ }
139
+ } catch (err) {
140
+ }
141
+ }
142
+ resources["core"] = resources["web"];
143
+ resources["client"] = resources["web"];
144
+ Object.keys(resources).forEach((name) => {
145
+ this.app.i18n.addResources(lang, name, resources[name]);
146
+ });
147
+ return resources;
148
+ }
149
+ async getI18nInstance(lang) {
150
+ if (lang === "*" || !lang) {
151
+ return this.app.i18n.cloneInstance({ initImmediate: false });
152
+ }
153
+ let instance = this.i18nInstances.get(lang);
154
+ if (!instance) {
155
+ instance = this.app.i18n.cloneInstance({ initImmediate: false });
156
+ this.i18nInstances.set(lang, instance);
157
+ }
158
+ return instance;
159
+ }
160
+ };
161
+ __name(_Locale, "Locale");
162
+ let Locale = _Locale;
163
+ // Annotate the CommonJS export names for ESM import in node:
164
+ 0 && (module.exports = {
165
+ Locale
166
+ });
@@ -0,0 +1 @@
1
+ export declare const getResource: (packageName: string, lang: string, isPlugin?: boolean) => any;