@ticatec/dyna-js 0.0.2 → 0.0.3
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 +22 -53
- package/README_CN.md +27 -58
- package/dist/DynaJs.d.ts +1 -3
- package/dist/DynaJs.d.ts.map +1 -1
- package/dist/DynaJs.esm.js +0 -54
- package/dist/DynaJs.js +6 -60
- package/dist/types.d.ts +0 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,14 +11,12 @@ A TypeScript library for safe dynamic code execution using `new Function()` that
|
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
13
|
|
|
14
|
-
✅ **Universal Compatibility** - Works in both Node.js and browser environments
|
|
15
|
-
✅ **TypeScript Support** - Full type safety with comprehensive type definitions
|
|
16
|
-
✅ **Singleton Pattern** - Initialize once, use anywhere
|
|
17
|
-
✅ **Module Imports** - Pre-define classes and functions for dynamic code
|
|
18
|
-
✅ **
|
|
19
|
-
✅ **
|
|
20
|
-
✅ **Form Class Creation** - Special method for creating dynamic form classes
|
|
21
|
-
✅ **Multiple Build Formats** - CommonJS and ESM support
|
|
14
|
+
✅ **Universal Compatibility** - Works in both Node.js and browser environments
|
|
15
|
+
✅ **TypeScript Support** - Full type safety with comprehensive type definitions
|
|
16
|
+
✅ **Singleton Pattern** - Initialize once, use anywhere
|
|
17
|
+
✅ **Module Imports** - Pre-define classes and functions for dynamic code
|
|
18
|
+
✅ **Configurable Security** - Fine-grained control over allowed APIs and operations
|
|
19
|
+
✅ **Multiple Build Formats** - CommonJS and ESM support
|
|
22
20
|
✅ **Performance Monitoring** - Built-in execution time tracking
|
|
23
21
|
|
|
24
22
|
## Installation
|
|
@@ -44,8 +42,6 @@ initializeDynaJs({
|
|
|
44
42
|
FlexiContext: FlexiContextClass,
|
|
45
43
|
ModuleLoader: ModuleLoaderClass
|
|
46
44
|
},
|
|
47
|
-
defaultInjectedKeys: ['Dialog', 'MessageBox', 'Indicator', 'Toast'],
|
|
48
|
-
useProxyByDefault: true,
|
|
49
45
|
allowBrowserAPIs: false, // Secure by default
|
|
50
46
|
validateCode: true
|
|
51
47
|
});
|
|
@@ -59,18 +55,18 @@ import { getDynaJs } from '@ticatec/dyna-js';
|
|
|
59
55
|
// Get the initialized loader
|
|
60
56
|
const loader = getDynaJs();
|
|
61
57
|
|
|
62
|
-
// Create form classes
|
|
63
|
-
const MyFormClass = loader.
|
|
58
|
+
// Create form classes
|
|
59
|
+
const MyFormClass = loader.executeSync(`
|
|
64
60
|
class CustomForm extends FlexiForm {
|
|
65
61
|
constructor() {
|
|
66
62
|
super();
|
|
67
|
-
this.dialog = Dialog;
|
|
63
|
+
this.dialog = Dialog;
|
|
68
64
|
}
|
|
69
|
-
|
|
65
|
+
|
|
70
66
|
show() {
|
|
71
|
-
MessageBox.info('Form is ready!');
|
|
67
|
+
MessageBox.info('Form is ready!');
|
|
72
68
|
}
|
|
73
|
-
|
|
69
|
+
|
|
74
70
|
render() {
|
|
75
71
|
return new FlexiCard({
|
|
76
72
|
title: 'Dynamic Form',
|
|
@@ -79,7 +75,7 @@ const MyFormClass = loader.createFormClass(`
|
|
|
79
75
|
}
|
|
80
76
|
}
|
|
81
77
|
return CustomForm;
|
|
82
|
-
`);
|
|
78
|
+
`).result;
|
|
83
79
|
|
|
84
80
|
// Instantiate and use
|
|
85
81
|
const form = new MyFormClass();
|
|
@@ -90,30 +86,6 @@ form.show();
|
|
|
90
86
|
|
|
91
87
|
### Core Methods
|
|
92
88
|
|
|
93
|
-
#### `createFormClass<T>(code: string, context?: object, injectedKeys?: string[]): T`
|
|
94
|
-
|
|
95
|
-
Creates a form class with proxy-based sandbox execution.
|
|
96
|
-
|
|
97
|
-
```typescript
|
|
98
|
-
const FormClass = loader.createFormClass(`
|
|
99
|
-
class MyForm extends FlexiForm {
|
|
100
|
-
constructor() {
|
|
101
|
-
super();
|
|
102
|
-
this.setupDialog();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
setupDialog() {
|
|
106
|
-
this.dialog = new Dialog({
|
|
107
|
-
title: 'Dynamic Dialog'
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return MyForm;
|
|
112
|
-
`, {
|
|
113
|
-
customData: 'additional context'
|
|
114
|
-
}, ['extraKey']);
|
|
115
|
-
```
|
|
116
|
-
|
|
117
89
|
#### `executeSync<T>(code: string, options?: ExecutionOptions): ExecutionResult<T>`
|
|
118
90
|
|
|
119
91
|
Synchronously execute code with result and timing information.
|
|
@@ -169,8 +141,6 @@ interface DynaJsConfig {
|
|
|
169
141
|
allowedGlobals?: string[]; // Whitelist of allowed global variables
|
|
170
142
|
blockedGlobals?: string[]; // Blacklist of blocked variables
|
|
171
143
|
defaultImports?: object; // Pre-imported classes/functions
|
|
172
|
-
defaultInjectedKeys?: string[]; // Auto-injected variable names
|
|
173
|
-
useProxyByDefault?: boolean; // Default: true
|
|
174
144
|
allowTimers?: boolean; // Allow setTimeout/setInterval (Default: false)
|
|
175
145
|
allowDynamicImports?: boolean; // Allow import()/require() (Default: false)
|
|
176
146
|
validateCode?: boolean; // Enable code validation (Default: true)
|
|
@@ -229,8 +199,7 @@ initializeDynaJs({
|
|
|
229
199
|
},
|
|
230
200
|
allowBrowserAPIs: false, // Still secure
|
|
231
201
|
allowTimers: false, // No timers needed for forms
|
|
232
|
-
validateCode: true
|
|
233
|
-
defaultInjectedKeys: ['window'] // Only inject window reference
|
|
202
|
+
validateCode: true // Keep validation
|
|
234
203
|
});
|
|
235
204
|
```
|
|
236
205
|
|
|
@@ -239,14 +208,14 @@ initializeDynaJs({
|
|
|
239
208
|
### Creating Dynamic Form Components
|
|
240
209
|
|
|
241
210
|
```typescript
|
|
242
|
-
const DynamicFormBuilder = loader.
|
|
211
|
+
const DynamicFormBuilder = loader.executeSync(`
|
|
243
212
|
class FormBuilder extends FlexiForm {
|
|
244
213
|
constructor(config) {
|
|
245
214
|
super();
|
|
246
215
|
this.config = config;
|
|
247
216
|
this.components = [];
|
|
248
217
|
}
|
|
249
|
-
|
|
218
|
+
|
|
250
219
|
addField(fieldConfig) {
|
|
251
220
|
const field = new FlexiCard({
|
|
252
221
|
title: fieldConfig.label,
|
|
@@ -255,7 +224,7 @@ const DynamicFormBuilder = loader.createFormClass(`
|
|
|
255
224
|
this.components.push(field);
|
|
256
225
|
return this;
|
|
257
226
|
}
|
|
258
|
-
|
|
227
|
+
|
|
259
228
|
createInput(type) {
|
|
260
229
|
switch(type) {
|
|
261
230
|
case 'text':
|
|
@@ -266,11 +235,11 @@ const DynamicFormBuilder = loader.createFormClass(`
|
|
|
266
235
|
return '<input type="text" />';
|
|
267
236
|
}
|
|
268
237
|
}
|
|
269
|
-
|
|
238
|
+
|
|
270
239
|
build() {
|
|
271
240
|
return this.components;
|
|
272
241
|
}
|
|
273
|
-
|
|
242
|
+
|
|
274
243
|
show() {
|
|
275
244
|
const dialog = new Dialog({
|
|
276
245
|
title: this.config.title,
|
|
@@ -278,14 +247,14 @@ const DynamicFormBuilder = loader.createFormClass(`
|
|
|
278
247
|
});
|
|
279
248
|
dialog.show();
|
|
280
249
|
}
|
|
281
|
-
|
|
250
|
+
|
|
282
251
|
render() {
|
|
283
252
|
return this.components.map(c => c.render()).join('');
|
|
284
253
|
}
|
|
285
254
|
}
|
|
286
|
-
|
|
255
|
+
|
|
287
256
|
return FormBuilder;
|
|
288
|
-
`);
|
|
257
|
+
`).result;
|
|
289
258
|
|
|
290
259
|
// Use the dynamically created form builder
|
|
291
260
|
const formBuilder = new DynamicFormBuilder({
|
package/README_CN.md
CHANGED
|
@@ -11,14 +11,12 @@
|
|
|
11
11
|
|
|
12
12
|
## 功能特性
|
|
13
13
|
|
|
14
|
-
✅ **通用兼容性** - 同时支持 Node.js 和浏览器环境
|
|
15
|
-
✅ **TypeScript 支持** - 完整的类型安全和类型定义
|
|
16
|
-
✅ **单例模式** - 一次初始化,全局使用
|
|
17
|
-
✅ **模块导入** - 为动态代码预定义类和函数
|
|
18
|
-
✅
|
|
19
|
-
✅
|
|
20
|
-
✅ **表单类创建** - 专门用于创建动态表单类的方法
|
|
21
|
-
✅ **多种构建格式** - 支持 CommonJS 和 ESM
|
|
14
|
+
✅ **通用兼容性** - 同时支持 Node.js 和浏览器环境
|
|
15
|
+
✅ **TypeScript 支持** - 完整的类型安全和类型定义
|
|
16
|
+
✅ **单例模式** - 一次初始化,全局使用
|
|
17
|
+
✅ **模块导入** - 为动态代码预定义类和函数
|
|
18
|
+
✅ **可配置安全性** - 对允许的 API 和操作进行细粒度控制
|
|
19
|
+
✅ **多种构建格式** - 支持 CommonJS 和 ESM
|
|
22
20
|
✅ **性能监控** - 内置执行时间跟踪
|
|
23
21
|
|
|
24
22
|
## 安装
|
|
@@ -44,8 +42,6 @@ initializeDynaJs({
|
|
|
44
42
|
FlexiContext: FlexiContextClass,
|
|
45
43
|
ModuleLoader: ModuleLoaderClass
|
|
46
44
|
},
|
|
47
|
-
defaultInjectedKeys: ['Dialog', 'MessageBox', 'Indicator', 'Toast'],
|
|
48
|
-
useProxyByDefault: true,
|
|
49
45
|
allowBrowserAPIs: false, // 默认安全
|
|
50
46
|
validateCode: true
|
|
51
47
|
});
|
|
@@ -60,17 +56,17 @@ import { getDynaJs } from '@ticatec/dyna-js';
|
|
|
60
56
|
const loader = getDynaJs();
|
|
61
57
|
|
|
62
58
|
// 创建表单类
|
|
63
|
-
const MyFormClass = loader.
|
|
59
|
+
const MyFormClass = loader.executeSync(`
|
|
64
60
|
class CustomForm extends FlexiForm {
|
|
65
61
|
constructor() {
|
|
66
62
|
super();
|
|
67
|
-
this.dialog = Dialog;
|
|
63
|
+
this.dialog = Dialog;
|
|
68
64
|
}
|
|
69
|
-
|
|
65
|
+
|
|
70
66
|
show() {
|
|
71
|
-
MessageBox.info('表单已就绪!');
|
|
67
|
+
MessageBox.info('表单已就绪!');
|
|
72
68
|
}
|
|
73
|
-
|
|
69
|
+
|
|
74
70
|
render() {
|
|
75
71
|
return new FlexiCard({
|
|
76
72
|
title: '动态表单',
|
|
@@ -79,7 +75,7 @@ const MyFormClass = loader.createFormClass(`
|
|
|
79
75
|
}
|
|
80
76
|
}
|
|
81
77
|
return CustomForm;
|
|
82
|
-
`);
|
|
78
|
+
`).result;
|
|
83
79
|
|
|
84
80
|
// 实例化并使用
|
|
85
81
|
const form = new MyFormClass();
|
|
@@ -90,30 +86,6 @@ form.show();
|
|
|
90
86
|
|
|
91
87
|
### 核心方法
|
|
92
88
|
|
|
93
|
-
#### `createFormClass<T>(code: string, context?: object, injectedKeys?: string[]): T`
|
|
94
|
-
|
|
95
|
-
使用基于代理的沙盒执行创建表单类。
|
|
96
|
-
|
|
97
|
-
```typescript
|
|
98
|
-
const FormClass = loader.createFormClass(`
|
|
99
|
-
class MyForm extends FlexiForm {
|
|
100
|
-
constructor() {
|
|
101
|
-
super();
|
|
102
|
-
this.setupDialog();
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
setupDialog() {
|
|
106
|
-
this.dialog = new Dialog({
|
|
107
|
-
title: '动态对话框'
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return MyForm;
|
|
112
|
-
`, {
|
|
113
|
-
customData: '附加上下文'
|
|
114
|
-
}, ['extraKey']);
|
|
115
|
-
```
|
|
116
|
-
|
|
117
89
|
#### `executeSync<T>(code: string, options?: ExecutionOptions): ExecutionResult<T>`
|
|
118
90
|
|
|
119
91
|
同步执行代码并返回结果和计时信息。
|
|
@@ -169,8 +141,6 @@ interface DynaJsConfig {
|
|
|
169
141
|
allowedGlobals?: string[]; // 允许的全局变量白名单
|
|
170
142
|
blockedGlobals?: string[]; // 阻止的变量黑名单
|
|
171
143
|
defaultImports?: object; // 预导入的类/函数
|
|
172
|
-
defaultInjectedKeys?: string[]; // 自动注入的变量名
|
|
173
|
-
useProxyByDefault?: boolean; // 默认:true
|
|
174
144
|
allowTimers?: boolean; // 允许 setTimeout/setInterval(默认:false)
|
|
175
145
|
allowDynamicImports?: boolean; // 允许 import()/require()(默认:false)
|
|
176
146
|
validateCode?: boolean; // 启用代码验证(默认:true)
|
|
@@ -229,8 +199,7 @@ initializeDynaJs({
|
|
|
229
199
|
},
|
|
230
200
|
allowBrowserAPIs: false, // 仍然安全
|
|
231
201
|
allowTimers: false, // 表单不需要定时器
|
|
232
|
-
validateCode: true
|
|
233
|
-
defaultInjectedKeys: ['window'] // 只注入 window 引用
|
|
202
|
+
validateCode: true // 保持验证
|
|
234
203
|
});
|
|
235
204
|
```
|
|
236
205
|
|
|
@@ -239,14 +208,14 @@ initializeDynaJs({
|
|
|
239
208
|
### 创建动态表单组件
|
|
240
209
|
|
|
241
210
|
```typescript
|
|
242
|
-
const DynamicFormBuilder = loader.
|
|
211
|
+
const DynamicFormBuilder = loader.executeSync(`
|
|
243
212
|
class FormBuilder extends FlexiForm {
|
|
244
213
|
constructor(config) {
|
|
245
214
|
super();
|
|
246
215
|
this.config = config;
|
|
247
216
|
this.components = [];
|
|
248
217
|
}
|
|
249
|
-
|
|
218
|
+
|
|
250
219
|
addField(fieldConfig) {
|
|
251
220
|
const field = new FlexiCard({
|
|
252
221
|
title: fieldConfig.label,
|
|
@@ -255,7 +224,7 @@ const DynamicFormBuilder = loader.createFormClass(`
|
|
|
255
224
|
this.components.push(field);
|
|
256
225
|
return this;
|
|
257
226
|
}
|
|
258
|
-
|
|
227
|
+
|
|
259
228
|
createInput(type) {
|
|
260
229
|
switch(type) {
|
|
261
230
|
case 'text':
|
|
@@ -266,11 +235,11 @@ const DynamicFormBuilder = loader.createFormClass(`
|
|
|
266
235
|
return '<input type="text" />';
|
|
267
236
|
}
|
|
268
237
|
}
|
|
269
|
-
|
|
238
|
+
|
|
270
239
|
build() {
|
|
271
240
|
return this.components;
|
|
272
241
|
}
|
|
273
|
-
|
|
242
|
+
|
|
274
243
|
show() {
|
|
275
244
|
const dialog = new Dialog({
|
|
276
245
|
title: this.config.title,
|
|
@@ -278,14 +247,14 @@ const DynamicFormBuilder = loader.createFormClass(`
|
|
|
278
247
|
});
|
|
279
248
|
dialog.show();
|
|
280
249
|
}
|
|
281
|
-
|
|
250
|
+
|
|
282
251
|
render() {
|
|
283
252
|
return this.components.map(c => c.render()).join('');
|
|
284
253
|
}
|
|
285
254
|
}
|
|
286
|
-
|
|
255
|
+
|
|
287
256
|
return FormBuilder;
|
|
288
|
-
`);
|
|
257
|
+
`).result;
|
|
289
258
|
|
|
290
259
|
// 使用动态创建的表单构建器
|
|
291
260
|
const formBuilder = new DynamicFormBuilder({
|
|
@@ -395,9 +364,9 @@ MIT 许可证 - 详情请查看 [LICENSE](LICENSE) 文件。
|
|
|
395
364
|
### 动态表单创建
|
|
396
365
|
```typescript
|
|
397
366
|
// 适合创建各种动态表单组件
|
|
398
|
-
const ContactForm = loader.
|
|
399
|
-
const SurveyForm = loader.
|
|
400
|
-
const RegistrationForm = loader.
|
|
367
|
+
const ContactForm = loader.executeSync(`...`).result;
|
|
368
|
+
const SurveyForm = loader.executeSync(`...`).result;
|
|
369
|
+
const RegistrationForm = loader.executeSync(`...`).result;
|
|
401
370
|
```
|
|
402
371
|
|
|
403
372
|
### 业务规则执行
|
|
@@ -408,13 +377,13 @@ const businessRule = loader.executeSync(`
|
|
|
408
377
|
// 复杂的业务逻辑
|
|
409
378
|
return processBusinessRule(data);
|
|
410
379
|
};
|
|
411
|
-
`);
|
|
380
|
+
`).result;
|
|
412
381
|
```
|
|
413
382
|
|
|
414
383
|
### 模板渲染
|
|
415
384
|
```typescript
|
|
416
385
|
// 动态模板处理
|
|
417
|
-
const templateEngine = loader.
|
|
386
|
+
const templateEngine = loader.executeSync(`
|
|
418
387
|
class TemplateEngine {
|
|
419
388
|
render(template, data) {
|
|
420
389
|
// 模板渲染逻辑
|
|
@@ -422,5 +391,5 @@ const templateEngine = loader.createFormClass(`
|
|
|
422
391
|
}
|
|
423
392
|
}
|
|
424
393
|
return TemplateEngine;
|
|
425
|
-
`);
|
|
394
|
+
`).result;
|
|
426
395
|
```
|
package/dist/DynaJs.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ExecutionOptions, ExecutionResult, DynaJsConfig, ModuleImports } from './types';
|
|
2
2
|
export default class DynaJs {
|
|
3
3
|
private config;
|
|
4
4
|
constructor(config?: DynaJsConfig);
|
|
@@ -9,8 +9,6 @@ export default class DynaJs {
|
|
|
9
9
|
createFunction<T extends (...args: any[]) => any>(code: string, paramNames?: string[], options?: ExecutionOptions): T;
|
|
10
10
|
executeWithImports<T = any>(code: string, imports: ModuleImports, options?: ExecutionOptions): ExecutionResult<T>;
|
|
11
11
|
executeWithImportsAsync<T = any>(code: string, imports: ModuleImports, options?: ExecutionOptions): Promise<ExecutionResult<T>>;
|
|
12
|
-
createFormClass<T = any>(code: string, context?: ExecutionContext, injectedKeys?: string[]): T;
|
|
13
|
-
private executeWithProxy;
|
|
14
12
|
static instance: DynaJs | null;
|
|
15
13
|
static initialize(config: DynaJsConfig): DynaJs;
|
|
16
14
|
static getInstance(): DynaJs;
|
package/dist/DynaJs.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DynaJs.d.ts","sourceRoot":"","sources":["../src/DynaJs.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"DynaJs.d.ts","sourceRoot":"","sources":["../src/DynaJs.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,aAAa,EACd,MAAM,SAAS,CAAC;AAGjB,MAAM,CAAC,OAAO,OAAO,MAAM;IACzB,OAAO,CAAC,MAAM,CAAyB;gBAE3B,MAAM,GAAE,YAAiB;IAe/B,OAAO,CAAC,CAAC,GAAG,GAAG,EACnB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,gBAAqB,GAC7B,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IA4B9B,WAAW,CAAC,CAAC,GAAG,GAAG,EACjB,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE,gBAAqB,GAC7B,eAAe,CAAC,CAAC,CAAC;YA4BP,kBAAkB;IAsBhC,OAAO,CAAC,WAAW;IAenB,cAAc,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EAC9C,IAAI,EAAE,MAAM,EACZ,UAAU,GAAE,MAAM,EAAO,EACzB,OAAO,GAAE,gBAAqB,GAC7B,CAAC;IAuBJ,kBAAkB,CAAC,CAAC,GAAG,GAAG,EACxB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,aAAa,EACtB,OAAO,GAAE,gBAAqB,GAC7B,eAAe,CAAC,CAAC,CAAC;IAOf,uBAAuB,CAAC,CAAC,GAAG,GAAG,EACnC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,aAAa,EACtB,OAAO,GAAE,gBAAqB,GAC7B,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;IAO9B,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAQ;IAEtC,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM;IAO/C,MAAM,CAAC,WAAW,IAAI,MAAM;IAO5B,MAAM,CAAC,KAAK,IAAI,IAAI;IAIpB,OAAO,CAAC,cAAc;CAwBvB"}
|
package/dist/DynaJs.esm.js
CHANGED
|
@@ -7,8 +7,6 @@ class DynaJs {
|
|
|
7
7
|
allowedGlobals: config.allowedGlobals ?? [],
|
|
8
8
|
blockedGlobals: config.blockedGlobals ?? [],
|
|
9
9
|
defaultImports: config.defaultImports ?? {},
|
|
10
|
-
defaultInjectedKeys: config.defaultInjectedKeys ?? [],
|
|
11
|
-
useProxyByDefault: config.useProxyByDefault ?? true,
|
|
12
10
|
allowTimers: config.allowTimers ?? false,
|
|
13
11
|
allowDynamicImports: config.allowDynamicImports ?? false,
|
|
14
12
|
validateCode: config.validateCode ?? true,
|
|
@@ -123,58 +121,6 @@ class DynaJs {
|
|
|
123
121
|
imports: { ...this.config.defaultImports, ...imports }
|
|
124
122
|
});
|
|
125
123
|
}
|
|
126
|
-
createFormClass(code, context = {}, injectedKeys = []) {
|
|
127
|
-
const useProxy = this.config.useProxyByDefault;
|
|
128
|
-
const mergedContext = { ...context, ...this.config.defaultImports };
|
|
129
|
-
const mergedKeys = [...this.config.defaultInjectedKeys, ...injectedKeys];
|
|
130
|
-
if (useProxy) {
|
|
131
|
-
return this.executeWithProxy(code, mergedContext, mergedKeys);
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
return this.executeSync(code, {
|
|
135
|
-
context: mergedContext,
|
|
136
|
-
imports: this.config.defaultImports
|
|
137
|
-
}).result;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
executeWithProxy(code, context, injectedKeys) {
|
|
141
|
-
const injected = {};
|
|
142
|
-
for (const key of injectedKeys) {
|
|
143
|
-
if (context.window && key in context.window) {
|
|
144
|
-
injected[key] = context.window[key];
|
|
145
|
-
}
|
|
146
|
-
else if (key in context) {
|
|
147
|
-
injected[key] = context[key];
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
const sandboxContext = { ...context, ...injected };
|
|
151
|
-
const sandbox = new Proxy(sandboxContext, {
|
|
152
|
-
has(target, key) {
|
|
153
|
-
if (key in target)
|
|
154
|
-
return true;
|
|
155
|
-
if (context.window && key in context.window)
|
|
156
|
-
return true;
|
|
157
|
-
return false;
|
|
158
|
-
},
|
|
159
|
-
get(target, key) {
|
|
160
|
-
if (key in target)
|
|
161
|
-
return target[key];
|
|
162
|
-
if (context.window && key in context.window)
|
|
163
|
-
return context.window[key];
|
|
164
|
-
console.warn(`DynaJs: ${String(key)} not found`);
|
|
165
|
-
return undefined;
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
const wrappedCode = `
|
|
169
|
-
with (sandbox) {
|
|
170
|
-
${Object.keys(sandbox).map(k => `const ${k} = sandbox.${k};`).join('\n')}
|
|
171
|
-
|
|
172
|
-
${code}
|
|
173
|
-
}
|
|
174
|
-
`;
|
|
175
|
-
const fn = new Function('sandbox', wrappedCode);
|
|
176
|
-
return fn(sandbox);
|
|
177
|
-
}
|
|
178
124
|
static initialize(config) {
|
|
179
125
|
if (DynaJs.instance == null) {
|
|
180
126
|
DynaJs.instance = new DynaJs(config);
|
package/dist/DynaJs.js
CHANGED
|
@@ -3,20 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const utils_1 = require("./utils");
|
|
4
4
|
class DynaJs {
|
|
5
5
|
constructor(config = {}) {
|
|
6
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k
|
|
6
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
7
7
|
this.config = {
|
|
8
8
|
defaultTimeout: (_a = config.defaultTimeout) !== null && _a !== void 0 ? _a : 5000,
|
|
9
9
|
defaultStrict: (_b = config.defaultStrict) !== null && _b !== void 0 ? _b : true,
|
|
10
10
|
allowedGlobals: (_c = config.allowedGlobals) !== null && _c !== void 0 ? _c : [],
|
|
11
11
|
blockedGlobals: (_d = config.blockedGlobals) !== null && _d !== void 0 ? _d : [],
|
|
12
12
|
defaultImports: (_e = config.defaultImports) !== null && _e !== void 0 ? _e : {},
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
allowBrowserAPIs: (_l = config.allowBrowserAPIs) !== null && _l !== void 0 ? _l : false,
|
|
19
|
-
allowNodeAPIs: (_m = config.allowNodeAPIs) !== null && _m !== void 0 ? _m : false
|
|
13
|
+
allowTimers: (_f = config.allowTimers) !== null && _f !== void 0 ? _f : false,
|
|
14
|
+
allowDynamicImports: (_g = config.allowDynamicImports) !== null && _g !== void 0 ? _g : false,
|
|
15
|
+
validateCode: (_h = config.validateCode) !== null && _h !== void 0 ? _h : true,
|
|
16
|
+
allowBrowserAPIs: (_j = config.allowBrowserAPIs) !== null && _j !== void 0 ? _j : false,
|
|
17
|
+
allowNodeAPIs: (_k = config.allowNodeAPIs) !== null && _k !== void 0 ? _k : false
|
|
20
18
|
};
|
|
21
19
|
}
|
|
22
20
|
async execute(code, options = {}) {
|
|
@@ -123,58 +121,6 @@ class DynaJs {
|
|
|
123
121
|
async executeWithImportsAsync(code, imports, options = {}) {
|
|
124
122
|
return this.execute(code, Object.assign(Object.assign({}, options), { imports: Object.assign(Object.assign({}, this.config.defaultImports), imports) }));
|
|
125
123
|
}
|
|
126
|
-
createFormClass(code, context = {}, injectedKeys = []) {
|
|
127
|
-
const useProxy = this.config.useProxyByDefault;
|
|
128
|
-
const mergedContext = Object.assign(Object.assign({}, context), this.config.defaultImports);
|
|
129
|
-
const mergedKeys = [...this.config.defaultInjectedKeys, ...injectedKeys];
|
|
130
|
-
if (useProxy) {
|
|
131
|
-
return this.executeWithProxy(code, mergedContext, mergedKeys);
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
return this.executeSync(code, {
|
|
135
|
-
context: mergedContext,
|
|
136
|
-
imports: this.config.defaultImports
|
|
137
|
-
}).result;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
executeWithProxy(code, context, injectedKeys) {
|
|
141
|
-
const injected = {};
|
|
142
|
-
for (const key of injectedKeys) {
|
|
143
|
-
if (context.window && key in context.window) {
|
|
144
|
-
injected[key] = context.window[key];
|
|
145
|
-
}
|
|
146
|
-
else if (key in context) {
|
|
147
|
-
injected[key] = context[key];
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
const sandboxContext = Object.assign(Object.assign({}, context), injected);
|
|
151
|
-
const sandbox = new Proxy(sandboxContext, {
|
|
152
|
-
has(target, key) {
|
|
153
|
-
if (key in target)
|
|
154
|
-
return true;
|
|
155
|
-
if (context.window && key in context.window)
|
|
156
|
-
return true;
|
|
157
|
-
return false;
|
|
158
|
-
},
|
|
159
|
-
get(target, key) {
|
|
160
|
-
if (key in target)
|
|
161
|
-
return target[key];
|
|
162
|
-
if (context.window && key in context.window)
|
|
163
|
-
return context.window[key];
|
|
164
|
-
console.warn(`DynaJs: ${String(key)} not found`);
|
|
165
|
-
return undefined;
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
const wrappedCode = `
|
|
169
|
-
with (sandbox) {
|
|
170
|
-
${Object.keys(sandbox).map(k => `const ${k} = sandbox.${k};`).join('\n')}
|
|
171
|
-
|
|
172
|
-
${code}
|
|
173
|
-
}
|
|
174
|
-
`;
|
|
175
|
-
const fn = new Function('sandbox', wrappedCode);
|
|
176
|
-
return fn(sandbox);
|
|
177
|
-
}
|
|
178
124
|
static initialize(config) {
|
|
179
125
|
if (DynaJs.instance == null) {
|
|
180
126
|
DynaJs.instance = new DynaJs(config);
|
package/dist/types.d.ts
CHANGED
|
@@ -22,8 +22,6 @@ export interface DynaJsConfig {
|
|
|
22
22
|
allowedGlobals?: string[];
|
|
23
23
|
blockedGlobals?: string[];
|
|
24
24
|
defaultImports?: ModuleImports;
|
|
25
|
-
defaultInjectedKeys?: string[];
|
|
26
|
-
useProxyByDefault?: boolean;
|
|
27
25
|
allowTimers?: boolean;
|
|
28
26
|
allowDynamicImports?: boolean;
|
|
29
27
|
validateCode?: boolean;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAC/B,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC5B,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,eAAe,CAAC,CAAC,GAAG,GAAG;IACtC,MAAM,EAAE,CAAC,CAAC;IACV,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,YAAY;IAC3B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,cAAc,CAAC,EAAE,aAAa,CAAC;IAC/B,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,gBAAgB;IAC/B,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,aAAa;IAC5B,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,CAAC,EAAE,gBAAgB,CAAC;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,aAAa,CAAC;IACxB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,eAAe,CAAC,CAAC,GAAG,GAAG;IACtC,MAAM,EAAE,CAAC,CAAC;IACV,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,YAAY;IAC3B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,cAAc,CAAC,EAAE,aAAa,CAAC;IAC/B,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ticatec/dyna-js",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "A TypeScript library for dynamic code execution using new Function() that works in both Node.js and browser environments",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|