gpt4 book ai didi

javascript - 读取 ReadableStream 并指定大小参数 : Why does state. highWaterMark 总是作为大小传递给 ._read()?

转载 作者:行者123 更新时间:2023-11-30 12:59:41 26 4
gpt4 key购买 nike

在 Node.js 中,我有一个可读流:

var rs = new (require('stream').Readable);                                                                                                                                                                                                    
rs._read = function(size) {
// Implementation.
}

在这个 HTTP 服务器实例中读取这个流:

var http = require('http');

var server = http.createServer(function(req, res) {
var size = require('url').parse(req.url, true).query.size;
if (size) {
rs.pipe(res);
rs.read(parseInt(size));
}
res.end('');
});

server.listen(3001, function() {
console.log('Listening on 3001');
});

现在,当我 curl localhost:3001/?size=n ,其中 n 是任意整数,sizers._read(size)永远是 16384!此数字是流的默认值 state.highWaterMark ,因为 rs._read()称为 this._read(state.highWaterMark);lib/_stream_readable.js .谁能解释一下? size 有什么意义?如果不使用参数?

谢谢。

最佳答案

看起来这只是流的 highWaterMarknot coincidentally默认的大小Read more here

var stream = require("stream");

var rs = new stream.Readable();

{ _readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: false,
calledRead: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
objectMode: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: {},
_maxListeners: 10 }

关于javascript - 读取 ReadableStream 并指定大小参数 : Why does state. highWaterMark 总是作为大小传递给 ._read()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17710712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com