-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace bfj with custom json utils
- Loading branch information
Showing
4 changed files
with
90 additions
and
169 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Readable } from 'stream'; | ||
|
||
/** | ||
* Recursively serialize an object (including arrays, Dates, etc.) into JSON chunks. | ||
* This function is a generator that yields small JSON string segments. | ||
*/ | ||
function* serializeObject<T>( | ||
obj: T, | ||
depth: number = 0, | ||
indent: string = ' ', | ||
): Generator<string, void, undefined> { | ||
const currentIndent = indent.repeat(depth); | ||
const nextIndent = indent.repeat(depth + 1); | ||
|
||
// 1. Handle Date | ||
if (obj instanceof Date) { | ||
yield JSON.stringify(obj.toISOString()); | ||
return; | ||
} | ||
|
||
// 2. Handle Array | ||
if (Array.isArray(obj)) { | ||
yield '[\n'; | ||
for (let i = 0; i < obj.length; i++) { | ||
if (i > 0) { | ||
// separate elements by comma + newline | ||
yield ',\n'; | ||
} | ||
yield nextIndent; | ||
// recursively yield elements | ||
yield* serializeObject(obj[i], depth + 1, indent); | ||
} | ||
yield `\n${currentIndent}]`; | ||
return; | ||
} | ||
|
||
// 3. Handle Object | ||
if (obj !== null && typeof obj === 'object') { | ||
yield '{\n'; | ||
const entries = Object.entries(obj); | ||
for (let i = 0; i < entries.length; i++) { | ||
const [key, value] = entries[i]; | ||
if (i > 0) { | ||
// separate properties by comma + newline | ||
yield ',\n'; | ||
} | ||
yield `${nextIndent}${JSON.stringify(key)}: `; | ||
yield* serializeObject(value, depth + 1, indent); | ||
} | ||
yield `\n${currentIndent}}`; | ||
return; | ||
} | ||
|
||
// 4. Handle primitives (string, number, boolean, null, etc.) | ||
// Use JSON.stringify() to ensure valid JSON. | ||
yield JSON.stringify(obj); | ||
} | ||
|
||
/** | ||
* Create a Readable stream that pushes JSON data by consuming our generator. | ||
*/ | ||
export function objectToReadableStream<T>(obj: T, indent: string = ' '): Readable { | ||
const generator = serializeObject(obj, 0, indent); | ||
|
||
return new Readable({ | ||
read() { | ||
const { value, done } = generator.next(); | ||
if (done) { | ||
this.push(null); // Signal end of the stream | ||
} else { | ||
this.push(value); | ||
} | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters