Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Localnode

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

 Localnode

An explanation of the technology behind my team's node.js knockout 2011 entry, Localnode.

David Gouldin

January 20, 2012

Transcript

23 slides
  1. “If the only tool you have is a browser, treat

    everything as if it were a web page.” - Nobody ever (but play along)
  2. design choices • No registration, tunneler reserves subdomain for as

    long as (s)he’s connected • Iframe source downloadable as a static file to be hosted by local web server • Tunnel is created as soon as static file is detected at URL tunneler specifies
  3. concurrent requests • >1 request is handled in parallel •

    node.js needs to keep them straight and respond to the correct request • We created a unique token identifier kept through entire request/response cycle
  4. cookies / headers • Headers are sent along with the

    url • Here’s what a request to the web tunnel looks like: { token: ‘some-unique-token’, url: ‘/foo/bar/’, method: ‘GET’, headers: ‘User-Agent Mozilla/5.0...’, {
  5. binary data • JavaScript doesn’t support binary strings • XMLHttpRequest2

    includes responseType “arraybuffer” https://developer.mozilla.org/en/ JavaScript_typed_arrays/ArrayBuffer
  6. binary data • Create a typed array from XHR2’s array

    buffer • Base64 encode the typed array of bytes • You’re now serializing binary data in JS! var xhr = new XMLHttpRequest(); xrh.responseType = ‘arraybuffer’; xhr.onload = function() { var bytes = new Uint8Array(this.response); // you’re off to the races! }
  7. binary data In node, of course, deserializing base64 and sending

    it down as a response is easy: var buffer = new Buffer(content, ‘base64’); res.end(buffer);
  8. 301 / 302 redirects • XHR2 does NOT pass “location”

    header • Instead, the redirect happens silently in the guts of the browser • Result is outdated url on the client THIS SUCKS. Please provide a fix! Dear W3C,
  9. file upload • Actually, this one is possible, we just

    didn’t have time to accomplish it. • XHR2.send accepts an instance of FormData for multi-part form posts. • Node.js would need to base64 encode for sending to the iframe via postMessage https://developer.mozilla.org/en/ XMLHttpRequest/FormData
  10. absolute urls • Didn’t your momma teach you not to

    use absolute urls to your own domain? • Could be solved with a regex, but I’d rather break your site.

More Decks by David Gouldin

Other Decks in Programming