|
| 1 | +/** |
| 2 | + * Serializes a JavaScript object into a string of source code that can be evaluated. |
| 3 | + * This function is used to create runtime plugin options without relying solely on JSON.stringify, |
| 4 | + * allowing support for non-JSON types like RegExp, Date, Map, Set, and Functions. |
| 5 | + * It also safely handles circular references. |
| 6 | + * |
| 7 | + * @param {Record<string, unknown>} options - The options object to serialize. |
| 8 | + * @returns {string} The resulting JavaScript source code string. |
| 9 | + */ |
| 10 | +export function serializeRuntimeOptions(options: Record<string, unknown>): string { |
| 11 | + // Use a WeakSet to track objects already encountered, which helps in detecting circular references. |
| 12 | + const seenObjects = new WeakSet<any>(); |
| 13 | + |
| 14 | + /** |
| 15 | + * Recursive inner function to serialize any value into a source code string. |
| 16 | + */ |
| 17 | + function valueToCode(val: any): string { |
| 18 | + // 1. Handle primitive values |
| 19 | + if (val === null) return 'null'; |
| 20 | + |
| 21 | + const type = typeof val; |
| 22 | + |
| 23 | + if (type === 'string') return JSON.stringify(val); |
| 24 | + if (type === 'number' || type === 'boolean') return String(val); |
| 25 | + if (type === 'undefined') return 'undefined'; |
| 26 | + |
| 27 | + // Handle Symbol |
| 28 | + if (type === 'symbol') { |
| 29 | + const desc = val.description ?? ''; |
| 30 | + return `Symbol(${JSON.stringify(desc)})`; |
| 31 | + } |
| 32 | + |
| 33 | + // Handle Function (returns the function's source code) |
| 34 | + if (type === 'function') return val.toString(); |
| 35 | + |
| 36 | + // 2. Handle special built-in objects |
| 37 | + if (val instanceof Date) return `new Date(${JSON.stringify(val.toISOString())})`; |
| 38 | + if (val instanceof RegExp) { |
| 39 | + return `new RegExp(${JSON.stringify(val.source)}, ${JSON.stringify(val.flags)})`; |
| 40 | + } |
| 41 | + |
| 42 | + // 3. Check for circular references and mark object as seen |
| 43 | + // This applies to objects, arrays, maps, and sets. |
| 44 | + if (type === 'object') { |
| 45 | + if (seenObjects.has(val)) { |
| 46 | + // This object has been seen previously in the recursion path |
| 47 | + return `"__circular__"`; |
| 48 | + } |
| 49 | + seenObjects.add(val); |
| 50 | + } |
| 51 | + |
| 52 | + // 4. Handle Array, Map, Set |
| 53 | + if (Array.isArray(val)) { |
| 54 | + // Recursively serialize each element |
| 55 | + return `[${val.map(valueToCode).join(', ')}]`; |
| 56 | + } |
| 57 | + |
| 58 | + if (val instanceof Map) { |
| 59 | + // Serialize Map entries into an array of [key, value] pairs |
| 60 | + const entries = Array.from(val.entries()).map( |
| 61 | + ([k, v]) => `[${valueToCode(k)}, ${valueToCode(v)}]` |
| 62 | + ); |
| 63 | + return `new Map([${entries.join(', ')}])`; |
| 64 | + } |
| 65 | + |
| 66 | + if (val instanceof Set) { |
| 67 | + // Serialize Set values into an array |
| 68 | + const items = Array.from(val.values()).map(valueToCode); |
| 69 | + return `new Set([${items.join(', ')}])`; |
| 70 | + } |
| 71 | + |
| 72 | + // 5. Handle plain objects (the default object type) |
| 73 | + if (type === 'object') { |
| 74 | + const properties: string[] = []; |
| 75 | + |
| 76 | + // Iterate over the object's own enumerable properties |
| 77 | + for (const key in val) { |
| 78 | + if (Object.prototype.hasOwnProperty.call(val, key)) { |
| 79 | + // Wrap the key in JSON.stringify to handle non-identifier keys |
| 80 | + properties.push(`${JSON.stringify(key)}: ${valueToCode(val[key])}`); |
| 81 | + } |
| 82 | + } |
| 83 | + return `{${properties.join(', ')}}`; |
| 84 | + } |
| 85 | + |
| 86 | + // 6. Fallback case (e.g., BigInt, other object types) |
| 87 | + // Coerce to string and then JSON.stringify that string for safety |
| 88 | + return JSON.stringify(String(val)); |
| 89 | + } |
| 90 | + |
| 91 | + // Start serialization for the top-level object |
| 92 | + const topLevelProps: string[] = []; |
| 93 | + |
| 94 | + // Iterate over the properties of the root 'options' object |
| 95 | + for (const key in options) { |
| 96 | + if (Object.prototype.hasOwnProperty.call(options, key)) { |
| 97 | + topLevelProps.push(`${JSON.stringify(key)}: ${valueToCode(options[key])}`); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return `{${topLevelProps.join(', ')}}`; |
| 102 | +} |
0 commit comments