1. /**
  2. * Indexes the runtime environment for sandbox usage with error logging.
  3. *
  4. * @returns {object} An object containing the indexed environment data and error logs.
  5. */
  6. function indexRuntimeEnvironment() {
  7. const environmentData = {};
  8. const errorLogs = [];
  9. try {
  10. // Collect environment variables
  11. const env = process.env;
  12. for (const key in env) {
  13. if (env.hasOwnProperty(key)) {
  14. environmentData[key] = env[key];
  15. }
  16. }
  17. // Collect global objects (careful with this for security)
  18. environmentData.globalObjects = global;
  19. // Collect built-in functions (careful with this for security)
  20. environmentData.builtInFunctions = Object.keys(global).filter(key => typeof global[key] === 'function');
  21. } catch (err) {
  22. errorLogs.push({
  23. type: 'environment_index_error',
  24. message: `Error during environment indexing: ${err.message}`,
  25. stack: err.stack
  26. });
  27. }
  28. return {
  29. environment: environmentData,
  30. errors: errorLogs
  31. };
  32. }
  33. // Example usage (for testing - remove in production)
  34. const result = indexRuntimeEnvironment();
  35. console.log(JSON.stringify(result.environment, null, 2)); // Output environment data
  36. console.log(JSON.stringify(result.errors, null, 2)); // Output error logs

Add your comment