Skip to content

Instantly share code, notes, and snippets.

View stolsma's full-sized avatar
🙂
Coding away...

Sander Tolsma stolsma

🙂
Coding away...
View GitHub Profile
@stolsma
stolsma / client.sh
Created June 23, 2011 11:17
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 https://raw.github.com/gist/1042374/client.sh | sh
# location of node and supporting scripts
NODE="/home/node/local/bin"
HAIBU="/home/haibu/haibu"
# put node, npm and other global node programs in path if needed
@stolsma
stolsma / net.js
Created August 12, 2011 16:19
carapace-haibu net.js test code to understand the port bind errors....
/*
* 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
stolsma / StackScript.sh
Created September 20, 2011 14:13 — forked from visnup/StackScript.sh
node.js knockout 2011 StackScript
#!/bin/bash
# <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
stolsma / example.js
Created September 20, 2011 14:47
Downgrade process after connecting to port 80
app.listen(process.env.NODE_ENV === 'production' ? 80 : 8000, function() {
console.log('Ready');
// 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)
process.setuid(stats.uid);
});
});
@stolsma
stolsma / clone.js
Created September 25, 2011 18:51 — forked from polotek/clone.js
Real, deep copied objects.
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
stolsma / explaination.md
Created September 26, 2011 19:29
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: http://stackoverflow.com/questions/1618240/how-to-support-both-ipv4-and-ipv6-connections

@stolsma
stolsma / pimp_prompt.sh
Created September 27, 2011 07:08 — forked from jamiew/pimp_prompt.sh
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
stolsma / getPeerCertificate.js
Created October 1, 2011 05:08
getPeerCertificate example
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) {

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 - http://www.raspberrypi.org/downloads

  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
stolsma / test.js
Created August 25, 2012 13:42
Lower worker privilege with cluster or fork when run as root and still able to use unprivilaged ports
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;