Skip to content

Instantly share code, notes, and snippets.

View TooTallNate's full-sized avatar

Nathan Rajlich TooTallNate

View GitHub Profile
/home/nowuser $ env
SHLVL=7
HOME=/home/nowuser
NOW=1
TEMP=/tmp
TERM=xterm-256color
PATH=/home/nowuser/src/node_modules/.bin:/home/nowuser/bin://usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/node_modules/@zeit/ace/build/node_modules/.bin
DEPLOYMENT_ID=<redacted>
AUTH_TOKEN=<redacted>
PWD=/home/nowuser
@TooTallNate
TooTallNate / updater.sh
Last active May 23, 2019 00:02
Google Domains Dynamic DNS record update script - syncs a host to a target (pseudo-ALIAS)
#!/usr/bin/env sh
#
# Usage:
# USERNAME=abcd12345 PASSWORD=ghij67890 HOST=n8.io TARGET=alias.zeit.co ./updater.sh
URL="https://domains.google.com/nic/update"
HOST_IP=$(dig @8.8.8.8 +short A "$HOST" | sort | tail -1)
TARGET_IP=$(dig @8.8.8.8 +short A "$TARGET" | sort | tail -1)
@TooTallNate
TooTallNate / http-error.js
Created August 3, 2016 00:48
HTTPError class for JavaScript HTTP errors
import { format } from 'url';
import { STATUS_CODES } from 'http';
import uppercamelcase from 'uppercamelcase';
class HTTPError extends Error {
constructor(code, message, extras) {
super(message || STATUS_CODES[code]);
if (arguments.length >= 3 && extras) {
Object.assign(this, extras);
}
// These theoretical API requests are deferred promises;
// Not executed until `.then()` is invoked.
let a = new Request('/foo');
let b = new Request('/bar');
let c = new Request('/baz');
// If invoked directly, then issue 3 direct HTTP requests:
Promise.all([ a, b, b ]).then((results) => {
// GET /foo
// GET /bar
@TooTallNate
TooTallNate / example.js
Created March 29, 2016 20:50
LazyPromise implementation. Doesn't invoke the executor function until `.then()` is called.
'use strict';
const Lazy = require('./lazy-promise');
class TimerPromise extends Lazy(Promise) {
constructor() {
const start = new Date();
super((resolve, reject) => resolve(new Date() - start));
}
}
@TooTallNate
TooTallNate / pbedit.sh
Created March 18, 2016 18:29
`pbedit` command: Edit the clipboard in your $EDITOR
# Edit the clipboard in your $EDITOR
# Add this to your `.bashrc` file
function pbedit () {
local tmpfile=`mktemp`
pbpaste > $tmpfile
$EDITOR $tmpfile
pbcopy < $tmpfile
rm -f $tmpmfile
}
export -f pbedit
@TooTallNate
TooTallNate / install.sh
Created January 13, 2016 03:25
Install Node.js one-liner
curl https://nodejs.org/dist/v4.2.4/node-v4.2.4-linux-x64.tar.gz | tar xzvf - --exclude CHANGELOG.md --exclude LICENSE --exclude README.md --strip-components 1 -C /usr/local/
@TooTallNate
TooTallNate / MatchIterator.js
Created November 16, 2015 21:03
for (let match of /foo/g.exec('foo bar baz')) console.log(match);
function *MatchIterator (string, regexp, match) {
if (match) yield match;
match = regexp.exec(string);
while (match != null) {
yield match;
match = regexp.exec(string);
}
};
module.exports = MatchIterator;
@TooTallNate
TooTallNate / line-iterator.js
Last active July 4, 2016 03:57
ES6 String iterator that parses line-by-line off of \n
'use strict';
function *LineIterator (str) {
let pos = 0;
let delimiter = '\n';
while (true) {
let index = str.indexOf(delimiter, pos);
if (index === -1) break;
yield str.substring(pos, index);
pos = index + delimiter.length;
const fs = require('fs');
const ffi = require('ffi');
const ref = require('ref');
const Struct = require('ref-struct');
const ArrayType = require('ref-array');
const ioctl = ffi.ForeignFunction(
ffi.DynamicLibrary().get('ioctl'),
'int', ['int', 'int', 'void*']
);