wechat wss demo

encoding.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict';
  2. var iconvLite = require('iconv-lite');
  3. // Load Iconv from an external file to be able to disable Iconv for webpack
  4. // Add /\/iconv-loader$/ to webpack.IgnorePlugin to ignore it
  5. var Iconv = require('./iconv-loader');
  6. // Expose to the world
  7. module.exports.convert = convert;
  8. /**
  9. * Convert encoding of an UTF-8 string or a buffer
  10. *
  11. * @param {String|Buffer} str String to be converted
  12. * @param {String} to Encoding to be converted to
  13. * @param {String} [from='UTF-8'] Encoding to be converted from
  14. * @param {Boolean} useLite If set to ture, force to use iconvLite
  15. * @return {Buffer} Encoded string
  16. */
  17. function convert(str, to, from, useLite) {
  18. from = checkEncoding(from || 'UTF-8');
  19. to = checkEncoding(to || 'UTF-8');
  20. str = str || '';
  21. var result;
  22. if (from !== 'UTF-8' && typeof str === 'string') {
  23. str = new Buffer(str, 'binary');
  24. }
  25. if (from === to) {
  26. if (typeof str === 'string') {
  27. result = new Buffer(str);
  28. } else {
  29. result = str;
  30. }
  31. } else if (Iconv && !useLite) {
  32. try {
  33. result = convertIconv(str, to, from);
  34. } catch (E) {
  35. console.error(E);
  36. try {
  37. result = convertIconvLite(str, to, from);
  38. } catch (E) {
  39. console.error(E);
  40. result = str;
  41. }
  42. }
  43. } else {
  44. try {
  45. result = convertIconvLite(str, to, from);
  46. } catch (E) {
  47. console.error(E);
  48. result = str;
  49. }
  50. }
  51. if (typeof result === 'string') {
  52. result = new Buffer(result, 'utf-8');
  53. }
  54. return result;
  55. }
  56. /**
  57. * Convert encoding of a string with node-iconv (if available)
  58. *
  59. * @param {String|Buffer} str String to be converted
  60. * @param {String} to Encoding to be converted to
  61. * @param {String} [from='UTF-8'] Encoding to be converted from
  62. * @return {Buffer} Encoded string
  63. */
  64. function convertIconv(str, to, from) {
  65. var response, iconv;
  66. iconv = new Iconv(from, to + '//TRANSLIT//IGNORE');
  67. response = iconv.convert(str);
  68. return response.slice(0, response.length);
  69. }
  70. /**
  71. * Convert encoding of astring with iconv-lite
  72. *
  73. * @param {String|Buffer} str String to be converted
  74. * @param {String} to Encoding to be converted to
  75. * @param {String} [from='UTF-8'] Encoding to be converted from
  76. * @return {Buffer} Encoded string
  77. */
  78. function convertIconvLite(str, to, from) {
  79. if (to === 'UTF-8') {
  80. return iconvLite.decode(str, from);
  81. } else if (from === 'UTF-8') {
  82. return iconvLite.encode(str, to);
  83. } else {
  84. return iconvLite.encode(iconvLite.decode(str, from), to);
  85. }
  86. }
  87. /**
  88. * Converts charset name if needed
  89. *
  90. * @param {String} name Character set
  91. * @return {String} Character set name
  92. */
  93. function checkEncoding(name) {
  94. return (name || '').toString().trim().
  95. replace(/^latin[\-_]?(\d+)$/i, 'ISO-8859-$1').
  96. replace(/^win(?:dows)?[\-_]?(\d+)$/i, 'WINDOWS-$1').
  97. replace(/^utf[\-_]?(\d+)$/i, 'UTF-$1').
  98. replace(/^ks_c_5601\-1987$/i, 'CP949').
  99. replace(/^us[\-_]?ascii$/i, 'ASCII').
  100. toUpperCase();
  101. }