Friday, March 2, 2012

Installing node.js on OS X 10.5

The binary distributions of node.js no longer work on 10.5 (at least no distribution I could find.). So I went about building my own from source. There's several pitfalls I had to overcome, so I figured I'd list the solutions here.

Step 1. Download the source. I did this using:

git clone git://github.com/joyent/node.git
git checkout origin/v0.6.11-release

Step 2. Patch the included v8 source. If you dont patch it you get an error about missing symbols for Dictionary::SlowReverseLookup, although they may be mangled, so it's not to obvious.

diff --git a/deps/v8/src/objects.cc b/deps/v8/src/objects.cc
index 88ebbf4..c4aea1c 100644
--- a/deps/v8/src/objects.cc
+++ b/deps/v8/src/objects.cc
@@ -10012,6 +10012,9 @@ template Object* Dictionary::
 template Object* Dictionary::SlowReverseLookup(
     Object*);
 
+template Object* Dictionary::SlowReverseLookup(
+    Object*);
+
 template void Dictionary::CopyKeysTo(
     FixedArray*,
     PropertyAttributes,

Step 3. Download a new version of openssl and build and install shared versions. I downloaded version 0.9.8t and built it using:

./config shared
make
make install
On OS X by default this installs to /usr/local/ssl If you try to use the default version of openssl then you get a bunch of errors:
../src/node_crypto.cc: In member function ‘bool node::crypto::DiffieHellman::Init(int)’: 
../src/node_crypto.cc:3537: error: ‘DH_generate_parameters_ex’ was not declared in this scope 
../src/node_crypto.cc: In static member function ‘static v8::Handle node::crypto::DiffieHellman::ComputeSecret(const v8::Arguments&)’: 
../src/node_crypto.cc:3811: error: ‘DH_check_pub_key’ was not declared in this scope 
../src/node_crypto.cc:3814: error: ‘DH_CHECK_PUBKEY_TOO_SMALL’ was not declared in this scope 
../src/node_crypto.cc:3817: error: ‘DH_CHECK_PUBKEY_TOO_LARGE’ was not declared in this scope 
If you forget to build a shared version then node will almost compile, but complain about missing symbols at link time for the final binary.

Step 4. Build node using the changes you've made:

./configure --openssl-includes=/usr/local/ssl/include/ --openssl-libpath=/usr/local/ssl/lib/
make
make install

Now hopefully you'll have a running version of node installed.

Thursday, February 9, 2012

Javascript objects are not hashes

I've some across several posts from people warning about using objects as maps( [1], [2] ), and while they're right I think they miss an important feature. Objects are not hashes. Instead they try to show you how to use them like objects.

The key to the whole issue is the difference in behavior of in and hasOwnProperty.

For example if I want to set a property in an object using a user supplied string I would do this:

This makes it clear that assigning & updating a field is a ternary (append/update/fail) rather than a binary issue (append/update).

If you're feeling paranoid about already broken data, then you might change the use of posts.hasOwnProperty to Object.prototype.hasOwnProperty.call(posts,slug). But this code should prevent hasOwnProperty from getting overwritten in the first place.

Tuesday, July 5, 2011

Examining multiple cores with gdb and grep

So you've got a bunch of core files from a failing application. You look in one of them and find what's causing that particular error. However you're not sure whether all the cores are caused by the one same issue.

The application I was having trouble with was a multithreaded app, so I couldn't rely on the stacs being identical every time... so this is what I did.

stackdumper.gdb Then in bash:

In my case I found that string in all 80 cores, so I know that was the only issue.

Thursday, June 2, 2011

Javascript databindings with observers

This is not quite as clear as the previous post, but heres a version of my javascrip prototyping with observers on the data. Again you can play with it using jsfiddle .. or heres the code.

Creating data bindings using javascript closures

I've been working on javascript codebase that stores all its data in forms, hiding and showing all the form elements as the view changes. This has worked well while the structure of the data was relatively rigid. But now we've got dynamically structured (tree-like) data we need to be able to grow and shrink the views in more flexible ways.

While I'm not a gung-ho MVC advocate, it certainly looked like the app could do with a bit of an MVC style cleanup (the data was in the view rather than being accessed by the view).

The problem was it wasn't clear how to get the data out of the view... here's a simplified version of the method I'm planning to use.


You can also play with this using jsfiddle

I'm thinking about implementing broadcasting of changes and more model based approach later... but maybe that'll have to be another post.

Wednesday, May 18, 2011

Gits prepare-commit-message hook

So I often write bad commit messages. At best they're inconsistent - sometimes with ticket ids at the start sometimes with ticket ids at the end. Sometimes only a short message, sometimes a nicely formatted bullet point list.

Thankfully git has a tool to help you get these things consistent. Its called the "prepare-commit-msg hook"

I wrote a little python script to make a default commit message. Its not perfect but it should help... It takes the branch name, searches for a version tag and removes it, then searches for anything that might be a ticket id and adds it to the message. Finally it adds some boilerplate.

Command line git trees

Often I'm stuck in a terminal and want to see the commit history in a nice tree format. There's a nice solution ... git log recently learnt the "--graph" feature.

You can get pretty nice results from "--graph --oneline"

But using the tricks from http://www.jukie.net/bart/blog/pimping-out-git-log we can get a much nicer result - including author and branches and time all coloured nicely