( value: unknown, budget: SerializedByteBudget, position: 'top' | 'array' | 'object', )
| 26 | } |
| 27 | |
| 28 | function countSerializedValue( |
| 29 | value: unknown, |
| 30 | budget: SerializedByteBudget, |
| 31 | position: 'top' | 'array' | 'object', |
| 32 | ): boolean { |
| 33 | if (value === undefined || typeof value === 'function' || typeof value === 'symbol') { |
| 34 | if (position === 'object') return false; |
| 35 | if (position === 'top' && value !== undefined) return false; |
| 36 | addSerializedBytes(budget, 4); |
| 37 | return true; |
| 38 | } |
| 39 | if (value === null) { |
| 40 | addSerializedBytes(budget, 4); |
| 41 | return true; |
| 42 | } |
| 43 | if (typeof value === 'string') { |
| 44 | countJsonString(value, budget); |
| 45 | return true; |
| 46 | } |
| 47 | if (typeof value === 'boolean') { |
| 48 | addSerializedBytes(budget, value ? 4 : 5); |
| 49 | return true; |
| 50 | } |
| 51 | if (typeof value === 'number') { |
| 52 | const encoded = Number.isFinite(value) ? JSON.stringify(value) : 'null'; |
| 53 | addSerializedBytes(budget, encoded.length); |
| 54 | return true; |
| 55 | } |
| 56 | if (typeof value === 'bigint' || typeof value !== 'object') return false; |
| 57 | if (budget.seen.has(value)) return false; |
| 58 | if (typeof (value as { toJSON?: unknown }).toJSON === 'function') return false; |
| 59 | if (!Array.isArray(value)) { |
| 60 | const prototype = Object.getPrototypeOf(value); |
| 61 | if (prototype !== Object.prototype && prototype !== null) return false; |
| 62 | } |
| 63 | |
| 64 | budget.seen.add(value); |
| 65 | try { |
| 66 | if (Array.isArray(value)) { |
| 67 | if (!addSerializedBytes(budget, 1)) return true; |
| 68 | for (let index = 0; index < value.length; index += 1) { |
| 69 | if (index > 0 && !addSerializedBytes(budget, 1)) return true; |
| 70 | if (!countSerializedValue(value[index], budget, 'array')) return false; |
| 71 | if (budget.bytes > budget.limit) return true; |
| 72 | } |
| 73 | addSerializedBytes(budget, 1); |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | if (!addSerializedBytes(budget, 1)) return true; |
| 78 | let emitted = 0; |
| 79 | for (const key of Object.keys(value)) { |
| 80 | const item = (value as Record<string, unknown>)[key]; |
| 81 | if (item === undefined || typeof item === 'function' || typeof item === 'symbol') continue; |
| 82 | if (emitted > 0 && !addSerializedBytes(budget, 1)) return true; |
| 83 | if (!countJsonString(key, budget)) return true; |
| 84 | if (!addSerializedBytes(budget, 1)) return true; |
| 85 | if (!countSerializedValue(item, budget, 'object')) return false; |
no test coverage detected