Skip to content

Instantly share code, notes, and snippets.

View TooTallNate's full-sized avatar

Nathan Rajlich TooTallNate

View GitHub Profile
@TooTallNate
TooTallNate / websocket-over-tor.js
Created August 16, 2013 21:15
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
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 / objc.js
Created July 13, 2011 05:07
Experimentations with node-ffi
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 / test.js
Created October 1, 2012 02:47
This gist has been moved to `ref-wchar` module on npm, see: https://github.com/TooTallNate/ref-wchar
var ffi = require('ffi')
var ref = require('ref')
var assert = require('assert')
var wchar_t = require('./wchar_t')
// now we can create our FFI'd "wcslen()" function
var libc = ffi.Library('libc', {
wcslen: [ 'size_t', [ wchar_t ] ],
wcstod: [ 'double', [ wchar_t, ref.refType(wchar_t) ] ]
})
@TooTallNate
TooTallNate / starwars.js
Created April 4, 2012 06:59
NodeJS script to play ACSII Star Wars (using a hidden cursor)
/**
* A little script to play the ACSII Star Wars, but with a hidden
* cursor, since over telnet the cursor remains visible
*/
var net = require('net')
var cursor = require('ansi')(process.stdout)
// connect to Star Wars server
@TooTallNate
TooTallNate / convert_to_hevc.sh
Last active March 24, 2020 03:32
Transcodes input video file to use the h265/HEVC codec
#!/bin/bash
# Transcodes input video file to use the h265/HEVC codec.
# Outputs the same filename but with x264/h264/xvid/etc. replaced with HEVC.
sed=sed
if type gsed >/dev/null 2>&1; then
sed=gsed
fi
get_streams() {
@TooTallNate
TooTallNate / README.md
Created January 10, 2012 19:31
'cflags' parameter is broken in gyp on OS X?

This is a barebones GYP file to compile 1 C source file. We are demonstrating that the cflags parameter in gyp files gets completely ignored :(

$ ../gyp/gyp -f make --depth=. hello.gyp
$ V=1 make
  cc   -fasm-blocks -mpascal-strings -Os -gdwarf-2  -MMD -MF out/Default/.deps/out/Default/obj.target/hello/hello.o.d.raw  -c -o out/Default/obj.target/hello/hello.o hello.c
  ./gyp-mac-tool flock out/Default/linker.lock g++ -Lout/Default   -o "out/Default/hello" out/Default/obj.target/hello/hello.o 
  LINK(target) out/Default/hello: Finished
@TooTallNate
TooTallNate / on-macos.txt
Created February 11, 2020 19:43
Does anyone know why this script takes >2s to receive the `error` event on Windows? On MacOS it's essentially instant…
$ time node slow-windows-error.js
Error: connect ECONNREFUSED 127.0.0.1:4
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:16) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 4
}
@TooTallNate
TooTallNate / why.md
Created May 26, 2012 18:28
Why is Node async?

Why is Node async?

  • Asynchronous code is how you write low-resource, high-concurrency servers. See http://www.kegel.com/c10k.html.
  • Node embracing async from the get-go means that servers are low-resource, high-concurrency by default.
  • Async + JavaScript = Perfect fit for an event loop.
  • When it comes to threads vs event-loop, there are times when either are advantageous.
    • But there are things very hard or impossible to do with threads.
    • WebSockets are difficult to do properly with threads. That's one example where non-blocking IO (async) has a major advantage.
    • RAM usage is another factor, especially when we're talking about the physical hardware required to run your application, which translates to real dollars.
  • It depends on your application in the end:
@TooTallNate
TooTallNate / repl-client.js
Created March 13, 2012 01:48
Full featured NodeJS repl server over a TCP socket (for node 0.6.x, uses some wacky server hacks)
var tty = require('tty')
var net = require('net')
var sock = net.createConnection(1337)
sock.on('connect', function () {
process.stdin.resume();
tty.setRawMode(true)
})
@TooTallNate
TooTallNate / mp3player.js
Created October 24, 2012 17:42
node.js command line MP3 player in 9 lines of code!
var fs = require('fs');
var lame = require('lame');
var Speaker = require('speaker');
fs.createReadStream(process.argv[2])
.pipe(new lame.Decoder())
.on('format', function (format) {
this.pipe(new Speaker(format));
});