Use only synchronous operations in process exit handler

According to
http://nodejs.org/api/process.html#process_event_exit only synchronous
operations will complete in the process exit handler. Since normal stream
writing to a file is asynchronous this resulted in an empty file.

This patch uses a memstream & fs.writeFileSync to avoid this issue.

Fixes #3.