arnaucube
arnaucube/thoughtsPublic Self-hosted repository
thoughts/node_modules/body-parser/node_modules/iconv-lite/README.md
node_modules/body-parser/node_modules/iconv-lite/README.mdRaw
  1## Pure JS character encoding conversion [![Build Status](https://travis-ci.org/ashtuchkin/iconv-lite.svg?branch=master)](https://travis-ci.org/ashtuchkin/iconv-lite)
  2
  3 * Doesn't need native code compilation. Works on Windows and in sandboxed environments like [Cloud9](http://c9.io).
  4 * Used in popular projects like [Express.js (body_parser)](https://github.com/expressjs/body-parser), 
  5   [Grunt](http://gruntjs.com/), [Nodemailer](http://www.nodemailer.com/), [Yeoman](http://yeoman.io/) and others.
  6 * Faster than [node-iconv](https://github.com/bnoordhuis/node-iconv) (see below for performance comparison).
  7 * Intuitive encode/decode API
  8 * Streaming support for Node v0.10+
  9 * [Deprecated] Can extend Node.js primitives (buffers, streams) to support all iconv-lite encodings.
 10 * In-browser usage via [Browserify](https://github.com/substack/node-browserify) (~180k gzip compressed with Buffer shim included).
 11 * License: MIT.
 12
 13[![NPM Stats](https://nodei.co/npm/iconv-lite.png?downloads=true&downloadRank=true)](https://npmjs.org/packages/iconv-lite/)
 14
 15## Usage
 16### Basic API
 17```javascript
 18var iconv = require('iconv-lite');
 19
 20// Convert from an encoded buffer to js string.
 21str = iconv.decode(new Buffer([0x68, 0x65, 0x6c, 0x6c, 0x6f]), 'win1251');
 22
 23// Convert from js string to an encoded buffer.
 24buf = iconv.encode("Sample input string", 'win1251');
 25
 26// Check if encoding is supported
 27iconv.encodingExists("us-ascii")
 28```
 29
 30### Streaming API (Node v0.10+)
 31```javascript
 32
 33// Decode stream (from binary stream to js strings)
 34http.createServer(function(req, res) {
 35    var converterStream = iconv.decodeStream('win1251');
 36    req.pipe(converterStream);
 37
 38    converterStream.on('data', function(str) {
 39        console.log(str); // Do something with decoded strings, chunk-by-chunk.
 40    });
 41});
 42
 43// Convert encoding streaming example
 44fs.createReadStream('file-in-win1251.txt')
 45    .pipe(iconv.decodeStream('win1251'))
 46    .pipe(iconv.encodeStream('ucs2'))
 47    .pipe(fs.createWriteStream('file-in-ucs2.txt'));
 48
 49// Sugar: all encode/decode streams have .collect(cb) method to accumulate data.
 50http.createServer(function(req, res) {
 51    req.pipe(iconv.decodeStream('win1251')).collect(function(err, body) {
 52        assert(typeof body == 'string');
 53        console.log(body); // full request body string
 54    });
 55});
 56```
 57
 58### [Deprecated] Extend Node.js own encodings
 59> NOTE: This doesn't work on latest Node versions. See [details](https://github.com/ashtuchkin/iconv-lite/wiki/Node-v4-compatibility).
 60
 61```javascript
 62// After this call all Node basic primitives will understand iconv-lite encodings.
 63iconv.extendNodeEncodings();
 64
 65// Examples:
 66buf = new Buffer(str, 'win1251');
 67buf.write(str, 'gbk');
 68str = buf.toString('latin1');
 69assert(Buffer.isEncoding('iso-8859-15'));
 70Buffer.byteLength(str, 'us-ascii');
 71
 72http.createServer(function(req, res) {
 73    req.setEncoding('big5');
 74    req.collect(function(err, body) {
 75        console.log(body);
 76    });
 77});
 78
 79fs.createReadStream("file.txt", "shift_jis");
 80
 81// External modules are also supported (if they use Node primitives, which they probably do).
 82request = require('request');
 83request({
 84    url: "http://github.com/", 
 85    encoding: "cp932"
 86});
 87
 88// To remove extensions
 89iconv.undoExtendNodeEncodings();
 90```
 91
 92## Supported encodings
 93
 94 *  All node.js native encodings: utf8, ucs2 / utf16-le, ascii, binary, base64, hex.
 95 *  Additional unicode encodings: utf16, utf16-be, utf-7, utf-7-imap.
 96 *  All widespread singlebyte encodings: Windows 125x family, ISO-8859 family, 
 97    IBM/DOS codepages, Macintosh family, KOI8 family, all others supported by iconv library. 
 98    Aliases like 'latin1', 'us-ascii' also supported.
 99 *  All widespread multibyte encodings: CP932, CP936, CP949, CP950, GB2313, GBK, GB18030, Big5, Shift_JIS, EUC-JP.
100
101See [all supported encodings on wiki](https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings).
102
103Most singlebyte encodings are generated automatically from [node-iconv](https://github.com/bnoordhuis/node-iconv). Thank you Ben Noordhuis and libiconv authors!
104
105Multibyte encodings are generated from [Unicode.org mappings](http://www.unicode.org/Public/MAPPINGS/) and [WHATWG Encoding Standard mappings](http://encoding.spec.whatwg.org/). Thank you, respective authors!
106
107
108## Encoding/decoding speed
109
110Comparison with node-iconv module (1000x256kb, on MacBook Pro, Core i5/2.6 GHz, Node v0.12.0). 
111Note: your results may vary, so please always check on your hardware.
112
113    operation             iconv@2.1.4   iconv-lite@0.4.7
114    ----------------------------------------------------------
115    encode('win1251')     ~96 Mb/s      ~320 Mb/s
116    decode('win1251')     ~95 Mb/s      ~246 Mb/s
117
118## BOM handling
119
120 * Decoding: BOM is stripped by default, unless overridden by passing `stripBOM: false` in options
121   (f.ex. `iconv.decode(buf, enc, {stripBOM: false})`).
122   A callback might also be given as a `stripBOM` parameter - it'll be called if BOM character was actually found.
123 * Encoding: No BOM added, unless overridden by `addBOM: true` option.
124
125## UTF-16 Encodings
126
127This library supports UTF-16LE, UTF-16BE and UTF-16 encodings. First two are straightforward, but UTF-16 is trying to be
128smart about endianness in the following ways:
129 * Decoding: uses BOM and 'spaces heuristic' to determine input endianness. Default is UTF-16LE, but can be 
130   overridden with `defaultEncoding: 'utf-16be'` option. Strips BOM unless `stripBOM: false`.
131 * Encoding: uses UTF-16LE and writes BOM by default. Use `addBOM: false` to override.
132
133## Other notes
134
135When decoding, be sure to supply a Buffer to decode() method, otherwise [bad things usually happen](https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding).  
136Untranslatable characters are set to � or ?. No transliteration is currently supported.  
137Node versions 0.10.31 and 0.11.13 are buggy, don't use them (see #65, #77).  
138
139## Testing
140
141```bash
142$ git clone git@github.com:ashtuchkin/iconv-lite.git
143$ cd iconv-lite
144$ npm install
145$ npm test
146    
147$ # To view performance:
148$ node test/performance.js
149
150$ # To view test coverage:
151$ npm run coverage
152$ open coverage/lcov-report/index.html
153```
154
155## Adoption
156[![NPM](https://nodei.co/npm-dl/iconv-lite.png)](https://nodei.co/npm/iconv-lite/)
157[![Codeship Status for ashtuchkin/iconv-lite](https://www.codeship.io/projects/81670840-fa72-0131-4520-4a01a6c01acc/status)](https://www.codeship.io/projects/29053)