@rainbow-o23/n3 1.0.33 → 1.0.35
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/index.cjs +63 -3
- package/index.js +60 -4
- package/lib/error-codes.d.ts +4 -0
- package/package.json +3 -3
- package/src/lib/error-codes.ts +4 -0
- package/src/lib/step/abstract-fragmentary-pipeline-step.ts +12 -1
- package/src/lib/step/utils.ts +62 -3
package/index.cjs
CHANGED
|
@@ -21,6 +21,10 @@ const ERR_PIPELINE_STEP_REF_NOT_EMPTY = 'O03-00013';
|
|
|
21
21
|
const ERR_PIPELINE_STEP_REF_NOT_FOUND = 'O03-00014';
|
|
22
22
|
const ERR_PIPELINE_REF_NOT_EMPTY = 'O03-00015';
|
|
23
23
|
const ERR_PIPELINE_REF_NOT_FOUND = 'O03-00016';
|
|
24
|
+
const ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL = 'O03-00017';
|
|
25
|
+
const ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS = 'O03-00018';
|
|
26
|
+
const ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL = 'O03-00019';
|
|
27
|
+
const ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION = 'O03-00020';
|
|
24
28
|
|
|
25
29
|
class AbstractTypeOrmDataSource {
|
|
26
30
|
_name;
|
|
@@ -322,6 +326,34 @@ class TypeOrmDataSourceHelper {
|
|
|
322
326
|
|
|
323
327
|
const AsyncFunction = Object.getPrototypeOf(async function () {
|
|
324
328
|
}).constructor;
|
|
329
|
+
const createGlobalProxy = (ex) => {
|
|
330
|
+
return new Proxy({}, {
|
|
331
|
+
get() {
|
|
332
|
+
ex();
|
|
333
|
+
},
|
|
334
|
+
set() {
|
|
335
|
+
ex();
|
|
336
|
+
},
|
|
337
|
+
apply() {
|
|
338
|
+
ex();
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
};
|
|
342
|
+
const AvoidNames = ['global', 'process', 'eval', 'Function'];
|
|
343
|
+
const AvoidProxyObjects = [
|
|
344
|
+
createGlobalProxy(() => {
|
|
345
|
+
throw new n1.UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL, 'Cannot use global in dynamic snippet.');
|
|
346
|
+
}),
|
|
347
|
+
createGlobalProxy(() => {
|
|
348
|
+
throw new n1.UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS, 'Cannot use process in dynamic snippet.');
|
|
349
|
+
}),
|
|
350
|
+
createGlobalProxy(() => {
|
|
351
|
+
throw new n1.UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL, 'Cannot use eval in dynamic snippet.');
|
|
352
|
+
}),
|
|
353
|
+
createGlobalProxy(() => {
|
|
354
|
+
throw new n1.UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION, 'Cannot use Function in dynamic snippet.');
|
|
355
|
+
})
|
|
356
|
+
];
|
|
325
357
|
class Utils {
|
|
326
358
|
static createFunction(snippet, creators) {
|
|
327
359
|
try {
|
|
@@ -329,8 +361,23 @@ class Utils {
|
|
|
329
361
|
return creators.createDefault();
|
|
330
362
|
}
|
|
331
363
|
else if (typeof snippet === 'string') {
|
|
332
|
-
const
|
|
333
|
-
|
|
364
|
+
const variableNames = creators.getVariableNames() ?? [];
|
|
365
|
+
if (creators.async) {
|
|
366
|
+
const func = new AsyncFunction(...variableNames, ...AvoidNames, snippet);
|
|
367
|
+
return (async (...args) => {
|
|
368
|
+
const availableArgs = [...args];
|
|
369
|
+
availableArgs.length = variableNames.length;
|
|
370
|
+
return await func(...availableArgs, ...AvoidProxyObjects);
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
const func = new Function(...variableNames, ...AvoidNames, snippet);
|
|
375
|
+
return ((...args) => {
|
|
376
|
+
const availableArgs = [...args];
|
|
377
|
+
availableArgs.length = variableNames.length;
|
|
378
|
+
return func(...availableArgs, ...AvoidProxyObjects);
|
|
379
|
+
});
|
|
380
|
+
}
|
|
334
381
|
}
|
|
335
382
|
else {
|
|
336
383
|
return snippet;
|
|
@@ -517,7 +564,16 @@ class AbstractFragmentaryPipelineStep extends n1.AbstractPipelineStep {
|
|
|
517
564
|
}
|
|
518
565
|
}
|
|
519
566
|
else if (typeof funcOrSnippet === 'string') {
|
|
520
|
-
const func =
|
|
567
|
+
const func = Utils.createSyncFunction(funcOrSnippet, {
|
|
568
|
+
createDefault: () => {
|
|
569
|
+
throw new n1.UncatchableError(ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY, 'Cannot create perform func on empty snippet.');
|
|
570
|
+
},
|
|
571
|
+
getVariableNames: () => this.generateToResponseVariableNames(),
|
|
572
|
+
error: (e) => {
|
|
573
|
+
this.getLogger().error(`Failed on create function for snippet[${funcOrSnippet}].`);
|
|
574
|
+
throw e;
|
|
575
|
+
}
|
|
576
|
+
});
|
|
521
577
|
if (this.useUnboxMerging()) {
|
|
522
578
|
return ($result, $request, $helpers, $) => {
|
|
523
579
|
const r = func($result, $request, $helpers, $);
|
|
@@ -2277,6 +2333,10 @@ exports.ERR_EACH_FRAGMENT_NOT_ANY_ARRAY = ERR_EACH_FRAGMENT_NOT_ANY_ARRAY;
|
|
|
2277
2333
|
exports.ERR_FETCH_ERROR = ERR_FETCH_ERROR;
|
|
2278
2334
|
exports.ERR_PIPELINE_REF_NOT_EMPTY = ERR_PIPELINE_REF_NOT_EMPTY;
|
|
2279
2335
|
exports.ERR_PIPELINE_REF_NOT_FOUND = ERR_PIPELINE_REF_NOT_FOUND;
|
|
2336
|
+
exports.ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL = ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL;
|
|
2337
|
+
exports.ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION = ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION;
|
|
2338
|
+
exports.ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL = ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL;
|
|
2339
|
+
exports.ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS = ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS;
|
|
2280
2340
|
exports.ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY = ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY;
|
|
2281
2341
|
exports.ERR_PIPELINE_STEP_METHOD_NOT_SUPPORTED = ERR_PIPELINE_STEP_METHOD_NOT_SUPPORTED;
|
|
2282
2342
|
exports.ERR_PIPELINE_STEP_REF_NOT_EMPTY = ERR_PIPELINE_STEP_REF_NOT_EMPTY;
|
package/index.js
CHANGED
|
@@ -19,6 +19,10 @@ const ERR_PIPELINE_STEP_REF_NOT_EMPTY = 'O03-00013';
|
|
|
19
19
|
const ERR_PIPELINE_STEP_REF_NOT_FOUND = 'O03-00014';
|
|
20
20
|
const ERR_PIPELINE_REF_NOT_EMPTY = 'O03-00015';
|
|
21
21
|
const ERR_PIPELINE_REF_NOT_FOUND = 'O03-00016';
|
|
22
|
+
const ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL = 'O03-00017';
|
|
23
|
+
const ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS = 'O03-00018';
|
|
24
|
+
const ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL = 'O03-00019';
|
|
25
|
+
const ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION = 'O03-00020';
|
|
22
26
|
|
|
23
27
|
class AbstractTypeOrmDataSource {
|
|
24
28
|
_name;
|
|
@@ -320,6 +324,34 @@ class TypeOrmDataSourceHelper {
|
|
|
320
324
|
|
|
321
325
|
const AsyncFunction = Object.getPrototypeOf(async function () {
|
|
322
326
|
}).constructor;
|
|
327
|
+
const createGlobalProxy = (ex) => {
|
|
328
|
+
return new Proxy({}, {
|
|
329
|
+
get() {
|
|
330
|
+
ex();
|
|
331
|
+
},
|
|
332
|
+
set() {
|
|
333
|
+
ex();
|
|
334
|
+
},
|
|
335
|
+
apply() {
|
|
336
|
+
ex();
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
};
|
|
340
|
+
const AvoidNames = ['global', 'process', 'eval', 'Function'];
|
|
341
|
+
const AvoidProxyObjects = [
|
|
342
|
+
createGlobalProxy(() => {
|
|
343
|
+
throw new UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL, 'Cannot use global in dynamic snippet.');
|
|
344
|
+
}),
|
|
345
|
+
createGlobalProxy(() => {
|
|
346
|
+
throw new UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS, 'Cannot use process in dynamic snippet.');
|
|
347
|
+
}),
|
|
348
|
+
createGlobalProxy(() => {
|
|
349
|
+
throw new UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL, 'Cannot use eval in dynamic snippet.');
|
|
350
|
+
}),
|
|
351
|
+
createGlobalProxy(() => {
|
|
352
|
+
throw new UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION, 'Cannot use Function in dynamic snippet.');
|
|
353
|
+
})
|
|
354
|
+
];
|
|
323
355
|
class Utils {
|
|
324
356
|
static createFunction(snippet, creators) {
|
|
325
357
|
try {
|
|
@@ -327,8 +359,23 @@ class Utils {
|
|
|
327
359
|
return creators.createDefault();
|
|
328
360
|
}
|
|
329
361
|
else if (typeof snippet === 'string') {
|
|
330
|
-
const
|
|
331
|
-
|
|
362
|
+
const variableNames = creators.getVariableNames() ?? [];
|
|
363
|
+
if (creators.async) {
|
|
364
|
+
const func = new AsyncFunction(...variableNames, ...AvoidNames, snippet);
|
|
365
|
+
return (async (...args) => {
|
|
366
|
+
const availableArgs = [...args];
|
|
367
|
+
availableArgs.length = variableNames.length;
|
|
368
|
+
return await func(...availableArgs, ...AvoidProxyObjects);
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
const func = new Function(...variableNames, ...AvoidNames, snippet);
|
|
373
|
+
return ((...args) => {
|
|
374
|
+
const availableArgs = [...args];
|
|
375
|
+
availableArgs.length = variableNames.length;
|
|
376
|
+
return func(...availableArgs, ...AvoidProxyObjects);
|
|
377
|
+
});
|
|
378
|
+
}
|
|
332
379
|
}
|
|
333
380
|
else {
|
|
334
381
|
return snippet;
|
|
@@ -515,7 +562,16 @@ class AbstractFragmentaryPipelineStep extends AbstractPipelineStep {
|
|
|
515
562
|
}
|
|
516
563
|
}
|
|
517
564
|
else if (typeof funcOrSnippet === 'string') {
|
|
518
|
-
const func =
|
|
565
|
+
const func = Utils.createSyncFunction(funcOrSnippet, {
|
|
566
|
+
createDefault: () => {
|
|
567
|
+
throw new UncatchableError(ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY, 'Cannot create perform func on empty snippet.');
|
|
568
|
+
},
|
|
569
|
+
getVariableNames: () => this.generateToResponseVariableNames(),
|
|
570
|
+
error: (e) => {
|
|
571
|
+
this.getLogger().error(`Failed on create function for snippet[${funcOrSnippet}].`);
|
|
572
|
+
throw e;
|
|
573
|
+
}
|
|
574
|
+
});
|
|
519
575
|
if (this.useUnboxMerging()) {
|
|
520
576
|
return ($result, $request, $helpers, $) => {
|
|
521
577
|
const r = func($result, $request, $helpers, $);
|
|
@@ -2261,4 +2317,4 @@ class TypeOrmTransactionalPipelineStepSets extends PipelineStepSets {
|
|
|
2261
2317
|
}
|
|
2262
2318
|
}
|
|
2263
2319
|
|
|
2264
|
-
export { AbstractFragmentaryPipelineStep, AbstractTypeOrmBySQLPipelineStep, AbstractTypeOrmDataSource, AbstractTypeOrmLoadBySQLPipelineStep, AbstractTypeOrmPipelineStep, AsyncPipelineStepSets, BetterSqlite3TypeOrmDatasource, ConditionalPipelineStepSets, DEFAULT_TRANSACTION_NAME, DeletePropertyPipelineStep, ERR_EACH_FRAGMENT_NOT_ANY_ARRAY, ERR_FETCH_ERROR, ERR_PIPELINE_REF_NOT_EMPTY, ERR_PIPELINE_REF_NOT_FOUND, ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY, ERR_PIPELINE_STEP_METHOD_NOT_SUPPORTED, ERR_PIPELINE_STEP_REF_NOT_EMPTY, ERR_PIPELINE_STEP_REF_NOT_FOUND, ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY, ERR_TYPEORM_DATASOURCE_CREATOR_NOT_FOUND, ERR_TYPEORM_DATASOURCE_NOT_FOUND, ERR_TYPEORM_DATASOURCE_TYPE_NOT_FOUND, ERR_TYPEORM_ENTITY_NOT_FOUND, ERR_TYPEORM_SQL_NOT_EMPTY, ERR_TYPEORM_STEP_SNIPPET_NOT_EMPTY, ERR_TYPEORM_TRANSACTION_NOT_FOUND, EachPipelineStepSets, FetchPipelineStep, GetPropertyPipelineStep, HttpAbortErrorCode, HttpUnknownErrorCode, MssqlTypeOrmDatasource, MysqlTypeOrmDatasource, OracleTypeOrmDatasource, ParallelPipelineStepSets, ParsedSqlSegmentType, PgsqlTypeOrmDatasource, PipelineStepSets, RefPipelinePipelineStep, RefStepPipelineStep, RoutesPipelineStepSets, SnippetPipelineStep, SnowflakePipelineStep, SupportedDataSourceTypes, TypeOrmBulkSaveBySQLPipelineStep, TypeOrmBySnippetPipelineStep, TypeOrmDataSourceHelper, TypeOrmDataSourceManager, TypeOrmLoadEntityByIdPipelineStep, TypeOrmLoadManyBySQLPipelineStep, TypeOrmLoadOneBySQLPipelineStep, TypeOrmParsedSQLCache, TypeOrmSaveBySQLPipelineStep, TypeOrmSaveEntityPipelineStep, TypeOrmTransactionalPipelineStepSets, Utils };
|
|
2320
|
+
export { AbstractFragmentaryPipelineStep, AbstractTypeOrmBySQLPipelineStep, AbstractTypeOrmDataSource, AbstractTypeOrmLoadBySQLPipelineStep, AbstractTypeOrmPipelineStep, AsyncPipelineStepSets, BetterSqlite3TypeOrmDatasource, ConditionalPipelineStepSets, DEFAULT_TRANSACTION_NAME, DeletePropertyPipelineStep, ERR_EACH_FRAGMENT_NOT_ANY_ARRAY, ERR_FETCH_ERROR, ERR_PIPELINE_REF_NOT_EMPTY, ERR_PIPELINE_REF_NOT_FOUND, ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL, ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION, ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL, ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS, ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY, ERR_PIPELINE_STEP_METHOD_NOT_SUPPORTED, ERR_PIPELINE_STEP_REF_NOT_EMPTY, ERR_PIPELINE_STEP_REF_NOT_FOUND, ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY, ERR_TYPEORM_DATASOURCE_CREATOR_NOT_FOUND, ERR_TYPEORM_DATASOURCE_NOT_FOUND, ERR_TYPEORM_DATASOURCE_TYPE_NOT_FOUND, ERR_TYPEORM_ENTITY_NOT_FOUND, ERR_TYPEORM_SQL_NOT_EMPTY, ERR_TYPEORM_STEP_SNIPPET_NOT_EMPTY, ERR_TYPEORM_TRANSACTION_NOT_FOUND, EachPipelineStepSets, FetchPipelineStep, GetPropertyPipelineStep, HttpAbortErrorCode, HttpUnknownErrorCode, MssqlTypeOrmDatasource, MysqlTypeOrmDatasource, OracleTypeOrmDatasource, ParallelPipelineStepSets, ParsedSqlSegmentType, PgsqlTypeOrmDatasource, PipelineStepSets, RefPipelinePipelineStep, RefStepPipelineStep, RoutesPipelineStepSets, SnippetPipelineStep, SnowflakePipelineStep, SupportedDataSourceTypes, TypeOrmBulkSaveBySQLPipelineStep, TypeOrmBySnippetPipelineStep, TypeOrmDataSourceHelper, TypeOrmDataSourceManager, TypeOrmLoadEntityByIdPipelineStep, TypeOrmLoadManyBySQLPipelineStep, TypeOrmLoadOneBySQLPipelineStep, TypeOrmParsedSQLCache, TypeOrmSaveBySQLPipelineStep, TypeOrmSaveEntityPipelineStep, TypeOrmTransactionalPipelineStepSets, Utils };
|
package/lib/error-codes.d.ts
CHANGED
|
@@ -15,3 +15,7 @@ export declare const ERR_PIPELINE_STEP_REF_NOT_EMPTY: O23ReservedErrorCode;
|
|
|
15
15
|
export declare const ERR_PIPELINE_STEP_REF_NOT_FOUND: O23ReservedErrorCode;
|
|
16
16
|
export declare const ERR_PIPELINE_REF_NOT_EMPTY: O23ReservedErrorCode;
|
|
17
17
|
export declare const ERR_PIPELINE_REF_NOT_FOUND: O23ReservedErrorCode;
|
|
18
|
+
export declare const ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL: O23ReservedErrorCode;
|
|
19
|
+
export declare const ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS: O23ReservedErrorCode;
|
|
20
|
+
export declare const ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL: O23ReservedErrorCode;
|
|
21
|
+
export declare const ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION: O23ReservedErrorCode;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rainbow-o23/n3",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.35",
|
|
4
4
|
"description": "o23 pipelines",
|
|
5
5
|
"main": "index.cjs",
|
|
6
6
|
"module": "index.js",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"url": "https://github.com/InsureMO/rainbow-o23/issues"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@rainbow-o23/n1": "1.0.
|
|
24
|
+
"@rainbow-o23/n1": "1.0.35",
|
|
25
25
|
"@theinternetfolks/snowflake": "^1.3.0",
|
|
26
26
|
"node-fetch": "2.6.7",
|
|
27
27
|
"typeorm": "^0.3.17"
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"better-sqlite3": "^9.0.0",
|
|
42
42
|
"eslint": "^8.29.0",
|
|
43
43
|
"mssql": "^10.0.1",
|
|
44
|
-
"mysql2": "^3.9.
|
|
44
|
+
"mysql2": "^3.9.7",
|
|
45
45
|
"oracledb": "^6.2.0",
|
|
46
46
|
"pg": "^8.11.3",
|
|
47
47
|
"pg-query-stream": "^4.5.3",
|
package/src/lib/error-codes.ts
CHANGED
|
@@ -16,3 +16,7 @@ export const ERR_PIPELINE_STEP_REF_NOT_EMPTY: O23ReservedErrorCode = 'O03-00013'
|
|
|
16
16
|
export const ERR_PIPELINE_STEP_REF_NOT_FOUND: O23ReservedErrorCode = 'O03-00014';
|
|
17
17
|
export const ERR_PIPELINE_REF_NOT_EMPTY: O23ReservedErrorCode = 'O03-00015';
|
|
18
18
|
export const ERR_PIPELINE_REF_NOT_FOUND: O23ReservedErrorCode = 'O03-00016';
|
|
19
|
+
export const ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL: O23ReservedErrorCode = 'O03-00017';
|
|
20
|
+
export const ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS: O23ReservedErrorCode = 'O03-00018';
|
|
21
|
+
export const ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL: O23ReservedErrorCode = 'O03-00019';
|
|
22
|
+
export const ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION: O23ReservedErrorCode = 'O03-00020';
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
PipelineStepPayload,
|
|
11
11
|
UncatchableError
|
|
12
12
|
} from '@rainbow-o23/n1';
|
|
13
|
+
import {ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY} from '../error-codes';
|
|
13
14
|
import {PipelineStepSetsContext} from './step-sets';
|
|
14
15
|
import {
|
|
15
16
|
HandleAnyError,
|
|
@@ -210,7 +211,17 @@ export abstract class AbstractFragmentaryPipelineStep<In = PipelineStepPayload,
|
|
|
210
211
|
};
|
|
211
212
|
}
|
|
212
213
|
} else if (typeof funcOrSnippet === 'string') {
|
|
213
|
-
|
|
214
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
215
|
+
const func: Function = Utils.createSyncFunction(funcOrSnippet, {
|
|
216
|
+
createDefault: (): never => {
|
|
217
|
+
throw new UncatchableError(ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY, 'Cannot create perform func on empty snippet.');
|
|
218
|
+
},
|
|
219
|
+
getVariableNames: () => this.generateToResponseVariableNames(),
|
|
220
|
+
error: (e: Error) => {
|
|
221
|
+
this.getLogger().error(`Failed on create function for snippet[${funcOrSnippet}].`);
|
|
222
|
+
throw e;
|
|
223
|
+
}
|
|
224
|
+
});
|
|
214
225
|
if (this.useUnboxMerging()) {
|
|
215
226
|
return ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers): Out => {
|
|
216
227
|
const r = func($result, $request, $helpers, $);
|
package/src/lib/step/utils.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {Nullable, Undefinable} from '@rainbow-o23/n1';
|
|
1
|
+
import {Nullable, UncatchableError, Undefinable} from '@rainbow-o23/n1';
|
|
2
|
+
import {
|
|
3
|
+
ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL,
|
|
4
|
+
ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION,
|
|
5
|
+
ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL,
|
|
6
|
+
ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS
|
|
7
|
+
} from '../error-codes';
|
|
2
8
|
import {ScriptFuncOrBody} from './types';
|
|
3
9
|
|
|
4
10
|
// get async function constructor, to create the dynamic function
|
|
@@ -6,6 +12,35 @@ const AsyncFunction = Object.getPrototypeOf(async function () {
|
|
|
6
12
|
// nothing, since this purpose is get the constructor, body is not concerned
|
|
7
13
|
}).constructor;
|
|
8
14
|
|
|
15
|
+
const createGlobalProxy = (ex: () => never) => {
|
|
16
|
+
return new Proxy({}, {
|
|
17
|
+
get() {
|
|
18
|
+
ex();
|
|
19
|
+
},
|
|
20
|
+
set() {
|
|
21
|
+
ex();
|
|
22
|
+
},
|
|
23
|
+
apply() {
|
|
24
|
+
ex();
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
const AvoidNames = ['global', 'process', 'eval', 'Function'];
|
|
29
|
+
const AvoidProxyObjects = [
|
|
30
|
+
createGlobalProxy(() => {
|
|
31
|
+
throw new UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL, 'Cannot use global in dynamic snippet.');
|
|
32
|
+
}),
|
|
33
|
+
createGlobalProxy(() => {
|
|
34
|
+
throw new UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS, 'Cannot use process in dynamic snippet.');
|
|
35
|
+
}),
|
|
36
|
+
createGlobalProxy(() => {
|
|
37
|
+
throw new UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL, 'Cannot use eval in dynamic snippet.');
|
|
38
|
+
}),
|
|
39
|
+
createGlobalProxy(() => {
|
|
40
|
+
throw new UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION, 'Cannot use Function in dynamic snippet.');
|
|
41
|
+
})
|
|
42
|
+
];
|
|
43
|
+
|
|
9
44
|
export class Utils {
|
|
10
45
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
11
46
|
public static createFunction<F = Function>(snippet: ScriptFuncOrBody<F>, creators: {
|
|
@@ -18,8 +53,32 @@ export class Utils {
|
|
|
18
53
|
if (snippet == null || (typeof snippet === 'string' && snippet.trim().length === 0)) {
|
|
19
54
|
return creators.createDefault();
|
|
20
55
|
} else if (typeof snippet === 'string') {
|
|
21
|
-
|
|
22
|
-
|
|
56
|
+
// if (snippet.includes('global')) {
|
|
57
|
+
// // noinspection ExceptionCaughtLocallyJS
|
|
58
|
+
// throw new UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL, '"global" is not allowed in dynamic snippet.');
|
|
59
|
+
// }
|
|
60
|
+
const variableNames = creators.getVariableNames() ?? [];
|
|
61
|
+
if (creators.async) {
|
|
62
|
+
const func = new AsyncFunction(...variableNames, ...AvoidNames, snippet);
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
64
|
+
return (async (...args: Array<any>) => {
|
|
65
|
+
const availableArgs = [...args];
|
|
66
|
+
availableArgs.length = variableNames.length;
|
|
67
|
+
// Pass specified length of parameters,
|
|
68
|
+
// along with an additional global object to prevent internal access to the actual global object.
|
|
69
|
+
return await func(...availableArgs, ...AvoidProxyObjects);
|
|
70
|
+
}) as F;
|
|
71
|
+
} else {
|
|
72
|
+
const func = new Function(...variableNames, ...AvoidNames, snippet);
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
+
return ((...args: Array<any>) => {
|
|
75
|
+
const availableArgs = [...args];
|
|
76
|
+
availableArgs.length = variableNames.length;
|
|
77
|
+
// Pass specified length of parameters,
|
|
78
|
+
// along with an additional global object to prevent internal access to the actual global object.
|
|
79
|
+
return func(...availableArgs, ...AvoidProxyObjects);
|
|
80
|
+
}) as F;
|
|
81
|
+
}
|
|
23
82
|
} else {
|
|
24
83
|
return snippet;
|
|
25
84
|
}
|