Skip to content

Instantly share code, notes, and snippets.

@hubgit
Last active August 26, 2022 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hubgit/7ee9693989b74c4414be0eb1a10836b1 to your computer and use it in GitHub Desktop.
Save hubgit/7ee9693989b74c4414be0eb1a10836b1 to your computer and use it in GitHub Desktop.
Read/write a gzipped JSON lines file with Deno
const createInputReader = async (path: string) => {
const file = await Deno.open(path, {
read: true,
})
return file.readable
.pipeThrough(new DecompressionStream('gzip'))
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream())
.pipeThrough(
new TransformStream({
transform(chunk, controller) {
controller.enqueue(JSON.parse(chunk))
},
})
)
}
// const input = await createInputReader('example.jsonl.gz')
// for await (const item of input) {
// // do something
// }
const createOutputWriter = async (path: string) => {
const file = await Deno.open(path, {
create: true,
write: true,
truncate: true,
})
const stream = new TransformStream()
stream.readable
.pipeThrough(
new TransformStream({
transform(chunk, controller) {
controller.enqueue(JSON.stringify(chunk))
controller.enqueue('\n')
},
})
)
.pipeThrough(new TextEncoderStream())
.pipeThrough(new CompressionStream('gzip'))
.pipeTo(file.writable)
return stream.writable.getWriter()
}
// const writer = await createOutputWriter('example.jsonl.gz')
// await writer.write({ foo: 'bar' })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment