Skip to content

Instantly share code, notes, and snippets.

View indexzero's full-sized avatar
🗽
✊ Fist in the air in the land of hypocrisy

Charlie Robbins indexzero

🗽
✊ Fist in the air in the land of hypocrisy
View GitHub Profile
@indexzero
indexzero / ssh2-tunnel.js
Last active February 4, 2024 05:47
Thoughts about SSH tunnellng using `ssh2`
var fs = require('fs'),
path = require('path');
Connection = require('ssh2');
var tunnelHost = process.argv[2],
dest = process.argv[3],
keyfile = process.argv[4];
var conn = new Connection();
@indexzero
indexzero / splice-or-die.js
Created March 7, 2013 05:57
splice or die
var x = [1,2,3,4,5,6,7,8,9,10],
y = [],
num;
for (var i = 0, count = 5; y.length < count && i < x.length; ) {
if (x[i] < 6) {
i++
continue;
}
@indexzero
indexzero / quotes.md
Created March 13, 2013 20:45
Quotes I like or find on the Internets and stuff.

"Nevertheless, the fact is that there is nothing as dreamy and poetic, nothing as radical, subversive, and psychedelic, as mathematics. It is every bit as mind blowing as cosmology or physics... and allows more freedom of expression than poetry, art, or music... Mathematics is the purest of the arts, as well as the most misunderstood." -Paul Lockhart

Node Error Handling and Domains

"Occurrences in this domain are beyond the reach of exact prediction because of the variety of factors in operation, not because of any lack of order in nature." - Albert Einstein

"Master of my domain" - Larry David

Error handling in an asynchronous language works in a unique way and presents many challenges, some unexpected. This tutorial reviews the various error handling patterns available in node, and explains how domains can be used to improve it.

There are four main error handling patterns in node:

  • Error return value
@indexzero
indexzero / array-memory-usage.js
Created March 20, 2013 02:50
Strings are not recycled in arrays.
var util = require('util');
var max = parseInt(process.argv[2], 10);
var obj = (new Array(26)).reduce(function (mem, _, i) {
var key = String.fromCharCode(65 + i);
mem[key] = key + ' value';
return mem;
}, {});
@indexzero
indexzero / reduce-dir.js
Created March 23, 2013 22:13
Recursively build an object of all files and directories in a directory tree.
var fs = require('fs'),
path = require('path'),
async = require('async');
function reduceDir(dir, mem, callback) {
if (!callback && typeof mem === 'function') {
callback = mem;
mem = null;
}
@indexzero
indexzero / styleguide.md
Last active June 27, 2016 21:29
A working draft of an "official" Nodejitsu Javascript style guide.

Javascript Styleguide

If you have comments on this or disagree about rules then please reach out to me directly. I want to hear it!

Table of Contents

Basics

So, to get something like /etc/rc.local you can use the custom SMF import facility. (See the source for more information about how this actually works.)

/opt is mounted out of zones/opt by default. You can create a directory /opt/custom/smf and populate it with SMF manifests. Any manifests you put in there will be imported by SmartOS when it boots. Below is an example SMF manifest that simply starts /opt/custom/bin/postboot, a self-explanatory shell script that you can use like /etc/rc.local.

Note that it would likely be better to customise and respin your own images, as putting a bunch of platform state in the zones pool undoes some of the benefits of the ramdisk platform architecture that SmartOS has.

@indexzero
indexzero / lsr
Last active December 16, 2015 11:29
ls recursive -- useful for big lists of files
#!/bin/bash
ROOT=$1
if [ ! -d $ROOT ]; then
echo "Usage lsr [dir]"
exit 1
fi
if [ ! -z $2 ]; then