remove caniuse integration in favor of headers

This commit is contained in:
cupcakearmy 2022-05-29 00:09:22 +02:00
parent 552fb3c572
commit 08470ba820
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
6 changed files with 436 additions and 512 deletions

View File

@ -11,6 +11,7 @@
}, },
"engines": { "engines": {
"node": "16", "node": "16",
"pnpm": ">=6",
"yarn": "plase-use-pnpm", "yarn": "plase-use-pnpm",
"npm": "plase-use-pnpm" "npm": "plase-use-pnpm"
}, },
@ -19,31 +20,29 @@
"@types/convict": "^6.1.1", "@types/convict": "^6.1.1",
"@types/flat": "^5.0.2", "@types/flat": "^5.0.2",
"@types/js-yaml": "^4.0.5", "@types/js-yaml": "^4.0.5",
"@types/minio": "^7.0.12", "@types/minio": "^7.0.13",
"@types/ms": "^0.7.31", "@types/ms": "^0.7.31",
"@types/node": "^16.11.22", "@types/node": "^16.11.36",
"@types/sharp": "^0.29.5", "@types/sharp": "^0.29.5",
"ts-node-dev": "^1.1.8", "ts-node-dev": "^2.0.0",
"typescript": "^4.5.5" "typescript": "^4.7.2"
}, },
"dependencies": { "dependencies": {
"@google-cloud/storage": "^5.18.1", "@fastify/caching": "^7.0.0",
"caniuse-db": "^1.0.30001307", "@fastify/compress": "^5.0.0",
"@fastify/cors": "^7.0.0",
"@google-cloud/storage": "^5.20.5",
"class-validator": "^0.13.2", "class-validator": "^0.13.2",
"convict": "^6.2.1", "convict": "^6.2.3",
"convict-format-with-validator": "^6.2.0", "convict-format-with-validator": "^6.2.0",
"device-detector-js": "^3.0.1",
"fast-crc32c": "^2.0.0", "fast-crc32c": "^2.0.0",
"fastify": "^3.27.1", "fastify": "^3.29.0",
"fastify-caching": "^6.2.0",
"fastify-compress": "^3.7.0",
"fastify-cors": "^6.0.2",
"flat": "^5.0.2", "flat": "^5.0.2",
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
"minio": "^7.0.26", "minio": "^7.0.28",
"ms": "^2.1.3", "ms": "^2.1.3",
"pino-pretty": "^7.5.1", "pino-pretty": "^7.6.1",
"sharp": "^0.29.3", "sharp": "^0.29.3",
"under-pressure": "^5.8.0" "under-pressure": "^5.8.1"
} }
} }

828
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -14,12 +14,10 @@ import { flatten, unflatten } from 'flat'
import type { IncomingHttpHeaders } from 'http2' import type { IncomingHttpHeaders } from 'http2'
import ms from 'ms' import ms from 'ms'
import sharp, { FitEnum, FormatEnum } from 'sharp' import sharp, { FitEnum, FormatEnum } from 'sharp'
import { App } from '..' import { App } from '..'
import { Config, URLClean } from '../config' import { Config, URLClean } from '../config'
import { storage } from '../storage' import { storage } from '../storage'
import { transform } from '../transform' import { transform } from '../transform'
import { supportsAvif, supportsWebP } from '../utils/caniuse'
import { ForbiddenError } from '../utils/errors' import { ForbiddenError } from '../utils/errors'
import { sha3, sortObjectByKeys, testForPrefixOrRegexp, validateSyncOrFail } from '../utils/utils' import { sha3, sortObjectByKeys, testForPrefixOrRegexp, validateSyncOrFail } from '../utils/utils'
@ -171,16 +169,7 @@ export class TransformQueryBase {
} }
} }
} }
if (ua) { // Fallback
if (supportsAvif(ua)) {
this.format!.name = 'avif'
return
}
if (supportsWebP(ua)) {
this.format!.name = 'webp'
return
}
}
this.format!.name = 'jpeg' this.format!.name = 'jpeg'
} }

View File

@ -2,7 +2,7 @@ import { FastifyInstance } from 'fastify'
export function init(App: FastifyInstance) { export function init(App: FastifyInstance) {
App.register(require('under-pressure')) App.register(require('under-pressure'))
App.register(require('fastify-caching')) App.register(require('@fastify/caching'))
App.register(require('fastify-compress'), { global: true }) App.register(require('@fastify/compress'), { global: true })
App.register(require('fastify-cors'), { origin: '*' }) App.register(require('@fastify/cors'), { origin: '*' })
} }

View File

@ -1,5 +1,4 @@
import fastify from 'fastify' import fastify from 'fastify'
import { Config, init as initConfig } from './config' import { Config, init as initConfig } from './config'
import { init as initRoutes } from './controllers' import { init as initRoutes } from './controllers'
import { init as initHooks } from './fastify/hooks' import { init as initHooks } from './fastify/hooks'

View File

@ -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
}
}