2014-09-22 38 views
0

我从stdin(使用nodejs)实时解压缩数据流时遇到问题。 需要处理解压缩的流,只要它到达stdin(最多几毫秒的延迟)。 问题是管道stdin zlib接缝等待流关闭。nodejs将管道标准输入到zlib中而不等待EOF

以下打印12345

$ echo 12345 | node deflate.js | node inflate.js 
12345 

然而下面的命令行不会因为它没有接收到EOF:

$ node generator.js | node deflate.js | node inflate.js 

这涉及到的问题,如果zlib的放气可在内部处理的部分输入,或应该将它添加到流中(例如每个流块之前的块的大小)。

deflate.js:

process.stdin 
.pipe(require("zlib").createDeflate()) 
.pipe(process.stdout); 

inflate.js

process.stdin 
.pipe(require('zlib').createInflate()) 
.pipe(process.stdout) 

generator.js:

var i = 0 
setInterval(function() { 
    process.stdout.write(i.toString()) 
    i++ 
},1000) 

回答

0

的问题是,Z_SYNC_FLUSH标志没有设置:

If flush is Z_SYNC_FLUSH, deflate() shall flush all pending output to next_out and align the output to a byte boundary. A synchronization point is generated in the output. 

deflate.js:

var zlib = require("zlib"); 
var deflate = zlib.createDeflate({flush: zlib.Z_SYNC_FLUSH}); 
process.stdin.pipe(deflate).pipe(process.stdout); 

inflate.js:

var zlib = require('zlib') 
process.stdin 
.pipe(zlib.createInflate({flush: zlib.Z_SYNC_FLUSH})) 
.pipe(process.stdout)