- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经创建了一个解析 NGinx 日志的模块,现在我正在编写一个使用它的命令工具。我的问题是我允许解析整个目录,这在读取然后解析方面不是问题,因为我有一个池可以读取和解析,但是,在命令行工具上,我允许以不同的格式重写日志,现在是 JSON,- 好吧,我将切入正题,我已经编写了这个 Writer 对象,它将保留所有 WriteStreams 的引用(wstreams[readFilePath](我知道我正在使用 readFilePath,这只是查找的关键),并且还有通过模块公开对象 Parser.rstreams[readFilePath] 的所有读取流的全局引用
// 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 岁
然后我有一个循环读取目录,如果是目录,然后写入。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);
});
}
谁能看出我在这里做错了什么?我是否以错误的方式创建流?
我知道需要阅读的内容很多,但我不想过于笼统。谢谢。
最佳答案
问题是您将文件名作为第二个参数传递给 stream.write() ,但是 .write()
的第二个参数是一个可选的编码(参见前面的链接)。
错误是因为它试图将文件名用作编码,这是一种未知编码
。如果data
是一个buffer,那么让它根据buffer来决定编码。
写入流与文件相关联,因此您无需在每次写入时传递文件名。尝试更改:
&& (this.wstreams[rfile].write(data, wfile) == false)
收件人:
&& (this.wstreams[rfile].write(data) == false)
关于Node.js WriteStream 未知编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15229856/
我有一个交叉表函数,我过去曾多次成功使用它,但现在它在最后转储所有数据,而不是将其旋转到输出表中。它似乎无法找到交叉表。我通过以下方式对其进行了研究; 如果 tablefunc 不存在则创建扩展; -
表1(客户表) Id, CustomerId, IsKnownCustomer,phonemacaddress 1, 空 0 00:9a:34:cf:a4 2, 004024 1 00:6f:64:c
知道为什么我总是收到这个烦人且无用的错误代码/描述吗? Failed to pull image myapidemodocker.azurecr.io/apidemo:v4.0: rpc error:
我正在进行 PHP 登录,并且之前可以正常工作,但我尝试使用户名功能不区分大小写,但此后代码一直无法正常工作。我删除了我添加的所有内容,以尝试使其不区分大小写,即 strtolower()。页面上显示
有人会帮助我提供有关此错误的任何可能信息吗?原因?登录?在哪里寻找/开始? Cannot use output buffering in output buffering display handl
我已经添加了这样的脚本 我在我的 test.js 中做了这个 var app = angular.module('MyApp', ['ngRoute']).config
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我有这个sql语句: selectAllUsersByCriteria = 连接.prepareStatement( “从用户那里选择*?=?” ); 下面的方法运行该语句: public Array
我有一个白色的 EditText,在 Android 3.1 及更高版本中,光标不显示(因为它也是白色的)。有关信息,我使用 android:background="@android:drawable
我正在尝试使用 Keras 实现深度学习模型。但是我有一个未知形状实现的问题。我一直在寻找类似的错误,但没有找到。 这是我的代码。 Xhome = dataset[:,32:62] Xaway = d
关注此introduction可以通过导入命名空间 System.Xml 来使用 XMLReader 类。在我的 Visual Studio 项目中,我使用 .NET 4.0,但 System.Xml
我有一个动态链接库的程序。该程序将函数指针传递给该库以执行。 但是 ubsan(Undefined Behavior Sanitizer)指定指针位于错误的函数类型上。那只会发生 如果回调函数有一个类
我正在尝试在我的 Swift SpriteKit 应用程序中使用 AVAudioSession。我遇到了奇怪的“未声明类型”问题。例如…… import AVFoundation var audioS
如果在编译期间(在实际编译和运行程序之前)其参数之一的值已知/未知,如何专门化模板函数? 我还不知道怎么做。 想法 1: #include #include int main(void){
我看到一些人的代码是这样的: while (!(baseType == typeof(Object))) { .... baseType = baseType.BaseType;
我正在尝试使用 GoColly 框架获取所有 HREF 链接,但是只允许任何域的 url 为根 URL 或子域(否路径)。我已经注释掉了我的 REGEXP。文件扩展名没有事情。我只是在“/”之后不想要
我有一个包含多个实体的数据库,特别是 Book 和 User。它们之间存在这样的 ManyToMany 关系: 书: @Entity @Table(name = "Books") public cla
如果我将范围的初始部分设置为 Range("A:A"),如何确保将整行传递给排序? 数据 id、fname、mname、lname、后缀、状态、位置、时区 通过在 id 中搜索起点和终点来选择范围。
我对kubernetes很陌生,而对于docker来说就不那么多了。 我一直在研究示例,但是我对自动缩放器(似乎无法缩放)感到困惑。 我在这里通过示例https://kubernetes.io/doc
我在 ChildWindow 中使用 SL Toolkit 5 中的 BusyIndicator 控件。 在某些解决方案中,它可以工作,但在其他解决方案中,使用完全相同的代码(至少看起来),我在运
我是一名优秀的程序员,十分优秀!