functional programming with streams in node.js

37
Functional Programming with Streams in node.js

Upload: adam-crabtree

Post on 17-May-2015

18.398 views

Category:

Technology


3 download

DESCRIPTION

My talk at HTML5DevConf && FluentConf covering how I combined some basic functional programming techniques with node.js streams to abstract a streaming VFS zipping layer that can also (theoretically / eventually) run in the browser.

TRANSCRIPT

Page 1: Functional Programming with Streams in node.js

Functional Programming with Streams in node.js

Page 2: Functional Programming with Streams in node.js

Who Am I?

  HP webOS Framework Team

  @CrabDude (Twitter, Github, etc…)

  Organizer BayNode & Dallas.node

  Author:

  trycatch – async try/catch in node.js

  stepup – step node.js control flow + trycatch

  tiki – in-browser package manager

Page 3: Functional Programming with Streams in node.js

The Problem VFS

hermesClient

hermesBuild 3rd Party API

hermesDeploy

Ares (browser)

VFS VFS

hermesClient hermesClient

hermes

Page 4: Functional Programming with Streams in node.js

The Goal

  Don’t buffer any data

  Don’t write to disk

  Minimize latency

  Allow reuse of functionality (e.g., readdir)

  Functionality may be local or remote (e.g., getFolder)

Page 5: Functional Programming with Streams in node.js

Why node.js?

“Node's goal is to provide an easy way to build scalable network programs.” – nodejs.org

Page 6: Functional Programming with Streams in node.js

node.js === JavaScript

JavaScript is the future of the web, and code reuse is its unfair advantage.

Page 7: Functional Programming with Streams in node.js
Page 8: Functional Programming with Streams in node.js

Basics

  EventEmitter

  Stream

  Traditional streams (aka non-node.js lazy streams)

Page 9: Functional Programming with Streams in node.js

EventEmitter: Example

Page 10: Functional Programming with Streams in node.js

EventEmitter: Extend

Page 11: Functional Programming with Streams in node.js

Streams

“Streams are to time as arrays are to space.” – Jed Schmidt @ JSConf.eu/2010

Page 12: Functional Programming with Streams in node.js

Readable Streams

  Events: data, end, error, close

  Methods: pause, resume, end, destroy

Page 13: Functional Programming with Streams in node.js

Writable Streams

  Events: drain, error, close, pause, resume

  Methods: write, end, destroy

Page 14: Functional Programming with Streams in node.js

readable.pipe(writeable)

Readable streams can be piped to writable streams (and vice-versa!)

  on(‘data’) => write()

  on(‘end’) => end()

  on(‘drain’) => resume()

  on(‘close’) => destroy()

  on(‘error’) => on(‘error’)

  on(‘pause’) => pause()

Page 15: Functional Programming with Streams in node.js

readable.pipe(writeable): Example

Page 16: Functional Programming with Streams in node.js

Lazy Streams

Streams in functional programming…

Page 17: Functional Programming with Streams in node.js

Lazy Streams: Higher Order Functions

A higher-order function is a function that does at least one of the following:

  Take one or more functions as input   output a function

Page 18: Functional Programming with Streams in node.js

Lazy Streams: Lists

Page 19: Functional Programming with Streams in node.js

Got All That?

Page 20: Functional Programming with Streams in node.js

Hermes: Revisited FTP Dropbox Box.net Filesystem

hermesFtp hermesDropbox hermesBox hermesFilesystem

hermesBuild 3rd Party API

hermesDeploy

Ares (browser)

Page 21: Functional Programming with Streams in node.js

Still Got All That?

Page 22: Functional Programming with Streams in node.js

We need…

We need to recursively zip a directory…

  Let’s start with abstract implementation non-specific functional versions of…

  Filter paths from readdir

  Filter directories from paths

  Filter files from paths

  Recursively return all file paths in a directory

Page 23: Functional Programming with Streams in node.js
Page 24: Functional Programming with Streams in node.js

We need…

  More “abstract implementation non-specific functional versions” (errr…) of…

  ls (aka readdir)

  getFile (aka readFile)

  getFolder (stream of gets)

Page 25: Functional Programming with Streams in node.js
Page 26: Functional Programming with Streams in node.js

We need…

  Yet More AINSFV of...

  Zip a stream of files

  Allow node.js streams to be piped to lazy streams

  pipe(): lazy stream => node.js stream

  pump(): node.js stream => lazy stream

Page 27: Functional Programming with Streams in node.js
Page 28: Functional Programming with Streams in node.js

Almost there!

Page 29: Functional Programming with Streams in node.js

Now we can…

Zip a folder up using an arbitrary…

  getFile / readFile

  ls / readdir

… And stream the response! Yeeeeehaw!

Page 30: Functional Programming with Streams in node.js
Page 31: Functional Programming with Streams in node.js

Sweeeeeet.

Page 32: Functional Programming with Streams in node.js

Now What?

Since all the implementation specific functions are passed in, we can…

Page 33: Functional Programming with Streams in node.js

Port this to the browser!

Page 34: Functional Programming with Streams in node.js

Say whaaat?

node-browserify by @Substack does a lot of this already

  Stream, Buffer, Path, etc…

We need…

  Complete buffer implementation (browser has TypedArrays & ArrayBuffer)

  fs (browser has FileSystem API)

  zlib…

  inflate(), deflate(), gzip(), gunzip(), unzip()

Page 35: Functional Programming with Streams in node.js

Stay Tuned.

I’m currently porting the fs and buffer modules to the browser, and eventually zlib.

http://github.com/crabdude/fs-browserify http://github.com/crabdude/buffer-browserify http://github.com/crabdude/zlib-browserify

Page 36: Functional Programming with Streams in node.js

We’re HIRING! webOS Framework Team

If you think this is pretty cool… Or you think I’M pretty cool…

Stop by the Enyo booth or talk to me afterwards.

Page 37: Functional Programming with Streams in node.js

Thank you.

[email protected] https://github.com/Gozala/streamer/blob/master/readme.js http://en.wikipedia.org/wiki/Higher-order_function https://github.com/mikeal/request http://felixge.s3.amazonaws.com/11/nodejs-streams.pdf https://github.com/substack/node-browserify

Functional Programming in JavaScript: http://igstan.ro/posts/2011-05-02-understanding-monads-with-

javascript.html http://ndc2011.macsimum.no/mp4/Day2%20Thursday/

Track6%201500-1600.mp4 http://drboolean.tumblr.com/ http://osteele.com/sources/javascript/functional/