作为一个前端猿,一直对后端天天能够任性的操做服务器文件感到羡慕。最近学习node的fs模块玩的也是意犹未尽好不知足。fs.readFile方法在读取大文件时没法胜任,因此流出现了。前端
个人理解 咱们在网上下载资源时,稍微大一点就要下载个把小时。咱们的带宽远远不能知足从服务端把资源整个端走,因此要把资源拆分红小块,一块一块的运输。资源就像水同样流到了咱们本地。node
While it is important for all Node.js users to understand how streams work.(官网说流很重要!)后端
nodejs有四种基本的流类型:缓存
可读流的功能是做为上游,提供数据给下游。bash
let {Readable} = require('stream');
let readable = Readable();
/*
Readable({read(){})
*/
let source = ['a','b','c'];
readable.setEncoding('utf8');
readable._read = function () {
let data = source.shift()||null;
console.log('read:',data);
this.push(data);
}
readable.on('end', function () {
console.log('end')
})
readable.on('data', function (data) {
console.log(data)
})
/*
输出:
read: a
read: b
a
read: c
b
read: null
c
end
*/
复制代码
Readable经过实例_read或read方法,在须要数据时,_read()方法会自动调用。服务器
data事件:当读入数据时触发data事件传入回调读到内容。
end事件:“消耗完”,须要知足两个条件:异步
flowing 模式下, 可读流自动从系统底层读取数据,并经过 EventEmitter 接口的事件尽快将数据提供给应用。
如下条件都可以使readable进入flowing模式:函数
paused 模式下,必须显式调用 stream.read() 方法来从流中读取数据片断。
可读流能够经过下面途径切换到 paused 模式:学习
深刻理解readable
众所周知node i/o操做一般采用异步操做。在读取数据时会把数据写入缓存区。ui
在建立流时,会设置一个highWaterMark参数建立最高水位线。
var stream = require('stream');
var util = require('util');
util.inherits(Counter, stream.Readable);
function Counter(options) {
stream.Readable.call(this, options);
this._index = 0;
}
Counter.prototype._read = function() {
if(this._index++<3){
this.push(this._index+'');
}else{
this.push(null);
}
};
var counter = new Counter();
counter.on('data', function(data){
console.log("读到数据: " + data.toString());//no maybe
});
counter.on('end', function(data){
console.log("读完了");
});
复制代码
可写流是对数据写入'目的地'的一种抽象。可写流的功能是做为下游,消耗上游提供的数据。
let {Writable} = require('stream');
var writable = Writable({
write: function (data,_,next) {
console.log(data);
next&&next();
}
})
/*
Writable._write(data,_,next)
*/
writable.write('a');
writable.write('b');
writable.write('c');
writable.end();
/*
<Buffer 61>
<Buffer 62>
<Buffer 63>
*/
复制代码
var stream = require('stream');
var util = require('util');
util.inherits(Writer, stream.Writable);
let stock = [];
function Writer(opt) {
stream.Writable.call(this, opt);
}
Writer.prototype._write = function(chunk, encoding, callback) {
setTimeout(()=>{
stock.push(chunk.toString('utf8'));
console.log("增长: " + chunk);
callback();
},500)
};
var w = new Writer();
for (var i=1; i<=5; i++){
w.write("项目:" + i, 'utf8');
}
w.end("结束写入",function(){
console.log(stock);
});
复制代码
从readable读出数据,writeable写入数据
const stream = require('stream')
var index = 0;
const readable = stream.Readable({
highWaterMark: 2,
read: function () {
process.nextTick(() => {
console.log('push', ++index)
this.push(index+'');
})
}
})
const writable = stream.Writable({
highWaterMark: 2,
write: function (chunk, encoding, next) {
console.log('写入:', chunk.toString())
}
})
readable.pipe(writable);
复制代码
const {Duplex} = require('stream');
const inoutStream = new Duplex({
write(chunk, encoding, callback) {
console.log(chunk.toString());
callback();
},
read(size) {
this.push((++this.index)+'');
if (this.index > 3) {
this.push(null);
}
}
});
inoutStream.index = 0;
process.stdin.pipe(inoutStream).pipe(process.stdout);
复制代码
const {Transform} = require('stream');
const upperCase = new Transform({
transform(chunk, encoding, callback) {
this.push(chunk.toString().toUpperCase());
callback();
}
});
process.stdin.pipe(upperCase).pipe(process.stdout);
/*
实现大小写转换
inpt: a
A
*/
复制代码
最近在学习nodejs,通过整理写下学习笔记。欢迎同窗沟通学习心得,若有错误的地方也欢迎指正。