index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var iconv = module.exports;
  2. // All codecs and aliases are kept here, keyed by encoding name/alias.
  3. // They are lazy loaded in `iconv.getCodec` from `encodings/index.js`.
  4. iconv.encodings = null;
  5. // Characters emitted in case of error.
  6. iconv.defaultCharUnicode = '�';
  7. iconv.defaultCharSingleByte = '?';
  8. // Public API.
  9. iconv.encode = function encode(str, encoding, options) {
  10. str = "" + (str || ""); // Ensure string.
  11. var encoder = iconv.getCodec(encoding).encoder(options);
  12. var res = encoder.write(str);
  13. var trail = encoder.end();
  14. return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res;
  15. }
  16. iconv.decode = function decode(buf, encoding, options) {
  17. if (typeof buf === 'string') {
  18. if (!iconv.skipDecodeWarning) {
  19. console.error('Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding');
  20. iconv.skipDecodeWarning = true;
  21. }
  22. buf = new Buffer("" + (buf || ""), "binary"); // Ensure buffer.
  23. }
  24. var decoder = iconv.getCodec(encoding).decoder(options);
  25. var res = decoder.write(buf);
  26. var trail = decoder.end();
  27. return (trail && trail.length > 0) ? (res + trail) : res;
  28. }
  29. iconv.encodingExists = function encodingExists(enc) {
  30. try {
  31. iconv.getCodec(enc);
  32. return true;
  33. } catch (e) {
  34. return false;
  35. }
  36. }
  37. // Legacy aliases to convert functions
  38. iconv.toEncoding = iconv.encode;
  39. iconv.fromEncoding = iconv.decode;
  40. // Search for a codec in iconv.encodings. Cache codec data in iconv._codecDataCache.
  41. iconv._codecDataCache = {};
  42. iconv.getCodec = function getCodec(encoding) {
  43. if (!iconv.encodings)
  44. iconv.encodings = require("../encodings"); // Lazy load all encoding definitions.
  45. // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
  46. var enc = (''+encoding).toLowerCase().replace(/[^0-9a-z]|:\d{4}$/g, "");
  47. // Traverse iconv.encodings to find actual codec.
  48. var codecData, codecOptions;
  49. while (true) {
  50. codecData = iconv._codecDataCache[enc];
  51. if (codecData)
  52. return codecData;
  53. var codec = iconv.encodings[enc];
  54. switch (typeof codec) {
  55. case "string": // Direct alias to other encoding.
  56. enc = codec;
  57. break;
  58. case "object": // Alias with options. Can be layered.
  59. if (!codecOptions) {
  60. codecOptions = codec;
  61. codecOptions.encodingName = enc;
  62. }
  63. else {
  64. for (var key in codec)
  65. codecOptions[key] = codec[key];
  66. }
  67. enc = codec.type;
  68. break;
  69. case "function": // Codec itself.
  70. if (!codecOptions)
  71. codecOptions = { encodingName: enc };
  72. codecOptions.iconv = iconv;
  73. // The codec function must load all tables and return object with .encoder and .decoder methods.
  74. // It'll be called only once (for each different options object).
  75. codecData = codec.call(iconv.encodings, codecOptions);
  76. iconv._codecDataCache[codecOptions.encodingName] = codecData; // Save it to be reused later.
  77. return codecData;
  78. default:
  79. throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
  80. }
  81. }
  82. }
  83. // Load extensions in Node. All of them are omitted in Browserify build via 'browser' field in package.json.
  84. var nodeVer = typeof process !== 'undefined' && process.versions && process.versions.node;
  85. if (nodeVer) {
  86. // Load streaming support in Node v0.10+
  87. var nodeVerArr = nodeVer.split(".").map(Number);
  88. if (nodeVerArr[0] > 0 || nodeVerArr[1] >= 10) {
  89. require("./streams")(iconv);
  90. }
  91. // Load Node primitive extensions.
  92. require("./extend-node")(iconv);
  93. }