1 | "use strict";
|
2 |
|
3 | var Buffer = require("safer-buffer").Buffer;
|
4 |
|
5 | var bomHandling = require("./bom-handling"),
|
6 | iconv = module.exports;
|
7 |
|
8 |
|
9 |
|
10 | iconv.encodings = null;
|
11 |
|
12 |
|
13 | iconv.defaultCharUnicode = '�';
|
14 | iconv.defaultCharSingleByte = '?';
|
15 |
|
16 |
|
17 | iconv.encode = function encode(str, encoding, options) {
|
18 | str = "" + (str || "");
|
19 |
|
20 | var encoder = iconv.getEncoder(encoding, options);
|
21 |
|
22 | var res = encoder.write(str);
|
23 | var trail = encoder.end();
|
24 |
|
25 | return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res;
|
26 | }
|
27 |
|
28 | iconv.decode = function decode(buf, encoding, options) {
|
29 | if (typeof buf === 'string') {
|
30 | if (!iconv.skipDecodeWarning) {
|
31 | console.error('Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding');
|
32 | iconv.skipDecodeWarning = true;
|
33 | }
|
34 |
|
35 | buf = Buffer.from("" + (buf || ""), "binary");
|
36 | }
|
37 |
|
38 | var decoder = iconv.getDecoder(encoding, options);
|
39 |
|
40 | var res = decoder.write(buf);
|
41 | var trail = decoder.end();
|
42 |
|
43 | return trail ? (res + trail) : res;
|
44 | }
|
45 |
|
46 | iconv.encodingExists = function encodingExists(enc) {
|
47 | try {
|
48 | iconv.getCodec(enc);
|
49 | return true;
|
50 | } catch (e) {
|
51 | return false;
|
52 | }
|
53 | }
|
54 |
|
55 |
|
56 | iconv.toEncoding = iconv.encode;
|
57 | iconv.fromEncoding = iconv.decode;
|
58 |
|
59 |
|
60 | iconv._codecDataCache = {};
|
61 | iconv.getCodec = function getCodec(encoding) {
|
62 | if (!iconv.encodings)
|
63 | iconv.encodings = require("../encodings");
|
64 |
|
65 |
|
66 | var enc = iconv._canonicalizeEncoding(encoding);
|
67 |
|
68 |
|
69 | var codecOptions = {};
|
70 | while (true) {
|
71 | var codec = iconv._codecDataCache[enc];
|
72 | if (codec)
|
73 | return codec;
|
74 |
|
75 | var codecDef = iconv.encodings[enc];
|
76 |
|
77 | switch (typeof codecDef) {
|
78 | case "string":
|
79 | enc = codecDef;
|
80 | break;
|
81 |
|
82 | case "object":
|
83 | for (var key in codecDef)
|
84 | codecOptions[key] = codecDef[key];
|
85 |
|
86 | if (!codecOptions.encodingName)
|
87 | codecOptions.encodingName = enc;
|
88 |
|
89 | enc = codecDef.type;
|
90 | break;
|
91 |
|
92 | case "function":
|
93 | if (!codecOptions.encodingName)
|
94 | codecOptions.encodingName = enc;
|
95 |
|
96 |
|
97 |
|
98 | codec = new codecDef(codecOptions, iconv);
|
99 |
|
100 | iconv._codecDataCache[codecOptions.encodingName] = codec;
|
101 | return codec;
|
102 |
|
103 | default:
|
104 | throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
|
105 | }
|
106 | }
|
107 | }
|
108 |
|
109 | iconv._canonicalizeEncoding = function(encoding) {
|
110 |
|
111 | return (''+encoding).toLowerCase().replace(/:\d{4}$|[^0-9a-z]/g, "");
|
112 | }
|
113 |
|
114 | iconv.getEncoder = function getEncoder(encoding, options) {
|
115 | var codec = iconv.getCodec(encoding),
|
116 | encoder = new codec.encoder(options, codec);
|
117 |
|
118 | if (codec.bomAware && options && options.addBOM)
|
119 | encoder = new bomHandling.PrependBOM(encoder, options);
|
120 |
|
121 | return encoder;
|
122 | }
|
123 |
|
124 | iconv.getDecoder = function getDecoder(encoding, options) {
|
125 | var codec = iconv.getCodec(encoding),
|
126 | decoder = new codec.decoder(options, codec);
|
127 |
|
128 | if (codec.bomAware && !(options && options.stripBOM === false))
|
129 | decoder = new bomHandling.StripBOM(decoder, options);
|
130 |
|
131 | return decoder;
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | iconv.enableStreamingAPI = function enableStreamingAPI(stream_module) {
|
140 | if (iconv.supportsStreams)
|
141 | return;
|
142 |
|
143 |
|
144 | var streams = require("./streams")(stream_module);
|
145 |
|
146 |
|
147 | iconv.IconvLiteEncoderStream = streams.IconvLiteEncoderStream;
|
148 | iconv.IconvLiteDecoderStream = streams.IconvLiteDecoderStream;
|
149 |
|
150 |
|
151 | iconv.encodeStream = function encodeStream(encoding, options) {
|
152 | return new iconv.IconvLiteEncoderStream(iconv.getEncoder(encoding, options), options);
|
153 | }
|
154 |
|
155 | iconv.decodeStream = function decodeStream(encoding, options) {
|
156 | return new iconv.IconvLiteDecoderStream(iconv.getDecoder(encoding, options), options);
|
157 | }
|
158 |
|
159 | iconv.supportsStreams = true;
|
160 | }
|
161 |
|
162 |
|
163 | var stream_module;
|
164 | try {
|
165 | stream_module = require("stream");
|
166 | } catch (e) {}
|
167 |
|
168 | if (stream_module && stream_module.Transform) {
|
169 | iconv.enableStreamingAPI(stream_module);
|
170 |
|
171 | } else {
|
172 |
|
173 | iconv.encodeStream = iconv.decodeStream = function() {
|
174 | throw new Error("iconv-lite Streaming API is not enabled. Use iconv.enableStreamingAPI(require('stream')); to enable it.");
|
175 | };
|
176 | }
|
177 |
|
178 | if ("Ā" != "\u0100") {
|
179 | console.error("iconv-lite warning: js files use non-utf8 encoding. See https://github.com/ashtuchkin/iconv-lite/wiki/Javascript-source-file-encodings for more info.");
|
180 | }
|