Skip to content

Instantly share code, notes, and snippets.

@danmactough
danmactough / comment.md
Created December 31, 2013 00:22
Isaac's description of process.nextTick vs. setImmediate

I agree the point you’re making here, 100%. However, a slight correction about Node’s APIs.

First of all, process.nextTick is actually first in, first out. Proof:

$ node -e 'process.nextTick(console.log.bind(console, 1)); process.nextTick(console.log.bind(console, 2))'
1
2
@danmactough
danmactough / localport.sh
Created December 17, 2013 15:21
Block port except via localhost. Optionally, pass `-D` as second argument to remove the block.
#/bin/bash
PORT=$1
ACTION=${2:-"-A"}
iptables $ACTION INPUT -p tcp -s localhost --dport $PORT -j ACCEPT
iptables $ACTION INPUT -p tcp --dport $PORT -j DROP
@danmactough
danmactough / tunnel
Created December 13, 2013 22:45
Make it easy to create an ssh tunnel
#!/bin/bash
if [ $# -ne 3 ]
then
echo "Usage: `basename $0` remote remote-port local-port"
exit 1
fi
ssh -N -L $3:localhost:$2 $1
@danmactough
danmactough / rtunnel
Created December 13, 2013 22:44
Make it easy to create a reverse ssh tunnel
#!/bin/bash
if [ $# -ne 3 ]
then
echo "Usage: `basename $0` remote remote-port local-port"
exit 1
fi
ssh -N -R $2:localhost:$3 $1
@danmactough
danmactough / no-with.js
Created December 13, 2013 05:42
Closure instead of with
[dan@Dans-MacBook-Pro code]$ node
> var code = 'function printer (val) { return val; }; var result = printer(a); console.log(result);'
undefined
> eval(code)
ReferenceError: a is not defined
at eval (eval at <anonymous> (repl:1:7), <anonymous>:1:62)
at repl:1:2
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
@danmactough
danmactough / if-piper.js
Created December 2, 2013 17:21
Pseudo-code for conditional piping
var feed = 'http://somefeed.com/rss'
var req = request(feed)
req.on('response', function (response) {
if (sometest(feed)) {
req.pipe(iconv).pipe(parser)
}
else {
req.pipe(parser)
}
})
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"not": {
"bool": {
"must": {
@danmactough
danmactough / wrapper.js
Created October 2, 2013 14:07
URL fetching wrapper for feedparser v.16
var FeedParser = require('feedparser')
, request = require('request')
, STATUS_CODES = require('http').STATUS_CODES;
module.exports = function (feed, cb) {
var parser = new FeedParser();
var req = request(feed);
var items = [];
var _called = false;
#!/bin/sh
#
# To enable this hook, link or rename this file to "pre-commit".
# If you save it in your ~bin/ directory as "check" and link it as "pre-commit,"
# you can easily run it manually from a dirty wc in addition to running as hook
STATUS=0
SELF=`basename $0`
CACHED=""
@danmactough
danmactough / fail.js
Created July 31, 2013 18:25
slug fail
var slug = require('slug');
console.log(slug('Ȁ ȁ Ȃ ȃ Ȅ ȅ Ȇ ȇ Ȉ ȉ Ȋ ȋ Ȍ ȍ Ȏ ȏ Ȑ ȑ Ȓ ȓ Ȕ ȕ Ȗ'));
// ''