utils.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Utility functions for SaaS VOC Cloud Functions
  3. */
  4. /**
  5. * Generate a unique public ID
  6. */
  7. function publicId() {
  8. return 'cf-' + Date.now().toString(36) + '-' + Math.random().toString(36).slice(2, 10);
  9. }
  10. /**
  11. * Safely serialize a value, removing sensitive fields
  12. */
  13. function safeValue(value, depth) {
  14. if (depth > 5 || value === null || value === undefined) return value;
  15. if (value instanceof Date) return value;
  16. if (Array.isArray(value)) return value.slice(0, 100).map((item) => safeValue(item, depth + 1));
  17. if (typeof value !== 'object') return value;
  18. const output = {};
  19. Object.keys(value).slice(0, 100).forEach((key) => {
  20. if (/token|secret|password|credential|authorization|master/i.test(key)) return;
  21. output[key] = safeValue(value[key], depth + 1);
  22. });
  23. return output;
  24. }
  25. /**
  26. * Limit a value to a bounded range
  27. */
  28. function boundedLimit(value, fallback, maximum = 100) {
  29. const limit = Number(value || fallback);
  30. return Number.isInteger(limit) ? Math.max(1, Math.min(limit, maximum)) : fallback;
  31. }
  32. /*
  33. // module.exports = {
  34. // publicId,
  35. // safeValue,
  36. // boundedLimit,
  37. // };
  38. */