3 var bomHandling = require('./bom-handling'),
4 iconv = module.exports;
6 // All codecs and aliases are kept here, keyed by encoding name/alias.
7 // They are lazy loaded in `iconv.getCodec` from `encodings/index.js`.
8 iconv.encodings = null;
10 // Characters emitted in case of error.
11 iconv.defaultCharUnicode = '�';
12 iconv.defaultCharSingleByte = '?';
15 iconv.encode = function encode(str, encoding, options) {
16 str = "" + (str || ""); // Ensure string.
18 var encoder = iconv.getEncoder(encoding, options);
20 var res = encoder.write(str);
21 var trail = encoder.end();
23 return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res;
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;
33 buf = new Buffer("" + (buf || ""), "binary"); // Ensure buffer.
36 var decoder = iconv.getDecoder(encoding, options);
38 var res = decoder.write(buf);
39 var trail = decoder.end();
41 return trail ? (res + trail) : res;
44 iconv.encodingExists = function encodingExists(enc) {
53 // Legacy aliases to convert functions
54 iconv.toEncoding = iconv.encode;
55 iconv.fromEncoding = iconv.decode;
57 // Search for a codec in iconv.encodings. Cache codec data in iconv._codecDataCache.
58 iconv._codecDataCache = {};
59 iconv.getCodec = function getCodec(encoding) {
61 iconv.encodings = require("../encodings"); // Lazy load all encoding definitions.
63 // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
64 var enc = (''+encoding).toLowerCase().replace(/[^0-9a-z]|:\d{4}$/g, "");
66 // Traverse iconv.encodings to find actual codec.
67 var codecOptions = {};
69 var codec = iconv._codecDataCache[enc];
73 var codecDef = iconv.encodings[enc];
75 switch (typeof codecDef) {
76 case "string": // Direct alias to other encoding.
80 case "object": // Alias with options. Can be layered.
81 for (var key in codecDef)
82 codecOptions[key] = codecDef[key];
84 if (!codecOptions.encodingName)
85 codecOptions.encodingName = enc;
90 case "function": // Codec itself.
91 if (!codecOptions.encodingName)
92 codecOptions.encodingName = enc;
94 // The codec function must load all tables and return object with .encoder and .decoder methods.
95 // It'll be called only once (for each different options object).
96 codec = new codecDef(codecOptions, iconv);
98 iconv._codecDataCache[codecOptions.encodingName] = codec; // Save it to be reused later.
102 throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
107 iconv.getEncoder = function getEncoder(encoding, options) {
108 var codec = iconv.getCodec(encoding),
109 encoder = new codec.encoder(options, codec);
111 if (codec.bomAware && options && options.addBOM)
112 encoder = new bomHandling.PrependBOM(encoder, options);
117 iconv.getDecoder = function getDecoder(encoding, options) {
118 var codec = iconv.getCodec(encoding),
119 decoder = new codec.decoder(options, codec);
121 if (codec.bomAware && !(options && options.stripBOM === false))
122 decoder = new bomHandling.StripBOM(decoder, options);
128 // Load extensions in Node. All of them are omitted in Browserify build via 'browser' field in package.json.
129 var nodeVer = typeof process !== 'undefined' && process.versions && process.versions.node;
132 // Load streaming support in Node v0.10+
133 var nodeVerArr = nodeVer.split(".").map(Number);
134 if (nodeVerArr[0] > 0 || nodeVerArr[1] >= 10) {
135 require("./streams")(iconv);
138 // Load Node primitive extensions.
139 require("./extend-node")(iconv);