2016-06-12 84 views
1

我正在通過它的許多方法與pngjs。大多數時候,他們工作得很好。然而,就像在下面的例子中,我得到一個錯誤:「流不可寫」node.js - pngjs錯誤:「流不可寫」隨機

var fs = require('fs'), 
PNG = require('pngjs').PNG; 

var dst = new PNG({width: 100, height: 50}); 
fs.createReadStream('http://1.1m.yt/hry7Eby.png') //download this picture in order to examine the code. 
    .pipe(new PNG()) 
    .on('parsed', function(data) { 
     console.log(data); 
    }); 

這種情況不是單一的,我得到1個隨機PNG圖像這個錯誤,每天一次,通過所有的pngjs方法,而且這個錯誤顯然會使我的應用崩潰。 (注:您無法使用HTTP鏈接我給你一個readStream,你必須下載&將其重命名,並完成類似):

fs.createReadStream('1.png') 

謝謝您的時間和精力。

回答

0

這似乎是圖書館的一個錯誤,雖然我很擔心,因爲我不是PNG專家。解析器似乎在流仍在寫入時完成。它遇到的IEND,所以調用此:

ParserAsync.prototype._finished = function() { 
    if (this.errord) { 
    return; 
    } 

    if (!this._inflate) { 
    this.emit('error', 'No Inflate block'); 
    } 
    else { 
    // no more data to inflate 
    this._inflate.end(); 
    } 
    this.destroySoon(); 
}; 

如果你註釋掉它正確地完成圖像的this.destroySoon();,而不是最終調用這個函數:

ChunkStream.prototype.end = function(data, encoding) { 

    if (data) { 
    this.write(data, encoding); 
    } 

    this.writable = false; 

    // already destroyed 
    if (!this._buffers) { 
    return; 
    } 

    // enqueue or handle end 
    if (this._buffers.length === 0) { 
    this._end(); 
    } 
    else { 
    this._buffers.push(null); 
    this._process(); 
    } 
}; 

...否則將結束將stream.writeable設置爲false,或者如果您註釋掉,則將空值推入_buffers陣列並擰緊ChunkStream._processRead

我相當肯定這是zlib的解析器需要完成的時間和流需要完成的時間之間的同步問題,因爲如果你這樣做同步正常工作:

var data = fs.readFileSync('pic.png'); 
var png = PNG.sync.read(data); 
var buff = PNG.sync.write(png); 
fs.writeFileSync('out2.png', buff);