<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>A blog about server side technologies, nodejs, language design and more.</description><title>TJ Holowaychuk</title><generator>Tumblr (3.0; @tjholowaychuk)</generator><link>http://tjholowaychuk.com/</link><item><title>Stylus gets @extend</title><description>&lt;p&gt;&lt;strong&gt;@extend&lt;/strong&gt; is a great little feature which originated in &lt;a href="http://sass-lang.com/" target="_blank"&gt;SASS&lt;/a&gt;, and now finds a home in &lt;a href="http://learnboost.github.com/stylus/" target="_blank"&gt;Stylus&lt;/a&gt;. If you’ve ever written CSS like this, you know it can become quite the pain to maintain large lists of selectors:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.message,
.warning,
.error {
  font-size: 14px;
  padding: 5px 10px;
  border: 1px solid #eee;
  border-radius: 5px;
}

.warning {
  color: yellow;
}

.error {
  color: red;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;@extend&lt;/strong&gt; makes this process flow naturally, and even supports inheritance. In Stylus you may use &lt;strong&gt;@extend&lt;/strong&gt; or &lt;strong&gt;@extends&lt;/strong&gt;, I prefer the latter personally but that’s up to you! Using this feature you would define &lt;code&gt;.message&lt;/code&gt; on it’s own, and simply __@extend_ it from within the other rulesets:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.message {
  font-size: 14px;
  padding: 5px 10px;
  border: 1px solid #eee;
  border-radius: 5px;
}

.warning {
  @extends .message;
  color: yellow;
}

.error {
  @extends .message;
  color: red;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Taking this even further with inheritance:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.fatal-error {
  @extends .error;
  font-weight: bold
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Would yield the following CSS:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.message,
.warning,
.error,
.fatal-error {
  font-size: 14px;
  padding: 5px 10px;
  border: 1px solid #eee;
  border-radius: 5px;
}
.warning {
  color: #ff0;
}
.error,
.fatal-error {
  color: #f00;
}
.fatal-error {
  font-weight: bold;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here’s another example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;form
  button
    padding: 10px
    border: 1px solid #eee
    border-radius: 5px

ul
  li a
    @extends form button
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Yielding:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;form button,
ul li a {
  padding: 10px;
  border: 1px solid #eee;
  border-radius: 5px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And of course you may utilize the alternative Stylus syntax if you prefer:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.message
  font-size: 14px
  padding: 5px 10px
  border: 1px solid #eee
  border-radius: 5px

.warning
  @extends .message
  color: yellow

.error
  @extends .message
  color: red

.fatal-error
  @extends .error
  font-weight: bold
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Grab &lt;a href="https://github.com/learnboost/stylus" target="_blank"&gt;Stylus&lt;/a&gt; 0.22.4 for these goodies!&lt;/p&gt;</description><link>http://tjholowaychuk.com/post/15686199710</link><guid>http://tjholowaychuk.com/post/15686199710</guid><pubDate>Wed, 11 Jan 2012 13:41:23 -0800</pubDate><category>nodejs</category><category>stylus</category><category>css</category></item><item><title>hahaha awesome</title><description>&lt;img src="http://24.media.tumblr.com/tumblr_lx5g9fUw891qdlh1io1_250.gif"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;hahaha awesome&lt;/p&gt;</description><link>http://tjholowaychuk.com/post/15550074782</link><guid>http://tjholowaychuk.com/post/15550074782</guid><pubDate>Sun, 08 Jan 2012 19:53:05 -0800</pubDate></item><item><title>Why Stylus is the most CSS-like high-level stylesheet language</title><description>&lt;a href="http://vimeo.com/33462524"&gt;Why Stylus is the most CSS-like high-level stylesheet language&lt;/a&gt;: &lt;p&gt;This short Vimeo screencast illustrates why Stylus is the most like CSS as a stylesheet “preprocessor”. More on Stylus &lt;a href="http://learnboost.github.com/stylus" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;</description><link>http://tjholowaychuk.com/post/14070358698</link><guid>http://tjholowaychuk.com/post/14070358698</guid><pubDate>Sun, 11 Dec 2011 09:09:00 -0800</pubDate><category>js</category><category>nodejs</category><category>stylus</category><category>screencast</category></item><item><title>node "debug" module</title><description>&lt;a href="https://github.com/visionmedia/debug"&gt;node "debug" module&lt;/a&gt;</description><link>http://tjholowaychuk.com/post/13486276667</link><guid>http://tjholowaychuk.com/post/13486276667</guid><pubDate>Mon, 28 Nov 2011 19:56:00 -0800</pubDate></item><item><title>Connect 1.8.0 - multipart support</title><description>&lt;p&gt;Connect 1.8.0 is a tiny but substantial release because it adds &lt;em&gt;multipart/form-data&lt;/em&gt; support via Felix Geisendörfer’s fantastic multipart parser &lt;a href="https://github.com/felixge/node-formidable" target="_blank"&gt;formidable&lt;/a&gt;, it’s great and chances are you’re already using it!&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;bodyParser()&lt;/code&gt; middleware now unifies multipart, json, and x-www-form-urlencoded parsing, providing the &lt;code&gt;req.body&lt;/code&gt; object containing the parsed data. For security purposes the files are placed in a separate object, &lt;code&gt;req.files&lt;/code&gt;, however just as accessible. A constant struggle that I’ve seen in the community is the concept of “missing” request &lt;em&gt;data&lt;/em&gt; events so this will
prevent further confusion. The downside to this is that if you wish to report upload progress, or access files and fields &lt;em&gt;as&lt;/em&gt; the request is streamed, you will have to use formidable directly.&lt;/p&gt;

&lt;p&gt;Before this addition your use of formidable might look something like the following (with Express):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  app.post('/someform', function(req, res, next){
    var form = new formidable.IncomingForm;
    form.parse(req, function(err, fields, files){
      if (err) return next(err);
      // do something with files.image etc
    });
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With the new &lt;code&gt;bodyParser()&lt;/code&gt; all you need is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  app.use(express.bodyParser());

  app.post('/some-form', function(req, res){
    // do something with req.files.image
  })
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The middleware takes the same options that are mentioned on formidable’s GitHub repo page, so you can specify things like size limits, retaining extensions, the upload directory etc.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  app.use(express.bodyParser({
    uploadDir: '/tmp/uploads'
  }));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That’s it! but I think this will cover well above the 80% use-case and prevent a lot of headaches that people have with the previous the inconsistencies.&lt;/p&gt;</description><link>http://tjholowaychuk.com/post/12943975936</link><guid>http://tjholowaychuk.com/post/12943975936</guid><pubDate>Thu, 17 Nov 2011 15:47:00 -0800</pubDate><category>connect</category><category>nodejs</category></item><item><title>Batman vs Pedophile (vigilante justice)</title><description>&lt;a href="http://bit.ly/v5bUgE"&gt;Batman vs Pedophile (vigilante justice)&lt;/a&gt;</description><link>http://tjholowaychuk.com/post/12914168031</link><guid>http://tjholowaychuk.com/post/12914168031</guid><pubDate>Wed, 16 Nov 2011 20:38:56 -0800</pubDate></item><item><title>Jade &amp; Stylus 0.16.0 released</title><description>&lt;h2&gt;Jade 0.16.0&lt;/h2&gt;

&lt;p&gt;This release of Jade adds quite a few new features such block support for &lt;code&gt;include&lt;/code&gt;, and template inheritance!&lt;/p&gt;

&lt;h3&gt;Extended grammar&lt;/h3&gt;

&lt;p&gt;Jade currently allows you to use JavaScript all over the place, rather than restricting a subset, or implement its own grammar. While this is flexible it tends to make templates look less like … templates! In an effort to make Jade more declarative I’ve added literal support for &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;unless&lt;/code&gt;, &lt;code&gt;while&lt;/code&gt;, &lt;code&gt;until&lt;/code&gt;, iteration, assignments and others.&lt;/p&gt;

&lt;p&gt;For example I consider it bad practice to define vars in a template, however if you must, you may have previously done something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  - var items = ['foo', 'bar', 'baz']
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Jade now allows for assignment, where the right-hand operand is still a regular javascript expression:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;   items = ['foo', 'bar', 'baz']
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As mentioned &lt;code&gt;if&lt;/code&gt; and &lt;code&gt;unless&lt;/code&gt; and friends are supported, so the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; - if (!(foo &amp;&amp; bar))
   p stuff
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;May now be defined as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  unless foo &amp;&amp; bar
    p stuff
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This rule applies to &lt;code&gt;else[ if]&lt;/code&gt; as well:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  if something
    p foo
  else if somethingElse
    p bar
  else
    p baz
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And finally iteration where &lt;code&gt;each&lt;/code&gt; and &lt;code&gt;for&lt;/code&gt; are interchangeable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; - items.forEach(function(item){
   li= item
 - }) 

each item in items
  li= item

for item in items
  li= item

each item, i in items
  li #{i}: #{item}

each val, key in obj
  li #{key}: #{val}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Include blocks&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;include&lt;/code&gt; directive now supports blocks, so for example one could have &lt;code&gt;head.jade&lt;/code&gt; defined as the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  head
    script(src='/javascripts/jquery.js')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Allowing you to either include the file as-is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  html
    include head
    body
      h1 Title
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or append to the last block defined, adding additional &lt;code&gt;script&lt;/code&gt; tags:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  html
    include head
      script(src='/javascripts/caustic.js')
      script(src='/javascripts/app.js')
    body
      h1 Title
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Template inheritance&lt;/h3&gt;

&lt;p&gt;Jade now supports template inheritance via the &lt;code&gt;block&lt;/code&gt; and &lt;code&gt;extends&lt;/code&gt; keywords. A block is simply a “block” (indented) of Jade that may be replaced within a child template, this process is recursive.&lt;/p&gt;

&lt;p&gt;Jade blocks can provide default content if desired, however optional as shown below by &lt;code&gt;block scripts&lt;/code&gt;, &lt;code&gt;block content&lt;/code&gt;, and &lt;code&gt;block foot&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// layout.jade
html
  head
    h1 My Site - #{title}
    block scripts
      script(src='/jquery.js')
  body
    block content
    block foot
      #footer
        p some footer content
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now to extend the layout, simply create a new file and use the &lt;code&gt;extends&lt;/code&gt; directive as shown below, giving the path (with or without the .jade extension). You may now define one or more blocks that will override the parent block content, note that here the &lt;code&gt;foot&lt;/code&gt; block is &lt;em&gt;not&lt;/em&gt; redefined and will output “some footer content”.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// index.jade
extends layout

block scripts
  script(src='/jquery.js')
  script(src='/pets.js')

block content
  h1= title
  each pet in pets
    include pet
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It’s also possible to override a block to provide additional blocks, as shown in the following example where &lt;code&gt;content&lt;/code&gt; now exposes a &lt;code&gt;sidebar&lt;/code&gt; and &lt;code&gt;primary&lt;/code&gt; block for overriding, or the child template could override &lt;code&gt;content&lt;/code&gt; all together.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// funky-layout.jade
extends layout

block content
  .sidebar
    block sidebar
      p nothing
  .primary
    block primary
      p nothing
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For the rest of the changes and bug fixes check out the &lt;a href="https://github.com/visionmedia/jade/blob/master/History.md" target="_blank"&gt;changelog&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Stylus 0.16.0&lt;/h2&gt;

&lt;p&gt;Fewer changes than the latest Jade however still some good ones!&lt;/p&gt;

&lt;h2&gt;:= operator&lt;/h2&gt;

&lt;p&gt;This new operator is an alias of &lt;code&gt;?=&lt;/code&gt; operator, because I think &lt;code&gt;?=&lt;/code&gt; is difficult to read in large quantities for configuration etc:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;grid-columns ?= 16
grid-column-width ?= 40px
grid-gutter-width ?= 20px
extra-space ?= 40px

grid-columns := 16
grid-column-width := 40px
grid-gutter-width := 20px
extra-space := 40px
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Adjusting color lightness&lt;/h2&gt;

&lt;p&gt;While the &lt;code&gt;lighten(color, amount)&lt;/code&gt; and &lt;code&gt;darken(color, amount)&lt;/code&gt; BIFs are still available, the &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;-&lt;/code&gt; operators now provide the same functionality when used with percentages as illustrated below:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  lighten(black, 50%)
  // =&gt; #808080

  black + 50%
  // =&gt; #808080
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;fade-in() &amp; fade-out() BIFs&lt;/h2&gt;

&lt;p&gt;These small built-in functions are defined in stylus as the following to adjust the opacity:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  // decerase opacity by amount

  fade-out(color, amount)
    color - rgba(black, amount)

  // increase opacity by amount

  fade-in(color, amount)
    color + rgba(black, amount)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fade-out(#eee, .5)
// =&gt; rgba(238,238,238,0.50);

fade-out(fade-out(#eee, .3), .5)
// =&gt; rgba(238,238,238,0.20);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Check out the &lt;a href="https://github.com/learnboost/stylus/blob/master/History.md" target="_blank"&gt;changelog&lt;/a&gt; for the other bugfixes and additions.&lt;/p&gt;</description><link>http://tjholowaychuk.com/post/10695801204</link><guid>http://tjholowaychuk.com/post/10695801204</guid><pubDate>Mon, 26 Sep 2011 12:56:00 -0700</pubDate></item><item><title>Stylus 0.15.1 - new property reference &amp; @keyframes fabrication features</title><description>&lt;p&gt;Stylus 0.15.x adds a bunch of cool new functionality as well as some important bug fixes.&lt;/p&gt;

&lt;h2&gt;@keyframes fabrication&lt;/h2&gt;

&lt;p&gt;The new keyframes fabrication support automatically duplicates your &lt;strong&gt;@keyframes&lt;/strong&gt; definitions to support vendor prefixes. Note that this is &lt;em&gt;only&lt;/em&gt; a default, as shown below:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@-moz-keyframes foo {
  0% {
    color: #000;
  }

  100% {
    color: #fff;
  }
}
@-webkit-keyframes foo {
  0% {
    color: #000;
  }

  100% {
    color: #fff;
  }
}
@keyframes foo {
  0% {
    color: #000;
  }

  100% {
    color: #fff;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;one can tweak this functionality using the &lt;code&gt;vendors&lt;/code&gt; list as shown in the following snippet which will only expand to the &lt;code&gt;-webkit-&lt;/code&gt; vendor prefix.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;vendors = moz official

@keyframes foo {
  from {
    color: black
  }
  to {
    color: white
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;vendors&lt;/code&gt; property may be utilized more in the future, and perhaps in frameworks like “nib” to unify configuration.&lt;/p&gt;

&lt;h2&gt;Property reference&lt;/h2&gt;

&lt;p&gt;The syntax &lt;code&gt;@&lt;property&gt;&lt;/code&gt; has been introduced to reference values of existing properties. For example if you want to re-use values, you may typically do something like below instead of typing the value several times, also allowing you to use functions with &lt;code&gt;w&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.button
  width: w = 50px
  height: w
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The new property reference syntax allows you to treat properties as variables, which can also be accessed within mixins:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.button
  width: 50px
  height: @width
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Comment compilation&lt;/h2&gt;

&lt;p&gt;When the &lt;code&gt;compress&lt;/code&gt; option is &lt;code&gt;true&lt;/code&gt; Stylus will &lt;em&gt;not&lt;/em&gt; output single-line nor multi-line comments, which are commonly found used for licensing in stylesheet frameworks etc, so it’s useful to strip these out &lt;em&gt;however&lt;/em&gt; you can now force output with the &lt;code&gt;!&lt;/code&gt; character &lt;code&gt;/*!&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;New built-in functions&lt;/h2&gt;

&lt;p&gt;The math functions &lt;code&gt;cos()&lt;/code&gt; and &lt;code&gt;sin()&lt;/code&gt; were added, as well as exposing &lt;code&gt;PI&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;Bug fixes&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;Fixed @import on windows&lt;/li&gt;
&lt;/ul&gt;</description><link>http://tjholowaychuk.com/post/9686647689</link><guid>http://tjholowaychuk.com/post/9686647689</guid><pubDate>Thu, 01 Sep 2011 17:35:08 -0700</pubDate><category>stylus</category><category>nodejs</category><category>js</category></item><item><title>Connect 1.7.0 - fast static file memory cache and more</title><description>&lt;p&gt;Be sure to update &lt;a href="https://github.com/senchalabs/connect" target="_blank"&gt;Connect&lt;/a&gt; to the latest release 1.7.0 for the following goodies!&lt;/p&gt;

&lt;h2&gt;staticCache() middleware&lt;/h2&gt;

&lt;p&gt;1.7.0 now provides a new middleware named &lt;code&gt;staticCache()&lt;/code&gt;, acting as a memory cache on top of the regular &lt;code&gt;static()&lt;/code&gt; middleware. Older versions of Connect used to provide memory caching, however it was baked right into &lt;code&gt;static()&lt;/code&gt;, bloating the middleware more than necessary.&lt;/p&gt;

&lt;h3&gt;Benchmarks&lt;/h3&gt;

&lt;p&gt;The test file for these benchmarks is a small 4kb readme:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ du -h Readme.md 
4.0K    Readme.md
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and the following ApacheBench command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ ab -n 5000 -c 50 -k http://local/Readme.md
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;First up we have Connect &lt;code&gt;static()&lt;/code&gt;, which does not perform any caching, performing disk I/O each request, serving ~2400rps.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;connect()
  .use(connect.static(__dirname))
  .listen(3000)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next up we have &lt;a href="https://github.com/cloudhead/node-static" target="_blank"&gt;node-static&lt;/a&gt; one of the other popular node solutions, serving ~3800rps&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var static = require('node-static')
  , file = new static.Server(__dirname);

http.createServer(function(req, res){
  file.serve(req, res);
}).listen(3000);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then we have the new &lt;code&gt;staticCache()&lt;/code&gt; middleware paired with &lt;code&gt;static()&lt;/code&gt; serving ~5000rps, a 24% increase over node-static (which also performs memory caching), and ~52% over &lt;code&gt;static()&lt;/code&gt; alone.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;connect()
  .use(connect.staticCache())
  .use(connect.static(__dirname))
  .listen(3000)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;staticCache()&lt;/code&gt; is configurable, allowing you to specify the maximum number of cache objects to store, and the maximum size allowed, so you can cap resources appropriately. The middleware implements a Least Recently Used algorithm to prioritize popular files, knocking less popular objects out of the cache.&lt;/p&gt;

&lt;h2&gt;res.headerSent&lt;/h2&gt;

&lt;p&gt;Node core has a private flag named &lt;code&gt;res._headerSent&lt;/code&gt;, allowing you to check if the header has been written or not, this is extremely useful in some cases, so Connect has publicized this property as &lt;code&gt;res.headerSent&lt;/code&gt; until Node core does (if ever), using a getter to reference the private property.&lt;/p&gt;

&lt;h2&gt;logger() immediate option&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;logger()&lt;/code&gt; middleware now provides &lt;code&gt;{ immediate: true }&lt;/code&gt;, as by default this middleware will log the &lt;em&gt;response&lt;/em&gt; of a request, not when the request is first received, allowing logging of response-times etc.&lt;/p&gt;</description><link>http://tjholowaychuk.com/post/9682643240</link><guid>http://tjholowaychuk.com/post/9682643240</guid><pubDate>Thu, 01 Sep 2011 16:02:00 -0700</pubDate><category>connect</category><category>nodejs</category></item><item><title>Game prototyping with JavaScript &amp; CSS3</title><description>&lt;p&gt;I wanted to share a quick few-hour &lt;a href="https://github.com/visionmedia/game-prototypes" target="_blank"&gt;game prototype&lt;/a&gt; to hopefully inspire a few people! The industry let-alone browser-based games seem very lacking in imagination and storytelling, or maybe I’m just not looking hard enough ;) but I miss games like &lt;a href="http://en.wikipedia.org/wiki/Grim_Fandango" target="_blank"&gt;Grim Fandango&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Below is a screenshot of the demo, consisting of several DOM nodes to represent parts of the main character, the birds, and the scene. Most of the animation is performed with CSS transitions / animations, some of which are dynamic and use the &lt;a href="https://github.com/visionmedia/move.js" target="_blank"&gt;move.js&lt;/a&gt; library.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://f.cl.ly/items/3x09401g2a1i3l3a2V2Y/Screenshot.png" alt=""/&gt;&lt;/p&gt;

&lt;p&gt;I have yet to profile anything but it runs quite smooth on my machine, and there are many aspects that could be optimized, and some simple ones like ditching jQuery.&lt;/p&gt;

&lt;h2&gt;iPhone gaming&lt;/h2&gt;

&lt;p&gt;Back to the original plan, roughly two years ago before the Ipad came out, I was writing an Objective-C iPhone game with &lt;a href="http://www.cocos2d-iphone.org/" target="_blank"&gt;Cocos2d&lt;/a&gt; and &lt;a href="http://code.google.com/p/chipmunk-physics/" target="_blank"&gt;Chipmunk physics&lt;/a&gt;, two really wicked libraries. The iPhone seemed too small, and at the time the resolution was not great either, not to mention the image size restraints that Apple enforced, so I gave up after a few prototypes, and one full-screen PC prototype.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://f.cl.ly/items/2K0j2x2A2a3H2z3c1d0B/Screenshot.png" alt=""/&gt;&lt;/p&gt;

&lt;h2&gt;Browser gaming&lt;/h2&gt;

&lt;p&gt;With the advent of modern browsers, browser gaming is already a lot less shitty than a year or two ago, and it’s only about to get much better with all the work browser vendors are putting into making sure canvas and friends run smooth as butter.&lt;/p&gt;

&lt;p&gt;The first prototype I wrote was written entirely with Canvas. One of the main benefits of this is a certain level of control that you obtain, it’s extremely easy to implement features like pausing, to apply post-processing, or ad-hoc features that are difficult with the DOM.&lt;/p&gt;

&lt;p&gt;At first I was pretty happy with the result, until I wrote the prototype using CSS3 and HTML, the performance impact was pretty significant (though I had not implemented dirty rects etc). Certain aspects of gaming are significantly easier using the DOM, for example leveraging z-index and CSS transitions, however contrasting canvas pausing and post-processing are more of a problem. At least with the currently state of browser technologies, it’s a good idea to mix and match. You can build a large portion of your game using the DOM, and canvas for aspects like particle emitters, or perhaps even WebGL shaders like this &lt;a href="http://hakim.se/experiments/webgl/particles/01/" target="_blank"&gt;awesome demo&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Demo&lt;/h2&gt;

&lt;p&gt;The video below shows off some of the interactivity, as well as the tiny scene building tool.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.screenr.com/ZfMs" target="_blank"&gt;&lt;img src="http://f.cl.ly/items/2z0f0l1x2v3b212Z3N1I/Screenshot.png" alt=""/&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The demo consists of ~200 lines of js and ~200 lines of CSS, using CSS for transformations, animations and obviously for styling, while the js performs some basic interactions like the eye-ball targeting, and randomized cat walk routine. The demo also has a small toolkit for 
creating scenes, but it’s certainly nothing robust.&lt;/p&gt;

&lt;h2&gt;Source&lt;/h2&gt;

&lt;p&gt;You can grab the source on &lt;a href="https://github.com/visionmedia/game-prototypes" target="_blank"&gt;Github&lt;/a&gt;. Ping me of you come up with some cool prototypes!&lt;/p&gt;

&lt;h2&gt;Concepts&lt;/h2&gt;

&lt;p&gt;Some more concepts:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://f.cl.ly/items/2b0x162O2i1w140O0U2K/Screenshot.png" alt=""/&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://f.cl.ly/items/2P0F1i2a0R2u3S2j1V1U/Screenshot.png" alt=""/&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src="http://f.cl.ly/items/3G1X1H1f3J00421H1Z2M/Screenshot.png" alt=""/&gt;&lt;/p&gt;</description><link>http://tjholowaychuk.com/post/9524129814</link><guid>http://tjholowaychuk.com/post/9524129814</guid><pubDate>Sun, 28 Aug 2011 17:13:00 -0700</pubDate><category>game</category><category>css</category><category>js</category></item><item><title>commander.js - nodejs command-line interfaces made easy</title><description>&lt;p&gt;&lt;a href="https://github.com/visionmedia/commander.js" target="_blank"&gt;Commander.js&lt;/a&gt; is a small &lt;a href="http://nodejs.org" target="_blank"&gt;node.js&lt;/a&gt; module allowing you to define options and interacte with the user’s terminal in a simple and natural way, inspired by the &lt;a href="https://github.com/visionmedia/commander" target="_blank"&gt;Ruby library&lt;/a&gt; of the same name.&lt;/p&gt;

&lt;h2&gt;Features&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;self-documenting code&lt;/li&gt;
&lt;li&gt;auto-generated help&lt;/li&gt;
&lt;li&gt;combined short flags (“-abc” == “-a -b -c”)&lt;/li&gt;
&lt;li&gt;option defaults&lt;/li&gt;
&lt;li&gt;option coercion&lt;/li&gt;
&lt;li&gt;command parsing&lt;/li&gt;
&lt;li&gt;prompts&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;Example&lt;/h2&gt;

&lt;p&gt;A basic commander program looks something like the following (taken from &lt;a href="https://github.com/visionmedia/serve" target="_blank"&gt;serve&lt;/a&gt;). It’s extremely easy to see what’s going on, all the options provided by the executable are laid out infront of you.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;program
  .version('0.0.1')
  .option('-p, --port &lt;port&gt;', 'specify the port [3000]', Number, 3000)
  .option('-H, --hidden', 'enable hidden file serving')
  .option('-I, --no-icons', 'disable file icons')
  .option('-L, --no-logs', 'disable request logging')
  .parse(process.argv);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the previous example only &lt;code&gt;--port&lt;/code&gt; accepts an argument, and the value of &lt;code&gt;program.port&lt;/code&gt; defaults to &lt;code&gt;3000&lt;/code&gt;. The options &lt;code&gt;--no-icons&lt;/code&gt; and &lt;code&gt;--no-logs&lt;/code&gt; default their properties to &lt;code&gt;true&lt;/code&gt;, only when &lt;code&gt;--no-icons&lt;/code&gt; is specified will &lt;code&gt;program.icons&lt;/code&gt; be &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The usage information is free!:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ serve --help

  Usage: serve [options]

  Options:

    -v, --version      output the version number
    -p, --port &lt;port&gt;  specify the port [3000]
    -H, --hidden       enable hidden file serving
    -I, --no-icons     disable file icons
    -L, --no-logs      disable request logging
    -h, --help         output usage information
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Utilities&lt;/h2&gt;

&lt;p&gt;Commander is bundled with some utilities for prompting user input, confirmations, passwords, lists of choices etc. Most of these utilities will ask for input if the user simply hits enter and should respond.&lt;/p&gt;

&lt;p&gt;Below is an example of asking for a name using a single-line input prompt:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;program.prompt('name: ', function(name){
  console.log('hi %s', name);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Multi-line input is easy too, just leave out the trailing space in the message:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;program.prompt('description:', function(name){
  console.log('hi %s', name);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Coercion is useful for dates, numbers etc:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; program.prompt('Age: ', Number, function(age){
  console.log('age: %j', age);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Password prompts masking off input:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;program.password('Password: ', function(pass){
  console.log('got "%s"', pass);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or providing a mask char:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;program.password('Password: ', '*', function(pass){
  console.log('got "%s"', pass);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Confirmations require “yes” or “y” to result in &lt;code&gt;true&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; program.confirm('continue? ', function(ok){
   console.log(' got %j', ok);
 });
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There’s also choice support, so users can select from a list:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];

console.log('Choose the coolest pet:');
program.choose(list, function(i){
  console.log('you chose %d "%s"', i, list[i]);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;presenting:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Choose the coolest pet:
  1) tobi
  2) loki
  3) jane
  4) manny
  5) luna
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Commands&lt;/h2&gt;

&lt;p&gt;Though I haven’t had time to polish them up yet, commander supports the idea of .. well… “commands”. The “root” executable is an instanceof &lt;code&gt;Command&lt;/code&gt;, and well you can recursively define these to create a rich interface. GIT is a great example of this, many larger utilities use sub-command such as &lt;code&gt;git remote&lt;/code&gt; to accept arguments, and may all then have their own options etc, using the same API as the root command. The following is a simple example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/usr/bin/env node

var program = require('../');

program
  .version('0.0.1')
  .option('-C, --chdir &lt;path&gt;', 'change the working directory')
  .option('-c, --config &lt;path&gt;', 'set config path [./deploy.conf]')
  .option('-T, --no-tests', 'ignore test hook')

// $ deploy setup stage
// $ deploy setup
program
  .command('setup [env]')
  .description('run setup commands for all envs')
  .action(function(env){
    env = env || 'all';
    console.log('setup for %s env(s)', env);
  });

// $ deploy stage
// $ deploy production
program
  .command('*')
  .action(function(env){
    console.log('deploying "%s"', env);
  });

program.parse(process.argv);
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Moar Libraries!&lt;/h2&gt;

&lt;p&gt;Dont forget to check out these other great CLI-related libraries:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="https://github.com/LearnBoost/cli-table" target="_blank"&gt;cli-table&lt;/a&gt; awesome unicode tables by Guillermo &lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/substack/node-multimeter" target="_blank"&gt;node-multimeter&lt;/a&gt;, ascii progress-bars by SubStack&lt;/li&gt;
&lt;/ul&gt;</description><link>http://tjholowaychuk.com/post/9103188408</link><guid>http://tjholowaychuk.com/post/9103188408</guid><pubDate>Thu, 18 Aug 2011 18:11:00 -0700</pubDate><category>node</category><category>nodejs</category><category>commander</category><category>ruby</category></item><item><title>Introducing Texty &amp; Super Agent screencast</title><description>&lt;p&gt;In this &lt;a href="http://vimeo.com/27363771" target="_blank"&gt;20 minute screencast&lt;/a&gt; we dive into the full-text Redis search library  &lt;a href="https://github.com/visionmedia/reds" target="_blank"&gt;reds&lt;/a&gt; for &lt;a href="http://nodejs.org" target="_blank"&gt;nodejs&lt;/a&gt;, an introduction to the canvas-only text editing library &lt;a href="https://github.com/learnboost/texty" target="_blank"&gt;Texty&lt;/a&gt;, discussing how you can style canvas drawings with CSS, and the “ajax with less suck” library &lt;a href="http://visionmedia.github.com/superagent/" target="_blank"&gt;superagent&lt;/a&gt;.&lt;/p&gt;</description><link>http://tjholowaychuk.com/post/8673547254</link><guid>http://tjholowaychuk.com/post/8673547254</guid><pubDate>Mon, 08 Aug 2011 20:01:24 -0700</pubDate><category>js</category><category>superagent</category><category>texty</category><category>reds</category><category>redis</category><category>nodejs</category></item><item><title>Reds - light-weight full text search for nodejs backed by redis</title><description>&lt;p&gt;&lt;a href="https://github.com/visionmedia/reds" target="_blank"&gt;Reds&lt;/a&gt; (red-s) is a &lt;em&gt;very&lt;/em&gt; small (~300LOC) light-weight full text search library for &lt;a href="http://nodejs.org" target="_blank"&gt;node.js&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I wrote reds with &lt;a href="http://learnboost.github.com/kue/" target="_blank"&gt;Kue&lt;/a&gt; in mind, a priority job queue for node. I wanted to add search capabilities so you can easily find jobs by any of the arbitrary data provided, names, emails, anything.&lt;/p&gt;

&lt;h2&gt;API&lt;/h2&gt;

&lt;p&gt;You can use reds for multiple isolated search indexes, which is why you must pass a key to &lt;code&gt;reds.createSearch()&lt;/code&gt;, as it’s used for namespacing.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; var search = reds.createSearch('pets');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As mentioned this library could be used with anything, to illustrate this we can even use it with a regular javascript array by indexing the value indices as shown below, where the first value passed to &lt;code&gt;search.index()&lt;/code&gt; is the text, and the second is the id.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; var strs = [];
 strs.push('Tobi wants four dollars');
 strs.push('Tobi only wants $4');
 strs.push('Loki is really fat');
 strs.push('Loki, Jane, and Tobi are ferrets');
 strs.push('Manny is a cat');
 strs.push('Luna is a cat');
 strs.push('Mustachio is a cat');

 strs.forEach(function(str, i){ search.index(str, i); });
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Within the another process, or the same one, we can then invoke &lt;code&gt;search.query()&lt;/code&gt; with a string and callback invoked with possible error
  and array of &lt;code&gt;ids&lt;/code&gt;. With these ids we can then determine their original values, be it an array, object in another data store etc.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; search.query('luna cat', function(err, ids){
   if (err) throw err;
   console.log('Search results:');
   ids.forEach(function(id){
     console.log('  - %s', strs[id]);
   });
 });
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Producing:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  Search results:
     - Luna is a cat
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You may also remove an id from the index:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  search.remove(id[, callback]);
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Implementation&lt;/h2&gt;

&lt;p&gt;While reds is backed by &lt;a href="http://redis.io" target="_blank"&gt;Redis&lt;/a&gt; you can easily use reds with any other data store, as it simply indexes by arbitrary numeric or string ids. This means you could create an index of files on disk, mongodb documents, urls, anything.&lt;/p&gt;

&lt;p&gt;The indexing process works like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;- tokenize words
- strip stop words ("about", "after", "am", "an", ...)
- stem words
- apply metaphone
- add id to metaphone constant set 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The process of stemming the words and applying the &lt;a href="http://en.wikipedia.org/wiki/Metaphone" target="_blank"&gt;metaphone&lt;/a&gt; algorithm provide leeway so the user does not need exact matches. For example thanks to metaphone the names “steven” and “stephen” both resolve to the constant &lt;strong&gt;STFN&lt;/strong&gt;. The process of &lt;a href="http://en.wikipedia.org/wiki/Stem_(linguistics)" target="_blank"&gt;stemming&lt;/a&gt; reduces variants to it’s stem, for example “counting” becomes “count”, and “waits” becomes “wait”.&lt;/p&gt;

&lt;p&gt;When a query is performed the same sequence is applied, resulting in an array of metaphone constants, providing us with the keys necessary to perform the Redis union or intersection to fetch our ids.&lt;/p&gt;

&lt;h2&gt;Performance&lt;/h2&gt;

&lt;p&gt;Preliminary benchmarks show that a small &lt;strong&gt;1.6kb&lt;/strong&gt; body of text is currently indexed in &lt;strong&gt;~6ms&lt;/strong&gt;, or &lt;strong&gt;163&lt;/strong&gt; ops/s. Medium bodies such as &lt;strong&gt;40kb&lt;/strong&gt; operate around &lt;strong&gt;6&lt;/strong&gt; ops/s, or &lt;strong&gt;166ms&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Querying with a multi-word phrase, and an index containing &lt;strong&gt;~3500&lt;/strong&gt; words operates around &lt;strong&gt;5300&lt;/strong&gt; ops/s. Not too bad.&lt;/p&gt;

&lt;p&gt;Indexing performance was decreased by nearly 100% by applying the porter stemmer algorithm in Chris Umbel’s &lt;a href="https://github.com/chrisumbel/natural" target="_blank"&gt;natural&lt;/a&gt; library, however reds is still reasonably quick at indexing text. If you have huge documents, you may want to consider allowing users to specify a description instead.&lt;/p&gt;

&lt;h2&gt;Future&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;use sorted sets for ordering and priority&lt;/li&gt;
&lt;li&gt;ranges&lt;/li&gt;
&lt;li&gt;sorting&lt;/li&gt;
&lt;li&gt;perf optimization if necessary&lt;/li&gt;
&lt;/ul&gt;</description><link>http://tjholowaychuk.com/post/8265428421</link><guid>http://tjholowaychuk.com/post/8265428421</guid><pubDate>Sat, 30 Jul 2011 11:52:00 -0700</pubDate><category>node</category><category>redis</category><category>search</category></item><item><title>Jade Mixins &amp; Includes</title><description>&lt;p&gt;The latest release of &lt;a href="http://github.com/visionmedia/jade" target="_blank"&gt;Jade&lt;/a&gt; 0.13.0 adds &lt;em&gt;mixins&lt;/em&gt; and &lt;em&gt;includes&lt;/em&gt;. Jade users have longed for some method of static include, so it’s (finally) here, and compliments mixins nicely, allowing you to store mixins in one or more separate files.&lt;/p&gt;

&lt;h2&gt;Mixins&lt;/h2&gt;

&lt;p&gt;A mixin definition takes the form &lt;code&gt;mixin &lt;name&gt; [( params )] block&lt;/code&gt;, where params is identical to JavaScript function params, simply a list, as it’s converted to a JavaScript function within Jade to become part of the output function.&lt;/p&gt;

&lt;p&gt;Using a mixin is identical to the definition, however omitting the block. Mixins allow you to encapsulate repeated chunks of a template, such as form fields as shown in the following snippet. This example has a local variable &lt;code&gt;user&lt;/code&gt; passed to the template with the properties &lt;code&gt;{ name: '...', email: '...' }&lt;/code&gt;, which is then accessible throughout any mixins defined without explicitly passing it (though you may if you like).&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;mixin field(type, name, label)
  .field(class='field-' + type)
    label #{label}:
    input(type=type, name='user[#{name}]', value=user[name])

form
  mixin field('text', 'name', 'Username')
  mixin field('text', 'email', 'Email')
  mixin field('password', 'pass', 'Password')
  input(type='submit', value='Sign Up')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Outputting:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;form&gt;
  &lt;div class="field field-text"&gt;
    &lt;label&gt;Username:&lt;/label&gt;
    &lt;input type="text" name="user[name]" value="TJ Holowaychuk"/&gt;
  &lt;/div&gt;
  &lt;div class="field field-text"&gt;
    &lt;label&gt;Email:&lt;/label&gt;
    &lt;input type="text" name="user[email]" value="tj@learnboost.com"/&gt;
  &lt;/div&gt;
  &lt;div class="field field-password"&gt;
    &lt;label&gt;Password:&lt;/label&gt;
    &lt;input type="password" name="user[pass]"/&gt;
  &lt;/div&gt;
  &lt;input type="submit" value="Sign Up"/&gt;
&lt;/form&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Includes&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;include &lt;path&gt;&lt;/code&gt; directive signals Jade to read and parse the file, then return it’s root node (a block), injecting it into the calling template, as if it were written in the same file. The &lt;code&gt;&lt;path&lt;/code&gt;&gt; given is relative to the dirname of the calling template, which is exposed to Jade via the &lt;code&gt;filename&lt;/code&gt; option, which is populated by &lt;code&gt;jade.renderFile()&lt;/code&gt; and Express, otherwise you need to supply this.&lt;/p&gt;

&lt;p&gt;For example our mixin(s) could live in &lt;code&gt;./mixins&lt;/code&gt;, and our previous example could look something like below:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;include mixins/form-helpers

form
  mixin field('text', 'name', 'Username')
  mixin field('text', 'email', 'Email')
  mixin field('password', 'pass', 'Password')
  input(type='submit', value='Sign Up')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or the classic header / footer example, first &lt;em&gt;index.jade&lt;/em&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;html
  include includes/head  
  body
    h1 My Site
    p Welcome to my super lame site.
    include includes/foot
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;includes/foot.jade&lt;/em&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#footer
  p Copyright (c) foobar
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;includes/head.jade&lt;/em&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;head
  title My Site
  script(src='/javascripts/jquery.js')
  script(src='/javascripts/app.js')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That’s all for now, hopefully I’ll have some time in the near future to clean things up and get 1.0 (finally again :)) out the door.&lt;/p&gt;</description><link>http://tjholowaychuk.com/post/7590787973</link><guid>http://tjholowaychuk.com/post/7590787973</guid><pubDate>Wed, 13 Jul 2011 16:18:00 -0700</pubDate><category>node</category><category>jade</category></item><item><title>The Luna programming language</title><description>&lt;p&gt;A few weeks ago I started implementing &lt;a href="https://github.com/visionmedia/luna" target="_blank"&gt;Luna&lt;/a&gt;, an elegant, expressive, and embeddable language written in ANSI C, with performance as a primary focus. For now it’s just a side project, but I thought it would be fun to blog about the different aspects as they unfold, building languages is a really fun process.&lt;/p&gt;

&lt;h2&gt;Goals&lt;/h2&gt;

&lt;p&gt;The current goals of Luna are the following:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;small, simple, expressive, and explicit syntax&lt;/li&gt;
&lt;li&gt;fast, fast, and fast&lt;/li&gt;
&lt;li&gt;robust reflection capabilities&lt;/li&gt;
&lt;li&gt;opt-in callee evaluated messages&lt;/li&gt;
&lt;li&gt;register based VM&lt;/li&gt;
&lt;li&gt;embeddable&lt;/li&gt;
&lt;/ul&gt;&lt;h2&gt;About Luna’s Syntax&lt;/h2&gt;

&lt;p&gt;Luna’s syntax is inspired by Python, &lt;a href="http://www.iolanguage.com/" target="_blank"&gt;IO&lt;/a&gt;, and &lt;a href="http://www.lua.org/" target="_blank"&gt;Lua&lt;/a&gt;, bits of Ruby, and of course, JavaScript.&lt;/p&gt;

&lt;p&gt;The language itself will utilize “pure” prototypes for inheritance. When I say “pure”, I mean no confusing class/prototype hybrid monster barfing up poor design, which we all face in JavaScript. The way JavaScript handles prototypes adds to the confusion of what may already be a completely new concept to some. Contrasting with JavaScript this means losing constructors, ditching &lt;code&gt;.prototype&lt;/code&gt;, the &lt;code&gt;new&lt;/code&gt; operator, &lt;code&gt;instanceof&lt;/code&gt;, &lt;code&gt;typeof&lt;/code&gt; and other clunky “features”.&lt;/p&gt;

&lt;p&gt;Take the following JavaScript for example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function User(name) {
  this.name = user;
}

User.prototype.toString = function(){
 return this.name;
}

new User('tj')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is a big W-T-F for some people, some get confused thinking it’s a class, and some get lost trying to understand that the prototype property has to do with anything at all. Simply put, a “prototype” is just an object, that’s it, it’s an object that you “clone”, which for performance reasons typically means you have a new object with the “parent” in a special prototype chain for property or “slot” lookup. That’s it! adding constructors and funky props and stupid keywords to the mix just makes things needlessly confusing, and really takes away from what would otherwise be a pretty elegant language.&lt;/p&gt;

&lt;h3&gt;Whitespace&lt;/h3&gt;

&lt;p&gt;Our earlier JavaScript &lt;code&gt;User&lt;/code&gt; example might be written as the following, where we first clone the primitive &lt;code&gt;Object&lt;/code&gt;, aka the prototype chain now contains &lt;code&gt;Object&lt;/code&gt;, then we assign the slot (property) &lt;code&gt;new&lt;/code&gt; to a function taking a single argument &lt;code&gt;name&lt;/code&gt;. Within this function we clone the &lt;code&gt;User&lt;/code&gt; object, so now the prototype chain contains &lt;code&gt;Object, User&lt;/code&gt;, and assign the user’s &lt;code&gt;name&lt;/code&gt; slot to the arg.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;User = Object clone()

User new =: name
  user = User clone()
  user name = name
  user

User toString =:
  self name

tj = User new('tj')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are a few things to note about Luna’s syntax here. The first being that linear white-space &lt;em&gt;is&lt;/em&gt; the slot or “property” delimiter, not &lt;code&gt;.&lt;/code&gt; like many languages. Another thing to note is the function literal syntax, which is simply a &lt;code&gt;:&lt;/code&gt; character followed by optional params and a block.&lt;/p&gt;

&lt;p&gt;Many of you know my thoughts regarding coffeescript, and like I’ve stated in Luna’s Readme, it’s not significant whitespace that I dislike, though it can become nasty very quickly in large doses. It’s of course an honor for any programmer to have a large number of followers, but the reality is that there’s almost nothing original about the language, and instead of striving for something elegant and simple, it just throws more sugar and syntax at the “problems”. The worst part about significant whitespace is that deep nesting, perhaps when defining methods on a class, it’s easy completely lose context, and in a big file/class that can get very annoying, very quickly. The fact that you must specify the receiver for each method reaffirms context within Luna.&lt;/p&gt;

&lt;h3&gt;Function Literals&lt;/h3&gt;

&lt;p&gt;So now that you’ve seen the function literal, you’re probably thinking what the hell… yes, the function literal is a single character, &lt;code&gt;:&lt;/code&gt;.  Consider the following JavaScript function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; var greet = function(user, msg) {
   console.log('hello ' + user + ' ' + msg);
 };
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The equivalent Luna script would be:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;greet =: user msg
  console log('hello ' . user . ' ' . msg)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While this looks like an operator (&lt;code&gt;=:&lt;/code&gt;), it’s simply convention, the following is valid as well:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;greet = : user msg
  console log(...)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You’ll see that the params do not require a comma, this is because Luna, like JavaScript, does not allow defining defaults in the signature. Personally I find this more flexible as params are often shifted and swapped based on their types or arity, and I find the clutter of defaults makes the signature difficult to read, clouding what would otherwise at a glance give you a hint at what the function does.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;greet =: user msg
  console log('hello #{user} #{msg}')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Another example would be a selection of &lt;code&gt;person&lt;/code&gt;’s ferrets older than &lt;code&gt;4&lt;/code&gt;. In Ruby this might look similar to below:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;person.pets.select do |pet|
  pet.species == 'ferret' and pet.age &gt; 4
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and the following CoffeeScript:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;person.pets.filter (pet) -&gt;
  pet.species == 'ferret' and pet.age &gt; 4
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The equivalent canonical Luna would look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;person pets select(: pet
  pet species == 'ferret' &amp;&amp; pet age &gt; 4
)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Because this looks odd, and because function literals are almost always passed as the last argument, there’s a special-case, allowing a function literal to be defined after the closing &lt;code&gt;)&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;person pets select(): pet
  pet species == 'ferret' &amp;&amp; pet age &gt; 4
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Furthering this special sauce, is the optional parens when no arguments are present:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;person pets select: pet
  pet species == 'ferret' &amp;&amp; pet age &gt; 4
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Taking this even further, I have planned to add deferred evaluation support to Luna, though I haven’t decided yet if it will opt-in (most likely) or not. This feature would provide extremely expressive capabilities, consider the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  person pets select(species == 'ferret' &amp;&amp; age &gt; 4)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In most imperative languages this would be evaluated &lt;em&gt;before&lt;/em&gt; calling the function, so the argument would simply consist of a boolean from this expression. However in Luna these expressions could be evaluated by the callee against a given context, or optionally the caller’s context. Another slick example, mapping the the names of users over 20, and joining them.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;users select(age &gt; 20) map(name) join(', ');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Followed by our lovely JavaScript:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;users.filter(function(user){
  return user.age &gt; 20;
}).map(function(user){
  return user.name;
}).join(', ');
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Number Literals&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;one&lt;/em&gt; cool thing about Ruby, is that it allows you to place underscores in numbers, which can dramatically increase readability for larger values, so I’ve added this to Luna as well.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;million = 1_000_000
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Luna also has the typical octal and hexadecimal literals &lt;code&gt;0755&lt;/code&gt; and &lt;code&gt;0xff&lt;/code&gt; etc, but also implements a few that I have not seen before, because these ones come from CSS! Often when programming in JavaScript with timeouts, intervals etc, so Luna has the &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;ms&lt;/code&gt; literals. The following are equivalent:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;2.5s
2500ms
2500
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I’m not a huge fan of getters, visual ambiguity is not an experienced programmers friend, merely a toy that is fun on occasion. That being said &lt;code&gt;s&lt;/code&gt; and &lt;code&gt;ms&lt;/code&gt; in Luna would be valid slot accessors, as &lt;code&gt;.&lt;/code&gt; is not required so they &lt;em&gt;could&lt;/em&gt; be implemented within the language itself if it were to have getters:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Number s =:
   self * 1000
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Another feature which I first encountered in Ruby, but is no doubt in several other languages is the means to represent a character code, as the character itself. In Ruby this takes the form &lt;code&gt;?&lt;char&gt;&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;?&amp;
// =&gt; 38
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In Luna the form is &lt;code&gt;#&lt;char&gt;&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#&amp;
// =&gt; 38
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Operators&lt;/h3&gt;

&lt;p&gt;Luna of course provides the swath of operators we’ve all come to know and love, along with a few others. I try to stay away form “wordy” operators, such as typical aliases “and”, and “or”, etc. The reason for this is while it does read better as english, large bodies of code quickly become nothing more than a soup of words, which I find tough to look at, after all, english is a terrible language, and an even worth programming language.&lt;/p&gt;

&lt;p&gt;Below is the operator precedence table from highest to lowest:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    operator       |  associativity
    ---------------|---------------
    [ ] ( ) sp     |  left
    ++ --          |  right
    ! ~ + -        |  right
    * / %          |  left
    .              |  left
    + -            |  left
    &lt;&lt; &gt;&gt;          |  left
    &lt; &lt;= &gt; &gt;=      |  left
    == !=          |  left
    &amp;              |  left
    ^              |  left
    |              |  left
    &amp;&amp;             |  left
    ||             |  left
    ?:             |  right
    = :=           |  right
    not            |  right
    ,              |  left
    if unless      |  left
    while until
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The low-precedence &lt;code&gt;not&lt;/code&gt; operator is currently the only exception to my rule. This operator is similar to &lt;code&gt;!&lt;/code&gt;, however this low precedence is handy, for example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; User allowed =: realm
   !(banned || blockedFrom(realm))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;may become:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; User allowed =: realm
   not banned || blockedFrom(realm)
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Lexical Analysis&lt;/h2&gt;

&lt;p&gt;Luna’s &lt;a href="https://github.com/visionmedia/luna/blob/master/src/lexer.c" target="_blank"&gt;Lexer&lt;/a&gt; currently consists of a small body of code (~350 LOC), not yet entirely complete. The Lexer’s job is to transform an input string into a normalized stream of tokens. For example Luna’s internals do not care if the operator &lt;code&gt;==&lt;/code&gt; is two or three characters, hell it could be “equal to”, all it cares is that &lt;strong&gt;LUNA_OP_EQ&lt;/strong&gt; is used internally to represent it.&lt;/p&gt;

&lt;h2&gt;Semantic Analysis&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://github.com/visionmedia/luna/blob/master/src/parser.c" target="_blank"&gt;Parser&lt;/a&gt; is in charge of semantics. Using the token stream created by the Lexer, the parser applies rules, and builds an &lt;a href="http://en.wikipedia.org/wiki/Abstract_syntax_tree" target="_blank"&gt;AST&lt;/a&gt;, representing the structure of the program. Luna’s parser is a very small (~900 LOC) hand-coded recursive descent parser.&lt;/p&gt;

&lt;h2&gt;AST Output&lt;/h2&gt;

&lt;p&gt;For debugging Luna is bundled with an &lt;a href="https://github.com/visionmedia/luna/blob/master/src/prettyprint.c" target="_blank"&gt;AST pretty printer&lt;/a&gt;. The function &lt;code&gt;luna_prettyprint(luna_node_t *node)&lt;/code&gt; simply visits each node recursively, incrementing and decrementing the indentation as they are invoked. For example, the following Luna would write a string followed by a LF to &lt;em&gt;stdout&lt;/em&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;puts =: str
  stdout write(str . '\\n')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When &lt;code&gt;luna(1)&lt;/code&gt; is invoked with the &lt;code&gt;--ast&lt;/code&gt; flag, the following is written to &lt;em&gt;stdout&lt;/em&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(= (id puts) (function str 
  (slot
    (id stdout)
    (call
      (id write)
      (. (id str) (string '\n'))))))
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;The Luna VM&lt;/h2&gt;

&lt;p&gt;So we’ve talked about syntax a bit, pretty trivial stuff, let’s get to the fun part! I’ve started implementing the VM in a separate branch, once the VM is usable I’ll begin the code generation, after which both will be merged into master. The code generation or “codegen” phase consists of traversing the AST (potentially several times), performing optimizations (such as precomputing expressions such as “10 / 2” to “5”), generating instructions for our virtual machine.&lt;/p&gt;

&lt;p&gt;The VM is register based, these faux registers allow us to dramatically reduce the number of instructions, lowering ANSI C dispatch loop overhead, thus increasing performance. The instructions consist of an opcode (operation code), and optional operands. For example the &lt;strong&gt;LUNA_OP_ADD&lt;/strong&gt; instruction would add two numbers. For example with a stack machine, the expression &lt;code&gt;b = a + 10&lt;/code&gt; may be expressed with the following sequence of pseudo instructions, pushing arguments onto the stack:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;push 'a'
get
push 3
add
push 'b'
set
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, that’s 6 instructions for a simple expression. In contrast, with our register based VM, we encode the opcode and operands into a 32 bit instruction, as expressed by the following macro used to pack the operands:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#define abc(op, a, b, c) ((LUNA_OP_##op) \
  | ((a) &lt;&lt; 8) \
  | ((b) &lt;&lt; 16) \
  | ((c) &lt;&lt; 24))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With this, our &lt;strong&gt;ADD&lt;/strong&gt; instruction may take the form shown below, where &lt;code&gt;1&lt;/code&gt; is 2nd register (&lt;code&gt;b&lt;/code&gt;), and &lt;code&gt;0&lt;/code&gt; is the first (&lt;code&gt;a&lt;/code&gt;), while &lt;code&gt;250&lt;/code&gt; is the first constant.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;add 1 0 250
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Ditching the overhead required by the “abstract” instructions of a stack machine, dramatically increase our performance. While less, more specific instructions is better, the performance benefit is largely due to the fact that our dispatch loop performs less work.&lt;/p&gt;

&lt;p&gt;The assembly output below is an ANSI C switch statement with &lt;code&gt;-O3&lt;/code&gt;. As you can see gcc create the instruction &lt;code&gt;cmpl&lt;/code&gt; here to compare the length of our switch, in this case &lt;code&gt;12&lt;/code&gt; cases.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;L18:
movl    (%rdx), %eax
addq    $4, %rdx
cmpl    $12, %eax
ja  L18
mov %eax, %eax
movslq  (%rsi,%rax,4),%rax
addq    %rsi, %rax
jmp *%rax
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I haven’t yet decided on the approach I will take, however I would like Luna to remain portable, but that doesn’t mean we cannot special-case. One solution may be to utilize gcc’s “first class labels” to implement direct (or indirect) threading, using &lt;code&gt;&amp;&amp;label&lt;/code&gt; to grab the label’s address. Using this we can perform unconditional jumps, removing the dispatch loop all together. With this technique gcc generates the following for &lt;code&gt;goto **ip++&lt;/code&gt;, much better!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;movq    (%rbx), %rax
addq    $8, %rbx
jmp *%rax
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;The End&lt;/h2&gt;

&lt;p&gt;If you are interested in seeing where the language goes, or trying it out when it becomes usable you can follow the progress on &lt;a href="https://github.com/visionmedia/luna" target="_blank"&gt;Github&lt;/a&gt;. Later I’ll touch on each component in greater detail, but that’s all for now!&lt;/p&gt;</description><link>http://tjholowaychuk.com/post/7517832396</link><guid>http://tjholowaychuk.com/post/7517832396</guid><pubDate>Mon, 11 Jul 2011 19:31:00 -0700</pubDate><category>luna</category><category>c</category></item><item><title>Styling Canvas Drawings With CSS</title><description>&lt;p&gt;Today I implemented an idea to style canvas elements with CSS, I’ve never personally seen this done, and it’s not always practical, but in some cases it would be very handy. For example a text selection api may be comprised of several “elements”, the text, the caret, the selection range rect etc.&lt;/p&gt;

&lt;p&gt;Let’s say by default, our drawing will look the image below:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://f.cl.ly/items/3D3S1o3A0M0C461h1c0g/Screenshot.png" alt="default text selection"/&gt;&lt;/p&gt;

&lt;p&gt;great, looks fine, but what if designers, or even developers for organizational purposes, could tweak this with CSS. Well &lt;em&gt;you can&lt;/em&gt;! Suppose we want to support the following css:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  #editor .text {
    font: 20px helvetica, arial, sans-serif;
  }

  #editor .text .selection {
    background: #DFF3FC;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;First, the API to access such data will look very similar to how we wrote our css, however we provide the css &lt;em&gt;property&lt;/em&gt; as well:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;style('#editor .text .selection', 'font-family')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now for the implementation details, keeping in mind this is not targeting cross-browser support, it’s simply a quick naive implementation, and I should note this does not cover the case of style changes, only initial styles.&lt;/p&gt;

&lt;p&gt;So, firstly we check our cache, to prevent several expensive lookups, here &lt;code&gt;style.cache&lt;/code&gt; is a property of our &lt;code&gt;style&lt;/code&gt; function, if it’s available, we simply return the result.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  function style(selector, prop) {
    var cache = style.cache = style.cache || {}
      , cid = selector + ':' + prop;

    if (cache[cid]) return cache[cid];

    ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next up we will naively split the selector into an array so that we can work with it.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    ...
    var parts = selector.split(/ +/)
      , len = parts.length
      , parent = root = document.createElement('div')
      , child
      , part;

   ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We loop through the “parts”, creating one &lt;code&gt;div&lt;/code&gt; per “part”. This will allow us to see what the computed style is for an element, that otherwise, does not exist! because it’s a canvas. The naive implementation I have here, supports ids and classes only, so we check the first character, and assign the &lt;code&gt;id&lt;/code&gt; and &lt;code&gt;class&lt;/code&gt; respectively.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    ...
    for (var i = 0; i &lt; len; ++i) {
      part = parts[i];
      child = document.createElement('div');
      parent.appendChild(child);
      parent = child;
      if ('#' == part[0]) {
        child.setAttribute('id', part.substr(1));
      } else if ('.' == part[0]) {
        child.setAttribute('class', part.substr(1));
      }
    }
    ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally we append the root element to the &lt;code&gt;document.body&lt;/code&gt; so that the styles are applied, get the computed style for the given &lt;code&gt;prop&lt;/code&gt;, remove the element, and cache/return our value.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  ...
    document.body.appendChild(root);
    var ret = getComputedStyle(child)[prop];
    document.body.removeChild(root);
    return cache[cid] = ret;
  }
&lt;/code&gt;&lt;/pre&gt;

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

&lt;p&gt;Now let’s try it out, first, the css, providing a light yellow background:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  #chalk .text .selection {
    background: #FAFFDF;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In our JavaScript text selection logic, we should provide a JavaScript API for altering styling as well, however for this example, let’s add it directly:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; ctx.fillStyle = style('#editor .text .selection', 'background-color');
 ctx.fill(...)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once applied, we get the following result:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://f.cl.ly/items/1l333L0Y2t251h2Z372u/Screenshot.png" alt="resulting canvas style"/&gt;&lt;/p&gt;

&lt;p&gt;That’s it! you can apply this trick to assign font sizes and families, foreground and background coloring, border colors, anything you can come up with! if anyone comes up with a robust implementation I would love to see it!&lt;/p&gt;

&lt;h2&gt;Full Source&lt;/h2&gt;

&lt;p&gt;Here’s the full implementation:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  function style(selector, prop) {
    var cache = style.cache = style.cache || {}
      , cid = selector + ':' + prop;

    if (cache[cid]) return cache[cid];

    var parts = selector.split(/ +/)
      , len = parts.length
      , parent = root = document.createElement('div')
      , child
      , part;

    for (var i = 0; i &lt; len; ++i) {
      part = parts[i];
      child = document.createElement('div');
      parent.appendChild(child);
      parent = child;
      if ('#' == part[0]) {
        child.setAttribute('id', part.substr(1));
      } else if ('.' == part[0]) {
        child.setAttribute('class', part.substr(1));
      }
    }

    document.body.appendChild(root);
    var ret = getComputedStyle(child)[prop];
    document.body.removeChild(root);
    return cache[cid] = ret;
  }
&lt;/code&gt;&lt;/pre&gt;</description><link>http://tjholowaychuk.com/post/6339741902</link><guid>http://tjholowaychuk.com/post/6339741902</guid><pubDate>Wed, 08 Jun 2011 18:59:00 -0700</pubDate><category>canvas</category><category>javascript</category><category>css</category></item><item><title>node recap #2</title><description>&lt;h2&gt;JSONSelect&lt;/h2&gt;

&lt;p&gt;This first library is &lt;a href="http://jsonselect.org/#tryit" target="_blank"&gt;JSONSelect&lt;/a&gt;. This library allows you to query JSON using css selectors! this seems like an obvious one, but I haven’t personally seen any other implementations.&lt;/p&gt;

&lt;p&gt;It’s not node-specific, however it is JavaScript, so naturally it works with nodejs as well, and over all it’s just a neat concept. Suppose we have the following JSON:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://jsonselect.org/#tryit" target="_blank"&gt;JSONSelect&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  {
      "name": {
          "first": "Lloyd",
          "last": "Hilaiel"
      },
      "favoriteColor": "yellow",
      "languagesSpoken": [
          {
              "language": "Bulgarian",
              "level": "advanced"
          },
          {
              "language": "English",
              "level": "native"
          },
          {
              "language": "Spanish",
              "level": "beginner"
          }
      ],
      "seatingPreference": [
          "window",
          "aisle"
      ],
      "drinkPreference": [
          "whiskey",
          "beer",
          "wine"
      ],
      "weight": 172
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To grab the languages “Bulgarian”, “English” and “Spanish” we could issue the following query:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.languagesSpoken .language
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;cool!&lt;/p&gt;

&lt;h2&gt;node-canvas Image#src=Buffer&lt;/h2&gt;

&lt;p&gt;A few days ago I added support to &lt;a href="https://github.com/LearnBoost/node-canvas" target="_blank"&gt;node-canvas&lt;/a&gt; for &lt;code&gt;Image#src=&lt;/code&gt; to accept &lt;code&gt;Buffer&lt;/code&gt; objects, not only path strings. This needs to be polished, and to increase format support but we can finally apply image data from arbitrary sources:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; var buf = fs.readFileSync(__dirname + '/textures/jpeg');
 var img = new Image;
 img.src = buf;
 ctx.drawImage(img, 0, 0, 400, 400);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When the &lt;code&gt;Buffer&lt;/code&gt; is assigned we can sniff the bytes to determine the format by looking for the “magic number”. For example a PNG starts with the following bytes “89 50 4e 47 0d 0a 1a 0a”, while a JPEG starts with “ff d8”.&lt;/p&gt;

&lt;h2&gt;Express 2.3.8&lt;/h2&gt;

&lt;p&gt;A recent Express refactor moved Connect’s &lt;code&gt;router&lt;/code&gt; middleware into Express so that I could modify it further. This also means that Connect 2.0 will no longer have &lt;code&gt;router&lt;/code&gt;. At first it seemed like a good idea, but routing to specific and it gives people the wrong idea, Connect is meant to be an abstraction layer for higher level frameworks, not to be used directly for application logic. Though this is not true for all cases, such as simply serving static files from a directory or two.&lt;/p&gt;

&lt;p&gt;This &lt;a href="https://github.com/visionmedia/express/commit/80e9ffbf5d23bfd5cf4f3c0ac3e8e230afac8c2d" target="_blank"&gt;commit&lt;/a&gt; consists of a general refactor of the router, while retaining the public API. What does this mean for Express? flexibility! For example now we may query Express to see which (if any) routes match a specific route path:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; app.get('/user/:id');
 // =&gt; [Route]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or routes that match any HTTP method:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; app.all('/user/:id');
 // =&gt; [Route, Route, Route]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;this is functionally equivalent to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; app.lookup.all('/user/:id');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Another alternative is “matching” rather than “lookup”. With &lt;code&gt;app.match.VERB()&lt;/code&gt; we can query Express to see which routes would &lt;em&gt;match&lt;/em&gt; the url passed, for example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;app.match.get('/user/12/edit');
// =&gt; [Route]

app.match.del('/user/12');
// =&gt; [Route]

app.match.all('/user/12?foo=bar');
// =&gt; [Route, Route]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this &lt;a href="https://github.com/visionmedia/express/commit/b1a7310263fe1bc086c4ec085eeb28fe46f3b25c" target="_blank"&gt;commit&lt;/a&gt; I’ve introduced the ability to register logic for defining parameters. Within Express core we only support middleware-style param pre-conditions, for example auto-loading a user for any route that includes the &lt;code&gt;:uid&lt;/code&gt; param:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;app.param('uid', function(req, res, next, uid){
  User.find(uid, function(err, user){
    if (err) return next(err);
    req.user = user;
    next();
  });
});

app.get('/user/:uid, function(){
  // req.user
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;while this is great, some cases can be less verbose, while still expanding to middleware-style functions, this is where &lt;a href="https://github.com/visionmedia/express-params" target="_blank"&gt;express-params&lt;/a&gt; comes in. This plugin extends Express with additional &lt;code&gt;app.param()&lt;/code&gt; logic, for example via &lt;code&gt;RegExp&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  app.param('uid', /^[0-9]+$/);

  app.get('/user/:uid', function(req, res, next){
    var uid = req.params.uid;
    res.send('user ' + uid);
  });

  app.get('/user/:name', function(req, res, next){
    var name = req.params.name;
    res.send('user ' + name);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or via return value, useful for validations and coercion:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  app.param('id', Number);

  app.get('/user/:id', function(req, res, next){
    var id = req.params.id;
    res.send('typeof ' + typeof id + ' ' + id);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Fabric.js&lt;/h2&gt;

&lt;p&gt;This one is not node related, but I wanted to sneak it in :) &lt;a href="http://kangax.github.com/fabric.js/test/demo/" target="_blank"&gt;Fabric&lt;/a&gt; is a slick client-side library providing an object model on top of canvas. It has a built-in drawing mode and path smoothing, SVG parsing and some other really cool features. If you do a lot of canvas work it’s worth checking out, the author did a good job at keeping the source clean and commented, which is &lt;em&gt;very&lt;/em&gt; important when utilizing open source.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://cl.ly/753h/Screenshot.png" alt="fabricjs"/&gt;&lt;/p&gt;

&lt;h2&gt;node-migrate&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/visionmedia/node-migrate" target="_blank"&gt;node-migrate&lt;/a&gt; is a very small abstract migration framework. All it cares about is that you supply an &lt;code&gt;up()&lt;/code&gt; and &lt;code&gt;down()&lt;/code&gt; method, and invoke the callback when your migration is complete:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; exports.up = function(next){
   next();
 };

 exports.down = function(next){
   next();
 };
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;node-migrate comes with the &lt;code&gt;migrate(1)&lt;/code&gt; executable, allowing you to run and create migrations, for example here we add two migrations, populating &lt;code&gt;./migrations/0-add-pets.js&lt;/code&gt; etc.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; $ migrate create add-pets
 $ migrate create add-owners
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The contents of our badass pet adding migration might look something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; var db = require('./db');

  exports.up = function(next){
    db.rpush('pets', 'tobi');
    db.rpush('pets', 'loki');
    db.rpush('pets', 'jane', next);
  };

  exports.down = function(next){
    db.rpop('pets');
    db.rpop('pets', next);
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now all we have to do is run the migrations:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; $ migrate
 up : migrations/0-add-pets.js
 up : migrations/1-add-jane.js
 up : migrations/2-add-owners.js
 up : migrations/3-coolest-pet.js
 migration : complete
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The next time we run &lt;code&gt;migrate&lt;/code&gt;, we’ll see that they have already been completed:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; $ migrate
 migration : complete
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For more information check out the &lt;a href="https://github.com/visionmedia/node-migrate" target="_blank"&gt;GitHub repo&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Move.js&lt;/h2&gt;

&lt;p&gt;The other night I noticed that Apple’s new &lt;a href="http://www.apple.com/imac/" target="_blank"&gt;iMac&lt;/a&gt; page showcased some really slick CSS3 animations, and wanted a really simple and intuitive way to create similar effects so I came up with &lt;a href="https://github.com/visionmedia/move.js" target="_blank"&gt;Move.js&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Below is an example of the API:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;move('#example-13 .box2')
  .set('background-color', 'red')
  .x(500)
  .scale(.5)
  .rotate(60)
    .then()
      .rotate(30)
      .scale(1.5)
      .set('border-radius', 5)
      .set('background-color', 'white')
      .then()
        .set('opacity', 0)
        .pop()
      .pop()
  .end();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It’s still in it’s early stages, and I don’t have much in the way of &lt;a href="http://visionmedia.github.com/move.js/" target="_blank"&gt;documentation&lt;/a&gt; yet however feel free to check out the &lt;a href="http://visionmedia.github.com/move.js/" target="_blank"&gt;site&lt;/a&gt; as well as the examples in the repository.&lt;/p&gt;</description><link>http://tjholowaychuk.com/post/5853435160</link><guid>http://tjholowaychuk.com/post/5853435160</guid><pubDate>Wed, 25 May 2011 19:20:20 -0700</pubDate><category>nodejs</category><category>express</category><category>canvas</category><category>css</category></item><item><title>node recap</title><description>&lt;p&gt;Figured I would whip up a quick blog post during lunch to mention some recent additions / releases to node projects.&lt;/p&gt;

&lt;h2&gt;Express Mongoose&lt;/h2&gt;

&lt;p&gt;Aaron recently released &lt;a href="https://github.com/LearnBoost/express-mongoose" target="_blank"&gt;express-mongoose&lt;/a&gt; which is a fantastic plugin for any of you using &lt;a href="https://github.com/learnboost/mongoose" target="_blank"&gt;Mongoose&lt;/a&gt; allowing you to pass query promises directly to &lt;code&gt;res.render()&lt;/code&gt;, &lt;code&gt;res.partial()&lt;/code&gt;, and `res.send() calls, deferring rendering until complete.&lt;/p&gt;

&lt;p&gt;For example you may currently be nesting as shown below:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var News = db.model('News');
app.get('/dashboard', function (req, res, next) {
  req.user.getLikes(function(err, likes){
    if (err) return next(err);
    News.getLatest(function(err, news){
      if (err) return next(err);
      res.render('dashboard', { likes: likes, latestNews: news });
    });
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;well now you can simply pass the promises, &lt;code&gt;next()&lt;/code&gt;ing exceptions. &lt;em&gt;Much&lt;/em&gt; cleaner!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var News = db.model('News');
app.get('/dashboard', function (req, res) {
  res.render('dashboard', {
      likes: req.user.getLikes()
    , latestNews: News.getLatest()
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Asset&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/visionmedia/asset" target="_blank"&gt;asset&lt;/a&gt; is a tiny but helpful utility written with node to manage assets such a JavaScript or css libraries. You could think of this as the homebrew or bundler of assets.&lt;/p&gt;

&lt;p&gt;Installing assets is extremely simple, and by default will be installed to &lt;code&gt;./public&lt;/code&gt;. For example we can install g.raphael and g.pie to &lt;code&gt;./public/javascripts&lt;/code&gt; which will install the raphael dependency as well:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ asset g.raphael g.pie -o public/javascripts

   install : raphael@1.4.7
   install : g.raphael@0.4.1
   install : g.pie@0.4.1
  download : raphael@1.4.7
  complete : raphael@1.4.7 public/javascripts/raphael.js
  download : g.raphael@0.4.1
  complete : g.raphael@0.4.1 public/javascripts/g.raphael.js
  download : g.pie@0.4.1
  complete : g.pie@0.4.1 public/javascripts/g.pie.js
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Last night I added a tiny patch which adds support for &lt;code&gt;assets.json&lt;/code&gt;, which you can add to your project to list dependencies. For example this file might contain something like below&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;{
     "g.raphael": "0.4.1"
   , "jquery": "1.5.2"
   , "modernizr": "1.7"
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which we can then install with a single command:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ asset

    install : g.raphael@0.4.1
 dependency : raphael@1.4.7
    install : raphael@1.4.7
    install : jquery@1.5.2
    install : modernizr@1.7
   download : jquery@1.5.2
   complete : jquery@1.5.2 public/jquery.js
   download : raphael@1.4.7
   complete : raphael@1.4.7 public/raphael.js
   download : g.raphael@0.4.1
   complete : g.raphael@0.4.1 public/g.raphael.js
   download : modernizr@1.7
   complete : modernizr@1.7 public/modernizr.js
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Cluster updates&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://learnboost.github.com/cluster" target="_blank"&gt;Cluster&lt;/a&gt; is a multi-process server manager for node. Recent additions now allow you to run cluster without a server, which is great for processing job queues! below is an example of how to utilize cluster for such a task:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var cluster = require('../');

var proc = cluster()
  .set('workers', 4)
  .use(cluster.debug())
  .start();

if (proc.isWorker) {
  var id = process.env.CLUSTER_WORKER;
  console.log('  worker #%d started', id);
  setInterval(function(){
    console.log('  processing job from worker #%d', id);
  }, 1000);
} else {
  setTimeout(function(){
    proc.close();
  }, 10000);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;Express Resource&lt;/h2&gt;

&lt;p&gt;For those of you who have been looking for resourceful routing for Express, you may have heard or &lt;a href="https://github.com/visionmedia/express-resource" target="_blank"&gt;express-resource&lt;/a&gt; already, and if not express-resource utilizes regular Express HTTP verb/pathname routing dressed up in an API better suited for resources.&lt;/p&gt;

&lt;p&gt;Installation:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ npm install express-resource
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Below is an example use-case providing actions for forums and their associated threads. By calling &lt;code&gt;forum.add(threads)&lt;/code&gt; routes such as &lt;code&gt;GET /forums/:forum/threads/:thread&lt;/code&gt; and &lt;code&gt;GET /forums/:forum/threads&lt;/code&gt; are mapped to action callbacks. This library also supports auto-loading of resources, providing them as &lt;code&gt;req.forum&lt;/code&gt;, &lt;code&gt;req.thread&lt;/code&gt; etc instead of manually doing this using the values of &lt;code&gt;req.params.forum&lt;/code&gt; or &lt;code&gt;req.params.thread&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  var forum = app.resource('forum', actionsHere)
    , threads = app.resource('thread', actionsHere)

  forum.add(threads);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For more details check out the documentation and tests in the &lt;a href="https://github.com/visionmedia/express-resource" target="_blank"&gt;repo&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;RedisKit&lt;/h2&gt;

&lt;p&gt;Week or so ago I started a small high-level redis library called &lt;a href="https://github.com/visionmedia/rediskit" target="_blank"&gt;rediskit&lt;/a&gt;. I started creating the lower level structures, and will be building up to composite structures and higher level abstractions.&lt;/p&gt;

&lt;p&gt;Install with:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ npm install rediskit
&lt;/code&gt;&lt;/pre&gt;

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

&lt;pre&gt;&lt;code&gt;var list = new List('pets')
  , client = list.client
  , tobi = new Hash('pet:tobi')
  , loki = new Hash('pet:loki')
  , jane = new Hash('pet:jane');

list.rpush('tobi');
list.rpush('jane');
list.rpush('loki');

tobi.set('age', 1);
loki.set('age', 0.5);
jane.set('age', 3);

list.sort.by('pet:*-&gt;age').get('#').get('pet:*-&gt;age').end(function(err, res){
  res.should.eql(['loki', '0.5', 'tobi', '1', 'jane', '3']);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;RedBack&lt;/h2&gt;

&lt;p&gt;I also just saw a tweet about a similar library &lt;a href="https://github.com/chriso/redback" target="_blank"&gt;redback&lt;/a&gt; which is worth checking out if you are a redis fan.&lt;/p&gt;

&lt;p&gt;Installation:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ npm install redback
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Usage:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var redback = require('redback').createClient();

//redback.create&lt;Structure&gt;(key)

var user1 = redback.createHash('user1');
user.set({username:'chris', password:'redisisawesome'}, callback);

var log = redback.createCappedList('log', 1000);
log.push('Log message ...');

var user3 = redback.createSocialGraph(3);
user3.follow(1, callback);
&lt;/code&gt;&lt;/pre&gt;</description><link>http://tjholowaychuk.com/post/5396719370</link><guid>http://tjholowaychuk.com/post/5396719370</guid><pubDate>Wed, 11 May 2011 12:16:00 -0700</pubDate><category>node</category></item><item><title>Stylus vs SASS vs LESS error reporting</title><description>&lt;p&gt;I was curious what &lt;a href="http://sass-lang.com/" target="_blank"&gt;SASS&lt;/a&gt; did as far as error reporting goes, so I tried the following snippet with SASS, LESS as well as &lt;a href="http://learnboost.github.com/stylus/" target="_blank"&gt;Stylus&lt;/a&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;body {
  form input {
    background: foo[fail];
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2&gt;LESS&lt;/h2&gt;

&lt;p&gt;The output from &lt;a href="http://lesscss.org" target="_blank"&gt;LESS&lt;/a&gt; was terrible:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  Syntax Error on line 4
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;note:&lt;/strong&gt; that for LESS I had to tweak the input to “foo[fail’]’;” since it simply consumes the input above&lt;/p&gt;

&lt;h2&gt;SASS&lt;/h2&gt;

&lt;p&gt;The SASS output was not bad, it shows you a tiny chunk of the line for context:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Syntax error: Invalid CSS after "...background: foo": expected ";", was "[fail];"
        on line 4 of standard input
  Use --trace for backtrace.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;note:&lt;/strong&gt; —trace is for the ruby stack trace, not SASS&lt;/p&gt;

&lt;h2&gt;Stylus&lt;/h2&gt;

&lt;p&gt;Then we have Stylus, showing you ~8 lines of context (by default), and even a detailed stack trace including the call sites much like you would find in other languages.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    Error: /tmp/stylus/test.styl:4
       1| 
       2| body {
       3|   form input {
     &gt; 4|     background: foo[fail];
       5|   }
       6| }
       7| 

    cannot perform foo[(fail)]
        at "form input" (/tmp/stylus/test.styl:4)
        at "body" (/tmp/stylus/test.styl:3)
&lt;/code&gt;&lt;/pre&gt;</description><link>http://tjholowaychuk.com/post/5002088731</link><guid>http://tjholowaychuk.com/post/5002088731</guid><pubDate>Wed, 27 Apr 2011 19:35:00 -0700</pubDate><category>nodejs</category><category>stylus</category><category>sass</category><category>less</category></item><item><title>Cluster Live 0.0.1</title><description>&lt;p&gt;&lt;a href="https://github.com/visionmedia/cluster-live" target="_blank"&gt;cluster-live&lt;/a&gt; is a plugin for the &lt;a href="http://learnboost.github.com/cluster" target="_blank"&gt;Cluster&lt;/a&gt; server manager written for node. Cluster is similar to Ruby’s “Unicorn”, however is extremely extensible via a simple plugin system.&lt;/p&gt;

&lt;p&gt;Cluster is built with the Express web framework, Jade for templates, Stylus for CSS, and Socket.IO for communication.&lt;/p&gt;

&lt;p&gt;Below is a screenshot of cluster-live in action. Each worker process is represented by its own &lt;a href="http://smoothiecharts.org/" target="_blank"&gt;smoothie chart&lt;/a&gt;, showing the active connections, total connections, and requests issued to the worker. Connections appear as the &lt;em&gt;blue&lt;/em&gt; line, and requests as &lt;em&gt;gray&lt;/em&gt;, however in the screenshot below a single connection was issued per simulated request.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://cl.ly/65XI/Screenshot.png" alt="full view"/&gt;&lt;/p&gt;

&lt;h2&gt;Administration&lt;/h2&gt;

&lt;p&gt;Currently the only administration task that can be performed from cluster-live is the spawning and removal of workers by pressing the plus or minus buttons in the top-right corner.&lt;/p&gt;

&lt;p&gt;&lt;img src="http://cl.ly/64UQ/Screenshot.png" alt="add or remove workers"/&gt;&lt;/p&gt;

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

&lt;p&gt;Using cluster, and cluster-live is extremely simple, first install the modules:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; $ npm install cluster cluster-live
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then initialize cluster with &lt;code&gt;cluster(server)&lt;/code&gt;, and &lt;code&gt;use()&lt;/code&gt; the &lt;code&gt;stats()&lt;/code&gt; plugin which produces the events that cluster-live uses for reporting, and then of course finally &lt;code&gt;live()&lt;/code&gt; itself.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var http = require('http')
  , cluster = require('cluster')
  , live = require('cluster-live');

var server = http.createServer(function(req, res){
  res.end('Hello World');
});

cluster(server)
  .set('workers', 4)
  .use(cluster.stats({ connections: true, lightRequests: true }))
  .use(live())
  .listen(3000);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By default the cluster-live port is &lt;code&gt;8888&lt;/code&gt;, so fire up &lt;code&gt;3000&lt;/code&gt; and &lt;code&gt;8888&lt;/code&gt; in your browser to see it in action. See the readme for information changing the port, host, or TLS options.&lt;/p&gt;

&lt;h2&gt;Future&lt;/h2&gt;

&lt;p&gt;This plugin is still very much a work-in-progress, but it’s somewhat usable for now although optimizations are still required in order to provide sufficient performance. With minimal profiling it seems that smoothie is currently one of the largest benchmarks but it is something I will have to dig into deeper. It would be great to add some more administration capabilities, as well as more reporting, as many statistics are still currently left out.&lt;/p&gt;

&lt;h2&gt;More Information&lt;/h2&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="https://github.com/visionmedia/cluster-live" target="_blank"&gt;cluster-live&lt;/a&gt; github repo&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/visionmedia/cluster" target="_blank"&gt;cluster&lt;/a&gt; github repo&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learnboost.github.com/cluster" target="_blank"&gt;cluster&lt;/a&gt; documentation&lt;/li&gt;
&lt;/ul&gt;</description><link>http://tjholowaychuk.com/post/4712080732</link><guid>http://tjholowaychuk.com/post/4712080732</guid><pubDate>Sun, 17 Apr 2011 22:34:00 -0700</pubDate><category>nodejs</category><category>cluster</category><category>cluster-live</category></item></channel></rss>

