|
@@ -1,12 +1,20 @@
|
|
|
-var debug = require('debug')('ylt:contentTypeChecker');
|
|
|
-var Q = require('q');
|
|
|
-var FileType = require('file-type');
|
|
|
-var isSvg = require('is-svg');
|
|
|
-var isJson = require('is-json');
|
|
|
+var debug = require('debug')('ylt:contentTypeChecker');
|
|
|
+var Q = require('q');
|
|
|
+var isJpg = require('is-jpg');
|
|
|
+var isPng = require('is-png');
|
|
|
+var isSvg = require('is-svg');
|
|
|
+var isGif = require('is-gif');
|
|
|
+var isWebp = require('is-webp');
|
|
|
+var isWoff = require('is-woff');
|
|
|
+var isWoff2 = require('is-woff2');
|
|
|
+var isOtf = require('is-otf');
|
|
|
+var isTtf = require('is-ttf');
|
|
|
+var isEot = require('is-eot');
|
|
|
+var isJson = require('is-json');
|
|
|
|
|
|
var ContentTypeChecker = function() {
|
|
|
|
|
|
- async function checkContentType(entry) {
|
|
|
+ function checkContentType(entry) {
|
|
|
var deferred = Q.defer();
|
|
|
|
|
|
// Setting isSomething values:
|
|
@@ -47,12 +55,12 @@ var ContentTypeChecker = function() {
|
|
|
var foundType;
|
|
|
|
|
|
try {
|
|
|
- foundType = await findContentType(entry.weightCheck.bodyBuffer);
|
|
|
+ foundType = findContentType(entry.weightCheck.bodyBuffer);
|
|
|
|
|
|
// If it's an image or a font, then rewrite.
|
|
|
if (foundType !== null && (foundType.type === 'image' || foundType.type === 'webfont' || foundType.type === 'json')) {
|
|
|
if (foundType.type !== entry.type) {
|
|
|
- debug('Content type %s is wrong for %s. It should be %s.', entry.type, entry.url, foundType.type);
|
|
|
+ debug('Content type %s is wrong for %s. It should be %s.', entry.type, entry.ulr, foundType.type);
|
|
|
}
|
|
|
rewriteContentType(entry, foundType);
|
|
|
}
|
|
@@ -68,21 +76,52 @@ var ContentTypeChecker = function() {
|
|
|
return deferred.promise;
|
|
|
}
|
|
|
|
|
|
- async function findContentType(bodyBuffer) {
|
|
|
+ function findContentType(bodyBuffer) {
|
|
|
var bodyStr = bodyBuffer.toString();
|
|
|
|
|
|
+ if (isJpg(bodyBuffer)) {
|
|
|
+ return contentTypes.jpeg;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isPng(bodyBuffer)) {
|
|
|
+ return contentTypes.png;
|
|
|
+ }
|
|
|
+
|
|
|
// https://github.com/sindresorhus/is-svg/issues/7
|
|
|
if (/<svg/.test(bodyStr) && isSvg(bodyStr)) {
|
|
|
return contentTypes.svg;
|
|
|
}
|
|
|
|
|
|
- if (isJson(bodyStr)) {
|
|
|
- return contentTypes.json;
|
|
|
+ if (isGif(bodyBuffer)) {
|
|
|
+ return contentTypes.gif;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isWebp(bodyBuffer)) {
|
|
|
+ return contentTypes.webp;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isWoff(bodyBuffer)) {
|
|
|
+ return contentTypes.woff;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isWoff2(bodyBuffer)) {
|
|
|
+ return contentTypes.woff2;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isOtf(bodyBuffer)) {
|
|
|
+ return contentTypes.otf;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isTtf(bodyBuffer)) {
|
|
|
+ return contentTypes.ttf;
|
|
|
}
|
|
|
|
|
|
- const type = await FileType.fromBuffer(bodyBuffer);
|
|
|
- if (type && type.ext && contentTypes[type.ext]) {
|
|
|
- return contentTypes[type.ext];
|
|
|
+ if (isEot(bodyBuffer)) {
|
|
|
+ return contentTypes.eot;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isJson(bodyStr)) {
|
|
|
+ return contentTypes.json;
|
|
|
}
|
|
|
|
|
|
return null;
|
|
@@ -107,7 +146,7 @@ var ContentTypeChecker = function() {
|
|
|
}
|
|
|
|
|
|
var contentTypes = {
|
|
|
- jpg: {
|
|
|
+ jpeg: {
|
|
|
type: 'image',
|
|
|
mimes: ['image/jpeg'],
|
|
|
updateFn: function(entry) {
|
|
@@ -148,14 +187,6 @@ var ContentTypeChecker = function() {
|
|
|
entry.isImage = true;
|
|
|
}
|
|
|
},
|
|
|
- avif: {
|
|
|
- type: 'image',
|
|
|
- mimes: ['image/avif'],
|
|
|
- updateFn: function(entry) {
|
|
|
- entry.type = 'image';
|
|
|
- entry.isImage = true;
|
|
|
- }
|
|
|
- },
|
|
|
woff: {
|
|
|
type: 'webfont',
|
|
|
mimes: ['application/x-font-woff', 'application/font-woff', 'font/woff'],
|