root / HServer / 00.Server / 00.Program / node_modules / iconv-lite / README.md
이력 | 보기 | 이력해설 | 다운로드 (6.64 KB)
| 1 | 39 | HKM | ## Pure JS character encoding conversion [](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 | * Typescript [type definition file](https://github.com/ashtuchkin/iconv-lite/blob/master/lib/index.d.ts) included. |
||
| 12 | * React Native is supported (need to explicitly `npm install` two more modules: `buffer` and `stream`). |
||
| 13 | * License: MIT. |
||
| 14 | |||
| 15 | [](https://npmjs.org/packages/iconv-lite/) |
||
| 16 | |||
| 17 | ## Usage |
||
| 18 | ### Basic API |
||
| 19 | ```javascript |
||
| 20 | var iconv = require('iconv-lite');
|
||
| 21 | |||
| 22 | // Convert from an encoded buffer to js string. |
||
| 23 | str = iconv.decode(new Buffer([0x68, 0x65, 0x6c, 0x6c, 0x6f]), 'win1251'); |
||
| 24 | |||
| 25 | // Convert from js string to an encoded buffer. |
||
| 26 | buf = iconv.encode("Sample input string", 'win1251');
|
||
| 27 | |||
| 28 | // Check if encoding is supported |
||
| 29 | iconv.encodingExists("us-ascii")
|
||
| 30 | ``` |
||
| 31 | |||
| 32 | ### Streaming API (Node v0.10+) |
||
| 33 | ```javascript |
||
| 34 | |||
| 35 | // Decode stream (from binary stream to js strings) |
||
| 36 | http.createServer(function(req, res) {
|
||
| 37 | var converterStream = iconv.decodeStream('win1251');
|
||
| 38 | req.pipe(converterStream); |
||
| 39 | |||
| 40 | converterStream.on('data', function(str) {
|
||
| 41 | console.log(str); // Do something with decoded strings, chunk-by-chunk. |
||
| 42 | }); |
||
| 43 | }); |
||
| 44 | |||
| 45 | // Convert encoding streaming example |
||
| 46 | fs.createReadStream('file-in-win1251.txt')
|
||
| 47 | .pipe(iconv.decodeStream('win1251'))
|
||
| 48 | .pipe(iconv.encodeStream('ucs2'))
|
||
| 49 | .pipe(fs.createWriteStream('file-in-ucs2.txt'));
|
||
| 50 | |||
| 51 | // Sugar: all encode/decode streams have .collect(cb) method to accumulate data. |
||
| 52 | http.createServer(function(req, res) {
|
||
| 53 | req.pipe(iconv.decodeStream('win1251')).collect(function(err, body) {
|
||
| 54 | assert(typeof body == 'string'); |
||
| 55 | console.log(body); // full request body string |
||
| 56 | }); |
||
| 57 | }); |
||
| 58 | ``` |
||
| 59 | |||
| 60 | ### [Deprecated] Extend Node.js own encodings |
||
| 61 | > NOTE: This doesn't work on latest Node versions. See [details](https://github.com/ashtuchkin/iconv-lite/wiki/Node-v4-compatibility). |
||
| 62 | |||
| 63 | ```javascript |
||
| 64 | // After this call all Node basic primitives will understand iconv-lite encodings. |
||
| 65 | iconv.extendNodeEncodings(); |
||
| 66 | |||
| 67 | // Examples: |
||
| 68 | buf = new Buffer(str, 'win1251'); |
||
| 69 | buf.write(str, 'gbk'); |
||
| 70 | str = buf.toString('latin1');
|
||
| 71 | assert(Buffer.isEncoding('iso-8859-15'));
|
||
| 72 | Buffer.byteLength(str, 'us-ascii'); |
||
| 73 | |||
| 74 | http.createServer(function(req, res) {
|
||
| 75 | req.setEncoding('big5');
|
||
| 76 | req.collect(function(err, body) {
|
||
| 77 | console.log(body); |
||
| 78 | }); |
||
| 79 | }); |
||
| 80 | |||
| 81 | fs.createReadStream("file.txt", "shift_jis");
|
||
| 82 | |||
| 83 | // External modules are also supported (if they use Node primitives, which they probably do). |
||
| 84 | request = require('request');
|
||
| 85 | request({
|
||
| 86 | url: "http://github.com/", |
||
| 87 | encoding: "cp932" |
||
| 88 | }); |
||
| 89 | |||
| 90 | // To remove extensions |
||
| 91 | iconv.undoExtendNodeEncodings(); |
||
| 92 | ``` |
||
| 93 | |||
| 94 | ## Supported encodings |
||
| 95 | |||
| 96 | * All node.js native encodings: utf8, ucs2 / utf16-le, ascii, binary, base64, hex. |
||
| 97 | * Additional unicode encodings: utf16, utf16-be, utf-7, utf-7-imap. |
||
| 98 | * All widespread singlebyte encodings: Windows 125x family, ISO-8859 family, |
||
| 99 | IBM/DOS codepages, Macintosh family, KOI8 family, all others supported by iconv library. |
||
| 100 | Aliases like 'latin1', 'us-ascii' also supported. |
||
| 101 | * All widespread multibyte encodings: CP932, CP936, CP949, CP950, GB2312, GBK, GB18030, Big5, Shift_JIS, EUC-JP. |
||
| 102 | |||
| 103 | See [all supported encodings on wiki](https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings). |
||
| 104 | |||
| 105 | Most singlebyte encodings are generated automatically from [node-iconv](https://github.com/bnoordhuis/node-iconv). Thank you Ben Noordhuis and libiconv authors! |
||
| 106 | |||
| 107 | Multibyte 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! |
||
| 108 | |||
| 109 | |||
| 110 | ## Encoding/decoding speed |
||
| 111 | |||
| 112 | Comparison with node-iconv module (1000x256kb, on MacBook Pro, Core i5/2.6 GHz, Node v0.12.0). |
||
| 113 | Note: your results may vary, so please always check on your hardware. |
||
| 114 | |||
| 115 | operation iconv@2.1.4 iconv-lite@0.4.7 |
||
| 116 | ---------------------------------------------------------- |
||
| 117 | encode('win1251') ~96 Mb/s ~320 Mb/s
|
||
| 118 | decode('win1251') ~95 Mb/s ~246 Mb/s
|
||
| 119 | |||
| 120 | ## BOM handling |
||
| 121 | |||
| 122 | * Decoding: BOM is stripped by default, unless overridden by passing `stripBOM: false` in options |
||
| 123 | (f.ex. `iconv.decode(buf, enc, {stripBOM: false})`).
|
||
| 124 | A callback might also be given as a `stripBOM` parameter - it'll be called if BOM character was actually found. |
||
| 125 | * If you want to detect UTF-8 BOM when decoding other encodings, use [node-autodetect-decoder-stream](https://github.com/danielgindi/node-autodetect-decoder-stream) module. |
||
| 126 | * Encoding: No BOM added, unless overridden by `addBOM: true` option. |
||
| 127 | |||
| 128 | ## UTF-16 Encodings |
||
| 129 | |||
| 130 | This library supports UTF-16LE, UTF-16BE and UTF-16 encodings. First two are straightforward, but UTF-16 is trying to be |
||
| 131 | smart about endianness in the following ways: |
||
| 132 | * Decoding: uses BOM and 'spaces heuristic' to determine input endianness. Default is UTF-16LE, but can be |
||
| 133 | overridden with `defaultEncoding: 'utf-16be'` option. Strips BOM unless `stripBOM: false`. |
||
| 134 | * Encoding: uses UTF-16LE and writes BOM by default. Use `addBOM: false` to override. |
||
| 135 | |||
| 136 | ## Other notes |
||
| 137 | |||
| 138 | When 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). |
||
| 139 | Untranslatable characters are set to � or ?. No transliteration is currently supported. |
||
| 140 | Node versions 0.10.31 and 0.11.13 are buggy, don't use them (see #65, #77). |
||
| 141 | |||
| 142 | ## Testing |
||
| 143 | |||
| 144 | ```bash |
||
| 145 | $ git clone git@github.com:ashtuchkin/iconv-lite.git |
||
| 146 | $ cd iconv-lite |
||
| 147 | $ npm install |
||
| 148 | $ npm test |
||
| 149 | |||
| 150 | $ # To view performance: |
||
| 151 | $ node test/performance.js |
||
| 152 | |||
| 153 | $ # To view test coverage: |
||
| 154 | $ npm run coverage |
||
| 155 | $ open coverage/lcov-report/index.html |
||
| 156 | ``` |
||
| 157 | |||
| 158 | ## Adoption |
||
| 159 | [](https://nodei.co/npm/iconv-lite/) |
||
| 160 | [](https://www.codeship.com/projects/29053) |