@flowgram.ai/runtime-js 0.1.0-alpha.29 → 0.1.0-alpha.30

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/dist/esm/index.js CHANGED
@@ -1450,6 +1450,7 @@ var ConditionExecutor = class {
1450
1450
  };
1451
1451
 
1452
1452
  // src/nodes/code/index.ts
1453
+ import { getQuickJS, shouldInterruptAfterDeadline } from "quickjs-emscripten";
1453
1454
  var CodeExecutor = class {
1454
1455
  constructor() {
1455
1456
  this.type = FlowGramNode.Code;
@@ -1478,35 +1479,53 @@ var CodeExecutor = class {
1478
1479
  }
1479
1480
  async javascript(inputs) {
1480
1481
  const { params = {}, script } = inputs;
1482
+ const serializedParams = JSON.stringify(params);
1483
+ const QuickJS = await getQuickJS();
1484
+ const context = QuickJS.newContext();
1481
1485
  try {
1482
- const executeCode = new Function(
1483
- "params",
1484
- `
1485
- 'use strict';
1486
+ const runtime = context.runtime;
1487
+ runtime.setMemoryLimit(32 * 1024 * 1024);
1488
+ runtime.setMaxStackSize(512 * 1024);
1489
+ runtime.setInterruptHandler(shouldInterruptAfterDeadline(Date.now() + 6e4));
1490
+ const wrappedCode = `
1491
+ 'use strict';
1486
1492
 
1487
- ${script.content}
1493
+ ${script.content}
1488
1494
 
1489
- // Ensure main function exists
1490
- if (typeof main !== 'function') {
1491
- throw new Error('main function is required in the script');
1492
- }
1495
+ if (typeof main !== 'function') {
1496
+ throw new Error('main function is required in the script');
1497
+ }
1493
1498
 
1494
- // Execute main function with params
1495
- return main({ params });
1496
- `
1497
- );
1498
- const timeoutPromise = new Promise((_, reject) => {
1499
- setTimeout(() => {
1500
- reject(new Error("Code execution timeout: exceeded 1 minute"));
1501
- }, 1e3 * 60);
1502
- });
1503
- const result = await Promise.race([executeCode(params), timeoutPromise]);
1504
- const outputs = result && typeof result === "object" && !Array.isArray(result) ? result : { result };
1505
- return {
1506
- outputs
1507
- };
1499
+ const __params__ = ${serializedParams};
1500
+ main({ params: __params__ });
1501
+ `;
1502
+ const evalResult = context.evalCode(wrappedCode);
1503
+ const resultHandle = context.unwrapResult(evalResult);
1504
+ let rawResult;
1505
+ try {
1506
+ const promiseState = context.getPromiseState(resultHandle);
1507
+ if (promiseState.type === "fulfilled") {
1508
+ rawResult = context.dump(promiseState.value);
1509
+ promiseState.value.dispose();
1510
+ } else if (promiseState.type === "rejected") {
1511
+ const errMsg = context.dump(promiseState.error);
1512
+ promiseState.error.dispose();
1513
+ throw new Error(typeof errMsg === "string" ? errMsg : JSON.stringify(errMsg));
1514
+ } else {
1515
+ const resolvedResult = await context.resolvePromise(resultHandle);
1516
+ const resolvedHandle = context.unwrapResult(resolvedResult);
1517
+ rawResult = context.dump(resolvedHandle);
1518
+ resolvedHandle.dispose();
1519
+ }
1520
+ } finally {
1521
+ resultHandle.dispose();
1522
+ }
1523
+ const outputs = rawResult && typeof rawResult === "object" && !Array.isArray(rawResult) ? rawResult : { result: rawResult };
1524
+ return { outputs };
1508
1525
  } catch (error) {
1509
1526
  throw new Error(`Code execution failed: ${error.message}`);
1527
+ } finally {
1528
+ context.dispose();
1510
1529
  }
1511
1530
  }
1512
1531
  };