@nocobase/plugin-flow-engine 2.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +29 -0
  3. package/build.config.ts +22 -0
  4. package/client.d.ts +2 -0
  5. package/client.js +1 -0
  6. package/dist/client/index.d.ts +15 -0
  7. package/dist/client/index.js +10 -0
  8. package/dist/externalVersion.js +21 -0
  9. package/dist/index.d.ts +10 -0
  10. package/dist/index.js +48 -0
  11. package/dist/locale/en-US.json +61 -0
  12. package/dist/locale/index.d.ts +141 -0
  13. package/dist/locale/index.js +79 -0
  14. package/dist/locale/zh-CN.json +61 -0
  15. package/dist/node_modules/ses/LICENSE +201 -0
  16. package/dist/node_modules/ses/LICENSE-aura +16 -0
  17. package/dist/node_modules/ses/LICENSE-caja +13 -0
  18. package/dist/node_modules/ses/LICENSE-corejs +19 -0
  19. package/dist/node_modules/ses/LICENSE-v8 +9 -0
  20. package/dist/node_modules/ses/assert-shim.js +1 -0
  21. package/dist/node_modules/ses/compartment-shim.js +1 -0
  22. package/dist/node_modules/ses/console-shim.js +1 -0
  23. package/dist/node_modules/ses/dist/lockdown.cjs +13912 -0
  24. package/dist/node_modules/ses/dist/lockdown.umd.js +13912 -0
  25. package/dist/node_modules/ses/dist/lockdown.umd.min.js +1 -0
  26. package/dist/node_modules/ses/dist/ses-hermes.cjs +13912 -0
  27. package/dist/node_modules/ses/dist/ses.cjs +1 -0
  28. package/dist/node_modules/ses/dist/ses.umd.js +13912 -0
  29. package/dist/node_modules/ses/dist/ses.umd.min.js +1 -0
  30. package/dist/node_modules/ses/dist/types.d.cts +606 -0
  31. package/dist/node_modules/ses/index.js +18 -0
  32. package/dist/node_modules/ses/lockdown-shim.js +1 -0
  33. package/dist/node_modules/ses/lockdown.js +1 -0
  34. package/dist/node_modules/ses/package.json +1 -0
  35. package/dist/node_modules/ses/src/assert-shim.js +4 -0
  36. package/dist/node_modules/ses/src/assert-sloppy-mode.js +11 -0
  37. package/dist/node_modules/ses/src/cauterize-property.js +69 -0
  38. package/dist/node_modules/ses/src/commons.js +425 -0
  39. package/dist/node_modules/ses/src/compartment-evaluate.js +93 -0
  40. package/dist/node_modules/ses/src/compartment-shim.js +22 -0
  41. package/dist/node_modules/ses/src/compartment.js +477 -0
  42. package/dist/node_modules/ses/src/console-shim.js +50 -0
  43. package/dist/node_modules/ses/src/enable-property-overrides.js +211 -0
  44. package/dist/node_modules/ses/src/enablements.js +244 -0
  45. package/dist/node_modules/ses/src/error/assert.js +584 -0
  46. package/dist/node_modules/ses/src/error/console.js +541 -0
  47. package/dist/node_modules/ses/src/error/fatal-assert.js +54 -0
  48. package/dist/node_modules/ses/src/error/internal-types.js +98 -0
  49. package/dist/node_modules/ses/src/error/note-log-args.js +77 -0
  50. package/dist/node_modules/ses/src/error/stringify-utils.js +195 -0
  51. package/dist/node_modules/ses/src/error/tame-console.js +197 -0
  52. package/dist/node_modules/ses/src/error/tame-error-constructor.js +284 -0
  53. package/dist/node_modules/ses/src/error/tame-v8-error-constructor.js +386 -0
  54. package/dist/node_modules/ses/src/error/types.js +59 -0
  55. package/dist/node_modules/ses/src/error/unhandled-rejection.js +122 -0
  56. package/dist/node_modules/ses/src/eval-scope.js +89 -0
  57. package/dist/node_modules/ses/src/get-anonymous-intrinsics.js +181 -0
  58. package/dist/node_modules/ses/src/get-source-url.js +50 -0
  59. package/dist/node_modules/ses/src/global-object.js +175 -0
  60. package/dist/node_modules/ses/src/intrinsics.js +192 -0
  61. package/dist/node_modules/ses/src/lockdown-shim.js +37 -0
  62. package/dist/node_modules/ses/src/lockdown.js +558 -0
  63. package/dist/node_modules/ses/src/make-eval-function.js +28 -0
  64. package/dist/node_modules/ses/src/make-evaluate.js +110 -0
  65. package/dist/node_modules/ses/src/make-function-constructor.js +79 -0
  66. package/dist/node_modules/ses/src/make-hardener.js +275 -0
  67. package/dist/node_modules/ses/src/make-safe-evaluator.js +112 -0
  68. package/dist/node_modules/ses/src/module-instance.js +497 -0
  69. package/dist/node_modules/ses/src/module-link.js +159 -0
  70. package/dist/node_modules/ses/src/module-load.js +719 -0
  71. package/dist/node_modules/ses/src/module-proxy.js +200 -0
  72. package/dist/node_modules/ses/src/permits-intrinsics.js +291 -0
  73. package/dist/node_modules/ses/src/permits.js +1761 -0
  74. package/dist/node_modules/ses/src/reporting-types.d.ts +13 -0
  75. package/dist/node_modules/ses/src/reporting.js +105 -0
  76. package/dist/node_modules/ses/src/scope-constants.js +180 -0
  77. package/dist/node_modules/ses/src/shim-arraybuffer-transfer.js +85 -0
  78. package/dist/node_modules/ses/src/sloppy-globals-scope-terminator.js +61 -0
  79. package/dist/node_modules/ses/src/strict-scope-terminator.js +99 -0
  80. package/dist/node_modules/ses/src/tame-date-constructor.js +127 -0
  81. package/dist/node_modules/ses/src/tame-domains.js +41 -0
  82. package/dist/node_modules/ses/src/tame-faux-data-properties.js +210 -0
  83. package/dist/node_modules/ses/src/tame-function-constructors.js +140 -0
  84. package/dist/node_modules/ses/src/tame-function-tostring.js +50 -0
  85. package/dist/node_modules/ses/src/tame-harden.js +29 -0
  86. package/dist/node_modules/ses/src/tame-locale-methods.js +78 -0
  87. package/dist/node_modules/ses/src/tame-math-object.js +41 -0
  88. package/dist/node_modules/ses/src/tame-module-source.js +51 -0
  89. package/dist/node_modules/ses/src/tame-regenerator-runtime.js +29 -0
  90. package/dist/node_modules/ses/src/tame-regexp-constructor.js +65 -0
  91. package/dist/node_modules/ses/src/tame-symbol-constructor.js +64 -0
  92. package/dist/node_modules/ses/src/transforms.js +267 -0
  93. package/dist/node_modules/ses/tools.js +25 -0
  94. package/dist/node_modules/ses/types.d.ts +606 -0
  95. package/dist/server/actions/ui-schema-action.d.ts +27 -0
  96. package/dist/server/actions/ui-schema-action.js +118 -0
  97. package/dist/server/collections/flowModelTreePath.d.ts +11 -0
  98. package/dist/server/collections/flowModelTreePath.js +74 -0
  99. package/dist/server/collections/flowModels.d.ts +11 -0
  100. package/dist/server/collections/flowModels.js +57 -0
  101. package/dist/server/collections/flowsql.d.ts +10 -0
  102. package/dist/server/collections/flowsql.js +51 -0
  103. package/dist/server/dao/ui_schema_node_dao.d.ts +26 -0
  104. package/dist/server/dao/ui_schema_node_dao.js +24 -0
  105. package/dist/server/helper.d.ts +8 -0
  106. package/dist/server/helper.js +9 -0
  107. package/dist/server/index.d.ts +9 -0
  108. package/dist/server/index.js +42 -0
  109. package/dist/server/model.d.ts +12 -0
  110. package/dist/server/model.js +38 -0
  111. package/dist/server/plugin.d.ts +26 -0
  112. package/dist/server/plugin.js +270 -0
  113. package/dist/server/repository.d.ts +116 -0
  114. package/dist/server/repository.js +1209 -0
  115. package/dist/server/server.d.ts +16 -0
  116. package/dist/server/server.js +198 -0
  117. package/dist/server/template/contexts.d.ts +73 -0
  118. package/dist/server/template/contexts.js +233 -0
  119. package/dist/server/template/resolver.d.ts +30 -0
  120. package/dist/server/template/resolver.js +225 -0
  121. package/dist/server/variables/registry.d.ts +42 -0
  122. package/dist/server/variables/registry.js +299 -0
  123. package/package.json +28 -0
  124. package/server.d.ts +2 -0
  125. package/server.js +1 -0
@@ -0,0 +1,16 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { Plugin } from '@nocobase/server';
10
+ export declare const compile: (title: string) => string;
11
+ export declare class PluginUISchemaStorageServer extends Plugin {
12
+ registerRepository(): void;
13
+ beforeLoad(): Promise<void>;
14
+ load(): Promise<void>;
15
+ }
16
+ export default PluginUISchemaStorageServer;
@@ -0,0 +1,198 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
36
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
+ var server_exports = {};
38
+ __export(server_exports, {
39
+ PluginUISchemaStorageServer: () => PluginUISchemaStorageServer,
40
+ compile: () => compile,
41
+ default: () => server_default
42
+ });
43
+ module.exports = __toCommonJS(server_exports);
44
+ var import_database = require("@nocobase/database");
45
+ var import_server = require("@nocobase/server");
46
+ var import_utils = require("@nocobase/utils");
47
+ var import_model = require("./model");
48
+ var import_repository = __toESM(require("./repository"));
49
+ const compile = (title) => (title || "").replace(/{{\s*t\(["|'|`](.*)["|'|`]\)\s*}}/g, "$1");
50
+ function extractFields(obj) {
51
+ var _a, _b, _c, _d, _e;
52
+ const fields = [
53
+ obj.title,
54
+ obj.description,
55
+ (_a = obj["x-component-props"]) == null ? void 0 : _a.title,
56
+ (_b = obj["x-component-props"]) == null ? void 0 : _b.description,
57
+ (_c = obj["x-decorator-props"]) == null ? void 0 : _c.title,
58
+ (_d = obj["x-decorator-props"]) == null ? void 0 : _d.description
59
+ ];
60
+ const content = (_e = obj["x-component-props"]) == null ? void 0 : _e.content;
61
+ if (typeof content === "string") {
62
+ const regex = /\{\{\s*t\s+['"]([^'"]+)['"]\s*\}\}/g;
63
+ let match;
64
+ while ((match = regex.exec(content)) !== null) {
65
+ fields.push(match[1]);
66
+ }
67
+ }
68
+ return fields.filter((value) => value !== void 0 && value !== "");
69
+ }
70
+ class PluginUISchemaStorageServer extends import_server.Plugin {
71
+ registerRepository() {
72
+ this.app.db.registerRepositories({
73
+ FlowModelRepository: import_repository.default
74
+ });
75
+ }
76
+ async beforeLoad() {
77
+ const db = this.app.db;
78
+ const pm = this.app.pm;
79
+ this.app.db.registerModels({ MagicAttributeModel: import_database.MagicAttributeModel, FlowSchemaModel: import_model.FlowSchemaModel });
80
+ this.registerRepository();
81
+ this.app.acl.registerSnippet({
82
+ name: "ui.flowModels",
83
+ actions: ["flowModels:*"]
84
+ });
85
+ db.on("flowModels.beforeCreate", function setUid(model) {
86
+ if (!model.get("name")) {
87
+ model.set("name", (0, import_utils.uid)());
88
+ }
89
+ });
90
+ db.on("flowModels.afterSave", async function setUid(model, options) {
91
+ var _a;
92
+ const localizationPlugin = pm.get("localization");
93
+ const texts = [];
94
+ const changedFields = extractFields(model.toJSON());
95
+ if (!changedFields.length) {
96
+ return;
97
+ }
98
+ changedFields.forEach((field) => {
99
+ field && texts.push({ text: compile(field), module: `resources.ui-schema-storage` });
100
+ });
101
+ await ((_a = localizationPlugin == null ? void 0 : localizationPlugin.addNewTexts) == null ? void 0 : _a.call(localizationPlugin, texts, options));
102
+ });
103
+ db.on("flowModels.afterCreate", async function insertSchema(model, options) {
104
+ const { transaction } = options;
105
+ const uiSchemaRepository = db.getCollection("flowModels").repository;
106
+ const context = options.context;
107
+ if (context == null ? void 0 : context.disableInsertHook) {
108
+ return;
109
+ }
110
+ await uiSchemaRepository.insert(model.toJSON(), {
111
+ transaction
112
+ });
113
+ });
114
+ db.on("flowModels.afterUpdate", async function patchSchema(model, options) {
115
+ const { transaction } = options;
116
+ const uiSchemaRepository = db.getCollection("flowModels").repository;
117
+ await uiSchemaRepository.patch(model.toJSON(), {
118
+ transaction
119
+ });
120
+ });
121
+ db.on("flowModels.afterDestroy", async function patchSchema(model, options) {
122
+ const { transaction } = options;
123
+ const uiSchemaRepository = db.getCollection("flowModels").repository;
124
+ await uiSchemaRepository.remove(model.get("name"), { transaction });
125
+ });
126
+ this.app.resourceManager.define({
127
+ name: "flowModels",
128
+ actions: {
129
+ findOne: async (ctx, next) => {
130
+ const { uid: uid2, parentId, subKey } = ctx.action.params;
131
+ const repository = ctx.db.getRepository("flowModels");
132
+ if (uid2) {
133
+ ctx.body = await repository.findModelById(uid2);
134
+ } else if (parentId) {
135
+ ctx.body = await repository.findModelByParentId(parentId, { subKey });
136
+ }
137
+ await next();
138
+ },
139
+ move: async (ctx, next) => {
140
+ const { sourceId, targetId, position } = ctx.action.params;
141
+ const repository = ctx.db.getRepository("flowModels");
142
+ await repository.move({ sourceId, targetId, position });
143
+ ctx.body = "ok";
144
+ await next();
145
+ },
146
+ save: async (ctx, next) => {
147
+ const { values } = ctx.action.params;
148
+ const repository = ctx.db.getRepository("flowModels");
149
+ const uid2 = await repository.upsertModel(values);
150
+ ctx.body = uid2;
151
+ await next();
152
+ },
153
+ destroy: async (ctx, next) => {
154
+ const { filterByTk } = ctx.action.params;
155
+ const repository = ctx.db.getRepository("flowModels");
156
+ await repository.remove(filterByTk);
157
+ ctx.body = "ok";
158
+ await next();
159
+ }
160
+ }
161
+ });
162
+ this.app.acl.allow("flowModels", ["findOne"], "loggedIn");
163
+ }
164
+ async load() {
165
+ const getSourceAndTargetForRemoveAction = async (ctx) => {
166
+ const { filterByTk } = ctx.action.params;
167
+ return {
168
+ targetCollection: "flowModels",
169
+ targetRecordUK: filterByTk
170
+ };
171
+ };
172
+ const getSourceAndTargetForInsertAdjacentAction = async (ctx) => {
173
+ var _a, _b;
174
+ return {
175
+ targetCollection: "flowModels",
176
+ targetRecordUK: (_b = (_a = ctx.request.body) == null ? void 0 : _a.options) == null ? void 0 : _b["uid"]
177
+ };
178
+ };
179
+ const getSourceAndTargetForPatchAction = async (ctx) => {
180
+ var _a;
181
+ return {
182
+ targetCollection: "flowModels",
183
+ targetRecordUK: (_a = ctx.request.body) == null ? void 0 : _a["uid"]
184
+ };
185
+ };
186
+ this.app.auditManager.registerActions([
187
+ { name: "flowModels:remove", getSourceAndTarget: getSourceAndTargetForRemoveAction },
188
+ { name: "flowModels:insertAdjacent", getSourceAndTarget: getSourceAndTargetForInsertAdjacentAction },
189
+ { name: "flowModels:patch", getSourceAndTarget: getSourceAndTargetForPatchAction }
190
+ ]);
191
+ }
192
+ }
193
+ var server_default = PluginUISchemaStorageServer;
194
+ // Annotate the CommonJS export names for ESM import in node:
195
+ 0 && (module.exports = {
196
+ PluginUISchemaStorageServer,
197
+ compile
198
+ });
@@ -0,0 +1,73 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { ResourcerContext } from '@nocobase/resourcer';
10
+ type Getter<T = any> = (ctx: ServerBaseContext) => T | Promise<T>;
11
+ export interface PropertyOptions {
12
+ /** 固定值,优先级高于 get */
13
+ value?: any;
14
+ /** 惰性求值 getter,返回值可为 Promise;默认会被缓存 */
15
+ get?: Getter;
16
+ /** 是否禁用缓存,true 表示每次访问都重新计算 */
17
+ cache?: boolean;
18
+ /** 仅允许首次定义,后续重复定义将被忽略 */
19
+ once?: boolean;
20
+ }
21
+ /**
22
+ * 服务器端上下文基础类。
23
+ * - 支持以 defineProperty 定义惰性/常量属性,并可选择缓存
24
+ * - 支持以 defineMethod 定义方法
25
+ * - 支持 delegate 机制,属性与方法可回退到被委托的上游上下文
26
+ * - 通过 Proxy 实现按需求值与 this 绑定
27
+ */
28
+ export declare class ServerBaseContext {
29
+ protected _props: Record<string, PropertyOptions>;
30
+ protected _methods: Record<string, (...args: any[]) => any>;
31
+ protected _cache: Record<string, any>;
32
+ protected _delegates: ServerBaseContext[];
33
+ protected _proxy?: ServerBaseContext;
34
+ [key: string]: any;
35
+ constructor();
36
+ protected _getOwn(key: string, current: ServerBaseContext): any;
37
+ /**
38
+ * 定义一个可枚举属性。
39
+ * - value:固定值
40
+ * - get:惰性 getter(默认带缓存)
41
+ * - once:已存在时忽略重复定义
42
+ */
43
+ defineProperty(key: string, options: PropertyOptions): void;
44
+ /** 定义一个方法(不可枚举、不可写),访问时会自动绑定到对应实例 */
45
+ defineMethod(name: string, fn: (...args: any[]) => any): void;
46
+ /**
47
+ * 委托到另一个 ServerBaseContext:
48
+ * - 访问属性/方法找不到时,会回退到被委托者进行解析
49
+ */
50
+ delegate(ctx: ServerBaseContext): void;
51
+ /** 清空所有委托 */
52
+ clearDelegates(): void;
53
+ /** 创建并返回代理对象(同一实例下保持稳定引用) */
54
+ createProxy(): any;
55
+ }
56
+ /**
57
+ * 全局上下文:
58
+ * - now/timestamp:时间值(不缓存)
59
+ * - env:环境变量(仅暴露白名单前缀)
60
+ * - date:日期快捷变量集合(包含前天等)
61
+ */
62
+ export declare class GlobalContext extends ServerBaseContext {
63
+ constructor(env?: Record<string, any>);
64
+ }
65
+ /**
66
+ * 请求级上下文:从 Koa-like 上下文中映射基础信息。
67
+ * - user(缓存)、roleName、locale、ip、headers、query、params
68
+ */
69
+ export declare class HttpRequestContext extends ServerBaseContext {
70
+ private koaCtx;
71
+ constructor(koaCtx: ResourcerContext);
72
+ }
73
+ export {};
@@ -0,0 +1,233 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
29
+ // If the importer is in node compatibility mode or this is not an ESM
30
+ // file that has been converted to a CommonJS file using a Babel-
31
+ // compatible transform (i.e. "__esModule" has not been set), then set
32
+ // "default" to the CommonJS "module.exports" for node compatibility.
33
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
34
+ mod
35
+ ));
36
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
37
+ var contexts_exports = {};
38
+ __export(contexts_exports, {
39
+ GlobalContext: () => GlobalContext,
40
+ HttpRequestContext: () => HttpRequestContext,
41
+ ServerBaseContext: () => ServerBaseContext
42
+ });
43
+ module.exports = __toCommonJS(contexts_exports);
44
+ var import_lodash = __toESM(require("lodash"));
45
+ var import_utils = require("@nocobase/utils");
46
+ class ServerBaseContext {
47
+ _props = {};
48
+ _methods = {};
49
+ _cache = {};
50
+ _delegates = [];
51
+ _proxy;
52
+ constructor() {
53
+ return this.createProxy();
54
+ }
55
+ _getOwn(key, current) {
56
+ const opt = this._props[key];
57
+ if (!opt) return void 0;
58
+ if (Object.prototype.hasOwnProperty.call(opt, "value")) return opt.value;
59
+ if (opt.get) {
60
+ if (opt.cache === false) return opt.get(current);
61
+ if (key in this._cache) return this._cache[key];
62
+ const p = opt.get(current);
63
+ this._cache[key] = p;
64
+ return p;
65
+ }
66
+ return void 0;
67
+ }
68
+ /**
69
+ * 定义一个可枚举属性。
70
+ * - value:固定值
71
+ * - get:惰性 getter(默认带缓存)
72
+ * - once:已存在时忽略重复定义
73
+ */
74
+ defineProperty(key, options) {
75
+ var _a;
76
+ if (this._props[key] && ((_a = this._props[key]) == null ? void 0 : _a.once)) return;
77
+ this._props[key] = options;
78
+ delete this._cache[key];
79
+ Object.defineProperty(this, key, {
80
+ configurable: true,
81
+ enumerable: true,
82
+ get: () => this._getOwn(key, this.createProxy())
83
+ });
84
+ }
85
+ /** 定义一个方法(不可枚举、不可写),访问时会自动绑定到对应实例 */
86
+ defineMethod(name, fn) {
87
+ this._methods[name] = fn;
88
+ Object.defineProperty(this, name, {
89
+ configurable: true,
90
+ enumerable: false,
91
+ writable: false,
92
+ value: fn
93
+ // bind at proxy access to keep delegate binding consistent
94
+ });
95
+ }
96
+ /**
97
+ * 委托到另一个 ServerBaseContext:
98
+ * - 访问属性/方法找不到时,会回退到被委托者进行解析
99
+ */
100
+ delegate(ctx) {
101
+ if (!(ctx instanceof ServerBaseContext)) {
102
+ throw new Error("Delegate must be a ServerBaseContext");
103
+ }
104
+ if (!this._delegates.includes(ctx)) this._delegates.unshift(ctx);
105
+ }
106
+ /** 清空所有委托 */
107
+ clearDelegates() {
108
+ this._delegates = [];
109
+ }
110
+ /** 创建并返回代理对象(同一实例下保持稳定引用) */
111
+ createProxy() {
112
+ if (this._proxy) return this._proxy;
113
+ this._proxy = new Proxy(this, {
114
+ get: (target, key, receiver) => {
115
+ var _a;
116
+ if (typeof key !== "string") return Reflect.get(target, key, receiver);
117
+ if (Reflect.has(target, key)) {
118
+ const v = Reflect.get(target, key, receiver);
119
+ return typeof v === "function" ? v.bind(target) : v;
120
+ }
121
+ if (Object.prototype.hasOwnProperty.call(target._props, key)) {
122
+ return target._getOwn(key, this.createProxy());
123
+ }
124
+ if (Object.prototype.hasOwnProperty.call(target._methods, key)) {
125
+ const fn = target._methods[key];
126
+ return typeof fn === "function" ? fn.bind(target) : fn;
127
+ }
128
+ for (const d of target._delegates) {
129
+ const candidate = (_a = d._getOwn) == null ? void 0 : _a.call(d, key, this.createProxy());
130
+ if (typeof candidate !== "undefined") return candidate;
131
+ if (Object.prototype.hasOwnProperty.call(d._methods || {}, key)) {
132
+ const fn = d._methods[key];
133
+ return typeof fn === "function" ? fn.bind(d) : fn;
134
+ }
135
+ }
136
+ return void 0;
137
+ },
138
+ has: (target, key) => {
139
+ if (typeof key !== "string") return Reflect.has(target, key);
140
+ if (Reflect.has(target, key)) return true;
141
+ if (Object.prototype.hasOwnProperty.call(target._props, key)) return true;
142
+ if (Object.prototype.hasOwnProperty.call(target._methods, key)) return true;
143
+ return target._delegates.some(
144
+ (d) => Object.prototype.hasOwnProperty.call(d._props, key) || Object.prototype.hasOwnProperty.call(d._methods, key)
145
+ );
146
+ }
147
+ });
148
+ return this._proxy;
149
+ }
150
+ }
151
+ class GlobalContext extends ServerBaseContext {
152
+ constructor(env) {
153
+ super();
154
+ this.defineProperty("now", { get: () => (/* @__PURE__ */ new Date()).toISOString(), cache: false });
155
+ this.defineProperty("timestamp", { get: () => Date.now(), cache: false });
156
+ if (env) this.defineProperty("env", { value: filterEnv(env) });
157
+ this.defineProperty("date", {
158
+ get: (_ctx) => buildDateVariables(),
159
+ cache: false
160
+ });
161
+ }
162
+ }
163
+ class HttpRequestContext extends ServerBaseContext {
164
+ constructor(koaCtx) {
165
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
166
+ super();
167
+ this.koaCtx = koaCtx;
168
+ this.defineProperty("user", { get: async () => {
169
+ var _a2, _b2;
170
+ return (_b2 = (_a2 = this.koaCtx) == null ? void 0 : _a2.auth) == null ? void 0 : _b2.user;
171
+ }, cache: true });
172
+ this.defineProperty("roleName", { value: (_b = (_a = this.koaCtx) == null ? void 0 : _a.auth) == null ? void 0 : _b.role });
173
+ this.defineProperty("locale", { value: (_d = (_c = this.koaCtx) == null ? void 0 : _c.getCurrentLocale) == null ? void 0 : _d.call(_c) });
174
+ this.defineProperty("ip", { value: ((_f = (_e = this.koaCtx) == null ? void 0 : _e.state) == null ? void 0 : _f.clientIp) || ((_h = (_g = this.koaCtx) == null ? void 0 : _g.request) == null ? void 0 : _h.ip) });
175
+ this.defineProperty("headers", { value: (_i = this.koaCtx) == null ? void 0 : _i.headers });
176
+ this.defineProperty("query", { value: (_k = (_j = this.koaCtx) == null ? void 0 : _j.request) == null ? void 0 : _k.query });
177
+ this.defineProperty("params", { value: import_lodash.default.get(this.koaCtx, "action.params") });
178
+ }
179
+ }
180
+ const DEFAULT_ENV_PREFIXES = ["PUBLIC_", "NEXT_PUBLIC_", "NCB_PUBLIC_"];
181
+ function filterEnv(env, allowedPrefixes = DEFAULT_ENV_PREFIXES) {
182
+ try {
183
+ const out = {};
184
+ for (const [k, v] of Object.entries(env || {})) {
185
+ if (allowedPrefixes.some((p) => k.startsWith(p))) {
186
+ out[k] = v;
187
+ }
188
+ }
189
+ return out;
190
+ } catch (_2) {
191
+ return {};
192
+ }
193
+ }
194
+ function buildDateVariables() {
195
+ const timezone = getServerTimezone();
196
+ const now = (/* @__PURE__ */ new Date()).toISOString();
197
+ const base = (0, import_utils.getDateVars)();
198
+ base.dayBeforeYesterday = (0, import_utils.toUnit)("day", -2);
199
+ const out = {};
200
+ for (const [key, val] of Object.entries(base)) {
201
+ try {
202
+ out[key] = typeof val === "function" ? val({ timezone, now }) : val;
203
+ } catch (_2) {
204
+ }
205
+ }
206
+ return out;
207
+ }
208
+ function getServerTimezone() {
209
+ var _a, _b;
210
+ const tzEnv = ((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.DB_TIMEZONE) || ((_b = process == null ? void 0 : process.env) == null ? void 0 : _b.TZ);
211
+ if (tzEnv) {
212
+ const m = tzEnv.match(/^([+-])(\d{1,2}):(\d{2})$/);
213
+ if (m) {
214
+ const sign2 = m[1];
215
+ const hh2 = m[2].padStart(2, "0");
216
+ const mm2 = m[3];
217
+ return `${sign2}${hh2}:${mm2}`;
218
+ }
219
+ return tzEnv;
220
+ }
221
+ const offsetMinutes = -(/* @__PURE__ */ new Date()).getTimezoneOffset();
222
+ const sign = offsetMinutes >= 0 ? "+" : "-";
223
+ const abs = Math.abs(offsetMinutes);
224
+ const hh = String(Math.floor(abs / 60)).padStart(2, "0");
225
+ const mm = String(abs % 60).padStart(2, "0");
226
+ return `${sign}${hh}:${mm}`;
227
+ }
228
+ // Annotate the CommonJS export names for ESM import in node:
229
+ 0 && (module.exports = {
230
+ GlobalContext,
231
+ HttpRequestContext,
232
+ ServerBaseContext
233
+ });
@@ -0,0 +1,30 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import 'ses';
10
+ export type JSONValue = string | {
11
+ [key: string]: JSONValue;
12
+ } | JSONValue[];
13
+ /**
14
+ * 解析 JSON 模板中形如 {{ ... }} 的占位符(服务端解析)。
15
+ * 仅支持以 ctx 开头的路径与表达式(如:{{ ctx.user.id }}、{{ ctx.record.roles[0].name }})。
16
+ * 无法解析或不受支持的表达式将原样保留。
17
+ *
18
+ * @param template 要解析的对象/数组/字符串模板
19
+ * @param ctx 变量上下文(实现了所需属性/方法的代理对象)
20
+ * @returns 解析后的结果,与输入结构相同
21
+ */
22
+ export declare function resolveJsonTemplate(template: JSONValue, ctx: any): Promise<any>;
23
+ /**
24
+ * 将表达式中的 ctx 访问改写为内部 __get 调用。
25
+ * 例如:ctx.user.id + 1 => (await __get('user', '.id')) + 1
26
+ *
27
+ * @param expression 原始表达式字符串
28
+ * @returns 改写后的表达式字符串
29
+ */
30
+ export declare function preprocessExpression(expression: string): string;