2 var iconv = module.exports;
4 // All codecs and aliases are kept here, keyed by encoding name/alias.
5 // They are lazy loaded in `iconv.getCodec` from `encodings/index.js`.
6 iconv.encodings = null;
8 // Characters emitted in case of error.
9 iconv.defaultCharUnicode = '�';
10 iconv.defaultCharSingleByte = '?';
13 iconv.encode = function encode(str, encoding, options) {
14 str = "" + (str || ""); // Ensure string.
16 var encoder = iconv.getCodec(encoding).encoder(options);
18 var res = encoder.write(str);
19 var trail = encoder.end();
21 return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res;
24 iconv.decode = function decode(buf, encoding, options) {
25 if (typeof buf === 'string') {
26 if (!iconv.skipDecodeWarning) {
27 console.error('Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding');
28 iconv.skipDecodeWarning = true;
31 buf = new Buffer("" + (buf || ""), "binary"); // Ensure buffer.
34 var decoder = iconv.getCodec(encoding).decoder(options);
36 var res = decoder.write(buf);
37 var trail = decoder.end();
39 return (trail && trail.length > 0) ? (res + trail) : res;
42 iconv.encodingExists = function encodingExists(enc) {
51 // Legacy aliases to convert functions
52 iconv.toEncoding = iconv.encode;
53 iconv.fromEncoding = iconv.decode;
55 // Search for a codec in iconv.encodings. Cache codec data in iconv._codecDataCache.
56 iconv._codecDataCache = {};
57 iconv.getCodec = function getCodec(encoding) {
59 iconv.encodings = require("../encodings"); // Lazy load all encoding definitions.
61 // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
62 var enc = (''+encoding).toLowerCase().replace(/[^0-9a-z]|:\d{4}$/g, "");
64 // Traverse iconv.encodings to find actual codec.
65 var codecData, codecOptions;
67 codecData = iconv._codecDataCache[enc];
71 var codec = iconv.encodings[enc];
73 switch (typeof codec) {
74 case "string": // Direct alias to other encoding.
78 case "object": // Alias with options. Can be layered.
81 codecOptions.encodingName = enc;
84 for (var key in codec)
85 codecOptions[key] = codec[key];
91 case "function": // Codec itself.
93 codecOptions = { encodingName: enc };
94 codecOptions.iconv = iconv;
96 // The codec function must load all tables and return object with .encoder and .decoder methods.
97 // It'll be called only once (for each different options object).
98 codecData = codec.call(iconv.encodings, codecOptions);
100 iconv._codecDataCache[codecOptions.encodingName] = codecData; // Save it to be reused later.
104 throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
109 // Load extensions in Node. All of them are omitted in Browserify build via 'browser' field in package.json.
110 var nodeVer = typeof process !== 'undefined' && process.versions && process.versions.node;
113 // Load streaming support in Node v0.10+
114 var nodeVerArr = nodeVer.split(".").map(Number);
115 if (nodeVerArr[0] > 0 || nodeVerArr[1] >= 10) {
116 require("./streams")(iconv);
119 // Load Node primitive extensions.
120 require("./extend-node")(iconv);