@worker-tools/json-stream
Version:
Utilities for working with streaming JSON in Worker Runtimes such as Cloudflare Workers, Deno Deploy and Service Workers.
24 lines (22 loc) • 624 B
text/typescript
// deno-lint-ignore-file no-explicit-any
import { jsonStringifyGenerator } from './json-stringify.js'
export class JSONStringifyStream extends TransformStream<any, string> {
constructor() {
let first: boolean;
super({
start(controller) {
first = true;
controller.enqueue('[')
},
async transform(obj, controller) {
if (!first) controller.enqueue(','); else first = false;
for await (const chunk of jsonStringifyGenerator(obj)) {
controller.enqueue(chunk)
}
},
flush(controller) {
controller.enqueue(']')
},
})
}
}