| 311 | * @returns Parsed WalFormat with defaults filled in |
| 312 | */ |
| 313 | export function parseWalFormat<T extends WalRecord = WalRecord>( |
| 314 | format: Partial<WalFormat<T>>, |
| 315 | ): WalFormat<T> { |
| 316 | const { |
| 317 | baseName = 'wal', |
| 318 | walExtension = '.log', |
| 319 | finalExtension = walExtension, |
| 320 | codec = stringCodec<T>(), |
| 321 | } = format; |
| 322 | |
| 323 | const finalizer = |
| 324 | format.finalizer ?? |
| 325 | ((records: (T | InvalidEntry<string>)[]) => { |
| 326 | // Encode each record using the codec before joining. |
| 327 | // For object types, codec.encode() will JSON-stringify them properly. |
| 328 | // InvalidEntry records use their raw string value directly. |
| 329 | const encoded = records.map(record => |
| 330 | typeof record === 'object' && record != null && '__invalid' in record |
| 331 | ? (record as InvalidEntry<string>).raw |
| 332 | : codec.encode(record as T), |
| 333 | ); |
| 334 | return `${encoded.join('\n')}\n`; |
| 335 | }); |
| 336 | |
| 337 | return { |
| 338 | baseName, |
| 339 | walExtension, |
| 340 | finalExtension, |
| 341 | codec, |
| 342 | finalizer, |
| 343 | } satisfies WalFormat<T>; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * NOTE: this helper is only used within the scope of wal and sharded wal logic. The rest of the repo avoids sync methods so it is not reusable. |