Nodejs:将字符串转换为缓冲区

时间:2011-08-17 14:34:25

标签: javascript string encoding node.js

我正在尝试将字符串写入套接字(套接字称为“响应”)。这是我已经安装的代码(我正在尝试实现一个字节缓存代理......):

var http = require('http');
var sys=require('sys');

var localHash={};

http.createServer(function(request, response) {
    var proxy = http.createClient(80, request.headers['host'])
    var proxy_request = proxy.request(request.method, request.url, request.headers);
    proxy_request.addListener('response', function (proxy_response) {
    proxy_response.addListener('data', function(x) {
        var responseData=x.toString();
        var f=50;
        var toTransmit="";
        var p=0;        

        var N=responseData.length;
        if(N>f){
            p=Math.floor(N/f);

            var hash="";
            var chunk="";
            for(var i=0;i<p;i++){
                chunk=responseData.substr(f*i,f);
                hash=DJBHash(chunk);
                if(localHash[hash]==undefined){
                    localHash[hash]=chunk;
                    toTransmit=toTransmit+chunk;
                }else{
                    sys.puts("***hit"+chunk);
                    toTransmit=toTransmit+chunk;//"***EOH"+hash;
                }
            }
            //remainder:
            chunk=responseData.substr(f*p);
            hash=DJBHash(chunk);
            if(localHash[hash]==undefined){
                localHash[hash]=chunk;
                toTransmit=toTransmit+chunk;
            }else{
                toTransmit=toTransmit+chunk;//"***EOH"+hash;
            }
        }else{
            toTransmit=responseData;
        }
        response.write(new Buffer(toTransmit));   /*error occurs here */
    });
    proxy_response.addListener('end', function() {
        response.end();
    });
    response.writeHead(proxy_response.statusCode, proxy_response.headers);
    });
    request.addListener('data', function(chunk) {
        sys.puts(chunk);
        proxy_request.write(chunk, 'binary');
    });
    request.addListener('end', function() {
        proxy_request.end();
    });
}).listen(8080);



function DJBHash(str) {
    var hash = 5381;
    for(var i = 0; i < str.length; i++) {
        hash = (((hash << 5) + hash) + str.charCodeAt(i)) & 0xffffffff;
    }
    if(hash<-1){
        hash=hash*-1;
    }
    return hash;
}

问题是,我在Firefox中不断收到“内容编码错误”。这就好像没有正确传输gizipped内容。我通过console.log(x)和console.log(toTransmit)确保“toTransmit”与“x”相同。

值得注意的是,如果我用response.write(new Buffer(toTransmit))替换response.write(x),代理就会按预期工作,但我需要进行一些有效负载分析,然后传递“toTransmit”,而不是“x”。 / p>

我也试过response.write(toTransmit)(即没有转换为缓冲区)并且我不断收到相同的内容编码错误。

我真的被困住了。我认为通过将字符串转换为另一个线程的缓冲区(http://stackoverflow.com/questions/7090510/nodejs-content-encoding-error)来修复此问题,但我重新打开了一个新线程讨论我遇到的这个新问题。

我应该补充说,如果我通过Opera中的代理打开一个页面,我会得到gobblydeegook - 就好像gzip压缩数据一样。

非常感谢任何见解。

非常感谢,

3 个答案:

答案 0 :(得分:20)

这个怎么样?

var responseData= Buffer.from( x, 'utf8' )

来自:Convert string to buffer Node

答案 1 :(得分:19)

如果不深入挖掘您的代码,我觉得您可能想要更改

var responseData=x.toString();

var responseData=x.toString("binary");

最后

response.write(new Buffer(toTransmit, "binary"));

答案 2 :(得分:5)

From the docs

  

Pure Javascript是Unicode友好的,但对二进制数据不好。什么时候   处理TCP流或文件系统时,需要处理   八位组流。 Node有几种操作,创建,   并消耗八位字节流。

     

原始数据存储在Buffer类的实例中。缓冲区是   类似于整数数组但对应于原始内存   在V8堆外部分配。缓冲区无法调整大小。

因此,不要使用字符串来处理二进制数据。

proxy_request.write(chunk, 'binary');更改为proxy_request.write(chunk);

忽略var responseData=x.toString();,这是一个坏主意。

而不是对字符串执行substr,而是在缓冲区上使用slice

不要使用字符串+,而是使用buffertools中的“concat”方法。

相关问题