Nodejs - streaming read and write unavailable

I start the node server and have the following code:

var readable = fs.createReadStream(__dirname + '/greet.txt',
{encoding: 'utf8', highWaterMark: 332 * 1024});

greet.txt:

hello

I am having trouble understanding the read stream and the write stream; In my code above, I have a readable stream that is read from greet.txt - the pieces go into the buffer and I see binary data ... the problem is there shouldn't be a writeable stream that sends data to my buffer on the other hand ? how binary data starts flying suddenly into my buffer, It’s just not clear.

Here is a combination of readable and writable:

var readable = fs.createReadStream(__dirname + '/greet.txt',
{encoding: 'utf8', highWaterMark: 332 * 1024});

var writeable = fs.createWriteStream(__dirname + '/greetcopy.txt');

readable.on('data', function(chunk){
writeable.write(chunk);
});

, ? greetcopy.txt( ), ?

node , . , , ...

+4
2

Node.js . , , .

5 : Readable, Writable, Duplex, Transform PassThrough.

, :

  • , .push(). , (null).
  • , "".
  • , "" , "read()" , null.
  • , , () ' , , push() false. , , . "HighWaterMark" ( ) .
  • _read(), , . . push, . , , read(), , _read(), .

Writable

  • , .write(). , .end().
  • .end(), . process.nextTick(), ! .
  • . (highWaterMark), false, .write(). , . , , - "", , .
  • _write() - . false, Writable , _write() , "".

Readable Writable Streams

  • ONE . , streamA.pipe(streamB).pipe(streamC) "... .. , streamA. streamC. streamB ( ) - , Transform.
  • 1: . .
  • 2: , , . . _write().

, , - . ? : ,

Duplex

  • . ( ), . , Writable.
  • 1: "streamA.pipe(duplexB).pipe(streamC)" , Readable streamA _read() duplexB _write(). streamC. , , duplexB_read(), streamC. , , streamA streamC.
  • 2: .push(null) .end() . , "" "". . end() .push(null)?

. , , , , , "link-stream", _read _write. streamA streamC , "finish" "end", . .

Transform

  • -
  • write() _write , _read()
  • this.push(...) _read , _transform()
  • _transform(). _transform. , , , , , _transform()
  • _transform, , .

  • Transform, _transform.

, . , Joyent Duplex , , PassThrough, link-stream, .

!

+22

, , . - Unix, . Stream EventEmitter, , Writable, Readable, Transform ..

, Writable Stream,

Writable stream , .

Readable Stream, :

Readable stream - , . , Readable stream.

, , Readable stream . , , , .

,

?

, , , .write(chunk);

http://www.sitepoint.com/basics-node-js-streams/

http://maxogden.com/node-streams.html

0

Source: https://habr.com/ru/post/1629016/


All Articles