-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrunner.js.html
215 lines (180 loc) · 6.52 KB
/
runner.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: runner.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: runner.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* This file is part of Domotz Agent.
*
* @license
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
*
* @module sandbox/runner
* @private
* @copyright Copyright (C) Domotz Inc
*/
/**
* @constant {number}
* @type {number}
* @default
*/
const DEFAULT_TIMEOUT = 5000;
/**
* @readonly
* @enum {number}
*/
const DEFAULT_CUSTOM_DRIVER_SETTINGS = {
max_log_entries: 100,
max_var_id_len: 50,
max_var_per_device: 100,
max_var_unit_len: 10,
max_data_len: 4096
};
var path = require('path');
var agentDriverSettings = null;
var dryRun = null;
/**
* Checks if a variable is valid
* @param {*} variable - a custom variable
* @returns {object|null} - new instance of the same variable if it is valid, null otherwise.
*/
function checkVariable(variable) {
if (variable === null || variable === undefined) {
return null;
}
if (
variable.uid === null ||
variable.uid === undefined ||
variable.uid.length < 1 ||
variable.uid.length > agentDriverSettings.max_var_id_len
) {
return null;
}
if (variable.value === undefined) { // must be null or string; anything else means createVariable hasn't been used;
return null;
}
return {
uid: variable.uid,
label: variable.label,
unit: variable.unit,
value: variable.value
};
}
function handleSuccessfulResponseData(response) {
// this function is executed in the sandboxRunner process, so no chance of corruption of the checks if the driver escapes the vm
if (dryRun !== true) {
response.log = null;
}
if (response.variables) {
var checkedVariables = [];
for (var i = 0; i < response.variables.length; i++) {
var checkedVariable = checkVariable(response.variables[i]);
if (checkedVariable !== null || checkedVariable !== undefined) {
checkedVariables.push(checkedVariable);
}
}
response.variables = checkedVariables;
}
return response;
}
function handleFailedResponse(response) {
if (!response.errorType){
response.errorType = 'GENERIC_ERROR';
}
return response;
}
function createMessageListener(subProcess, onError, onSuccess, myConsole) {
return function (response) {
myConsole.debug("Response outcome: ", response.outcome);
myConsole.debug("Response log:" + response.log);
if (response.variables) {
myConsole.debug("Response variables: " + JSON.stringify(response.variables));
}
subProcess.kill();
subProcess = null;
if (response.outcome === 'success') {
response = handleSuccessfulResponseData(response);
return onSuccess(response);
} else {
response = handleFailedResponse(response);
return onError(response);
}
};
}
function createErrorListener(subProcess, onError, myConsole) {
return function () {
myConsole.debug("Process exited");
if (subProcess !== null && subProcess.killed !== true) {
myConsole.error("Sandbox exited without sending back message - error");
return onError(Error("Sandbox exited without sending back message"));
}
};
}
function sandboxRunner(data, resourceLocator, cid, onError, onSuccess) {
var subProcess = require('child_process').fork(__dirname + path.sep + 'sandbox.js');
var scriptText = data.code;
var device = data.device;
var timeout = data.timeout || DEFAULT_TIMEOUT;
var logLevel = data.logLevel || 'warning';
var myConsole = resourceLocator.log.decorateLogs().decorate("(" + cid + ")");
// Set values to Global Variables
dryRun = data.dry_run;
agentDriverSettings = resourceLocator.configuration.settings.custom_driver || DEFAULT_CUSTOM_DRIVER_SETTINGS;
if (!scriptText || !device) {
return onError(Error("Missing script text or device: " + JSON.stringify(data)));
}
if (!device.ip) {
device.ip = resourceLocator.interfacesBindingStorage.getIpsFromMac(device.hw_address)[0];
}
myConsole.debug("Script %s", scriptText);
myConsole.debug("Device: %s", JSON.stringify(device));
subProcess.on('message', createMessageListener(subProcess, onError, onSuccess, myConsole));
subProcess.on('exit', createErrorListener(subProcess, onError, myConsole));
subProcess.send({
script: scriptText.toString(),
device: device,
timeout: timeout,
logLevel: logLevel,
agentDriverSettings: agentDriverSettings
});
}
module.exports.sandboxRunner = sandboxRunner;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Externals</h3><ul><li><a href="D.external__.html">_</a></li></ul><h3>Namespaces</h3><ul><li><a href="console.html">console</a></li><li><a href="D.html">D</a></li><li><a href="D.device.html">device</a></li><li><a href="D.device.http.html">http</a></li><li><a href="D.math.html">math</a></li></ul><h3><a href="global.html">Global</a></h3>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Tue Dec 14 2021 14:10:54 GMT+0200 (EET)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>