detector.js 817 B

123456789101112131415161718192021222324252627282930
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.detector = void 0;
  4. const index_1 = require("./types/index");
  5. const keys = Object.keys(index_1.typeHandlers);
  6. // This map helps avoid validating for every single image type
  7. const firstBytes = {
  8. 0x38: 'psd',
  9. 0x42: 'bmp',
  10. 0x44: 'dds',
  11. 0x47: 'gif',
  12. 0x49: 'tiff',
  13. 0x4d: 'tiff',
  14. 0x52: 'webp',
  15. 0x69: 'icns',
  16. 0x89: 'png',
  17. 0xff: 'jpg',
  18. };
  19. function detector(input) {
  20. const byte = input[0];
  21. if (byte in firstBytes) {
  22. const type = firstBytes[byte];
  23. if (type && index_1.typeHandlers[type].validate(input)) {
  24. return type;
  25. }
  26. }
  27. const finder = (key) => index_1.typeHandlers[key].validate(input);
  28. return keys.find(finder);
  29. }
  30. exports.detector = detector;