Skip to content

Instantly share code, notes, and snippets.

Avatar

Nathan Rajlich TooTallNate

View GitHub Profile
@TooTallNate
TooTallNate / ffi_closure_alloc_test.c
Created Jan 7, 2012
The example from the libffi docs for how to use `ffi_closure` with `ffi_closure_alloc()` and `ffi_closure_free()`.
View ffi_closure_alloc_test.c
#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);
}
@TooTallNate
TooTallNate / http-error.js
Created Aug 3, 2016
HTTPError class for JavaScript HTTP errors
View http-error.js
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 / bbs.js
Created Mar 16, 2012 — forked from substack/bbs.js
Running a node.js REPL over `curl`
View bbs.js
/**
* Requires node v0.7.7 or greater.
*
* To connect: $ curl -sSNT. localhost:8000
*/
var http = require('http')
, repl = require('repl')
, buf0 = new Buffer([0])
@TooTallNate
TooTallNate / starttls.js
Created Mar 1, 2011
Upgrade a regular `net.Stream` connection to a secure `tls` connection.
View starttls.js
// Target API:
//
// var s = require('net').createStream(25, 'smtp.example.com');
// s.on('connect', function() {
// require('starttls')(s, options, function() {
// if (!s.authorized) {
// s.destroy();
// return;
// }
//
@TooTallNate
TooTallNate / install-nodejs.sh
Created Aug 7, 2012
Simple Node.js installation script using the precompiled binary tarballs
View install-nodejs.sh
#!/bin/sh
VERSION=0.8.6
PLATFORM=darwin
ARCH=x64
PREFIX="$HOME/node-v$VERSION-$PLATFORM-$ARCH"
mkdir -p "$PREFIX" && \
curl http://nodejs.org/dist/v$VERSION/node-v$VERSION-$PLATFORM-$ARCH.tar.gz \
| tar xzvf - --strip-components=1 -C "$PREFIX"
@TooTallNate
TooTallNate / repl-client.js
Created Mar 26, 2012
Running a "full-featured" REPL using a net.Server and net.Socket
View repl-client.js
var net = require('net')
var sock = net.connect(1337)
process.stdin.pipe(sock)
sock.pipe(process.stdout)
sock.on('connect', function () {
process.stdin.resume();
process.stdin.setRawMode(true)
@TooTallNate
TooTallNate / websocket-over-tor.js
Created Aug 16, 2013
Establishing a WebSocket connection to "ws://echo.websocket.org" over a Tor SOCKS proxy. Currently depends on pull request: https://github.com/einaros/ws/pull/220
View websocket-over-tor.js
var WebSocket = require('ws');
var SocksProxyAgent = require('socks-proxy-agent');
var url = process.argv[2] || 'ws://echo.websocket.org';
var agent = new SocksProxyAgent('socks://127.0.0.1:9050');
var opts = {};
opts.agent = agent;
var socket = new WebSocket(url, opts);
socket.on('open', function () {
@TooTallNate
TooTallNate / transcode.js
Last active Nov 17, 2020
Transcode an OGG Vorbis audio file to an MP3 using node-ogg, node-vorbis and node-lame
View transcode.js
/**
* Module dependencies.
*/
var fs = require('fs');
var ogg = require('ogg');
var lame = require('lame');
var vorbis = require('vorbis');
@TooTallNate
TooTallNate / objc.js
Created Jul 13, 2011
Experimentations with node-ffi
View objc.js
var ffi = require('node-ffi')
, objc = new ffi.Library(null, {
'objc_getClass': [ 'pointer', [ 'string' ] ]
, 'class_getName': [ 'string', [ 'pointer' ] ]
, 'sel_registerName': [ 'pointer', [ 'string' ] ]
, 'sel_getName': [ 'string', [ 'pointer' ] ]
, 'objc_msgSend': [ 'pointer', [ 'pointer', 'pointer' ] ]
})
// The problem with libffi and C functions that accepts varargs is that we
// have to create new wrappers for each different argument and return value
@TooTallNate
TooTallNate / .gitignore
Created Jul 9, 2011
low-level objc runtime apis
View .gitignore
*
!*.m
!Makefile