Skip to content

Instantly share code, notes, and snippets.

View luciferous's full-sized avatar
🏠
Working from home

Neuman Vong luciferous

🏠
Working from home
View GitHub Profile
@luciferous
luciferous / README.md
Created May 27, 2011 03:55
Rate Limiter in Javascript

How to use

Let's say you want to limit some event to 5 occurrences every 20 seconds, you would set up the RateLimit like so:

var limit = new RateLimit(20); // Initialize a 20 second ticklog

// For events that we initiate...
if (limit.count('myevent_id') <= 5)) {

// Do some stuff

@luciferous
luciferous / 0.README.md
Created June 1, 2011 22:13
Secure WebSockets in Safari 5.0

Safari's implementation of secure WebSockets (wss) starts off with an SSLv2 handshake[1]. Normal browsing over HTTPs uses TLS[2], so why do WebSocket connections use SSLv2? SSLv2 is known to be insecure[3] and some frameworks even explicitly disable its use[4].

WebSocket connections don't always use SSLv2, however. Opening a secure WebSocket connection to a server immediately after browsing to it over HTTPs results in the WebSocket connection using TLS[2].

Furthermore, Safari's WebSocket connections fail when connecting to a test SSL server that is configured to accept SSLv2[[5]][5]. Is Safari's implementation of SSLv2 even correct?

Make your browser beep

Step 1. Include beep.js

<script type="text/javascript" src="beep.js"></script>

Step 2. Beep!

@luciferous
luciferous / echo.hs
Created August 7, 2011 02:04
Looping Echo
import Network (listenOn, PortID(..))
import Network.Socket
import Control.Exception
import Control.Concurrent
import Control.Monad (forever)
import System.IO
type HandlerFn = String -> Socket -> IO ()
loopEcho :: HandlerFn
@luciferous
luciferous / node-jsmockito.sh
Created September 19, 2011 21:45
Use JsMockito with NodeJS (patched for CommonJS Module 1.1 compliance)
#!/bin/sh
# Begin with an empty file.
echo > mockito.js
# Download and concatenate jshamcrest with jsmockito.
curl --silent --compressed \
http://jshamcrest.destaquenet.com/jshamcrest.js \
http://cloud.github.com/downloads/chrisleishman/jsmockito/jsmockito-1.0.4.js \
> mockito.js
@luciferous
luciferous / lib.js
Created October 6, 2011 17:24
Testing dynamic JS load
Foo = {};
Foo.hi = function() { alert("hi") };
@luciferous
luciferous / broadcast.html
Created November 6, 2011 11:13
WebSocket Radio!
<!doctype html>
<html><head>
<script type="text/javascript" src="Microphone/swfobject.js"></script>
<script type="text/javascript" src="ulaw.js"></script>
<script type="text/javascript" src="Microphone/microphone.js"></script>
<script type="text/javascript" src="sockjs-0.1.min.js"></script>
<script type="text/javascript">
Microphone.debug = true;
bsock = new SockJS("/broadcast");
Microphone.initialize({ swfLocation: "Microphone/MicrophoneMain.swf" });
@luciferous
luciferous / cps.md
Created January 3, 2012 22:38
Continuation passing style interface

Continuation passing style interface

Instead of attaching listeners to events, viz.:

    foo = new Foo()
    foo.on "loaded", ->
      bar = new Bar()
 bar.on "loaded", -&gt;
@luciferous
luciferous / readme.md
Created January 10, 2012 01:10
Retryable continuations

This is an experiment in retrying code that can sometimes throw errors. Consider a function login which returns true if the password argument is correct.

    login = (password) -> password == "456"

Then another function tryPasswords which attempts to login with a list of passwords.

retryable = (fn) -> (args..., callbacks) ->
cont =
attempts: 0
return: -> callbacks.return?.apply cont, arguments
error: -> callbacks.error?.apply cont, arguments
retry: ->
cont.attempts++
fn.apply cont, args
fn.apply cont, args