镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

if Buffer#toString not consume all bytes? #6

Closed

Description

@heroboy

If i write data one byte by one byte, it emit empty.

var split = require("split2");
var buf = new Buffer("烫烫烫\r\n锟斤拷", "utf8");
var s = split();
s.on("data", function (data) { 
    console.log(data);
});

for (var i = 0; i < buf.length; ++i)
{
    s.write(buf.slice(i, i + 1));
}
s.end();

Activity

  1. mcollina commented on Jan 19, 2016

    @mcollina
    Owner

    It works fine for me.

    I've also checked:

    var split = require("split2");
    var buf = new Buffer("aa\r\nbbb", "utf8");
    var s = split();
    s.on("data", function (data) {
        console.log(data);
    });
    
    for (var i = 0; i < buf.length; ++i)
    {
        s.write(buf.slice(i, i + 1));
    }
    s.end();

    Which version of node are you using?

  2. heroboy commented on Jan 19, 2016

    @heroboy
    Author

    no, you should not use string like "aaabbb".
    You should use strings that string.length not equals to buffer.length .
    For example, "烫" buffer in utf8 is [0xe7,0x83,0xab], in above code it will get three chunks in transform function,none of the three chunks can be converted to "烫".

    (new Buffer([0xe7,0x83,0xab])).toString("utf8") == "烫";
    (new Buffer([0xe7])).toString("utf8") == "";
    (new Buffer([0x83])).toString("utf8") == "";
    (new Buffer([0xab])).toString("utf8") == "";
    
  3. mcollina commented on Jan 19, 2016

    @mcollina
    Owner

    I'm lost. I think the best way is for you to send a PR with a valid unit test and possibly a fix.

    It is working for me as far as I know: https://github.com/mcollina/split2/blob/master/test.js#L211-L221

  4. heroboy commented on Jan 19, 2016

    @heroboy
    Author

    change

    input.end("烫烫烫\r\n锟斤拷")
    

    to

    var buf = new Buffer("烫烫烫\r\n锟斤拷", "utf8");
    for (var i = 0; i < buf.length; ++i)
    {
        input.write(buf.slice(i, i + 1));
    }
    input.end();
    
  5. mcollina commented on Jan 19, 2016

    @mcollina
    Owner

    Confirmed, I'll see how it is fixable.

  6. heroboy commented on Jan 19, 2016

    @heroboy
    Author

    try this

    through({encoding:"utf8"})                 //convert Buffer to String (buf => data)
        .pipe(through({decodeStrings:false},   //prevent convert String to Buffer (buf => chunk)
            function(chunk,enc,cb){}           //your transform function,now chunk is string.
    );
    

    And please expose the encoding in options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions