Mascarpone
Mascarpone

Reputation: 2556

Write a brotli stream by chunks

I'm trying to consume a subscription channel and write it to a compress stream by appending the messages:

import zlib from 'zlib'
import fs from 'fs'

const writeStream = fs.createWriteStream('test.br')
const brotli = zlib.createBrotliCompress()
const stream = brotli.pipe(writeStream)


stream.write('Hello ')
stream.write('world')
stream.close()

Unfortunately the file is a plain string, with no compression.

What am I doing wrong?

On the other hand it works if I read from file or try this:

import zlib from 'zlib'
import fs from 'fs'
import {Readable} from 'stream'


const writeStream = fs.createWriteStream('test.br')
const brotli = zlib.createBrotliCompress()
Readable.from('Hello World').pipe(brotli).pipe(writeStream)

but I lose the ability to write chunks.

Edit: I made it work, but I'm not sure why:

import zlib from 'zlib'
import fs from 'fs'
import {PassThrough} from 'stream'

const writeStream = fs.createWriteStream('test.br')
const brotli = zlib.createBrotliCompress()
const dup = new PassThrough()

const pipedStream = dup.pipe(brotli).pipe(writeStream)

const finish = dup

finish.write('Hello ')
finish.write(' world')
finish.end()
  1. Why do I need a Passthrough stream instead of writing directly to brotli?
  2. Why I need to write to the duplex stream (dup) and not pipedStream ?

Upvotes: 0

Views: 758

Answers (1)

Mascarpone
Mascarpone

Reputation: 2556

The solution is:

import zlib from 'zlib'
import fs from 'fs'

const writeStream = fs.createWriteStream('stream.br')
const brotli = zlib.createBrotliCompress()
brotli.pipe(writeStream)


brotli.write('Hello ')
brotli.write('world')
brotli.end()

close() terminates the stream without allowing the underling stream to consume the data, end() should be used instead

Upvotes: 1

Related Questions