0

I am a big fan of sugar. Here's how a gzip compression statement works in Node.JS:

var q = require('qtree');
var zlib = require('zlib');

q(someData)
.then(q.nbind(zlib.gzip, zlib))
.then(function(gzippedData) {
    // play with gzipped data
})
.done();

Now zlib.gzip is a convenience method, and according to Node Documentation:
The convenience methods use the default settings for all options.

Now, how do I set (default) options for these convenience methods? Because I cannot use the same sugar writing style when I first do:

var gzip = zlib.createGzip({
    level : zlib.Z_BEST_COMPRESSION
});

this new gzip is not a function in the form of gzip(input, callback) so I cannot use it that way.

Redsandro
  • 11,060
  • 13
  • 76
  • 106
  • Create your own promisified method on top of gzip.createGzip that pipes to it and hooks on it finishing. Then call that method instead of `q.nbind...` :) – Benjamin Gruenbaum Apr 06 '14 at 05:08
  • That's the solution I'm trying to circumvent, as it's a "lot of work" that any good "framework" should do out of the box imo. I figured out an easy solution, I will post as an answer. – Redsandro Apr 07 '14 at 14:07

1 Answers1

0

I figured out a trick to do exactly what I want without having to wrap zlib.createGzip() in defers with listeners for every necessary event. Just change the default compression. It's not documented but the defaults can be changed:

zlib.Z_DEFAULT_COMPRESSION = 6;

Note though, that the default is set to -1, and according to zlib documentation, that's, the 'optimal' speed/compression ratio setting, usually equal to 8. But I don't know about that. In Node.js I sometimes found I have pretty poor compression on the default setting.

Redsandro
  • 11,060
  • 13
  • 76
  • 106
  • 1
    The fact the defaults can be changed and it's not documented means this can easily break between node versions, node minor versions and even security updates. Do not rely on unspecified API behavior. Write a 4 line wrapper instead or use a library that lets you do this easily like Bluebird instead of Q. – Benjamin Gruenbaum Apr 07 '14 at 16:39
  • There is no such thing as a 4 line wrapper that implements the listeners. Otherwise be my guest and I'll gladly accept your answer. _"Bluebird instead of Q"_ is not an answer, just a preference. `Q` more closely follows the `Promise Spec`. `Bluebird` has inaccurate API naming choices. The only plus Bluebird has is speed. `Q2` will change that. I appreciate any advice, but shamelessly plugging Bluebird for irrelevant reasons is not advice. (Upvoted for your initial advice though. :)) – Redsandro Apr 07 '14 at 19:48
  • 1
    A bit off topic but whatever: Fwiw - Bluebird adheres to the A+ spec closely and passes the whole test suite - it will also be faster than Q for as long as Kris can't get Petka to work on Q :P Right now a lot of people like RSVP's Stef are trying to help Kris but honestly Petka is the only involved promise library party with intimate knowledge of the V8 compiler, what it optimizes, deoptimizes and so on - he is also more aware of new optimizations, partially because some of those he writes into v8 himself. Not to mention Bluebird stack traces etc... Just benchmark the v2 Q branch. – Benjamin Gruenbaum Apr 07 '14 at 19:58
  • Upvoted because interesting but still not relevant. :P I remember a thread with some good points about API naming and other (minor) details that BB (initially?) didn't seem to think about, so I side with Kris for now. Even if I did not, replacing a library for an entire set of projects that will piss off multiple people is not a good way of 'setting a zlib default'. – Redsandro Apr 07 '14 at 20:00
  • Oh yeah, those API issues were resolved rather quickly when Petka and Domenic sat on those and fixed fulfilled/resolved naming - that was ages ago :) The whole point of the A+ spec if that you can swap out the library and still interop with other libraries correctly without a fuss. Anyway, you just need a method that `return new Promise(function(resolve,reject){...` that creates a readStream for the input (probably with resumer as that's easy there), pipes it to a createGZip , and does `.on("data",function(){ /* accumulate */}).on("end",resolve.bind(null,data)).on("error",reject);` – Benjamin Gruenbaum Apr 07 '14 at 20:12