Skip to content

Instantly share code, notes, and snippets.

Coding away...

Sander Tolsma stolsma

Coding away...
View GitHub Profile
stolsma / test.js
Created Aug 25, 2012
Lower worker privilege with cluster or fork when run as root and still able to use unprivilaged ports
View test.js
var cluster = require('cluster');
var util = require('util');
var http = require('http');
var numCPUs = 2; //require('os').cpus().length;
// general functions
function isRoot() {
return process.getuid() == 0;
View 1 Instructions for compiling node0.8.8 with

These instructions work for the Raspberry Pi running Raspbian (hard float) and create a hardware optimized version of NodeJS for the Raspberry PI, (and include a working install and NPM!!!):

  1. Install Raspbian -

  2. Install the necessary dependecies:

sudo apt-get install git-core build-essential

(If you just installed git then you need to administer your git identity first, else adding the patches below will fail!!!)

stolsma / getPeerCertificate.js
Created Oct 1, 2011
getPeerCertificate example
View getPeerCertificate.js
var https = require('https'),
fs = require('fs');
var options = {
key: fs.readFileSync('./ssl/privatekey.pem'),
cert: fs.readFileSync('./ssl/certificate.pem'),
requestCert: true
https.createServer(options, function (req, res) {
stolsma /
Created Sep 27, 2011 — forked from jamiew/
Put this in your ~/.bash_profile or ~/.bashrc file. Then type source ~/.bashrc or source ~/.bash_profile to reload.
parse_git_branch() {
ref=$(git symbolic-ref -q HEAD 2> /dev/null) || return
printf "${1:-(%s)}" "${ref#refs/heads/}"
pimp_prompt() {
local BLUE="\[\033[0;34m\]"
local BLUE_BOLD="\[\033[1;34m\]"
local RED="\[\033[0;31m\]"
local LIGHT_RED="\[\033[1;31m\]"
stolsma /
Created Sep 26, 2011
Why only IPv6 will also listen for IPv4 on most Linux systems...

The best approach is to create an IPv6 server socket that can also accept IPv4 connections. To do so, create a regular IPv6 socket, turn off the socket option IPV6_V6ONLY, bind it to the "any" address, and start receiving. IPv4 addresses will be presented as IPv6 addresses, in the IPv4-mapped format.

The major difference across systems is whether IPV6_V6ONLY is a) available, and b) turned on or off by default. It is turned off by default on Linux (i.e. allowing dual-stack sockets without setsockopt), and is turned on on most other systems.

In addition, the IPv6 stack on Windows XP doesn't support that option. In these cases, you will need to create two separate server sockets, and place them into select or into multiple threads.

Found on:

stolsma / clone.js
Created Sep 25, 2011 — forked from polotek/clone.js
Real, deep copied objects.
View clone.js
function hasOwnProperty(key) {
if(this[key]) {
var proto = this.prototype;
if(proto) {
return ((key in this.prototype) && (this[key] === this.prototype[key]));
return true;
} else {
return false;
stolsma / example.js
Created Sep 20, 2011
Downgrade process after connecting to port 80
View example.js
app.listen(process.env.NODE_ENV === 'production' ? 80 : 8000, function() {
// if run as root, downgrade to the owner of this file
if (process.getuid() === 0)
require('fs').stat(__filename, function(err, stats) {
if (err) return console.log(err)
stolsma /
Created Sep 20, 2011 — forked from visnup/
node.js knockout 2011 StackScript
# <UDF name="ssh_key" Label="Paste in your public SSH key" default="" example="" optional="false" />
# root ssh keys
mkdir /root/.ssh
echo $SSH_KEY >> /root/.ssh/authorized_keys
chmod 0700 /root/.ssh
# update to latest
stolsma / net.js
Created Aug 12, 2011
carapace-haibu net.js test code to understand the port bind errors....
View net.js
* net.js: Wrapper around node.js core `net` module for observing relevant events
* (C) 2011 Nodejitsu Inc.
var net = require('net'),
binding = process.binding('net'),
carapace = require('./carapace');
stolsma /
Created Jun 23, 2011
Install NodeJS:master, NPM:master from Github and node-inspector from NPM on clean system. Install Haibu with standard config on system.
# Install paths to Nodejs, NPM, node-inspector and haibu in user system.
# this gist can be run using: curl | sh
# location of node and supporting scripts
# put node, npm and other global node programs in path if needed