Learning a node stream

2,523 views

Published on

2011-01-28
Osaka Node gakuen 1 slide / 大阪Node学園一時限目 発表スライド

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,523
On SlideShare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Learning a node stream

  1. 1. Learning a Node stream 2012-01-28 kumatch / Yosuke Kumakura
  2. 2. • Yosuke Kumakura (kumatch)• @kumatch• Feedtailor inc.• Video game fun
  3. 3. Agenda• Summary• Usage• Case study• Stream classes
  4. 4. Stream summary• An abstract interface Nodes I/O• Controls data stream• Readable / Writable stream
  5. 5. Case studyHow to copy files on Node ?
  6. 6. var fs = require(fs);var input = ./oriainal.jpg;var output = ./copy.jpg;var data = fs.readFileSync(input);fs.writeFileSync(output, data);console.log(copied.);
  7. 7. var fs = require(fs);var input = ./original.jpg;var output = ./copy.jpg;fs.readFile(input, function (err, data) { if (err) throw err; fs.writeFile(output, data, function (err) { if (err) throw err; console.log(copied.); });});
  8. 8. var fs = require(fs);var readStream = fs.createReadStream(./original.jpg);var writeStream = fs.createWriteStream(./copy.jpg);readStream.resume();readStream.on(data, function (buffer) { writeStream.write(buffer);});readStream.on(end, function () { writeStream.end();});writeStream.on(close, function () { console.log(copied);});
  9. 9. var fs = require(fs);var readStream = fs.createReadStream(./original.jpg);var writeStream = fs.createWriteStream(./copy.jpg);readStream.pipe(writeStream);writeStream.on(close, function () { console.log(copied);});
  10. 10. Stream usage• Stream is EventEmmiter• and has some methods.
  11. 11. Stream usage / Readable stream• Methods • resume • pause • destroy
  12. 12. Stream usage / Readable stream• Events • data • end • close • error
  13. 13. Stream usage / Writable stream• Methods • write • end • destroy
  14. 14. Stream usage / Writable stream• Events • drain • close • error
  15. 15. var fs = require(fs);var readStream = fs.createReadStream(./original.jpg);var writeStream = fs.createWriteStream(./copy.jpg);readStream.resume();readStream.on(data, function (buffer) { writeStream.write(buffer);});readStream.on(end, function () { writeStream.end();});writeStream.on(close, function () { console.log(copied);});
  16. 16. Stream usage / Stream pipe() pipeSource stream Destination stream (readable) (writable)
  17. 17. Stream usage / Stream pipe()• destination.write() if source on ‘data’.• source.pause() if destination buffer is full.• source.resume() if destination on ‘drain’.
  18. 18. Stream usage / Stream pipe()• [optional] Keeps the destination stream open. • Do not destination.end().
  19. 19. var fs = require(fs);var readStream = fs.createReadStream(./original.jpg);var writeStream = fs.createWriteStream(./copy.jpg);readStream.pipe(writeStream);writeStream.on(close, function () { console.log(copied);});
  20. 20. Case study 2Digest SHA1 hash of a file
  21. 21. Stream classes• Filesystem (fs) • readStream (Readable) • writeStream (Writable)
  22. 22. Stream classes• Net • net.Socket (Readable/Writable)
  23. 23. Stream classes• HTTP • http.ServerRequest (Readable) • http.ServerResponse (Writable)
  24. 24. Stream classes• Zlib • all classes (Readable/Writable) • Gzip/Gunzip • Deflate/Inflate • DeflateRaw/InflateRaw
  25. 25. Stream classesfs.Readable pipe zlib.Gzip pipe http.response
  26. 26. References• Node manual & documentation• by Jxck • http://d.hatena.ne.jp/Jxck/20111204• A future in stream / Streams2 (Github issue) • https://github.com/joyent/node/pull/1681

×