<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[about:benjie]]></title>
  <link href="http://www.benjiegillam.com/atom.xml" rel="self"/>
  <link href="http://www.benjiegillam.com/"/>
  <updated>2017-08-04T17:48:45+01:00</updated>
  <id>http://www.benjiegillam.com/</id>
  <author>
    <name><![CDATA[Benjie Gillam]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Switching to Yarn Workspaces: an Example]]></title>
    <link href="http://www.benjiegillam.com/2017/08/switching-graphile-to-yarn-workspaces/"/>
    <updated>2017-08-04T09:45:27+01:00</updated>
    <id>http://www.benjiegillam.com/2017/08/switching-graphile-to-yarn-workspaces</id>
    <content type="html"><![CDATA[<p>Wow, it&rsquo;s been a fair while since I last blogged! Well: let&rsquo;s get straight to it!</p>

<p>Yesterday I discovered that <code>yarn</code> (the alternative <code>npm</code> client from Facebook)
had introduced a new feature called <code>yarn workspaces</code> which makes working with
monorepos a lot easier. I&rsquo;m working on a couple of projects recently that use
the monorepo approach (one is a client project in which I introduced the
monorepo approach to share code between React, React-Native and Chrome
extension apps easily; the other is the <a href="https://www.graphile.org/">Graphile OSS
project</a> I&rsquo;m currently working on).  We&rsquo;ve had a few
minor irritations working with <code>lerna</code>, the most notable of which is that when
we want to install a new dependency we can&rsquo;t just <code>yarn install</code> it - instead
we have to add it to package.json and run <code>lerna bootstrap</code> again. At first
this wasn&rsquo;t so bad, but it quickly becomes a chore!</p>

<p>Upon reading <a href="https://yarnpkg.com/blog/2017/08/02/introducing-workspaces/">the
announcement</a> it
immediately sounded like yarn workspaces would solve this problem (and more!)
so I decided to get on board!</p>

<blockquote class="twitter-tweet" data-lang="en"><p lang="en" dir="ltr">This is awesome; I&#39;m going to try this out with Graphile tomorrow! <a href="https://t.co/213mvRmGbX">https://t.co/213mvRmGbX</a></p>&mdash; Benjie Gillam (@Benjie) <a href="https://twitter.com/Benjie/status/893246353660411904">August 3, 2017</a></blockquote>


<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>


<p>So, without further ado, here&rsquo;s how I switched
<a href="https://github.com/graphile/graphile-build">graphile-build</a> to yarn workspaces (still with
lerna):</p>

<!-- more -->


<p>First we must install v2.0.0 of lerna and v0.28+ of yarn; currently <code>yarn@latest</code> is 0.27.5, so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>npm i -g lerna@latest yarn@rc
</span></code></pre></td></tr></table></div></figure>


<p>Now, since yarn workspaces is experimental we must manually enable this feature:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>yarn config <span class="nb">set </span>workspaces-experimental <span class="nb">true</span>
</span></code></pre></td></tr></table></div></figure>


<p>We must also enable <code>useWorkspaces</code> in our <code>lerna.json</code> - open it up and add the following:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'>  <span class="s2">&quot;useWorkspaces&quot;</span><span class="err">:</span> <span class="kc">true</span><span class="err">,</span>
</span></code></pre></td></tr></table></div></figure>


<p>We also need to declare that this project uses yarn workspaces (which is
independent of Lerna), so in <code>package.json</code> ensure that <code>private</code> is set to
true, and add the <code>workspaces</code> key:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'>  <span class="s2">&quot;private&quot;</span><span class="err">:</span> <span class="kc">true</span><span class="err">,</span>
</span><span class='line'>  <span class="s2">&quot;workspaces&quot;</span><span class="err">:</span> <span class="p">[</span>
</span><span class='line'>    <span class="s2">&quot;packages/*&quot;</span>
</span><span class='line'>  <span class="p">]</span><span class="err">,</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we&rsquo;re pretty much ready to go, so let&rsquo;s clear out the old stuff and start afresh:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>rm -Rf node_modules packages/*/node_modules
</span><span class='line'>rm -Rf yarn.lock packages/*/yarn.lock
</span><span class='line'>yarn
</span><span class='line'>lerna bootstrap
</span></code></pre></td></tr></table></div></figure>


<p>And that&rsquo;s it! Enjoy your new workspaces-enabled monorepo!</p>

<p>You can see the changes I made in this pull request:</p>

<p><a href="https://github.com/graphile/graphile-build/pull/36">https://github.com/graphile/graphile-build/pull/36</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Long Live CoffeeScript and Long Live ES6]]></title>
    <link href="http://www.benjiegillam.com/2015/02/long-live-coffeescript-and-es6/"/>
    <updated>2015-02-12T17:15:00+00:00</updated>
    <id>http://www.benjiegillam.com/2015/02/long-live-coffeescript-and-es6</id>
    <content type="html"><![CDATA[<p>Clearly ES6 is a huge improvement over ES5, and tools like <a href="https://6to5.org/">6to5</a> allow us to use these cool features now. I was reading <a href="http://robots.thoughtbot.com/replace-coffeescript-with-es6">Replace CoffeeScript with ES6</a> by <a href="https://twitter.com/blakewilliams__">Blake Williams</a> and thought it was a great summary of how ES6 solves many of the same problems that CoffeeScript solves; however I&rsquo;d like to comment on a few of Blake&rsquo;s points and talk about why I&rsquo;ll be sticking with CoffeeScript.</p>

<h2>Classes</h2>

<p>Classes in ES6 (like many of the syntax changes in ES6) are very similar to the CoffeeScript equivalent. To support browsers that are not fully ES5 compliant (e.g. IE8-), however, we still can&rsquo;t really use getters/setters, so ignoring these the comparison is:</p>

<!-- more -->




<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='coffee'><span class='line'><span class="k">class</span> <span class="nx">Person</span>
</span><span class='line'>  <span class="nv">constructor: </span><span class="nf">(@firstName, @lastName) -&gt;</span>
</span><span class='line'>
</span><span class='line'>  <span class="nv">name: </span><span class="nf">-&gt;</span>
</span><span class='line'>    <span class="s">&quot;</span><span class="si">#{</span><span class="nx">@firstName</span><span class="si">}</span><span class="s"> </span><span class="si">#{</span><span class="nx">@lastName</span><span class="si">}</span><span class="s">&quot;</span>
</span><span class='line'>
</span><span class='line'>  <span class="nv">setName: </span><span class="nf">(name) -&gt;</span>
</span><span class='line'>    <span class="p">[</span><span class="nx">@firstName</span><span class="p">,</span> <span class="nx">@lastName</span><span class="p">]</span> <span class="o">=</span> <span class="nx">name</span><span class="p">.</span><span class="nx">split</span> <span class="s">&quot; &quot;</span>
</span><span class='line'>
</span><span class='line'>  <span class="vi">@defaultName: </span><span class="nf">-&gt;</span>
</span><span class='line'>    <span class="s">&quot;Unidentified Person&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">blake = </span><span class="k">new</span> <span class="nx">Person</span> <span class="s">&quot;Blake&quot;</span><span class="p">,</span> <span class="s">&quot;Williams&quot;</span>
</span><span class='line'><span class="nx">blake</span><span class="p">.</span><span class="nx">setName</span> <span class="s">&quot;Blake Anderson&quot;</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="nx">blake</span><span class="p">.</span><span class="nx">name</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>vs</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kr">class</span> <span class="nx">Person</span> <span class="p">{</span>
</span><span class='line'>  <span class="nx">constructor</span><span class="p">(</span><span class="nx">firstName</span><span class="p">,</span> <span class="nx">lastName</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">firstName</span> <span class="o">=</span> <span class="nx">firstName</span><span class="p">;</span>
</span><span class='line'>    <span class="k">this</span><span class="p">.</span><span class="nx">lastName</span> <span class="o">=</span> <span class="nx">lastName</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">name</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="err">`</span><span class="nx">$</span><span class="p">{</span><span class="k">this</span><span class="p">.</span><span class="nx">firstName</span><span class="p">}</span> <span class="nx">$</span><span class="p">{</span><span class="k">this</span><span class="p">.</span><span class="nx">lastName</span><span class="p">}</span><span class="err">`</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="nx">setName</span><span class="p">(</span><span class="nx">name</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">firstName</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">lastName</span><span class="p">]</span> <span class="o">=</span> <span class="nx">name</span><span class="p">.</span><span class="nx">split</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">);</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="kr">static</span> <span class="nx">defaultName</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="s2">&quot;Unidentified Person&quot;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="kd">var</span> <span class="nx">blake</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Person</span><span class="p">(</span><span class="s2">&quot;Blake&quot;</span><span class="p">,</span> <span class="s2">&quot;Williams&quot;</span><span class="p">);</span>
</span><span class='line'><span class="nx">blake</span><span class="p">.</span><span class="nx">setName</span><span class="p">(</span><span class="s2">&quot;Blake Anderson&quot;</span><span class="p">);</span>
</span><span class='line'><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">blake</span><span class="p">.</span><span class="nx">name</span><span class="p">());</span>
</span></code></pre></td></tr></table></div></figure>


<p>I definitely like the way this is headed - no need for commas, no need to write <code>function</code> all over the place; but I&rsquo;m in the &ldquo;write less code&rdquo; camp, so I prefer the former. However this is really a matter of taste so if you&rsquo;re happy with typing <code>this.</code> instead of <code>@</code> and adding in all the extra curly brackets it definitely backs up Blake&rsquo;s statement that ES6 is a viable alternative.</p>

<p>That said, I much prefer CoffeeScript&rsquo;s implementation of <code>super</code>, though I can see why ES6 went the way they did allowing you to call a different method on the object&rsquo;s super.</p>

<h2>Interpolation</h2>

<p>Clearly ES6&rsquo;s and CoffeeScript&rsquo;s string interpolations are very similar; CoffeeScript interpolates for normal <code>"strings"</code>, whereas ES6 uses <code>`backtick escaped strings`</code> (which are annoying to write in Markdown, by the way). CoffeeScript uses <code>#{var}</code> whereas ES6 uses <code>${var}</code>. All much of a muchness at this point.</p>

<p>Where the difference really stands out is in the handling of whitespace - ES6 (or at least the 6to5 tool) includes all the whitespace between the <code>`</code>s (including newlines and indentation), whereas CoffeeScript either joins with a single space in the case of simple <code>"</code> strings or preserves all whitespace <em>accounting for indentation level</em> in the case of <code>"""</code> block strings. To my mind both of these behaviours are desirable, whereas ES6&rsquo;s is not, take for example:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">function</span> <span class="nx">foo</span><span class="p">(</span><span class="nx">bar</span><span class="p">,</span> <span class="nx">val</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="nx">bar</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="kd">var</span> <span class="nx">str</span> <span class="o">=</span> <span class="err">`</span><span class="nx">This</span> <span class="nx">is</span> <span class="nx">quite</span> <span class="nx">a</span> <span class="kr">long</span> <span class="nx">first</span> <span class="nx">line</span>
</span><span class='line'>        <span class="nx">so</span> <span class="nx">I</span> <span class="nx">wrap</span> <span class="nx">it</span> <span class="nx">to</span> <span class="nx">a</span> <span class="nx">second</span> <span class="nx">line</span> <span class="nx">and</span> <span class="nx">then</span>
</span><span class='line'>        <span class="nx">append</span> <span class="nx">the</span> <span class="nx">value</span> <span class="nx">$</span><span class="p">{</span><span class="nx">val</span><span class="p">}</span><span class="err">`</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">})();</span>
</span></code></pre></td></tr></table></div></figure>


<p>The output from passing this through <a href="https://6to5.org/repl/#?experimental=true&amp;playground=true&amp;evaluate=true&amp;loose=false">6to5&rsquo;s REPL</a> is:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">str</span> <span class="o">=</span> <span class="s2">&quot;This is quite a long first line\n        so I wrap it to a second line and then\n        append the value &quot;</span> <span class="o">+</span> <span class="nx">val</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>CoffeeScript equivalents:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='coffee'><span class='line'><span class="nx">do</span> <span class="nf">-&gt;</span>
</span><span class='line'>  <span class="nv">foo = </span><span class="nf">(bar, val) -&gt;</span>
</span><span class='line'>    <span class="k">if</span> <span class="nx">bar</span>
</span><span class='line'>      <span class="nv">str = </span><span class="s">&quot;This is quite a long first line</span>
</span><span class='line'><span class="s">        so I wrap it to a second line and then</span>
</span><span class='line'><span class="s">        append the value </span><span class="si">#{</span><span class="nx">val</span><span class="si">}</span><span class="s">&quot;</span><span class="p">;</span>
</span><span class='line'>      <span class="nv">str2 =</span>
</span><span class='line'>        <span class="s">&quot;&quot;&quot;</span>
</span><span class='line'><span class="s">        This is quite a long first line</span>
</span><span class='line'><span class="s">        so I wrap it to a second line and then</span>
</span><span class='line'><span class="s">        append the value </span><span class="si">#{</span><span class="nx">val</span><span class="si">}</span><span class="s"></span>
</span><span class='line'><span class="s">        &quot;&quot;&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>produce</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">str</span> <span class="o">=</span> <span class="s2">&quot;This is quite a long first line so I wrap it to a second line and then append the value &quot;</span> <span class="o">+</span> <span class="nx">val</span><span class="p">;</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">str2</span> <span class="o">=</span> <span class="s2">&quot;This is quite a long first line\nso I wrap it to a second line and then\nappend the value &quot;</span> <span class="o">+</span> <span class="nx">val</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<p>I can&rsquo;t think of a situation where I&rsquo;d prefer 6to5&rsquo;s implementation.</p>

<h2>Fat Arrows, Default Arguments</h2>

<p>Brilliant additions to the JS syntax, these behave the same as CoffeeScript&rsquo;s but with ever-so-slightly different syntax rules.</p>

<h2>Splats</h2>

<p>Another brilliant addition, but I find splats can be quite powerful in the middle of an argument list, particularly in Node.js-style callback situations so that the callback is automatically popped off the end. For example:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='coffee'><span class='line'><span class="c1"># Splat dereferencing</span>
</span><span class='line'><span class="nv">names = </span><span class="p">[</span><span class="s">&quot;Alice&quot;</span><span class="p">,</span> <span class="s">&quot;Belinda&quot;</span><span class="p">,</span> <span class="s">&quot;Catherine&quot;</span><span class="p">,</span> <span class="s">&quot;Davidson&quot;</span><span class="p">]</span>
</span><span class='line'><span class="p">[</span><span class="nx">firstName</span><span class="p">,</span> <span class="nx">middleNames</span><span class="p">...,</span> <span class="nx">lastName</span><span class="p">]</span> <span class="o">=</span> <span class="nx">names</span>
</span><span class='line'>
</span><span class='line'><span class="c1"># Splat function args preserving callback</span>
</span><span class='line'><span class="nv">foo = </span><span class="nf">(fn, args..., callback) -&gt;</span>
</span><span class='line'>  <span class="nv">results = </span><span class="p">(</span><span class="nx">fn</span> <span class="nx">arg</span> <span class="k">for</span> <span class="nx">arg</span> <span class="k">in</span> <span class="nx">args</span><span class="p">)</span>
</span><span class='line'>  <span class="nx">process</span><span class="p">.</span><span class="nx">nextTick</span> <span class="nf">-&gt;</span>
</span><span class='line'>    <span class="nx">callback</span> <span class="kc">null</span><span class="p">,</span> <span class="nx">results</span>
</span><span class='line'>
</span><span class='line'><span class="nv">double = </span><span class="nf">(a) -&gt;</span> <span class="nx">a</span> <span class="o">*</span> <span class="mi">2</span>
</span><span class='line'>
</span><span class='line'><span class="nx">foo</span> <span class="nx">double</span><span class="p">,</span> <span class="mi">80</span><span class="p">,</span> <span class="mi">60</span><span class="p">,</span> <span class="mi">40</span><span class="p">,</span> <span class="nf">(err, results) -&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="nx">results</span>
</span></code></pre></td></tr></table></div></figure>


<p>Sadly ES6 only allows splats at the end, requiring you to then manually <code>pop()</code> the callback (or lastName), making your code longer and making you choose more variables names (and we all hate picking variable names right?):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">var</span> <span class="nx">names</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;Alice&quot;</span><span class="p">,</span> <span class="s2">&quot;Belinda&quot;</span><span class="p">,</span> <span class="s2">&quot;Catherine&quot;</span><span class="p">,</span> <span class="s2">&quot;Davidson&quot;</span><span class="p">];</span>
</span><span class='line'><span class="kd">var</span> <span class="nx">firstName</span><span class="p">,</span> <span class="nx">middleNames</span><span class="p">,</span> <span class="nx">lastName</span><span class="p">,</span> <span class="nx">rest</span><span class="p">;</span>
</span><span class='line'><span class="p">[</span><span class="nx">firstName</span><span class="p">,</span> <span class="p">...</span><span class="nx">rest</span><span class="p">]</span> <span class="o">=</span> <span class="nx">names</span><span class="p">;</span>
</span><span class='line'><span class="nx">lastName</span> <span class="o">=</span> <span class="nx">rest</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>
</span><span class='line'><span class="nx">middleNames</span> <span class="o">=</span> <span class="nx">rest</span><span class="p">;</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Structuring/Destructuring</h2>

<p>I must say I do like that ES6 lets you leave true blanks when doing <code>var [first, , last] = [1, 2, 3]</code> but using an underscore or similar is a one character workaround.</p>

<p>ES6 does object de/structuring pretty much the same as CoffeeScript (<code>var {a, b} = {a: 1, c:3}</code>, <code>var {foo: a, bar: b} = {foo: 1, baz: 3}</code>  and <code>var c = {a, b}</code>) however there&rsquo;s a slight circumstance where CoffeeScript does it better: when referencing properties off of the current object, e.g. <code>c = {@a, @b}</code> (<code>var c = {a: this.a, b: this.b}</code>).</p>

<h2>Things ES6 has that I wish CoffeeScript had</h2>

<p>It wouldn&rsquo;t be fair to try and paint CoffeeScript as a perfect language - it certainly is not without its faults. Here&rsquo;s a list of features from ES6 (and even ES3) that I miss in CoffeeScript:</p>

<ul>
<li>The ternary operator <code>a ? b : c</code> (<code>if a then b else c</code> is too verbose for my taste; that said there&rsquo;s no way I&rsquo;d give up the <code>?</code> operator!)</li>
<li>Computed (dynamic) property names, <code>{[getKey()]: getValue()}</code> (see the links in <a href="http://stackoverflow.com/a/13998890">this StackOverflow answer</a> for some <a href="https://github.com/jashkenas/coffeescript/issues/786">interesting</a> <a href="https://github.com/jashkenas/coffeescript/issues/1731">history</a>)</li>
</ul>


<h2>Conclusion</h2>

<p>All in all ES6 is a great leap forward for JavaScript and my huge thanks to all the developers who have made this possible. Not mentioned above are many of the features that ES6 has added that don&rsquo;t involve syntax changes and hence CoffeeScript can use without changes, such as Proxies, WeakMaps and much more. (We even have <code>yield</code> now too.)</p>

<p>I still prefer CoffeeScript&rsquo;s syntax and find it very readable yet concise which significantly boosts my productivity. I would also find it hard to give up all CoffeeScript&rsquo;s various syntax sugars, such as: <code>object?.property</code> which doesn&rsquo;t throw if object is null/undefined; <code>a ?= b</code>, <code>a ||= b</code>, etc.; implicit returns; <code>unless</code>; improved <code>switch</code>; ranges <code>[a..b]</code>; array/string slicing with ranges <code>arr[2..6]</code>; <code>for own k, v of obj</code>; chained comparisons <code>a &lt; b &lt; c</code>; block RegExps; and many more!</p>

<h3>Long live CoffeeScript and long live ES6!</h3>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Quantum JavaScript?]]></title>
    <link href="http://www.benjiegillam.com/2013/06/quantum-javascript/"/>
    <updated>2013-06-06T09:31:00+01:00</updated>
    <id>http://www.benjiegillam.com/2013/06/quantum-javascript</id>
    <content type="html"><![CDATA[<p><strong>TL;DR</strong>: In Chrome or Safari&rsquo;s JavaScript console (or in <code>jsc</code>, but not <code>node</code>), run the following:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="o">&gt;</span> <span class="p">{}</span> <span class="o">+</span> <span class="p">{}</span>
</span><span class='line'><span class="kc">NaN</span>
</span><span class='line'><span class="o">&gt;</span> <span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="p">{}</span> <span class="o">+</span> <span class="p">{};</span> <span class="nx">a</span>
</span><span class='line'><span class="s1">&#39;[object Object][object Object]&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>a) Why is the first result wrong?<br/>
b) Why does storing it to a variable (observing it) change it?</p>

<p>(The answers are below.)</p>

<hr />

<p>My friend <a href="http://www.strangeparty.com/">Anton</a> pointed out to me <a href="https://www.destroyallsoftware.com/talks/wat">this short &lsquo;WAT&rsquo; talk</a> by <a href="https://www.destroyallsoftware.com/">Gary
Bernhardt</a> from 2012. It did something that confused me somewhat.
Here&rsquo;s what Gary&rsquo;s example was:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">$</span> <span class="nx">jsc</span>
</span><span class='line'><span class="o">&gt;</span> <span class="p">[]</span> <span class="o">+</span> <span class="p">[]</span> <span class="c1">// 1</span>
</span><span class='line'>
</span><span class='line'><span class="o">&gt;</span> <span class="p">[]</span> <span class="o">+</span> <span class="p">{}</span> <span class="c1">// 2</span>
</span><span class='line'><span class="p">[</span><span class="nx">object</span> <span class="nb">Object</span><span class="p">]</span>
</span><span class='line'><span class="o">&gt;</span> <span class="p">{}</span> <span class="o">+</span> <span class="p">[]</span> <span class="c1">// 3</span>
</span><span class='line'><span class="mi">0</span>
</span><span class='line'><span class="o">&gt;</span> <span class="p">{}</span> <span class="o">+</span> <span class="p">{}</span> <span class="c1">// 4</span>
</span><span class='line'><span class="kc">NaN</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now 1 and 2 are fine, but 3 and 4 immediately went against what I was
expecting - it truly was a &lsquo;wat&rsquo; moment. I noticed was he was running
this in JavaScriptCore, let&rsquo;s see what Node.js makes of it:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nx">$</span> <span class="nx">node</span>
</span><span class='line'><span class="o">&gt;</span> <span class="p">[]</span> <span class="o">+</span> <span class="p">[]</span> <span class="c1">// 1</span>
</span><span class='line'><span class="s1">&#39;&#39;</span>
</span><span class='line'><span class="o">&gt;</span> <span class="p">[]</span> <span class="o">+</span> <span class="p">{}</span> <span class="c1">// 2</span>
</span><span class='line'><span class="s1">&#39;[object Object]&#39;</span>
</span><span class='line'><span class="o">&gt;</span> <span class="p">{}</span> <span class="o">+</span> <span class="p">[]</span> <span class="c1">// 3</span>
</span><span class='line'><span class="s1">&#39;[object Object]&#39;</span>
</span><span class='line'><span class="o">&gt;</span> <span class="p">{}</span> <span class="o">+</span> <span class="p">{}</span> <span class="c1">// 4</span>
</span><span class='line'><span class="s1">&#39;[object Object][object Object]&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Node.js seems to be doing exactly what I&rsquo;d expect - using the
<code>+</code> as string concatenation (since the leading argument is not numeric).
So it&rsquo;s calling <code>.toString()</code> on everything to produce the results you
see above (an Array&rsquo;s <code>toString</code> is effectively <code>return this.join(",")</code>,
and since it&rsquo;s empty it just returns the empty string).</p>

<p>So what&rsquo;s going on?! Let&rsquo;s try V8 (the engine behind Node.js) in the
browser (Chrome):</p>

<!-- more -->




<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="o">&gt;</span> <span class="p">{}</span> <span class="o">+</span> <span class="p">[]</span> <span class="c1">// 3</span>
</span><span class='line'><span class="mi">0</span>
</span><span class='line'><span class="o">&gt;</span> <span class="p">{}</span> <span class="o">+</span> <span class="p">{}</span> <span class="c1">// 4</span>
</span><span class='line'><span class="kc">NaN</span>
</span></code></pre></td></tr></table></div></figure>


<p>So&hellip; V8 in the browser agrees with JSC, not Node, and is behaving
in this strange fashion. But wait!</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="o">&gt;</span> <span class="kd">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="p">{}</span> <span class="o">+</span> <span class="p">[];</span>
</span><span class='line'><span class="kc">undefined</span>
</span><span class='line'><span class="o">&gt;</span> <span class="nx">a</span>
</span><span class='line'><span class="s2">&quot;[object Object]&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>In the words of Gary: WAT?! By storing the value to a variable I&rsquo;ve
changed it&rsquo;s value? This immediately reminds me of the Heisenberg
Uncertainty Principle - that by observing something you implicitly
change what it is; only in this case the observation is effectively
storing it to a variable. Quantum JavaScript?</p>

<p>If you want to figure this out yourself, take a while to ponder this now
before reading on&hellip;</p>

<h2>Read on for the spoiler</h2>

<p>Digging further, I realise that I could reproduce this by calling <code>eval</code>
(which is basically what the E in REPL stands for). This allowed me to
reproduce the same thing in Node:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="o">&gt;</span> <span class="nb">eval</span><span class="p">(</span><span class="s2">&quot;[] + []&quot;</span><span class="p">)</span>
</span><span class='line'><span class="s1">&#39;&#39;</span>
</span><span class='line'><span class="o">&gt;</span> <span class="nb">eval</span><span class="p">(</span><span class="s2">&quot;[] + {}&quot;</span><span class="p">)</span>
</span><span class='line'><span class="s1">&#39;[object Object]&#39;</span>
</span><span class='line'><span class="o">&gt;</span> <span class="nb">eval</span><span class="p">(</span><span class="s2">&quot;{} + []&quot;</span><span class="p">)</span>
</span><span class='line'><span class="mi">0</span>
</span><span class='line'><span class="o">&gt;</span> <span class="nb">eval</span><span class="p">(</span><span class="s2">&quot;{} + {}&quot;</span><span class="p">)</span>
</span><span class='line'><span class="kc">NaN</span>
</span></code></pre></td></tr></table></div></figure>


<p>At this pointed I cheated: I hopped on over to #javascript on
irc.freenode.net and asked there. After some pondering, ckknight
figured it out (with a little help from DDR_ and Havvy). Here&rsquo;s the
highlights:</p>

<pre><code>DDR_     : Benjie: I think that {} is interpreted specially somehow in the console.
Havvy    : &gt;&gt; ({}) + ({})
ecmabot  : Havvy: (string) '[object Object][object Object]'
Havvy    : &gt;&gt; {}
ecmabot  : Havvy: undefined
ckknight : I get it
DDR_     : I'm guessing it's ambiguous or something.
ckknight : {} + {} turns into {}; (+{})
ckknight : since the first is interpreted as a block statement
ckknight : and then you have a unary + and an object
</code></pre>

<p>You can read the full transcript at the bottom of this post.</p>

<p>ckknight figured out that the first <code>{}</code> was being interpretted as a
code block, rather than an object literal, and JavaScript&rsquo;s automatic
semicolon insertion (ASI) was taking over and changing the code to be
interpretted like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="o">&gt;</span> <span class="p">{};</span> <span class="o">+</span><span class="p">[]</span> <span class="c1">// 3</span>
</span><span class='line'><span class="mi">0</span>
</span><span class='line'><span class="o">&gt;</span> <span class="p">{};</span> <span class="o">+</span><span class="p">{}</span> <span class="c1">// 4</span>
</span><span class='line'><span class="kc">NaN</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>{};</code> is ignored (empty code block), and the leading <code>+</code> coerces
the values to a number, which is equivalent to:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="o">&gt;</span> <span class="nb">Number</span><span class="p">([])</span> <span class="c1">// 3</span>
</span><span class='line'><span class="mi">0</span>
</span><span class='line'><span class="o">&gt;</span> <span class="nb">Number</span><span class="p">([].</span><span class="nx">toString</span><span class="p">())</span>
</span><span class='line'><span class="mi">0</span>
</span><span class='line'><span class="o">&gt;</span> <span class="nb">Number</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
</span><span class='line'><span class="mi">0</span>
</span><span class='line'><span class="o">&gt;</span> <span class="nb">Number</span><span class="p">({})</span> <span class="c1">// 4</span>
</span><span class='line'><span class="kc">NaN</span>
</span><span class='line'><span class="o">&gt;</span> <span class="nb">Number</span><span class="p">({}.</span><span class="nx">toString</span><span class="p">())</span>
</span><span class='line'><span class="kc">NaN</span>
</span><span class='line'><span class="o">&gt;</span> <span class="nb">Number</span><span class="p">(</span><span class="s2">&quot;[object Object]&quot;</span><span class="p">)</span>
</span><span class='line'><span class="kc">NaN</span>
</span></code></pre></td></tr></table></div></figure>


<p><em>(Aside: to expand on the meaning of calling <code>Number</code> above, read about
<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.1">Number called as a function</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.3">ToNumber</a>, <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.1">ToPrimitive</a>,
and finally <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.8">DefaultValue</a>.)</em></p>

<p>So - mystery solved: storing to a variable gives the JavaScript
interpretter enough context to interpret the first <code>{}</code> as an object
literal and not a code block.</p>

<p>And I&rsquo;m guessing that Node&rsquo;s REPL, rather than calling plain <code>eval("{} +
{}")</code>, does something a little more complex - perhaps like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="nb">eval</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="s2">&quot;(function(){return   {} + {}   }).call(this)&quot;</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>thereby (coincidentally?) side-stepping the issue.</p>

<h2>IRC Transcript</h2>

<pre><code>Benjie   : When I type `{} + {}` into the javascript console (JSC or
         &gt; Chrome inspector or Safari inspector; note: not node.js REPL) I get
         &gt; `NaN` - why is this not the same as when I type `var a = {} + {}; a`
         &gt; (which gives me `[object Object][object Object]`?
Benjie   : i.e. why does storing it to a variable somehow change it's
         &gt; value. (Or more specifically, at a guess, how does the javascript
         &gt; console coerce values and why isn't it the same as normal coercion?)
ckknight : Benjie: seems like an implementation flaw, like a bug seeped in
DDR_     : Benjie: I think that {} is interpreted specially somehow in the console.
Havvy    : &gt;&gt; var a = {} + {}; a
ecmabot  : Havvy: (string) '[object Object][object Object]'
Havvy    : &gt;&gt; {} + {}
ecmabot  : Havvy: (number) NaN
Havvy    : &gt;&gt; ({}) + ({})
ecmabot  : Havvy: (string) '[object Object][object Object]'
DDR_     : The second way is the correct (sob) way.
ckknight : &gt;&gt; {} + ({})
ecmabot  : ckknight: (number) NaN
ckknight : &gt;&gt; ({}) + {}
ecmabot  : ckknight: (string) '[object Object][object Object]'
ckknight : qwies
ckknight : weird*
Benjie   : ^_^
DDR_     : {} is undefined.
Havvy    : &gt;&gt; {}
ecmabot  : Havvy: undefined
DDR_     : In the console.
DDR_     : Heh, yes.
Havvy    : &gt;&gt; undefined + {}
ecmabot  : Havvy: (string) 'undefined[object Object]'
ckknight : DDR_: as an expression, yes
Havvy    : &gt;&gt; undefined + undefined
ecmabot  : Havvy: (number) NaN
ckknight : err
ckknight : as a statement, yes
ckknight : oh
ckknight : I get it
DDR_     : I'm guessing it's ambiguous or something.
DDR_     : I don't. :P
ckknight : {} + {} turns into {}; (+{})
Havvy    : {}; (+{})
Havvy    : &gt;&gt; {}; (+{})
ecmabot  : Havvy: (number) NaN
ckknight : &gt;&gt; {} + {}
ecmabot  : ckknight: (number) NaN
Havvy    : ckknight: Yeah, that could very well be it.
ckknight : since the first is interpreted as a block statement
ckknight : and then you have a unary + and an object
DDR_     : Ah.
Benjie   : ckknight: so the first {} is interpretted as a block
         &gt; statement. Fascinating, I'm impressed you reached that conclusion so
         &gt; quickly!
</code></pre>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My Favourite Git Commands V: Yet More]]></title>
    <link href="http://www.benjiegillam.com/2013/05/favourite-git-commands-v/"/>
    <updated>2013-05-01T16:25:00+01:00</updated>
    <id>http://www.benjiegillam.com/2013/05/favourite-git-commands-v</id>
    <content type="html"><![CDATA[<p><em>This is part 5 in my <a href="http://www.benjiegillam.com/categories/favourite-git-commands/">Favourite Git
Commands</a> series.</em></p>

<h2>Aliases</h2>

<h3><code>git lg</code></h3>

<p>Covered in <a href="http://www.benjiegillam.com/2013/02/favourite-git-commands-ii/">post 2</a>:</p>

<p><img src="http://www.benjiegillam.com/files/img/git-lg.png" title="`git lg` command output" ></p>

<p>To add it:</p>

<pre><code>git config alias.lg log --graph --pretty=format:'%Cgreen%h%Creset -%Creset %s%C(yellow)%d %Cblue(%aN, %cr)%Creset' --abbrev-commit --date=relative
</code></pre>

<h3><code>git heir</code></h3>

<p>Also covered in <a href="http://www.benjiegillam.com/2013/02/favourite-git-commands-ii/">post 2</a>:</p>

<p>Tells you the hierarchy of your various git branches.</p>

<p><img src="http://www.benjiegillam.com/files/img/git-heir.png" title="`git heir` command output" ></p>

<!-- more -->


<p>To add it:</p>

<pre><code>git config alias.heir log --all --graph --decorate --oneline --simplify-by-decoration
</code></pre>

<h3>Shell aliases</h3>

<p>For those commands you type frequently, it&rsquo;s nice to have your shell
alias them for you:</p>

<pre><code>alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias gds='git diff --staged'
alias go='git checkout '

alias got='git '
alias get='git '
</code></pre>

<h2>Commands to use sparingly</h2>

<p>These are commands that make it very easy to get messy code all up in
your git history. And no one wants messy git history.</p>

<h3><code>git commit -a</code></h3>

<p>Are you sure you want to commit every single line? Really really sure?
If you&rsquo;re going to do this always <strong>always</strong> do a <code>git diff</code> beforehand
to make sure silly changes haven&rsquo;t made their way in.  Probably better
to use <code>git add --patch</code> and then just <code>git commit</code> though, as
discussed in <a href="http://www.benjiegillam.com/2013/02/favourite-git-commands/">post 1</a>.</p>

<h3><code>git add -A</code></h3>

<p>Really? <strong>All?!</strong> Be a <em>little</em> selective!</p>

<h2>Pushing and pulling</h2>

<h3><code>git push --set-upstream [remote] [branch]</code></h3>

<p>Want <code>git push</code> to just work? Just do one push using the above syntax
and future <code>git push</code>es will know what to do. (Shorter: <code>git push -u</code>.)</p>

<p>You should also look into git&rsquo;s <code>push.default</code> setting, and set it to
suit your needs.</p>

<h3><code>git pull --rebase</code></h3>

<p>If you want to apply your local commits linearly on top of the remote
commits (rather than in parallel via a merge as would be normal) then
you can use this command. I use it if I make changes from two different
computers in unrelated areas.</p>

<p>Try not to over-use it - you should only use it where it improves the
clarity of the history - git is very clever and a normal <code>git pull</code>
resulting in a merge is generally preferred. Rebasing loses context of
your commits (by modifying the patches to apply against later versions
of the code) which makes it harder for others to understand the
reasoning behind your changes.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My Favourite Git Commands IV: Cleaning Up]]></title>
    <link href="http://www.benjiegillam.com/2013/04/favourite-git-commands-iv/"/>
    <updated>2013-04-28T14:25:00+01:00</updated>
    <id>http://www.benjiegillam.com/2013/04/favourite-git-commands-iv</id>
    <content type="html"><![CDATA[<p><em>This is part 4 in my <a href="http://www.benjiegillam.com/categories/favourite-git-commands/">Favourite Git
Commands</a> series.</em></p>

<h2>Cleaning up</h2>

<p>Whether you want to do a quick bugfix in a different branch or just get
rid of all your debugging, keeping your working directory clean is a
good idea. Another way to do this is to use <code>git reset --patch</code> from
<a href="http://www.benjiegillam.com/2013/02/favourite-git-commands/">part 1</a>.</p>

<h3><code>git stash</code></h3>

<p>The stash is where you can store local modifications which you can then
restore later.</p>

<h4>Quickfix in other branch</h4>

<p>If you need to hop to another branch but you&rsquo;re in the middle of
something then this type of thing works great:</p>

<!-- more -->


<pre><code>git stash                # Save all local changes to stash
git checkout release     # Switch to branch needing quick fix
# Perform fix here
git commit               # Commit the fix to the release branch
git checkout feature     # Return to the feature branch you were on
git stash apply          # Re-apply the changes that you saved to stash
git stash drop           # If the above went well, tidy up
</code></pre>

<h4>Testing staged changes</h4>

<p>If you have a lot of changes in your working copy that you don&rsquo;t want to
commit (e.g. debugging) but you want to test your staged changes then
you can do the following:</p>

<pre><code>git stash --keep-index   # Remember index===staging area
# Perform tests here
git stash pop            # apply/drop all in one
</code></pre>

<p>If you&rsquo;re planning to keep the changes on the stash for longer then I&rsquo;d
advise you to use <code>git stash apply</code> followed by <code>git stash drop</code> over
<code>git stash pop</code> as if the apply fails you still have your original
changes in the stash and it&rsquo;s easier to resolve.</p>

<h3><code>git reset --hard HEAD</code></h3>

<p>This will clear out your staging area and working copy, checking out a
fresh copy of HEAD. <strong>Use with caution</strong> as it does not store the work
that is discarded. <code>git reset --hard HEAD^</code> will do the same, but will
also throw away your top commit on the current branch as well.</p>

<h3><code>git stash; git stash drop</code></h3>

<p>This is similar to the above, but it actually stores the code into git&rsquo;s
local files (before deleting the reference) so it is ever-so-slightly
safer. Note this will be lost when git next does garbage collection.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My Favourite Git Commands III: Editing History]]></title>
    <link href="http://www.benjiegillam.com/2013/02/favourite-git-commands-iii/"/>
    <updated>2013-02-22T09:52:00+00:00</updated>
    <id>http://www.benjiegillam.com/2013/02/favourite-git-commands-iii</id>
    <content type="html"><![CDATA[<p><em>This is part 3 in my <a href="http://www.benjiegillam.com/categories/favourite-git-commands/">Favourite Git
Commands</a> series.</em></p>

<h2>Editing History</h2>

<p><em>You should never change a commit that has been shared with someone else
- it makes everything very confusing for your collaborators. This is one
of the many reasons <a href="http://www.benjiegillam.com/2013/02/favourite-git-commands-ii">you should always be aware exactly what you&rsquo;re
committing</a>. However&hellip;</em></p>

<p><span class='pullquote-right' data-pullquote='It&#8217;s often useful to edit, reorder, split or merge your local (unpushed) commits'>
It&rsquo;s often useful to edit, reorder, split or merge your local (unpushed) commits
- for example you might want to fix a bug you introduced 2 commits ago
that you don&rsquo;t want anyone to ever know about. <em>EVER</em>. This ability to
easily edit history, fix mistakes in commits (for example if you
accidentally added a password) and generally control your revision
history is one of the things that makes git so powerful in my opinion.
</span></p>

<h3><code>git commit --amend</code></h3>

<!-- more -->


<p><span class='pullquote-right' data-pullquote='it often makes sense to apply a fix to the previous commit'>
Sometimes working on the next commit reveals a bug or typo in a previous
commit, in that instance
it often makes sense to apply a fix to the previous commit
directly.  This will save time for your collaborators who may
immediately spot the mistake, make a note of it or tell you about it,
only to find out that you fixed it in the next commit. Better to fix it
so no-one else has to process it, I think.
</span></p>

<p>Other times you may notice you forgot to add a file or said <code>n</code> instead
of <code>y</code> to one of the <code>git add --patch</code> hunks. To fix these situations
simply add any missing files, changes or fixes; review these changes;
and then <code>git commit --amend</code>.</p>

<p>You can also use this if you just want to change the commit message and
nothing else, but be aware that this still changes the commit&rsquo;s hash,
so you still shouldn&rsquo;t do this on commits that have been pushed.</p>

<h3><code>git reset --soft HEAD^</code></h3>

<p><span class='pullquote-right' data-pullquote='remove the last commit and try again'>
This is useful if you messed up so bad you want to
remove the last commit and try again
- it resets the current branch to the first parent of HEAD and moves the
committed hunks back to the staging area. One reason you might want to
do this is if you accidentally did a <code>git commit -a</code> and committed more
code than you intended to. (<strong>Tip</strong>: you can move changes from the
staging area to the working copy with <code>git reset HEAD [filename]</code>.)
</span></p>

<p>If you want to throw away the code altogether then you can swap <code>--soft</code>
for <code>--hard</code> (<strong>warning</strong>: this discards your working copy changes too).
Before doing this it might make sense to branch - e.g. <code>git branch
messed_up_code</code> - and stash your changes with <code>git stash</code>. When you&rsquo;re
happy you did want to discard that code, you can delete this branch with
<code>git branch -d messed_up_code</code> and drop the stash with <code>git stash drop</code>.</p>

<h3><code>git rebase --interactive HEAD~3</code></h3>

<p><span class='pullquote-right' data-pullquote='reorder or drop commits by rearranging or deleting their lines'>
Almost everything in git can be achieved with the use of <code>git rebase</code>.
Interactive mode is a great introduction to this very powerful mode, and
is useful for squashing (merging), reordering, dropping or editing
commits. When you run this command, it will allow you to chose various
options for each of the last 3 commits (see the help text at the bottom)
and then it will guide you through making the changes. Note that you can
reorder or drop commits by rearranging or deleting their lines.
</span></p>

<p>For example, to edit the 3rd commit ago, I&rsquo;d run the above command and
then change the relevant <code>pick</code> to <code>edit</code> in the editor that appears,
then save and exit. Git will then revert me back to that commit, where I
can make changes to the files in the local copy, and then <code>git add
--patch</code> and <code>git commit --amend</code>. When I&rsquo;m happy with the result I
simply <code>git rebase --continue</code> and git will apply the future commits on
top of my newly edited commit.  Recent versions of git very helpfully
tell us what we need to do at each stage.</p>

<p>Rebase commands in my version of git are:</p>

<ul>
<li><code>pick</code> = use commit</li>
<li><code>reword</code> = use commit, but edit the commit message</li>
<li><code>edit</code> = use commit, but stop for amending</li>
<li><code>squash</code> = use commit, but meld into previous commit</li>
<li><code>fixup</code> = like <code>squash</code>, but discard this commit&rsquo;s log message</li>
<li><code>exec</code> = run command (the rest of the line) using shell</li>
</ul>


<p>(and don&rsquo;t forget you can also drop and reorder commits by deleting or
rearranging their lines.)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My Favourite Git Commands II: Viewing Changes/History]]></title>
    <link href="http://www.benjiegillam.com/2013/02/favourite-git-commands-ii/"/>
    <updated>2013-02-20T14:25:00+00:00</updated>
    <id>http://www.benjiegillam.com/2013/02/favourite-git-commands-ii</id>
    <content type="html"><![CDATA[<p><em>This is part 2 in my <a href="http://www.benjiegillam.com/categories/favourite-git-commands/">Favourite Git
Commands</a> series.</em></p>

<h2>Viewing Changes/History</h2>

<p>A distributed version control system is very useful at helping you to
track down bugs, revert to previously working versions, and especially
for collaborating with other (remote) users. But you can lose many of
these benefits if you don&rsquo;t keep your revision history tidy.</p>

<p><span class='pullquote-right' data-pullquote='keep each commit small and focussed on just one feature.'>
I find the best way to do this is to
keep each commit small and focussed on just one feature.
Unrelated changes and fixes should go in their own
commits, even if this means that you&rsquo;re committing just one line: &ldquo;fix
typo in random number generator.&rdquo;
</span></p>

<!-- more -->


<p>Keeping a tidy revision history is especially useful if you ever need
<code>git bisect</code> to help you track down a bug a number of
commits ago - when you find the commit that introduces the bug it should
be easy to see what line caused it because there won&rsquo;t be any unrelated
lines in that commit.</p>

<p><span class='pullquote-right' data-pullquote='you should always be aware exactly what you&#8217;re committing.'>
To help you keep a tidy revision history
you should always be aware exactly what you&rsquo;re committing.
When you pull changes from another user you should always look through
what they&rsquo;ve done (which you can do with <code>git show</code>) so put yourself in
their shoes: when they look at your code they want to be able to scan
through the changes as fast as possible, and not have to try and
understand how unrelated changes fit in to implementing a specific
feature. They especially don&rsquo;t want to see you adding debugging lines in
one commit, then removing them in a later commit, then adding them again
- this is noise that will get in the way of their timely understanding
of your changes.
</span></p>

<h3><code>git diff</code></h3>

<p><span class='pullquote-right' data-pullquote='be your own pair programming partner!'>
You should be typing this many times a day, it shows you at a glance
what lines you have changed in your working copy. A great way of
reviewing just those lines for typos or bugs -
be your own pair programming partner!
</span></p>

<h3><code>git diff --ignore-space-change</code></h3>

<p>Or <code>git diff -b</code> for short - this is particularly useful if you&rsquo;ve
changed a lot of whitespace (e.g. you&rsquo;ve decided to switch tabs for 2
spaces throughout) but want to make sure you didn&rsquo;t accidentally edit
any lines of code.</p>

<p>It&rsquo;s also useful if you&rsquo;re using a whitespace sensitive language (e.g.
Python or CoffeeScript) and you know you indented a bunch of old code,
but just want to see your new modifications.</p>

<p>Also of use <code>git diff --ignore-all-space</code> / <code>git diff -w</code> which ignores
the difference between a line with whitespace and a line without.</p>

<p>Either way, if you use this then I&rsquo;d still advise you use vanilla <code>git
diff</code> before committing, as this is what your collaborators will see
when they review your code.</p>

<h3><code>git diff --staged</code></h3>

<p><span class='pullquote-right' data-pullquote='when I forget to do this, I generally regret it.'>
This command shows you the changes you have staged for the next commit.
I do this before almost every commit to check I&rsquo;m only committing
exactly what I want to commit, and
when I forget to do this, I generally regret it.
</span></p>

<h3><code>git show HEAD~3</code></h3>

<p>When you pull down someone elses changes, or before modifying history,
it&rsquo;s generally a good idea to know what happened in each commit. I use
<code>git show HEAD</code> to review the last commit, this is very useful and
sometimes reveals that I added more than I meant to (or less). You can
use <code>git show HEAD^^^</code> or <code>git show HEAD~3</code> to view the 4th commit ago
if you want to go back further. (Note: each <code>^</code> means &ldquo;this commit&rsquo;s
first parent&rdquo; so more <code>^</code>s means further back. If your history is
non-linear you need to be more careful that you&rsquo;re reviewing all the
relevant commits.)</p>

<h3><code>git blame</code></h3>

<p>Very useful for figuring out which commit a particular bug or typo came
from (so you can edit that bug out of history and no-one will ever know:
see part 3&hellip;). Try it now: <code>git blame [filename]</code> - pretty neat, isn&rsquo;t
it? Also useful for debunking &ldquo;it couldn&rsquo;t possibly have been me that
wrote that messy code, it must have been you!&rdquo;</p>

<h2>git log</h2>

<p>Of course, it&rsquo;s generally a good idea to keep an eye on your git log
too, and this is why you want your commit messages to be short and to
the point. <code>git log</code> is a great command, and very flexible, but it has
so many options many people simply use <code>git log</code> and miss out on it&rsquo;s
power!</p>

<p>Here&rsquo;s a couple of my favourite <code>git log</code> aliases and how to add them to
your own configuration.</p>

<h3><code>git lg</code></h3>

<p>This is my preferred way of viewing history. It inherits from a number
of other peoples&#8217;, sorry I can&rsquo;t remember their names right now.</p>

<p><img src="http://www.benjiegillam.com/files/img/git-lg.png" title="`git lg` command output" ></p>

<p>To add it:</p>

<pre><code>git config --global alias.lg "log --graph --pretty=format:'%Cgreen%h%Creset -%Creset %s%C(yellow)%d %Cblue(%aN, %cr)%Creset' --abbrev-commit --date=relative"
</code></pre>

<h3><code>git heir</code></h3>

<p>Tells you the hierarchy of your various git branches.</p>

<p><img src="http://www.benjiegillam.com/files/img/git-heir.png" title="`git heir` command output" ></p>

<p>To add it:</p>

<pre><code>git config --global alias.heir "log --all --graph --decorate --oneline --simplify-by-decoration"
</code></pre>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[My Favourite Git Commands I: Patch]]></title>
    <link href="http://www.benjiegillam.com/2013/02/favourite-git-commands/"/>
    <updated>2013-02-19T14:25:00+00:00</updated>
    <id>http://www.benjiegillam.com/2013/02/favourite-git-commands</id>
    <content type="html"><![CDATA[<p><em>This is part 1 in my <a href="http://www.benjiegillam.com/categories/favourite-git-commands/">Favourite Git
Commands</a> series.</em></p>

<p>Last year I <a href="http://www.benjiegillam.com/2012/02/happy-new-year-2012/">wrote a post</a> about how I intended to
switch over to and master the tools I&rsquo;d always meant to but somehow
never got around to. Among these was git.</p>

<p>I&rsquo;ve now been using git exclusively for the last year (even when
interfacing with SVN projects I do so using the awesome <code>git-svn</code>
bridge) and I love it. It took me a while to form a decent workflow and
break the old SVN habits, so over the next few days I thought I&rsquo;d share
with you some of my favourite git commands.</p>

<p>I&rsquo;m going to assume that you&rsquo;ve used git at least once or twice, and
know what I mean by repo (where the git history is stored, i.e. the
<code>.git</code> folder), HEAD (the top commit in the current branch), staging
area (where the files for the next commit are stored, also known as the
index or cache), and the working copy (the files that you&rsquo;re modifying).</p>

<h2>The <code>--patch</code> option</h2>

<p>My friend <a href="http://jspr.tndy.me/">Jasper</a> helped a lot when I first started getting into git,
and noted one of his favourite commands was <code>git add -i</code> - an
interactive way of adding files to your staging area before commits. I
used this a lot, but found that mostly I was using the patch function,
and going through all the menus was unnecessarily slowing me down.
Enter:</p>

<!-- more -->


<h3><code>git add --patch</code></h3>

<p>Or <code>git add -p</code> if you prefer. This command jumps straight to patch mode
on all your tracked files, asking you for each hunk whether you want to
add them to the staging area or not. I generally answer with one of the
following, though there are other options that can be useful:</p>

<ul>
<li><code>y</code> - yes, add this hunk</li>
<li><code>n</code> - no, skip over this hunk</li>
<li><code>s</code> - split this hunk into smaller hunks</li>
<li><code>e</code> - edit this hunk, then add it (I normally use this to remove debugging
lines)</li>
<li><code>q</code> - I&rsquo;m all done adding stuff, go away.</li>
</ul>


<p>This ensures that I can keep my debugging and other temporary mods out
of git, only adding the files I want to appear in git history. But
sometimes I accidentally add a line I didn&rsquo;t mean to, so then I use:</p>

<h3><code>git reset --patch</code></h3>

<p>Or <code>git reset -p</code> if you prefer. This command is basically the opposite
of <code>git add --patch</code> - it enables you to move specific hunks back out of
staging and into your working copy. It accepts the same options as its
sibling above.</p>

<hr />

<h4>Tip: removing one line from the middle of a hunk</h4>

<p>You can use the <code>e</code>dit command to open the hunk in your text editor,
then change all the <code>+</code> at the beginning of lines for a space <em>except</em> on
the lines you want to remove. Remember this is removing a hunk, so a <code>+</code>
actually means &ldquo;remove this line from the staging area&rdquo;. (The spaces are
needed for context.)</p>

<hr />

<p>When I have the staging area how I want it, I commit it (see below). But
then I&rsquo;m left with all the lines I didn&rsquo;t stage. Some of these I want to
keep whilst I work on the next commit, others I&rsquo;m done with (mostly
<code>console.log</code>/<code>NSLog</code> statements, if I&rsquo;m honest). So I want to
selectively delete modified lines from my working copy without having to
find them in my text editor. Enter:</p>

<h3><code>git checkout --patch</code></h3>

<p>Yes, or <code>git checkout -p</code>. This command is more dangerous as it <strong>throws
away</strong> the work you select, with no history of it (since it was never
committed). You select the hunks you want to throw away using the same
options as its siblings above. You can also use it for surgery-like
removal of lines at any time using the <code>e</code> option and the same tip as
<code>git reset --patch</code> but be <strong>very careful</strong>!</p>

<h3><code>git [add/etc] --patch [filenames]</code></h3>

<p>All the above commands accept file or directory names as an argument, so
you can just patch add/reset/checkout a small selection of files without
worrying about modifications to other unrelated files. This is
particularly powerful when paired with zsh&rsquo;s globbing.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[LightwaveRF RF Protocol]]></title>
    <link href="http://www.benjiegillam.com/2013/02/lightwaverf-rf-protocol/"/>
    <updated>2013-02-03T11:10:00+00:00</updated>
    <id>http://www.benjiegillam.com/2013/02/lightwaverf-rf-protocol</id>
    <content type="html"><![CDATA[<p>Following on from <a href="http://www.benjiegillam.com/2013/02/reverse-engineering-lightwaverf/">yesterday&rsquo;s success</a>, I&rsquo;ve done a big data
capture of various commands and have figured out (mostly) the protocol.</p>

<p>As you saw <a href="http://www.benjiegillam.com/2013/02/reverse-engineering-lightwaverf/">yesterday</a>, each command comprises 10 bytes. But actually,
each of these only ever take one of 16 values, thus they really
represent nibbles. The values of these nibbles are:</p>

<figure class='code'><figcaption><span>Lightwave RF nibble values</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='raw'><span class='line'><span class="err"> 0 : 1111 0110</span>
</span><span class='line'><span class="err"> 1 : 1110 1110</span>
</span><span class='line'><span class="err"> 2 : 1110 1101</span>
</span><span class='line'><span class="err"> 3 : 1110 1011</span>
</span><span class='line'><span class="err"> 4 : 1101 1110</span>
</span><span class='line'><span class="err"> 5 : 1101 1101</span>
</span><span class='line'><span class="err"> 6 : 1101 1011</span>
</span><span class='line'><span class="err"> 7 : 1011 1110</span>
</span><span class='line'><span class="err"> 8 : 1011 1101</span>
</span><span class='line'><span class="err"> 9 : 1011 1011</span>
</span><span class='line'><span class="err">10 : 1011 0111</span>
</span><span class='line'><span class="err">11 : 0111 1110</span>
</span><span class='line'><span class="err">12 : 0111 1101</span>
</span><span class='line'><span class="err">13 : 0111 1011</span>
</span><span class='line'><span class="err">14 : 0111 0111</span>
</span><span class='line'><span class="err">15 : 0110 1111</span>
</span></code></pre></td></tr></table></div></figure>


<p>Then the payload is made out of 10 of these nibbles.</p>

<h2>Level (2 nibbles)</h2>

<!-- more -->


<p>The level is defined by nibbles 1 and 2, giving a range of 0-255 (NOTE:
this full range is not used, as far as I can tell). For simple commands
(on/off), the level is 0.</p>

<p>You can dim a light to one of the 31 different levels, but these levels
start at 129. I find that the easiest way to think of this is <code>0x80</code>
+ dim level (from 1 to 31), where <code>0x80</code> is the hex representation of
the number 128. Note that to dim a light you simply use the <code>ON</code> command
- there isn&rsquo;t a specific &ldquo;dim&rdquo; command.</p>

<p>As with dimming, for a <code>MOOD</code> command (see later), you add <code>0x80</code> to your mood value -
there are at least 5 moods supported: 0-4.</p>

<p><img class="right" src="http://www.benjiegillam.com/files/img/lightwaveremote.png" title="LightwaveRF Remote" ></p>

<h2>Subunit (1 nibble)</h2>

<p>Each LightwaveRF remote can target 16 different &ldquo;subunits&rdquo; - e.g. the
remote shown has in effect 34 buttons - on and off the the 16 subunits
(A-D * 1-4) plus the two &ldquo;global&rdquo; buttons below.</p>

<p>Commands which communicate with all devices send the highest subunit
number, 15, where as simple commands can be sent to individual
subunits. On the remote above the subunits are 0=A1, 1=A2, 2=A3, &hellip;
14=D3, 15=D4.</p>

<h2>Command (1 nibble)</h2>

<p>LightwaveRF supports (by the looks of it) up to 16 commands. The ones
I&rsquo;m aware of are:</p>

<figure class='code'><figcaption><span>LightwaveRF Commands</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='raw'><span class='line'><span class="err"> 0 : OFF   (specify subunit, level=0)</span>
</span><span class='line'><span class="err"> 1 : ON    (specify subunit,</span>
</span><span class='line'><span class="err">            send level = 0 to switch on to previous state, or</span>
</span><span class='line'><span class="err">            level = 0x80 + level[1..31] to turn on to a specific</span>
</span><span class='line'><span class="err">            level)</span>
</span><span class='line'><span class="err"> 2 : MOOD  (subunit = 15, level = 0x80 + mood[0..4])</span>
</span></code></pre></td></tr></table></div></figure>


<p><img class="right" src="http://www.benjiegillam.com/files/img/lightwavemoodremote.png" title="LightwaveRF Mood Remote" ></p>

<p>Note on my mood remote (when the switch on the back is in &ldquo;mood&rdquo; mode)
the buttons (excluding the off button) from top left to bottom right
have moods: 2, 3, 4, 0, 1 - the ordering of which seems a bit bizarre.</p>

<h2>Remote Identifier (6 nibbles)</h2>

<p>The remainder of the payload I assume to be the identity of the remote
control, e.g.</p>

<figure class='code'><figcaption><span>LightwaveRF Commands</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='raw'><span class='line'><span class="err">0xF21527 encodes to: 0110 1111 1110 1101 1110 1110 1101 1101 1110 1101 1011 1110</span>
</span></code></pre></td></tr></table></div></figure>


<h1>Calculator</h1>

<p>I&rsquo;ve knocked up a <a href="http://www.benjiegillam.com/files/lightwaveRFCalculator.html">quick and dirty calculator</a> to help with
encoding/decoding.</p>

<h1>How can I use this?</h1>

<p>Watch this space, I&rsquo;ll be posting how to make your own LightwaveRF
transceiver using an Arduino soon. As always, the code is <a href="https://github.com/benjie/rf-experiments">on
GitHub</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Reverse Engineering LightwaveRF]]></title>
    <link href="http://www.benjiegillam.com/2013/02/reverse-engineering-lightwaverf/"/>
    <updated>2013-02-02T15:42:00+00:00</updated>
    <id>http://www.benjiegillam.com/2013/02/reverse-engineering-lightwaverf</id>
    <content type="html"><![CDATA[<p>I&rsquo;ve been <a href="https://groups.google.com/d/msg/southackton/YjQ4-7diqt0/mryGKDIOp1MJ">stumped</a> for a <a href="http://openmicros.org/index.php/component/kunena/7-communications-and-protocols/2751-lightwaverfsiemensjsjs-protocol-help?Itemid=0">little
while</a> trying to decode LightwaveRF data using a
<a href="http://www.ebay.co.uk/itm/RF-Wireless-Transmitter-and-Receiver-Link-Kit-Module-433Mhz-For-Remote-Control-/350628314207?pt=UK_Gadgets&amp;hash=item51a3137c5f">cheap 433.92MHz receiver</a> and a Arduino Nano.</p>

<p>Today I&rsquo;ve finally cracked it! Thanks to <a href="http://geekgrandad.wordpress.com/2013/01/03/lightwaverf-arduino-library/">Geek Grandad</a>&rsquo;s
<a href="https://github.com/lawrie/LightwaveRF">LightwaveRF GitHub repo</a> I finally figured out the
missing link - my zeros were not zeros, but instead 10s! Sure, this
doesn&rsquo;t make sense yet, but let me explain.</p>

<!-- more -->


<h3>The setup</h3>

<p>To capture the radio data, I used a <a href="http://www.ebay.co.uk/itm/RF-Wireless-Transmitter-and-Receiver-Link-Kit-Module-433Mhz-For-Remote-Control-/350628314207?pt=UK_Gadgets&amp;hash=item51a3137c5f">cheap 433.92MHz receiver</a>
hooked up to an Arduino Nano. The wiring was simple: just wire up +5V
and GND to the radio from the Arduino, and connect the DATA pin to
Arduino Nano digital pin 2. I wrote a quick and dirty <a href="https://github.com/benjie/rf-experiments/blob/master/signal_capture.ino">receive sketch</a>
and installed it to the Arduino. This sketch just listens for the
&ldquo;CHANGE&rdquo; event on the radio&rsquo;s pin (i.e. when it transitions from 0 to 1,
or from 1 to 0) and then effectively times the duration it was in each
state (high or low).</p>

<p>For example, if it was low for 250us (microseconds) then high for 350us
then low for 250us, the sketch would output the equivalent of &ldquo;250, 350,
250&rdquo;. Each value was compressed down to 2 bytes and sent over the serial
link to <a href="https://github.com/benjie/rf-experiments/blob/master/sigcap.py">my python script</a>, which would receive the data, decode it
back to textual numbers, write it to a capture file and then plot a
graph of it so I could check the data that was received made sense.</p>

<p><img src="http://www.benjiegillam.com/files/img/rf-data-A1A2A3A4-on.png" alt="RF Data Graph" /></p>

<p>In the image above the regular bits is the sensible data (4 different
button presses, each automatically repeated to help with collisions) and
the areas in-between are just noise. The x-axis is the record number
(from 0 through to 5000) and the y-axis is the value of that record -
i.e. how long in microseconds the pin remained in one state before
changing.</p>

<h3>Limitations</h3>

<p>Due to not wanting to overflow the <a href="http://en.wikipedia.org/wiki/Circular_buffer">ringbuffer</a> in the Arduino sketch
I only allowed each capture to go up to 5,000 records (which worked out
to be pretty variable, but generally a couple of seconds).</p>

<h3>Capture</h3>

<p>I have a LightwaveRF remove that&rsquo;s basically a matrix of on/off buttons.
There are 4 sets of on/off buttons (8 buttons total), and then a slider
switch that has 4 positions, which effectively means that there are 16 *
2 = 32 buttons. I captured each of these, both pressed and held.</p>

<p>I also have a &ldquo;Mood Remote&rdquo; which I captured separately.</p>

<h3>Analysis</h3>

<p>Having captured the data, I then went about trying to decode it. I
noticed that every other record was of a similar duration, and the
alternating records varied but took two forms - either long or short - I
assumed therefore that this meant that they carried the data and as they
came in 2 lengths I arbitrarily stated that the short length was a zero,
and the long length was a 1.</p>

<p>This worked great - my captures came through very reliably - every time
I captured data for the same button it ALWAYS came out the same, and
different buttons always came out differently. Brilliant! But the data
just didn&rsquo;t feel right; I felt there was something off. I tried to
analyse it but just came up empty each time - very frustrating.</p>

<h3>Breakthrough</h3>

<p>I had to give it a rest for a while because of having other things to do
- a newborn and trying to get <a href="http://www.somakeit.org.uk/">So Make It</a> off of the ground - so I
did nothing on it for a while, except write a few forum posts hoping
someone could shed some light, and asking around the helpful fellows of
<a href="http://southackton.org.uk/">SoutHACKton</a>.</p>

<p>Finally today I decided to give it another go, and found <a href="https://github.com/lawrie/LightwaveRF">Geek Grandad&rsquo;s
repository</a>. Reading through the code on there I
immediately noticed that he received/sent 10 bytes, whereas I was only
working with 8. This seemed wrong, but he was having success so I delved
deeper. I spent a while reading the receiving code before realising the
sending code would be much simpler to grok! And lo and behold I
discovered that he sent each bit, BIT, (0 is LOW, 1 is HIGH) using the
following method:</p>

<ol>
<li>SEND BIT for 320us</li>
<li>SEND LOW for 345us</li>
</ol>


<p>(And also that there were a lot of single bits separating the bytes and
the messages.)</p>

<h4>Sending: High High High</h4>

<p>Simply comes out as HIGH(320) LOW(345) HIGH(320) LOW(345) HIGH(320) LOW(345). Simple to decode. But&hellip;</p>

<h4>Sending: High Low High</h4>

<p>Comes out as HIGH(320) <strong>LOW(345) LOW(320) LOW(345)</strong> HIGH(320) LOW(345)
LOW(345).</p>

<p>Which is the same as HIGH(320) <strong>LOW(1010)</strong> HIGH(320) LOW(345).</p>

<p>And this meant that:</p>

<h2>Long low periods imply a &ldquo;10&rdquo; rather than a &ldquo;0&rdquo;</h2>

<p>So simply changing my <a href="https://github.com/benjie/rf-experiments/blob/master/decode.coffee">decode script</a> to recognise &ldquo;10&rdquo; and &ldquo;1&rdquo;
instead of &ldquo;1&rdquo; and &ldquo;0&rdquo; respectively, the data suddenly came out really
clearly (it&rsquo;s frustrating how close I was!):</p>

<figure class='code'><figcaption><span>Remote F30537 sends the on signals for buttons 1 through 16</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='raw'><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1111 0110  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1110 1110  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1110 1101  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1110 1011  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1101 1110  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1101 1101  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1101 1011  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1011 1110  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1011 1101  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1011 1011  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1011 0111  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  0111 1110  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  0111 1101  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  0111 1011  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  0111 0111  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  0110 1111  1  1110 1110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you can see, the 3rd byte of this data is the only bit that changes;
this specifies the button number, or &ldquo;subunit&rdquo; in RFXCOM parlance.</p>

<p>For good measure, here&rsquo;s what the &ldquo;All off&rdquo; button sends:</p>

<figure class='code'><figcaption><span>Remote F30537 Sends the &#8220;all off&#8221; command</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='raw'><span class='line'><span class="err"> 1  0111 1101  1  1111 0110  1  0110 1111  1  1111 0110  1  0110 1111  1  1110 1011  1  1111 0110  1  1101 1101  1  1110 1011  1  1011 1110</span>
</span></code></pre></td></tr></table></div></figure>


<p>From this you can see that the initial byte isn&rsquo;t always &ldquo;1111 0110&rdquo;
(and there are plenty more examples among my data).</p>

<p>Here&rsquo;s the &lsquo;on&rsquo; command for my mood remote:</p>

<figure class='code'><figcaption><span>Remote F2A002 Sends the &#8216;on&#8217; command</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='raw'><span class='line'><span class="err"> 1  1111 0110  1  1111 0110  1  1111 0110  1  1110 1110  1  0110 1111  1  1110 1101  1  1011 0111  1  1111 0110  1  1111 0110  1  1110 1101</span>
</span></code></pre></td></tr></table></div></figure>


<p>Clearly we can see that it&rsquo;s subunit code is the equivalent of the first
subunit on the other remote. Bytes 1-6 are the same as the other remote,
so I assume that the ID of the remote is stored in the last 4 bytes;
though my RFXCOM only needs a 3 byte ID so there&rsquo;s either a checksum or
some form of encoding going on here. It&rsquo;s clear that there is no way to
encode &ldquo;00&rdquo; or &ldquo;000&rdquo; and so each byte doesn&rsquo;t actually give 256
possibilities, but <a href="https://gist.github.com/4698300">instead 55</a>. I shall continue to ponder&hellip;</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Info Leak: Virgin Media]]></title>
    <link href="http://www.benjiegillam.com/2013/01/info-leak-virgin-media/"/>
    <updated>2013-01-29T11:08:00+00:00</updated>
    <id>http://www.benjiegillam.com/2013/01/info-leak-virgin-media</id>
    <content type="html"><![CDATA[<p>At 10:57am today (29th Jan 2013) I received a call from <a href="http://phoneowner.info/Number.aspx/00501">00501</a>, which
according to phoneowner.info is commonly used by scammers (PPI
reclaiming, etc.). The man on the other end of the phone had a heavy
accent and the audio distortion on the line made it sound like it was
coming from overseas. The call went as follows:</p>

<p><strong>Them</strong>: Hello sir, I&rsquo;m calling from the technical department of your
broadband provider.</p>

<!-- more -->


<p><strong>Me</strong>: Sorry, you&rsquo;re calling from where?</p>

<p><strong>Them</strong>: From your broadband provider.</p>

<p><strong>Me</strong>: Oh, and which company is that?</p>

<p><strong>Them</strong>: <em>pause:</em> Virgin Media</p>

<p><strong>Me</strong>: <em>surprised:</em> Okay, hello.</p>

<p><strong>They hang up</strong>: <em>beeeeeeeep</em></p>

<p>They probably hung up because they figured it&rsquo;d be too much effort to
try and scam me if I was asking questions before even greeting them.</p>

<p>I get a lot of spam calls, despite using the <a href="http://www.tpsonline.org.uk/tps/index.html">Telephone Preference
Service</a> and rarely giving my landline out for anything, but this
one slightly surprised me: they knew I used Virgin Media. How did they
know that, I wondered? Was it just a fluke?  Were they legit? Was it
just a bad connection making it sound like an overseas call? Then I
realised: it&rsquo;s really easy to find out if someone used Virgin Media in
the UK.</p>

<h2>How to find out if a phone number uses Virgin Media</h2>

<p>It&rsquo;s easy: just plug the phone number into the <a href="https://my.virginmedia.com/faults/service-status">Virgin Media Status page</a>:</p>

<p><img src="http://www.benjiegillam.com/files/img/virgin-phone-fail.png" title="Virgin Media Status page with phone number entered" ></p>

<p><span class='pullquote-right' data-pullquote='If the phone number exists and is a Virgin Media number, they&#8217;ll display a status page.'>
If the phone number exists and is a Virgin Media number, they&rsquo;ll display a status page.
If it doesn&rsquo;t then they&rsquo;ll tell you &ldquo;We don&rsquo;t recognise this phone
number.  Please re-enter your full Virgin Media home phone number.&rdquo;
</span></p>

<p>Now this isn&rsquo;t 100% reliable - you might use a different provider for
phone line, or you may have no landline at all. But for the most part (I
expect) a scammer could just try random combinations of digits in the
status page until they get a phone number that works, then phone them up
claiming (semi-convincingly) to be Virgin Media and telling them to
enter <em>this command</em> to wipe the viruses from this computer (or whatever
thing the scammers are doing these days). <em>Then they&rsquo;ll get confused
because Linux doesn&rsquo;t have a &ldquo;Start&rdquo; button for you to press, and Ctrl+R
doesn&rsquo;t do anything, and all that jazz&hellip;</em></p>

<h2>Conclusion</h2>

<p>I see this as an information disclosure leak and I think Virgin Media
should fix it. Postcode is fine for that status page, since it&rsquo;s
(generally) not specific to one address like a landline (generally) is.</p>

<p>Or why not just make your status page visible to all - not requiring a
phone number/postcode/etc. This would show you are proud of the services
you offered and don&rsquo;t want nor need to hide anything: why not have a
status chart <a href="http://status.aws.amazon.com/">like Amazon Webservices do</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Octopress: CoffeeScript/JS code toggle]]></title>
    <link href="http://www.benjiegillam.com/2012/07/octopress-coffeescript-slash-js-code-toggle/"/>
    <updated>2012-07-04T19:13:00+01:00</updated>
    <id>http://www.benjiegillam.com/2012/07/octopress-coffeescript-slash-js-code-toggle</id>
    <content type="html"><![CDATA[<p><a href="http://www.twitter.com/drurly">Dru Riley</a> asked <a href="http://www.benjiegillam.com/2012/06/node-dot-js-ssl-certificate-chain/#comment-572278011">via a comment</a> how I
achieved the CoffeeScript/JS highlighting switch on my blog which looks like
this:</p>

<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_406505913');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_406505913');">.js</a></span><span><-- These buttons toggle CoffeeScript/JavaScript</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='coffee-script'><span class='line'><span class="nx">CoffeeScript</span> <span class="o">is</span> <span class="s">&#39;awesome&#39;</span>
</span><span class='line'><span class="nx">dance</span><span class="p">()</span> <span class="k">if</span> <span class="nx">you</span><span class="p">.</span><span class="nx">love</span> <span class="nx">CoffeeScript</span>
</span><span class='line'><span class="nx">i</span><span class="p">.</span><span class="nx">love</span> <span class="nx">CoffeeScript</span>
</span><span class='line'><span class="nx">yay</span><span class="p">()</span>
</span></code></pre></td></tr></table></div></figure>


<p>The answer: a hack. I don&rsquo;t code ruby, so I couldn&rsquo;t figure out how to
do it neatly (or even if I could - feedback welcome!). The easiest way for me to do it was
just to modify <code>plugins/backtick_code_block.rb</code>, then add the JS and CSS
to make it work.</p>

<p>If you want to see how I did it, or do it yourself, here are my diffs:</p>

<!-- more -->


<p><strong>NOTE</strong>: These diffs should be ran against revision <a href="https://github.com/imathis/octopress/commit/c069dc72761dd89531c05376e84c1c6c06ef48da">c069dc7</a> of Octopress,
which is already quite out of date&hellip;</p>

<figure class='code'><figcaption><span>Hack backtick plugin to highlight both JS and CS (plugins/backtick_code_block.rb.diff)</span> <a href='http://www.benjiegillam.com/code/plugins/backtick_code_block.rb.diff'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
</pre></td><td class='code'><pre><code class='diff'><span class='line'><span class="gh">diff --git a/plugins/backtick_code_block.rb b/plugins/backtick_code_block.rb</span>
</span><span class='line'><span class="gh">index 40e7900..4450f12 100644</span>
</span><span class='line'><span class="gd">--- a/plugins/backtick_code_block.rb</span>
</span><span class='line'><span class="gi">+++ b/plugins/backtick_code_block.rb</span>
</span><span class='line'><span class="gu">@@ -14,12 +14,18 @@ module BacktickCodeBlock</span>
</span><span class='line'>       @options = $1 || &#39;&#39;
</span><span class='line'>       str = $2
</span><span class='line'>
</span><span class='line'><span class="gi">+      extra = &#39;&#39;</span>
</span><span class='line'><span class="gi">+      id = rand(999999999)</span>
</span><span class='line'><span class="gi">+      fig_id = &quot;figure_#{id}&quot;</span>
</span><span class='line'><span class="gi">+</span>
</span><span class='line'>       if @options =~ AllOptions
</span><span class='line'>         @lang = $1
</span><span class='line'><span class="gd">-        @caption = &quot;&lt;figcaption&gt;&lt;span&gt;#{$2}&lt;/span&gt;&lt;a href=&#39;#{$3}&#39;&gt;#{$4 || &#39;link&#39;}&lt;/a&gt;&lt;/figcaption&gt;&quot;</span>
</span><span class='line'><span class="gi">+        extra = genExtra @lang, fig_id</span>
</span><span class='line'><span class="gi">+        @caption = &quot;&lt;figcaption&gt;#{extra}&lt;span&gt;#{$2}&lt;/span&gt;&lt;a href=&#39;#{$3}&#39;&gt;#{$4 || &#39;link&#39;}&lt;/a&gt;&lt;/figcaption&gt;&quot;</span>
</span><span class='line'>       elsif @options =~ LangCaption
</span><span class='line'>         @lang = $1
</span><span class='line'><span class="gd">-        @caption = &quot;&lt;figcaption&gt;&lt;span&gt;#{$2}&lt;/span&gt;&lt;/figcaption&gt;&quot;</span>
</span><span class='line'><span class="gi">+        extra = genExtra @lang, fig_id</span>
</span><span class='line'><span class="gi">+        @caption = &quot;&lt;figcaption&gt;#{extra}&lt;span&gt;#{$2}&lt;/span&gt;&lt;/figcaption&gt;&quot;</span>
</span><span class='line'>       end
</span><span class='line'>
</span><span class='line'>       if str.match(/\A( {4}|\t)/)
</span><span class='line'><span class="gu">@@ -35,9 +41,40 @@ module BacktickCodeBlock</span>
</span><span class='line'>           raw += &quot;\n```\n&quot;
</span><span class='line'>         else
</span><span class='line'>           code = highlight(str, @lang)
</span><span class='line'><span class="gd">-          &quot;&lt;figure class=&#39;code&#39;&gt;#{@caption}#{code}&lt;/figure&gt;&quot;</span>
</span><span class='line'><span class="gi">+          if extra == &#39;&#39;</span>
</span><span class='line'><span class="gi">+            &quot;&lt;figure class=&#39;code&#39;&gt;#{@caption}#{code}&lt;/figure&gt;&quot;            </span>
</span><span class='line'><span class="gi">+          else</span>
</span><span class='line'><span class="gi">+            alt_lang = &#39;&#39;</span>
</span><span class='line'><span class="gi">+            str2 = &#39;&#39;</span>
</span><span class='line'><span class="gi">+            altcode = &#39;&#39;</span>
</span><span class='line'><span class="gi">+            if @lang == &#39;coffee-script&#39;</span>
</span><span class='line'><span class="gi">+              lang_display = &#39;CoffeeScript&#39;</span>
</span><span class='line'><span class="gi">+              alt_lang = &#39;javascript&#39;</span>
</span><span class='line'><span class="gi">+              alt_lang_display = &#39;JavaScript&#39;</span>
</span><span class='line'><span class="gi">+              stdin, stdout, stderr, wait_thr = Open3.popen3(&quot;coffee -s -b -c -p&quot;)</span>
</span><span class='line'><span class="gi">+              stdin.print(str)</span>
</span><span class='line'><span class="gi">+              stdin.close</span>
</span><span class='line'><span class="gi">+              str2 = stdout.readlines(nil)[0]</span>
</span><span class='line'><span class="gi">+              if !str2</span>
</span><span class='line'><span class="gi">+                str2 = stderr.readlines(nil)[0]</span>
</span><span class='line'><span class="gi">+                str2 = &quot;Compilation failed: \n#{str2}&quot;</span>
</span><span class='line'><span class="gi">+              end</span>
</span><span class='line'><span class="gi">+              stdout.close</span>
</span><span class='line'><span class="gi">+              stderr.close</span>
</span><span class='line'><span class="gi">+              exit_status = wait_thr.value</span>
</span><span class='line'><span class="gi">+            end</span>
</span><span class='line'><span class="gi">+            if alt_lang != &#39;&#39;</span>
</span><span class='line'><span class="gi">+              altcode = highlight(str2, alt_lang)</span>
</span><span class='line'><span class="gi">+            end</span>
</span><span class='line'><span class="gi">+            &quot;&lt;figure class=&#39;code&#39;&gt;#{@caption}&lt;div clang=&#39;#{lang_display}&#39; id=\&quot;#{fig_id}\&quot;&gt;#{code}&lt;/div&gt;&lt;div clang=&#39;#{alt_lang_display}&#39; id=\&quot;#{fig_id}_alt\&quot; style=\&quot;display:none;\&quot;&gt;#{altcode}&lt;/div&gt;&lt;/figure&gt;&quot;</span>
</span><span class='line'><span class="gi">+          end</span>
</span><span class='line'>         end
</span><span class='line'>       end
</span><span class='line'>     end
</span><span class='line'>   end
</span><span class='line'><span class="gi">+  def genExtra(lang,fig_id)</span>
</span><span class='line'><span class="gi">+    if @lang == &quot;coffee-script&quot;</span>
</span><span class='line'><span class="gi">+      return &quot;&lt;span class=\&quot;switchLang\&quot;&gt;&lt;a class=\&quot;switchLang selected\&quot; onclick=\&quot;switchLang(event,this,&#39;#{fig_id}&#39;);\&quot;&gt;.coffee&lt;/a&gt;&lt;a class=\&quot;switchLang\&quot; onclick=\&quot;switchLang(event,this,&#39;#{fig_id}&#39;);\&quot;&gt;.js&lt;/a&gt;&lt;/span&gt;&quot;</span>
</span><span class='line'><span class="gi">+    end</span>
</span><span class='line'><span class="gi">+  end</span>
</span><span class='line'> end
</span></code></pre></td></tr></table></div></figure>


<figure class='code'><figcaption><span>The JavaScript to allow the user to toggle (source/javascripts/switchlang.js)</span> <a href='http://www.benjiegillam.com/code/source/javascripts/switchlang.js'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
</pre></td><td class='code'><pre><code class='js'><span class='line'><span class="kd">function</span> <span class="nx">switchLang</span><span class="p">(</span><span class="nx">e</span><span class="p">,</span><span class="nx">me</span><span class="p">,</span><span class="nx">id</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="nx">me</span><span class="p">.</span><span class="nx">className</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/selected/</span><span class="p">))</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">e</span><span class="p">)</span> <span class="nx">e</span> <span class="o">=</span> <span class="nb">window</span><span class="p">.</span><span class="nx">event</span><span class="p">;</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">me</span><span class="p">.</span><span class="nx">onmouseup</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">me</span><span class="p">.</span><span class="nx">onmouseup</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">if</span> <span class="p">(</span><span class="nx">e</span> <span class="o">&amp;&amp;</span> <span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="nx">e</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">el1</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="nx">id</span><span class="p">);</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">el2</span> <span class="o">=</span> <span class="nb">document</span><span class="p">.</span><span class="nx">getElementById</span><span class="p">(</span><span class="nx">id</span><span class="o">+</span><span class="s2">&quot;_alt&quot;</span><span class="p">);</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">el1</span> <span class="o">||</span> <span class="o">!</span><span class="nx">el2</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="nx">el1</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">display</span> <span class="o">==</span> <span class="s1">&#39;none&#39;</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">el2</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">display</span> <span class="o">=</span> <span class="s1">&#39;none&#39;</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">el1</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">display</span> <span class="o">=</span> <span class="s1">&#39;block&#39;</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">me</span><span class="p">.</span><span class="nx">parentNode</span><span class="p">.</span><span class="nx">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span> <span class="s2">&quot;switchLang selected&quot;</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">me</span><span class="p">.</span><span class="nx">parentNode</span><span class="p">.</span><span class="nx">childNodes</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span> <span class="s2">&quot;switchLang&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>    <span class="nx">el1</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">display</span> <span class="o">=</span> <span class="s1">&#39;none&#39;</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">el2</span><span class="p">.</span><span class="nx">style</span><span class="p">.</span><span class="nx">display</span> <span class="o">=</span> <span class="s1">&#39;block&#39;</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">me</span><span class="p">.</span><span class="nx">parentNode</span><span class="p">.</span><span class="nx">childNodes</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span> <span class="s2">&quot;switchLang&quot;</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">me</span><span class="p">.</span><span class="nx">parentNode</span><span class="p">.</span><span class="nx">childNodes</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="nx">className</span> <span class="o">=</span> <span class="s2">&quot;switchLang selected&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<figure class='code'><figcaption><span>Require the JS (source/_includes/custom/head.html.diff)</span> <a href='http://www.benjiegillam.com/code/source/_includes/custom/head.html.diff'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='diff'><span class='line'><span class="gh">diff --git a/source/_includes/custom/head.html b/source/_includes/custom/head.html</span>
</span><span class='line'><span class="gh">index 85879f4..96009eb 100644</span>
</span><span class='line'><span class="gd">--- a/source/_includes/custom/head.html</span>
</span><span class='line'><span class="gi">+++ b/source/_includes/custom/head.html</span>
</span><span class='line'><span class="gu">@@ -1,3 +1,7 @@</span>
</span><span class='line'> &lt;!--Fonts from Google&quot;s Web font directory at http://google.com/webfonts --&gt;
</span><span class='line'> &lt;link href=&quot;http://fonts.googleapis.com/css?family=PT+Serif:regular,italic,bold,bolditalic&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;
</span><span class='line'> &lt;link href=&quot;http://fonts.googleapis.com/css?family=PT+Sans:regular,italic,bold,bolditalic&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;
</span><span class='line'><span class="gi">+&lt;script src=&quot;/javascripts/switchlang.js&quot;&gt;&lt;/script&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<figure class='code'><figcaption><span>Add some (terrible) styling (sass/custom/_styles.scss.diff)</span> <a href='http://www.benjiegillam.com/code/sass/custom/_styles.scss.diff'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
</pre></td><td class='code'><pre><code class='diff'><span class='line'><span class="gh">diff --git a/sass/custom/_styles.scss b/sass/custom/_styles.scss</span>
</span><span class='line'><span class="gh">index 680d07f..85db5e3 100644</span>
</span><span class='line'><span class="gd">--- a/sass/custom/_styles.scss</span>
</span><span class='line'><span class="gi">+++ b/sass/custom/_styles.scss</span>
</span><span class='line'><span class="gu">@@ -13,4 +13,102 @@ footer {</span>
</span><span class='line'>          vertical-align:top;
</span><span class='line'>      }
</span><span class='line'>  }
</span><span class='line'><span class="gd">-}</span>
</span><span class='line'>\ No newline at end of file
</span><span class='line'><span class="gi">+}</span>
</span><span class='line'><span class="gi">+</span>
</span><span class='line'><span class="gi">+figure.code figcaption {</span>
</span><span class='line'><span class="gi">+  &amp; a.switchLang {</span>
</span><span class='line'><span class="gi">+    color:white !important;</span>
</span><span class='line'><span class="gi">+    text-shadow: #333 0 1px 0;</span>
</span><span class='line'><span class="gi">+  }</span>
</span><span class='line'><span class="gi">+  span.switchLang {</span>
</span><span class='line'><span class="gi">+    position:absolute;</span>
</span><span class='line'><span class="gi">+    left: .8em;</span>
</span><span class='line'><span class="gi">+    right:auto;</span>
</span><span class='line'><span class="gi">+    padding-left:0;</span>
</span><span class='line'><span class="gi">+    padding-right:3em;</span>
</span><span class='line'><span class="gi">+    -webkit-user-select:none;</span>
</span><span class='line'><span class="gi">+    -moz-user-select:none;</span>
</span><span class='line'><span class="gi">+    user-select:none;</span>
</span><span class='line'><span class="gi">+    -webkit-transition: text-shadow, color 0.3s;</span>
</span><span class='line'><span class="gi">+  }</span>
</span><span class='line'><span class="gi">+  a.switchLang {</span>
</span><span class='line'><span class="gi">+    -webkit-user-select:none;</span>
</span><span class='line'><span class="gi">+    -moz-user-select:none;</span>
</span><span class='line'><span class="gi">+    user-select:none;</span>
</span><span class='line'><span class="gi">+    -webkit-transition: text-shadow, color 0.3s;</span>
</span><span class='line'><span class="gi">+    background-color: #888;</span>
</span><span class='line'><span class="gi">+    position:initial;</span>
</span><span class='line'><span class="gi">+    right:auto;</span>
</span><span class='line'><span class="gi">+    padding:0;</span>
</span><span class='line'><span class="gi">+    padding-left:.8em;</span>
</span><span class='line'><span class="gi">+    padding-right:.8em;</span>
</span><span class='line'><span class="gi">+    //border:1px solid black;</span>
</span><span class='line'><span class="gi">+    display: inline-block;</span>
</span><span class='line'><span class="gi">+    border-top-right-radius:6px;</span>
</span><span class='line'><span class="gi">+    border-bottom-right-radius:6px;</span>
</span><span class='line'><span class="gi">+    border-left:0;</span>
</span><span class='line'><span class="gi">+    height: 1.4em;</span>
</span><span class='line'><span class="gi">+    line-height: 1.4em;</span>
</span><span class='line'><span class="gi">+  }</span>
</span><span class='line'><span class="gi">+  a.switchLang:first-child {</span>
</span><span class='line'><span class="gi">+    //border-right:0;</span>
</span><span class='line'><span class="gi">+    //border-left:1px solid black;</span>
</span><span class='line'><span class="gi">+    border-radius:0;</span>
</span><span class='line'><span class="gi">+    border-top-left-radius:6px;</span>
</span><span class='line'><span class="gi">+    border-bottom-left-radius:6px;</span>
</span><span class='line'><span class="gi">+    </span>
</span><span class='line'><span class="gi">+  }</span>
</span><span class='line'><span class="gi">+  a.switchLang.selected {</span>
</span><span class='line'><span class="gi">+    background-color:#666;</span>
</span><span class='line'><span class="gi">+  }</span>
</span><span class='line'><span class="gi">+}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Thanks for your question, Dru!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Node.JS SSL Certificate Chain]]></title>
    <link href="http://www.benjiegillam.com/2012/06/node-dot-js-ssl-certificate-chain/"/>
    <updated>2012-06-28T09:32:00+01:00</updated>
    <id>http://www.benjiegillam.com/2012/06/node-dot-js-ssl-certificate-chain</id>
    <content type="html"><![CDATA[<p>I&rsquo;ve just discovered that one of our servers is not serving up it&rsquo;s SSL
certificate chain correctly. This is fine for modern web browsers who
trust the COMODO certificate, but for older browsers/operating systems
you need to support higher up the trust chain.</p>

<p>Previously I followed the technique in <a href="http://qugstart.com/blog/node-js/install-comodo-positivessl-certificate-with-node-js/">this article</a>,
but it turns out that the <code>ca</code> parameter of the TLS/HTTPS server <a href="http://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener">should
be an array</a>. More than this, you <strong>cannot</strong>
just feed it an array containing your chain file as a string/buffer
(i.e. <code>[fs.readFileSync("/path/to/mydomain.ca-bundle")]</code>) since the Node
TLS module only reads the first certificate in each entry of this array.</p>

<p>To solve this you can parse your existing certificate chain:</p>

<!-- more -->




<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_77017795');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_77017795');">.js</a></span><span>Parse your certificate chain</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
</pre></td><td class='code'><pre><code class='coffee-script'><span class='line'><span class="nv">https = </span><span class="nx">require</span> <span class="s">&#39;https&#39;</span>
</span><span class='line'><span class="nv">fs = </span><span class="nx">require</span> <span class="s">&#39;fs&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">ca = </span><span class="p">[]</span>
</span><span class='line'><span class="nv">chain = </span><span class="nx">fs</span><span class="p">.</span><span class="nx">readFileSync</span> <span class="s">&quot;/path/to/mydomain.ca-bundle&quot;</span><span class="p">,</span> <span class="s">&#39;utf8&#39;</span>
</span><span class='line'><span class="nv">chain = </span><span class="nx">chain</span><span class="p">.</span><span class="nx">split</span> <span class="s">&quot;\n&quot;</span>
</span><span class='line'><span class="nv">cert = </span><span class="p">[]</span>
</span><span class='line'><span class="k">for</span> <span class="nx">line</span> <span class="k">in</span> <span class="nx">chain</span> <span class="k">when</span> <span class="nx">line</span><span class="p">.</span><span class="nx">length</span> <span class="o">isnt</span> <span class="mi">0</span>
</span><span class='line'>  <span class="nx">cert</span><span class="p">.</span><span class="nx">push</span> <span class="nx">line</span>
</span><span class='line'>  <span class="k">if</span> <span class="nx">line</span><span class="p">.</span><span class="nx">match</span> <span class="sr">/-END CERTIFICATE-/</span>
</span><span class='line'>    <span class="nx">ca</span><span class="p">.</span><span class="nx">push</span> <span class="nx">cert</span><span class="p">.</span><span class="nx">join</span> <span class="s">&quot;\n&quot;</span>
</span><span class='line'>    <span class="nv">cert = </span><span class="p">[]</span>
</span><span class='line'>
</span><span class='line'><span class="nv">httpsOptions =</span>
</span><span class='line'>  <span class="nv">ca: </span><span class="nx">ca</span>
</span><span class='line'>  <span class="nv">key: </span><span class="s">&quot;/path/to/server.key&quot;</span>
</span><span class='line'>  <span class="nv">cert: </span><span class="s">&quot;/path/to/mydomain.crt&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">requestHandler = </span><span class="nf">(req, res) -&gt;</span>
</span><span class='line'>  <span class="nx">res</span><span class="p">.</span><span class="nx">writeHead</span> <span class="mi">501</span>
</span><span class='line'>  <span class="nx">res</span><span class="p">.</span><span class="nx">end</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="nv">httpsServer = </span><span class="nx">https</span><span class="p">.</span><span class="nx">createServer</span> <span class="nx">httpsOptions</span><span class="p">,</span> <span class="nx">requestHandler</span>
</span><span class='line'><span class="nx">httpsServer</span><span class="p">.</span><span class="nx">listen</span> <span class="mi">443</span><span class="p">,</span> <span class="s">&quot;localhost&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can test your certificate chain using this command:</p>

<pre><code>openssl s_client -connect hostname:443 -showcerts | grep "^ "
</code></pre>

<p>An EssentialSSL/COMODO certificate chain would look something like:</p>

<pre><code> 0 s:/OU=Domain Control Validated/OU=Free SSL/CN=example.com
   i:/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=EssentialSSL CA
 1 s:/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=EssentialSSL CA
   i:/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO Certification Authority
 2 s:/C=GB/ST=Greater Manchester/L=Salford/O=COMODO CA Limited/CN=COMODO Certification Authority
   i:/C=US/ST=UT/L=Salt Lake City/O=The USERTRUST Network/OU=http://www.usertrust.com/CN=UTN - DATACorp SGC
 3 s:/C=US/ST=UT/L=Salt Lake City/O=The USERTRUST Network/OU=http://www.usertrust.com/CN=UTN - DATACorp SGC
   i:/C=SE/O=AddTrust AB/OU=AddTrust External TTP Network/CN=AddTrust External CA Root
 4 s:/C=SE/O=AddTrust AB/OU=AddTrust External TTP Network/CN=AddTrust External CA Root
   i:/C=SE/O=AddTrust AB/OU=AddTrust External TTP Network/CN=AddTrust External CA Root
</code></pre>

<p>Alternatively, if you prefer, you could have Node read the certificate
files individually which can be more concise, but requires more maintenance:</p>

<!-- more -->




<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_785107356');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_785107356');">.js</a></span><span>Parse your certificate chain</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='coffee-script'><span class='line'><span class="nv">https = </span><span class="nx">require</span> <span class="s">&#39;https&#39;</span>
</span><span class='line'><span class="nv">fs = </span><span class="nx">require</span> <span class="s">&#39;fs&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">files = </span><span class="p">[</span>
</span><span class='line'>  <span class="s">&quot;EssentialSSLCA_2.crt&quot;</span>
</span><span class='line'>  <span class="s">&quot;ComodoUTNSGCCA.crt&quot;</span>
</span><span class='line'>  <span class="s">&quot;UTNAddTrustSGCCA.crt&quot;</span>
</span><span class='line'>  <span class="s">&quot;AddTrustExternalCARoot.crt&quot;</span>
</span><span class='line'><span class="p">]</span>
</span><span class='line'><span class="nv">ca = </span><span class="p">(</span><span class="nx">fs</span><span class="p">.</span><span class="nx">readFileSync</span> <span class="s">&quot;/path/to/</span><span class="si">#{</span><span class="nx">file</span><span class="si">}</span><span class="s">&quot;</span> <span class="k">for</span> <span class="nx">file</span> <span class="k">in</span> <span class="nx">files</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="nv">httpsOptions =</span>
</span><span class='line'>  <span class="nv">ca: </span><span class="nx">ca</span>
</span><span class='line'>  <span class="nv">key: </span><span class="s">&quot;/path/to/server.key&quot;</span>
</span><span class='line'>  <span class="nv">cert: </span><span class="s">&quot;/path/to/mydomain.crt&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">requestHandler = </span><span class="nf">(req, res) -&gt;</span>
</span><span class='line'>  <span class="nx">res</span><span class="p">.</span><span class="nx">writeHead</span> <span class="mi">501</span>
</span><span class='line'>  <span class="nx">res</span><span class="p">.</span><span class="nx">end</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="nv">httpsServer = </span><span class="nx">https</span><span class="p">.</span><span class="nx">createServer</span> <span class="nx">httpsOptions</span><span class="p">,</span> <span class="nx">requestHandler</span>
</span><span class='line'><span class="nx">httpsServer</span><span class="p">.</span><span class="nx">listen</span> <span class="mi">443</span><span class="p">,</span> <span class="s">&quot;localhost&quot;</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Node, Websockets, Safari and Emoji]]></title>
    <link href="http://www.benjiegillam.com/2012/03/node-websockets-safari-and-emoji/"/>
    <updated>2012-03-05T14:02:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/03/node-websockets-safari-and-emoji</id>
    <content type="html"><![CDATA[<p>It seems <a href="http://en.wikipedia.org/wiki/Emoji">emoji</a> break <a href="https://github.com/LearnBoost/websocket.io">websocket.io</a> (the websocket
library used by socket.io). They seem to cause the message payload to
terminate prematurely.</p>

<p>After a morning of research on the subject, I was pointed at <a href="https://gist.github.com/1707371">mranney&rsquo;s
essay on the subject</a> by <em>`3rdEden</em> on
<a href="irc://irc.freenode.net/socket.io">irc.freenode.net/socket.io</a>. It turns out that the issue
is due to V8 (the JavaScript engine used by Node.JS) using the
<a href="http://en.wikipedia.org/wiki/Universal_Character_Set">UCS</a> encoding internally rather than the more modern UTF-16. Emoji
require 17 bits (their code values are larger than 65,535), which is
more than UCS can give (UCS uses exactly 2 bytes (16 bits) to represent
every character, so it only supports values between 0 and 65,535;
whereas UTF-8 and UTF-16 use a variable number of bytes).</p>

<p>I&rsquo;ve confirmed with WireShark that Safari is sending valid UTF-8
(11110000 10011111 10011000 10010011 or hex: f0 9f 98 93, which
<a href="http://www.ltg.ed.ac.uk/~richard/utf-8.cgi?input=f0+9f+98+93&amp;mode=bytes">gives</a> the Unicode codepoint U+1F613 😓). So I know it&rsquo;s Node&rsquo;s
issue receiving and processing it.</p>

<p>This is probably one of the reasons that Google Chrome doesn&rsquo;t support
Emoji - check out <a href="http://en.wikipedia.org/wiki/Emoji">this page</a> in Chrome, then view it in Safari
to see what you&rsquo;re missing! (Chrome uses the V8 engine.)</p>

<h2>Solution</h2>

<p>You could base64-encode your payload, or simply <code>escape()</code>/<code>unescape()</code>
it. Or you could trim anything outside of UCS. Or you could do a custom
encode/unencode such as <a href="http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt">this one</a>. I&rsquo;m not really happy with
any of these, so I&rsquo;m still looking for a solution.</p>

<h2>Update</h2>

<p>In the end I implemented <a href="http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html">this</a> (see below) on the client
side, and left the data encoded server side. All clients are responsible
for <code>encode()</code>ing characters when sending, and <code>decode()</code>ing upon
receiving (whether that be via websockets or HTTP). It seems to work
quite well and doesn&rsquo;t massively inflate the content-size for ASCII and
possibly more[citation needed], so I&rsquo;m relatively happy with it.
Besides it&rsquo;s midnight and I need to get some sleep.</p>

<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_198513724');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_198513724');">.js</a></span><span>Patch UTF-8 issues</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='coffee-script'><span class='line'><span class="nv">encode = </span><span class="nf">(s) -&gt;</span> <span class="nx">unescape</span> <span class="nb">encodeURIComponent</span> <span class="nx">s</span>
</span><span class='line'><span class="nv">decode = </span><span class="nf">(s) -&gt;</span> <span class="nb">decodeURIComponent</span> <span class="nx">escape</span> <span class="nx">s</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Switching to Vim: The Easy Way - Commands]]></title>
    <link href="http://www.benjiegillam.com/2012/02/switching-to-vim-the-easy-way-ii/"/>
    <updated>2012-02-27T16:50:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/02/switching-to-vim-the-easy-way-ii</id>
    <content type="html"><![CDATA[<p>Continuing from what we learned in <a href="http://www.benjiegillam.com/2012/02/switching-to-vim-the-easy-way/">part 1</a> (installing and
vimtutor), I&rsquo;d now like to focus on the fundamental commands in Vim.</p>

<h2>Commands</h2>

<p>Vim commands are formed from a combination of <em>verbs</em> and <em>targets</em>. The
targets could be <em>objects</em> (words, sentences, paragraphs, lines, the
contents of parentheses) or <em>movements</em> (jump to end of word, jump to end
of paragraph, jump forward until the letter &lsquo;e&rsquo;, etc). Forming objects
generally involves the use of a <em>modifier</em>. You can also add
a <em>count</em> to perform the action <em>count</em> times.</p>

<h3>Verbs</h3>

<p>Here&rsquo;s some example verbs (don&rsquo;t try and learn them all at once!):</p>

<!--more-->


<ul>
<li><code>i</code> - insert - enter <em>insert mode</em></li>
<li><code>a</code> - append - enter <em>insert mode</em> after the carat</li>
<li><code>I</code> - enter <em>insert mode</em> at the beginning of the line</li>
<li><code>A</code> - append to line (enter <em>insert mode</em> at the end of the line)</li>
<li><code>o</code> - open a line after the current one and enter <em>insert mode</em></li>
<li><code>O</code> - open a line before the current one and enter <em>insert mode</em></li>
<li><code>d</code>[target] - delete (remove from the document and put in buffer)</li>
<li><code>y</code>[target] - yank (copy)</li>
<li><code>p</code> - paste the buffer after the cursor</li>
<li><code>P</code> - paste the buffer before the cursor</li>
<li><code>c</code>[target] - change (delete and then enter <em>insert mode</em>)</li>
<li><code>r</code>[char] - replace the character under the cursor with [char]</li>
<li><code>x</code> - delete the character under the cursor</li>
<li><code>u</code> - undo the last command</li>
<li><code>Ctrl-R</code> - redo the last undo (sidenote: in vim undo/redo forms a tree, changes aren&rsquo;t lost)</li>
<li><code>/</code> - enter regex search mode</li>
<li><code>n</code> - find the next instance of the search term</li>
<li><code>N</code> - find the previous instance of the search term</li>
<li><code>.</code> - repeat last change (extremely powerful)</li>
</ul>


<h3>Nouns/Movements</h3>

<p>Nouns or movements are commands for moving within the document or
representing an area within a document. Common movements are:</p>

<ul>
<li><code>h</code>, <code>j</code>, <code>k</code>, <code>l</code> - equivalent to the arrow keys left, down, up,
right - you should aim to use these rather than the arrow keys, but
don&rsquo;t worry too much yet!</li>
<li><code>0</code> - move to the very beginning of the current line</li>
<li><code>^</code> - move to the first non-whitespace character on the line</li>
<li><code>$</code> - move to the end of the line</li>
<li><code>w</code>, <code>b</code> - move to the next/previous word</li>
<li><code>W</code>, <code>B</code> - as <code>w</code>/<code>b</code> only Words are bigger</li>
<li><code>)</code>, <code>(</code> - move to the next/previous sentence</li>
<li><code>}</code>, <code>{</code> - move to the next/previous paragraph</li>
<li><code>f</code>/<code>F</code>[char] - find the next/previous instance of [char] in the current line</li>
<li><code>t</code>/<code>T</code>[char] - un<strong>t</strong>il - find the character before the next/after the
previous instance of [char]</li>
<li><code>/</code>[regexp]<Enter> - like <code>t</code> but instead of finding a character it finds a
regexp</li>
<li><code>%</code> - jump to the matching parenthesis (vim understands nested
parenthesis)</li>
<li><code>_</code> - move to the current line (useful for making commands
line-based)</li>
</ul>


<h3>Trying it out</h3>

<p>Try some of these movements out now - you can start to imagine how much faster
document navigation will be once you&rsquo;ve mastered them.</p>

<p><span class='pullquote-right' data-pullquote='Once you&#8217;ve got a feel for the movements, try combining them with verbs.'>
Once you&rsquo;ve got a feel for the movements, try combining them with verbs.
Don&rsquo;t forget that if you do something wrong (or don&rsquo;t understand what
happened) you can simply press <code>u</code> to undo, and try it again. For example:
</span></p>

<ul>
<li><code>cw</code> - &ldquo;change&rdquo; until the end of the current &ldquo;word&rdquo;</li>
<li><code>d}</code> - delete until the next paragraph (note that this is line-based
delete, so it will delete the whole of the current line too)</li>
<li><code>d_</code> - delete the current line (this can also be entered as <code>dd</code> for
brevity)</li>
<li><code>y_</code> - yank the current line (this can also be entered as <code>yy</code> for
brevity)</li>
<li><code>p</code>/<code>P</code> - paste the previously yanked (or deleted) text. If
the yank (or delete) was character based then it will paste after/before the
cursor, otherwise it will paste on the next/previous line.</li>
<li><code>27x</code> - delete the next 27 characters</li>
</ul>


<h3>Counts</h3>

<p>You can generally precede a verb or a movement with a count, which will
perform that action that many times. For example:</p>

<ul>
<li><code>3w</code> move to the third word from here</li>
<li><code>3j</code> move the cursor three lines down</li>
<li><code>3}</code> move to the 3rd paragraph from here</li>
<li><code>3i</code> will write whatever you insert 3 times. (The duplication will
not occur until you exit <em>insert mode</em>, e.g. with <code>&lt;Esc&gt;</code>.)</li>
<li><code>c3w</code> will change the next 3 words (i.e. it will delete 3 words and
then enter <em>insert mode</em>).</li>
<li><code>3dd</code> will delete the current line and the two below it</li>
<li><code>3p</code> will paste the contents of the buffer three times</li>
</ul>


<h3>Modifiers</h3>

<p>Modifiers can be used between a verb and a noun to modify its meaning.
For example, let&rsquo;s look at <code>i</code>[noun]  (inner/inside):</p>

<ul>
<li><code>ciw</code> - change the whole word the cursor is on, even if the cursor is
in the middle of the word</li>
<li><code>ci)</code> - change the contents of the current pair of brackets
(understands bracket pairing)</li>
<li><code>ci"</code> - change the contents of the current pair of doublequotes
(understands quote escaping)</li>
</ul>


<p>another modifier is <code>a</code> - around - like <code>i</code> but includes the container.</p>

<h2>Next time</h2>

<p>In my next post, I&rsquo;ll talk about some of the ways I&rsquo;ve used these
basic commands to speed up my editing.</p>

<!--
Next time
---------

In the next posts I plan to cover:

 * visual mode
 * accessing help
 * some of the commands you can enter in *command-line mode*
 * modifying your vimrc, e.g. remapping commands
 * buffers
 * macros
 * `.`
-->



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Switching to Vim: The Easy Way - Installing/Vimtutor]]></title>
    <link href="http://www.benjiegillam.com/2012/02/switching-to-vim-the-easy-way/"/>
    <updated>2012-02-24T10:20:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/02/switching-to-vim-the-easy-way</id>
    <content type="html"><![CDATA[<p>This post is the first in a series aiming to encourage you to switch to
Vim without going through that first period of significantly reduced
productivity that you&rsquo;re bound to go through if you follow certain Vim
gurus advice such as disabling your arrow keys and what not. (This is
good advice once you&rsquo;ve fully mastered the basics of Vim as a way to
kick bad habits, but I certainly wouldn&rsquo;t advise starting that way!)</p>

<h2>Installing Vim</h2>

<p>I recommend you start with a graphical Vim app so that you can use your
mouse to click around. Ultimately you will rarely if ever use your mouse
(moving your had that far will seem really inefficient) but to start
with it can significantly alleviate the frustration of being slow with
the movement commands.</p>

<h3>Mac</h3>

<p>If you use <a href="http://mxcl.github.com/homebrew/">Homebrew</a> then you can simply issue</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ brew install macvim</span></code></pre></td></tr></table></div></figure>


<p>And voila, you&rsquo;re done. Otherwise take a look at the <a href="https://github.com/b4winckler/macvim">MacVim</a>
instructions.</p>

<!-- more -->


<p></p>

<h3>Linux</h3>

<p>You&rsquo;re going to want gVim, probably. Install it through your usual
package manager.</p>

<h3>Windows</h3>

<p>You&rsquo;re on <a href="http://www.vim.org/download.php#pc">your own</a>.</p>

<h3>Themes and Plugins</h3>

<p>To get you off to a running start, I highly recommend using the <a href="https://github.com/carlhuda/janus">Janus
vim distribution</a> for all your plugin needs. You can install more
plugins later, but these will cover all the basics you&rsquo;re likely to need
including syntax highlighting for a huge number of languages (even stuff
like Markdown files) and a vast array of colour schemes (I like
<code>ir_black</code>).</p>

<p>If security is not a concern for you then you can run their installer
via the following command:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>$ curl -Lo- http://bit.ly/janus-bootstrap | bash</span></code></pre></td></tr></table></div></figure>


<h2>Vimtutor</h2>

<p><span class='pullquote-right' data-pullquote='if you haven&#8217;t used a modal editor before then I assure you it will be worth the time spent learning it'>
The first thing you should do is to try out <code>vimtutor</code> - just run it
from the command line assuming it was installed with Vim. Spend 25
minutes going through everything to get a feel for Vim. Don&rsquo;t worry if
it doesn&rsquo;t all click into place at first - you can use Vim just like a
plain text editor at first, and slowly improve your skill over time. If
you&rsquo;re a programmer and
if you haven&rsquo;t used a modal editor before then I assure you it will be worth the time spent learning it.
</span></p>

<h2>Absolute basics</h2>

<p>Initially you only need to worry about two of vims modes: <em>normal mode</em>
and <em>insert mode</em>.</p>

<h3>Normal mode</h3>

<p>This is the mode that vim opens in. Here you can type commands. Typing
<code>:w&lt;Enter&gt;</code> saves the current document. Typing <code>i</code> puts you into <em>insert
mode</em>.</p>

<h3>Insert mode</h3>

<p><span class='pullquote-right' data-pullquote='Initially you can just treat it like a plain text editor'>
<em>Insert mode</em> is where you enter text.
Initially you can just treat it like a plain text editor
- write stuff and use the arrow keys to move around. When
you want to save, press <code>&lt;Esc&gt;</code> to exit back to <em>normal mode</em> and then
type <code>:w&lt;Enter&gt;</code> to save. You can then press <code>i</code> to get back into <em>insert
mode</em>.
</span></p>

<p>When you are in <em>insert mode</em> you will see <code>-- INSERT --</code> displayed at the
bottom of the screen. Remember: to exit <em>insert mode</em> just press <code>&lt;Esc&gt;</code>.</p>

<h3>Moving on</h3>

<p>Take a while to master the above before moving on. It doesn&rsquo;t matter how
long it takes you - the key is to avoid the frustration of having
deminished productivity by trying to do too much at once.</p>

<p>My next post will focus on the fundamental commands in Vim, like those
you touched upon in vimtutor.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[12 Years Together!]]></title>
    <link href="http://www.benjiegillam.com/2012/02/12-years-together/"/>
    <updated>2012-02-14T00:38:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/02/12-years-together</id>
    <content type="html"><![CDATA[<p>Today marks the 12th anniversary of Jem and I being a couple. We met
when we were 13 on a Scout camp site near Southampton and immediately
became pen-pals. We wrote each other frequently, meeting up when we
could, and soon fell in love. That love, despite being the painfully
intense all-encompassing love of teenagers, has matured, strengthened
and grown over time. We&rsquo;ve now been married for over 3 years and our son
Xander is 16 months old.</p>

<p>I want to take this opportunity to thank my wife for everything. Her
constant loving support helps me in every aspect of my life from
fatherhood to business to helping me deal with my health problems. I
honestly don&rsquo;t know what I would do without her.</p>

<p style='font-size:4em;text-align:center;font-weight:bold;line-height:1.3em;margin-bottom:0.3em'>&#10084; Thank You, Jem &#10084;</p>


<p>You&rsquo;re the most wonderful woman I know - beautiful, funny, clever,
loving, honest, understanding and a fantastic wife and mother.  Every
day I feel grateful to have you in my life and I truly hope I make you
feel loved and appreciated. Together we make a brilliant team - the
future never scares me, because I know whatever happens you&rsquo;ll be there,
by my side.</p>

<h2 style='background:none'>
To me, you are perfect; I love you now, always and forever.
</h2>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[A Plugin-Free Web]]></title>
    <link href="http://www.benjiegillam.com/2012/02/a-plugin-free-web/"/>
    <updated>2012-02-06T11:11:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/02/a-plugin-free-web</id>
    <content type="html"><![CDATA[<p>It&rsquo;s almost unheard of for me to complement something that Microsoft are
doing - especially when it comes to Internet Explorer - but I&rsquo;m 100%
behind a <a href="http://daringfireball.net/linked/2012/02/02/plugin-free-web">plugin-free web</a>:</p>

<blockquote><p>John Hrvatin from the Internet Explorer team:</p><p><blockquote> The transition to a plug-in free Web is happening today. Any site that uses plug-ins needs to understand what their customers experience when browsing plug-in free. Lots of Web browsing today happens on devices that simply don’t support plug-ins. Even browsers that do support plug-ins offer many ways to run plug-in free.</p><p>Metro style IE runs plug-in free to improve battery life as well as security, reliability, and privacy for consumers.<br/></blockquote></p><p>How long until Google joins the party?</p><footer><strong>John Gruber</strong> <cite><a href='http://daringfireball.net/linked/2012/02/02/plugin-free-web'>Microsoft Pushes for Plugin-Free Web</a></cite></footer></blockquote>


<p>Frequently friends and acquaintances will talk to me about iOS/Android
(being an iOS developer) and one of the things they&rsquo;ll very often say is
&ldquo;yeah, but iOS can&rsquo;t run Flash,&rdquo; as if that&rsquo;s a bad thing! Banning Flash
from the platform is one of the things that makes me really glad I own
iOS devices and that they&rsquo;ve such a large market share. Many websites
that use Flash are being forced to redevelop into open and
standards-compliant HTML &amp; JS so that they can capture the mobile
audience.</p>

<p><span class='pullquote-right' data-pullquote='If a shopping website forces me to use Flash to shop there then I simply don&#8217;t shop there'>
Flash is the most common browser plugin, and I loathe it. I block it on
all my computers and only use it where I absolutely have to.
If a shopping website forces me to use Flash to shop there then I simply don&rsquo;t shop there.
And with HTML5 Video/Audio and the speed of JavaScript
in todays modern browsers (even the latest Internet Explorers!) there&rsquo;s
less and less justification for websites to use Flash (or worse:
Silverlight! I&rsquo;m looking at you, <a href="http://blog.lovefilm.com/uncategorized/why-were-switching-from-flash-to-silverlight.html">LoveFilm</a>).
</span></p>

<p>I&rsquo;m not the only person who hates Flash, just look at <a href="http://www.google.co.uk/search?q=flash+block">all the plugins to
disable it</a>!</p>

<h2>Why Plugins Suck</h2>

<p>There&rsquo;s many reasons, but mostly it comes down to non-compatibility,
performance, security and user experience.</p>

<h2>Compatibility</h2>

<p>For a long time there was no Flash player for Linux. Then
there was no Flash player for 64-bit operating systems. There&rsquo;s no Flash
for iOS. My Blu-ray player can&rsquo;t run Flash. Apparently my Tivo does run
Flash, but it&rsquo;s god-awful. <!-- more --></p>

<p>And because Flash runs in a different rendering pipeline it doesn&rsquo;t
integrate well with web browsers - especially when it comes to
transparency. (See &ldquo;User Experience&rdquo;)</p>

<h2>Performance</h2>

<p>A lot of devices are easily capable of rendering HTML/CSS/JS but Flash
pushes them too hard and the experience is ugly and slow. And Flash
seems to be used for a whole host of things that HTML/CSS/JS have been
capable of for years and years across all major browsers - one thing
that comes to mind is all the advertising you get on certain websites.</p>

<h2>Security</h2>

<p><span class='pullquote-right' data-pullquote='sharing your webcam feed without your permission'>
Bugs in plugins expose your browser to a lot of potential security risks -
<a href="http://www.google.co.uk/search?&amp;q=flash+xss">cross site scripting</a> (XSS) attacks, <a href="http://www.google.co.uk/search?q=flash+information+disclosure">information
disclosure</a>, even
sharing your webcam feed without your permission
(<a href="http://news.yahoo.com/blogs/technology-blog/adobe-flash-exploit-allows-websites-access-webcam-without-010049284.html">October 2011</a>)! Over
the years a lot of these issues have been patched, but it should be the
job of the web browser to allow/deny access to various resources, having
two interfaces increases the attack surface - it&rsquo;s risky and confusing.
</span></p>

<h2>User Experience</h2>

<p>When hover to expand adverts first came out they used to actually block
the viewing of the content on some websites in some browsers - so I
simply stopped visiting those website. What&rsquo;s the point if you can&rsquo;t
actually read the content?!</p>

<p>Once you&rsquo;ve got 10 Flash adverts on a web-page the scroll performance
drops significantly, even on a dual core computer. There&rsquo;s also the
increased render time of the webpage as a whole, and the annoying (and
unnecessary!) loading indicators.</p>

<p>When you close a browser tab, plugins on that page playing audio/etc
often won&rsquo;t close until a couple of seconds later, which can be very
irritating. And don&rsquo;t get me started on websites that suddenly start
talking to you without you asking them to! (Though this is possible with
HTML5 also&hellip;)</p>

<h2>Acceptable Use Cases</h2>

<p>There used to be lots of use-cases for which I found the use of Flash
acceptable, but many of these are being diminished due to HTML5 adding
extra capabilities to the browser:</p>

<ul>
<li>3D browser games (soon to be invalid due to <a href="http://get.webgl.org/">WebGL</a> and similar technologies)</li>
<li>streaming video/audio (now supported by <a href="https://developer.mozilla.org/en/Using_HTML5_audio_and_video">HTML5 audio and
video</a>)</li>
<li>video conferencing (there&rsquo;s many workarounds for this - W3 are
attempting to create a <a href="http://www.w3.org/TR/html-media-capture/">Media Capture API</a>)</li>
<li>real-time socket communications (<a href="http://www.w3.org/TR/websockets/">WebSockets</a> allow real time
communications by extending HTTP)</li>
</ul>


<h2>Progress</h2>

<p>Apple, Microsoft I applaud your efforts to rid the internet of these
plugins (despite the existence of QuickTime, Silverlight and the like).</p>

<p>Google, I&rsquo;m disappointed with you integrating Flash so deeply into
Chrome, but Chrome&rsquo;s an awesome browser for development so I shall use it
anyway. Good work on making YouTube work with HTML5 though :)</p>

<p>HTML5 guys, you all rock.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Happy New Year 2012]]></title>
    <link href="http://www.benjiegillam.com/2012/02/happy-new-year-2012/"/>
    <updated>2012-02-04T18:57:00+00:00</updated>
    <id>http://www.benjiegillam.com/2012/02/happy-new-year-2012</id>
    <content type="html"><![CDATA[<p><span class='pullquote-right' data-pullquote='Xander&#8217;s walking well and he&#8217;s getting closer to speech every day'>
How&rsquo;s your year going so far? Mine&rsquo;s going really well.
Xander&rsquo;s walking well and he&rsquo;s getting closer to speech every day
which is fascinating and exciting to watch, my health has improved
massively over last year and I&rsquo;m really enjoying work.
</span></p>

<p><span class='pullquote-right' data-pullquote='making an effort to do the things I&#8217;ve always meant to'>
This year I&rsquo;m
making an effort to do the things I&rsquo;ve always meant to
do as a programmer; and playing with lots of new (to me)
tech in the process! I intend to cover a few of these topics in the coming
weeks/months, but here&rsquo;s a summary:
</span></p>

<h2>Vim</h2>

<p><span class='pullquote-right' data-pullquote='you don&#8217;t have to jump in at the deep end'>
I&rsquo;ve started using Vim as my main editor (or more specifically
<a href="https://github.com/b4winckler/macvim">MacVim</a> with the <a href="https://github.com/carlhuda/janus">Janus vim distribution</a>) and I&rsquo;m
loving it. It&rsquo;s a relatively steep learning curve, but
you don&rsquo;t have to jump in at the deep end
 - just start with <code>i</code> to enter insert mode, use
it like a normal editor, and then to save press <code>&lt;Esc&gt;</code> followed by
<code>:wq&lt;Enter&gt;</code> to save and exit (drop the <code>q</code> if you don&rsquo;t want to exit).
Slowly start learning new things and forcing yourself to start using
normal mode (where you type commands like <code>:wq</code>) and as you use more,
you&rsquo;ll want to use more. Once you&rsquo;ve mastered the whole
count-verb-modifier-noun grammar of vim you&rsquo;ll be using it for
everything and wondering how you ever managed without it!
</span></p>

<h2>Node.js</h2>

<p><span class='pullquote-right' data-pullquote='anything that needs scripting I now do in JavaScript'>
Okay, so I&rsquo;ve been playing with this for a while, but I&rsquo;m using it for
more and more stuff now - pretty much
anything that needs scripting I now do in JavaScript, or more specifically&hellip;
</span></p>

<h2>CoffeeScript</h2>

<p><span class='pullquote-right' data-pullquote='I&#8217;m now using it for ALL my JavaScript programming'>
&hellip; <a href="http://coffeescript.org/">CoffeeScript</a>, which is bloody brilliant - especially
if you have a strong understanding of how the underlying JavaScript
functions. This isn&rsquo;t new either, but
I&rsquo;m now using it for ALL my JavaScript programming
</span></p>

<h2>Underscore.js</h2>

<p><a href="http://documentcloud.github.com/underscore/">Underscore.js</a> is a light library of helpful JavaScript
functions for working with objects, arrays, collections and other such
things in a functional programming way <strong>without</strong> extending the native
objects. jQuery/Mootools use <code>$</code> for working with DOM nodes, Underscore
uses <code>_</code> for dealing with everything else.</p>

<h2>Backbone.js</h2>

<p><a href="http://documentcloud.github.com/backbone/">Backbone.js</a> extends Underscore.js and is a great way of
keeping the code of your <abbr title="Rich Internet Applications">RIAs</abbr> organised.
It&rsquo;s very flexible and can fit to many different programming
methodologies, but I use it in a M-VC (Model, ViewController) manner not
dissimilar to iOS. Event are emitted from models when they&rsquo;re added,
changed or deleted; hooking into these events allows your app to always
display consistent information without the need for lots of hard-coded
callbacks.</p>

<h2>jQuery</h2>

<p>It seems everybody uses jQuery, and has done since its inception. I
refused to jump on the band-wagon and I&rsquo;m glad I did - I coded
everything in JavaScript from scratch using native methods and help
from articles across the web (especially sites such as
<a href="http://www.quirksmode.org/">QuirksMode</a>) and now have a very good understanding of how
different parts of a modern web browser piece together and the
faster/slower ways to go about doing things.</p>

<p><span class='pullquote-right' data-pullquote='reduces programming time significantly and adds flexibility'>
However, jQuery is a lot faster than it used to be, as are modern web
browsers, so my concerns are much less valid. It
reduces programming time significantly and adds flexibility
so I now use it (or
<a href="http://zeptojs.com/">Zepto.js</a>) in a lot of my projects and I really like it - it
doesn&rsquo;t take long to learn and master, and it improves the speed of
Backbone development considerably.
</span></p>

<h2>Git</h2>

<p><span class='pullquote-right' data-pullquote='brilliant - lightening fast and so flexible'>
Git, as you should know, is a brilliant version control software. I&rsquo;ve
been using SVN for years but have had the itch to switch for ages, so
with some new projects that I&rsquo;ve been working on (using the above
technologies) I decided to switch to using git where possible. It&rsquo;s
brilliant - lightening fast and so flexible. The lack of the old
<code>/trunk/</code>, <code>/branches/</code>, <code>/tags/</code> layout makes me happy too - trying to
convince employees to use <code>svn switch</code> was very hard, but with <code>git</code>
they&rsquo;ve got no choice but to do it the right way! I&rsquo;m using
<a href="https://github.com/sitaramc/gitolite">gitolite</a> for our internal (private) hosting, and &hellip;
</span></p>

<h2>Open Sourcing</h2>

<p><span class='pullquote-right' data-pullquote='I intend to release much more in the coming year'>
&hellip; I&rsquo;ve started Open Sourcing some stuff I&rsquo;m working on to GitHub.
First is my <a href="https://github.com/BenjieGillam/ec2-prune-snapshots">ec2-prune-snapshots</a> script for
automatically deleting sensibly old <abbr title="Elastic Block Storage">EBS</abbr>
snapshots on <a href="https://aws.amazon.com/">AWS</a>, but
I intend to release much more in the coming year, and
possibly even retro-actively releasing some older software,
such as <a href="http://code.google.com/p/mythpywii/">MythPyWii</a> which currently sits on Google Code.
</span></p>

<h2>What Are You Using?</h2>

<p>If you&rsquo;re using some cool technology for web/mobile application
development that you think I might enjoy experimenting with then please
let me know in the cooments!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Multiple Asynchronous Callbacks]]></title>
    <link href="http://www.benjiegillam.com/2011/11/multiple-asynchronous-callbacks/"/>
    <updated>2011-11-23T10:01:00+00:00</updated>
    <id>http://www.benjiegillam.com/2011/11/multiple-asynchronous-callbacks</id>
    <content type="html"><![CDATA[<p>When programming in JavaScript (or CoffeeScript) you sometimes face a
situation where you need to complete multiple independant asynchronous
methods before continuing. This situation crops up in web browsers but
its much more common when writing server-side JavaScript, e.g. with
<a href="http://nodejs.org/">Node.js</a> - for example you might need to fetch from a
database, fetch from a <abbr title="Key value store, e.g. Redis/Memcached">KVS</abbr>,
read a file and perform a remote HTTP request before outputting the
compiled information to the end user.</p>

<p>One method of solving this issue is to chain the asynchronous calls, however this
means that they&rsquo;re run one after the other (serially) - and thus it will
take longer to complete them. A better way would be to run them in
parallel and have something track their completion. This is exactly what
my very simple <code>AsyncBatch</code> class does:</p>

<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_531106522');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_531106522');">.js</a></span><span>AsyncBatch class, triggers event once all wrapped callbacks complete</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='coffee-script'><span class='line'><span class="k">class</span> <span class="nx">AsyncBatch</span> <span class="k">extends</span> <span class="nx">EventEmitter</span>
</span><span class='line'>  <span class="nv">constructor: </span><span class="o">-&gt;</span>
</span><span class='line'>    <span class="vi">@_complete = </span><span class="p">{}</span>
</span><span class='line'>    <span class="vi">@_scheduled = </span><span class="p">{}</span>
</span><span class='line'>
</span><span class='line'>  <span class="nv">wrap: </span><span class="nf">(name,cb) -&gt;</span>
</span><span class='line'>    <span class="nx">@_scheduled</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span>
</span><span class='line'>    <span class="k">return</span> <span class="o">=&gt;</span>
</span><span class='line'>      <span class="nx">@_complete</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">cb</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">@</span><span class="p">,</span> <span class="nx">arguments</span>
</span><span class='line'>      <span class="k">if</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">@_complete</span><span class="p">).</span><span class="nx">length</span> <span class="o">==</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">@_scheduled</span><span class="p">).</span><span class="nx">length</span>
</span><span class='line'>        <span class="nx">@emit</span> <span class="s">&#39;done&#39;</span><span class="p">,</span> <span class="nx">@_complete</span>
</span></code></pre></td></tr></table></div></figure>


<p>To use AsyncBatch, just do the following</p>

<figure class='code'><figcaption><span class="switchLang"><a class="switchLang selected" onclick="switchLang(event,this,'figure_918439754');">.coffee</a><a class="switchLang" onclick="switchLang(event,this,'figure_918439754');">.js</a></span><span>How to use AsyncBatch</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='coffee-script'><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="cm">Create a new AsyncBatch instance</span>
</span><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="nv">batch = </span><span class="k">new</span> <span class="nx">AsyncBatch</span>
</span><span class='line'>
</span><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="cm">Wrap your callbacks with batch.wrap and a name</span>
</span><span class='line'><span class="cm">The name is used to store the result returned by your callback</span>
</span><span class='line'><span class="cm">(Don&#39;t forget to return the result you&#39;re interested in!)</span>
</span><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="nx">delay</span> <span class="mi">50</span><span class="p">,</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">wrap</span> <span class="s">&#39;timer&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>  <span class="k">return</span> <span class="s">&quot;Timer complete&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="cm">Add a completion handler that accepts `results` as a parameter.</span>
</span><span class='line'><span class="cm">  `results` is a JS object where the keys are the callback names </span>
</span><span class='line'><span class="cm">  from above and the values are the return values of the</span>
</span><span class='line'><span class="cm">  callbacks.</span>
</span><span class='line'><span class="cm">###</span>
</span><span class='line'><span class="nx">batch</span><span class="p">.</span><span class="kc">on</span> <span class="s">&#39;done&#39;</span><span class="p">,</span> <span class="nf">(results) -&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;Batch complete, timer result: </span><span class="si">#{</span><span class="nx">results</span><span class="p">.</span><span class="nx">timer</span><span class="si">}</span><span class="s">&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p><strong>NOTE</strong>: If your asynchronous method accepts both a success and
  failure callback then simply wrap both individually but
  ensure you use the same name for both.</p>

<p><strong>NOTE</strong>: Other than the case in the previous NOTE, all callbacks
  should have different names.</p>

<p>A full example (including a stub <code>EventEmitter</code> implementation) follows:</p>

<!-- more -->




<figure class='code'><figcaption><span>An example of how to use AsyncBatch to handle parallel callbacks in Node.JS  (async-batch.coffee)</span> <a href='http://www.benjiegillam.com/code/async-batch.coffee'>download</a></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
<span class='line-number'>76</span>
<span class='line-number'>77</span>
<span class='line-number'>78</span>
<span class='line-number'>79</span>
<span class='line-number'>80</span>
<span class='line-number'>81</span>
<span class='line-number'>82</span>
<span class='line-number'>83</span>
</pre></td><td class='code'><pre><code class='coffee-script'><span class='line'><span class="c1">#EventEmitter = require(&#39;events&#39;).EventEmitter</span>
</span><span class='line'>
</span><span class='line'><span class="k">if</span> <span class="o">!</span><span class="nx">EventEmitter</span><span class="o">?</span>
</span><span class='line'>  <span class="k">class</span> <span class="nx">EventEmitter</span>
</span><span class='line'>    <span class="kc">on</span><span class="o">:</span> <span class="nf">(eventName, cb) -&gt;</span>
</span><span class='line'>      <span class="vi">@_eventCallbacks = </span><span class="p">{}</span> <span class="nx">unless</span> <span class="nx">@_eventCallbacks</span>
</span><span class='line'>      <span class="nx">@_eventCallbacks</span><span class="p">[</span><span class="nx">eventName</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span> <span class="nx">unless</span> <span class="nx">@_eventCallbacks</span><span class="p">[</span><span class="nx">eventName</span><span class="p">]</span>
</span><span class='line'>      <span class="nx">@_eventCallbacks</span><span class="p">[</span><span class="nx">eventName</span><span class="p">].</span><span class="nx">push</span> <span class="nx">cb</span>
</span><span class='line'>    <span class="nv">emit: </span><span class="nf">(eventName, args...) -&gt;</span>
</span><span class='line'>      <span class="k">if</span> <span class="nx">@_eventCallbacks</span> <span class="o">&amp;&amp;</span> <span class="nx">@_eventCallbacks</span><span class="p">[</span><span class="nx">eventName</span><span class="p">]</span>
</span><span class='line'>        <span class="nx">cb</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">@</span><span class="p">,</span> <span class="nx">args</span> <span class="k">for</span> <span class="nx">cb</span> <span class="k">in</span> <span class="nx">@_eventCallbacks</span><span class="p">[</span><span class="nx">eventName</span><span class="p">]</span>
</span><span class='line'>
</span><span class='line'><span class="k">class</span> <span class="nx">AsyncBatch</span> <span class="k">extends</span> <span class="nx">EventEmitter</span>
</span><span class='line'>  <span class="nv">constructor: </span><span class="o">-&gt;</span>
</span><span class='line'>    <span class="vi">@_complete = </span><span class="p">{}</span>
</span><span class='line'>    <span class="vi">@_scheduled = </span><span class="p">{}</span>
</span><span class='line'>
</span><span class='line'>  <span class="nv">wrap: </span><span class="nf">(name,cb) -&gt;</span>
</span><span class='line'>    <span class="nx">@_scheduled</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span>
</span><span class='line'>    <span class="k">return</span> <span class="o">=&gt;</span>
</span><span class='line'>      <span class="nx">@_complete</span><span class="p">[</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">cb</span><span class="p">.</span><span class="nx">apply</span> <span class="nx">@</span><span class="p">,</span> <span class="nx">arguments</span>
</span><span class='line'>      <span class="k">if</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">@_complete</span><span class="p">).</span><span class="nx">length</span> <span class="o">==</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">@_scheduled</span><span class="p">).</span><span class="nx">length</span>
</span><span class='line'>        <span class="nx">@emit</span> <span class="s">&#39;done&#39;</span><span class="p">,</span> <span class="nx">@_complete</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="nv">batch = </span><span class="k">new</span> <span class="nx">AsyncBatch</span>
</span><span class='line'>
</span><span class='line'><span class="cm">########################</span><span class="c1">##</span>
</span><span class='line'><span class="c1"># Task 1</span>
</span><span class='line'><span class="nv">http = </span><span class="nx">require</span> <span class="s">&#39;http&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">options =</span>
</span><span class='line'>  <span class="nv">host: </span><span class="s">&#39;www.example.com&#39;</span>
</span><span class='line'>  <span class="nv">port: </span><span class="mi">80</span>
</span><span class='line'>  <span class="nv">path: </span><span class="s">&#39;/&#39;</span>
</span><span class='line'>  <span class="nv">method: </span><span class="s">&#39;GET&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="nv">req = </span><span class="nx">http</span><span class="p">.</span><span class="nx">request</span> <span class="nx">options</span><span class="p">,</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">wrap</span> <span class="s">&#39;status&#39;</span><span class="p">,</span> <span class="nf">(res) -&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&#39;STATUS: &#39;</span> <span class="o">+</span> <span class="nx">res</span><span class="p">.</span><span class="nx">statusCode</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&#39;HEADERS: &#39;</span> <span class="o">+</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">res</span><span class="p">.</span><span class="nx">headers</span><span class="p">)</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">res</span><span class="p">.</span><span class="nx">statusCode</span>
</span><span class='line'>
</span><span class='line'><span class="nx">req</span><span class="p">.</span><span class="kc">on</span> <span class="s">&#39;error&#39;</span><span class="p">,</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">wrap</span> <span class="s">&#39;status&#39;</span><span class="p">,</span> <span class="nf">(e) -&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&#39;problem with request: &#39;</span> <span class="o">+</span> <span class="nx">e</span><span class="p">.</span><span class="nx">message</span>
</span><span class='line'>  <span class="k">return</span> <span class="kc">null</span>
</span><span class='line'>
</span><span class='line'><span class="nx">req</span><span class="p">.</span><span class="nx">end</span><span class="p">()</span>
</span><span class='line'>
</span><span class='line'><span class="cm">########################</span><span class="c1">#</span>
</span><span class='line'><span class="c1"># Task 2</span>
</span><span class='line'><span class="nv">fs = </span><span class="nx">require</span> <span class="s">&#39;fs&#39;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">fs</span><span class="p">.</span><span class="nx">readFile</span> <span class="s">&#39;/etc/hosts&#39;</span><span class="p">,</span> <span class="s">&#39;utf8&#39;</span><span class="p">,</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">wrap</span> <span class="s">&#39;hosts&#39;</span><span class="p">,</span> <span class="nf">(err, data) -&gt;</span>
</span><span class='line'>  <span class="k">if</span> <span class="nx">err</span>
</span><span class='line'>    <span class="k">return</span> <span class="kc">null</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="nx">data</span>
</span><span class='line'>  <span class="nv">tmp = </span><span class="nx">data</span><span class="p">.</span><span class="nx">split</span> <span class="s">&quot;\n&quot;</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">tmp</span><span class="p">[</span><span class="nb">Math</span><span class="p">.</span><span class="nx">floor</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="o">*</span><span class="nx">tmp</span><span class="p">.</span><span class="nx">length</span><span class="p">)]</span>
</span><span class='line'>
</span><span class='line'><span class="cm">########################</span>
</span><span class='line'><span class="c1"># Task 3</span>
</span><span class='line'><span class="nv">delay = </span><span class="nf">(ms, cb) -&gt;</span> <span class="nx">setTimeout</span> <span class="nx">cb</span><span class="p">,</span> <span class="nx">ms</span>
</span><span class='line'>
</span><span class='line'><span class="nx">delay</span> <span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">()</span><span class="o">*</span><span class="mi">100</span><span class="p">,</span> <span class="nx">batch</span><span class="p">.</span><span class="nx">wrap</span> <span class="s">&#39;timer&#39;</span><span class="p">,</span> <span class="o">-&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;Timer done&quot;</span>
</span><span class='line'>  <span class="k">return</span> <span class="kc">true</span>
</span><span class='line'>
</span><span class='line'><span class="cm">########################</span><span class="c1">#</span>
</span><span class='line'><span class="c1"># Completion:</span>
</span><span class='line'><span class="nx">batch</span><span class="p">.</span><span class="kc">on</span> <span class="s">&#39;done&#39;</span><span class="p">,</span> <span class="nf">(results) -&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;\n\n\nThe fetch, read and timer have completed.&quot;</span>
</span><span class='line'>  <span class="k">if</span> <span class="nx">results</span><span class="p">.</span><span class="nx">status</span> <span class="o">!=</span> <span class="kc">null</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;Fetch succeeded, status code: </span><span class="si">#{</span><span class="nx">results</span><span class="p">.</span><span class="nx">status</span><span class="si">}</span><span class="s">&quot;</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">error</span> <span class="s">&quot;Fetch failed!&quot;</span>
</span><span class='line'>  <span class="k">if</span> <span class="nx">results</span><span class="p">.</span><span class="nx">hosts</span> <span class="o">!=</span> <span class="kc">null</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;Random entry from /etc/hosts: &quot;</span><span class="o">+</span><span class="nx">results</span><span class="p">.</span><span class="nx">hosts</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>    <span class="nx">console</span><span class="p">.</span><span class="nx">error</span> <span class="s">&quot;Couldn&#39;t read /etc/hosts!&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="nx">batch</span><span class="p">.</span><span class="kc">on</span> <span class="s">&#39;done&#39;</span><span class="p">,</span> <span class="nf">(results) -&gt;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="s">&quot;\nSecond completion callback, raw results:&quot;</span>
</span><span class='line'>  <span class="nx">console</span><span class="p">.</span><span class="nx">log</span> <span class="nx">results</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
</feed>
