Debugging with Mocha

22 Aug 2013 | By Alex Young | Comments | Tags node modules testing techniques

I do a lot of work with Mocha, because I like to write well-tested Node projects. Sometimes there are things I just can’t figure out non-interactively, and that’s when I reach for the debugger. It’s a lot better than repeatedly running tests with more and more console.log statements.

Rather than using any extra Mocha extensions, I find Node’s standard debugger works great. With Mocha you access it by running your tests in debug mode:

mocha debug

The debug option works just like node debug file.js. Node’s debugger comes from V8, which has a built-in JSON-based protocol that supports communication with a live process using TCP. That means you’re not limited to Node’s command-line tools: the fancy GUI debugger in Chrome can be used instead.

Debugging a Failing Test

To debug a failing test, look at the stack trace to figure out a good place to add a breakpoint. I usually put them in test cases, just before a failing assertion, but you can put them anywhere in the program.

Breakpoints are added by inserting debugger on a single line by itself. Once you’ve strategically placed some breakpoints, run the tests with mocha debug.

Whenever you run mocha debug or node debug, you’ll be dropped into the debugger on the first line of the program. Type c to allow the tests to continue until the first debugger statement is encountered – there may be a delay after you issue the continue command, so be patient. After that the program will run until a breakpoint is hit, at which point you’ll be dropped into a prompt.

Debugger Commands

The commands are like gdb or lldb for the most part – if you’ve used another debugger before you should be able to figure out what’s going on. If not, there are three main commands that will get you started:

  • s: Step into code
  • n: Run the next line without stepping into it if it’s a function/method call
  • repl: Enter a REPL, which we mainly use for printing values and objects

As I have some experience with other debuggers, I found it took me a while to get used to typing repl: in most debuggers there’s a specific command for printing and formatting values or locations in memory. This REPL mode is exited by typing CTRL-C once. While you’re in the REPL, you can type the name of a value or object to see it: this is usually how I fix most of the problems in my tests.

Other Thoughts

I haven’t found a way to automatically drop into a debugger when an assertion fails. I’d really like to do this – it would be a bit like how IDEs work, but I’m not sure if it’s possible.

Node Roundup: 0.10.16, ungit, image-size

21 Aug 2013 | By Alex Young | Comments | Tags node modules git apps images
You can send in your Node projects for review through our contact form.

Node 0.10.16

Node 0.10.16 has been released, which includes an update for npm, and fixes for the crypto, http, and stream modules.

ungit

ungit logo

ungit (GitHub: FredrikNoren / ungit, License: MIT, npm: ungit) is a web-based UI for Git, written with Node. It makes Git repositories easier to visualise, a bit like gitk or git instaweb, but it has some GitHub-specific tweaks.

ungit repo

It can be installed with npm install -g ungit, and is run with ungit on the command-line. You can set up an .ungitrc which is a JSON file that currently just changes the port.

Once you’re running ungit, you can make commits, discard them, fetch remote changes – pretty much the standard Git operations you’re used to, with a friendlier workflow.

image-size

If you need to get image sizes without using command-line binaries, then take a look at image-size (GitHub: netroy / image-size, License: MIT, npm: image-size) by Aditya. It looks at the relevant bits in a file by using a Node buffer, and supports popular formats like PNG, GIF, BMP, and even PSD.

It has an asynchronous and synchronous API:

var sizeOf = require('image-size');
sizeOf('images/example.png', function(err, dimensions) {
  console.log(dimensions.width, dimensions.height);
});

jQuery Roundup: Fine Uploader, SimpleSlideView, jQuery.uheprnGen

20 Aug 2013 | By Alex Young | Comments | Tags jquery plugins ui widgets mobile html5 cryto
Note: You can send your plugins and articles in for review through our contact form.

Fine Uploader

Fine Uploader

Fine Uploader (GitHub: Widen / fine-uploader, License: GPLv3) by Mark Feltner is a client-side library for managing file uploads. It’s free of dependencies, but there’s a jQuery plugin for convenience. It supports multiple file uploads, progress bar, drag and drop, upload cancelation, validation, and direct to Amazon S3 uploads. S3 uploads work using CORS, and XHR2 is used for browsers with File API support and monitoring upload progress.

The author maintains a blog about the project, and a recent post includes details on the S3 support.

SimpleSlideView

SimpleSlideView (GitHub: cloudfour / SimpleSlideView, License: MIT) by Tyler Sticka is a mobile-style UI widget for displaying and managing animated view hierarchies:

This plugin was designed to work well with non-fixed layouts, which means it can be helpful to scroll to the top of the window or container prior to a view changing. If a $.scrollTo plugin is available, SimpleSlideView will attempt to use it by default. It has been tested with jquery.scrollTo and ZeptoScroll.

The views are divs marked up with a container so they can be displayed and hidden as needed. Data attributes are used to associate navigation with markup: a link with data-popview or data-pushview allows movement through the view hierarchy. There’s also a JavaScript API for managing this.

The readme has full documentation and an example for using it in a responsive interface.

jQuery.uheprnGen

jQuery.uheprnGen (GitHub: ryanmcdonough / jQuery.uheprnGen, License: Public Domain) is Ryan McDonough’s first attempt at a jQuery plugin, but it’s cool because it wraps around Steve Gibson’s UHE PRNG script, which stands for Ultra-High Entropy Pseudo-Random Number Generator.

This carefully designed PRNG utilizes more than 1536 bits of internal state memory. The operating parameters of the generator’s algorithm were carefully chosen (it uses a safe prime factor) to guarantee that every possible PRNG state is visited before the sequence begins to repeat.

With Ryan’s script, you can call $(this).uheprngGen({ range: 100, count: 10001 }); to generate 10001 random numbers with a range of 0 to 100.

Speech Router, Botdylan, Google Cloud Storage

19 Aug 2013 | By Alex Young | Comments | Tags api storage speech chrome github

Speech Router

Speech Router (GitHub: lukasolson / speech-router, License: MIT) is a router that wraps around Chrome’s Web Speech API. It’s based around a constructor function that accepts a routes object:

new SpeechRouter({
  routes: {
    'search google for *query': function(query) {
      window.open('https://google.com/search?q=' + query);
    }
  }
});

If you haven’t seen the Web Speech API before, it’s worth checking out. At least the results seem quite good with my accent and Chrome – your mileage may vary!

Botdylan

Botdylan (GitHub: botdylan / botdylan, License: MIT) by Pau Ramon is a Hubot-inspired GitHub automation system:

We use GitHub heavily. There are some repetitive tasks that were taking away our time to hack on a better product. Since we are in the productivity space we decided to stop doing things manually and start to have a more productive environment to work with.

It can do things like add labels to issues based on events, integrate with a CI server, talk to chat rooms, and so on. It runs as a daemon, and can execute tasks periodically in the background.

Google Cloud Storage

Google Cloud Storage, sent in by Jonathan Simon, is a screencast that demonstrates a JavaScript application that talks to Google’s Cloud Storage platform. The source code is here: storage-getting-started-javascript.

This demonstration covers getting started with Google Cloud Storage using JavaScript. See the process of setting up the sample application, running the sample and using it to try out Cloud Storage API commands like ‘list buckets’.

NoFlo, Peppy

16 Aug 2013 | By Alex Young | Comments | Tags node

NoFlo

NoFlo

NoFlo (GitHub: noflo) is a project to bring a visual-based approach to programming. It currently focuses on Node, but the authors want to branch out to other platforms if you give them enough money.

The project and the site seem interesting enough, but the Kickstarter video comes off a little bit too enthusiastic. Here is a spoonfull of delicious hyperbole for you to digest: “Nothing new has really been invented in programming in the last 50 years”, “code starts as whiteboards”, “flow-based programming can eliminate spaghetti code”.

Whether or not something is truly new is irrelevant: monads have been around less than 50 years, but even so we’re still exploring their uses after a good 20 something years (random citation: Extensibe Effects: An Alternative to Monad Transformers). Just because strongly typed object oriented programming is the dominant paradigm doesn’t stop thousands of papers a year being published about programming and related fields. Even if you spend all day writing JavaScript, Java, C#, or whatever, the field is still forging ahead: these languages themselves are evolving.

The assumption that we use whiteboards perplexes me. I don’t think I’ve used a whiteboard once in my career, and I’ve been programming commercially for 12 years now. How can making tangled masses of diagrams be a solution to spaghetti code? Won’t we just end up with spaghetti diagrams?

NoFlo has been written in CoffeeScript for simplicity.

Oh, OK… Before I give up and post that Grandpa Simpson hat gif, I’ll say this: I’m not sold on the idea, but that doesn’t mean I won’t try it. Let’s set aside the hilariously hyperbolic marketing video and see what it can do.

Peppy

Peppy (GitHub: jdonaghue / Peppy, License: FreeBSD) by James Donaghue is a CSS selector engine. He originally wrote it over four years ago, and it was well known at the time for being fast. He’s recently rewritten it with a different focus:

Peppy no longer uses large regular expressions for parsing selector strings. It now uses a selector parser that I wrote called LLSelectorParser. This is a top down parser that returns an abstract syntax tree. Peppy internally now works off of this tree.

Back when I regularly wrote the Let’s Make a Framework posts I was fascinated by how CSS frameworks work. Things have changed since then, but parsing selectors is still an interesting topic. Check out LLSelectorParser if you want to see how James implemented his.

A Look at Facebook's React

15 Aug 2013 | By Alex Young | Comments | Tags frameworks facebook ui reactive

React

React (GitHub: facebook / react, License: Apache 2.0, npm: react-tools, bower: react) from Facebook and Instagram is a UI framework. It’s maintained by Paul O’Shannessy, and Paul and Christopher Chedeau both post to the React blog regularly about new features and community news.

React is meant to be a declarative framework, so it focuses on transforming data into interface components. Data can come from servers and user input – there’s no specific transport layer support, instead other libraries like jQuery are relied on.

The first thing you’ll notice about React is it mixes HTML and JavaScript:

var HelloMessage = React.createClass({
  render: function() {
    return <div>{'Hello ' + this.props.name}</div>;
  }
});

React.renderComponent(<HelloMessage name="John" />, mountNode);

What’s going on? Well, this snippet is intended to be placed inside a script tag, with the type attribute set to text/jsx. JSX is React’s XML-based template language. It doesn’t hit the DOM until it needs to: instead a mock DOM, or faked browser is used to maintain an in-memory representation of your templates. Be aware that this comes with caveats: everything is escaped by default, so HTML entities can easily be double escaped.

You don’t need to use JSX, however: React.DOM.div({}, 'Hello ' + this.props.name) is equivalent to the previous example.

Applications made with React are based on components. Components are created with React.createClass method (not React.createComponent). Components have a “state” and “props”: the state is private, and mutable. If the state is updated, then the component will be re-rendered: this is known as reactive state, and is how React models reactive data flows. React doesn’t re-render the entire component, it tries to only render what changed:

render() methods return a description of the DOM, and React can diff this description with the in-memory representation to compute the fastest way to update the browser.

React plays well with Node-based build systems: there’s a Grunt task for building JSX, in case you don’t want to render everything in the browser. The documentation has a section on React’s tooling integration.

The documentation for React is straightforward and easy to follow: there’s a tutorial that introduces the main aspects of the framework. It’s difficult to say how it compares to other frameworks like AngularJS, but I like the distinction between mutable and immutable state and properties for modeling reactive data flows.

Node Roundup: The Future of Node, takeapeek, subtitle

14 Aug 2013 | By Alex Young | Comments | Tags node modules subtitles video
You can send in your Node projects for review through our contact form.

The Future of Node

Yakulu posted The Future of Programming in Node.js to Hacker News, which is a long message by Isaac Schlueter on the nodejs Google Group:

Callbacks will remain the de facto way to implement asynchrony. Generators and Promises are interesting and will remain a userland option.

This is not a democracy. However, there’s plenty of room for everyone’s opinion. If you want to make exciting dramatic breaking changes to node-core, and you can’t find satisfaction by writing userland npm modules, then please fork joyent/node, give it a new name and logo, and go as completely crazy as you want.

It sounds like the Node core developers are focusing on keeping Node maintainable, rather than jumping on whatever new language or API style is currently fashionable. I like this approach, because after working with faddish web frameworks that resulted in unmaintainable projects after a surprisingly short amount of time, the idea of a stable and calm core library is definitely welcome.

There were cries of a lack of innovation in the comments, setting off an interesting discussion about the future of Node. Eventually the comparison with Meteor was brought up, and Isaac talks about how it differs to Node, npm, and even how the Meteor community is managed.

It’s required reading for those of us investing in Node for the next few years.

takeapeek

takeapeek (GitHub: giodamelio / takeapeek, License: MIT, npm: takeapeek) by Gio d’Amelio is a command-line web server in the spirit of python -m SimpleHTTPServer. It supports directory indexes, and allows settings to be saved in .takeapeekrc. You can also use it as a module in Node.

It’s built with connect, and was inspired by glance by Jesse Keane.

subtitler

subtitler (GitHub: aetheon / node-opensubtitles-client, License: BSD, npm: opensubtitles-client) by Oscar Brito is an open subtitles API client.

It has a command-line tool for downloading subtitle files, and also has an event-based, asynchronous JavaScript API.

jQuery Roundup: jQuery.emphasis.js, Tipue Search

Note: You can send your plugins and articles in for review through our contact form.

jQuery.emphasis.js

jQuery.emphasis.js (GitHub: zmmbreeze / jquery.emphasis, License: MIT, jQuery: emphasis) by “mzhou” is a CSS3 text-emphasis fallback.

The author says it’s useful for East Asian languages because it adds symbols above or below characters to improve the clarity of emphasised sections. The equivalent CSS currently only works in WebKit with a vendor prefix, so this plugin will be useful to those working with these languages.

The source is quite interesting – it’s based on the CSS3 specification, which involves determining if a character is suitable for emphasis, then the insertion of fairly complex styles to simulate the emphasis marks.

Tipue image search

Tipue Search (License: MIT) is a site search plugin. It can search static JSON content, or pages on the same origin using Ajax. It also supports an image search mode, which looks for matches in JSON content that points to image URLs.

Given an index, like this:

var tipuesearch = {"pages": [
  {"title": "Tipue Search, a site search engine jQuery plugin", "text": "Tipue Search is a site search engine jQuery plugin. Tipue Search is open source and released under the MIT License, which means it's free for both commercial and non-commercial use. Tipue Search is responsive and works on all reasonably modern browsers.", "tags": "JavaScript", "loc": "http://www.tipue.com/search"},
  {"title": "Tipue drop, a search suggestion box jQuery plugin", "text": "Tipue drop is a search suggestion box jQuery plugin. Tipue drop is open source and released under the MIT License, which means it's free for both commercial and non-commercial use. Tipue drop is responsive and works on all reasonably modern browsers.", "tags": "JavaScript", "loc": "http://www.tipue.com/drop"},
  {"title": "About Tipue", "text": "Tipue is a small web development studio based in North London. We've been around for over a decade. We like minimalism with the occasional hint of retro.", "tags": "", "loc": "http://www.tipue.com/about"}
]};

Tipue can be enabled using $('#tipue_search_input').tipuesearch(); on a suitable input element.

Auto Iframe Height Plugin Update

Ilker Guller updated his Auto Iframe Height Plugin (GitHub: Sly777 / Iframe-Height-Jquery-Plugin, License: MIT, GPL) to automatically check the iframe height when the contents change.

To enable automatic resizing, you can use the watcher option passed to $.iframeHeight. It also supports several other options, including: defaultHeight, minimumHeight, and watcherTime.

Ground, Generators and Async Patterns in Node, Time Grunt

12 Aug 2013 | By Alex Young | Comments | Tags node modules grunt typescript frameworks async es6

Ground

Ground (GitHub: OptimalBits / ground, License: MIT) from OptimalBits is a web framework for Node that is written with TypeScript:

In ground, most of the application logic is moved from the server to the client, whereas the server acts mostly as an scalable, efficient distributed storage and synchronization controller.

It includes also some rather useful features such as a hierarchical routing system, an undo/redo manager, property and declarative bindings, reference counting and automatic synchronization between clients and servers. It is design to always deliver high performance and low memory consumption.

Grunt is used for building the client-side code and deploying it. It uses models in the style of the Mongoose ODM, and most parts of the framework including models have event APIs. Views are implemented using view models.

The documentation at gnd.io includes more examples and demos.

Generators and Async Patterns in Node

Gorgi Kosev sent in his essay, Analysis of generators and other async patterns in node. He looks at how generators work, how the code compares to other implementations (for example, promises), using a complexity measurement, and benchmarks of memory and execution time.

The generator solution suspend is really fast. It incurred minimal overhead of about 50-80% running time. It’s also roughly comparable with streamlinejs (when in raw callbacks mode).

Time Grunt

time-grunt (GitHub: sindresorhus / time-grunt, License: MIT, npm: time-grunt) by Sindre Sorhus displays the elapsed time of Grunt tasks:

// Gruntfile.js
module.exports = function(grunt) {
  // require it at the top and pass in the grunt instance
  require('time-grunt')(grunt);

  grunt.initConfig();
  grunt.registerTask('default', []);
}

git-html5.js, TodoMVC 1.2

09 Aug 2013 | By Alex Young | Comments | Tags node modules polymer git apps chrome

git-html5.js

Tailor

git-html5.js (GitHub: ryanackley / git-html5.js, License: MIT) by Ryan Ackley is a Git implementation for JavaScript:

git-html5.js is a pure JavaScript git client library. It implements a complete Git workflow in nothing but JavaScript. It’s meant to run in a browser environment and depends on so-called “html5” APIs. Some example use cases:

Ryan took Adobe’s Brackets app, ported to run as a Chrome packaged app, and then added git-html5.js to create an editor that can read and write to Git repositories. I’ve tried out the basics and it seems to work pretty well.

The project includes tests and build instructions, but you can just run the Tailor app in Chrome if you want to try it out.

TodoMVC 1.2

Sindre Sorhus sent in TodoMVC 1.2, an update to the monster MV asterisk collection:

We released TodoMVC 1.2 yesterday with some new app examples of frameworks from Google (Polymer), Twitter (Flight) and Facebook (React), and other improvements.

If you’re interested in seeing what a small but functional Polymer project looks like, then check it out!

Reactive JavaScript

08 Aug 2013 | By Alex Young | Comments | Tags node modules browser coffee-script reactive

As JavaScript developers, whether server-side, browser-based, or both, two things we love are composition and asynchronous APIs. Once you’ve got the hang of callbacks, promises aren’t too difficult to master, which is when libraries like q and async come into their own.

Reactive programming offers a different approach. It gives us tools that attack composition and asynchronicity head-on by focusing on data flows. This is a declarative paradigm – logic expressed through data flows. We’re entirely familiar with this: you can’t go far with Node or jQuery without thinking in terms of events. The difference, however, is a layer of formality that provides a richer language for expressing logic through change.

If you’ve ever worked with .NET, then you might have seen Reactive Extensions. There’s also RxJS (GitHub: Reactive-Extensions / RxJS, CodePlex: Rx, License: Apache 2.0, npm: rxjs), a Microsoft Open Technologies project designed to help us compose asynchronous and event-based programs using observable collections, drawing on inspiration from LINQ:

One question you may ask yourself, is why RxJS? What about Promises? Promises are good for solving asynchronous operations such as querying a service with an XMLHttpRequest, where the expected behavior is one value and then completion. The Reactive Extensions for JavaScript unifies both the world of Promises, callbacks as well as evented data such as DOM Input, Web Workers, Web Sockets. Once we have unified these concepts, this enables rich composition.

The first example in the RxJS readme demonstrates how composition can be used:

/* Only get the value from each key up */
var keyups = Rx.Observable.fromEvent(input, 'keyup')
    .select(function(e) {
        return e.target.value;
    })
    .where(function(text) {
        return text.length > 2;
    });

/* Throttle/debounce the input for 500ms */
var throttled = keyups.throttle(500 /* ms */);

/* Get only distinct values, so we eliminate the arrows */
var distinct = keyups.distinctUntilChanged();

function searchWikipedia(term) {
    var url = 'http://en.wikipedia.org/w/api.php?action=opensearch'
        + '&format=json' 
        + '&search=' + encodeURI(term);
    return Rx.Observable.getJSONPRequest(url);
}

Recently, Yang Zhang sent me Introducing reactive.coffee: reactive programming and declarative UIs in CoffeeScript, a blog post about reactive.coffee (GitHub: yang / reactive-coffee, License: MIT, bower: reactive-coffee). This is a library of reactive programming primitives – it’s not AngularJS or Knockout, but something different and more generic. It provides reactive data structures that can be used for many things, including declarative DOM construction.

It’s also designed to work with jQuery, so normal jQuery objects can be manipulated using reactive programming.

There are dozens of reactive programming modules on npm, so check those out if you’re interested in learning more. Also, if like me you’re a Unix-focused web developer, don’t underestimate Microsoft’s resources on the topic because they’ve been doing this for a long time now.

Node Roundup: 0.11.5, nvi, signobj

07 Aug 2013 | By Alex Young | Comments | Tags node modules vim security crypto
You can send in your Node projects for review through our contact form.

Node 0.11.5

Node 0.11.5 was released this week. This unstable version updates v8 and uv. It also includes fixes for buffer, child_process, dgram, fs, https, openssl, os, tls, and util.

The patch for fs by Trevor Norris is interesting (pull request) – rather than writing strings to a buffer and then the disk, it changes fs.js and src/node_file.cc to write directly to the disk instead.

nvi

nvi (GitHub: mikesmullin / nvi, License: GPLv3, npm: nvi) by Mike Smullin is a ‘very opinionated Vi clone’. I tried installing it with npm install -g nvi, but it wouldn’t run; I had to check out the repository manually. It doesn’t clone Vi or Vim in a way that I think it’s fair to call ‘clone’ – I can’t seem to get hjkl to move the cursor, and the modes have been changed to include ‘COMBO’ mode instead of Normal mode which makes using it extremely confusing for a seasoned Vim veteran.

Despite all that, and the fact that the name nvi is a bad choice, I find the project interesting because making complex text user interfaces isn’t an easy task. Also, Mike’s nvi is focused on collaborative features, which potentially makes Node a great fit.

signobj

Django has a cryptographic API for setting and reading signed cookies, and presumably you can also use this to sign API responses for RESTful JSON APIs. Inspired by this, signobj (GitHub: Submersible / node-signobj, License: MIT, npm: signobj by Ryan Munro allows you to sign JSON data with a SHA-1 HMAC:

signobj() - Signs data with secret, you can also pass in some extra hidden data that is used when hashing. This can be useful if you’re creating an access token, and you want it to become invalid when they change their password, and also don’t want the password with the public data.

Ryan said he’s been using it to sign cookies and localStorage sessions.

jQuery Roundup: spy-js, yadcf

06 Aug 2013 | By Alex Young | Comments | Tags jquery plugins sponsored-content services tables ui
Note: You can send your plugins and articles in for review through our contact form.

spy-js

spy-js.

spy-js, created by Artem Govorov, is a commercial tool that aims to make JavaScript instrumentation better. You can use it to trace, debug, and profile code that would otherwise be difficult to work with.

Although tools like Chrome DevTools are excellent, the advantage of spy-js is it can work with any browser. It also allows performance between browsers to be compared more easily.

spy-js in action.

There’s a free beta which you can download from spy-js.com, and issues are being tracked on GitHub. The documentation is also on GitHub, at spy-js/spy-js. The project isn’t open source, but the author intends to keep it free while he collects beta feedback.

For more information, take a look at spy-js.com and @SpyDashJs on Twitter.

yadcf

Yet Another DataTables Column Filter (GitHub: vedmack / yadcf, License: GPL2/BSD, jQuery: yadcf) by Daniel Reznick is a plugin for DataTables that adds filtering components. It can filter based on a select, or the jQuery UI Autocomplete widget. It also parses different data types, like delimited plain text and HTML elements.

$(document).ready(function(){
  $('#example').dataTable().yadcf([
    { column_number: 0 },
    { column_number: 1, filter_container_id: 'external_filter_container' },
    { column_number: 2, data:['Yes', 'No'], filter_default_label: 'Select Yes/No' },
    { column_number: 3, text_data_delimiter: ',', enable_auto_complete: true },
    { column_number: 4, column_data_type: 'html', html_data_type: 'text', filter_default_label: 'Select tag' }]);
});

It also works perfectly well with multiple tables on a page. For more examples, check out the yadcf showcase and the project’s readme.

Signature Pad, Windows 8.1 Apps with Open Source JavaScript

05 Aug 2013 | By Alex Young | Comments | Tags ui windows mobile canvas

Signature Pad

Signature Pad (GitHub: szimek / signature_pad, License: MIT) by Szymon Nowak is a client-side library for drawing smooth signatures. It works with touchscreens and desktop browsers, and was inspired by the Smoother Signatures post on Square’s (excellent) engineering blog.

The problem is that the signer’s finger did not move in straight lines from point to point when tracing out this shape. Rather, our touch points are sampled from a full curve that the signer’s finger traced on the touchscreen. While we can’t know the original shape between the sampled points Android gave us, straight lines are not the best guess.

Szymon’s implementation doesn’t depend on any external libraries, and can draw signatures from DATA URIs.

var canvas = document.querySelector('canvas');
var signaturePad = new SignaturePad(canvas); 
signaturePad.clear();     // Clears the canvas
signaturePad.toDataURL(); // Returns signature image as data URL
signaturePad.fromDataURL('data:image/png;base64,iVBORw0K...') // Draws signature image from data URL

Building a Windows 8.1 App using Typescript and Open Source Libraries

Ingredimeals!

This screencast by Ala Shiban demonstrates how to use some of Microsoft’s technologies with open source projects like AngularJS to create a Windows Store application.

I put together a 17 minute video that gets you started with writing a native Win8.1 app using HTML, CSS, JavaScript, TypeScript, AngularJS, Bootstrap, underscore, BankersBox and jQuery. The goal of the tutorial is to go through the end-to-end experience of developing a win8.1 app as quickly as possible while not developing a random ‘hello world’ app.

It’s a fast screencast that packs a lot in, so if you can’t keep up check out the source at AlaShiban / Ingredimeals. It uses TypeScript, Blend for Visual Studio, NuGet, jQuery, AngularJS, Underscore, and Bootstrap. Custom fonts are used to give the application a Windows 8 feel.

I haven’t used Windows 8 or Visual Studio much before, so I thought it was interesting seeing how it integrated with third-party open source JavaScript libraries through NuGet, and how debugging JavaScript worked.

Something I hadn’t seen before was MSApp.execUnsafeLocalFunction. I noticed Ala had to wrap some calls inside AngularJS (mainly things that wrote to innerHTML) to satisfy Microsoft’s security requirements for Windows Store apps.

Bootstrap 3, Node Web Development, Groc

02 Aug 2013 | By Alex Young | Comments | Tags node books bootstrap documentation

Bootstrap 3

Bootstrap 3's homepage.

Bootstrap 3 RC1 is out, and you’ll see Bootstrap 3 on the front page of getbootstrap.com:

With over ~1,600 commits, ~72,000 additions/deletions, and ~300 files changed, everything has changed. We’ve added features, removed features, and cleaned up a lot more. The v3 pull request on GitHub has all the details you’ll need for a complete list of changes and some helpful migration tips.

Before you get too excited you should know that it’ll take some work to migrate to Bootstrap 3. There’s no magic migration button as far as I know. Grids still scale up to 12 columns, but now use the class prefix .col-. Forms have different classes and tweaked markup, but the newer markup seems better to me. I don’t think we need <div class="controls"> anymore, for example.

Node Web Development - Second Edition

Node Web Development - Second Edition has been released, written by David Herron.

He’s written a post about it on his blog:

The final version of the sample application includes user authentication support, the ability to work with multiple database engines, be deployed on cloud services, and it dynamically sends data back and forth between server and browser to dynamically update the screen when other people edit things, and to support sending little messages between users of the application.

Groc

In Rock Your Doc With Groc, Our Favorite Automated Frontend Documentation Tool, a fork of Groc is discussed which adds support for tags:

We just couldn’t find a tool that gave us everything we needed. In the end, we created our own automated documentation tool by forking Docco and adding (hacking) support for Tags.

Groc, by Ian MacLeod, is a fork of docco by Jeremy Ashkenas. The post on Gilt’s blog does a good job of explaining why they need tags and how they use them.

NeDB: SQLite for Node

01 Aug 2013 | By Alex Young | Comments | Tags node modules databases code-review

NeDB (GitHub: louischatriot / nedb, License: MIT, npm: nedb) by Louis Chatriot is datastore for Node that implements a subset of MongoDB’s API. It has very modest dependencies (async, underscore, binary-search-tree, mkdirp), and can even be used in browsers:

As of v0.8.0, you can use NeDB in the browser! You can find it and its minified version in the repository, in the browser-version/out directory. You only need to require nedb.js or nedb.min.js in your HTML file and the global object NeDB can be used right away, with the same API as the server version.

The author has been enthusiastically benchmarking the project and is rather confident about its performance. And it really does look like MongoDB:

db.find({ satellites: { $lt: 'Amos' } }, function(err, docs) {
  // docs is empty since Phobos and Deimos are after Amos in lexicographical order
});

db.update({ system: 'solar' }, { $set: { system: 'solar system' } }, { multi: true }, function(err, numReplaced) {
  // numReplaced = 3
  // Field 'system' on Mars, Earth, Jupiter now has value 'solar system'
});

// Using a unique constraint with the index
db.ensureIndex({ fieldName: 'somefield', unique: true }, function(err) {
});

The persistence layer isn’t required: databases can be in-memory if desired. To understand how it all works, first take a look at how each database operation is implemented. For example, Datastore.prototype._insert on lines 265 to 268 in datastore.js calls persistence.persistNewState. The persistNewState method is called for anything that changes data (insert, update, remove). The persistNewState method itself returns early if inMemoryOnly has been set, otherwise it appends UTF8-encoded serialized models to the file that backs the database.

Models are serialized using serialize in model.js. This uses JSON.stringify with a callback that maps undefined values to null, and checks if keys are valid using similar rules to MongoDB (you can’t have keys that start with a dollar or contain a full-stop).

When data is retrieved from disk, async.waterfall is used, from Caolan McMahon’s popular module:

Runs an array of functions in series, each passing their results to the next in the array. However, if any of the functions pass an error to the callback, the next function is not executed and the main callback is immediately called with the error.

Elsewhere the async module is used to queue commands, which ensures they’re executed in the desired order. All commands are passed through an instance of the Executor class, including the loading of data from disk when the persistence layer is initialised.

By combining async and the native JSON encoder and parser, Louis has made a convincing yet lightweight MongoDB implementation. It ties together a lot of the skills required to work as a Node developer – asynchronous I/O, and event-based programming. NeDB currently has 886 stars on GitHub, so it’s clearly a popular project: I suggest taking a look at the source if you’re interested in how people use modules like async.

Node Roundup: 0.10.15, IntervalStream, StreamToMongo, fileswap-stream

31 Jul 2013 | By Alex Young | Comments | Tags node modules fs streams
You can send in your Node projects for review through our contact form.

0.10.15

Node 0.10.15 was released last week, which quickly followed 0.10.14. The newer release adds a fix for process.getuid, to address an issue on Mac OS X:

This commit should unbreak npm on OS X - it’s hitting the new ‘uid must be an unsigned int’ check when installing as e.g. user ‘nobody’ (which has an UID of -2 in /etc/passwd or 4294967294 when cast to an uid_t.)

Version 0.10.14 fixed bugs in os, url, and upgraded npm and uv.

IntervalStream

node-interval-stream (GitHub: czzarr / node-interval-stream, License: MIT, npm: interval-stream) by Stanislas Marion is a simple module that provides a Transform stream that triggers events based on an interval. The author’s example combines IntervalStream with request to display the results of a large download every 2 seconds:

var request = require('request');
var IntervalStream  = require('interval-stream');
var is = new IntervalStream(2000); // emit every 2 seconds

request('http://example.com/large_data_set.json')
  .pipe(is)
  .pipe(process.stdout);

StreamToMongo

StreamToMongo (GitHub: czzarr / node-stream-to-mongo, License: MIT, npm: stream-to-mongo), also by Stanislas Marion, allows data to be streamed to MongoDB. This could be used to stream JSON data directly into a database. The example in the readme uses the npm registry, effectively allowing you to create a structured local cache in Mongo of all the module metadata on npm.

fileswap-stream

Finally, fileswap-stream (GitHub: bpostlethwaite / fileswap-stream, License: MIT, npm: fileswap-stream) by Ben Postlethwaite allows underlying file resources to be swapped. This might be useful if you’re streaming data to log files, and want to split the files:

Write to a writable file-stream that swaps out its underlying file resources according to swapper and naming functions. This can be used for a persistent log or data stream - just stream to it 24/7 and let it swap out to new files whenever you trigger it to.

jQuery Roundup: Orangevolt Ampere, Succinct, jQuery Age

30 Jul 2013 | By Alex Young | Comments | Tags jquery plugins state-machine text time date
Note: You can send your plugins and articles in for review through our contact form.

Orangevolt Ampere

Orangevolt Ampere

If you like state machines, then you might find Orangevolt Ampere (GitHub: lgersman / jquery.orangevolt-ampere, License: MIT/GPL2) interesting. Created by Lars Gersmann, it uses AngularJS, jQuery, Boostrap, and Font Awesome and attempts to help you model single page applications by encapsulating data operations and route transitions in state machines. That means your applications get some features for “free”, like infinite undo/redo.

The author has made a presentation about Orangevolt Ampere that gently introduces the main concepts, which I recommend looking at before diving into the code.

The wizard example is a good example of how state machines can model something that would ordinarily be awkward to work with.

Succinct

Succinct (GitHub: micjamking / succinct, License: MIT) by Mike King is a text truncation plugin. It truncates based on the simplest case where you know the number of characters you want to display: $('.truncate').succinct({ size: 80 }) will cut text down to 80 characters and add an ellipsis.

jQuery Age

jQuery Age (GitHub: ksylvest / jquery-age) by Kevin Sylvestre formats and tracks dates and times as human readable text. It allows the text suffixes and grammatical formatting to be overridden, so it could be internationalised as needed.

<time datetime="2010-01-01T12:00:00Z" class="age">January 1, 2010 12:00</time>
<time datetime="2020-01-01T12:00:00Z" class="age">January 1, 2020 12:00</time>

<script type="text/javascript">
  $('.age').age();
</script>

Disc, scaleApp, Cal-HeatMap

29 Jul 2013 | By Alex Young | Comments | Tags browserify ui graphs frameworks libraries modules node

Disc

Disc

Disc (GitHub: hughsk / disc, License: MIT, npm: disc) by Hugh Kennedy is a tool for generating interactive views of browserify project bundles:

It’s especially handy for catching large and/or duplicate modules which might be either bloating up your bundle or slowing down the build process. It works with node projects too!

Running disc index.js > stats.html will generate a file that uses D3.js to display two visualisations: file count and file size. It’s a little bit like the file system usage applications for desktop systems.

scaleApp

Markus Kohlhase has been quietly working on a project called scaleApp (GitHub: flosse / scaleApp, License: MIT, npm: scaleapp) – a framework for creating single page applications. It’s designed to help you write decoupled, event-driven apps, and is influenced by Nicholas C. Zakas’s talk Scalable JavaScript Application Architecture.

The project has no dependencies and supports Node and browsers. AMD and CommonJS modules are both supported, and it can be extended with plugins. There are already plugins for internationalisation, DOM manipulation, MVC, and more.

Applications are based around modules, which are registered and “started”. Multiple instances of a module can be started, and they talk to each other using a pub/sub API. Flow control for asynchronous methods is also possible, with the runSeries and runWaterfalladdressed methods.

Projects are deployed to browsers by using “browser bundles” – Grunt is used as the build system. There is a scaleApp demo application (source), so you can see what a real application built with the framework looks like.

Cal-HeatMap

Cal-HeatMap

Cal-HeatMap (GitHub: kamisama / cal-heatmap, License: MIT, Bower: cal-heatmap) is a GitHub-style calendar heatmap created by Wan Qi Chen. It uses D3.js, and comes with a build script and tests.

var cal = new CalHeatMap();
cal.init({
  start: new Date(2000, 0),
  range: 12,
  domain: 'year',
  subDomain: 'month',
  data: 'http://example.com/api.json'
});

Data can be loaded remotely, and CSV, JSON, TSV, and plain text formats are supported. The API allows values to be highlighted, and the legend, cell size, and position are all configurable.

Ractive.js, Japanese DailyJS Translations

Ractive

Ryan McDonough sent in Ractive.js (GitHub: Rich-Harris/Ractive, License: MIT, Bower: ractive), a new alternative to libraries like AngularJS, created by developers at The Guardian. Are we still calling these libraries MVC, MV*, or something else?

Ractive.js is different. It solves some of the biggest headaches in web development - data binding, efficient DOM updates, event handling - and does so with almost no learning curve.

There’s a nice and short example in the documentation. Given the following HTML fragment:

<p>Hello ! You have
    <strong> new messages</strong>.
</p>

Then the Ractive constructor can be used to populate data:

var ractive = new Ractive({
  el: result,
  template: html,
  data: {
    user: 'Dave',
    messages: { total: 11, unread: 4 }
  }
});

ractive.set('messages.unread', 5);

However…:

Ractive.js constructs a parallel DOM representation which is aware of its dependencies on the values of user and messages.unread. When those values change, it knows exactly which parts of the real DOM need to be updated.

This could be the data-binding library with an idiomatic JavaScript API that we’ve been looking for. By focusing on the “data model” problem the project seems immediately easier to understand than some larger libraries, and the early adoption of Bower and Grunt means it should be straightforward to slot into your projects.

Japanese DailyJS Translations

Hideharu Sakai sent in his translation of my recent “Getting started with Nodebots” article: Nodebot を始めよう(著者:アレックス・ヤング). He said there’s a lot of interest in Node and DailyJS from the Japanese developer community, so thanks for that!