NodeJS从缓冲区读取ASCII

我有一个应用程序将收到一个二进制格式的文件<type>:<location>\n<binary> ,所以图片可能看起来像image:~/documents/image.png\n<image>

要在节点中读取,我有以下代码。

 var type = ''; var destination = ''; var i = -1; while (data[++i] != ':') type += data[i]; while (data[++i] != '\n') destination += data[i]; data = data.slice(i); 

但是,这会导致节点吃掉所有的RAM和CPU,并最终崩溃。 我究竟做错了什么?

谢谢!

解决了!

 var type = ''; var destination = ''; var char = ''; var i = 0; while ((char = String.fromCharCode(data.readUInt8(i++))) != ':') type += char; while ((char = String.fromCharCode(data.readUInt8(i++))) != '\n') destination += char; data = data.slice(i);