- Doesn't need native code compilation. Works on Windows and in sandboxed environments like Cloud9.
- Used in popular projects like Grunt, Nodemailer, Yeoman and others.
- Faster than node-iconv (see below for performance comparison).
- Intuitive encode/decode API
- Streaming support for Node v0.10+
- Can extend Node.js primitives (buffers, streams) to support all iconv-lite encodings - for the lazy.
- In-browser usage via Browserify (150k gzip compressed with Buffer shim included).
- License: MIT.
var iconv = require('iconv-lite');
// Convert from an encoded buffer to js string.
str = iconv.decode(buf, 'win1251');
// Convert from js string to an encoded buffer.
buf = iconv.encode("Sample input string", 'win1251');
// Check if encoding is supported
iconv.encodingExists("us-ascii")
// Decode stream example (from binary stream to js strings)
// Only available in Node v0.10+
http.createServer(function(req, res) {
var converterStream = iconv.decodeStream('win1251');
req.pipe(converterStream);
converterStream.on('data', function(str) {
console.log(str); // Do something with decoded strings, chunk-by-chunk.
});
});
// Convert encoding streaming example
fs.createReadStream('file-in-win1251.txt')
.pipe(iconv.decodeStream('win1251'))
.pipe(iconv.encodeStream('ucs2'))
.pipe(fs.createWriteStream('file-in-ucs2.txt'));
// Sugar: all encode/decode streams have .collect(cb) method to accumulate data.
http.createServer(function(req, res) {
req.pipe(iconv.decodeStream('win1251')).collect(function(err, body) {
assert(typeof body == 'string');
console.log(body); // full request body string
});
});
// For the brave/lazy: make Node basic primitives understand all iconv encodings.
require('iconv-lite').extendNodeEncodings();
buf = new Buffer(str, 'win1251');
buf.write(str, 'gbk');
str = buf.toString('latin1');
assert(Buffer.isEncoding('iso-8859-15'));
Buffer.byteLength(str, 'us-ascii');
http.createServer(function(req, res) {
req.setEncoding('big5');
req.collect(function(err, body) {
console.log(body);
});
});
fs.createReadStream("file.txt", "shift_jis");
// External modules are also supported (if they use Node primitives, which they probably do).
request = require('request');
request({
url: "http://github.com/",
encoding: "cp932"
});
- All node.js native encodings: 'utf8', 'ucs2', 'ascii', 'binary', 'base64'
- All widespread singlebyte encodings: Windows 125x family, ISO-8859 family, IBM/DOS codepages, Macintosh family, KOI8 family, all others supported by iconv library. Aliases like 'latin1', 'us-ascii' also supported.
- Multibyte encodings: CP932, CP936, CP949, CP950, GBK, GB2313, GB18030, Big5, Shift_JIS.
Most singlebyte encodings are generated automatically from node-iconv. Thank you Ben Noordhuis and libiconv authors!
Not supported yet: EUC family, ISO2022 family.
Comparison with node-iconv module (1000x256kb, on MacBook Pro, Core i5/2.6 GHz, Node v0.10.26). Note: your results may vary, so please always check on your hardware.
operation [email protected] [email protected]
----------------------------------------------------------
encode('win1251') ~115 Mb/s ~340 Mb/s
decode('win1251') ~110 Mb/s ~180 Mb/s
When decoding, a 'binary'-encoded string can be used as a source buffer.
Untranslatable characters are set to � or ?. No transliteration is currently supported.
git clone [email protected]:ashtuchkin/iconv-lite.git
cd iconv-lite
npm install
npm test
# To view performance:
node test/performance.js