~ps-jenkins/ubuntu-push/ubuntu-vivid-proposed

« back to all changes in this revision

Viewing changes to docs/example-server/node_modules/body-parser/node_modules/iconv-lite/README.md

  • Committer: Roberto Alsina
  • Date: 2014-09-05 14:57:17 UTC
  • mto: (91.179.25 automatic)
  • mto: This revision was merged to the branch mainline in revision 129.
  • Revision ID: roberto.alsina@canonical.com-20140905145717-0ufcsv27w25i1nnu
added example app server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Pure JS character encoding conversion
 
2
 
 
3
<!-- [![Build Status](https://secure.travis-ci.org/ashtuchkin/iconv-lite.png?branch=master)](http://travis-ci.org/ashtuchkin/iconv-lite) -->
 
4
 
 
5
 * Doesn't need native code compilation. Works on Windows and in sandboxed environments like [Cloud9](http://c9.io).
 
6
 * Used in popular projects like [Grunt](http://gruntjs.com/), [Nodemailer](http://www.nodemailer.com/), [Yeoman](http://yeoman.io/) and others.
 
7
 * Faster than [node-iconv](https://github.com/bnoordhuis/node-iconv) (see below for performance comparison).
 
8
 * Intuitive encode/decode API
 
9
 * Streaming support for Node v0.10+
 
10
 * Can extend Node.js primitives (buffers, streams) to support all iconv-lite encodings.
 
11
 * In-browser usage via [Browserify](https://github.com/substack/node-browserify) (~180k gzip compressed with Buffer shim included).
 
12
 * License: MIT.
 
13
 
 
14
[![NPM Stats](https://nodei.co/npm/iconv-lite.png?downloads=true)](https://npmjs.org/packages/iconv-lite/)
 
15
 
 
16
## Usage
 
17
### Basic API
 
18
```javascript
 
19
var iconv = require('iconv-lite');
 
20
 
 
21
// Convert from an encoded buffer to js string.
 
22
str = iconv.decode(new Buffer([0x68, 0x65, 0x6c, 0x6c, 0x6f]), 'win1251');
 
23
 
 
24
// Convert from js string to an encoded buffer.
 
25
buf = iconv.encode("Sample input string", 'win1251');
 
26
 
 
27
// Check if encoding is supported
 
28
iconv.encodingExists("us-ascii")
 
29
```
 
30
 
 
31
### Streaming API (Node v0.10+)
 
32
```javascript
 
33
 
 
34
// Decode stream (from binary stream to js strings)
 
35
http.createServer(function(req, res) {
 
36
    var converterStream = iconv.decodeStream('win1251');
 
37
    req.pipe(converterStream);
 
38
 
 
39
    converterStream.on('data', function(str) {
 
40
        console.log(str); // Do something with decoded strings, chunk-by-chunk.
 
41
    });
 
42
});
 
43
 
 
44
// Convert encoding streaming example
 
45
fs.createReadStream('file-in-win1251.txt')
 
46
    .pipe(iconv.decodeStream('win1251'))
 
47
    .pipe(iconv.encodeStream('ucs2'))
 
48
    .pipe(fs.createWriteStream('file-in-ucs2.txt'));
 
49
 
 
50
// Sugar: all encode/decode streams have .collect(cb) method to accumulate data.
 
51
http.createServer(function(req, res) {
 
52
    req.pipe(iconv.decodeStream('win1251')).collect(function(err, body) {
 
53
        assert(typeof body == 'string');
 
54
        console.log(body); // full request body string
 
55
    });
 
56
});
 
57
```
 
58
 
 
59
### Extend Node.js own encodings
 
60
```javascript
 
61
// After this call all Node basic primitives will understand iconv-lite encodings.
 
62
iconv.extendNodeEncodings();
 
63
 
 
64
// Examples:
 
65
buf = new Buffer(str, 'win1251');
 
66
buf.write(str, 'gbk');
 
67
str = buf.toString('latin1');
 
68
assert(Buffer.isEncoding('iso-8859-15'));
 
69
Buffer.byteLength(str, 'us-ascii');
 
70
 
 
71
http.createServer(function(req, res) {
 
72
    req.setEncoding('big5');
 
73
    req.collect(function(err, body) {
 
74
        console.log(body);
 
75
    });
 
76
});
 
77
 
 
78
fs.createReadStream("file.txt", "shift_jis");
 
79
 
 
80
// External modules are also supported (if they use Node primitives, which they probably do).
 
81
request = require('request');
 
82
request({
 
83
    url: "http://github.com/", 
 
84
    encoding: "cp932"
 
85
});
 
86
 
 
87
// To remove extensions
 
88
iconv.undoExtendNodeEncodings();
 
89
```
 
90
 
 
91
## Supported encodings
 
92
 
 
93
 *  All node.js native encodings: utf8, ucs2 / utf16, ascii, binary, base64, hex.
 
94
 *  Additional unicode encodings: utf16, utf16-be.
 
95
 *  All widespread singlebyte encodings: Windows 125x family, ISO-8859 family, 
 
96
    IBM/DOS codepages, Macintosh family, KOI8 family, all others supported by iconv library. 
 
97
    Aliases like 'latin1', 'us-ascii' also supported.
 
98
 *  All widespread multibyte encodings: CP932, CP936, CP949, CP950, GB2313, GBK, GB18030, Big5, Shift_JIS, EUC-JP.
 
99
 
 
100
See [all supported encodings on wiki](https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings).
 
101
 
 
102
Most singlebyte encodings are generated automatically from [node-iconv](https://github.com/bnoordhuis/node-iconv). Thank you Ben Noordhuis and libiconv authors!
 
103
 
 
104
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!
 
105
 
 
106
 
 
107
## Encoding/decoding speed
 
108
 
 
109
Comparison with node-iconv module (1000x256kb, on MacBook Pro, Core i5/2.6 GHz, Node v0.10.26). 
 
110
Note: your results may vary, so please always check on your hardware.
 
111
 
 
112
    operation             iconv@2.1.4   iconv-lite@0.4.0
 
113
    ----------------------------------------------------------
 
114
    encode('win1251')     ~130 Mb/s     ~380 Mb/s
 
115
    decode('win1251')     ~127 Mb/s     ~210 Mb/s
 
116
 
 
117
 
 
118
## Notes
 
119
 
 
120
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).  
 
121
Untranslatable characters are set to � or ?. No transliteration is currently supported.
 
122
 
 
123
## Testing
 
124
 
 
125
```bash
 
126
$ git clone git@github.com:ashtuchkin/iconv-lite.git
 
127
$ cd iconv-lite
 
128
$ npm install
 
129
$ npm test
 
130
    
 
131
$ # To view performance:
 
132
$ node test/performance.js
 
133
```
 
134
 
 
135
## Adoption
 
136
[![NPM](https://nodei.co/npm-dl/iconv-lite.png)](https://nodei.co/npm/iconv-lite/)
 
137