@flowgram.ai/runtime-js 1.0.7 → 1.0.9

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/index.js CHANGED
@@ -1491,6 +1491,7 @@ var ConditionExecutor = class {
1491
1491
  };
1492
1492
 
1493
1493
  // src/nodes/code/index.ts
1494
+ var import_quickjs_emscripten = require("quickjs-emscripten");
1494
1495
  var CodeExecutor = class {
1495
1496
  constructor() {
1496
1497
  this.type = FlowGramNode.Code;
@@ -1519,35 +1520,53 @@ var CodeExecutor = class {
1519
1520
  }
1520
1521
  async javascript(inputs) {
1521
1522
  const { params = {}, script } = inputs;
1523
+ const serializedParams = JSON.stringify(params);
1524
+ const QuickJS = await (0, import_quickjs_emscripten.getQuickJS)();
1525
+ const context = QuickJS.newContext();
1522
1526
  try {
1523
- const executeCode = new Function(
1524
- "params",
1525
- `
1526
- 'use strict';
1527
+ const runtime = context.runtime;
1528
+ runtime.setMemoryLimit(32 * 1024 * 1024);
1529
+ runtime.setMaxStackSize(512 * 1024);
1530
+ runtime.setInterruptHandler((0, import_quickjs_emscripten.shouldInterruptAfterDeadline)(Date.now() + 6e4));
1531
+ const wrappedCode = `
1532
+ 'use strict';
1527
1533
 
1528
- ${script.content}
1534
+ ${script.content}
1529
1535
 
1530
- // Ensure main function exists
1531
- if (typeof main !== 'function') {
1532
- throw new Error('main function is required in the script');
1533
- }
1536
+ if (typeof main !== 'function') {
1537
+ throw new Error('main function is required in the script');
1538
+ }
1534
1539
 
1535
- // Execute main function with params
1536
- return main({ params });
1537
- `
1538
- );
1539
- const timeoutPromise = new Promise((_, reject) => {
1540
- setTimeout(() => {
1541
- reject(new Error("Code execution timeout: exceeded 1 minute"));
1542
- }, 1e3 * 60);
1543
- });
1544
- const result = await Promise.race([executeCode(params), timeoutPromise]);
1545
- const outputs = result && typeof result === "object" && !Array.isArray(result) ? result : { result };
1546
- return {
1547
- outputs
1548
- };
1540
+ const __params__ = ${serializedParams};
1541
+ main({ params: __params__ });
1542
+ `;
1543
+ const evalResult = context.evalCode(wrappedCode);
1544
+ const resultHandle = context.unwrapResult(evalResult);
1545
+ let rawResult;
1546
+ try {
1547
+ const promiseState = context.getPromiseState(resultHandle);
1548
+ if (promiseState.type === "fulfilled") {
1549
+ rawResult = context.dump(promiseState.value);
1550
+ promiseState.value.dispose();
1551
+ } else if (promiseState.type === "rejected") {
1552
+ const errMsg = context.dump(promiseState.error);
1553
+ promiseState.error.dispose();
1554
+ throw new Error(typeof errMsg === "string" ? errMsg : JSON.stringify(errMsg));
1555
+ } else {
1556
+ const resolvedResult = await context.resolvePromise(resultHandle);
1557
+ const resolvedHandle = context.unwrapResult(resolvedResult);
1558
+ rawResult = context.dump(resolvedHandle);
1559
+ resolvedHandle.dispose();
1560
+ }
1561
+ } finally {
1562
+ resultHandle.dispose();
1563
+ }
1564
+ const outputs = rawResult && typeof rawResult === "object" && !Array.isArray(rawResult) ? rawResult : { result: rawResult };
1565
+ return { outputs };
1549
1566
  } catch (error) {
1550
1567
  throw new Error(`Code execution failed: ${error.message}`);
1568
+ } finally {
1569
+ context.dispose();
1551
1570
  }
1552
1571
  }
1553
1572
  };