( req: IsolatedVMExecutionRequest, options?: IsolatedVMExecutionOptions )
| 1302 | * Execute JavaScript code in an isolated V8 isolate via Node.js subprocess. |
| 1303 | */ |
| 1304 | export async function executeInIsolatedVM( |
| 1305 | req: IsolatedVMExecutionRequest, |
| 1306 | options?: IsolatedVMExecutionOptions |
| 1307 | ): Promise<IsolatedVMExecutionResult> { |
| 1308 | const ownerKey = normalizeOwnerKey(req.ownerKey) |
| 1309 | const ownerWeight = normalizeOwnerWeight(req.ownerWeight) |
| 1310 | const ownerState = getOrCreateOwnerState(ownerKey, ownerWeight) |
| 1311 | const brokers = options?.brokers |
| 1312 | const signal = options?.signal |
| 1313 | |
| 1314 | if (signal?.aborted) { |
| 1315 | maybeCleanupOwner(ownerKey) |
| 1316 | return { |
| 1317 | result: null, |
| 1318 | stdout: '', |
| 1319 | error: { message: 'Execution cancelled', name: 'AbortError' }, |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | if (req.task) { |
| 1324 | for (const brokerName of req.task.brokers) { |
| 1325 | if (!brokers?.[brokerName]) { |
| 1326 | maybeCleanupOwner(ownerKey) |
| 1327 | return { |
| 1328 | result: null, |
| 1329 | stdout: '', |
| 1330 | error: { |
| 1331 | message: `Task "${req.task.id}" requires broker "${brokerName}" but none was provided`, |
| 1332 | name: 'Error', |
| 1333 | isSystemError: true, |
| 1334 | }, |
| 1335 | } |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | const distributedLeaseId = `${req.requestId}:${Date.now()}:${randomFloat().toString(36).slice(2, 10)}` |
| 1341 | const leaseAcquireResult = await tryAcquireDistributedLease( |
| 1342 | ownerKey, |
| 1343 | distributedLeaseId, |
| 1344 | req.timeoutMs |
| 1345 | ) |
| 1346 | if (leaseAcquireResult === 'limit_exceeded') { |
| 1347 | logger.warn('Isolated-vm saturation: distributed lease limit exceeded', { |
| 1348 | reason: 'distributed_lease_limit', |
| 1349 | ownerKey, |
| 1350 | max: DISTRIBUTED_MAX_INFLIGHT_PER_OWNER, |
| 1351 | }) |
| 1352 | maybeCleanupOwner(ownerKey) |
| 1353 | return { |
| 1354 | result: null, |
| 1355 | stdout: '', |
| 1356 | error: { |
| 1357 | message: |
| 1358 | 'Too many concurrent code executions. Please wait for some to complete before running more.', |
| 1359 | name: 'Error', |
| 1360 | }, |
| 1361 | } |
no test coverage detected