SlideShare a Scribd company logo
1 of 37
Download to read offline
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
The Problem
     VFS         VFS           VFS

hermesClient hermesClient hermesClient

              hermesBuild        3rd Party API


    hermes      hermesDeploy



   Ares
  (browser)
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)
Why node.js?

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

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

  EventEmitter

  Stream

  Traditional streams (aka non-node.js lazy streams)
EventEmitter: Example
EventEmitter: Extend
Streams

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

  Events: data, end, error, close

  Methods: pause, resume, end, destroy
Writable Streams

  Events: drain, error, close, pause, resume

  Methods: write, end, destroy
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()
readable.pipe(writeable): Example
Lazy Streams

Streams in functional programming…
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
Lazy Streams: Lists
Got All That?
Hermes: Revisited
     FTP        Dropbox        Box.net   Filesystem

hermesFtp hermesDropbox hermesBox hermesFilesystem

               hermesBuild                 3rd Party API

                          hermesDeploy

     Ares
   (browser)
Still Got All That?
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
We need…

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

    ls (aka readdir)

    getFile (aka readFile)

    getFolder (stream of gets)
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
Almost there!
Now we can…

Zip a folder up using an arbitrary…

  getFile / readFile

  ls / readdir



… And stream the response! Yeeeeehaw!
Sweeeeeet.
Now What?

Since all the implementation specific functions
  are passed in, we can…
Port this to the browser!
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()
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
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.
Thank you.

        adam.crabtree@palm.com
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/

More Related Content

What's hot

Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for Rubists
Sagiv Ofek
 
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Fwdays
 
Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18
Pierre Joye
 
HTTP::Parser::XS - writing a fast & secure XS module
HTTP::Parser::XS - writing a fast & secure XS moduleHTTP::Parser::XS - writing a fast & secure XS module
HTTP::Parser::XS - writing a fast & secure XS module
Kazuho Oku
 
Nginx وب سروری برای تمام فصول
Nginx وب سروری برای تمام فصولNginx وب سروری برای تمام فصول
Nginx وب سروری برای تمام فصول
efazati
 

What's hot (20)

Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for Rubists
 
Applied Shell Scripting - stills to time-lapse
Applied Shell Scripting - stills to time-lapseApplied Shell Scripting - stills to time-lapse
Applied Shell Scripting - stills to time-lapse
 
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
Алексей Петров "PHP at Scale: Knowing enough to be dangerous!"
 
Александр Махомет "Beyond the code или как мониторить ваш PHP сайт"
Александр Махомет "Beyond the code или как мониторить ваш PHP сайт"Александр Махомет "Beyond the code или как мониторить ваш PHP сайт"
Александр Махомет "Beyond the code или как мониторить ваш PHP сайт"
 
Server side scripting smack down - Node.js vs PHP
Server side scripting smack down - Node.js vs PHPServer side scripting smack down - Node.js vs PHP
Server side scripting smack down - Node.js vs PHP
 
Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18Webdevcon Keynote hh-2012-09-18
Webdevcon Keynote hh-2012-09-18
 
Basic testing with selenium
Basic testing with seleniumBasic testing with selenium
Basic testing with selenium
 
HTTP::Parser::XS - writing a fast & secure XS module
HTTP::Parser::XS - writing a fast & secure XS moduleHTTP::Parser::XS - writing a fast & secure XS module
HTTP::Parser::XS - writing a fast & secure XS module
 
Chenli linux-kerne-community
Chenli linux-kerne-communityChenli linux-kerne-community
Chenli linux-kerne-community
 
A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...
 
Searching CPAN Offline
Searching CPAN OfflineSearching CPAN Offline
Searching CPAN Offline
 
Nginx وب سروری برای تمام فصول
Nginx وب سروری برای تمام فصولNginx وب سروری برای تمام فصول
Nginx وب سروری برای تمام فصول
 
A slightly advanced introduction to node.js
A slightly advanced introduction to node.jsA slightly advanced introduction to node.js
A slightly advanced introduction to node.js
 
Web Development with Python and Django
Web Development with Python and DjangoWeb Development with Python and Django
Web Development with Python and Django
 
Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.
 
Developer-friendly taskqueues: What you should ask yourself before choosing one
Developer-friendly taskqueues: What you should ask yourself before choosing oneDeveloper-friendly taskqueues: What you should ask yourself before choosing one
Developer-friendly taskqueues: What you should ask yourself before choosing one
 
5 things MySql
5 things MySql5 things MySql
5 things MySql
 
Jruby a Pi and a database
Jruby a Pi and a databaseJruby a Pi and a database
Jruby a Pi and a database
 
A rough guide to JavaScript Performance
A rough guide to JavaScript PerformanceA rough guide to JavaScript Performance
A rough guide to JavaScript Performance
 
OSDC 2016 - DNS for Developers by Jan-Piet Mens
OSDC 2016 - DNS for Developers by Jan-Piet MensOSDC 2016 - DNS for Developers by Jan-Piet Mens
OSDC 2016 - DNS for Developers by Jan-Piet Mens
 

Viewers also liked

优丽奇中国 公司手册
优丽奇中国 公司手册优丽奇中国 公司手册
优丽奇中国 公司手册
Goodrich Global
 
Blowin In The Wind
Blowin In The  WindBlowin In The  Wind
Blowin In The Wind
goznevi
 
Renewable energy and water treatment: emerging opportunities
Renewable energy and water treatment: emerging opportunitiesRenewable energy and water treatment: emerging opportunities
Renewable energy and water treatment: emerging opportunities
CambridgeIP Ltd
 
Smart camera monitoring system
Smart camera monitoring systemSmart camera monitoring system
Smart camera monitoring system
Arvind Krishnaa
 
E11 Physics Evaluation Sheet
E11 Physics Evaluation SheetE11 Physics Evaluation Sheet
E11 Physics Evaluation Sheet
guest411ccf79
 

Viewers also liked (20)

Writing native bindings to node.js in C++
Writing native bindings to node.js in C++Writing native bindings to node.js in C++
Writing native bindings to node.js in C++
 
Use of 3D Immersive Technology for the Support of Gifted Learners
Use of 3D Immersive Technology for the Support of Gifted LearnersUse of 3D Immersive Technology for the Support of Gifted Learners
Use of 3D Immersive Technology for the Support of Gifted Learners
 
♥♥♥
♥♥♥♥♥♥
♥♥♥
 
Vision2music
Vision2musicVision2music
Vision2music
 
优丽奇中国 公司手册
优丽奇中国 公司手册优丽奇中国 公司手册
优丽奇中国 公司手册
 
Goodrich Global Corporate Brochure
Goodrich Global Corporate  BrochureGoodrich Global Corporate  Brochure
Goodrich Global Corporate Brochure
 
Digiaika - Mikä Muuttuu Markkinoinnissa
Digiaika - Mikä Muuttuu MarkkinoinnissaDigiaika - Mikä Muuttuu Markkinoinnissa
Digiaika - Mikä Muuttuu Markkinoinnissa
 
Accelerating innovation and diffusion of renewable energy technologies: techn...
Accelerating innovation and diffusion of renewable energy technologies: techn...Accelerating innovation and diffusion of renewable energy technologies: techn...
Accelerating innovation and diffusion of renewable energy technologies: techn...
 
Voita peli verkossa: Parempia asiakkaita verkkopalvelun optimoinnilla
Voita peli verkossa: Parempia asiakkaita verkkopalvelun optimoinnillaVoita peli verkossa: Parempia asiakkaita verkkopalvelun optimoinnilla
Voita peli verkossa: Parempia asiakkaita verkkopalvelun optimoinnilla
 
CambridgeIP Webinar: Developing a fact Based IP Strategy
CambridgeIP Webinar: Developing a fact Based IP StrategyCambridgeIP Webinar: Developing a fact Based IP Strategy
CambridgeIP Webinar: Developing a fact Based IP Strategy
 
CambridgeIP: Case Studies Of Recent Client Engagements
CambridgeIP: Case Studies Of Recent Client EngagementsCambridgeIP: Case Studies Of Recent Client Engagements
CambridgeIP: Case Studies Of Recent Client Engagements
 
Goodrich Global 产品常见问题 (地板)
Goodrich Global 产品常见问题 (地板)Goodrich Global 产品常见问题 (地板)
Goodrich Global 产品常见问题 (地板)
 
Blowin In The Wind
Blowin In The  WindBlowin In The  Wind
Blowin In The Wind
 
Renewable energy and water treatment: emerging opportunities
Renewable energy and water treatment: emerging opportunitiesRenewable energy and water treatment: emerging opportunities
Renewable energy and water treatment: emerging opportunities
 
Smart camera monitoring system
Smart camera monitoring systemSmart camera monitoring system
Smart camera monitoring system
 
E11 Physics Evaluation Sheet
E11 Physics Evaluation SheetE11 Physics Evaluation Sheet
E11 Physics Evaluation Sheet
 
The HFA pMDI Patent Landscape: Minefield or Goldmine
The HFA pMDI Patent Landscape: Minefield or GoldmineThe HFA pMDI Patent Landscape: Minefield or Goldmine
The HFA pMDI Patent Landscape: Minefield or Goldmine
 
Burns supper (3ºb 2012 13)
Burns supper (3ºb 2012 13)Burns supper (3ºb 2012 13)
Burns supper (3ºb 2012 13)
 
TodiCastle: villa rentals & historic hotel in Umbria
TodiCastle: villa rentals & historic hotel in UmbriaTodiCastle: villa rentals & historic hotel in Umbria
TodiCastle: villa rentals & historic hotel in Umbria
 
Ota sosiaalinen media tehokäyttöön
Ota sosiaalinen media tehokäyttöönOta sosiaalinen media tehokäyttöön
Ota sosiaalinen media tehokäyttöön
 

Similar to Functional Programming with Streams in node.js

Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?
Dinh Pham
 
SD, a P2P bug tracking system
SD, a P2P bug tracking systemSD, a P2P bug tracking system
SD, a P2P bug tracking system
Jesse Vincent
 
PuppetDB: Sneaking Clojure into Operations
PuppetDB: Sneaking Clojure into OperationsPuppetDB: Sneaking Clojure into Operations
PuppetDB: Sneaking Clojure into Operations
grim_radical
 

Similar to Functional Programming with Streams in node.js (20)

Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
 
Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?
 
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...TorqueBox: The beauty of Ruby with the power of JBoss.  Presented at Devnexus...
TorqueBox: The beauty of Ruby with the power of JBoss. Presented at Devnexus...
 
TypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersTypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack Developers
 
SD, a P2P bug tracking system
SD, a P2P bug tracking systemSD, a P2P bug tracking system
SD, a P2P bug tracking system
 
Introduction to node.js GDD
Introduction to node.js GDDIntroduction to node.js GDD
Introduction to node.js GDD
 
Clustered PHP - DC PHP 2009
Clustered PHP - DC PHP 2009Clustered PHP - DC PHP 2009
Clustered PHP - DC PHP 2009
 
Node.js - async for the rest of us.
Node.js - async for the rest of us.Node.js - async for the rest of us.
Node.js - async for the rest of us.
 
RuG Guest Lecture
RuG Guest LectureRuG Guest Lecture
RuG Guest Lecture
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredev
 
PuppetDB: Sneaking Clojure into Operations
PuppetDB: Sneaking Clojure into OperationsPuppetDB: Sneaking Clojure into Operations
PuppetDB: Sneaking Clojure into Operations
 
Node js beginner
Node js beginnerNode js beginner
Node js beginner
 
Docker Tips And Tricks at the Docker Beijing Meetup
Docker Tips And Tricks at the Docker Beijing MeetupDocker Tips And Tricks at the Docker Beijing Meetup
Docker Tips And Tricks at the Docker Beijing Meetup
 
Developing IT infrastructures with Puppet
Developing IT infrastructures with PuppetDeveloping IT infrastructures with Puppet
Developing IT infrastructures with Puppet
 
Demo 0.9.4
Demo 0.9.4Demo 0.9.4
Demo 0.9.4
 
Node.js - A practical introduction (v2)
Node.js  - A practical introduction (v2)Node.js  - A practical introduction (v2)
Node.js - A practical introduction (v2)
 
Java Hates Linux. Deal With It.
Java Hates Linux.  Deal With It.Java Hates Linux.  Deal With It.
Java Hates Linux. Deal With It.
 
Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016
Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016
Erik Skytthe - Monitoring Mesos, Docker, Containers with Zabbix | ZabConf2016
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Functional Programming with Streams in node.js

  • 1. Functional Programming with Streams in node.js
  • 2. 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
  • 3. The Problem VFS VFS VFS hermesClient hermesClient hermesClient hermesBuild 3rd Party API hermes hermesDeploy Ares (browser)
  • 4. 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)
  • 5. Why node.js? “Node's goal is to provide an easy way to build scalable network programs.” – nodejs.org
  • 6. node.js === JavaScript JavaScript is the future of the web, and code reuse is its unfair advantage.
  • 7.
  • 8. Basics   EventEmitter   Stream   Traditional streams (aka non-node.js lazy streams)
  • 11. Streams “Streams are to time as arrays are to space.” – Jed Schmidt @ JSConf.eu/2010
  • 12. Readable Streams   Events: data, end, error, close   Methods: pause, resume, end, destroy
  • 13. Writable Streams   Events: drain, error, close, pause, resume   Methods: write, end, destroy
  • 14. 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()
  • 16. Lazy Streams Streams in functional programming…
  • 17. 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
  • 20. Hermes: Revisited FTP Dropbox Box.net Filesystem hermesFtp hermesDropbox hermesBox hermesFilesystem hermesBuild 3rd Party API hermesDeploy Ares (browser)
  • 21. Still Got All That?
  • 22. 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
  • 23.
  • 24. We need…   More “abstract implementation non-specific functional versions” (errr…) of…   ls (aka readdir)   getFile (aka readFile)   getFolder (stream of gets)
  • 25.
  • 26. 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
  • 27.
  • 29. Now we can… Zip a folder up using an arbitrary…   getFile / readFile   ls / readdir … And stream the response! Yeeeeehaw!
  • 30.
  • 32. Now What? Since all the implementation specific functions are passed in, we can…
  • 33. Port this to the browser!
  • 34. 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()
  • 35. 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
  • 36. 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.
  • 37. Thank you. adam.crabtree@palm.com 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/