Skip to content

Node v4 compatibility

Alexander Shtuchkin edited this page Sep 26, 2015 · 3 revisions

TL;DR: extendNodeEncodings() doesn't work with Node v4+. You'll need to update your code to use encode and decode functions directly.

Context

In Node v4, Buffer inherits directly from Uint8Array and all string conversion routines are hidden in local scopes, so it's not possible to use patching machinery of extendNodeEncodings function. It was arguably a hack from the very beginning, so we cannot blame Node authors for it. We'll work with Node community to create an officially supported way of extending encoding set.

What will happen if I don't change the code?

Utf8 will be used instead. This is the default behavior when you give an unknown encoding to Node.

Conversion table

NOTE: Streaming API works without changes.

Current (deprecated, bad) Supported equivalent
iconv.extendNodeEncodings() --
iconv.undoExtendNodeEncodings() --
new Buffer(str, enc) iconv.encode(str, enc)
buf.toString(enc) iconv.decode(buf, enc)
buf.write(str, enc) iconv.encode(str, enc).copy(buf)
Buffer.isEncoding(enc) iconv.encodingExists(enc)
stream.setEncoding(enc) stream = stream.pipe(iconv.decodeStream(enc))
fs.createReadStream(file, enc) fs.createReadStream(file).pipe(iconv.decodeStream(enc))

More information

Issues: #105, nodejs/node#2835