<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>dave eddy</title>
		<description>All Dropped Packets go to Heaven</description>
		<link type="application/atom+xml" href="http://www.daveeddy.com" rel="self"/>
		<link type="text" href="http://www.daveeddy.com" rel="alternate"/>
		<atom:link href="/rss.xml" rel="self" type="application/rss+xml" />
		<updated>2013-04-19T18:36:17+00:00</updated>
		<id>http://www.daveeddy.com</id>
		<author>
			<name>Dave Eddy</name>
		</author>
		<rights>Copyright (c) 2012 Dave Eddy</rights>
		
		<item>
			<title>Synchronous File IO in Node.js</title>
			<link>http://www.daveeddy.com/2013/03/26/synchronous-file-io-in-nodejs/</link>
			<updated>2013-03-26T00:00:00+00:00</updated>
			<pubDate>2013-03-26T00:00:00+00:00</pubDate>
			<id>http://www.daveeddy.com/2013/03/26/synchronous-file-io-in-nodejs/</id>
			<gid>http://www.daveeddy.com/2013/03/26/synchronous-file-io-in-nodejs/</gid>
			<author></author>
			<summary type="html">&lt;blockquote&gt;&lt;p&gt;Does calling &lt;code&gt;fs.writeFileSync&lt;/code&gt; trigger a synchronous write to the file system?&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;If you are familiar with &lt;a href=&quot;http://nodejs.org&quot;&gt;Node.js&lt;/a&gt;, or have at least heard of it, you've
most likely heard that it uses non-blocking IO, and lets you do work
asynchronously. One of the most basic APIs that Node provides is for the &lt;a href=&quot;http://nodejs.org/api/fs.html&quot;&gt;file
system&lt;/a&gt;; With this API, you can read, write, remove, etc. files and do
other file system related tasks and modifications.&lt;/p&gt;

&lt;p&gt;This API follows a standard pattern of exposing 2 functions for each operation:
one for asynchronous work, and the other for synchronous work.  For example, if
you want to read a file in Node you can do so asynchronously:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;fs&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/etc/passwd&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Node will continue executing any javascript code it encounters while reading
the file.  Once all javascript is done being executed and the file is ready, it
will run the anonymous function and print the file contents.&lt;/p&gt;

&lt;p&gt;You can do the same task synchronously:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;fs&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;contents&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readFileSync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/etc/passwd&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;contents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;In this example, &lt;code&gt;contents&lt;/code&gt; will be set to the contents of the file, and no
javascript code will be executed while the file is being read.&lt;/p&gt;

&lt;p&gt;The first approach is done asynchronously, and will return immediately to not
block your code from running.  The second is done synchronously, and will halt
execution until the task has completed.  The same 2 types of functions exist
for writing, renaming, deleting, etc. files.&lt;/p&gt;

&lt;h2&gt;Synchronous Writes&lt;/h2&gt;

&lt;p&gt;So the question is, does calling &lt;code&gt;fs.writeFileSync&lt;/code&gt; actually trigger a
synchronous write to the file system?  In the userland Node process, it's
synchronous in the sense that execution of any javascript is halted, but what
about in the Kernel?  An asynchronous write is a very different thing from a
synchronous write to a file system.&lt;/p&gt;

&lt;p&gt;For the rest of this blog post I'll be speaking within the context of the
&lt;a href=&quot;http://illumos.org/&quot;&gt;Illumos&lt;/a&gt; Kernel, and the &lt;a href=&quot;http://en.wikipedia.org/wiki/Zfs&quot;&gt;ZFS File System&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;There are a couple ways to answer this question.  The most obvious way is to
pull the Node.js source code, find the functions that talk to the file
system that &lt;code&gt;fs.js&lt;/code&gt; uses, and see how they are called.  I haven't done much
work on the Node core, and know it could (and most likely would) take a long time
to find the code I was looking for. Instead, I'll just use &lt;a href=&quot;http://dtrace.org&quot;&gt;DTrace&lt;/a&gt; to
answer the question, and see exactly what Node is doing.&lt;/p&gt;

&lt;h2&gt;DTrace to the Rescue&lt;/h2&gt;

&lt;p&gt;I wrote a couple test programs that exercise these file system functions.
Using DTrace, we'll be able to see what flags a file is opened with, which
will show whether the operations are synchronous or not.&lt;/p&gt;

&lt;h3&gt;fs.writeFile()&lt;/h3&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// writefile.js&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;fs&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;writeFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/tmp/fs.tmp&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This script exercises Node's asynchronous file writing mechanism.  Using
DTrace, we can print the flags that were passed to &lt;code&gt;open(2)&lt;/code&gt; for that specific
file.  Then, using &lt;a href=&quot;https://github.com/papertigers/decimal-to-oflag&quot;&gt;fileflags&lt;/a&gt;, we can turn that decimal into the
symbolic names that make up the decimal (see &lt;code&gt;open(2)&lt;/code&gt; for more information).&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ sudo dtrace -qn 'syscall::open*:entry /pid == $target &amp;amp;&amp;amp; copyinstr(arg0) == &quot;/tmp/fs.tmp&quot;/ { printf(&quot;%s: %d&quot;, probefunc, arg1); }' -c 'node writefile.js'
open64: 769
$ fileflags 769
  769: O_WRONLY|O_CREAT|O_TRUNC
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first command tells DTrace to run &lt;code&gt;node writefile.js&lt;/code&gt;, and look for any of
the open family of syscalls.  If the first argument to open (the pathname)
matches the file we are writing to, print out the exact syscall fired, and the
flags decimal.&lt;/p&gt;

&lt;p&gt;It turns out that &lt;code&gt;open64(2)&lt;/code&gt; was called for our file, given the following
options.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;O_WRONLY&lt;/code&gt;: open write-only&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_CREAT&lt;/code&gt;: create the file if it doesn't exist&lt;/li&gt;
&lt;li&gt;&lt;code&gt;O_TRUNC&lt;/code&gt;: truncate the file&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Fairly standard options to open a file. Since none of the options are for
synchronous IO (&lt;code&gt;O_SYNC&lt;/code&gt;, &lt;code&gt;O_DSYNC&lt;/code&gt;, etc.) this file write is asynchronous to
ZFS, and the call to &lt;code&gt;write(2)&lt;/code&gt; returns before the data is guaranteed to be
sitting on stable storage.&lt;/p&gt;

&lt;p&gt;Node's asynchronous &lt;code&gt;fs.writeFile&lt;/code&gt; does indeed do an asynchronous write to the
file system.&lt;/p&gt;

&lt;h3&gt;fs.writeFileSync()&lt;/h3&gt;

&lt;p&gt;So what about Node's synchronous file writing mechanism, is it an actual
synchronous write to the file system?&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// writefilesync.js&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;fs&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;writeFileSync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/tmp/fs.tmp&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This script will block the event loop while the data is written to the file (or so we think),
as it uses Node's synchronous file writing mechanism.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ sudo dtrace -qn 'syscall::open*:entry /pid == $target &amp;amp;&amp;amp; copyinstr(arg0) == &quot;/tmp/fs.tmp&quot;/ { printf(&quot;%s: %d&quot;, probefunc, arg1); }' -c 'node writefilesync.js'
open64: 769
$ fileflags 769
  769: O_WRONLY|O_CREAT|O_TRUNC
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Same commands as above, and the same output.&lt;/p&gt;

&lt;p&gt;Node's &lt;code&gt;fs.writeFileSync&lt;/code&gt; does &lt;strong&gt;NOT&lt;/strong&gt; initiate a synchronous write to the file
system.&lt;/p&gt;

&lt;p&gt;From the perspective of a Node program, we know the same thing when a call to
&lt;code&gt;fs.writeFileSync&lt;/code&gt; returns, as we know when the callback to &lt;code&gt;fs.writeFile&lt;/code&gt; is
fired.  We know the underlying call, &lt;code&gt;write(2)&lt;/code&gt; has returned; We do &lt;strong&gt;NOT&lt;/strong&gt; know
that the data has made it to stable storage.  The only difference then, is that
one function blocks Node's event loop, while the other allows it to continue
processing events.&lt;/p&gt;

&lt;h3&gt;fs.createWriteStream()&lt;/h3&gt;

&lt;p&gt;Another mechanism that allows file IO is to create, and write to, a
Node &lt;code&gt;WritableStream&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// writestream.js&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;fs&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;createWriteStream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/tmp/fs.tmp&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;pre&gt;&lt;code&gt;$ sudo dtrace -qn 'syscall::open*:entry /pid == $target &amp;amp;&amp;amp; copyinstr(arg0) == &quot;/tmp/fs.tmp&quot;/ { printf(&quot;%s: %d&quot;, probefunc, arg1); }' -c 'node writestream.js'
open64: 769
$ fileflags 769
  769: O_WRONLY|O_CREAT|O_TRUNC
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Same output as above, again.  This mechanism opens the file with the same flags
as both &lt;code&gt;fs.writeFile&lt;/code&gt; and &lt;code&gt;fs.writeFileSync&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;fs.appendFile()&lt;/h3&gt;

&lt;p&gt;So writing to a file uses the same flags for opening the file, what about
appending?  Same drill as above&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// apendfile.js&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;fs&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;appendFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/tmp/fs.tmp&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;pre&gt;&lt;code&gt;$ sudo dtrace -qn 'syscall::open*:entry /pid == $target &amp;amp;&amp;amp; copyinstr(arg0) == &quot;/tmp/fs.tmp&quot;/ { printf(&quot;%s: %d&quot;, probefunc, arg1); }' -c 'node appendfile.js'
open64: 265
$ fileflags 265
  265: O_WRONLY|O_APPEND|O_CREAT
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So the flags are different, that's a good sign.  &lt;code&gt;O_TRUNC&lt;/code&gt; has been swapped out
for &lt;code&gt;O_APPEND&lt;/code&gt;, since we are no longer truncating the file to 0 bytes and
instead are appending to it.&lt;/p&gt;

&lt;p&gt;Again, like all the commands above, &lt;code&gt;fs.appendFile&lt;/code&gt; opens the file for
asynchronous IO.&lt;/p&gt;

&lt;h3&gt;fs.appendFileSync()&lt;/h3&gt;

&lt;p&gt;Last but not least let's test out the synchronous version of &lt;code&gt;appendFile&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// appendfilesync.js&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;fs&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;fs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;appendFileSync&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;/tmp/fs.tmp&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;pre&gt;&lt;code&gt;$ sudo dtrace -qn 'syscall::open*:entry /pid == $target &amp;amp;&amp;amp; copyinstr(arg0) == &quot;/tmp/fs.tmp&quot;/ { printf(&quot;%s: %d&quot;, probefunc, arg1); }' -c 'node appendfilesync.js'
open64: 265
$ fileflags 265
  265: O_WRONLY|O_APPEND|O_CREAT
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Same as &lt;code&gt;fs.appendFile&lt;/code&gt;; the file is &lt;strong&gt;NOT&lt;/strong&gt; opened for synchronous writes.&lt;/p&gt;

&lt;h2&gt;Common Flags&lt;/h2&gt;

&lt;p&gt;Let's use a simple C program to open a file using &lt;code&gt;fopen(3C)&lt;/code&gt; to see what flags
it uses.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;c&quot;&gt;    &lt;span class=&quot;cm&quot;&gt;/* fs.c */&lt;/span&gt;
    &lt;span class=&quot;cp&quot;&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;kt&quot;&gt;FILE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fopen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;/tmp/fs-c.tmp&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;w&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Then run it with the same command as above to see what flags the file was opened with.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ gcc fs.c -o fs
$ sudo dtrace -qn 'syscall::open*:entry /pid == $target &amp;amp;&amp;amp; copyinstr(arg0) == &quot;/tmp/fs-c.tmp&quot;/ { printf(&quot;%s: %d&quot;, probefunc, arg1); }' -c './fs'
open: 769
$ fileflags 769
  769: O_WRONLY|O_CREAT|O_TRUNC
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sure enough, the same flags as opening a file for writing in Node land.&lt;/p&gt;

&lt;h2&gt;Results&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;fs.writeFileSync&lt;/code&gt; is synchronous in the sense that it blocks the event loop
while it executes.  It does &lt;strong&gt;NOT&lt;/strong&gt; ask the Kernel to do a synchronous write to the
underlying file system.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;This script will block the event loop while the data is written to the file
(or so we think)...&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;None of the functions above open files for synchronous IO.  Because of this,
all we know is that the call to &lt;code&gt;write(2)&lt;/code&gt; returns, not that the data has been
written to the file system and flushed to stable storage.  Don't get tripped
up on the names, &lt;code&gt;fs.writeSync&lt;/code&gt; doesn't synchronously write to the file system.&lt;/p&gt;

&lt;p&gt;If you want to open a file for synchronous IO, you'll have to use the lower
level fs functions that Node offers such as &lt;code&gt;fs.open()&lt;/code&gt; and &lt;code&gt;fs.fsync()&lt;/code&gt;.&lt;/p&gt;
</summary>
		</item>
		
		<item>
			<title>Chef Performance and Migration</title>
			<link>http://www.daveeddy.com/2013/03/23/chef-performance-and-migration/</link>
			<updated>2013-03-23T00:00:00+00:00</updated>
			<pubDate>2013-03-23T00:00:00+00:00</pubDate>
			<id>http://www.daveeddy.com/2013/03/23/chef-performance-and-migration/</id>
			<gid>http://www.daveeddy.com/2013/03/23/chef-performance-and-migration/</gid>
			<author></author>
			<summary type="html">&lt;p&gt;I wrote 2 blog posts for the &lt;a href=&quot;http://engineering.voxer.com/&quot;&gt;Voxer Engineering Blog&lt;/a&gt;.  The first
post talks about migrating from Hosted Chef to a private, self-hosted Chef
instance.  The second goes over performance analysis of Chef, and getting
it to run &lt;strong&gt;16x&lt;/strong&gt; faster!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://engineering.voxer.com/post/45943967787/chef-part-1-migration&quot;&gt;Chef Part 1 - Migration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://engineering.voxer.com/post/45944103472/chef-part-2-performance&quot;&gt;Chef Part 2 - Performance&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</summary>
		</item>
		
		<item>
			<title>Command line Hue lights</title>
			<link>http://www.daveeddy.com/2013/03/15/command-line-hue-lights/</link>
			<updated>2013-03-15T00:00:00+00:00</updated>
			<pubDate>2013-03-15T00:00:00+00:00</pubDate>
			<id>http://www.daveeddy.com/2013/03/15/command-line-hue-lights/</id>
			<gid>http://www.daveeddy.com/2013/03/15/command-line-hue-lights/</gid>
			<author></author>
			<summary type="html">&lt;blockquote&gt;&lt;p&gt;A command line interface to &lt;a href=&quot;http://meethue.com&quot;&gt;phillips hue&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;I wrote a command line tool to interface with hue lights.  Check
out the project page for more information&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/bahamas10/hue-cli&quot;&gt;https://github.com/bahamas10/hue-cli&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Installation&lt;/h2&gt;

&lt;p&gt;First, install &lt;a href=&quot;http://nodejs.org&quot;&gt;Node.js&lt;/a&gt;, then:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;npm install -g hue-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;...and the executable will be installed globally as &lt;code&gt;hue&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;Usage&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;Usage: hue [-H host] [--json] [command]

control phillips hue over the command line

examples
  hue config          # view the hue config
  hue lights          # get a list of lights
  hue lights 5        # get information about light 5
  hue lights 5,6,7 on # turn lights 5 6 and 7 on
  hue lights on       # turn all lights on
  hue lights 1 ff0000 # turn light 1 red
  hue lights 1 red    # same as above
  hue help            # this message
  hue register        # register this app to hue, done automatically
  hue search          # search for hue base stations

commands
  config, lights, help, register, search

options
  -h, --help     print this message and exit
  -H, --host     the hostname or ip of the bastion to control
  -i, --init     initialize the config file at /Users/dave/.hue.json
  -j, --json     force output to be in json
  -u, --updates  check for available updates
  -v, --version  print the version number and exit
&lt;/code&gt;&lt;/pre&gt;
</summary>
		</item>
		
		<item>
			<title>Unfreeze Photoshop</title>
			<link>http://www.daveeddy.com/2013/02/27/unfreeze-photoshop/</link>
			<updated>2013-02-27T00:00:00+00:00</updated>
			<pubDate>2013-02-27T00:00:00+00:00</pubDate>
			<id>http://www.daveeddy.com/2013/02/27/unfreeze-photoshop/</id>
			<gid>http://www.daveeddy.com/2013/02/27/unfreeze-photoshop/</gid>
			<author></author>
			<summary type="html">&lt;blockquote&gt;&lt;p&gt;Unfreeze a frozen Photoshop on Mac&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;I wrote an app to unfreeze a stuck or frozen photoshop. If you're running photoshop,
and it starts beachballing, or not responding on you, try running this.  This application
will help if suddenly you have a frozen photoshop, and you want to fix it.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://files.daveeddy.com/unfreeze-latest.zip&quot;&gt;Download the app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://files.daveeddy.com/unfreeze-latest.zip&quot;&gt;&lt;img src=&quot;/static/media/github/unfreeze-icon.png&quot; alt=&quot;screenshot&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read more about it &lt;a href=&quot;http://bahamas10.github.com/unfreeze-ps/&quot;&gt;here&lt;/a&gt; or check out this &lt;a href=&quot;http://skyeillustration.com/post/66-photoshop-trick&quot;&gt;blog post&lt;/a&gt;&lt;/p&gt;
</summary>
		</item>
		
		<item>
			<title>High Performance Node.js Logging with console.log buffering</title>
			<link>http://www.daveeddy.com/2012/12/06/high-performance-nodejs-logging-with-consolelog-buffering/</link>
			<updated>2012-12-06T00:00:00+00:00</updated>
			<pubDate>2012-12-06T00:00:00+00:00</pubDate>
			<id>http://www.daveeddy.com/2012/12/06/high-performance-nodejs-logging-with-consolelog-buffering/</id>
			<gid>http://www.daveeddy.com/2012/12/06/high-performance-nodejs-logging-with-consolelog-buffering/</gid>
			<author></author>
			<summary type="html">&lt;blockquote&gt;&lt;p&gt;Buffer calls to &lt;code&gt;console.log&lt;/code&gt;, &lt;code&gt;console.warn&lt;/code&gt;, etc. for high performance logging&lt;/p&gt;&lt;/blockquote&gt;

&lt;h2&gt;Description&lt;/h2&gt;

&lt;p&gt;Calls to &lt;code&gt;console.log&lt;/code&gt;, &lt;code&gt;console.error&lt;/code&gt;, etc. are synchronous, and as such,
will block the event loop while the data is being written to a file, terminal,
socket, pipe, etc.&lt;/p&gt;

&lt;p&gt;This module provides a seamless, drop-in buffer for all calls to these
functions, and flushes them when the buffers exceed a certain size (8k by
default).&lt;/p&gt;

&lt;p&gt;See &lt;a href=&quot;#known-issues&quot;&gt;Known Issues&lt;/a&gt; for timing concerns with this module.&lt;/p&gt;

&lt;p&gt;View the &lt;a href=&quot;https://github.com/bahamas10/node-log-buffer&quot;&gt;project on GitHub&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Example&lt;/h2&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;log-buffer&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;Hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// buffered&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;world&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// buffered&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// flushed at exit or 8k of data&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Even though there are 2 calls to &lt;code&gt;console.log&lt;/code&gt;, this example only writes to a
file descriptor once.&lt;/p&gt;

&lt;h2&gt;Customization&lt;/h2&gt;

&lt;p&gt;You can specify an alternative buffer size to use for automatic flushing like
this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;log-buffer&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4096&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// buffer will flush at 4k&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This module also exposes the &lt;code&gt;flush&lt;/code&gt; function used to flush all buffers, so
if you would like you can manually invoke a flush.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;logbuffer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;log-buffer&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// buffered&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;world&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// buffered&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;logbuffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;flush&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// flushed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Also, you can specify an interval to automatically flush all buffers so logs
don't get held in memory indefinitely.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;logbuffer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;log-buffer&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;setInterval&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;logbuffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;flush&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// flush every 5 seconds&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;This will flush automatically at 8k of data as well as every 5 seconds.&lt;/p&gt;

&lt;h2&gt;Benchmark&lt;/h2&gt;

&lt;h3&gt;Speed&lt;/h3&gt;

&lt;p&gt;Tested on a Joyent smartmachine in the Joyent Public Cloud
(joyent_20120912T055050Z)&lt;/p&gt;

&lt;p&gt;Counting to a million, logging each iteration, piping to dd, without buffering&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ time node benchmark/count.js | dd &amp;gt; /dev/null
0+982421 records in
13454+1 records out
6888890 bytes (6.9 MB) copied, 19.0066 s, 362 kB/s

real    0m19.111s
user    0m16.409s
sys     0m6.546s
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Counting to a million, logging each iteration, piping to dd, with buffering (8k)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ time node benchmark/bcount.js | dd &amp;gt; /dev/null
13446+841 records in
13454+1 records out
6888890 bytes (6.9 MB) copied, 3.46552 s, 2.0 MB/s

real    0m3.495s
user    0m3.390s
sys     0m0.136s
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A &lt;strong&gt;5.5x&lt;/strong&gt; increase in speed with log buffering&lt;/p&gt;

&lt;h3&gt;syscalls&lt;/h3&gt;

&lt;p&gt;Using DTrace(1M) we can see how many times the system was asked to write&lt;/p&gt;

&lt;p&gt;In the examples below, the output is redirected to &lt;code&gt;/dev/null&lt;/code&gt; so we don't
get a line printed for each iteration of the loop.  DTrace is then told to
output to stderr so its data doesn't get sent to &lt;code&gt;/dev/null&lt;/code&gt; as well.&lt;/p&gt;

&lt;p&gt;Counting to a million, logging each iteration to &lt;code&gt;/dev/null&lt;/code&gt;, without buffering&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ dtrace -n 'syscall::write*:entry /pid == $target/ { @ = count(); }' -c 'node count.js' -o /dev/stderr &amp;gt; /dev/null
dtrace: description 'syscall::write*:entry ' matched 2 probes
dtrace: pid 33117 has exited

          1000000
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Counting to a million, logging each iteration to &lt;code&gt;/dev/null&lt;/code&gt;, with buffering (8k)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ dtrace -n 'syscall::write*:entry /pid == $target/ { @ = count(); }' -c 'node bcount.js' -o /dev/stderr &amp;gt; /dev/null
dtrace: description 'syscall::write*:entry ' matched 2 probes
dtrace: pid 31513 has exited

              841
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;1,000,000 write(2) syscalls are fired without buffering, whereas only 841 are fired
when the output is buffered.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;1,189x&lt;/strong&gt; decrease in the number of syscalls; &lt;strong&gt;1&lt;/strong&gt; buffered syscall for
every &lt;strong&gt;1,189&lt;/strong&gt; unbuffered syscalls.&lt;/p&gt;

&lt;h2&gt;Install&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;npm install log-buffer
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Tests&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;npm test
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Known Issues&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;All buffers are flushed when &lt;code&gt;flush&lt;/code&gt; is called (whether automatically
or manually).  Because of this, calls to different &lt;code&gt;console&lt;/code&gt; family functions
may return out of order.&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;Example:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;log-buffer&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;yields&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;1
3
2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;1&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt; are both written to stdout and &lt;code&gt;2&lt;/code&gt; is written stderr.
The priority order in flushing  is &lt;code&gt;['warn', 'log', 'error', 'info']&lt;/code&gt;&lt;/p&gt;
</summary>
		</item>
		
		<item>
			<title>pcurl - Node Module</title>
			<link>http://www.daveeddy.com/2012/10/26/pcurl--node-module/</link>
			<updated>2012-10-26T00:00:00+00:00</updated>
			<pubDate>2012-10-26T00:00:00+00:00</pubDate>
			<id>http://www.daveeddy.com/2012/10/26/pcurl--node-module/</id>
			<gid>http://www.daveeddy.com/2012/10/26/pcurl--node-module/</gid>
			<author></author>
			<summary type="html">&lt;blockquote&gt;&lt;p&gt;Concurrently curl a list of hosts and print the results&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;http://www.daveeddy.com/static/media/github/pcurl.png&quot; alt=&quot;Pcurl&quot; /&gt;&lt;/p&gt;

&lt;p&gt;View the project page on &lt;a href=&quot;https://github.com/bahamas10/node-pcurl&quot;&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Usage&lt;/h2&gt;

&lt;p&gt;Used as a command line tool&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pcurl url1 url2 url3 ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or pipe through stdin&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pcurl &amp;lt; newline_sep_list_of_urls.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Examples&lt;/h2&gt;

&lt;p&gt;Use bash expansion to hit multiple URLs at the same time, and report the results&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ pcurl http://ifconfig.me/{ip,host,ua,port,lang,connection}
URL                            CODE  BODY
http://ifconfig.me/connection  200   keep-alive
http://ifconfig.me/host        200   host.isp.net
http://ifconfig.me/ip          200   2.6.9.8
http://ifconfig.me/lang        404
http://ifconfig.me/port        200   41699
http://ifconfig.me/ua          200
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or send the same data through stdin&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ printf &quot;%s\n&quot; http://ifconfig.me/{ip,host,ua,port,lang,connection}
http://ifconfig.me/ip
http://ifconfig.me/host
http://ifconfig.me/ua
http://ifconfig.me/port
http://ifconfig.me/lang
http://ifconfig.me/connection
$ printf &quot;%s\n&quot; http://ifconfig.me/{ip,host,ua,port,lang,connection} | pcurl
URL                            CODE  BODY
http://ifconfig.me/connection  200   keep-alive
http://ifconfig.me/host        200   host.isp.net
http://ifconfig.me/ip          200   2.6.9.8
http://ifconfig.me/lang        404
http://ifconfig.me/port        200   41699
http://ifconfig.me/ua          200
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Limitations&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Only supports GET requests&lt;/li&gt;
&lt;li&gt;Meant to receive small body data&lt;/li&gt;
&lt;li&gt;Hardcoded 10 second timeout&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;Install&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;npm install -g pcurl
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;License&lt;/h2&gt;

&lt;p&gt;MIT&lt;/p&gt;
</summary>
		</item>
		
		<item>
			<title>perms - Node Module</title>
			<link>http://www.daveeddy.com/2012/09/25/perms--node-module/</link>
			<updated>2012-09-25T00:00:00+00:00</updated>
			<pubDate>2012-09-25T00:00:00+00:00</pubDate>
			<id>http://www.daveeddy.com/2012/09/25/perms--node-module/</id>
			<gid>http://www.daveeddy.com/2012/09/25/perms--node-module/</gid>
			<author></author>
			<summary type="html">&lt;blockquote&gt;&lt;p&gt;Convert Unix style permissions to strings like ls (0755 =&gt; 'rwxr-xr-x')&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;I wrote this module to make it easy to print out nice looking permissions
in my scripts before calling &lt;code&gt;fs.chmod&lt;/code&gt; for the user.&lt;/p&gt;

&lt;h2&gt;Examples&lt;/h2&gt;

&lt;p&gt;Given&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;perms&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;perms&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;Convert a mode to a human-readable string like ls(1) generates&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;perms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;0755&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;yields&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rwxr-xr-x
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Also handles special permissions&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;perms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;6660&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;yields&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;rwSrwS---
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Go backwards as well!&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;perms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;toMode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;rwxr-xr-t&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;mode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;yields&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;1755
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;NOTE&lt;/em&gt;: This module makes it super tempting to parse ls(1)... don't do that!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://mywiki.wooledge.org/ParsingLs&quot;&gt;http://mywiki.wooledge.org/ParsingLs&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Usage&lt;/h2&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;perms&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;perms&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;h2&gt;Functions&lt;/h2&gt;

&lt;h3&gt;toMode(s)&lt;/h3&gt;

&lt;p&gt;Given a string, return the mode suitable for passing to &lt;code&gt;fs.chmod&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;toString(s)&lt;/h3&gt;

&lt;p&gt;Given a mode, return a string suitable for printing to a user&lt;/p&gt;

&lt;h2&gt;Installation&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;npm install perms
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Tests&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;npm test
&lt;/code&gt;&lt;/pre&gt;
</summary>
		</item>
		
		<item>
			<title>Compiling watch(1) on SmartOS</title>
			<link>http://www.daveeddy.com/2012/09/18/compiling-watch1-on-smartos/</link>
			<updated>2012-09-18T00:00:00+00:00</updated>
			<pubDate>2012-09-18T00:00:00+00:00</pubDate>
			<id>http://www.daveeddy.com/2012/09/18/compiling-watch1-on-smartos/</id>
			<gid>http://www.daveeddy.com/2012/09/18/compiling-watch1-on-smartos/</gid>
			<author></author>
			<summary type="html">&lt;blockquote&gt;&lt;p&gt;watch - execute a program periodically, showing output fullscreen&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;&lt;img src=&quot;/static/media/2012/09/18/watch.png&quot; alt=&quot;Watch&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Coming from Linux, I have become used to the &lt;code&gt;watch&lt;/code&gt; tool.  The program is very simple,
it lets you run a command at a given interval, and update the terminal with the output.
It's like a simple while loop that runs a command, but it uses ncurses to update the view,
which makes it easy to spot differences.&lt;/p&gt;

&lt;p&gt;Compiling it on &lt;a href=&quot;http://smartos.org&quot;&gt;SmartOS&lt;/a&gt; was straight forward, the hardest part
was finding the source of watch(1).  It turns out it is bundled with a handful of tools
for dealing with the Linux proc filesystem.  Because of this, most of the tools wont compile
(or be useful) on an Illumos based operating system, so the Makefile proved to
not be helpful.&lt;/p&gt;

&lt;p&gt;To install watch(1) on SmartOS, you first have to grab the source and extract it&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;wget http://procps.sourceforge.net/procps-3.2.8.tar.gz
tar xf procps-3.2.8.tar.gz
cd procps-3.2.8
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then, you need make sure you have the proper dependencies&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;pkgin in ncurses
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, compile it and install it manually&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gcc -I/opt/local/include/ncurses -L/opt/local/lib -lncurses -m64 watch.c -o watch
cp -f ./watch /opt/local/bin
cp -f ./watch.1 /opt/local/share/man/man1/
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That will ensure the program is installed along with its manpage&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;dave @ [ dave-01 :: (SunOS) ] ~ $ watch --version
0.2.0
&lt;/code&gt;&lt;/pre&gt;
</summary>
		</item>
		
		<item>
			<title>Trace streams in Node.js</title>
			<link>http://www.daveeddy.com/2012/08/29/trace-streams-in-nodejs/</link>
			<updated>2012-08-29T00:00:00+00:00</updated>
			<pubDate>2012-08-29T00:00:00+00:00</pubDate>
			<id>http://www.daveeddy.com/2012/08/29/trace-streams-in-nodejs/</id>
			<gid>http://www.daveeddy.com/2012/08/29/trace-streams-in-nodejs/</gid>
			<author></author>
			<summary type="html">&lt;p&gt;I was working on my latest module for node, &lt;a href=&quot;https://github.com/bahamas10/node-latest&quot;&gt;latest&lt;/a&gt;, when I encountered
a problem.  While testing, I noticed output being written to the terminal
that I wasn't generating.  This means that some module had the audacity to
write directly to the terminal... bad.&lt;/p&gt;

&lt;p&gt;My project was only including the &lt;a href=&quot;http://npmjs.org&quot;&gt;npm&lt;/a&gt; module, however the npm module included
its own handful of other modules, and they included their own modules, and so-on.
If I were to look through the source myself to try and find what was outputting to
the terminal it would have taken forever, trying to trace what function was calling what.
Instead, I decided to override the stdout streams &lt;code&gt;write&lt;/code&gt; function, to
stack trace when invoked.  This means that any function that writes to stdout
will cause a stack trace to be printed to the screen.&lt;/p&gt;

&lt;h2&gt;Example&lt;/h2&gt;

&lt;p&gt;Imagine a script that looks like this&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;./1&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;./2&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;./3&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;done&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;very simple.  It includes 3 modules, and then prints &quot;done&quot;.  Let's look at the output
it generates.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;undefined
done
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What? why does it print &quot;undefined&quot; before it prints &quot;done&quot;?  One of the included modules
is taking it upon itself to write directly to the terminal.  Without knowing where to look
I might make the mistake of trying to hunt down the function myself, tracing code paths in my
head, trying to make sense of someone else's code.  Instead, let's cut it off at the source,
and force every write to dump a stack trace.&lt;/p&gt;

&lt;p&gt;All I have to do is add a bit of code to the top of the example file&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;trace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;./1&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;./2&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;./3&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;done&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;It might look confusing what is happening here, but it's actually very simple.  I first declare a
variable &lt;code&gt;a&lt;/code&gt;, and set it to what &lt;code&gt;process.stdout.write&lt;/code&gt; is set to (a function).  Then, I redefine
&lt;code&gt;process.stdout.write&lt;/code&gt; to be a function that first dumps a trace, and then calls the real/original &lt;code&gt;write&lt;/code&gt;
function.  It might look recursive, but this is possible in javascript thanks to &lt;a href=&quot;http://web.archive.org/web/20080209105120/http://blog.morrisjohns.com/javascript_closures_for_dummies&quot;&gt;closures&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, when I run this I see&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Trace
    at WriteStream.process.stdout.write (/Users/dave/temp/streamsnoop/main.js:3:13)
    at Object.exports.log (console.js:25:18)
    at Object.&amp;lt;anonymous&amp;gt; (/Users/dave/temp/streamsnoop/2.js:1:71)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.&amp;lt;anonymous&amp;gt; (/Users/dave/temp/streamsnoop/main.js:8:1)
undefined
Trace
    at WriteStream.process.stdout.write (/Users/dave/temp/streamsnoop/main.js:3:13)
    at Object.exports.log (console.js:25:18)
    at Object.&amp;lt;anonymous&amp;gt; (/Users/dave/temp/streamsnoop/main.js:11:9)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
done
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are 2 traces, 1 from &quot;undefined&quot; being printed to the screen, and the other where I print
&quot;done&quot;.  The first trace is the most telling, if we follow it backwards we will find the culprit.
The first line of the trace says &lt;code&gt;main.js:3:13&lt;/code&gt;, which is telling us the line of code where the trace
was triggered.  The next line shows &lt;code&gt;console.js&lt;/code&gt;, because we called trace manually. Moving onto the
third line we see &lt;code&gt;2.js:1:71&lt;/code&gt;, which tells us exactly where the problem is.&lt;/p&gt;

&lt;p&gt;The module &lt;code&gt;2.js&lt;/code&gt; is writing to stdout on line 1.  If we look into this file we see&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;and sure enough, that's the problem.  Overriding the &lt;code&gt;process.stdout.write&lt;/code&gt; function has made it
possible to find this issue without having to dig through (potentially) a lot of files manually.&lt;/p&gt;

&lt;p&gt;You can read the abridged version in this &lt;a href=&quot;https://gist.github.com/3503492&quot;&gt;github gist&lt;/a&gt;&lt;/p&gt;
</summary>
		</item>
		
		<item>
			<title>exec - Node Module</title>
			<link>http://www.daveeddy.com/2012/08/26/exec--node-module/</link>
			<updated>2012-08-26T00:00:00+00:00</updated>
			<pubDate>2012-08-26T00:00:00+00:00</pubDate>
			<id>http://www.daveeddy.com/2012/08/26/exec--node-module/</id>
			<gid>http://www.daveeddy.com/2012/08/26/exec--node-module/</gid>
			<author></author>
			<summary type="html">&lt;blockquote&gt;&lt;p&gt;Call a child process with the ease of exec and safety of spawn&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;View the &lt;a href=&quot;https://github.com/bahamas10/node-exec&quot;&gt;Project page on GitHub&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Why?&lt;/h2&gt;

&lt;p&gt;This module provides the best of both worlds of &lt;code&gt;spawn&lt;/code&gt; and &lt;code&gt;exec&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will simply return 2 strings containing stdout and stderr
(like &lt;code&gt;child_process.exec&lt;/code&gt;), but will take an array of process arguments
(like &lt;code&gt;child_process.spawn&lt;/code&gt;) to avoid any nasty shell expansion.&lt;/p&gt;

&lt;h2&gt;Usage&lt;/h2&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;exec&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;h2&gt;Example&lt;/h2&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;exec&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;exec&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;nx&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;ls&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;-lha&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;stdout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;The example above will call &lt;code&gt;ls -lha&lt;/code&gt; safely, by passing the arguments directly
to exec(2) without using an shell expansion/word splitting.&lt;/p&gt;

&lt;p&gt;It returns a &lt;code&gt;child_process.spawn&lt;/code&gt; object, and callbacks with any stdout,
stderr, and the exit status of the command.  The above example will throw an
error if any stderr was produced, otherwise it will print the stdout
and exit with the exit code of &lt;code&gt;ls&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Functions&lt;/h2&gt;

&lt;h3&gt;exec(['args'], [opts], callback)&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;opts&lt;/code&gt; is additional options to pass to &lt;code&gt;child_process.spawn&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;Installation&lt;/h2&gt;

&lt;pre&gt;&lt;code&gt;npm install exec
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;License&lt;/h2&gt;

&lt;p&gt;MIT&lt;/p&gt;
</summary>
		</item>
		
	</channel>
</rss>
