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.

47 lines
1.3 KiB

  1. "use strict";
  2. const iconvLite = require("iconv-lite");
  3. const supportedNames = require("./supported-names.json");
  4. const labelsToNames = require("./labels-to-names.json");
  5. const supportedNamesSet = new Set(supportedNames);
  6. // https://encoding.spec.whatwg.org/#concept-encoding-get
  7. exports.labelToName = label => {
  8. label = String(label).trim().toLowerCase();
  9. return labelsToNames[label] || null;
  10. };
  11. // https://encoding.spec.whatwg.org/#decode
  12. exports.decode = (buffer, fallbackEncodingName) => {
  13. let encoding = fallbackEncodingName;
  14. if (!exports.isSupported(encoding)) {
  15. throw new RangeError(`"${encoding}" is not a supported encoding name`);
  16. }
  17. const bomEncoding = exports.getBOMEncoding(buffer);
  18. if (bomEncoding !== null) {
  19. encoding = bomEncoding;
  20. }
  21. // iconv-lite will strip BOMs for us, so no need to do the stuff the spec does
  22. return iconvLite.decode(buffer, encoding);
  23. };
  24. // https://github.com/whatwg/html/issues/1910#issuecomment-254017369
  25. exports.getBOMEncoding = buffer => {
  26. if (buffer[0] === 0xFE && buffer[1] === 0xFF) {
  27. return "UTF-16BE";
  28. } else if (buffer[0] === 0xFF && buffer[1] === 0xFE) {
  29. return "UTF-16LE";
  30. } else if (buffer[0] === 0xEF && buffer[1] === 0xBB && buffer[2] === 0xBF) {
  31. return "UTF-8";
  32. }
  33. return null;
  34. };
  35. exports.isSupported = name => {
  36. return supportedNamesSet.has(String(name));
  37. };