+ All Categories
Home > Technology > Learning a node stream

Learning a node stream

Date post: 12-May-2015
Category:
Upload: kumatch-kumatch
View: 2,082 times
Download: 8 times
Share this document with a friend
Description:
2011-01-28 Osaka Node gakuen 1 slide / 大阪Node学園一時限目 発表スライド
Popular Tags:
26
Learning a Node stream 2012-01-28 kumatch / Yosuke Kumakura
Transcript
Page 1: Learning a node stream

Learning a Node stream

2012-01-28kumatch / Yosuke Kumakura

Page 2: Learning a node stream

• Yosuke Kumakura (kumatch)

• @kumatch

• Feedtailor inc.

• Video game fun

Page 3: Learning a node stream

Agenda

• Summary

• Usage

• Case study

• Stream classes

Page 4: Learning a node stream

Stream summary

• An abstract interface Node's I/O

• Controls data stream

• Readable / Writable stream

Page 5: Learning a node stream

Case study

How to copy files on Node ?

Page 6: Learning a node stream

var fs = require('fs');

var input = './oriainal.jpg';

var output = './copy.jpg';

var data = fs.readFileSync(input);

fs.writeFileSync(output, data);

console.log('copied.');

Page 7: Learning a node stream

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.'); });});

Page 8: Learning a node stream

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');});

Page 9: Learning a node stream

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');});

Page 10: Learning a node stream

Stream usage

• Stream is EventEmmiter

• and has some methods.

Page 11: Learning a node stream

Stream usage / Readable stream

• Methods

• resume

• pause

• destroy

Page 12: Learning a node stream

Stream usage / Readable stream

• Events

• data

• end

• close

• error

Page 13: Learning a node stream

Stream usage /Writable stream

• Methods

• write

• end

• destroy

Page 14: Learning a node stream

Stream usage /Writable stream

• Events

• drain

• close

• error

Page 15: Learning a node stream

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');});

Page 16: Learning a node stream

Stream usage /Stream pipe()

Source stream(readable)

pipeDestination stream

(writable)

Page 17: Learning a node stream

Stream usage /Stream pipe()

• destination.write() if source on ‘data’.

• source.pause() if destination buffer is full.

• source.resume() if destination on ‘drain’.

Page 18: Learning a node stream

Stream usage /Stream pipe()

• [optional]Keeps the destination stream open.

• Do not destination.end().

Page 19: Learning a node stream

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');});

Page 20: Learning a node stream

Case study 2

Digest SHA1 hash of a file

Page 21: Learning a node stream

Stream classes

• Filesystem (fs)

• readStream (Readable)

• writeStream (Writable)

Page 22: Learning a node stream

Stream classes

• Net

• net.Socket (Readable/Writable)

Page 23: Learning a node stream

Stream classes

• HTTP

• http.ServerRequest (Readable)

• http.ServerResponse (Writable)

Page 24: Learning a node stream

Stream classes

• Zlib

• all classes (Readable/Writable)

• Gzip/Gunzip

• Deflate/Inflate

• DeflateRaw/InflateRaw

Page 25: Learning a node stream

Stream classes

fs.Readable

zlib.Gzip

http.response

pipe

pipe

Page 26: Learning a node stream

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


Recommended