<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Procbits</title>
	<atom:link href="http://procbits.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://procbits.com</link>
	<description>source code snippets and other random musings about software</description>
	<lastBuildDate>Sat, 26 May 2012 19:41:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='procbits.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Procbits</title>
		<link>http://procbits.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://procbits.com/osd.xml" title="Procbits" />
	<atom:link rel='hub' href='http://procbits.com/?pushpress=hub'/>
		<item>
		<title>Thinking Asynchronously in CoffeeScript/JavaScript: Loops and Callbacks</title>
		<link>http://procbits.com/2012/05/24/thinking-asynchronously-in-coffeescriptjavascript-loops-and-callbacks/</link>
		<comments>http://procbits.com/2012/05/24/thinking-asynchronously-in-coffeescriptjavascript-loops-and-callbacks/#comments</comments>
		<pubDate>Thu, 24 May 2012 18:19:20 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://procbits.com/?p=321</guid>
		<description><![CDATA[Awhile back, I wrote about my new experience in learning Node.js: A Node.js Experiment: Thinking Asynchronously, Using Recursion to Calculate the Total File Size in a Directory. Consider this snippet of code: Equivalent CoffeeScript: Click here to run it. If you guessed that the loop would alert &#8220;Leslie&#8221; three times, then you&#8217;d be correct. The [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=321&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Awhile back, I wrote about my new experience in learning Node.js: <a href="http://procbits.com/2011/10/29/a-node-js-experiment-thinking-asynchronously-recursion-calculate-file-size-directory/">A Node.js Experiment: Thinking Asynchronously, Using Recursion to Calculate the Total File Size in a Directory</a>.</p>
<p>Consider this snippet of code:<br />
<pre class="brush: jscript;">
var names = ['JP', 'Chris', 'Leslie'];
for (var i = 0; i &lt; names.length; ++i){
  var name = names[i];
  setTimeout(function(){
    alert(name);              
  },10);
}​
</pre></p>
<p>Equivalent CoffeeScript:<br />
<pre class="brush: ruby;">
names = ['JP', 'Chris', 'Leslie']
for name in names
  setTimeout(-&gt;
    alert(name)
  ,10)
</pre><br />
Click <a href="http://coffeescript.org/#try:names%20%3D%20%5B'JP'%2C%20'Chris'%2C%20'Leslie'%5D%0Afor%20name%20in%20names%0A%20%20setTimeout(-%3E%0A%20%20%20%20alert(name)%0A%20%20%2C10)">here</a> to run it.</p>
<p>If you guessed that the loop would alert &#8220;Leslie&#8221; three times, then you&#8217;d be correct.</p>
<p>The problem is, that before the callback executes, the loop has completed. Thus callback always has the last value.</p>
<p>How do you solve this problem? You wrap the callback in a closure that executes immediately.</p>
<p>JavaScript:<br />
<pre class="brush: jscript;">
var names = ['JP', 'Chris', 'Leslie'];
for (var i = 0; i &lt; names.length; ++i){
  var name = names[i];
  (function(name){
    setTimeout(function(){
      alert(name);              
    },10);
  })(name);
}​
</pre></p>
<p>CoffeeScript:<br />
<pre class="brush: ruby;">
names = ['JP', 'Chris', 'Leslie']
for name in names
  do (name) -&gt;
    setTimeout(-&gt;
      alert(name)
    ,10)
</pre><br />
Click <a href="http://coffeescript.org/#try:names%20%3D%20%5B'JP'%2C%20'Chris'%2C%20'Leslie'%5D%0Afor%20name%20in%20names%0A%20%20do%20(name)%20-%3E%0A%20%20%20%20setTimeout(-%3E%0A%20%20%20%20%20%20alert(name)%0A%20%20%20%20%2C10)">here</a> to run it.</p>
<p>These solutions execute the block of code in a parallel manner. Using the alert&#8217;s are not a good indication in showing this behavior. However, if you were opening files, all of them would be opened approximately (not exactly) at the same time.</p>
<p>What if you wanted to perform the action in the callback in a serial manner?</p>
<p>Using the previous simple example, it&#8217;d look like this:</p>
<p>JavaScript:<br />
<pre class="brush: jscript;">
var names = ['JP', 'Chris', 'Leslie'];
loop = function(i){
    setTimeout(function(){
      alert(names[i]);
      if (i &lt; names.length - 1)
        loop(i + 1);       
    },10);
}
loop(0);
</pre> </p>
<p>CoffeeScript:<br />
<pre class="brush: ruby;">
names = ['JP', 'Chris', 'Leslie'];
doloop = (i) -&gt;
  setTimeout(-&gt;
    alert(names[i])
    if i &lt; names.length - 1
      doloop(i + 1)       
  ,10);
doloop(0)
</pre><br />
<a href="http://coffeescript.org/#try:names%20%3D%20%5B'JP'%2C%20'Chris'%2C%20'Leslie'%5D%3B%0Adoloop%20%3D%20(i)%20-%3E%0A%20%20setTimeout(-%3E%0A%20%20%20%20alert(names%5Bi%5D)%0A%20%20%20%20if%20i%20%3C%20names.length%20-%201%0A%20%20%20%20%20%20doloop(i%20%2B%201)%20%20%20%20%20%20%20%0A%20%20%2C10)%3B%0Adoloop(0)">Run it.</a></p>
<p>If you were doing file processing in the loop, it would be executed sequentially.</p>
<p>Hopefully this helps you to better understand asynchronous design of algorithms in JavaScript.</p>
<p><b>Update:</b><br />
I forgot about the forEach function that exists in Node.js and most modern browsers. This function pretty much solves the problem. </p>
<p>Here&#8217;s the JavaScript code:<br />
<pre class="brush: jscript;">
var names = ['JP', 'Chris', 'Leslie'];
names.forEach(function(name){
  setTimeout(function(){
    alert(name);              
  },10);
}​);
</pre></p>
<p>Much cleaner. Thanks to <a href="http://www.reddit.com/r/programming/comments/u34ed/thinking_asynchronously_in_coffeescriptjavascript/c4ryifw">smog_alado [Reddit]</a> for the reminder.</p>
<p>Checkout <a href="http://gitpilot.com">Gitpilot</a>, a different kind of Git GUI.</p>
<p>Follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a></p>
<p>-JP</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/321/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=321&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2012/05/24/thinking-asynchronously-in-coffeescriptjavascript-loops-and-callbacks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
		<item>
		<title>Why Do All the Great Node.js Developers Hate CoffeeScript?</title>
		<link>http://procbits.com/2012/05/18/why-do-all-the-great-node-js-developers-hate-coffeescript/</link>
		<comments>http://procbits.com/2012/05/18/why-do-all-the-great-node-js-developers-hate-coffeescript/#comments</comments>
		<pubDate>Fri, 18 May 2012 20:40:11 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://procbits.com/?p=312</guid>
		<description><![CDATA[Why do all the great Node.js developers hate CoffeeScript? Take a look at the following Github repositories of the well-known Node.js developers: Isaac Schleuter (maintainer of Node.js / NPM) TJ Holowaychuk (express, Jade, Mocha) Mikeal Rogers (Request) James Haliday &#8220;substack&#8221; (Browserify, dnode, Optimist) Guillermo Rauch (Socket.IO) Aaron Heckmann (Mongoose) Nathan Rajich &#8220;Too Tall Nate&#8221; (node-gyp) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=312&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Why do all the great Node.js developers hate CoffeeScript?</p>
<p>Take a look at the following Github repositories of the well-known Node.js developers:</p>
<ul>
<li><a href="https://github.com/isaacs">Isaac Schleuter</a> (maintainer of Node.js / NPM)</li>
<li><a href="https://github.com/visionmedia">TJ Holowaychuk</a> (express, Jade, Mocha)</li>
<li><a href="https://github.com/mikeal">Mikeal Rogers</a> (Request)</li>
<li><a href="https://github.com/substack">James Haliday &#8220;substack&#8221;</a> (Browserify, dnode, Optimist)</li>
<li><a href="https://github.com/guille">Guillermo Rauch</a> (Socket.IO)</li>
<li><a href="https://github.com/aheckmann">Aaron Heckmann</a> (Mongoose)</li>
<li><a href="https://github.com/TooTallNate">Nathan Rajich &#8220;Too Tall Nate&#8221;</a> (node-gyp)</li>
<li><a href="https://github.com/marak">Marak Squires</a> (Hook.io, pdf.js, color.js)</li>
<li><a href="https://github.com/felixge">Felix Geisendörfer </a></li>
<li><a href="https://github.com/creationix">Tim Caswell</a> (NVM)</li>
</ul>
<p>Did you look at them? Not one of them has a project (that isn&#8217;t forked) that is written in CoffeeScript. So does the absence of CoffeeScript on Github imply these developers hate it? Absolutely not. Listen to episode <a href="http://nodeup.com/eighteen">18</a> or <a href="http://nodeup.com/nineteen">19</a> of Nodeup (don&#8217;t remember which one) but there are a couple of instances where they (expert Node.js devs) joke and laugh about writing in CoffeeScript. If this offensive? Of course not. But the attitude is curious to me.</p>
<p>One of the aforementioned developers said the following about a technology:</p>
<blockquote><p>What if we could omit braces? How about semi-colons?</p></blockquote>
<p>Sounds like the developer is talking about CoffeeScript, doesn&#8217;t it? No, it was TJ Holowaychuk describing <a href="http://learnboost.github.com/stylus/">Stylus</a>, his CSS replacement language. Look at Stylus, look how CoffeeScript-esque it is. This is the the same <a href="https://github.com/visionmedia/jade/issues/430">TJ that doesn&#8217;t like CoffeeScript</a>. This is meant to be partially tongue &amp; cheek, but it does lend credance to my point.</p>
<p>Can you guess what the second most <a href="http://search.npmjs.org/">depended-upon package is on NPM</a>? If you guessed CoffeeScript, you&#8217;d be right!</p>
<p><a href="http://procbits.files.wordpress.com/2012/05/coffeescript-dep2.png"><img class="alignnone size-medium wp-image-316" title="NPM / CoffeeScript" src="http://procbits.files.wordpress.com/2012/05/coffeescript-dep2.png?w=300&h=232" alt="" width="300" height="232" /></a></p>
<p>So if it&#8217;s the second most depended-upon package, it must be in use by us mere-mortal developers. Having defected from Rails, I love CoffeeScript. But, I ask again, why do the greats have a haughty attitude towards CoffeeScript? This isn&#8217;t meant to be a crusade trying to get people to convert to the holier-than-though CoffeeScript, but a genuine lack of understanding of why the disdain exists. Especially given the acceptance towards Haml, SASS, SCSS, Jade, etc. I mean, when it comes down to it, write in whatever makes you happy, but I feel like I&#8217;m missing something. If you&#8217;re part of the Node.js community, you&#8217;ll know what I&#8217;m talking about.</p>
<p>Looking over the <a href="http://coffeescript.org/">CoffeeScrip</a>t page, I think that you can safely conclude that in general, you&#8217;ll write less lines of code using CoffeeScript. <a href="http://www.codinghorror.com/blog/2007/12/size-is-the-enemy.html">Code is our enemy</a> so that&#8217;s a good thing.</p>
<p>What do you think about CoffeeScript? Why do you think these developers don&#8217;t like CoffeeScript?</p>
<p>More fun CoffeeScript hatred:</p>
<ul>
<li><a href="http://ryanflorence.com/2011/case-against-coffeescript/">A Case Against Using CoffeeScript</a></li>
<li><a href="http://net.tutsplus.com/articles/interviews/should-you-learn-coffeescript/">Should You Learn CoffeeScript?</a></li>
</ul>
<p>If you use Git with others, you should checkout <a href="http://gitpilot.com">Gitpilot</a> to make collaboration with Git simple. We would love your advice.</p>
<p>If you made it this far, follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a></p>
<p>-JP</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/312/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/312/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/312/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=312&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2012/05/18/why-do-all-the-great-node-js-developers-hate-coffeescript/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>

		<media:content url="http://procbits.files.wordpress.com/2012/05/coffeescript-dep2.png?w=300" medium="image">
			<media:title type="html">NPM / CoffeeScript</media:title>
		</media:content>
	</item>
		<item>
		<title>Quick and Dirty Screen Scraping with Node.js using Request and Cheerio</title>
		<link>http://procbits.com/2012/04/11/quick-and-dirty-screen-scraping-with-node-js-using-request-and-cheerio/</link>
		<comments>http://procbits.com/2012/04/11/quick-and-dirty-screen-scraping-with-node-js-using-request-and-cheerio/#comments</comments>
		<pubDate>Wed, 11 Apr 2012 22:24:09 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://procbits.com/?p=309</guid>
		<description><![CDATA[I wrote my own screen scraping module built on PhantomJS, but unfortunately it&#8217;s too slow for most screen scraping tasks that don&#8217;t require browser-side JavaScript. One easy way to scrape pages with Node.js is to use Request and Cheerio. Here is an example of scraping Bing to get all of the search results: Cheerio acts [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=309&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I wrote my own screen scraping module built on <a href="http://www.phantomjs.org/">PhantomJS</a>, but unfortunately it&#8217;s too slow for most screen scraping tasks that don&#8217;t require browser-side JavaScript. One easy way to scrape pages with Node.js is to use <a href="https://github.com/mikeal/request">Request</a> and <a href="https://github.com/MatthewMueller/cheerio">Cheerio</a>.</p>
<p>Here is an example of scraping Bing to get all of the search results:</p>
<p><pre class="brush: jscript;">
var request = require('request');
var cheerio = require('cheerio');

var searchTerm = 'screen+scraping';
var url = 'http://www.bing.com/search?q=' + searchTerm;

request(url, function(err, resp, body){
  $ = cheerio.load(body);
  links = $('.sb_tlst h3 a'); //use your CSS selector here
  $(links).each(function(i, link){
    console.log($(link).text() + ':\n  ' + $(link).attr('href'));
  });
});
</pre></p>
<p>Cheerio acts a jQuery replacement for a lot of jQuery tasks. It doesn&#8217;t replicate jQuery in every way, and most importantly it&#8217;s not meant for the browser but for the server. But it beats the pants off of the <a href="https://github.com/tmpvar/jsdom">jsdom</a>/jQuery combo for screen scraping.</p>
<p>Do you use Git? If so, checkout <a href="http://gitpilot.com">Gitpilot</a> to make collaborating on software development easy.</p>
<p>You should follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a>.</p>
<p>-JP</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/309/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=309&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2012/04/11/quick-and-dirty-screen-scraping-with-node-js-using-request-and-cheerio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
		<item>
		<title>Submitting/Posting Files and Fields to an HTTP Form using C#/.NET</title>
		<link>http://procbits.com/2012/02/29/submittingposting-files-and-fields-to-an-http-form-using-c-net/</link>
		<comments>http://procbits.com/2012/02/29/submittingposting-files-and-fields-to-an-http-form-using-c-net/#comments</comments>
		<pubDate>Thu, 01 Mar 2012 03:42:27 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://procbits.com/?p=305</guid>
		<description><![CDATA[Awhile back, I had to integrate a C# program with a web system that allowed the user to upload a few files and include some misc. data. I Googled around and didn&#039;t find a comprehensive solution. I did use some code I found on the internet, unfortunately I don&#039;t remember where, so I can&#039;t give [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=305&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Awhile back, I had to integrate a C# program with a web system that allowed the user to upload a few files and include some misc. data. I Googled around and didn&#039;t find a comprehensive solution.</p>
<p>I did use some code I found on the internet, unfortunately I don&#039;t remember where, so I can&#039;t give proper attribution. If you know, please let me know; it&#039;s the code relevant to the <code>MimePart</code> class. I added the form values code and packaged it up into the <code>HttpForm</code> sugar.</p>
<p>Here is the code:</p>
<p><pre class="brush: csharp;">
public class HttpForm {

    private Dictionary&lt;string, string&gt; _files = new Dictionary&lt;string, string&gt;();
    private Dictionary&lt;string, string&gt; _values = new Dictionary&lt;string, string&gt;();

    public HttpForm(string url) {
        this.Url = url;
        this.Method = &quot;POST&quot;;
    }

    public string Method { get; set; }
    public string Url { get; set; }

    //return self so that we can chain
    public HttpForm AttachFile(string field, string fileName) {
        _files[field] = fileName;
        return this;
    }

    public HttpForm ResetForm(){
        _files.Clear();
        _values.Clear();
        return this;
    }

    //return self so that we can chain
    public HttpForm SetValue(string field, string value) {
        _values[field] = value;
        return this;
    }

    public HttpWebResponse Submit() {
        return this.UploadFiles(_files, _values);
    }


    private HttpWebResponse UploadFiles(Dictionary&lt;string, string&gt; files, Dictionary&lt;string, string&gt; otherValues) {
        var req = (HttpWebRequest)WebRequest.Create(this.Url);

        req.Timeout = 10000 * 1000;
        req.Accept = &quot;text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&quot;;
        req.AllowAutoRedirect = false;

        var mimeParts = new List&lt;MimePart&gt;();
        try {
            if (otherValues != null) {
                foreach (var fieldName in otherValues.Keys) {
                    var part = new MimePart();

                    part.Headers[&quot;Content-Disposition&quot;] = &quot;form-data; name=\&quot;&quot; + fieldName + &quot;\&quot;&quot;;
                    part.Data = new MemoryStream(Encoding.UTF8.GetBytes(otherValues[fieldName]));

                    mimeParts.Add(part);
                }
            }

            if (files != null) {
                foreach (var fieldName in files.Keys) {
                    var part = new MimePart();

                    part.Headers[&quot;Content-Disposition&quot;] = &quot;form-data; name=\&quot;&quot; + fieldName + &quot;\&quot;; filename=\&quot;&quot; + files[fieldName] + &quot;\&quot;&quot;;
                    part.Headers[&quot;Content-Type&quot;] = &quot;application/octet-stream&quot;;
                    part.Data = File.OpenRead(files[fieldName]);

                    mimeParts.Add(part);
                }
            }

            string boundary = &quot;----------&quot; + DateTime.Now.Ticks.ToString(&quot;x&quot;);

            req.ContentType = &quot;multipart/form-data; boundary=&quot; + boundary;
            req.Method = this.Method;

            long contentLength = 0;

            byte[] _footer = Encoding.UTF8.GetBytes(&quot;--&quot; + boundary + &quot;--\r\n&quot;);

            foreach (MimePart part in mimeParts) {
                contentLength += part.GenerateHeaderFooterData(boundary);
            }

            req.ContentLength = contentLength + _footer.Length;

            byte[] buffer = new byte[8192];
            byte[] afterFile = Encoding.UTF8.GetBytes(&quot;\r\n&quot;);
            int read;

            using (Stream s = req.GetRequestStream()) {
                foreach (MimePart part in mimeParts) {
                    s.Write(part.Header, 0, part.Header.Length);

                    while ((read = part.Data.Read(buffer, 0, buffer.Length)) &gt; 0)
                        s.Write(buffer, 0, read);

                    part.Data.Dispose();

                    s.Write(afterFile, 0, afterFile.Length);
                }

                s.Write(_footer, 0, _footer.Length);
            }

            var res = (HttpWebResponse)req.GetResponse();

            return res;
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
            foreach (MimePart part in mimeParts)
                if (part.Data != null)
                    part.Data.Dispose();

            return (HttpWebResponse)req.GetResponse();
        }
    }

    private class MimePart {
        private NameValueCollection _headers = new NameValueCollection();
        public NameValueCollection Headers { get { return _headers; } }

        public byte[] Header { get; protected set; }

        public long GenerateHeaderFooterData(string boundary) {
            StringBuilder sb = new StringBuilder();

            sb.Append(&quot;--&quot;);
            sb.Append(boundary);
            sb.AppendLine();
            foreach (string key in _headers.AllKeys) {
                sb.Append(key);
                sb.Append(&quot;: &quot;);
                sb.AppendLine(_headers[key]);
            }
            sb.AppendLine();

            Header = Encoding.UTF8.GetBytes(sb.ToString());

            return Header.Length + Data.Length + 2;
        }

        public Stream Data { get; set; }
    }
}
</pre></p>
<p>You can easily use it like so:</p>
<p><pre class="brush: csharp;">
var file1 = @&quot;C:\file&quot;;
var file2 = @&quot;C:\file2&quot;;

var yourUrl = &quot;http://yourdomain.com/process.php&quot;;
var httpForm = new HttpForm(yourUrl);
httpForm.AttachFile(&quot;file1&quot;, file1).AttachFile(&quot;file2&quot;, file2);
httpForm.setValue(&quot;foo&quot;, &quot;some foo&quot;).setValue(&quot;blah&quot;, &quot;rarrr!&quot;);
httpForm.Submit();
</pre></p>
<p>Do you use Git? If so, checkout <a href="http://gitpilot.com">Gitpilot</a> to make using Git thoughtless.</p>
<p>Follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a>.</p>
<p>-JP Richardson</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/305/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/305/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/305/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=305&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2012/02/29/submittingposting-files-and-fields-to-an-http-form-using-c-net/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
		<item>
		<title>Installing Node.js on Ubuntu 10.04 LTS</title>
		<link>http://procbits.com/2012/01/29/installing-node-js-on-ubuntu-10-4-lts/</link>
		<comments>http://procbits.com/2012/01/29/installing-node-js-on-ubuntu-10-4-lts/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 04:29:57 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://procbits.wordpress.com/?p=301</guid>
		<description><![CDATA[Installing Node.js on Ubuntu 10.04 LTS is pretty straight forward. You will want a Node.js versioning manager. Node.js has a quick release cycle, point releases happen quite frequently. A Node.js versioning manager will help you keep all of your versions isolated from each other. As it stands today, there are four Node.js version managers. They [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=301&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Installing Node.js on Ubuntu 10.04 LTS is pretty straight forward.</p>
<p>You will want a Node.js versioning manager. Node.js has a quick release cycle, point releases happen quite frequently. A Node.js versioning manager will help you keep all of your versions isolated from each other.</p>
<p>As it stands today, there are four Node.js version managers. They are:</p>
<ol>
<li><a href="https://github.com/creationix/nvm">NVM</a> &#8211; NVM works like RVM. It must be sourced in your ~./bashrc or ~./profile file. Some people don&#8217;t like this. It&#8217;s my understanding that some find this to be a bit of hackery.</li>
<li><a href="https://github.com/isaacs/nave">Nave</a> &#8211; Nave doesn&#8217;t need to be sourced or loaded up into your bash profile. But, when you use Nave it executes commands into a <a href="http://docstore.mik.ua/orelly/unix/upt/ch38_04.htm">subshell</a>. It&#8217;s my understanding that if any process in a subshell modifies the environment then these changes won&#8217;t persist to the parent process. It&#8217;s not entirely clear these changes persist or not. But the rhetoric from some regarding using subshells for version management was enough to drive me away.</li>
<li><a href="https://github.com/visionmedia/n">n</a> &#8211; I love the simplicity of &#8216;n&#8217;. It doesn&#8217;t use subshells and it doesn&#8217;t require that you modify your bash profile. I would use &#8216;n&#8217; if it installed NPM (Node.js package manager) with each release, and <a href="https://github.com/visionmedia/n/issues/47">it doesn&#8217;t</a>.</li>
<li><a href="https://github.com/ekalinin/nodeenv">nodeenv </a>- I never seriously considered this one as it requires Python to be installed. I haven&#8217;t read about anyone using this. But I wanted to list it so that you&#8217;d be informed about its existence.</li>
</ol>
<p>Use NVM. Seriously, it just works.</p>
<p>On your clean Ubuntu machine, make sure that Git is installed:</p>
<p><pre class="brush: bash;">
sudo apt-get install git-core
</pre></p>
<p>Then install NVM:</p>
<p><pre class="brush: bash;">
git clone git://github.com/creationix/nvm.git ~/.nvm
. ~/.nvm/nvm.sh # &lt;------ be sure to add this line to the end of your ~./profile or ~./bashrc file
</pre></p>
<p>Now install all of the packages need to build Node.js:<br />
<pre class="brush: bash;">
sudo apt-get install build-essential openssl libssl-dev pkg-config
</pre></p>
<p>Now install the latest version of Node.js, at the time of this writing it&#8217;s v0.6.9<br />
<pre class="brush: bash;">
nvm install v0.6.9
</pre></p>
<p>You now have a Node.js environment on your machine! Just run <code>node</code> on the command line to experiment with the Node.js REPL. You can also run <code>npm</code> to install Node.js packages. Read more about <a href="http://npmjs.org/">NPM here</a>.</p>
<p>Do you use Git? If so, checkout <a href="http://gitpilot.com">Gitpilot</a> to make using Git mindless.</p>
<p>Follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a> and read my blog on entrepreneurship: <a href="http://techneur.com">Techneur</a>.</p>
<p>-JP Richardson</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/301/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/301/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/301/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=301&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2012/01/29/installing-node-js-on-ubuntu-10-4-lts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
		<item>
		<title>Comparing Two Javascript Objects</title>
		<link>http://procbits.com/2012/01/19/comparing-two-javascript-objects/</link>
		<comments>http://procbits.com/2012/01/19/comparing-two-javascript-objects/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 19:04:01 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://procbits.com/?p=295</guid>
		<description><![CDATA[Recently, I was faced with a problem where I needed to compare two Javascript objects. My initial strategy was to convert them to JSON and compare the JSON strings. Sort of like this: Simple enough, right? Not so fast. I encountered a case like this: The data is the same, but the string is different. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=295&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently, I was faced with a problem where I needed to compare two Javascript objects. My initial strategy was to convert them to JSON and compare the JSON strings.</p>
<p>Sort of like this:<br />
<pre class="brush: jscript;">
var a = JSON.stringify(person1);//'{&quot;firstName&quot;:&quot;JP&quot;,&quot;lastName&quot;:&quot;Richardson&quot;}'
var b = JSON.stringify(person2);//'{&quot;firstName&quot;:&quot;JP&quot;,&quot;lastName&quot;:&quot;Richardson&quot;}'

assert(a === b);
</pre></p>
<p>Simple enough, right?</p>
<p>Not so fast. I encountered a case like this:<br />
<pre class="brush: jscript;">
var a = JSON.stringify(person1);//'{&quot;firstName&quot;:&quot;JP&quot;,&quot;lastName&quot;:&quot;Richardson&quot;}'
var b = JSON.stringify(person2);//'{&quot;lastName&quot;:&quot;Richardson&quot;,&quot;firstName&quot;:&quot;JP&quot;}'

assert(a === b);
</pre></p>
<p>The data is the same, but the string is different. Fortunately, Stackoverflow had a nice <a href="http://stackoverflow.com/questions/1068834/object-comparison-in-javascript">Javascript object comparison algorithm</a> to dump into my app.</p>
<p><pre class="brush: jscript;">
Object.prototype.equals = function(x)
{
  var p;
  for(p in this) {
      if(typeof(x[p])=='undefined') {return false;}
  }

  for(p in this) {
      if (this[p]) {
          switch(typeof(this[p])) {
              case 'object':
                  if (!this[p].equals(x[p])) { return false; } break;
              case 'function':
                  if (typeof(x[p])=='undefined' ||
                      (p != 'equals' &amp;&amp; this[p].toString() != x[p].toString()))
                      return false;
                  break;
              default:
                  if (this[p] != x[p]) { return false; }
          }
      } else {
          if (x[p])
              return false;
      }
  }

  for(p in x) {
      if(typeof(this[p])=='undefined') {return false;}
  }

  return true;
}
</pre></p>
<p>Test passed. I eventually hit a situation where I had some code with an Object that had a Person prototype and some data that came from JSON. Kinda like this:</p>
<p><pre class="brush: jscript;">
var person1 = new Person('JP', 'Richardson');
var person2 = JSON.parse('{&quot;firstName&quot;:&quot;JP&quot;,&quot;lastName&quot;:&quot;Richardson&quot;}');

//deepEquals is code snippet above ^
person1.deepEquals(person2); // &lt;--- THIS FAILS
</pre></p>
<p>I only cared about comparing the data. The methods associated with the object (Prototype) didn&#8217;t matter. Let&#8217;s modify the above algorithm. I use CoffeeScript. Here&#8217;s the modification:</p>
<p><pre class="brush: ruby;">
Object::jsonEquals = (x) -&gt;
  #we do this because two objects may have the same data fields and data but different prototypes
  x1 = JSON.parse(JSON.stringify(this))
  x2 = JSON.parse(JSON.stringify(x))

  p = null
  for p of x1
    return false if typeof (x2[p]) is 'undefined'
  for p of x1
    if x1[p]
      switch typeof (x1[p])
        when 'object'
          return false unless x1[p].jsonEquals(x2[p])
        when 'function'
          return false if typeof (x2[p]) is 'undefined' or (p isnt 'equals' and x1[p].toString() isnt x2[p].toString())
        else
          return false  unless x1[p] is x2[p]
    else
      return false if x2[p]
  for p of x2
    return false if typeof (x1[p]) is 'undefined'
  true
</pre></p>
<p>This causes the situation like I described above to pass. Essentially convert to JSON to remove the prototype. I suppose you could make this more efficient my just manually setting the prototype to Object before doing the comparison, but oh well this works for the time being.</p>
<p>Do you use Git? If so, checkout <a href="http://gitpilot.com">Gitpilot</a> to make project management and collaborating on projects seamless.</p>
<p>Follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a> and read my blog on entrepreneurship: <a href="http://techneur.com">Techneur</a>.</p>
<p>-JP Richardson</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/295/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/295/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=295&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2012/01/19/comparing-two-javascript-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
		<item>
		<title>Node.js Exec Like Ruby Exec and Writing a Node.js Native Add On Module</title>
		<link>http://procbits.com/2011/12/04/node-js-exec-like-ruby-exec-and-writing-a-node-js-native-add-on-module/</link>
		<comments>http://procbits.com/2011/12/04/node-js-exec-like-ruby-exec-and-writing-a-node-js-native-add-on-module/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 02:14:55 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://procbits.wordpress.com/?p=286</guid>
		<description><![CDATA[Recently, I was faced with a problem that required my Node.js programs process to execute another process and have the procoess that&#8217;s passed to the exec function completely replace the Node.js process. In short, I wanted an &#8216;exec&#8217; function like Ruby&#8217;s &#8216;exec&#8217; function. Unfortunately, out of the box, Node.js doesn&#8217;t support this functionality. I asked [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=286&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently, I was faced with a problem that required my Node.js programs process to execute another process and have the procoess that&#8217;s passed to the exec function completely replace the Node.js process. In short, I wanted an &#8216;exec&#8217; function like Ruby&#8217;s &#8216;exec&#8217; function. Unfortunately, out of the box, Node.js doesn&#8217;t support this functionality. I asked on <a href="http://stackoverflow.com/questions/8362181/like-ruby-exec-but-for-node-js">Stackoverflow.com, and someone had a response</a> that I should use the <a href="http://linux.die.net/man/3/exec">POSIX exec functions</a> to solve my problem and to consider writing a native Node.js extension.</p>
<p><code><br />
npm install kexec<br />
</code></p>
<p>You can then use it like:</p>
<p><pre class="brush: jscript;">
var kexec = require('kexec');
kexec('top'); //you can pass any process that you want here
</pre></p>
<p>Here is the C++ source for Node Kexec:</p>
<p><pre class="brush: cpp;">

#include &lt;v8.h&gt;
#include &lt;node.h&gt;
#include &lt;cstdio&gt;

//#ifdef __POSIX__
#include &lt;unistd.h&gt;
/*#else
#include &lt;process.h&gt;
#endif*/

using namespace node;
using namespace v8;

static Handle&lt;Value&gt; kexec(const Arguments&amp; args) {
    String::Utf8Value v8str(args[0]);
    char* argv2[] = {&quot;&quot;, &quot;-c&quot;, *v8str, NULL};

    execvp(&quot;/bin/sh&quot;, argv2);      
    return Undefined();
}

extern &quot;C&quot; {
    static void init (Handle&lt;Object&gt; target) {
        NODE_SET_METHOD(target, &quot;kexec&quot;, kexec);
    }

    NODE_MODULE(kexec, init);
}
</pre></p>
<p>As you can see, writing a C++ add on in Node.js isn&#8217;t too difficult. You can use it in your Node.js Javascript like so:</p>
<p><pre class="brush: jscript;">
var kexec;

try {
  kexec = require(&quot;./build/default/kexec.node&quot;); //Node.js v0.4
} catch(e) {
  kexec = require(&quot;./build/Release/kexec.node&quot;); //Node.js v0.6
}

module.exports = kexec.kexec; //function of kexec module is named kexec
</pre></p>
<p>Don&#8217;t forget your wscript file, which ironically is Python code:</p>
<p><pre class="brush: python;">
def set_options(opt):
  opt.tool_options(&quot;compiler_cxx&quot;)

def configure(conf):
  conf.check_tool(&quot;compiler_cxx&quot;)
  conf.check_tool(&quot;node_addon&quot;)

def build(bld):
  obj = bld.new_task_gen(&quot;cxx&quot;, &quot;shlib&quot;, &quot;node_addon&quot;) 
  obj.cxxflags = [&quot;-g&quot;, &quot;-D_FILE_OFFSET_BITS=64&quot;, &quot;-D_LARGEFILE_SOURCE&quot;,&quot;-Wall&quot;]
  obj.target = &quot;kexec&quot;
  obj.source = &quot;src/node_kexec.cpp&quot;
</pre></p>
<p>In your package.json, include this bit:<br />
<pre class="brush: jscript;">
&quot;scripts&quot;: { &quot;install&quot;: &quot;node-waf configure build&quot; }
</pre></p>
<p>Github Sourcecode: <a href="https://github.com/jprichardson/node-kexec">Node.js kernel exec</a></p>
<p>I&#8217;ve also included other resources for writing a Node.js Native Add On Module:</p>
<ol>
<li><a href="http://code.google.com/apis/v8/get_started.html">Google V8 Engine Getting Started</a></li>
<li><a href="http://code.google.com/apis/v8/embed.html">Google V8 Embedder&#8217;s Guide</a></li>
<li><a href="http://syskall.com/how-to-roll-out-your-own-javascript-api-with">How to Roll Your Own Javascript API with V8</a></li>
<li><a href="http://syskall.com/how-to-write-your-own-native-nodejs-extension">How to Write Your Own Native Node.js Extension</a></li>
<li><a href="https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/">Writing Node.js Native Extensions</a></li>
<li><a href="http://odoe.net/blog/?p=168">Node.js Native Extension with Hammer and a Prayer</a></li>
<li><a href="http://www.ipreferjim.com/2011/04/node-js-mastering-node-excerpt-addons/">Mastering Node; Add Ons</a></li>
<li><a href="http://nodejs.org/docs/v0.6.4/api/addons.html">Node.js Documentation: Add Ons</a></li>
<li><a href="https://github.com/ry/node_postgres">Postgres Node.js Module</a></li>
<li><a href="http://v8.googlecode.com/svn/trunk/samples/shell.cc">V8 Sample: shell.cc</a></li>
<li><a href="http://create.tpsitulsa.com/blog/2009/01/29/v8-objects/">V8 Objects</a></li>
<li><a href="http://nikhilm.bitbucket.org/articles/c_in_my_javascript/c_in_javascript_part_2.html">There&#8217;s C in My JavaScript</a></li>
<li><a href="http://stackoverflow.com/questions/7476145/converting-from-v8arguments-to-c-types">Converting V8 Arguments to C++ Types</a></li>
</ol>
<p>Do you use Git? If so, checkout <a href="http://gitpilot.com">Gitpilot</a> to make using Git easy.</p>
<p>Follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a> and read my blog on entrepreneurship: <a href="http://techneur.com">Techneur</a>.</p>
<p>-JP Richardson</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/286/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/286/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/286/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=286&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2011/12/04/node-js-exec-like-ruby-exec-and-writing-a-node-js-native-add-on-module/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
		<item>
		<title>Using OCMock with Mac OS X Lion, Xcode 4, to Mock and Unit Test Cocoa Desktop Apps</title>
		<link>http://procbits.com/2011/11/29/using-ocmock-with-mac-os-x-lion-xcode-4-to-mock-and-unit-test-cocoa-desktop-apps/</link>
		<comments>http://procbits.com/2011/11/29/using-ocmock-with-mac-os-x-lion-xcode-4-to-mock-and-unit-test-cocoa-desktop-apps/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 17:03:07 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Objective C]]></category>

		<guid isPermaLink="false">http://procbits.wordpress.com/?p=277</guid>
		<description><![CDATA[If you&#8217;re trying to learn how to use OCMock, you&#8217;ll encounter a number of articles dedicated to using it with iOS. You won&#8217;t find very many related to writing tests, mocks, and stubs for your Cocoa desktop applications for OS X Lion. If you&#8217;re writing your apps to exclusively target OS X Lion (10.7), then [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=277&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re trying to learn how to use <a href="http://ocmock.org/">OCMock</a>, you&#8217;ll encounter a number of articles dedicated to using it with iOS. You won&#8217;t find very many related to writing tests, mocks, and stubs for your Cocoa desktop applications for OS X Lion. If you&#8217;re writing your apps to exclusively target OS X Lion (10.7), then this article will be of use to you. I&#8217;m not sure if this technique will work for Snow Leopard apps or not. But, since OS X Lion 10.7 is a 64 bit OS the distributable library in the <a href="http://ocmock.org/downloads/ocmock-1.77.dmg">downloadable package (1.77)</a> will not work. Hence, the reason for writing this article.</p>
<p>Here are the steps that you need to follow, you will create a brand new demo application that will demo using OCMock. You should be able to apply part of these instructions to your own project.</p>
<p>I should preface these instructions by stating that I am not an Xcode or Objective C expert. Also, I&#8217;m using Xcode version 4.1 despite version 4.2 being available. I just haven&#8217;t upgraded yet.</p>
<h3>Building 64-bit OCMock Library</h3>
<p>This is the first necessary task. As stated earlier, the libOCMock.a file found in the downloadable package is 32 bit only.</p>
<ol>
<li>Download the <a href="http://ocmock.org/downloads/ocmock-1.77.dmg">latest OCMock package</a>. At the time of this writing, it&#8217;s version 1.77. It&#8217;s conceivable that later versions will include the 64 bit version and you&#8217;ll be able to skip these steps entirely.</li>
<li>Mount the package and extract the Source and Release directories.</li>
<li>Navigate to Source/ocmock-1.77 and open up OCMock.xcodeproj</li>
<li>When Xcode opens up, click the root node &#8220;5 targets, multiple platforms&#8221; of the project navigator. The project&#8217;s build settings will show up. Observe that there are 5 targets: OCMock, OCMockTests, OCMockPhoneSim, OCMockPhoneDevice, OCMockLib</li>
<li>Notice how there are 4 projects schemes: OCMockPhoneSim, OCMockPhoneDevice, OCMock, and OCMockLib</li>
<li>Notice how there are four products: OCMock.framework, OCMockTests.octest, libOCMock.a, and libOCMock.a</li>
<li>Recall, that we are most interested in a 64 bit libOCMock.a</li>
<li>Delete the target OCMockPhoneSim. Select it. Right click and hit &#8216;Delete&#8217; You should notice that one of the libOCMock.a products disappears, leaving us with one left.</li>
<li>You should still be in the OCMockPhoneSim target with &#8220;My Mac 64-bit&#8221; Select the target OCMockPhoneDevice. Change the Base SDK to Mac OS X 10.7 on every dropdown that you can.</li>
<li>Change Architecture to 64-bit on every drop down that you can.</li>
<li>Remove i386 from Valid Architecture.</li>
<li>Change scheme to &#8220;OCMockLib &#8211; My Mac 64-bit&#8221;</li>
<li>Click &#8216;OCMockLib&#8217; target. Click &#8216;Build Phases&#8221; and then expand the &#8216;Run Script&#8217;, remove the following text:<pre class="brush: bash;">
# combine lib files for device and simulator platforms into one

lipo -create &quot;${BUILD_DIR}/${BUILD_STYLE}-iphoneos/libOCMock.a&quot; &quot;${BUILD_DIR}/${BUILD_STYLE}-iphonesimulator/libOCMock.a&quot; -output &quot;${TARGET_BUILD_DIR}/Library/libOCMock.a&quot;

&amp;nbsp;

# copy the headers (we could have used a copy files build phase, too)

cp -R &quot;${BUILD_DIR}/${BUILD_STYLE}-iphoneos/Headers&quot; &quot;${TARGET_BUILD_DIR}/Library&quot;
</pre></li>
<li>Click &#8216;Build&#8217; from the &#8216;Product&#8217; menu. 64 bit libOCMock.a should be built now. Right click libOCMock.a in the project navigator under the &#8216;Products&#8217; group. Click &#8216;Show in Finder&#8217;. Copy libOCMock.a and the directory OCMock founder in the Headers directory that is located in the same directory as libOCMock.a. Copy these two items to a location that you can find them later.</li>
</ol>
<h3>Adding OCMock to the Demo Project</h3>
<p>Now we&#8217;ll add the library and header to a demo project.</p>
<ol>
<li>Open Xcode. Click File&#8230;New Project. Select Cocoa Application.</li>
<li>Name it whatever you want. Make sure that you click &#8220;Include Unit Tests&#8221;</li>
<li>Verify that the default test is working&#8230; click Product&#8230;Test You should get a test error in the testing file. If so, works as expected.</li>
<li>Navigate to your project directory. Create a directory in it called &#8216;TestLibraries&#8217;</li>
<li>Copy libOCMock.a and the folder &#8216;OCMock&#8217; containing the header files into the &#8216;TestLibraries&#8217; directory.</li>
<li>You&#8217;ll have a group (folder) that is named like so: (YOUR_PROJECT_NAME)Tests. We&#8217;ll refer to this as the testing group. Right click it and click &#8216;Add Files to..&#8217;</li>
<li>Select the folder in your project directory that you created: &#8216;TestLibraries&#8217; Make sure that &#8220;Copy items into designations group&#8217;s folder&#8221; is NOT checked since these files already exist at the project root. Select &#8216;Create groups for any added folders&#8217;. Uncheck your project target and make sure that your testing target is checked.</li>
<li>Go to the Test target build settings. Make sure that: &#8220;Library Search Paths&#8221; has &#8220;TestLibraries&#8221; in it. If not, you&#8217;ll need to add the following string WITH THE QUOTES: &#8220;${SRCROOT}/TestLibraries&#8221;</li>
<li>In the Test target build settings, make sure that &#8220;Header Search Paths&#8221; has &#8220;TestLibraries&#8221; with recursive selected. If not, add the following string WITH THE QUOTES:  &#8221;${SRCROOT}/TestLibraries&#8221; Select the &#8216;recursive&#8217; option.</li>
<li>In the Test target build settings, locate &#8216;Other Linker Flags&#8217;, add: -ObjC -all_load</li>
<li>In your implementation test file, locate the textExample method. Put this snippet in its place:<br />
<pre class="brush: objc;">
#include &lt;OCMock/OCMock.h&gt; //put this at the top
id mockString = [OCMockObject mockForClass:[NSString class]];

[[[mockString stub] andReturn:@&quot;MOCKS UP IN&quot;] lowercaseString];

STAssertEqualObjects([mockString lowercaseString], @&quot;MOCKS UP IN&quot;, nil);
</pre></li>
<li>Click &#8220;Product&#8221; menu and &#8220;Build For Testing&#8221;</li>
<li>Then click &#8220;Product&#8221; and &#8220;Test&#8221; All should pass.</li>
</ol>
<p>If you get the following error: &#8216;unrecognized selector sent to instance&#8217; then you didn&#8217;t at the &#8216;Other Linker Flags&#8217;</p>
<p>Hope this helps. References:</p>
<ul>
<li><a href="http://everburning.com/news/poking-objective-c-with-a-testing-stick/">Poking Objective-C with a Testing Stick</a></li>
<li><a href="http://iamthewalr.us/blog/2008/11/ocmock-and-the-iphone/">OCMock and the iPhone</a></li>
<li><a href="http://alexvollmer.com/posts/2010/06/28/making-fun-of-things-with-ocmock/">Making Fun of Things with OCMock</a></li>
<li><a href="http://www.raywenderlich.com/3716/unit-testing-in-xcode-4-quick-start-guide">Unit Testing in Xcode 4 Quick Start Guide</a></li>
<li><a href="http://erik.doernenburg.com/2008/07/testing-cocoa-controllers-with-ocmock/">Testing Cocoa Controllers with OCMock</a></li>
<li><a href="http://www.sunetos.com/items/2011/01/21/creating-an-xcode-project-template-with-ghunit-and-ocmock/">Creating an Xcode Project Template with GHUnit and OCMock</a></li>
<li><a href="http://jtigger-learning.wikidot.com/getting-ocmock-to-work-with-an-iphone-project">Getting OCMock to Work with an iPhone Project</a></li>
</ul>
<p>Do you use Git? If so, checkout <a href="http://gitpilot.com">Gitpilot</a> to make using Git easy.</p>
<p>Follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a> and read my blog on entrepreneurship: <a href="http://techneur.com">Techneur</a>.</p>
<p>-JP Richardson</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/277/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/277/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/277/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=277&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2011/11/29/using-ocmock-with-mac-os-x-lion-xcode-4-to-mock-and-unit-test-cocoa-desktop-apps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
		<item>
		<title>Synchronous File Copy in Node.js</title>
		<link>http://procbits.com/2011/11/15/synchronous-file-copy-in-node-js/</link>
		<comments>http://procbits.com/2011/11/15/synchronous-file-copy-in-node-js/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 21:58:04 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://procbits.com/?p=274</guid>
		<description><![CDATA[Sometimes, asynchronous operations can be a burden. Especially when you&#8217;re writing small console utilities like to batch process files. There are many asynchronous ways to copy a file. Here is a synchronous version (CoffeeScript): You can view the converted version in JavaScript. Do you use Git? If so, checkout Gitpilot to make using Git thoughtless. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=274&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes, asynchronous operations can be a burden. Especially when you&#8217;re writing small console utilities like to batch process files.</p>
<p>There are many asynchronous ways to copy a file. Here is a synchronous version (CoffeeScript):</p>
<p><pre class="brush: ruby;">
copyFileSync = (srcFile, destFile) -&gt;
  BUF_LENGTH = 64*1024
  buff = new Buffer(BUF_LENGTH)
  fdr = fs.openSync(srcFile, 'r')
  fdw = fs.openSync(destFile, 'w')
  bytesRead = 1
  pos = 0
  while bytesRead &gt; 0
    bytesRead = fs.readSync(fdr, buff, 0, BUF_LENGTH, pos)
    fs.writeSync(fdw,buff,0,bytesRead)
    pos += bytesRead
  fs.closeSync(fdr)
  fs.closeSync(fdw)
</pre></p>
<p>You can view the <a href="http://jashkenas.github.com/coffee-script/#try:copyFileSync%20%3D%20(srcFile%2C%20destFile)%20-%3E%0A%20%20BUF_LENGTH%20%3D%2064*1024%0A%20%20buff%20%3D%20new%20Buffer(BUF_LENGTH)%0A%20%20fdr%20%3D%20fs.openSync(srcFile%2C%20'r')%0A%20%20fdw%20%3D%20fs.openSync(destFile%2C%20'w')%0A%20%20bytesRead%20%3D%201%0A%20%20pos%20%3D%200%0A%20%20while%20bytesRead%20%3E%200%0A%20%20%20%20bytesRead%20%3D%20fs.readSync(fdr%2C%20buff%2C%200%2C%20BUF_LENGTH%2C%20pos)%0A%20%20%20%20fs.writeSync(fdw%2Cbuff%2C0%2CbytesRead)%0A%20%20%20%20pos%20%2B%3D%20bytesRead%0A%20%20fs.closeSync(fdr)%0A%20%20fs.closeSync(fdw)">converted version in JavaScript</a>.</p>
<p>Do you use Git? If so, checkout <a href="http://gitpilot.com">Gitpilot</a> to make using Git thoughtless.</p>
<p>Follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a> and read my blog on entrepreneurship: <a href="http://techneur.com">Techneur</a>.</p>
<p>-JP Richardson </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/274/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/274/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/274/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=274&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2011/11/15/synchronous-file-copy-in-node-js/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
		<item>
		<title>Buzz: A Node.js Command Line Program to Keep Your App Running Indefinitely; Like the Program Forever</title>
		<link>http://procbits.com/2011/11/08/buzz-a-node-js-command-line-program-to-keep-your-app-running-indefinitely-like-the-program-forever/</link>
		<comments>http://procbits.com/2011/11/08/buzz-a-node-js-command-line-program-to-keep-your-app-running-indefinitely-like-the-program-forever/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 20:35:56 +0000</pubDate>
		<dc:creator>JP</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://procbits.com/?p=272</guid>
		<description><![CDATA[Buzz is a command line program that can kill your app routinely and restart it. It&#8217;ll will also restart your app if it dies. It&#8217;s a lot like the other Node.js program Forever. It&#8217;s much simpler than Forever. Approximately 50 lines of CoffeeScript code. It displays your apps output to STDOUT and also displays any [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=272&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Buzz is a command line program that can kill your app routinely and restart it.<br />
It&#8217;ll will also restart your app if it dies. It&#8217;s a lot like the other Node.js<br />
program <a href="https://github.com/indexzero/forever">Forever</a>.</p>
<p>It&#8217;s much simpler than Forever. Approximately 50 lines of CoffeeScript code.<br />
It displays your apps output to STDOUT and also displays any of your apps<br />
STDERR output in red.</p>
<p><strong>Usage</strong></p>
<p>Install it via npm:<br />
<code><br />
npm install buzz<br />
</code></p>
<p>Then run:<br />
<code><br />
buzz 240 your_cool_app param1 param2<br />
</code></p>
<p>The first parameter to buzz is the time in seconds that it&#8217;ll be killed and<br />
restarted. So, `your_cool_app` would be killed and restarted after four minutes.</p>
<p>If you don&#8217;t want buzz to kill your app, but you want it to bring it back to<br />
life if it dies, run:<br />
<code><br />
buzz your_cool_app param1 param2<br />
</code></p>
<p>You can test buzz by running his the app `buzz_test`:<br />
<code><br />
buzz_test<br />
</code></p>
<p>`buzz_test` runs the app `smarty_pants` that spews out random facts to you and<br />
taunts you. Occasionally `smarty_pants` will commit suicide, but buzz will<br />
bring him back to life.</p>
<p>`buzz_test` ends up actualy just running the following command:<br />
<code><br />
buzz 10 smarty_pants 2000 0.15<br />
</code></p>
<p>Which will kill smarty pants every 10 seconds and bring him back to life. Also,<br />
every two seconds, smarty pants will spit out a random fact. Approximately, every<br />
13 seconds smarty pants will take his own life, but Buzz will bring him back.</p>
<p><strong>Motivation</strong></p>
<p>I have a command line app that is nasty to debug. It&#8217;s working fine for the first<br />
five minutes or so. Thus, Buzz was born. Instead of fixing the bug, I wanted<br />
to make this. =)</p>
<p>But really, it&#8217;s utility is that it&#8217;s a much simpler Forever.</p>
<p>The name comes from Buzz Lightyear in the movie Toy Story. His popular phrase was: To infinity and beyond!</p>
<p>Do you use Git? If so, checkout <a href="http://gitpilot.com">Gitpilot</a> to make using Git thoughtless.</p>
<p>Follow me on Twitter: <a href="http://twitter.com/jprichardson">@jprichardson</a> and read my blog on entrepreneurship: <a href="http://techneur.com">Techneur</a>.</p>
<p>-JP Richardson </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/procbits.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/procbits.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/procbits.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/procbits.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/procbits.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/procbits.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/procbits.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/procbits.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/procbits.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/procbits.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/procbits.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/procbits.wordpress.com/272/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/procbits.wordpress.com/272/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/procbits.wordpress.com/272/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=procbits.com&#038;blog=6893023&#038;post=272&#038;subd=procbits&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://procbits.com/2011/11/08/buzz-a-node-js-command-line-program-to-keep-your-app-running-indefinitely-like-the-program-forever/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0f56a5e429de009a27b0ae8f796ef2df?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">JP</media:title>
		</media:content>
	</item>
	</channel>
</rss>
