(
key: string, value: unknown, maxBytes: number)
| 64 | // most `maxBytes` UTF-8 bytes. Returns the serialized length on success so |
| 65 | // callers can enforce a cumulative cap without serializing twice. |
| 66 | const validatePluginValue = ( |
| 67 | key: string, value: unknown, maxBytes: number): {ok: true, bytes: number} | {ok: false, reason: string} => { |
| 68 | let serialized: string; |
| 69 | try { |
| 70 | serialized = JSON.stringify(value); |
| 71 | } catch (e: any) { |
| 72 | return {ok: false, reason: `JSON.stringify failed: ${e && e.message || e}`}; |
| 73 | } |
| 74 | if (serialized === undefined) { |
| 75 | // JSON.stringify returns undefined for top-level functions/undefined. |
| 76 | return {ok: false, reason: 'value is not JSON-serializable (function/undefined)'}; |
| 77 | } |
| 78 | const bytes = Buffer.byteLength(serialized, 'utf8'); |
| 79 | if (bytes > maxBytes) { |
| 80 | return {ok: false, reason: `serialized size ${bytes}B exceeds per-key cap ${maxBytes}B`}; |
| 81 | } |
| 82 | return {ok: true, bytes}; |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * Copied from the Etherpad source code. It converts Windows line breaks to Unix |
no outgoing calls
no test coverage detected