* Execute code in isolated-vm
(request, executionId)
| 161 | * Execute code in isolated-vm |
| 162 | */ |
| 163 | async function executeCode(request, executionId) { |
| 164 | const { code, params, envVars, contextVariables, timeoutMs, requestId } = request |
| 165 | const stdoutChunks = [] |
| 166 | let stdoutLength = 0 |
| 167 | let stdoutTruncated = false |
| 168 | let isolate = null |
| 169 | |
| 170 | const appendStdout = (line) => { |
| 171 | if (stdoutTruncated || !line) return |
| 172 | |
| 173 | const remaining = MAX_STDOUT_CHARS - stdoutLength |
| 174 | if (remaining <= 0) { |
| 175 | stdoutTruncated = true |
| 176 | stdoutChunks.push('[stdout truncated]\n') |
| 177 | return |
| 178 | } |
| 179 | |
| 180 | if (line.length <= remaining) { |
| 181 | stdoutChunks.push(line) |
| 182 | stdoutLength += line.length |
| 183 | return |
| 184 | } |
| 185 | |
| 186 | stdoutChunks.push(line.slice(0, remaining)) |
| 187 | stdoutChunks.push('\n[stdout truncated]\n') |
| 188 | stdoutLength = MAX_STDOUT_CHARS |
| 189 | stdoutTruncated = true |
| 190 | } |
| 191 | |
| 192 | let context = null |
| 193 | let bootstrapScript = null |
| 194 | let userScript = null |
| 195 | let logCallback = null |
| 196 | let errorCallback = null |
| 197 | let fetchCallback = null |
| 198 | let brokerCallback = null |
| 199 | const externalCopies = [] |
| 200 | |
| 201 | try { |
| 202 | isolate = new ivm.Isolate({ memoryLimit: 128 }) |
| 203 | if (executionId !== undefined) activeIsolates.set(executionId, isolate) |
| 204 | context = await isolate.createContext() |
| 205 | const jail = context.global |
| 206 | |
| 207 | await jail.set('global', jail.derefInto()) |
| 208 | |
| 209 | logCallback = new ivm.Callback((...args) => { |
| 210 | const message = args.map((arg) => stringifyLogValue(arg)).join(' ') |
| 211 | appendStdout(`${message}\n`) |
| 212 | }) |
| 213 | await jail.set('__log', logCallback) |
| 214 | |
| 215 | errorCallback = new ivm.Callback((...args) => { |
| 216 | const message = args.map((arg) => stringifyLogValue(arg)).join(' ') |
| 217 | appendStdout(`ERROR: ${message}\n`) |
| 218 | }) |
| 219 | await jail.set('__error', errorCallback) |
| 220 |
no test coverage detected