How to gzipSync?

1,111 views
Skip to first unread message

Axel Kittenberger

unread,
Apr 18, 2012, 5:12:36 AM4/18/12
to nodejs
Is there some easy way to make yourself a zlib.gzipSync wrapper around
the nodes default callback/pipe interface to zlib? (Without streamline
or fibers)

Its for the server startup sequence where it generates one hugh
javascript file for the client side to download and it keeps it raw as
well gzipped in memory. It does read the files with readSyncFile etc.
So yes I want the node process to be halted until it is done. Its fine
it takes 2 seconds, so please don't argue it could do this or that in
parallel. I also do the other *Sync calls for startup. I just want to
keep that simple please.

- Axel

Jake Verbaten

unread,
Apr 18, 2012, 5:46:26 AM4/18/12
to nod...@googlegroups.com
An asynchronous start up sequence is significantly more trivial to implement then writing a wrapper for gzipSync


- Axel

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Arnout Kazemier

unread,
Apr 18, 2012, 5:50:32 AM4/18/12
to nod...@googlegroups.com
Or you could just add your server.listen in the callback of the gzip and you achieve the same result.
A delayed startup of your server, until the shit is gziped

Oliver Leics

unread,
Apr 18, 2012, 5:59:24 AM4/18/12
to nod...@googlegroups.com
Am also interested in some easy way.
What are the options?

BTW: Please, stay with the topic. It is about "how to do it" and not
about "convince me to do not this way" Thank you.

--
Oliver Leics @ G+
https://plus.google.com/112912441146721682527

Axel Kittenberger

unread,
Apr 18, 2012, 6:07:38 AM4/18/12
to nod...@googlegroups.com

Yes thank you. That's a pragmatic solution that only came to my mind after I posted. I suppose later ill have to do a more developed startup anyway when I'm utilizing a database.

Oliver Leics

unread,
Apr 18, 2012, 6:13:25 AM4/18/12
to nod...@googlegroups.com
OP governs the thread. Thats the way.. :-)

--

dhruvbird

unread,
Apr 19, 2012, 8:30:31 AM4/19/12
to nodejs
Hello,

You could (at least in theory) spawn a gzip process that writes to a
FIFO* (named pipe) and then use fs.readFileSync() to read that FIFO.
Your process should block till you have completed reading in the FIFO.
Create a wrapper function to do this an you have a blocking gzip()
routine!

* Might not work on windows.

Regards,
-Dhruv.

Tim Caswell

unread,
Apr 19, 2012, 8:35:34 AM4/19/12
to nod...@googlegroups.com
I wonder why node doesn't just expose a sync version of the zlib functions.  It's not like they are performing I/O or anything. FWIW I intend to have both sync and async in luvit's zlib bindings unless there is a good reason not to.

Jorge

unread,
Apr 19, 2012, 9:01:29 AM4/19/12
to nod...@googlegroups.com
On Apr 19, 2012, at 2:35 PM, Tim Caswell wrote:

> I wonder why node doesn't just expose a sync version of the zlib functions. It's not like they are performing I/O or anything. FWIW I intend to have both sync and async in luvit's zlib bindings unless there is a good reason not to.

Did you say *sync*? Heresy!
--
Jorge.

Ben Noordhuis

unread,
Apr 19, 2012, 9:22:26 AM4/19/12
to nod...@googlegroups.com
On Thu, Apr 19, 2012 at 14:35, Tim Caswell <t...@creationix.com> wrote:
> I wonder why node doesn't just expose a sync version of the zlib functions.
> It's not like they are performing I/O or anything. FWIW I intend to have
> both sync and async in luvit's zlib bindings unless there is a good reason
> not to.

Here's why: https://github.com/joyent/node/issues/3128#issuecomment-5197171

Bruno Jouhier

unread,
Apr 19, 2012, 1:46:37 PM4/19/12
to nod...@googlegroups.com
Sync calls should only be allowed in startup sequence and in require.

Would it be possible to have a "strict async" switch that make all Sync calls throw if called outside of startup or require sequences?
This would make it easy to spot libraries that are potential perf killers.

Bruno

Mark Hahn

unread,
Apr 19, 2012, 1:48:39 PM4/19/12
to nod...@googlegroups.com
 "strict async" switch 

+1

Axel Kittenberger

unread,
Apr 19, 2012, 5:12:41 PM4/19/12
to nod...@googlegroups.com
> Would it be possible to have a "strict async" switch that make all Sync
> calls throw if called outside of startup or require sequences?
> This would make it easy to spot libraries that are potential perf killers.

I'm not sure how the node modules system behaves if you alter required
tables, so would this work*?

var gotyou = function() { throw new Error('Evil Sync caller you!'); };
for (var k in fs) {
if (fs.hasOwnProperty(k) && k.search(/Sync$/) >= 0) { fs[k] = gotyou; }
}

*Unless something is that evil to cache the function pointers, or to
require later on. However, one could set "require" to gotyou as well
after startup.

Oleg Efimov (Sannis)

unread,
Apr 20, 2012, 1:05:08 AM4/20/12
to nod...@googlegroups.com
Latest Crockford's JSLint have an option to alert Sync functions usage.

You proposal is more complicated, I don't really think how to realize this :(

четверг, 19 апреля 2012 г., 21:46:37 UTC+4 пользователь Bruno Jouhier написал:

Bruno Jouhier

unread,
Apr 20, 2012, 3:12:33 AM4/20/12
to nod...@googlegroups.com
Would work for most but my stuff requires additional modules dynamically as it runs, to avoid polluting startup with things that may never be needed (like dev/test/maintenance functions). So, I'd need to restore the Sync functions temporarily for require. Looks like it can be done by wrapping the require hooks.

The bottom line is that require should not be sync. It does not work well if you require modules dynamically, it also makes it harder to hack require so that it loads code from something else than the local file system (a database or a RESTful service). But it is probably too late to do anything about it.

Bruno

Isaac Schlueter

unread,
Apr 20, 2012, 11:06:29 AM4/20/12
to nod...@googlegroups.com
On Thu, Apr 19, 2012 at 10:46, Bruno Jouhier <bjou...@gmail.com> wrote:
> Sync calls should only be allowed in startup sequence and in require.
>
> Would it be possible to have a "strict async" switch that make all Sync
> calls throw if called outside of startup or require sequences?
> This would make it easy to spot libraries that are potential perf killers.

npm install nosync

require("nosync")
fs.openSync("filename", "r") // <-- throws

Axel Kittenberger

unread,
Apr 23, 2012, 7:30:14 AM4/23/12
to nod...@googlegroups.com
> npm install nosync
>
> require("nosync")
> fs.openSync("filename", "r") // <-- throws

Cool. Thats why I don't buy into the argument, the Sync versions of
the un/compress API would lead to people misusing it, since it can be
inhibited as easy as you showed us.

Although I do buy into the argument, it is hardly used/requested and
for the amount of code to maintain it's not worth it and easy to
workaround with Asyncs rather than bloating the node core.

Egor Egorov

unread,
Apr 25, 2012, 12:44:32 PM4/25/12
to nod...@googlegroups.com

Axel Kittenberger

unread,
Apr 26, 2012, 3:37:38 AM4/26/12
to nod...@googlegroups.com
Great, I actually seen this, when I searched for a solution, but I
need gzip, not compress.

If this isn't "compress" but gzip after all, you really should change
your README to highlight this. Maybe its the age from back of the
1990s so when one read or wrote "compress" without any highlight, Its
assumed the compression algorithmn that used to be called "compress",
that is LZ77 without huffman encoding.

Matthew Hazlett

unread,
Apr 26, 2012, 9:03:33 PM4/26/12
to nod...@googlegroups.com
Just an FYI.

Geddy has a mailing list now:
https://groups.google.com/forum/?fromgroups#!forum/geddyjs


Reply all
Reply to author
Forward
0 new messages