@logicflow/engine 0.0.10-beta.0 → 0.0.11-beta.0
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/README.md +2 -5
- package/cjs/EventEmitter.js +70 -0
- package/cjs/FlowModel.js +277 -0
- package/cjs/Scheduler.js +252 -0
- package/{lib/constant/logCode.js → cjs/constant/LogCode.js} +13 -16
- package/{lib/constant/index.js → cjs/constant/constant.js} +3 -4
- package/cjs/expression/browserVm.js +81 -0
- package/cjs/expression/index.js +57 -0
- package/cjs/index.js +210 -0
- package/cjs/nodes/BaseNode.js +252 -0
- package/cjs/nodes/StartNode.js +27 -0
- package/cjs/nodes/TaskNode.js +27 -0
- package/cjs/recorder/index.js +168 -0
- package/cjs/util/ID.js +16 -0
- package/cjs/util/global.js +32 -0
- package/{lib/utils → cjs/util}/storage.js +17 -18
- package/es/EventEmitter.d.ts +4 -34
- package/es/EventEmitter.js +48 -70
- package/es/FlowModel.d.ts +73 -75
- package/es/FlowModel.js +173 -130
- package/es/Scheduler.d.ts +34 -50
- package/es/Scheduler.js +209 -134
- package/es/constant/LogCode.js +28 -0
- package/es/constant/{index.js → constant.js} +7 -8
- package/es/expression/browserVm.d.ts +4 -0
- package/es/expression/browserVm.js +76 -0
- package/es/expression/index.d.ts +1 -1
- package/es/expression/index.js +53 -16
- package/es/index.d.ts +38 -135
- package/es/index.js +144 -92
- package/es/nodes/BaseNode.d.ts +110 -0
- package/es/nodes/BaseNode.js +250 -0
- package/es/nodes/{start.d.ts → StartNode.d.ts} +2 -3
- package/es/nodes/StartNode.js +25 -0
- package/es/nodes/{task.d.ts → TaskNode.d.ts} +2 -3
- package/es/nodes/TaskNode.js +25 -0
- package/es/recorder/index.d.ts +10 -36
- package/es/recorder/index.js +135 -82
- package/es/util/ID.js +13 -0
- package/es/util/global.d.ts +5 -0
- package/es/util/global.js +26 -0
- package/es/{utils → util}/storage.js +16 -17
- package/lib/main.js +1 -0
- package/package.json +71 -30
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/es/EventEmitter.js.map +0 -1
- package/es/FlowModel.js.map +0 -1
- package/es/Scheduler.js.map +0 -1
- package/es/constant/index.js.map +0 -1
- package/es/constant/logCode.js +0 -29
- package/es/constant/logCode.js.map +0 -1
- package/es/expression/brewserVm.d.ts +0 -2
- package/es/expression/brewserVm.js +0 -30
- package/es/expression/brewserVm.js.map +0 -1
- package/es/expression/index.js.map +0 -1
- package/es/expression/nodeVm.d.ts +0 -4
- package/es/expression/nodeVm.js +0 -10
- package/es/expression/nodeVm.js.map +0 -1
- package/es/index.js.map +0 -1
- package/es/nodes/base.d.ts +0 -108
- package/es/nodes/base.js +0 -149
- package/es/nodes/base.js.map +0 -1
- package/es/nodes/index.d.ts +0 -3
- package/es/nodes/index.js +0 -4
- package/es/nodes/index.js.map +0 -1
- package/es/nodes/start.js +0 -11
- package/es/nodes/start.js.map +0 -1
- package/es/nodes/task.js +0 -11
- package/es/nodes/task.js.map +0 -1
- package/es/platform/browser/browserVm.d.ts +0 -4
- package/es/platform/browser/browserVm.js +0 -44
- package/es/platform/browser/browserVm.js.map +0 -1
- package/es/platform/browser/index.d.ts +0 -4
- package/es/platform/browser/index.js +0 -23
- package/es/platform/browser/index.js.map +0 -1
- package/es/platform/index.d.ts +0 -1
- package/es/platform/index.js +0 -2
- package/es/platform/index.js.map +0 -1
- package/es/platform/node/index.d.ts +0 -4
- package/es/platform/node/index.js +0 -23
- package/es/platform/node/index.js.map +0 -1
- package/es/platform/node/nodeVm.d.ts +0 -1
- package/es/platform/node/nodeVm.js +0 -9
- package/es/platform/node/nodeVm.js.map +0 -1
- package/es/recorder/index.js.map +0 -1
- package/es/utils/global.d.ts +0 -5
- package/es/utils/global.js +0 -27
- package/es/utils/global.js.map +0 -1
- package/es/utils/id.js +0 -14
- package/es/utils/id.js.map +0 -1
- package/es/utils/index.d.ts +0 -4
- package/es/utils/index.js +0 -5
- package/es/utils/index.js.map +0 -1
- package/es/utils/storage.js.map +0 -1
- package/lib/EventEmitter.d.ts +0 -37
- package/lib/EventEmitter.js +0 -94
- package/lib/EventEmitter.js.map +0 -1
- package/lib/FlowModel.d.ts +0 -146
- package/lib/FlowModel.js +0 -236
- package/lib/FlowModel.js.map +0 -1
- package/lib/Scheduler.d.ts +0 -78
- package/lib/Scheduler.js +0 -179
- package/lib/Scheduler.js.map +0 -1
- package/lib/constant/index.d.ts +0 -16
- package/lib/constant/index.js.map +0 -1
- package/lib/constant/logCode.d.ts +0 -12
- package/lib/constant/logCode.js.map +0 -1
- package/lib/expression/brewserVm.d.ts +0 -2
- package/lib/expression/brewserVm.js +0 -33
- package/lib/expression/brewserVm.js.map +0 -1
- package/lib/expression/index.d.ts +0 -2
- package/lib/expression/index.js +0 -20
- package/lib/expression/index.js.map +0 -1
- package/lib/expression/nodeVm.d.ts +0 -4
- package/lib/expression/nodeVm.js +0 -13
- package/lib/expression/nodeVm.js.map +0 -1
- package/lib/index.d.ts +0 -157
- package/lib/index.js +0 -159
- package/lib/index.js.map +0 -1
- package/lib/nodes/base.d.ts +0 -108
- package/lib/nodes/base.js +0 -152
- package/lib/nodes/base.js.map +0 -1
- package/lib/nodes/index.d.ts +0 -3
- package/lib/nodes/index.js +0 -7
- package/lib/nodes/index.js.map +0 -1
- package/lib/nodes/start.d.ts +0 -6
- package/lib/nodes/start.js +0 -15
- package/lib/nodes/start.js.map +0 -1
- package/lib/nodes/task.d.ts +0 -6
- package/lib/nodes/task.js +0 -15
- package/lib/nodes/task.js.map +0 -1
- package/lib/platform/browser/browserVm.d.ts +0 -4
- package/lib/platform/browser/browserVm.js +0 -49
- package/lib/platform/browser/browserVm.js.map +0 -1
- package/lib/platform/browser/index.d.ts +0 -4
- package/lib/platform/browser/index.js +0 -28
- package/lib/platform/browser/index.js.map +0 -1
- package/lib/platform/index.d.ts +0 -1
- package/lib/platform/index.js +0 -5
- package/lib/platform/index.js.map +0 -1
- package/lib/platform/node/index.d.ts +0 -4
- package/lib/platform/node/index.js +0 -28
- package/lib/platform/node/index.js.map +0 -1
- package/lib/platform/node/nodeVm.d.ts +0 -1
- package/lib/platform/node/nodeVm.js +0 -13
- package/lib/platform/node/nodeVm.js.map +0 -1
- package/lib/recorder/index.d.ts +0 -46
- package/lib/recorder/index.js +0 -117
- package/lib/recorder/index.js.map +0 -1
- package/lib/utils/global.d.ts +0 -5
- package/lib/utils/global.js +0 -31
- package/lib/utils/global.js.map +0 -1
- package/lib/utils/id.d.ts +0 -3
- package/lib/utils/id.js +0 -20
- package/lib/utils/id.js.map +0 -1
- package/lib/utils/index.d.ts +0 -4
- package/lib/utils/index.js +0 -9
- package/lib/utils/index.js.map +0 -1
- package/lib/utils/storage.d.ts +0 -7
- package/lib/utils/storage.js.map +0 -1
- package/src/EventEmitter.ts +0 -103
- package/src/FlowModel.ts +0 -325
- package/src/Scheduler.ts +0 -244
- package/src/constant/index.ts +0 -23
- package/src/constant/logCode.ts +0 -34
- package/src/expression/brewserVm.ts +0 -36
- package/src/expression/index.ts +0 -17
- package/src/expression/nodeVm.ts +0 -14
- package/src/index.ts +0 -300
- package/src/nodes/base.ts +0 -234
- package/src/nodes/index.ts +0 -3
- package/src/nodes/start.ts +0 -8
- package/src/nodes/task.ts +0 -8
- package/src/platform/browser/browserVm.ts +0 -52
- package/src/platform/browser/index.ts +0 -28
- package/src/platform/index.ts +0 -1
- package/src/platform/node/index.ts +0 -28
- package/src/platform/node/nodeVm.ts +0 -12
- package/src/recorder/index.ts +0 -137
- package/src/typings.d.ts +0 -0
- package/src/utils/global.ts +0 -41
- package/src/utils/id.ts +0 -16
- package/src/utils/index.ts +0 -5
- package/src/utils/storage.ts +0 -55
- /package/es/constant/{logCode.d.ts → LogCode.d.ts} +0 -0
- /package/es/constant/{index.d.ts → constant.d.ts} +0 -0
- /package/es/{utils/id.d.ts → util/ID.d.ts} +0 -0
- /package/es/{utils → util}/storage.d.ts +0 -0
package/src/nodes/base.ts
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { Engine } from '..'
|
|
2
|
-
import { ActionStatus } from '../constant'
|
|
3
|
-
import { getExpressionResult } from '../platform'
|
|
4
|
-
|
|
5
|
-
export interface IBaseNodeProps {
|
|
6
|
-
nodeConfig: BaseNode.NodeConfig
|
|
7
|
-
context: Record<string, any>
|
|
8
|
-
globalData: Record<string, unknown>
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export class BaseNode implements BaseNode.Base {
|
|
12
|
-
readonly baseType: string
|
|
13
|
-
static nodeTypeName = 'BaseNode'
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 节点的入边
|
|
17
|
-
*/
|
|
18
|
-
incoming: BaseNode.IncomingConfig[]
|
|
19
|
-
/**
|
|
20
|
-
* 节点的出边
|
|
21
|
-
*/
|
|
22
|
-
outgoing: BaseNode.OutgoingConfig[]
|
|
23
|
-
/**
|
|
24
|
-
* 节点的属性
|
|
25
|
-
*/
|
|
26
|
-
properties?: Record<string, unknown>
|
|
27
|
-
nodeId: Engine.Key
|
|
28
|
-
type: string
|
|
29
|
-
/**
|
|
30
|
-
* 节点的上下文,是调用流程时传入的上下文
|
|
31
|
-
*/
|
|
32
|
-
context: Record<string, any>
|
|
33
|
-
/**
|
|
34
|
-
* 节点的全局数据,是调用流程时传入的全局数据
|
|
35
|
-
* 在计算表达式时,即基于全局数据进行计算
|
|
36
|
-
*/
|
|
37
|
-
globalData: Record<string, unknown>
|
|
38
|
-
|
|
39
|
-
constructor({ nodeConfig, context, globalData }: IBaseNodeProps) {
|
|
40
|
-
const { outgoing, incoming, id, type, properties } = nodeConfig
|
|
41
|
-
this.baseType = 'base'
|
|
42
|
-
this.outgoing = outgoing
|
|
43
|
-
this.incoming = incoming
|
|
44
|
-
this.nodeId = id
|
|
45
|
-
this.type = type
|
|
46
|
-
this.properties = properties
|
|
47
|
-
|
|
48
|
-
this.context = context
|
|
49
|
-
this.globalData = globalData
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* 节点的执行逻辑
|
|
54
|
-
* @overridable 可以自定义节点重写此方法
|
|
55
|
-
* @param param.executionId 流程执行记录 ID
|
|
56
|
-
* @param param.actionId 此节点执行记录 ID
|
|
57
|
-
* @param param.nodeId 节点 ID
|
|
58
|
-
* @return 返回下一步的执行参数
|
|
59
|
-
* 当不返回时,表示此节点执行成功,流程会继续执行下一步。
|
|
60
|
-
* 当返回时,返回格式为
|
|
61
|
-
*/
|
|
62
|
-
public async action(
|
|
63
|
-
param?: Engine.ActionParam,
|
|
64
|
-
): Promise<BaseNode.ActionResult | undefined> {
|
|
65
|
-
console.log('action param --->>>', param)
|
|
66
|
-
return undefined
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* 节点重新恢复执行的逻辑
|
|
71
|
-
* @overridable 可以自定义节点重写此方法
|
|
72
|
-
* @param params.executionId 流程执行记录 ID
|
|
73
|
-
* @param params.actionId 此节点执行记录 ID
|
|
74
|
-
* @param params.nodeId 节点 ID
|
|
75
|
-
*/
|
|
76
|
-
public async onResume(params: Engine.ResumeParam): Promise<void> {
|
|
77
|
-
console.log('onResume params --->>>', params)
|
|
78
|
-
return undefined
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* 判断该节点是否满足条件
|
|
83
|
-
*/
|
|
84
|
-
private async isPass(properties?: Record<string, unknown>): Promise<boolean> {
|
|
85
|
-
if (!properties) return true
|
|
86
|
-
|
|
87
|
-
const { conditionExpression } = properties
|
|
88
|
-
if (!conditionExpression) return true
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
// bug:uuid 创建的 NodeId 为 xxxx-xxxx-xxxx-zzzz 格式,eval 执行时会将 - 识别为数学减号,导致执行报错
|
|
92
|
-
// 解决方案: 赋值变量直接命名为 isPassResult, 因为每次执行 getExpressionResult 时,都会重新射程一个 context
|
|
93
|
-
const result = await getExpressionResult(
|
|
94
|
-
`isPassResult = (${conditionExpression})`,
|
|
95
|
-
{
|
|
96
|
-
...this.globalData,
|
|
97
|
-
},
|
|
98
|
-
)
|
|
99
|
-
return result.isPassResult
|
|
100
|
-
} catch (error) {
|
|
101
|
-
return false
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* 获取当前节点执行的下一个节点
|
|
107
|
-
*/
|
|
108
|
-
private async getOutgoing(): Promise<BaseNode.OutgoingConfig[]> {
|
|
109
|
-
const outgoing: BaseNode.OutgoingConfig[] = []
|
|
110
|
-
const expressions: any = []
|
|
111
|
-
for (const item of this.outgoing) {
|
|
112
|
-
const { properties } = item
|
|
113
|
-
expressions.push(this.isPass(properties))
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const result = await Promise.all(expressions)
|
|
117
|
-
result.forEach((item, index) => {
|
|
118
|
-
const out = this.outgoing[index]
|
|
119
|
-
out.result = item
|
|
120
|
-
outgoing.push(out)
|
|
121
|
-
})
|
|
122
|
-
return outgoing
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* 节点的每一次执行都会生成一个唯一的 actionId
|
|
127
|
-
*/
|
|
128
|
-
public async execute(
|
|
129
|
-
params: Engine.ExecParam,
|
|
130
|
-
): Promise<Engine.NextActionParam> {
|
|
131
|
-
const { executionId, actionId } = params
|
|
132
|
-
const res = await this.action({
|
|
133
|
-
nodeId: this.nodeId,
|
|
134
|
-
executionId,
|
|
135
|
-
actionId,
|
|
136
|
-
})
|
|
137
|
-
const status = res ? res.status : 'success'
|
|
138
|
-
|
|
139
|
-
if (status === ActionStatus.SUCCESS) {
|
|
140
|
-
const outgoing = await this.getOutgoing()
|
|
141
|
-
const detail = res ? res.detail : {}
|
|
142
|
-
params.next({
|
|
143
|
-
status: ActionStatus.SUCCESS,
|
|
144
|
-
detail,
|
|
145
|
-
nodeId: this.nodeId,
|
|
146
|
-
nodeType: this.type,
|
|
147
|
-
properties: this.properties,
|
|
148
|
-
executionId,
|
|
149
|
-
actionId,
|
|
150
|
-
outgoing,
|
|
151
|
-
})
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return {
|
|
155
|
-
status,
|
|
156
|
-
detail: res?.detail,
|
|
157
|
-
executionId,
|
|
158
|
-
actionId,
|
|
159
|
-
nodeId: this.nodeId,
|
|
160
|
-
nodeType: this.type,
|
|
161
|
-
properties: this.properties,
|
|
162
|
-
outgoing: [],
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
public async resume(params: Engine.ExecResumeParam): Promise<undefined> {
|
|
167
|
-
const outgoing = await this.getOutgoing()
|
|
168
|
-
await this.onResume({
|
|
169
|
-
executionId: params.executionId,
|
|
170
|
-
actionId: params.actionId,
|
|
171
|
-
nodeId: params.nodeId,
|
|
172
|
-
data: params.data,
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
params.next({
|
|
176
|
-
executionId: params.executionId,
|
|
177
|
-
actionId: params.actionId,
|
|
178
|
-
nodeId: this.nodeId,
|
|
179
|
-
nodeType: this.type,
|
|
180
|
-
properties: this.properties,
|
|
181
|
-
outgoing,
|
|
182
|
-
status: ActionStatus.SUCCESS,
|
|
183
|
-
})
|
|
184
|
-
return undefined
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export namespace BaseNode {
|
|
189
|
-
export interface Base {
|
|
190
|
-
incoming: IncomingConfig[]
|
|
191
|
-
outgoing: OutgoingConfig[]
|
|
192
|
-
properties?: Record<string, unknown>
|
|
193
|
-
nodeId: Engine.Key
|
|
194
|
-
type: string
|
|
195
|
-
readonly baseType: string
|
|
196
|
-
execute(actionParam: Engine.ActionParam): Promise<Engine.NextActionParam>
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export type IncomingConfig = {
|
|
200
|
-
id: Engine.Key
|
|
201
|
-
source: string
|
|
202
|
-
properties?: Record<string, unknown>
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export type OutgoingConfig = {
|
|
206
|
-
id: Engine.Key
|
|
207
|
-
target: string
|
|
208
|
-
properties?: Record<string, unknown>
|
|
209
|
-
result?: string | boolean
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
export type NodeConfig = {
|
|
213
|
-
id: Engine.Key
|
|
214
|
-
type: string
|
|
215
|
-
properties?: Record<string, unknown>
|
|
216
|
-
incoming: IncomingConfig[]
|
|
217
|
-
outgoing: OutgoingConfig[]
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
export type NodeConstructor = {
|
|
221
|
-
new (config: {
|
|
222
|
-
nodeConfig: NodeConfig
|
|
223
|
-
context: Record<string, any>
|
|
224
|
-
globalData: Record<string, unknown>
|
|
225
|
-
}): BaseNode
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export type ActionResult = {
|
|
229
|
-
status: ActionStatus
|
|
230
|
-
detail?: Record<string, unknown>
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export default BaseNode
|
package/src/nodes/index.ts
DELETED
package/src/nodes/start.ts
DELETED
package/src/nodes/task.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ErrorCode,
|
|
3
|
-
getErrorMsg,
|
|
4
|
-
getWarningMsg,
|
|
5
|
-
WarningCode,
|
|
6
|
-
} from '../../constant/logCode'
|
|
7
|
-
|
|
8
|
-
const createContext = (globalData: Record<string, unknown>) => {
|
|
9
|
-
const iframe = document.createElement('iframe')
|
|
10
|
-
iframe.style.display = 'none'
|
|
11
|
-
if (!document || !document.body) {
|
|
12
|
-
console.error(getErrorMsg(ErrorCode.NO_DOCUMENT_BODY))
|
|
13
|
-
}
|
|
14
|
-
document.body.appendChild(iframe)
|
|
15
|
-
const iframeWindow = iframe.contentWindow
|
|
16
|
-
if (iframeWindow) {
|
|
17
|
-
// TODO: 确认是否需要该代码,parent 置为空是为了解决什么问题
|
|
18
|
-
// @ts-ignore
|
|
19
|
-
;(iframeWindow!.parent as any) = null
|
|
20
|
-
Object.keys(globalData).forEach((key) => {
|
|
21
|
-
iframeWindow[key] = globalData[key]
|
|
22
|
-
})
|
|
23
|
-
}
|
|
24
|
-
return iframeWindow
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const runInContext = (code: string, context) => {
|
|
28
|
-
try {
|
|
29
|
-
const iframeEval = context.eval
|
|
30
|
-
iframeEval.call(context, code)
|
|
31
|
-
if (context.iframeElement) {
|
|
32
|
-
document.body.removeChild(context.iframeElement)
|
|
33
|
-
}
|
|
34
|
-
} catch (e) {
|
|
35
|
-
console.warn(getWarningMsg(WarningCode.EXPRESSION_EXEC_ERROR), {
|
|
36
|
-
code,
|
|
37
|
-
context,
|
|
38
|
-
e,
|
|
39
|
-
})
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const runInBrowserContext = async (
|
|
44
|
-
code: string,
|
|
45
|
-
globalData = {},
|
|
46
|
-
): Promise<any> => {
|
|
47
|
-
const context = createContext(globalData)
|
|
48
|
-
runInContext(code, context)
|
|
49
|
-
return context
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export { createContext, runInContext, runInBrowserContext }
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { runInBrowserContext } from './browserVm'
|
|
2
|
-
|
|
3
|
-
const isInBrowser = typeof window === 'object' && window.window === window
|
|
4
|
-
|
|
5
|
-
const globalScope: any = (() => {
|
|
6
|
-
if (isInBrowser) {
|
|
7
|
-
return window
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (typeof self === 'object' && self.self === self) {
|
|
11
|
-
return self
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (typeof globalThis === 'object') {
|
|
15
|
-
return globalThis
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return {
|
|
19
|
-
eval: () => undefined,
|
|
20
|
-
} as Record<string, unknown>
|
|
21
|
-
})()
|
|
22
|
-
|
|
23
|
-
const getExpressionResult = async (code: string, context: any) => {
|
|
24
|
-
const r = await runInBrowserContext(code, context)
|
|
25
|
-
return r
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export { isInBrowser, globalScope, getExpressionResult }
|
package/src/platform/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './node'
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { runInNodeContext } from './nodeVm'
|
|
2
|
-
|
|
3
|
-
const isInNodeJS = typeof global === 'object' && global.global === global
|
|
4
|
-
|
|
5
|
-
const globalScope: any = (() => {
|
|
6
|
-
if (typeof self === 'object' && self.self === self) {
|
|
7
|
-
return self
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
if (isInNodeJS) {
|
|
11
|
-
return global
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (typeof globalThis === 'object') {
|
|
15
|
-
return globalThis
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return {
|
|
19
|
-
eval: () => undefined,
|
|
20
|
-
} as Record<string, unknown>
|
|
21
|
-
})()
|
|
22
|
-
|
|
23
|
-
const getExpressionResult = async (code: string, context: any) => {
|
|
24
|
-
const r = await runInNodeContext(code, context)
|
|
25
|
-
return r
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export { isInNodeJS, globalScope, getExpressionResult }
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import vm from 'node:vm'
|
|
2
|
-
// const vm = require('node:vm');
|
|
3
|
-
|
|
4
|
-
export const runInNodeContext = async (
|
|
5
|
-
code: string,
|
|
6
|
-
globalData: Record<string, unknown> = {},
|
|
7
|
-
): Promise<any> => {
|
|
8
|
-
const context = vm.createContext(globalData)
|
|
9
|
-
vm.runInContext(code, context)
|
|
10
|
-
|
|
11
|
-
return context
|
|
12
|
-
}
|
package/src/recorder/index.ts
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { Engine } from '..'
|
|
2
|
-
import { storage } from '../utils'
|
|
3
|
-
|
|
4
|
-
export const MAX_RECORDER = 100
|
|
5
|
-
export const MAX_INSTANCE = 100
|
|
6
|
-
export const LOGICFLOW_ENGINE_INSTANCES = 'LOGICFLOW_ENGINE_INSTANCES'
|
|
7
|
-
|
|
8
|
-
export class Recorder implements Recorder.Base {
|
|
9
|
-
instanceId: Engine.Key
|
|
10
|
-
maxRecorder: number
|
|
11
|
-
|
|
12
|
-
constructor({ instanceId }) {
|
|
13
|
-
this.instanceId = instanceId
|
|
14
|
-
this.maxRecorder = MAX_RECORDER
|
|
15
|
-
|
|
16
|
-
const instances = this.getItem(LOGICFLOW_ENGINE_INSTANCES) || []
|
|
17
|
-
if (instances.indexOf(instanceId) === -1) {
|
|
18
|
-
instances.push(instanceId)
|
|
19
|
-
}
|
|
20
|
-
if (instances.length > MAX_INSTANCE) {
|
|
21
|
-
const clearInstance = instances.shift()
|
|
22
|
-
this.clearInstance(clearInstance)
|
|
23
|
-
}
|
|
24
|
-
this.setItem(LOGICFLOW_ENGINE_INSTANCES, instances)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
setMaxRecorderNumber(max: number) {
|
|
28
|
-
this.maxRecorder = max
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// 将存储 storage 的方法收敛到此处,并在此处做异常处理 - setItem
|
|
32
|
-
setItem(key: string | number, value: unknown) {
|
|
33
|
-
try {
|
|
34
|
-
storage.setItem(key, value)
|
|
35
|
-
} catch (error) {
|
|
36
|
-
console.error('Ops, something wrong with storage.setItem !!!')
|
|
37
|
-
storage.clear()
|
|
38
|
-
storage.setItem(key, value)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// getItem 方法
|
|
43
|
-
getItem(key: string | number) {
|
|
44
|
-
return storage.getItem(key)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async getExecutionActions(executionId: Engine.Key) {
|
|
48
|
-
return this.getItem(executionId)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async getExecutionList() {
|
|
52
|
-
return this.getItem(this.instanceId) || []
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
private addExecution(executionId: Engine.Key) {
|
|
56
|
-
const instanceExecutions = this.getItem(this.instanceId) || []
|
|
57
|
-
if (instanceExecutions.length >= this.maxRecorder) {
|
|
58
|
-
const toBeRemovedItem = instanceExecutions.shift()
|
|
59
|
-
this.popExecution(toBeRemovedItem)
|
|
60
|
-
}
|
|
61
|
-
instanceExecutions.push(executionId)
|
|
62
|
-
this.setItem(this.instanceId, instanceExecutions)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private popExecution(executionId: Engine.Key) {
|
|
66
|
-
const instanceData = this.getItem(executionId) || []
|
|
67
|
-
instanceData.forEach((actionId) => {
|
|
68
|
-
storage.removeItem(actionId)
|
|
69
|
-
})
|
|
70
|
-
storage.removeItem(executionId)
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
private pushActionToExecution(executionId: Engine.Key, actionId: Engine.Key) {
|
|
74
|
-
const actions = this.getItem(executionId) || []
|
|
75
|
-
actions.push(actionId)
|
|
76
|
-
this.setItem(executionId, actions)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* @param {Object} action
|
|
81
|
-
* {
|
|
82
|
-
* actionId: '',
|
|
83
|
-
* nodeId: '',
|
|
84
|
-
* executionId: '',
|
|
85
|
-
* nodeType: '',
|
|
86
|
-
* timestamp: '',
|
|
87
|
-
* properties: {},
|
|
88
|
-
* }
|
|
89
|
-
*/
|
|
90
|
-
async addActionRecord(action: Recorder.Info) {
|
|
91
|
-
const { executionId, actionId } = action
|
|
92
|
-
const instanceData = await this.getExecutionActions(executionId)
|
|
93
|
-
|
|
94
|
-
if (!instanceData) {
|
|
95
|
-
this.addExecution(executionId)
|
|
96
|
-
}
|
|
97
|
-
this.pushActionToExecution(executionId, actionId)
|
|
98
|
-
this.setItem(actionId, action)
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
async getActionRecord(actionId: Engine.Key): Promise<Recorder.Info> {
|
|
102
|
-
return this.getItem(actionId)
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
clear() {
|
|
106
|
-
this.clearInstance(this.instanceId)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
clearInstance(instanceId: Engine.Key) {
|
|
110
|
-
const instanceExecutions = this.getItem(instanceId) || []
|
|
111
|
-
// TODO: 完善类型定义
|
|
112
|
-
instanceExecutions.forEach((executionId) => {
|
|
113
|
-
storage.removeItem(executionId)
|
|
114
|
-
const instanceData = this.getItem(executionId) || []
|
|
115
|
-
instanceData.forEach((actionId) => {
|
|
116
|
-
storage.removeItem(actionId)
|
|
117
|
-
})
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
storage.removeItem(instanceId)
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export namespace Recorder {
|
|
125
|
-
export interface Base {
|
|
126
|
-
addActionRecord: (action: Info) => Promise<void>
|
|
127
|
-
getActionRecord: (actionId: Engine.Key) => Promise<Info>
|
|
128
|
-
getExecutionActions: (executionId: Engine.Key) => Promise<string[]>
|
|
129
|
-
clear: () => void
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export type Info = {
|
|
133
|
-
timestamp: number
|
|
134
|
-
} & Engine.NextActionParam
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export default Recorder
|
package/src/typings.d.ts
DELETED
|
File without changes
|
package/src/utils/global.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
// 判断当前环境是否为服务端
|
|
2
|
-
// const isServer = typeof window === undefined;
|
|
3
|
-
|
|
4
|
-
// const isServer = process.env.BROWSER === true;
|
|
5
|
-
|
|
6
|
-
const isInBrowser = typeof window === 'object' && window.window === window
|
|
7
|
-
|
|
8
|
-
const isInNodeJS = typeof global === 'object' && global.global === global
|
|
9
|
-
|
|
10
|
-
const isInWebWorker =
|
|
11
|
-
!isInBrowser && typeof self === 'object' && self.constructor
|
|
12
|
-
|
|
13
|
-
const globalScope: any = (() => {
|
|
14
|
-
if (isInBrowser) {
|
|
15
|
-
return window
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (typeof self === 'object' && self.self === self) {
|
|
19
|
-
return self
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (isInNodeJS) {
|
|
23
|
-
return global
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (typeof globalThis === 'object') {
|
|
27
|
-
return globalThis
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return {
|
|
31
|
-
eval: () => undefined,
|
|
32
|
-
} as Record<string, unknown>
|
|
33
|
-
})()
|
|
34
|
-
|
|
35
|
-
export {
|
|
36
|
-
// 环境相关方法
|
|
37
|
-
globalScope,
|
|
38
|
-
isInWebWorker,
|
|
39
|
-
isInBrowser,
|
|
40
|
-
isInNodeJS,
|
|
41
|
-
}
|
package/src/utils/id.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { v4 as uuidV4 } from 'uuid'
|
|
2
|
-
|
|
3
|
-
export const createExecId = (): string => {
|
|
4
|
-
const uuid = uuidV4()
|
|
5
|
-
return `exec-${uuid}`
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export const createActionId = (): string => {
|
|
9
|
-
const uuid = uuidV4()
|
|
10
|
-
return `action-${uuid}`
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const createEngineId = (): string => {
|
|
14
|
-
const uuid = uuidV4()
|
|
15
|
-
return `engine-${uuid}`
|
|
16
|
-
}
|
package/src/utils/index.ts
DELETED
package/src/utils/storage.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 存储执行记录
|
|
3
|
-
*/
|
|
4
|
-
import { globalScope } from './global'
|
|
5
|
-
|
|
6
|
-
if (!globalScope.sessionStorage) {
|
|
7
|
-
const storage = {
|
|
8
|
-
data: {} as Record<string, unknown>,
|
|
9
|
-
|
|
10
|
-
setItem(key, value) {
|
|
11
|
-
storage.data[key] = value
|
|
12
|
-
},
|
|
13
|
-
|
|
14
|
-
getItem(key) {
|
|
15
|
-
return storage.data[key]
|
|
16
|
-
},
|
|
17
|
-
|
|
18
|
-
removeItem(key) {
|
|
19
|
-
delete storage.data[key]
|
|
20
|
-
},
|
|
21
|
-
|
|
22
|
-
clear() {
|
|
23
|
-
storage.data = {}
|
|
24
|
-
},
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
globalScope.sessionStorage = storage
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export default {
|
|
31
|
-
setItem(key, value) {
|
|
32
|
-
if (typeof value === 'object') {
|
|
33
|
-
value = JSON.stringify(value)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
globalScope.sessionStorage.setItem(key, value)
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
getItem(key) {
|
|
40
|
-
const value = globalScope.sessionStorage.getItem(key)
|
|
41
|
-
try {
|
|
42
|
-
return JSON.parse(value)
|
|
43
|
-
} catch (error) {
|
|
44
|
-
return value
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
removeItem(key) {
|
|
49
|
-
globalScope.sessionStorage.removeItem(key)
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
clear() {
|
|
53
|
-
globalScope.sessionStorage.clear()
|
|
54
|
-
},
|
|
55
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|