NodeJS Streams

  • Current implementation is known as streams2.
  • Introduced in node v0.10.
  • "suck" streams instead of "spew" streams.
  • Instead of data events spewing, call read() to pull data from source.
  • When there isn't any data to consume, then read() will return undefined.
  • Adding a data event listener will switch the Readable stream into "old mode", where data is emitted as soon as it is available rather than waiting for you to call read() to consume it. This requires you to handle backpressure problems manually.
  • The pipe method helps write less code and handles back-pressure.
  • If you add an end listener and don't ever read() or pipe(), it'll never emit end.

Raspbian Strech Lite Quick setup

In order to enable SSH and wifi on first boot we need to add two files to the root folder on the SD card before inserting it into the raspberry.

First, create an empty file named ssh. Then, create a second file called wpa_supplicant.conf. The contents of this file should be

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
View passphrase.js
// npm install pbkdf2
const pb = require("pbkdf2");
const ssid = "ssid";
const password = "plain-text-password";
printHash = (err, res) => console.log(res.toString("hex"));
pb.pbkdf2(password, ssid, 4096, 32, "sha1", printHash);
# The main issues with Ruby 1.8.7 are that it won't build with Clang,
# and the Net::HTTPS module won't work with modern versions of OpenSSL.
# This assumes you have already set up homebrew, and run: xcode-select --install
#brew install homebrew/dupes/apple-gcc42
#brew install libyaml libffi
brew install
export CC=/usr/local/bin/gcc-4.2
View a-seeds.exs
# In this example, Video has_many authors.
# Here is the code for creating a video with authors
# using a single call to Repo.insert[
%{"title" => "Elixir", "duration" => 123, "authors" => [
%{"name" => "José Valim"}
%{"title" => "JavaScript", "duration" => 666, "authors" => [
%{"name" => "Brendan Eich"}
View todo.jsx
var React = require("react");
var allItems = []
allItems.push("Buy ingredients for Crock Pot");
allItems.push("Pick up chair at IKEA");
allItems.push("Go see mom");
class TodoList extends React.Component {

Tested with Chrome, Firefox and Safari.

The following code will not trigger an alert. target.innerHTML = "<script> alert('XSS Attack'); </script>";

The following code will trigger an alert. target.innerHTML = "<img src=x onerror=\"alert('XSS Attack')\" >";

View application_controller.rb
class ApplicationController < ActionController::Base
def handle_unverified_request
raise ActionController::InvalidAuthenticityToken

#Node.js and the Event Loop

Node.js is a framework for writing server-side JavaScript applications. It is built on top of the V8 JavaScript runtime and uses an event-driven, non-blocking I/O model that makes it perfectly suited for data-intensive real-time applications.

This blog post will describe what non-blocking I/O means and how working with the event loop can help your applications be more efficient.

##The Restaurant

"This [non-blocking I/O] model simplifies access to slow resources in a scalable way that is intuitive to JavaScript programmers and easy to learn for everyone else." - Node Up and Running.

View import_from_wp.rake
desc 'import from WordPress XML file'
task :import_from_wp => :environment do
require 'hpricot'
doc = Hpricot::XML('wp-export-file.xml'))
posts = (doc/:channel/:item)
posts.each do |post|
p "Post: #{'link').inner_text}"