mirror of
https://github.com/cupcakearmy/morphus.git
synced 2025-09-06 00:00:40 +00:00
remove caniuse integration in favor of headers
This commit is contained in:
@@ -14,12 +14,10 @@ import { flatten, unflatten } from 'flat'
|
||||
import type { IncomingHttpHeaders } from 'http2'
|
||||
import ms from 'ms'
|
||||
import sharp, { FitEnum, FormatEnum } from 'sharp'
|
||||
|
||||
import { App } from '..'
|
||||
import { Config, URLClean } from '../config'
|
||||
import { storage } from '../storage'
|
||||
import { transform } from '../transform'
|
||||
import { supportsAvif, supportsWebP } from '../utils/caniuse'
|
||||
import { ForbiddenError } from '../utils/errors'
|
||||
import { sha3, sortObjectByKeys, testForPrefixOrRegexp, validateSyncOrFail } from '../utils/utils'
|
||||
|
||||
@@ -171,16 +169,7 @@ export class TransformQueryBase {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ua) {
|
||||
if (supportsAvif(ua)) {
|
||||
this.format!.name = 'avif'
|
||||
return
|
||||
}
|
||||
if (supportsWebP(ua)) {
|
||||
this.format!.name = 'webp'
|
||||
return
|
||||
}
|
||||
}
|
||||
// Fallback
|
||||
this.format!.name = 'jpeg'
|
||||
}
|
||||
|
||||
|
@@ -2,7 +2,7 @@ import { FastifyInstance } from 'fastify'
|
||||
|
||||
export function init(App: FastifyInstance) {
|
||||
App.register(require('under-pressure'))
|
||||
App.register(require('fastify-caching'))
|
||||
App.register(require('fastify-compress'), { global: true })
|
||||
App.register(require('fastify-cors'), { origin: '*' })
|
||||
App.register(require('@fastify/caching'))
|
||||
App.register(require('@fastify/compress'), { global: true })
|
||||
App.register(require('@fastify/cors'), { origin: '*' })
|
||||
}
|
||||
|
@@ -1,5 +1,4 @@
|
||||
import fastify from 'fastify'
|
||||
|
||||
import { Config, init as initConfig } from './config'
|
||||
import { init as initRoutes } from './controllers'
|
||||
import { init as initHooks } from './fastify/hooks'
|
||||
|
@@ -1,71 +0,0 @@
|
||||
import Avif from 'caniuse-db/features-json/avif.json'
|
||||
import WebP from 'caniuse-db/features-json/webp.json'
|
||||
import DeviceDetector from 'device-detector-js'
|
||||
|
||||
const detector = new DeviceDetector()
|
||||
|
||||
function findLowestSupportedVersion(stat: Record<string, string>): number | null {
|
||||
const entries = Object.entries(stat).sort((a, b) => parseInt(a[0]) - parseInt(b[0]))
|
||||
for (const [version, support] of entries) {
|
||||
if (support.startsWith('y') || support.startsWith('a')) {
|
||||
return parseInt(version)
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const BrowserMappings = {
|
||||
'Internet Explorer': 'ie',
|
||||
'Microsoft Edge': 'edge',
|
||||
Firefox: 'firefox',
|
||||
Chrome: 'chrome',
|
||||
Safari: 'safari',
|
||||
Opera: 'opera',
|
||||
'Mobile Safari': 'ios_saf',
|
||||
'Opera Mini': 'op_mini',
|
||||
'Android Browser': 'android',
|
||||
'Chrome Mobile': 'and_chr',
|
||||
'Firefox Mobile': 'and_ff',
|
||||
'UC Browser': 'and_uc',
|
||||
'Samsung Browser': 'samsung',
|
||||
'QQ Browser': 'and_qq',
|
||||
}
|
||||
|
||||
function matchBrowserToStat(browser: DeviceDetector.DeviceDetectorResult): string {
|
||||
if (browser.os!.name === 'iOS') {
|
||||
return 'ios_saf'
|
||||
}
|
||||
if (browser.client!.name in BrowserMappings) {
|
||||
return BrowserMappings[browser.client!.name as keyof typeof BrowserMappings]
|
||||
}
|
||||
throw new Error('Could not determine mapping for browser')
|
||||
}
|
||||
|
||||
function match(feature: typeof Avif | typeof WebP, ua: string): boolean {
|
||||
const browser = detector.parse(ua)
|
||||
if (!browser.client || !browser.os) {
|
||||
throw new Error('Could not parse browser')
|
||||
}
|
||||
const stats = feature.stats[matchBrowserToStat(browser) as keyof typeof feature.stats]
|
||||
const lowestSupported = findLowestSupportedVersion(stats)
|
||||
if (lowestSupported === null) {
|
||||
return false
|
||||
}
|
||||
return lowestSupported <= parseInt(browser.client.version)
|
||||
}
|
||||
|
||||
export function supportsAvif(ua: string): boolean {
|
||||
try {
|
||||
return match(Avif, ua)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export function supportsWebP(ua: string): boolean {
|
||||
try {
|
||||
return match(WebP, ua)
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user