Ris*_*vik 2 stream node.js highland.js
我在使用nodejs request流时遇到问题,因为我传递给它已经是空的数组.
var _ = require('highland'),
fs = require('fs'),
request = require('request');
// This works but not using the stream approach
// function get(path) {
// return _(function (push, next) {
// request(path, function (error, response, body) {
// // The response itself also contains the body
// push(error, response);
// push(null, _.nil);
// });
// });
// }
var google = _(request.get('http://www.google.com'));
google
// res is empty array
.map(function (res) {
// console.log(res);
return res;
})
// res is empty array
.toArray(function (res) {
console.log(res);
});
Run Code Online (Sandbox Code Playgroud)
request()模块使用一种旧式流 - 它从代码Stream模块调用流原型上的.pipe()方法:
stream.Stream.prototype.pipe.call(this, dest, opts)
Run Code Online (Sandbox Code Playgroud)
https://github.com/mikeal/request/blob/11224dd1f02e311afcc11df8a8f0be1d9fb2bf83/request.js#L1310
我将实际问题追溯到以下检查节点的核心流模块:
function ondata(chunk) {
if (dest.writable) {
if (false === dest.write(chunk) && source.pause) {
source.pause();
}
}
}
Run Code Online (Sandbox Code Playgroud)
https://github.com/joyent/node/blob/master/lib/stream.js#L50
可以通过在上面的示例中执行以下操作来修补此问题
var google = _(request.get('http://www.google.com'));
google.writable = true;
Run Code Online (Sandbox Code Playgroud)
我提出了一个拉取请求,以便在https://github.com/caolan/highland/pull/42上正确修复此问题,这已经合并,因此从版本1.14.0开始,该错误将不再可重现
| 归档时间: |
|
| 查看次数: |
765 次 |
| 最近记录: |