Please excuse my poor English.
I've already ported node-v4.1.1 to my Android device, but I got the following error:
$ node-v4.1.1 trans.js "hello"
buffer.js:238
buf.copy(buffer, pos);
TypeError: buf.copy is not a function
at Function.Buffer.concat (buffer.js:238:9)
at IncomingMessage.<anonymous> (/data/local/trans.js:20:22)
at emitNone (event.js:72:20)
at IncomingMessage.emit (event.js:166:7)
at endReadableNT (_stream_readable.js:893:12)
at doNTCallback2 (node.js:429:9)
at process._tickCallback (node.js:343:17)
But there's no problem with v0.12.7
$ node-v0.12.7 trans.js "hello"
您好
Here is the content of trans.js:
var api = 'http://openapi.baidu.com/public/2.0/bmt/translate?client_id=wo99UGjCxweEkgKrKFuf7N0n&from=auto&to=auto&q=';
var http = require("http");
var qs = require("querystring");
function translate(text, callback) {
var getstr = api + qs.escape(text);
http.get(getstr, function(res) {
var buffer = [];
res.setEncoding('utf-8');
res.on('data', function(chunk) {
buffer.push(chunk);
});
res.on('end', function() {
var body = Buffer.concat(buffer).toString();
var json = JSON.parse(body);
callback(json);
});
});
}
function showResult(json) {
var res = json.trans_result;
res.forEach(function(item) {
console.log(item.dst);
});
}
if (process.argv.length <= 2) {
console.log("Usage: translate.js <string-to-translate>");
process.exit(1);
}
var argv = process.argv.slice(2);
translate(argv[0], showResult);
`buffer.push(chunk);`` — could you log chunks at this point?
The res.setEncoding('utf-8') causes your buffer to be an array of strings (in this case, a single string) and Buffer.concat([<String>]) is causing problems. If you remove the res.setEncoding this example works.
Here is a simpler repro case:
var a = ['abc']
Buffer.concat(a)
The API docs don't indicate that Buffer.concat([<String>]) should be supported, but it's also not listed as one of the breaking changes https://github.com/nodejs/node/wiki/API-changes-between-v0.10-and-v4
thanks, I removed res.setEncoding, and now it works. :+1:
@brycebaril Did Buffer.concat([<String>]) work in 0.10 and/or 0.12?
@ChALkeR when I test the 2-line repro script it works in every version of node from 0.8 up until io.js 3.0.0.
This is either a regression or an API change.
@trevnorris Should this be mentioned in the API changes or fixed?
@ChALkeR I just went though code back to v0.8. I don't see how this ever could have worked. Just tried it on v0.12 myself:
$ ./node -v
v0.12.7
$ ./node
> Buffer.concat(['abc', 'def'])
TypeError: undefined is not a function
This may have been a bug in setEncoding() in v0.12. Not sure though.
@trevnorris it only worked in the past if a.length === 1
bryce@x1c:~/boneyard$ node -v
v0.12.7
bryce@x1c:~/boneyard$ node
> Buffer.concat(['abc'])
'abc'
> Buffer.concat(['abc', 'def'])
TypeError: undefined is not a function
at Function.Buffer.concat (buffer.js:215:9)
at repl:1:8
at REPLServer.defaultEval (repl.js:132:27)
at bound (domain.js:254:14)
at REPLServer.runBound [as eval] (domain.js:267:12)
at REPLServer.<anonymous> (repl.js:279:12)
at REPLServer.emit (events.js:107:17)
at REPLServer.Interface._onLine (readline.js:214:10)
at REPLServer.Interface._line (readline.js:553:8)
at REPLServer.Interface._ttyWrite (readline.js:830:14)
@brycebaril ah. because of a previous API stupidity where it would simply return the same value. instead of making a copy. that's been fixed.
@trevnorris got it, since it used to be documented I'd be happy if the resolution to this was to get it included in @Fishrock123's API change list
edit: actually looking at @Fishrock123's list it _does_ mention it:
Buffer.concat() now always creates a new buffer, even if only called with one element.
That language isn't extremely clear because it doesn't point out the old behavior, but perhaps it's enough?
Maybe someone could edit in another point under mine about this? It _is_ a wiki. :)
@Fishrock123 edited, thanks for the nudge ;)
I'd say this can be closed now.
Most helpful comment
thanks, I removed
res.setEncoding, and now it works. :+1: