我已经创建了一个解析NGinx日志的模块,现在我正在编写一个使用它的命令工具。我的问题是,我允许整个目录被解析,这在读取和解析方面不是问题,因为我有一个池来读取和解析,但是,在命令行工具中,我允许现在重写日志的格式为JSON - 确定我将切入追逐,我写了这个Writer对象,它将保持所有WriteStreams的引用(wstreams [readFilePath](我知道我正在使用readFilePath,this仅仅是一键查找), 并且也通过模块暴露对象全部看完流的全球参考Parser.rstreams [readFilePath]Node.js WriteStream未知编码错误
// creating a writer to handle the data buffering from the parser's readstreams
writer = {
wstreams: {},
append: function(data, wfile, rfile){
console.log(JSON.stringify(this.wstreams[rfile]));
if(this.wstreams[rfile]
&& (this.wstreams[rfile].write(data, wfile) == false) // <-- crashing here
&& parser.rstreams[rfile]
&& parser.rstreams[rfile].pause){
console.log('Pausing: ' + rfile);
parser.rstreams[rfile].pause();
}
},
addStream: function(wfile, rfile){
var wstream = fs.createWriteStream(wfile, {'flags': 'w', 'encoding':'utf8', 'mode': '0666'});
console.log("stream added: " + wfile + " r: " + rfile);
this.wstreams[rfile] = wstream;
this.wstreams[rfile].on('drain', function(){
if(parser.rstreams[rfile]
&& parser.rstreams[rfile].readable
&& parser.rstreams[rfile].resume){
console.log('Drained: ' + rfile);
parser.rstreams[rfile].resume();
}
});
}
}
当一个writeStream试图写入数据,它抛出一个未知的编码异常,因为它默认为utf8,所以没有任何意义第二,即使我通过可选的编码,它会做同样的事情,我想UT8,UTF-8和ASCII
{"path":"/Users/akhoury/code/rk/ginx/bin/here.json","fd":8,"writable":true,"flags":"w","encoding":"utf8","mode":"0666","bytesWritten":0,"busy":false,"_queue":[],"_events":{}}
[GINX][ERROR][uncaughtException] Error: Unknown encoding
[GINX-DEBUG] Exiting - 0 {file:cursor} record(s) stored in /Users/akhoury/code/rk/ginx/tmp/stored.cursors
/Users/akhoury/code/rk/ginx/lib/ginx.js:453
throw err;
^
Error: Unknown encoding
at Buffer.write (buffer.js:382:13)
at new Buffer (buffer.js:261:26)
at WriteStream.write (fs.js:1548:12)
at Object.writer.append (/Users/akhoury/code/rk/ginx/bin/ginx.js:95:38)
at /Users/akhoury/code/rk/ginx/bin/ginx.js:152:16
at Ginx.eval [as hardParseLine] (eval at generateParseLine (/Users/akhoury/code/rk/ginx/lib/ginx.js:59:21))
at streamData (/Users/akhoury/code/rk/ginx/lib/ginx.js:179:13)
at Ginx.parseFile.fs.stat.stream.on.streamEnd.cursor (/Users/akhoury/code/rk/ginx/lib/ginx.js:346:28)
at EventEmitter.emit (events.js:93:17)
at ReadStream._emitData (fs.js:1365:10)
我甚至JSON.stringify流,看看里面有什么,它看起来罚款。
我看着buffer.js的来源,它没有任何意义,当编码是没有允许列表中应该出现的错误 https://github.com/joyent/node/blob/master/lib/buffer.js:50的
然后,我有一个循环,将读的目录,如果目录,然后write.addStream(OUTPUTFILE,inputfile中)
if (stats.isDirectory()) {
fs.mkdir(output, function() {
fs.readdir(input, function (err, files) {
if (err) error(err);
files.forEach(function (wfile) {
wfile = path.join(output, file);
rfile = path.join(input, file);
console.log("W:"+ wfile + " R: " + rfile);
//prepend the JSON openings for each new file before we go on.
if (isNewFile(rfile)) {
fs.writeFileSync(wfile, "{[", 'utf8');
}
writer.addStream(wfile, rfile); // <-- adding the stream to writer here
});
processDirectory(input, output);
});
});
} else if (stats.isFile()) {
if (isNewFile(input)) {
fs.writeFile(output, "{[", 'utf8', function() {
writer.addStream(output, input);
processFile(input, output);
});
} else {
writer.addStream(output, input);
processFile(input, output);
}
}
然后在processFile和processDirectory,每次我接收rowCallback,这意味着行被解析,我使用writer.append
// process file parsing to JSON output
function processFile(input, ouput) {
parser.parseFile(input,
function (err, row) {
if (err) error(err);
writer.append(ifLastRow(row), output, row.__file);
},
function (err, rfile) {
if (err) error(err);
//close the JSON array
writer.append("]}", output, file);
});
}
// process directory parsing to JSON outputs
function processDirectory(input, output) {
parser.parseDir(input,
function (err, row) {
if (err) error(err);
var fname = row.__fname;
writer.append(ifLastRow(row), path.join(output, fname), row.__file);
},
function (err, rfile) {
if (err) error(err);
var wfile = path.join(output, rfile.substring(rfile.lastIndexOf(path.sep) + 1));
//close the JSON array
writer.append("]}", wfile, rfile);
},
function (err, filesCount) {
if (err) error(err);
});
}
有人能看到我在做什么错在这里? 我是以错误的方式创建流吗?
我知道这有很多需要阅读的内容,但我不想过于笼统。 谢谢。
这是很多的代码来读取。如果你能投入最少量的代码来重现你的问题,那将是非常好的。 – Floby 2013-03-05 17:18:03
哦,对不起,我只是不想暧昧, 崩溃的部分是在第一个代码块。寻找这一行:'&&(this.wstreams [rfile]。写(数据,wfile)== false)// < - 这里崩溃了' – bentael 2013-03-05 18:03:01
你究竟在这里做什么? 它看起来像一些复杂的机器,基本上只是实现'rfile.pipe(wfile)'。 – isaacs 2013-03-05 18:48:34