UBERF-7425: Fix some CF caching issues (#5936)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2024-06-28 01:22:19 +07:00 committed by GitHub
parent 9217aff35c
commit f9aabe98c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 7 deletions

View File

@ -168,7 +168,12 @@ async function getFile (
preConditions.IfModifiedSince(req.headers, { lastModified: new Date(stat.modifiedOn) }) === 'notModified' preConditions.IfModifiedSince(req.headers, { lastModified: new Date(stat.modifiedOn) }) === 'notModified'
) { ) {
// Matched, return not modified // Matched, return not modified
res.statusCode = 304 res.writeHead(304, {
'content-type': stat.contentType,
etag: stat.etag,
'last-modified': new Date(stat.modifiedOn).toISOString(),
'cache-control': cacheControlValue
})
res.end() res.end()
return return
} }
@ -375,7 +380,10 @@ export function start (
res.status(404).send() res.status(404).send()
return return
} }
const isImage = blobInfo.contentType.includes('image/')
// try image and octet streams
const isImage =
blobInfo.contentType.includes('image/') || blobInfo.contentType.includes('application/octet-stream')
if (token === undefined) { if (token === undefined) {
if (blobInfo !== undefined && !isImage) { if (blobInfo !== undefined && !isImage) {

View File

@ -1,4 +1,4 @@
import { Request } from 'express' import type { IncomingMessage } from 'http'
export const ETagSupport = { export const ETagSupport = {
isWeak (etag: string): boolean { isWeak (etag: string): boolean {
@ -30,7 +30,7 @@ function toList (value: string): string[] {
} }
export const preConditions = { export const preConditions = {
IfMatch: (headers: Request['headers'], state: { etag: string }): 'fetch' | 'notModified' => { IfMatch: (headers: IncomingMessage['headers'], state: { etag: string }): 'fetch' | 'notModified' => {
const header = (headers as any)['if-match'] const header = (headers as any)['if-match']
if (header == null) { if (header == null) {
return 'fetch' return 'fetch'
@ -40,7 +40,7 @@ export const preConditions = {
} }
return toList(header).some((etag) => ETagSupport.strongMatch(etag, state.etag)) ? 'notModified' : 'fetch' return toList(header).some((etag) => ETagSupport.strongMatch(etag, state.etag)) ? 'notModified' : 'fetch'
}, },
IfNoneMatch: (headers: Request['headers'], state: { etag: string }): 'fetch' | 'notModified' => { IfNoneMatch: (headers: IncomingMessage['headers'], state: { etag: string }): 'fetch' | 'notModified' => {
const header = (headers as any)['if-none-match'] const header = (headers as any)['if-none-match']
if (header == null) { if (header == null) {
return 'fetch' return 'fetch'
@ -52,7 +52,7 @@ export const preConditions = {
return toList(header).some((etag) => ETagSupport.weakMatch(etag, state.etag)) ? 'notModified' : 'fetch' return toList(header).some((etag) => ETagSupport.weakMatch(etag, state.etag)) ? 'notModified' : 'fetch'
} }
}, },
IfModifiedSince: (headers: Request['headers'], state: { lastModified: Date }): 'fetch' | 'notModified' => { IfModifiedSince: (headers: IncomingMessage['headers'], state: { lastModified: Date }): 'fetch' | 'notModified' => {
if ((headers as any)['if-none-match'] != null) { if ((headers as any)['if-none-match'] != null) {
return 'fetch' return 'fetch'
} }
@ -67,7 +67,7 @@ export const preConditions = {
} }
return state.lastModified.getTime() <= date ? 'notModified' : 'fetch' return state.lastModified.getTime() <= date ? 'notModified' : 'fetch'
}, },
IfUnmodifiedSince: (headers: Request['headers'], state: { lastModified: Date }): 'fetch' | 'failed' => { IfUnmodifiedSince: (headers: IncomingMessage['headers'], state: { lastModified: Date }): 'fetch' | 'failed' => {
if ((headers as any)['if-match'] != null) { if ((headers as any)['if-match'] != null) {
return 'fetch' return 'fetch'
} }