The JavaScript blog.


redis browser localStorage ES6 proxies

ES6 Metaprogramming, Barn

Posted on .

ES6 Metaprogramming

The last time I seriously looked at JavaScript metaprogramming I tried all kinds of dubious things like exploiting the with statement. With ECMAScript 6 proxies, the situation might be about to change. In Meta programming with ECMAScript 6 proxies, Dr. Axel Rauschmayer writes about the concept of metaprogramming, the different approaches (introspection, self-modification, intercession), and provides JavaScript examples.

This article includes an overview of proxies and what they're useful for, and shows how they can be used for intercepting method calls. He also mentions how proxies impact performance:

Data binding is a complex topic. Given its popularity and concerns over proxies not being performant enough, a dedicated mechanism has been created for data binding: Object.observe(). It will probably be part of ECMAScript 7 and is already supported by Chrome.

This is an in-depth article that is worth taking some time over, but will influence many people who are creating the next generation of ECMAScript 6-based APIs.


Barn (GitHub: arokor/barn, License: MIT, npm: barn) by Aron Kornhall is an API for localStorage that is based on Redis. The idea is to make operations atomic, so they survive things like a tab being closed or unexpected exceptions.

If you're used to Redis, then you might like this:

var barn = new Barn(localStorage);

barn.set('key', 'val');  
console.log(barn.get('key')); // val

barn.lpush('list', 'val1');  
barn.lpush('list', 'val2');  
console.log(barn.rpop('list')); // val1  
console.log(barn.rpop('list')); // val2  

Internally a mutating flag is used to provide a locking mechanism around the localStorage access. I think this should prevent issues that you might get when performing multiple writes, because the Web Storage specification says writes don't wait until the data has been written to disk.

Barn itself is based on NSStorage, which is also written by Aron. NSStorage is a smaller module that provides namespacing for the Web Storage API.


redis node modules queue

Node Roundup: 0.10.23, 2013, Bull

Posted on .

Node 0.10.23 Released

Node v0.10.23 is out. This release upgrades uv, npm, and has patches for several core modules. I particularly liked "events: avoid calling once functions twice". This would happen when emitting an event from inside the event hander. The fix involves using a boolean to track if the event has fired, so it's not too drastic.

Node in 2013

If I think back over 2013, all I can remember is being punched in the face a lot by my son. He's a year old, so I'm hoping this sort of thing stops before he gets much bigger. For those of you who still have brain cells, Gergely Nemeth sent in a timeline of Node in 2013. This includes news like Ghost getting funded, Nodeschool.io's release, and the ScaleNPM funding drive.

Bull Job Manager

Manuel Astudillo sent in Bull Job Manager (npm: bull, License: BSD). It's a job queue manager that uses Redis and has an API inspired by LearnBoost's Kue.

Manuel says it's designed with atomicity in mind. Looking at the source, it uses atomic increments and blocking operations (BRPOPLPUSH), so it should be as atomic as Redis is.


HTML redis node modules statistics parsing curl

Node Roundup: parse5, redis-timeseries, request-as-curl

Posted on .


parse5 (GitHub: inikulin / parse5, License: MIT, npm: parse5) by Ivan Nikulin is a new HTML5 parser, based on the WhatWG HTML5 standard. It was built for a commercial project called TestCafé, when the authors found other HTML5 parsers to be too slow or inaccurate.

It's used like this:

var Parser = require('parse5').Parser;  
var parser = new Parser();  
var document = parser.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>')  
var fragment = parser.parseFragment('<title>Parse5 is &#102;&#117;&#99;&#107;ing awesome!</title><h1>42</h1>');  

I had a look at the source, and it doesn't look like it was made with a parser generator. It has a preprocessor, tokenizer, and special UTF-8 handling. There are no dependencies, other than nodeunit for testing. The tests were derived from html5lib, and include over 8000 test cases.

If you wanted to use it, you'll probably need to write a "tree adapter". Ivan has included an example tree adapter, which reminds me of writing SAX parser callbacks.

Ivan also sent in mods, which is a module system designed to need less boilerplate than AMD-style libraries.

Redis Time Series

Tony Sokhon sent in redis-timeseries (GitHub: tonyskn / node-redis-timeseries, License: MIT, npm: redis-timeseries), a project for managing time series data. I've used Redis a few times as a data sink for projects that need realtime statistics, and I always found it worked well for the modest amounts of data my projects generated. This project gives things a bit more structure -- you can create instances of time series and then record hits, then query them later.

A time series has a granularity, so you can store statistics at whatever resolution you require: ts.getHits('your_stats_key', '1second', ts.minutes(3), callback). This module is used by Tony's dashboard project, which can be used to make a realtime dashboard.


request-as-curl (GitHub: azproduction / node-request-as-curl, License: BSD, npm: request-as-curl) by Mikhail Davydov serialises options for http.ClientRequest into an equivalent curl command. It also works for Express.

// http.ClientRequest:
var req = request('http://google.com/', {method: 'POST', json: data}, function (error, response, expected) {  
  curlify(req.req, data);
  // curl 'http://google.com' -H 'accept: application/json' -H 'content-type: application/json' -H 'connection: keep-alive' --data '{"data":"data"}' --compressed

// Express:

app.get('/', function (req) {  
  // curl 'http://localhost/pewpew' -H 'x-real-ip:' -H etc...

I imagine Mikhail has been using this so he can replicate requests based on logs to aid in debugging.


redis databases node modules grunt

Node Roundup: 0.8.21, Node Redis Pubsub, node-version-assets

Posted on .

You can send in your Node projects for review through our contact form.

Node 0.8.21

Node 0.8.21 is out. There are fixes for the http and zlib modules, so it's safe and sensible to update.

Node Redis Pubsub

Louis Chatriot sent in NRP (Node Redis Pubsub) (GitHub: louischatriot / node-redis-pubsub, License: MIT, npm: node-redis-pubsub) which provides a Node-friendly API to Redis' pub/sub functionality. The API looks a lot like EventEmitter, so if you need to communicate between separate processes and you're using Redis then this might be a good solution.

Louis says he's using it at tl;dr in production, and the project comes with tests.


node-version-assets (GitHub: techjacker / node-version-assets, License: MIT, npm: node-version-assets) by Andrew Griffiths is a module for hashing assets and placing the hash in the filename. For example, styles.css would become styles.7d47723e723251c776ce9deb5e23062b.css. This is implemented using Node's file system streams, and the author has provided a Grunt example in case you want to invoke it that way.


libraries redis node modules couchdb streams

Node Roundup: redis-stream, DataGen, Cushion

Posted on .

You can send in your Node projects for review through our contact form or @dailyjs.


redis-stream (License: MIT, npm: redis-stream) by Thomas Blobaum is a stream-based wrapper around the Redis protocol. It's actually an extremely lightweight module, but the author has included tests and some interesting examples. The standard Node stream methods work, so data can be piped:

var Redis = require('redis-stream')  
  , client = new Redis(6379, localhost, 0)
  , rpop = client.stream('rpop');


This doesn't just apply to rpop, other Redis commands will also work in a similar way.


DataGen (GitHub: cliffano / datagen, License: MIT, npm: datagen) by Cliffano Subagio is a multi-process test data file generator. It can be used to generate files in various formats, including CSV and JSON, based on template files that describe the output. Random numbers, dates, and strings can be generated.

The underlying random data generation is based on the Faker library, and Mocha tests are included.


Cushion (GitHub: Zoddy / cushion, License: MIT, npm: cushion) by André Kussmann is a CouchDB API. It has Node-friendly asynchronous wrappers around the usual CouchDB API methods, and it also supports low-level requests by calling cushion.request. Fetching documents returns a document object that can be modified and saved like this:

var doc = db.document('id');  
doc.load(function(err, document) {  
  document.body({ name: 'Quincy' });

Designs and users can also be fetched and manipulated.