From f9aabe98c3a301db13e25e834f58245141ea61b3 Mon Sep 17 00:00:00 2001
From: Andrey Sobolev <haiodo@users.noreply.github.com>
Date: Fri, 28 Jun 2024 01:22:19 +0700
Subject: [PATCH] UBERF-7425: Fix some CF caching issues (#5936)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
---
 server/front/src/index.ts | 12 ++++++++++--
 server/front/src/utils.ts | 10 +++++-----
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/server/front/src/index.ts b/server/front/src/index.ts
index cb3765c39a..570e0076c1 100644
--- a/server/front/src/index.ts
+++ b/server/front/src/index.ts
@@ -168,7 +168,12 @@ async function getFile (
     preConditions.IfModifiedSince(req.headers, { lastModified: new Date(stat.modifiedOn) }) === 'notModified'
   ) {
     // 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()
     return
   }
@@ -375,7 +380,10 @@ export function start (
             res.status(404).send()
             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 (blobInfo !== undefined && !isImage) {
diff --git a/server/front/src/utils.ts b/server/front/src/utils.ts
index 0f0473081e..5345fb329d 100644
--- a/server/front/src/utils.ts
+++ b/server/front/src/utils.ts
@@ -1,4 +1,4 @@
-import { Request } from 'express'
+import type { IncomingMessage } from 'http'
 
 export const ETagSupport = {
   isWeak (etag: string): boolean {
@@ -30,7 +30,7 @@ function toList (value: string): string[] {
 }
 
 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']
     if (header == null) {
       return 'fetch'
@@ -40,7 +40,7 @@ export const preConditions = {
     }
     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']
     if (header == null) {
       return 'fetch'
@@ -52,7 +52,7 @@ export const preConditions = {
       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) {
       return 'fetch'
     }
@@ -67,7 +67,7 @@ export const preConditions = {
     }
     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) {
       return 'fetch'
     }