Merge "LOG SQL dump files getting installed"
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / body-parser / node_modules / iconv-lite / lib / index.js
1
2 var iconv = module.exports;
3
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;
7
8 // Characters emitted in case of error.
9 iconv.defaultCharUnicode = '�';
10 iconv.defaultCharSingleByte = '?';
11
12 // Public API.
13 iconv.encode = function encode(str, encoding, options) {
14     str = "" + (str || ""); // Ensure string.
15
16     var encoder = iconv.getCodec(encoding).encoder(options);
17
18     var res = encoder.write(str);
19     var trail = encoder.end();
20     
21     return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res;
22 }
23
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;
29         }
30
31         buf = new Buffer("" + (buf || ""), "binary"); // Ensure buffer.
32     }
33
34     var decoder = iconv.getCodec(encoding).decoder(options);
35
36     var res = decoder.write(buf);
37     var trail = decoder.end();
38
39     return (trail && trail.length > 0) ? (res + trail) : res;
40 }
41
42 iconv.encodingExists = function encodingExists(enc) {
43     try {
44         iconv.getCodec(enc);
45         return true;
46     } catch (e) {
47         return false;
48     }
49 }
50
51 // Legacy aliases to convert functions
52 iconv.toEncoding = iconv.encode;
53 iconv.fromEncoding = iconv.decode;
54
55 // Search for a codec in iconv.encodings. Cache codec data in iconv._codecDataCache.
56 iconv._codecDataCache = {};
57 iconv.getCodec = function getCodec(encoding) {
58     if (!iconv.encodings)
59         iconv.encodings = require("../encodings"); // Lazy load all encoding definitions.
60     
61     // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
62     var enc = (''+encoding).toLowerCase().replace(/[^0-9a-z]|:\d{4}$/g, "");
63
64     // Traverse iconv.encodings to find actual codec.
65     var codecData, codecOptions;
66     while (true) {
67         codecData = iconv._codecDataCache[enc];
68         if (codecData)
69             return codecData;
70
71         var codec = iconv.encodings[enc];
72
73         switch (typeof codec) {
74             case "string": // Direct alias to other encoding.
75                 enc = codec;
76                 break;
77
78             case "object": // Alias with options. Can be layered.
79                 if (!codecOptions) {
80                     codecOptions = codec;
81                     codecOptions.encodingName = enc;
82                 }
83                 else {
84                     for (var key in codec)
85                         codecOptions[key] = codec[key];
86                 }
87
88                 enc = codec.type;
89                 break;
90
91             case "function": // Codec itself.
92                 if (!codecOptions)
93                     codecOptions = { encodingName: enc };
94                 codecOptions.iconv = iconv;
95
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);
99
100                 iconv._codecDataCache[codecOptions.encodingName] = codecData; // Save it to be reused later.
101                 return codecData;
102
103             default:
104                 throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
105         }
106     }
107 }
108
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;
111 if (nodeVer) {
112
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);
117     }
118
119     // Load Node primitive extensions.
120     require("./extend-node")(iconv);
121 }
122