Olivier Lalonde's blog

Hey. I'm a friendly hacker, HN addict and startup guy. I tweet as @o_lalonde.

Migrated Blog to Jekyll

I just finished migrating my blog from Posterous to Jekyll. Posterous was acquired by Twitter a few months ago and is now in maintenance mode. I’m now hosting this blog for free on Github and use Octopress/Jekyll to generate the files. So far, it is really a joy to write all my posts in Markdown. I’m basically blogging from Vim right now (and using the Hammer plugin to preview my posts). I wish I had done this move earlier.

<3 Jekyll + Octopress + Github + Markdown + Vim + Hammer.vim

Please let me know if you find any broken links or images not loading: olalonde@gmail.com.

Thanks!

17 Crazy and Not So Crazy Startup and Project Ideas

So today I am cleaning up my phone’s Memo app so this post will be dedicated to dumping all those late night ideas I had when I was very tired or had a few too many beers. Note that I had most of those ideas independently but some of them were (partially) implemented later on or I simply hadn’t heard of them.

1. Beetcoin

Mobile app that acts as a nice UI on top of various Bitcoin exchanges. It should be possible to view pending trades, get current bid/ask prices and place new trades.

The twist: Sell it as a paid app and give away “free” Bitcoins. The app should be expensive enough that when you give away Bitcoins you still make a profit.

Why it will work: This would enable regular people to buy Bitcoins with their credit card by buying the app on the Android market.

Why it won’t work: It is possibly against most markets / credit card processors TOS.

2. Code reading web app

Reading other people’s code is one of the surest way to improve your programming skills. For each programming language, this app would list open source projects by difficulty level (from beginner to expert) and make it easy to browse and comment the code from within the website. The MVP could simply list links to Github repos. Inspiration comes from http://todomvc.com.

Use case: I just started to learn Javascript and I want to see some real world Javascript code. I go to the site, select the Javascript language and I can see a list of projects rated from Beginner level to Expert level. Each projects that are at the same difficulty level are grouped together. The beginner level should list simple “Hello world” apps while the expert level should be reserved for complex projects.

3. Prediction market, Hacker News style

Not sure what I meant when writing this. I think the idea is that it would be a news aggregator like Hacker News but when you vote up it costs you real money (you are investing in a vote). If the vote count goes up, you are rewarded for predicting correctly that the article was interesting to the community. The reward should be proportional to how many people up voted after you. I guess someone still needs to work out the “details”. The whole idea is to mix karma with money to self-moderate a community (most people care more about money than karma).

4. OAuth for payments

I’m not sure why I haven’t seen this yet in the wild. Probably due to legal compliance and security issues. But well, I think it would be cool if I could just buy stuff by clicking “Pay 10$” and whenever I click that button, I get the standard OAuth dialog asking me to authorize the payment. There should be a maximum amount by transaction which should be fairly low if security is an issue (50$?).

5. URL#hash renderer API

I first had this idea when developing a website with Backbone.js. All my URLs looked like http://domain/#a/b/c and I thought it would be nice if my server could proxy all requests to http://domain/a/b/c through an API that returns the rendered HTML of http://domain/#a/b/c.

Some websites are not crawlable or unusable if users don’t have Javascript enabled. This service takes an url of the form http://domain/#hash and returns the rendered HTML that a user would see if they had Javascript enabled. Web developers could use this service to easily offer a crawlable website that is friendly to visitors that have Javascript disabled.

6. Startups for front-end developers

Build a service that makes it possible to develop a full website completely on the client side (probably using Backbone.js or similar), interfacing with a list of predefined APIs. This startups’ job would be to take care of providing a list of easily accessible APIs and make it easy for designers to publish UIs that interact with the APIs. The startup should also take care of common issues like cross-origin request sharing, caching, etc. Eventually, there would be a market where developers can publish APIs. This idea is rooted in the idea of division of labor. Backend developers should focus on what they do best and so should frontend developers. Wouldn’t it be nice if all startups out there were simply a REST API with dozens of UIs you can chose from. What if Paypal was simply an API and anyone could build a nice UI on top of that API.

7. Hacker News directory

List all Hacker News related services, websites, Chrome extensions, publications, apps, etc. There is currently no such official website.

8. “Hello dear [social network] user. I would really appreciate if you [tweeted/liked/up voted] this.”

This idea was inspired by seeing a few “I see you are coming from HN. Please up vote and comment.” when clicking submissions on HN.

This should be a small embeddable script or iframe which should display a small message asking to share the current page on a given social network. The social network is chosen using the visitor’s HTTP referer header or their browser’s history. The script should first check if their referrer matches a social network and if not, use the browser history hack to see if the user has recently visited a known social network. This would be a good alternative for people who want to have multiple sharing buttons but don’t want to clutter the screen real estate with too many choices.

9. Offline Hacker News mobile app

Whenever I have to go on a long subway ride with no 3G or Wifi, I really wish I could just click a button that would load all HN’s front page submissions + comments and load them in my browser’s tab before I leave. Hence, the offline HN app idea.

10. Github orphan

Update: this appears to have been somewhat solved since I had this idea. See http://stillmaintained.com

There should be an easy way to orphan and adopt new projects in Github.

Use case: You want to orphan a project

Connect with Github. Select project to orphan.

This automatically writes this at the top of your README file:

# ADOPT ME! #
I am no longer able to maintain this project. Click here to adopt it!

Use case: You want to adopt a project

  1. Visit a Github project. See that it is open for adoption.
  2. Click the adopt link in the README file.
  3. Authenticate with Github
  4. Wait for adoption authorization by current maintainer

Use case: Approve adopter

  1. You see that someone applied to become a new maintainer for your project.
  2. Click Approve.
  3. Erase everything in your repository except a single README file:

    # THIS PROJECT IS NOW MAITAINED BY ‘new maintainer’ AT github.com/new-maintainer/blabla #

11. Public transportation visualization and simulation

Update: There is something in that vein available for London: http://busmapper.co.uk/. Hey guys, if you are reading this, I’m available for hire :)

Wouldn’t it be cool if you could see buses and subways on a map traveling “real time” and seeing where they are going at a glance. Let’s say your on some street and want to go in some direction (let’s say downtown), you could just open that mobile app, see which buses are coming your way and where they are going to. The simulation could be computed by using bus schedule and averaging the speed between two stops. I prototyped this a few years ago at http://dev.syskall.com/map.

12. Quadcopter delivery

Update: Taco Copter

In areas of high density and high rise buildings, wouldn’t it be nice if you could order something from 7/11 and have a quadcopter (semi-) autonomously deliver your stuff through the window/balcony?

13. Pixel copter advertising

Update: MIT flyfire

Wouldn’t it be nice to have a fleet of programmable mini pixel copters (small helicopters that carry a colored light) draw images / video in the sky. This could be used for promotion and advertising.

14. Online food delivery

This problem has been tackled many times and there are a few companies doing it right now but it still hasn’t gain lots of momentum. Anyways, the idea is that you could just open up a mobile app / web app and through geolocation, get a list of restaurants that can deliver to your location. You can filter out the restaurants by cost, food type, etc. You can then navigate to the menu and place your order.

The twist: There are a lot of “Amazon” and “Ebays” in that space but I haven’t seen any “Shopifys” so far. Meaning that most of the startups in that space, have a central website where they list restaurants. Probably some restaurants would be interested in a white label solution where they could host on their own domain and customize everything to their needs.

15. Instant UML diagram for a given Github repository

I’ve always wanted something like this. Select a given Github repository and instantly get an UML class diagram, whatever the language is. UML is not really a good fit for more dynamical languages but nowadays there are statical analysis tools even for Javascript and company.

16. Ad-hoc sport teams

You want to play a game of soccer? Open up this web app, change you status to “I want to play soccer in the next X hours”. When there is enough people who want to play at the same time as you within a given geographical zone, we all notify you, you chose a location/time and the game begins!

17. Hotel Wifi web app

You connect to your hotel’s wifi and on first browser request, you get redirected to this web app. The web app, displays events in the area, there is a chatroom for other lonely and bored travelers, you can order stuff from the hotel directly from the web app, there hotel guest specific promotions from local businesses, etc. Maybe you will find someone with similar tastes in the chatroom and you can hang out together in this unknown city?

Conclusion

I think that’s about it for now. I will update the post if I bump into more crazy ideas while cleaning up my phone. Let me know what you think in the comments and I’d love to know if you’re solving one of these problems or planning to!

Trouble Installing ImageMagick on OS X 10.7.2

I wanted to use the node-imagemagick library but was getting weird error messages coming from the `identify` binary although I had installed ImageMagick through brew (brew install imagemagick).

I fixed it by grabbing the latest libfreetype from another brew package and linking it to the old library:

YC W12 Startups’ Hosting Decisions

Back in 2011, Joël Franusic published a neat visualization of all Y Combinator startups’ hosting decisions using his neat open source domain-profiler software. I was curious to know what had changed since then, so instead of profiling all YC startups this time, I only profiled the Winter 2012 batch as retrieved from this unofficial YC startups spreadsheet.

So below are the hosting decisions for Y Combinator’s Winter 2012 startups:

Raw data:

Credits to jpf for the awesome domain profiling software.

Discussion on Hacker News.

You should follow me on Twitter here.

UPDATE: Amazon hosting may include Heroku or other services using Amazon as infrastructure. According to the certificate type column in the raw data, at least 6 domains are using Heroku.

First “Startup Tuesday” in Shenzhen a Success!

Only a few weeks ago, there seemed to be absolutely no interest for tech startups in Shenzhen, China (or at least, among the English speaking community). As a startup guy, I was pretty upset by the seemingly nonexistent community. Of course, there was Hong Kong nearby with a vibrant community but I wanted something in Shenzhen. I decided I’d prove myself wrong by creating a ”Startups and technology SZ” group on a popular expat social network called ShenzhenStuff.com

It turned I was wrong about the lack of interest! The group grew to 32 members within only a few days. Within the same time frame, Michael Michelini, an American expat, and his associates were officially launching a coworking space aimed at web workers and freelancers. Talk about timing! Michael from SZTeam offered to host and run a first “Startup Tuesday” meeting which turned out to be a huge success.

Michael wrote a nice outline of the event and published his slides. The ~50 (I suck at guesstimates) attendees came from all kinds of backgrounds (technical, business, finance, curious people, etc.) and the crowd was about half foreigners, half Chinese. 

For the upcoming events, many suggestions were made: invite speakers to talk about a specific topic, co-founder “speed dating”, startup weekends, pitch nights, etc. I’m particularly interested in co-founder speed dating as I’m looking for a co-founder :)

This first meeting was a huge success and Michael deserves all the credit. I would also recommend SZTeam to freelancers or anyone who is working on a startup. The people are friendly, there is a nice conference room, free coffee and the pricing is pretty cheap: hot desk for 500 RMB/month (80 USD/month) and fixed desk for 1500 RMB/month (235 USD/month).

From now on, we will try to gather the community around the Shenzhen Startup mailing list and the Startups and technology SZ group. SZTeam will continue to host Startup Tuesday every Tuesday 7pm.

I think we are on the right path to build a vibrant startup community in Shenzhen!

Adding Google’s +1 (Plus One) Button to Your Posterous

As well all know, Javascript isn’t allowed in Posterous themes. Luckily, iframes are and that’s what we’ll use to go around the no Javascript limitation.

posterous screenshot

Simply go to your Posterous dashboard, click "Settings" on the top right, then click the "Edit Theme" button and select the "Advanced tab". Now, all you have to do is paste one of the following snippets where you want the button to show up and click the "Save, I’m done!" button.

  • Standard button:

  • Tall button:

That’s all you have to do to integrate Google +1 to your Posterous.

If you want to host the script yourself, the source code is available on Github:

git clone https://olalonde@github.com/olalonde/google-plusone-posterous.git

How to Write Your Own Native Node.js Extension

UPDATE: There is now a Node.js addon for loading and calling dynamic libraries using pure JavaScript: node-ffi. Also, node-waf is no longer being used to compile Node.js extensions.

TRANSLATIONS: This post was translated to Chinese: http://www.oschina.net/translate/how-to-write-your-own-native-nodejs-extension

Introduction

This is a follow up to How to roll out your own Javascript API with V8. You should still be able to follow if you haven’t read it.

We will now port the code we have written for V8 to Node.js and package it for npm.

node-notify screenshot

The full source code of this tutorial is available from github:

git clone git://github.com/olalonde/jsnotify.git

You can also install it through npm:

npm install notify

The code was tested on Ubuntu 10.10 64-bit and Node.js v0.5.0-pre.

Getting started

First let’s create a node-notify folder and with the following directory structure.

.
|-- build/                   # This is where our extension is built. 
|-- demo/
|   `-- demo.js              # This is a demo Node.js script to test our extension.
|-- src/
|   `-- node_gtknotify.cpp   # This is the where we do the mapping from C++ to Javascript.
`-- wscript                  # This is our build configuration used by node-waf

This fine looking tree was generated with the tree utility.

Now let’s create our test script demo.js and decide upfront what our extension’s API should look like:

1
2
3
4
5
6
7
8
// This loads our extension on the notify variable. 
// It will only load a constructor function, notify.notification().
var notify = require("../build/default/gtknotify.node"); // path to our extension

var notification = new notify.notification();
notification.title = "Notification title";
notification.icon = "emblem-default"; // see /usr/share/icons/gnome/16x16
notification.send("Notification message");

Writing our Node.js extension

The Init method

In order to create a Node.js extension, we need to write a C++ class that extends node::ObjectWrap. ObjectWrap implements some utility methods that lets us easily interface with Javascript.

Let’s write the skeletton for our class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <v8.h> // v8 is the Javascript engine used by Node
#include <node.h>
// We will need the following libraries for our GTK+ notification 
#include <string>
#include <gtkmm.h>
#include <libnotifymm.h>

using namespace v8;

class Gtknotify : node::ObjectWrap {
  private:
  public:
    Gtknotify() {}
    ~Gtknotify() {}
    static void Init(Handle<Object> target) {
      // This is what Node will call when we load the extension through require(), see boilerplate code below.
    }
};

/*
 * WARNING: Boilerplate code ahead.
 * 
 * See https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/ & http://www.freebsd.org/cgi/man.cgi?query=dlsym
 *  
 * Thats it for actual interfacing with v8, finally we need to let Node.js know how to dynamically load our code. 
 * Because a Node.js extension can be loaded at runtime from a shared object, we need a symbol that the dlsym function can find, 
 * so we do the following:  
 */

v8::Persistent<FunctionTemplate> Gtknotify::persistent_function_template;
extern "C" { // Cause of name mangling in C++, we use extern C here
  static void init(Handle<Object> target) {
    Gtknotify::Init(target);
  }
  // @see http://github.com/ry/node/blob/v0.2.0/src/node.h#L101
  NODE_MODULE(gtknotify, init);
}

Now, we’ll have to we have to write the following code in our Init() method:

  1. Declare our constructor function and bind it to our target variable. var n = require("notification"); will bind notification() to n: n.notification().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Wrap our C++ New() method so that it's accessible from Javascript
// This will be called by the new operator in Javascript, for example: new notification();
v8::Local<FunctionTemplate> local_function_template = v8::FunctionTemplate::New(New);

// Make it persistent and assign it to persistent_function_template which is a static attribute of our class.
Gtknotify::persistent_function_template = v8::Persistent<FunctionTemplate>::New(local_function_template);

// Each JavaScript object keeps a reference to the C++ object for which it is a wrapper with an internal field.
Gtknotify::persistent_function_template->InstanceTemplate()->SetInternalFieldCount(1); // 1 since a constructor function only references 1 object
// Set a "class" name for objects created with our constructor
Gtknotify::persistent_function_template->SetClassName(v8::String::NewSymbol("Notification"));

// Set the "notification" property of our target variable and assign it to our constructor function
target->Set(String::NewSymbol("notification"), Gtknotify::persistent_function_template->GetFunction());
  1. Declare our attributes: n.title and n.icon.
1
2
3
4
5
// Set property accessors
// SetAccessor arguments: Javascript property name, C++ method that will act as the getter, C++ method that will act as the setter
Gtknotify::persistent_function_template->InstanceTemplate()->SetAccessor(String::New("title"), GetTitle, SetTitle);
Gtknotify::persistent_function_template->InstanceTemplate()->SetAccessor(String::New("icon"), GetIcon, SetIcon);
// For instance, n.title = "foo" will now call SetTitle("foo"), n.title will now call GetTitle()
  1. Declare our prototype method: n.send()
1
2
3
// This is a Node macro to help bind C++ methods to Javascript methods (see https://github.com/joyent/node/blob/v0.2.0/src/node.h#L34)
// Arguments: our constructor function, Javascript method name, C++ method name
NODE_SET_PROTOTYPE_METHOD(Gtknotify::persistent_function_template, "send", Send);

Our Init() method should now look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Our constructor
static v8::Persistent<FunctionTemplate> persistent_function_template;

static void Init(Handle<Object> target) {
  v8::HandleScope scope; // used by v8 for garbage collection

  // Our constructor
  v8::Local<FunctionTemplate> local_function_template = v8::FunctionTemplate::New(New);
  Gtknotify::persistent_function_template = v8::Persistent<FunctionTemplate>::New(local_function_template);
  Gtknotify::persistent_function_template->InstanceTemplate()->SetInternalFieldCount(1); // 1 since this is a constructor function
  Gtknotify::persistent_function_template->SetClassName(v8::String::NewSymbol("Notification"));

  // Our getters and setters
  Gtknotify::persistent_function_template->InstanceTemplate()->SetAccessor(String::New("title"), GetTitle, SetTitle);
  Gtknotify::persistent_function_template->InstanceTemplate()->SetAccessor(String::New("icon"), GetIcon, SetIcon);

  // Our methods
  NODE_SET_PROTOTYPE_METHOD(Gtknotify::persistent_function_template, "send", Send);

  // Binding our constructor function to the target variable
  target->Set(String::NewSymbol("notification"), Gtknotify::persistent_function_template->GetFunction());
}

All that is left to do is to write the C++ methods that we used in our Init method: New, GetTitle, SetTitle, GetIcon, SetIcon, Send

Our constructor method: New()

The New() method creates an instance of our class (a Gtknotify object), sets some default values to our properties and returns a Javascript handle to this object. This is the expected behavior when calling a constructor function with the new operator in Javascript.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::string title;
std::string icon;

// new notification()
static Handle<Value> New(const Arguments& args) {
  HandleScope scope;
  Gtknotify* gtknotify_instance = new Gtknotify();
  // Set some default values
  gtknotify_instance->title = "Node.js";
  gtknotify_instance->icon = "terminal";

  // Wrap our C++ object as a Javascript object
  gtknotify_instance->Wrap(args.This());

  return args.This();
}

Our getters and setters: GetTitle(), SetTitle(), GetIcon(), SetIcon()

The following is pretty much boilerplate code. It boils down to back and forth conversion between C++ values to Javascript (V8) values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// this.title
static v8::Handle<Value> GetTitle(v8::Local<v8::String> property, const v8::AccessorInfo& info) {
  // Extract the C++ request object from the JavaScript wrapper.
  Gtknotify* gtknotify_instance = node::ObjectWrap::Unwrap<Gtknotify>(info.Holder());
  return v8::String::New(gtknotify_instance->title.c_str());
}
// this.title=
static void SetTitle(Local<String> property, Local<Value> value, const AccessorInfo& info) {
  Gtknotify* gtknotify_instance = node::ObjectWrap::Unwrap<Gtknotify>(info.Holder());
  v8::String::Utf8Value v8str(value);
  gtknotify_instance->title = *v8str;
}
// this.icon
static v8::Handle<Value> GetIcon(v8::Local<v8::String> property, const v8::AccessorInfo& info) {
  // Extract the C++ request object from the JavaScript wrapper.
  Gtknotify* gtknotify_instance = node::ObjectWrap::Unwrap<Gtknotify>(info.Holder());
  return v8::String::New(gtknotify_instance->icon.c_str());
}
// this.icon=
static void SetIcon(Local<String> property, Local<Value> value, const AccessorInfo& info) {
  Gtknotify* gtknotify_instance = node::ObjectWrap::Unwrap<Gtknotify>(info.Holder());
  v8::String::Utf8Value v8str(value);
  gtknotify_instance->icon = *v8str;
}

Our prototype method: Send()

First we have to extract the C++ object this references. We then build our notification using the object’s properties (title, icon) and finally display it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// this.send()
static v8::Handle<Value> Send(const Arguments& args) {
  v8::HandleScope scope;
  // Extract C++ object reference from "this"
  Gtknotify* gtknotify_instance = node::ObjectWrap::Unwrap<Gtknotify>(args.This());

  // Convert first argument to V8 String
  v8::String::Utf8Value v8str(args[0]);

  // For more info on the Notify library: http://library.gnome.org/devel/libnotify/0.7/NotifyNotification.html 
  Notify::init("Basic");
  // Arguments: title, content, icon
  Notify::Notification n(gtknotify_instance->title.c_str(), *v8str, gtknotify_instance->icon.c_str()); // *v8str points to the C string it wraps
  // Display the notification
  n.show();
  // Return value
  return v8::Boolean::New(true);
}

Compiling our extension

node-waf is the build tool used to compile Node extensions which is basically a wrapper for waf. The build process can be configured with a file called wscript in our top directory:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def set_options(opt):
  opt.tool_options("compiler_cxx")

def configure(conf):
  conf.check_tool("compiler_cxx")
  conf.check_tool("node_addon")
  # This will tell the compiler to link our extension with the gtkmm and libnotifymm libraries.
  conf.check_cfg(package='gtkmm-2.4', args='--cflags --libs', uselib_store='LIBGTKMM')
  conf.check_cfg(package='libnotifymm-1.0', args='--cflags --libs', uselib_store='LIBNOTIFYMM')

def build(bld):
  obj = bld.new_task_gen("cxx", "shlib", "node_addon")
  obj.cxxflags = ["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall"]
  # This is the name of our extension.
  obj.target = "gtknotify"
  obj.source = "src/node_gtknotify.cpp"
  obj.uselib = ['LIBGTKMM', 'LIBNOTIFYMM']

We’re now ready to build! In the top directory, run the following command:

node-waf configure && node-waf build

If everything goes right, we should now have our compiled extension in ./build/default/gtknotify.node. Let’s try it!

1
2
3
4
5
6
$ node
> var notif = require('./build/default/gtknotify.node');
> n = new notif.notification();
{ icon: 'terminal', title: 'Node.js' }
> n.send("Hello World!");
true

The previous code should display a notification in the top right corner of your screen!

Packaging for npm

That’s pretty cool, but how about sharing your hard work with the Node community? That’s primarily what the Node Package Manager is used for: making it easy to import extensions/modules and distribute them.

Packaging an extension for npm is very straightforward. All you have to do is create a package.json file in your top directory which contains some info about your extension:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
  // Name of your extension (do not include node or js in the name, this is implicit). 
  // This is the name that will be used to import the extension through require().

  "name" : "notify",

  // Version should be http://semver.org/ compliant

  "version" : "v0.1.0"

  // These scripts will be run when calling npm install and npm uninstall.

  , "scripts" : {
      "preinstall" : "node-waf configure && node-waf build"
      , "preuninstall" : "rm -rf build/*"
    }

  // This is the relative path to our built extension.

  , "main" : "build/default/gtknotify.node"

  // The following fields are optional:

  , "description" : "Description of the extension...."
  , "homepage" : "https://github.com/olalonde/node-notify"
  , "author" : {
      "name" : "Olivier Lalonde"
      , "email" : "olalonde@gmail.com"
      , "url" : "http://www.syskall.com/"
    }
  , "repository" : {
      "type" : "git"
      , "url" : "https://github.com/olalonde/node-notify.git"
    }
}

For more details on the package.json format, documentation is available through npm help json. Note that most fields are optional.

You can now install your new npm package by running npm install in your top directory. If everything goes right, you should be able to load your extension with a simple var notify = require('your-package-name');. Another useful command is npm link which creates a symlink to your development directory so that any change to your code is reflected instantly - no need to install/uninstall perpetually.

Assuming you wrote a cool extension, you might want to publish it online in the central npm repository. In order to do that, you first need to create an account:

$ npm adduser

Next, go back to the root of your package code and run:

$ npm publish

That’s it, your package is now available for anyone to install through the npm install your-package-name command.

Conclusion

Writing a native Node extension can be cumbersome and verbose at times but it is well worth the hard earned bragging rights!

Thanks for reading. Let me know in the comments if you run into any problem, I’ll be glad to help.

If you liked this, maybe you’d also like what I tweet on Twitter! Might even want to hire me?

References

Resizing a VDI in VirtualBox 3 With One Command

Here’s a quick way to expand a (VDI) Virtual Disk Image in VirtualBox 3.

  1. Create a new VDI with the new size of your choice. (File / Virtual Media Manager / New…)
  2. Run this command:

    $ VBoxManage clonehd --existing old.vdi new.vdi

    This may take a few minutes.

  3. Simply replace the attached old.vdi with the new.vdi in your virtual machine’s storage settings.

  4. You will need to extend your partition from your guest OS. This can be done under Windows 7 from the control panel (Create and format hard disk partitions) and with GParted in Ubuntu and compatible Linux distributions.

Minimalist Shell in C (for Educational Purposes Only!)

Introduction

I was kind of bored tonight so I decided to write a very minimalist shell in C. Here’s how it looks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * DISCLAIMER: THIS CODE IS FOR EDUCATIONAL PURPOSES ONLY. USE AT YOUR OWN RISKS.
 *
 * This code shows the basic workings of a shell.
 *
 * Append "/path/to/dashell" to /etc/shells, to make it a valid shell:
 * sudo echo "/path/to/dashell" >> /etc/shells
 *
 * Change your "username"'s shell. "username" should have execute permission for the shell:
 * chsh --shell /path/to/dashell username
 *
 */

#include <unistd.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/signal.h>

#define STDIN 0
#define STDOUT 1
#define STDERR 2

#define BUFFER_SIZE 1024

void parse_arguments(char buffer[], int *args_count, char *args[]) {
  char *delimiters = " \r\n";
  char *token;
  *args_count = 0;
  // "abc def ghi" => {"abc", "def", "ghi"}
  while(token = strsep(&buffer, delimiters)) {
    args[*args_count] = token;
    (*args_count)++;
  }
}

int main(int argc, const char* argv[]) {
  // The weird characters are used to format the text's appearance.
  // See http://en.wikipedia.org/wiki/ANSI_escape_code
  char prompt[] = "\033[1mdashell\033[2m>\033[0m ";
  char exec_error[] = "Cannot execute program %s.\n";
  char buffer[BUFFER_SIZE + 1];

  int args_count;
  char *args[BUFFER_SIZE];

  int n;
  while(1) {
    write(STDOUT, prompt, strlen(prompt) + 1);
    n = read(STDIN, buffer, BUFFER_SIZE); // Read from STDIN (keyboard input)
    buffer[n] = '\0'; // Null character to indicate string end

    // "abc def ghi" => {"abc", "def", "ghi"}
    parse_arguments(buffer, &args_count, args);

    // No arguments
    if(args_count == 0 || strcmp(args[0], "") == 0) continue;

    // Argument = exit
    if(strcmp(args[0], "exit") == 0) exit(0);

    pid_t child_pid = fork(); // Duplicate process
    if(child_pid == 0) {
      // Child
      if(execvp(args[0], args) < 0) { // Replace executable code by command passed
        fprintf(stderr, exec_error, args[0]);
      }
    }
    else {
      // Parent
      // Wait for child to finish
      wait();
    }
  }
}

The full source code is freely available at Github:

git clone git://github.com/olalonde/dashell.git

Note that I never code in C so it might not be perfect… I’m looking at you parse_arguments() ;)

Install & have fun

The optional steps will let you use the shell as a login shell for a given user.

  1. make

  2. (optional) Append “/path/to/dashell” to /etc/shells, to make it a valid shell:

1
sudo echo "/path/to/dashell" >> /etc/shells
  1. (optional) Change “username“‘s shell. “username” should have execute permission for the shell:
1
chsh --shell /path/to/dashell username

Now, you can launch the shell and start having fun with it and be reminded how great bash really is!

1
2
3
4
5
./dashell
dashell> ls -al
dashell> ./launchme 1 2 3
....
dashell> exit

Feel free to ask questions!

How to Roll Out Your Own Javascript API With V8

Update: I wrote a new tutorial on porting our V8 code as a Node.js extension: How to write your own native Node.js extension.

Introduction

This tutorial will teach you how to:

  1. Compile the V8 Javascript engine
  2. Bind a Javascript function to your own C++ function

For the sake of demonstration and to impress your co-workers, we will bind a Javascript function “alert()“ that will display desktop notifications through the GTK library. Here’s what the end result looks like:

jsnotify screenshot

You can get the full source code of this tutorial from github:

git clone git://github.com/olalonde/jsnotify.git

This tutorial was tested on Ubuntu 10.04 and 10.10 64-bit but should work fine on any Linux distribution. The notification part requires the GTK+ library.

Compiling Google’s V8 Javascript engine

First, let’s make sure we have all the required tools and dependencies to compile.

sudo apt-get install build-essential scons subversion 
  • The build-essential package is a meta package that installs all the necessary tools and libraries to compile C++ programs.
  • SCons is a build tool which attempts to replace the classic “make” and is used by the V8 project.
  • Subversion is needed to checkout the source code of V8.

Now, let’s grab V8’s source from the official repository:

svn checkout http://v8.googlecode.com/svn/trunk/ v8 

We can now move into the V8 directory and try to compile!

cd v8;
scons arch=x64; 

The “arch=x64” option specifies that we want to build a 64-bit version of V8 (the default value would be 32-bit otherwise).

If V8 compiled fine, you should now have a libv8.a file in your v8/ directory. As you probably guessed, libv8.a is the library that our C++ program will use to execute Javascript code.

So, if everything compiled fine, just skip to the next section. Otherwise, keep on reading.

When you get errors as a result of compiling third party code, it is usually due to the fact that the compiler can’t find required libraries (/usr/lib) and/or their associated header files (/usr/lib/include). The latter are usually available through packages conventionally named libname-dev . In order to find out which package installs a given file, there is a neat utility called apt-file.

sudo apt-get install apt-file;
apt-file search missing-header-file.h; 

The apt-file search command lists the package(s) that install a given file (missing-header-file.h in this case). If there are more than one package listed, we have to take a semi-educated guess on which package we should install based on its name (let me know in the comments if you know of a better trick!). We then simply install the package with the usual apt-get install package-name command.

Hint: If you are on Ubuntu 10.04, you might need to install the following packages:

sudo apt-get install libc6-dev-i368 lib32stdc++6

Now that we’ve installed all the missing files, the compilation should work. Let’s move on to the next section.

If you are still stuck with compiling V8, this tutorial might help.

Building our own Javascript API

Now that we have successfully compiled the V8 library, we will build our own C++ project that will be “Javascript scriptable”. This means that our program will be able to run Javascript code which in turn will be able to call our custom C++ functions.

Note: You can also get the full source code of this tutorial from my jsnotify github repository): git clone git://github.com/olalonde/jsnotify.git

First let’s create our file structure.

jsnotify/
  |-- deps/  # third party code
  |   `-- v8  # move your v8 folder here
  `-- src/ # our code goes here
      `-- jsnotify.cpp

Now let’s copy the sample code available at deps/v8/samples/shell.cc and paste it into jsnotify.cpp. The sample code given by V8 let’s you execute a Javascript file or start an interactive Javascript shell. It also binds some useful Javascript functions such as print() which will output text to the terminal.

Let’s try to compile this!

g++ src/jsnotify.cpp; 

Of course, this gives us a bunch of errors since we haven’t specified where the V8 header and library files are. Let’s try again!

g++ src/jsnotify.cpp -Ideps/v8/include -Ldeps/v8/ -lv8

Oops, still some errors. Looks like we also have to link the pthread library.

g++ src/jsnotify.cpp -Ideps/v8/include -Ldeps/v8/ -lv8 -lpthread

This finally compiles! Now that we have our mini Javascript shell, let’s play a bit with it.

1
2
3
4
5
$ ./a.out
V8 version 3.1.5
> var foo = “Hello World”;
> print(foo);
Hello World

Now, all we have to do is to create our custom alert() function in C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// INSERT THIS BEFORE int RunMain(int argc, char* argv[]) {
// We need those two libraries for the GTK+ notification 
#include <gtkmm.h>
#include <libnotifymm.h>
v8::Handle<v8::Value> Alert(const v8::Arguments& args);

// INSERT THIS AT END OF FILE   
// The callback that is invoked by v8 whenever the JavaScript 'alert'
// function is called.  Displays a GTK+ notification.
v8::Handle<v8::Value> Alert(const v8::Arguments& args) {
  v8::String::Utf8Value str(args[0]); // Convert first argument to V8 String
  const char* cstr = ToCString(str); // Convert V8 String to C string

  Notify::init("Basic");
  // Arguments: title, content, icon
  Notify::Notification n("Alert", cstr, "terminal");
  // Display notification
  n.show();

  return v8::Undefined();
}

Now that we have our Alert C++ function, we need to tell V8 to bind it to the Javascript alert() function. This is done by adding the following code in the RunMain function:

1
2
3
// INSERT AFTER v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
// Bind the global 'alert' function to the C++ Alert callback.
global->Set(v8::String::New("alert"), v8::FunctionTemplate::New(Alert));

Now, in order to compile, the compiler needs to know where to find the two header files we introduced. This is done using the pkg-config utility:

g++ src/jsnotify.cpp -Ideps/v8/include -Ldeps/v8/ -lv8 -lpthread `pkg-config --cflags --libs gtkmm-2.4 libnotifymm-1.0`

We can now try our new alert function.

1
2
3
$./a.out
V8 version 3.1.5
> alert(“wow, it works!”);

You should see a nice notification in the top right of your screen! Note that you can also put you Javascript code in a file and pass the file name as an argument ./a.out filename.js.

Conclusion

It’s quite easy to make a C++ program “Javascriptable” with V8 and the proper setup. If you’d like to practice your newfound skills, I suggest you try to add a title argument to the alert function. You might also want to follow me on Posterous if you’d like to be informed when I post the follow up to this tutorial which will explain how to extend Node.js with our alert function.

That’s all for today, thanks for reading! Let me know in the comments if you run into any problem, I’ll be glad to help.

If you liked this, maybe you’d also like what I tweet on Twitter!