twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

index.js (5123B)


      1 "use strict";
      2 
      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;
      6 
      7 var bomHandling = require("./bom-handling"),
      8     iconv = module.exports;
      9 
     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;
     13 
     14 // Characters emitted in case of error.
     15 iconv.defaultCharUnicode = '�';
     16 iconv.defaultCharSingleByte = '?';
     17 
     18 // Public API.
     19 iconv.encode = function encode(str, encoding, options) {
     20     str = "" + (str || ""); // Ensure string.
     21 
     22     var encoder = iconv.getEncoder(encoding, options);
     23 
     24     var res = encoder.write(str);
     25     var trail = encoder.end();
     26     
     27     return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res;
     28 }
     29 
     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;
     35         }
     36 
     37         buf = Buffer.from("" + (buf || ""), "binary"); // Ensure buffer.
     38     }
     39 
     40     var decoder = iconv.getDecoder(encoding, options);
     41 
     42     var res = decoder.write(buf);
     43     var trail = decoder.end();
     44 
     45     return trail ? (res + trail) : res;
     46 }
     47 
     48 iconv.encodingExists = function encodingExists(enc) {
     49     try {
     50         iconv.getCodec(enc);
     51         return true;
     52     } catch (e) {
     53         return false;
     54     }
     55 }
     56 
     57 // Legacy aliases to convert functions
     58 iconv.toEncoding = iconv.encode;
     59 iconv.fromEncoding = iconv.decode;
     60 
     61 // Search for a codec in iconv.encodings. Cache codec data in iconv._codecDataCache.
     62 iconv._codecDataCache = {};
     63 iconv.getCodec = function getCodec(encoding) {
     64     if (!iconv.encodings)
     65         iconv.encodings = require("../encodings"); // Lazy load all encoding definitions.
     66     
     67     // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
     68     var enc = iconv._canonicalizeEncoding(encoding);
     69 
     70     // Traverse iconv.encodings to find actual codec.
     71     var codecOptions = {};
     72     while (true) {
     73         var codec = iconv._codecDataCache[enc];
     74         if (codec)
     75             return codec;
     76 
     77         var codecDef = iconv.encodings[enc];
     78 
     79         switch (typeof codecDef) {
     80             case "string": // Direct alias to other encoding.
     81                 enc = codecDef;
     82                 break;
     83 
     84             case "object": // Alias with options. Can be layered.
     85                 for (var key in codecDef)
     86                     codecOptions[key] = codecDef[key];
     87 
     88                 if (!codecOptions.encodingName)
     89                     codecOptions.encodingName = enc;
     90                 
     91                 enc = codecDef.type;
     92                 break;
     93 
     94             case "function": // Codec itself.
     95                 if (!codecOptions.encodingName)
     96                     codecOptions.encodingName = enc;
     97 
     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);
    101 
    102                 iconv._codecDataCache[codecOptions.encodingName] = codec; // Save it to be reused later.
    103                 return codec;
    104 
    105             default:
    106                 throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
    107         }
    108     }
    109 }
    110 
    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, "");
    114 }
    115 
    116 iconv.getEncoder = function getEncoder(encoding, options) {
    117     var codec = iconv.getCodec(encoding),
    118         encoder = new codec.encoder(options, codec);
    119 
    120     if (codec.bomAware && options && options.addBOM)
    121         encoder = new bomHandling.PrependBOM(encoder, options);
    122 
    123     return encoder;
    124 }
    125 
    126 iconv.getDecoder = function getDecoder(encoding, options) {
    127     var codec = iconv.getCodec(encoding),
    128         decoder = new codec.decoder(options, codec);
    129 
    130     if (codec.bomAware && !(options && options.stripBOM === false))
    131         decoder = new bomHandling.StripBOM(decoder, options);
    132 
    133     return decoder;
    134 }
    135 
    136 
    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;
    139 if (nodeVer) {
    140 
    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);
    145     }
    146 
    147     // Load Node primitive extensions.
    148     require("./extend-node")(iconv);
    149 }
    150 
    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.");
    153 }