You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 5.0KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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("safer-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 = Buffer.from("" + (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 = iconv._canonicalizeEncoding(encoding);
  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._canonicalizeEncoding = function(encoding) {
  84. // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
  85. return (''+encoding).toLowerCase().replace(/:\d{4}$|[^0-9a-z]/g, "");
  86. }
  87. iconv.getEncoder = function getEncoder(encoding, options) {
  88. var codec = iconv.getCodec(encoding),
  89. encoder = new codec.encoder(options, codec);
  90. if (codec.bomAware && options && options.addBOM)
  91. encoder = new bomHandling.PrependBOM(encoder, options);
  92. return encoder;
  93. }
  94. iconv.getDecoder = function getDecoder(encoding, options) {
  95. var codec = iconv.getCodec(encoding),
  96. decoder = new codec.decoder(options, codec);
  97. if (codec.bomAware && !(options && options.stripBOM === false))
  98. decoder = new bomHandling.StripBOM(decoder, options);
  99. return decoder;
  100. }
  101. // Load extensions in Node. All of them are omitted in Browserify build via 'browser' field in package.json.
  102. var nodeVer = typeof process !== 'undefined' && process.versions && process.versions.node;
  103. if (nodeVer) {
  104. // Load streaming support in Node v0.10+
  105. var nodeVerArr = nodeVer.split(".").map(Number);
  106. if (nodeVerArr[0] > 0 || nodeVerArr[1] >= 10) {
  107. require("./streams")(iconv);
  108. }
  109. // Load Node primitive extensions.
  110. require("./extend-node")(iconv);
  111. }
  112. if ("Ā" != "\u0100") {
  113. 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.");
  114. }