index.js 4.4 KB

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