Skip to content

Instantly share code, notes, and snippets.

View TooTallNate's full-sized avatar

Nathan Rajlich TooTallNate

View GitHub Profile
@TooTallNate
TooTallNate / emitLines.js
Created February 10, 2012 01:11
Make any ReadableStream emit "line" events
/**
* A quick little thingy that takes a Stream instance and makes
* it emit 'line' events when a newline is encountered.
*
* Usage:
* ‾‾‾‾‾
* emitLines(process.stdin)
* process.stdin.resume()
* process.stdin.setEncoding('utf8')
* process.stdin.on('line', function (line) {
@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);
}
@TooTallNate
TooTallNate / jsfunc.sh
Created June 20, 2018 05:37
Write JavaScript functions - use as bash functions
#!/bin/bash
jsfunc() {
local code="$(cat)"
local fn="$(cat <<EOFF
$1() {
node <(cat <<EOF
require('stream').Readable.prototype.then = function (...args) { return new Promise((res, rej) => { const bufs = []; this.on('error', rej).on('data', buf => bufs.push(buf)).on('end', () => res(Buffer.concat(bufs))); }).then(...args) };
(async () => {
${code}
})().then(val => typeof val !== 'undefined' && console.log(typeof val === 'string' ? val : JSON.stringify(val, null, 2))).catch(err => console.error(err.stack) || process.exit(1));
@TooTallNate
TooTallNate / endianness.js
Created February 10, 2013 20:32
Get host machine endianness using JavaScript Typed Arrays (polyfill for `os.endianness()` in node.js)
function endianness () {
var b = new ArrayBuffer(4);
var a = new Uint32Array(b);
var c = new Uint8Array(b);
a[0] = 0xdeadbeef;
if (c[0] == 0xef) return 'LE';
if (c[0] == 0xde) return 'BE';
throw new Error('unknown endianness');
}
@TooTallNate
TooTallNate / agent.js
Last active October 12, 2022 06:37
Node.js `http.Agent` class implementations...
/**
* Module dependencies.
*/
var net = require('net');
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
/**
@TooTallNate
TooTallNate / vc-files.sh
Last active September 5, 2022 21:54
Uploads a single file as a Now deployment
#!/bin/bash
set -euo pipefail
input="$1"
shift
files_dir="$HOME/files"
mkdir -p "$files_dir"
# Delete old files
rm -f "$files_dir"/*
@TooTallNate
TooTallNate / ref-buffer-type.js
Last active June 3, 2022 13:26
Fixed length "Buffer" type, for use in `ref-struct` type definitions.
var ref = require('ref');
module.exports = BufferType;
/**
* Fixed length "Buffer" type, for use in Struct type definitions.
*
* Optionally setting the `encoding` param will force to call
* `toString(encoding)` on the buffer returning a String instead.
*/
@TooTallNate
TooTallNate / crc.js
Created April 28, 2011 16:53
Modbus Serial RTU CRC Algorithm
var data;
process.stdin.on('data', function(chunk) {
data = chunk
});
process.stdin.on('end', function() {
console.log(data);
var givenCrc = data.slice(data.length-2);
givenCrc = (data[1] << 7 | data[0]);
console.log(givenCrc);
@TooTallNate
TooTallNate / test.sh
Created August 15, 2018 17:46
Running `shakedown` HTTP unit tests through `import`
#!/usr/bin/env bash
eval "`curl -sfLS import.pw`"
import "import.pw/robwhitby/shakedown" # load the framework
shakedown GET /foo # make a GET request
status 404 # assert on http status code
content_type 'text/html' # assert Content-Type header contains string
contains 'Not found' # assert body contains string
matches 'No.*' # assert body matches regex
@TooTallNate
TooTallNate / ffi_closure_alloc_test.c
Created January 7, 2012 20:12
The example from the libffi docs for how to use `ffi_closure` with `ffi_closure_alloc()` and `ffi_closure_free()`.
#include <stdio.h>
#include <ffi.h>
/* Acts like puts with the file given at time of enclosure. */
void puts_binding(ffi_cif *cif, unsigned int *ret, void* args[],
FILE *stream)
{
*ret = fputs(*(char **)args[0], stream);
}