1
在我的程序中,我有一個可讀的Stream
source
,它從共享的字節數據流中發出數據事件。然後我解釋數據以提取Buffer
對象,然後將它們傳遞給適當的函數。將Buffer
s傳遞給我程序其餘部分的最優雅的方式似乎是使用可讀的Stream
對象。這樣我的函數就可以像讀取獨立流一樣讀取流。如何在我自己的Node.JS可讀流中實現setEncoding?
這裏是我當前的代碼:
var stream = new stream.Stream()
stream.readable = true
stream.pause = function() {
source.pause()
}
stream.resume = function() {
source.resume()
}
stream.readable = true
// in a loop, read the data from source stream, dissect the parts we want,
// and emit Buffer objects to the stream's data event.
我如何能實現setEncoding
?
編輯:我想答案是
the stream.js source code
Stream.Readable = require('_stream_readable');
而且關係到這條線,正確的答案可能是
- 與
new stream.Readable
取代new stream.Stream
和 - 更換
stream.emit('data', buffer)
與
要麼stream.onread(buffer)
要麼stream.push(buffer)
。事情是,我不知道哪一個。
編輯:stream.Readable
不是0.8版本。它在0.9測試版中可用,並且當它出現時將在v0.10穩定版中可用。這個問題的理想答案將會描述0.8解決方案和0.9+解決方案。