3 // Some environments don't have global Buffer (e.g. React Native).
4 // Solution would be installing npm modules "buffer" and "stream" explicitly.
5 var Buffer = require("safer-buffer").Buffer;
7 var bomHandling = require("./bom-handling"),
8 iconv = module.exports;
10 // All codecs and aliases are kept here, keyed by encoding name/alias.
11 // They are lazy loaded in `iconv.getCodec` from `encodings/index.js`.
12 iconv.encodings = null;
14 // Characters emitted in case of error.
15 iconv.defaultCharUnicode = '�';
16 iconv.defaultCharSingleByte = '?';
19 iconv.encode = function encode(str, encoding, options) {
20 str = "" + (str || ""); // Ensure string.
22 var encoder = iconv.getEncoder(encoding, options);
24 var res = encoder.write(str);
25 var trail = encoder.end();
27 return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res;
30 iconv.decode = function decode(buf, encoding, options) {
31 if (typeof buf === 'string') {
32 if (!iconv.skipDecodeWarning) {
33 console.error('Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding');
34 iconv.skipDecodeWarning = true;
37 buf = Buffer.from("" + (buf || ""), "binary"); // Ensure buffer.
40 var decoder = iconv.getDecoder(encoding, options);
42 var res = decoder.write(buf);
43 var trail = decoder.end();
45 return trail ? (res + trail) : res;
48 iconv.encodingExists = function encodingExists(enc) {
57 // Legacy aliases to convert functions
58 iconv.toEncoding = iconv.encode;
59 iconv.fromEncoding = iconv.decode;
61 // Search for a codec in iconv.encodings. Cache codec data in iconv._codecDataCache.
62 iconv._codecDataCache = {};
63 iconv.getCodec = function getCodec(encoding) {
65 iconv.encodings = require("../encodings"); // Lazy load all encoding definitions.
67 // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
68 var enc = iconv._canonicalizeEncoding(encoding);
70 // Traverse iconv.encodings to find actual codec.
71 var codecOptions = {};
73 var codec = iconv._codecDataCache[enc];
77 var codecDef = iconv.encodings[enc];
79 switch (typeof codecDef) {
80 case "string": // Direct alias to other encoding.
84 case "object": // Alias with options. Can be layered.
85 for (var key in codecDef)
86 codecOptions[key] = codecDef[key];
88 if (!codecOptions.encodingName)
89 codecOptions.encodingName = enc;
94 case "function": // Codec itself.
95 if (!codecOptions.encodingName)
96 codecOptions.encodingName = enc;
98 // The codec function must load all tables and return object with .encoder and .decoder methods.
99 // It'll be called only once (for each different options object).
100 codec = new codecDef(codecOptions, iconv);
102 iconv._codecDataCache[codecOptions.encodingName] = codec; // Save it to be reused later.
106 throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
111 iconv._canonicalizeEncoding = function(encoding) {
112 // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
113 return (''+encoding).toLowerCase().replace(/:\d{4}$|[^0-9a-z]/g, "");
116 iconv.getEncoder = function getEncoder(encoding, options) {
117 var codec = iconv.getCodec(encoding),
118 encoder = new codec.encoder(options, codec);
120 if (codec.bomAware && options && options.addBOM)
121 encoder = new bomHandling.PrependBOM(encoder, options);
126 iconv.getDecoder = function getDecoder(encoding, options) {
127 var codec = iconv.getCodec(encoding),
128 decoder = new codec.decoder(options, codec);
130 if (codec.bomAware && !(options && options.stripBOM === false))
131 decoder = new bomHandling.StripBOM(decoder, options);
137 // Load extensions in Node. All of them are omitted in Browserify build via 'browser' field in package.json.
138 var nodeVer = typeof process !== 'undefined' && process.versions && process.versions.node;
141 // Load streaming support in Node v0.10+
142 var nodeVerArr = nodeVer.split(".").map(Number);
143 if (nodeVerArr[0] > 0 || nodeVerArr[1] >= 10) {
144 require("./streams")(iconv);
147 // Load Node primitive extensions.
148 require("./extend-node")(iconv);
151 if ("Ā" != "\u0100") {
152 console.error("iconv-lite warning: javascript files use encoding different from utf-8. See https://github.com/ashtuchkin/iconv-lite/wiki/Javascript-source-file-encodings for more info.");