Skip to content

Commit b6f2bb6

Browse files
committed
refactor: wip
1 parent e4f66d5 commit b6f2bb6

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

packages/utils/src/lib/wal.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,9 @@ export const stringCodec = <
290290
* - walExtension defaults to '.log'
291291
* - finalExtension defaults to '.log'
292292
* - codec defaults to stringCodec<T>()
293-
* - finalizer defaults to (encodedRecords: (T | InvalidEntry<string>)[]) => `${encodedRecords.join('\n')}\n`
293+
* - finalizer defaults to encoding each record using codec.encode() and joining with newlines.
294+
* For object types, this properly JSON-stringifies them (not [object Object]).
295+
* InvalidEntry records use their raw string value directly.
294296
* @param format - Partial WalFormat configuration
295297
* @returns Parsed WalFormat with defaults filled in
296298
*/
@@ -306,8 +308,11 @@ export function parseWalFormat<T extends object | string = object>(
306308

307309
const finalizer =
308310
format.finalizer ??
309-
((encodedRecords: (T | InvalidEntry<string>)[]) => {
310-
const encoded = encodedRecords.map(record =>
311+
((records: (T | InvalidEntry<string>)[]) => {
312+
// Encode each record using the codec before joining.
313+
// For object types, codec.encode() will JSON-stringify them properly.
314+
// InvalidEntry records use their raw string value directly.
315+
const encoded = records.map(record =>
311316
typeof record === 'object' && record != null && '__invalid' in record
312317
? (record as InvalidEntry<string>).raw
313318
: codec.encode(record as T),

packages/utils/src/lib/wal.unit.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,19 @@ describe('parseWalFormat', () => {
662662
const output = result.finalizer(records);
663663
expect(output).toBe('valid\ninvalid-raw\nalso-valid\n');
664664
});
665+
666+
it('should encode objects correctly when using default type parameter', () => {
667+
// Test parseWalFormat({}) with default type parameter (object)
668+
const result = parseWalFormat({});
669+
const records = [
670+
{ id: 1, name: 'test1' },
671+
{ id: 2, name: 'test2' },
672+
];
673+
const output = result.finalizer(records);
674+
// Should be JSON strings, not [object Object]
675+
expect(output).toBe('{"id":1,"name":"test1"}\n{"id":2,"name":"test2"}\n');
676+
expect(output).not.toContain('[object Object]');
677+
});
665678
});
666679

667680
describe('isLeaderWal', () => {

0 commit comments

Comments
 (0)