Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use @gfx/zopfli with Nodejs streams. #21

Open
gios opened this issue Jan 15, 2022 · 1 comment
Open

Use @gfx/zopfli with Nodejs streams. #21

gios opened this issue Jan 15, 2022 · 1 comment

Comments

@gios
Copy link

gios commented Jan 15, 2022

I am wondering if possible to use this library as a stream like node-zopfli. I've found only callback and async methods, it would be great to add methods which implements stream interface.

node-zopfli

const zopfli = require('node-zopfli');
fs.createReadStream('file.js')
  .pipe(zopfli.createGzip(options))
  .pipe(fs.createWriteStream('file.js.gz'));

@gfx/zopfli (hypothetical)

const zopfli = require('@gfx/zopfli'); // or const { createGzip } = require('@gfx/zopfli');
fs.createReadStream('file.js')
  .pipe(zopfli.createGzip(options)) // or  .pipe(createGzip(options))
  .pipe(fs.createWriteStream('file.js.gz'));
@monochromer
Copy link

I used @gfx/zopfli with streams something like that:

import stream from 'node:stream'
import fs from 'node:fs'
import { gzipAsync } from '@gfx/zopfli'

await stream.promises.pipeline(
  fs.createReadStream('./file.txt'),
  async function* (source) {
    for await (const chunk of source) {
      yield await gzipAsync(chunk)
    }
  },
  fs.createWriteStream('./file.txt.gzip')
)

but best compression achieved with one entire chunk:

import stream from 'node:stream'
import fs from 'node:fs'
import { gzipAsync } from '@gfx/zopfli'

await stream.promises.pipeline(
  fs.createReadStream('./file.text'),
  async function* (source) {
    const chunks = []
    for await (const chunk of source) {
      chunks.push(chunk)
    }
    yield await gzipAsync(Buffer.concat(chunks))
  },
  fs.createWriteStream('./file.text.gzip')
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants