index.js 4.8 KB

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