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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. "use strict";
  2. var Buffer = require("safer-buffer").Buffer;
  3. var bomHandling = require("./bom-handling"),
  4. iconv = module.exports;
  5. // All codecs and aliases are kept here, keyed by encoding name/alias.
  6. // They are lazy loaded in `iconv.getCodec` from `encodings/index.js`.
  7. iconv.encodings = null;
  8. // Characters emitted in case of error.
  9. iconv.defaultCharUnicode = '�';
  10. iconv.defaultCharSingleByte = '?';
  11. // Public API.
  12. iconv.encode = function encode(str, encoding, options) {
  13. str = "" + (str || ""); // Ensure string.
  14. var encoder = iconv.getEncoder(encoding, options);
  15. var res = encoder.write(str);
  16. var trail = encoder.end();
  17. return (trail && trail.length > 0) ? Buffer.concat([res, trail]) : res;
  18. }
  19. iconv.decode = function decode(buf, encoding, options) {
  20. if (typeof buf === 'string') {
  21. if (!iconv.skipDecodeWarning) {
  22. console.error('Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding');
  23. iconv.skipDecodeWarning = true;
  24. }
  25. buf = Buffer.from("" + (buf || ""), "binary"); // Ensure buffer.
  26. }
  27. var decoder = iconv.getDecoder(encoding, options);
  28. var res = decoder.write(buf);
  29. var trail = decoder.end();
  30. return trail ? (res + trail) : res;
  31. }
  32. iconv.encodingExists = function encodingExists(enc) {
  33. try {
  34. iconv.getCodec(enc);
  35. return true;
  36. } catch (e) {
  37. return false;
  38. }
  39. }
  40. // Legacy aliases to convert functions
  41. iconv.toEncoding = iconv.encode;
  42. iconv.fromEncoding = iconv.decode;
  43. // Search for a codec in iconv.encodings. Cache codec data in iconv._codecDataCache.
  44. iconv._codecDataCache = {};
  45. iconv.getCodec = function getCodec(encoding) {
  46. if (!iconv.encodings)
  47. iconv.encodings = require("../encodings"); // Lazy load all encoding definitions.
  48. // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
  49. var enc = iconv._canonicalizeEncoding(encoding);
  50. // Traverse iconv.encodings to find actual codec.
  51. var codecOptions = {};
  52. while (true) {
  53. var codec = iconv._codecDataCache[enc];
  54. if (codec)
  55. return codec;
  56. var codecDef = iconv.encodings[enc];
  57. switch (typeof codecDef) {
  58. case "string": // Direct alias to other encoding.
  59. enc = codecDef;
  60. break;
  61. case "object": // Alias with options. Can be layered.
  62. for (var key in codecDef)
  63. codecOptions[key] = codecDef[key];
  64. if (!codecOptions.encodingName)
  65. codecOptions.encodingName = enc;
  66. enc = codecDef.type;
  67. break;
  68. case "function": // Codec itself.
  69. if (!codecOptions.encodingName)
  70. codecOptions.encodingName = enc;
  71. // The codec function must load all tables and return object with .encoder and .decoder methods.
  72. // It'll be called only once (for each different options object).
  73. codec = new codecDef(codecOptions, iconv);
  74. iconv._codecDataCache[codecOptions.encodingName] = codec; // Save it to be reused later.
  75. return codec;
  76. default:
  77. throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
  78. }
  79. }
  80. }
  81. iconv._canonicalizeEncoding = function(encoding) {
  82. // Canonicalize encoding name: strip all non-alphanumeric chars and appended year.
  83. return (''+encoding).toLowerCase().replace(/:\d{4}$|[^0-9a-z]/g, "");
  84. }
  85. iconv.getEncoder = function getEncoder(encoding, options) {
  86. var codec = iconv.getCodec(encoding),
  87. encoder = new codec.encoder(options, codec);
  88. if (codec.bomAware && options && options.addBOM)
  89. encoder = new bomHandling.PrependBOM(encoder, options);
  90. return encoder;
  91. }
  92. iconv.getDecoder = function getDecoder(encoding, options) {
  93. var codec = iconv.getCodec(encoding),
  94. decoder = new codec.decoder(options, codec);
  95. if (codec.bomAware && !(options && options.stripBOM === false))
  96. decoder = new bomHandling.StripBOM(decoder, options);
  97. return decoder;
  98. }
  99. // Streaming API
  100. // NOTE: Streaming API naturally depends on 'stream' module from Node.js. Unfortunately in browser environments this module can add
  101. // up to 100Kb to the output bundle. To avoid unnecessary code bloat, we don't enable Streaming API in browser by default.
  102. // If you would like to enable it explicitly, please add the following code to your app:
  103. // > iconv.enableStreamingAPI(require('stream'));
  104. iconv.enableStreamingAPI = function enableStreamingAPI(stream_module) {
  105. if (iconv.supportsStreams)
  106. return;
  107. // Dependency-inject stream module to create IconvLite stream classes.
  108. var streams = require("./streams")(stream_module);
  109. // Not public API yet, but expose the stream classes.
  110. iconv.IconvLiteEncoderStream = streams.IconvLiteEncoderStream;
  111. iconv.IconvLiteDecoderStream = streams.IconvLiteDecoderStream;
  112. // Streaming API.
  113. iconv.encodeStream = function encodeStream(encoding, options) {
  114. return new iconv.IconvLiteEncoderStream(iconv.getEncoder(encoding, options), options);
  115. }
  116. iconv.decodeStream = function decodeStream(encoding, options) {
  117. return new iconv.IconvLiteDecoderStream(iconv.getDecoder(encoding, options), options);
  118. }
  119. iconv.supportsStreams = true;
  120. }
  121. // Enable Streaming API automatically if 'stream' module is available and non-empty (the majority of environments).
  122. var stream_module;
  123. try {
  124. stream_module = require("stream");
  125. } catch (e) {}
  126. if (stream_module && stream_module.Transform) {
  127. iconv.enableStreamingAPI(stream_module);
  128. } else {
  129. // In rare cases where 'stream' module is not available by default, throw a helpful exception.
  130. iconv.encodeStream = iconv.decodeStream = function() {
  131. throw new Error("iconv-lite Streaming API is not enabled. Use iconv.enableStreamingAPI(require('stream')); to enable it.");
  132. };
  133. }
  134. if ("Ā" != "\u0100") {
  135. console.error("iconv-lite warning: js files use non-utf8 encoding. See https://github.com/ashtuchkin/iconv-lite/wiki/Javascript-source-file-encodings for more info.");
  136. }