cwl_utils.sandboxjs
Safe execution of CWL Expressions in a NodeJS sandbox.
Attributes
Default number of seconds to wait while running a javascript engine. |
|
Classes
Helper class that provides a standard way to create an ABC using |
|
Helper class that provides a standard way to create an ABC using |
|
Evaluate CWL javascript fragments with the |
Functions
|
|
|
|
|
|
Return the process-wide JS engine, creating it on first use. |
|
|
|
|
|
|
Run a javascript text. |
|
Module Contents
- cwl_utils.sandboxjs.default_timeout: float = 20
Default number of seconds to wait while running a javascript engine.
- cwl_utils.sandboxjs.seg_symbol = '\\w+'
- cwl_utils.sandboxjs.seg_single = "\\['([^']|\\\\')+'\\]"
- cwl_utils.sandboxjs.seg_double = '\\["([^"]|\\\\")+"\\]'
- cwl_utils.sandboxjs.seg_index = '\\[[0-9]+\\]'
- cwl_utils.sandboxjs.segments = '(\\.\\w+|\\[\'([^\']|\\\\\')+\'\\]|\\["([^"]|\\\\")+"\\]|\\[[0-9]+\\])'
- cwl_utils.sandboxjs.segment_re
- cwl_utils.sandboxjs.param_str = '\\((\\w+)(\\.\\w+|\\[\'([^\']|\\\\\')+\'\\]|\\["([^"]|\\\\")+"\\]|\\[[0-9]+\\])*\\)$'
- cwl_utils.sandboxjs.param_re
- cwl_utils.sandboxjs.code_fragment_to_js(jscript, jslib='')
- class cwl_utils.sandboxjs.JSEngine
Bases:
abc.ABC
Helper class that provides a standard way to create an ABC using inheritance.
- abstractmethod eval(scan, jslib='', timeout=default_timeout, force_docker_pull=False, debug=False, js_console=False, container_engine='docker', **kwargs)
- Parameters:
- Return type:
cwl_utils.types.CWLOutputType | collections.abc.Awaitable[cwl_utils.types.CWLOutputType]
- regex_eval(parsed_string, remaining_string, current_value, **kwargs)
Walk a parameter reference without a javascript engine (pure Python).
- Parameters:
parsed_string (str)
remaining_string (str)
current_value (cwl_utils.types.CWLOutputType)
kwargs (Any)
- Return type:
cwl_utils.types.CWLOutputType | collections.abc.Awaitable[cwl_utils.types.CWLOutputType]
- class cwl_utils.sandboxjs.NodeJSEngine(have_node_slim=False, minimum_node_version_str='0.10.26', process_finished_str='r1cepzbhUTxtykz5XTC4\n')
Bases:
JSEngine
Helper class that provides a standard way to create an ABC using inheritance.
- localdata
- processes_to_kill: Deque[subprocess.Popen[str]]
- __del__()
- Return type:
None
- check_js_threshold_version(working_alias)
Check if the nodeJS engine version on the system with the allowed minimum version.
https://github.com/nodejs/node/blob/master/CHANGELOG.md#nodejs-changelog
- exec_js_process(js_text, timeout=default_timeout, js_console=False, context=None, force_docker_pull=False, container_engine='docker')
Run a javascript text.
- new_js_proc(js_text, force_docker_pull=False, container_engine='docker')
Return a subprocess ready to submit javascript to.
- Parameters:
- Return type:
- eval(scan, jslib='', timeout=default_timeout, force_docker_pull=False, debug=False, js_console=False, container_engine='docker', **kwargs)
- class cwl_utils.sandboxjs.QuickJSEngine(qjs_path='qjs')
Bases:
JSEngine
Evaluate CWL javascript fragments with the
qjsinterpreter.QuickJS starts in about a millisecond, so unlike
NodeJSEngineno persistent process or wire protocol is needed: each evaluation runs in one short-livedqjssubprocess. The container fallback keyword arguments accepted by the NodeJS engine (force_docker_pull,container_engine) are accepted and ignored;js_consoleis not supported.- Parameters:
qjs_path (str)
- qjs_path = 'qjs'
- check_js_threshold_version(working_alias=None)
Check that the qjs interpreter supports the features this engine needs.
QuickJS distributions do not share a version scheme (the original QuickJS uses dates while quickjs-ng uses semantic versions), so rather than parsing version strings this probes the interpreter with a small script exercising the features the engine relies on: eval() completion values, globalThis, Object.assign, JSON, and print. A missing or broken interpreter fails the probe.
- eval(scan, jslib='', timeout=default_timeout, force_docker_pull=False, debug=False, js_console=False, container_engine='docker', **kwargs)
- exec_js_process(js_text, timeout=default_timeout, js_console=False, context=None, force_docker_pull=False, container_engine='docker')
Run a javascript text in a fresh qjs subprocess.
The
contextscript, when given, is evaluated first and the properties of its completion value become global variables forjs_text, mirroring how cwlNodeEngineWithContext.js appliesvm.runInNewContext(js_text, context).- Parameters:
- Returns:
A tuple of the return code, stdout, and stderr of the javascript engine invocation. A timeout is reported as return code -1.
- Return type:
- cwl_utils.sandboxjs.get_js_engine()
Return the process-wide JS engine, creating it on first use.
The engine is chosen by the
CWL_JS_ENGINEenvironment variable (nodeorquickjs); when it is unset, QuickJS is preferred if a usableqjsexecutable is found on the PATH, NodeJS otherwise. Engines installed withset_js_engine()take precedence.- Return type:
- cwl_utils.sandboxjs.check_js_threshold_version(*args, **kwargs)
- Parameters:
args (Any)
kwargs (Any)
- Return type:
- cwl_utils.sandboxjs.exec_js_process(*args, **kwargs)
Run a javascript text.
- cwl_utils.sandboxjs.new_js_proc(*args, **kwargs)
- Parameters:
args (Any)
kwargs (Any)
- Return type: