Skip to content

Instantly share code, notes, and snippets.

View samgiles's full-sized avatar
🗺️

Sam Giles samgiles

🗺️
  • Citymapper
  • London
View GitHub Profile

Called done { '0': undefined } is logged if the x method does not throw an error.

Nothing is logged if x throws an error.

...
this.y = Q.Promise.resolve(this.y).then(this.x.bind(this)).done(function() {
  console.log("Called done", arguments);
});
@samgiles
samgiles / tcp-proxy.sh
Created December 9, 2014 12:01
Simple TCP proxy
#!/bin/sh -e
if [ $# != 3 ]
then
echo "usage: $0 <src-port> <dst-host> <dst-port>"
exit 0
fi
TMP=`mktemp -d`
BACK=$TMP/pipe.back
@samgiles
samgiles / tcp-proxy.sh
Created January 29, 2015 16:28
Simple TCP proxy src-port -> dest-host:port.
#!/bin/sh -e
if [ $# != 3 ]
then
echo "usage: $0 <src-port> <dst-host> <dst-port>"
exit 1
fi
TMP=`mktemp -d`
BACK=$TMP/pipe.back
@samgiles
samgiles / promisetimeout.js
Last active August 29, 2015 14:16
Tiny Promise timeout implementation
// License MIT;
// Simple Promise timeout implementation, always assume IO is being done by carrier pigeon. Timeout.
function promiseTimeout(promise, timeInMs) {
function promiseTimer(resolve, reject) {
setTimeout(function() { reject(new Error("Promise timed out")); }, timeInMs);
}
return Promise.race([promise, new Promise(promiseTimer)]);
}
// Usage:
@samgiles
samgiles / gist:b3fa9da8f6b8d71ca6b2
Created March 13, 2015 14:12
php56w configure flags
--build=x86_64-redhat-linux-gnu --host=x86_64-redhat-linux-gnu --target=x86_64-redhat-linux-gnu --program-prefix= --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --sysconfdir=/etc --datadir=/usr/share --includedir=/usr/include --libdir=/usr/lib64 --libexecdir=/usr/libexec --localstatedir=/var --sharedstatedir=/var/lib --mandir=/usr/share/man --infodir=/usr/share/info --cache-file=../config.cache --with-libdir=lib64 --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --disable-debug --with-pic --disable-rpath --without-pear --with-exec-dir=/usr/bin --with-freetype-dir=/usr --with-png-dir=/usr --with-xpm-dir=/usr --enable-gd-native-ttf --with-t1lib=/usr --without-gdbm --with-jpeg-dir=/usr --with-openssl --with-pcre-regex --with-zlib --with-layout=GNU --with-kerberos --with-libxml-dir=/usr --with-system-tzdata --with-mhash --enable-dtrace --enable-force-cgi-redirect --disable-phpdbg --libdir=/usr/lib64/php --enable-pcntl --enable-fastcgi --without-readline --with-libedit --
@samgiles
samgiles / api-style.js
Last active August 29, 2015 14:17
API style?
// THIS?
// #wrap(func) - Wrap the function in error handling code
// #wrap(context, func) - Wrap the function in error handling code with additional context
function wrap(context, func) {
if (func === undefined) {
func = context;
context = {};
}
return doThing(context, func)
@samgiles
samgiles / timer.js
Created March 30, 2015 15:54
hrtimer.js
// Usage:
//
// ```JS
// var timer = new Timer();
//
// timer.start("javascript");
//
// compile(function(e, callback) {
// var timeInSecs = timer.end("javascript");
// console.log("It took: ", timeInSecs, "s");
@samgiles
samgiles / async-streams-and-promises.js
Last active October 6, 2017 00:04
Returning a Stream in a Promise is an anti-pattern.
/** Method 1. Use a PassThrough stream */
var PassThrough = require('stream').PassThrough;
function myAsyncContent() {
var myContentStream = new PassThrough();
var streamPromise = getAsyncInfo().then(function(info) {
return createStreamFromInfo(info);
});
@samgiles
samgiles / zonefile.js
Created August 6, 2015 14:25
Parse Zone File to generate a list of all domains defined within it.
// Polyfill
// String.prototype.includes
String.prototype.includes = function (string, index) {
if (typeof string === 'object' && string instanceof RegExp) throw new TypeError("First argument to String.prototype.includes must not be a regular expression");
return this.indexOf(string, index) !== -1;
};
// Array.prototype.includes
Array.prototype.includes = function includes(searchElement /*, fromIndex*/ ) {'use strict';
var O = Object(this);
@samgiles
samgiles / gist:54f11668dc7cdb697500
Created September 17, 2015 09:48
b2gdroid jimdb start error.
Launching org.mozilla.b2gdroid...
Starting: Intent { cmp=org.mozilla.b2gdroid/.App }
Error type 3
Error: Activity class {org.mozilla.b2gdroid/org.mozilla.b2gdroid.App} does not exist.
/Users/sam/code/c/jimdb-arm/bin/../utils/gdbinit:136: Error in sourced command file:
Error while launching org.mozilla.b2gdroid.