1 | "use strict"
|
2 |
|
3 | var bomHandling = require('./bom-handling'),
|
4 | iconv = module.exports;
|
5 |
|
6 |
|
7 |
|
8 | iconv.encodings = null;
|
9 |
|
10 |
|
11 | iconv.defaultCharUnicode = '�';
|
12 | iconv.defaultCharSingleByte = '?';
|
13 |
|
14 |
|
15 | iconv.encode = function encode(str, encoding, options) {
|
16 | str = "" + (str || "");
|
17 |
|
18 | var encoder = iconv.getEncoder(encoding, options);
|
19 |
|
20 | var res = encoder.write(str);
|
21 | var trail = encoder.end();
|
22 |
|
23 | return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res;
|
24 | }
|
25 |
|
26 | iconv.decode = function decode(buf, encoding, options) {
|
27 | if (typeof buf === 'string') {
|
28 | if (!iconv.skipDecodeWarning) {
|
29 | console.error('Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding');
|
30 | iconv.skipDecodeWarning = true;
|
31 | }
|
32 |
|
33 | buf = new Buffer("" + (buf || ""), "binary");
|
34 | }
|
35 |
|
36 | var decoder = iconv.getDecoder(encoding, options);
|
37 |
|
38 | var res = decoder.write(buf);
|
39 | var trail = decoder.end();
|
40 |
|
41 | return trail ? (res + trail) : res;
|
42 | }
|
43 |
|
44 | iconv.encodingExists = function encodingExists(enc) {
|
45 | try {
|
46 | iconv.getCodec(enc);
|
47 | return true;
|
48 | } catch (e) {
|
49 | return false;
|
50 | }
|
51 | }
|
52 |
|
53 |
|
54 | iconv.toEncoding = iconv.encode;
|
55 | iconv.fromEncoding = iconv.decode;
|
56 |
|
57 |
|
58 | iconv._codecDataCache = {};
|
59 | iconv.getCodec = function getCodec(encoding) {
|
60 | if (!iconv.encodings)
|
61 | iconv.encodings = require("../encodings");
|
62 |
|
63 |
|
64 | var enc = (''+encoding).toLowerCase().replace(/[^0-9a-z]|:\d{4}$/g, "");
|
65 |
|
66 |
|
67 | var codecOptions = {};
|
68 | while (true) {
|
69 | var codec = iconv._codecDataCache[enc];
|
70 | if (codec)
|
71 | return codec;
|
72 |
|
73 | var codecDef = iconv.encodings[enc];
|
74 |
|
75 | switch (typeof codecDef) {
|
76 | case "string":
|
77 | enc = codecDef;
|
78 | break;
|
79 |
|
80 | case "object":
|
81 | for (var key in codecDef)
|
82 | codecOptions[key] = codecDef[key];
|
83 |
|
84 | if (!codecOptions.encodingName)
|
85 | codecOptions.encodingName = enc;
|
86 |
|
87 | enc = codecDef.type;
|
88 | break;
|
89 |
|
90 | case "function":
|
91 | if (!codecOptions.encodingName)
|
92 | codecOptions.encodingName = enc;
|
93 |
|
94 |
|
95 |
|
96 | codec = new codecDef(codecOptions, iconv);
|
97 |
|
98 | iconv._codecDataCache[codecOptions.encodingName] = codec;
|
99 | return codec;
|
100 |
|
101 | default:
|
102 | throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
|
103 | }
|
104 | }
|
105 | }
|
106 |
|
107 | iconv.getEncoder = function getEncoder(encoding, options) {
|
108 | var codec = iconv.getCodec(encoding),
|
109 | encoder = new codec.encoder(options, codec);
|
110 |
|
111 | if (codec.bomAware && options && options.addBOM)
|
112 | encoder = new bomHandling.PrependBOM(encoder, options);
|
113 |
|
114 | return encoder;
|
115 | }
|
116 |
|
117 | iconv.getDecoder = function getDecoder(encoding, options) {
|
118 | var codec = iconv.getCodec(encoding),
|
119 | decoder = new codec.decoder(options, codec);
|
120 |
|
121 | if (codec.bomAware && !(options && options.stripBOM === false))
|
122 | decoder = new bomHandling.StripBOM(decoder, options);
|
123 |
|
124 | return decoder;
|
125 | }
|
126 |
|
127 |
|
128 |
|
129 | var nodeVer = typeof process !== 'undefined' && process.versions && process.versions.node;
|
130 | if (nodeVer) {
|
131 |
|
132 |
|
133 | var nodeVerArr = nodeVer.split(".").map(Number);
|
134 | if (nodeVerArr[0] > 0 || nodeVerArr[1] >= 10) {
|
135 | require("./streams")(iconv);
|
136 | }
|
137 |
|
138 |
|
139 | require("./extend-node")(iconv);
|
140 | }
|
141 |
|