From e889b07638a81e49bb3a0b75041455794e4c6ac8 Mon Sep 17 00:00:00 2001 From: flakey5 <73616808+flakey5@users.noreply.github.com> Date: Mon, 23 Dec 2024 21:43:10 -0800 Subject: [PATCH 1/3] fix: serve index.tab files as text files Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com> --- src/providers/r2Provider.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/providers/r2Provider.ts b/src/providers/r2Provider.ts index c72114a..5b399d1 100644 --- a/src/providers/r2Provider.ts +++ b/src/providers/r2Provider.ts @@ -98,10 +98,19 @@ function r2MetadataToHeaders( ): HttpResponseHeaders { const { httpMetadata } = object; + let contentType: string; + if (object.httpMetadata?.contentType !== undefined) { + contentType = object.httpMetadata.contentType; + } else { + // Serve .tab files as text files + contentType = object.key.endsWith('.tab') + ? 'text/plain' + : 'application/octet-stream'; + } + return { etag: object.httpEtag, - 'content-type': - object.httpMetadata?.contentType ?? 'application/octet-stream', + 'content-type': contentType, 'accept-range': 'bytes', // https://github.com/nodejs/build/blob/e3df25d6a23f033db317a53ab1e904c953ba1f00/ansible/www-standalone/resources/config/nodejs.org?plain=1#L194-L196 'access-control-allow-origin': object.key.endsWith('.json') ? '*' : '', From 764b4b88b6c592105de7c22bed5f729f350527c1 Mon Sep 17 00:00:00 2001 From: flakey5 <73616808+flakey5@users.noreply.github.com> Date: Fri, 3 Jan 2025 21:25:07 -0800 Subject: [PATCH 2/3] review Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com> --- src/constants/contentTypeOverrides.json | 3 +++ src/providers/r2Provider.ts | 14 ++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 src/constants/contentTypeOverrides.json diff --git a/src/constants/contentTypeOverrides.json b/src/constants/contentTypeOverrides.json new file mode 100644 index 0000000..178c598 --- /dev/null +++ b/src/constants/contentTypeOverrides.json @@ -0,0 +1,3 @@ +{ + "tab": "text/plain" +} diff --git a/src/providers/r2Provider.ts b/src/providers/r2Provider.ts index 5b399d1..77743b3 100644 --- a/src/providers/r2Provider.ts +++ b/src/providers/r2Provider.ts @@ -1,5 +1,6 @@ import { CACHE_HEADERS } from '../constants/cache'; import { R2_RETRY_LIMIT } from '../constants/limits'; +import contentTypeOverrides from '../constants/contentTypeOverrides.json' assert { type: 'json' }; import type { Context } from '../context'; import { objectHasBody } from '../utils/object'; import { retryWrapper } from '../utils/provider'; @@ -98,14 +99,15 @@ function r2MetadataToHeaders( ): HttpResponseHeaders { const { httpMetadata } = object; + const fileExtension = object.key.substring(object.key.lastIndexOf('.') + 1); + let contentType: string; - if (object.httpMetadata?.contentType !== undefined) { - contentType = object.httpMetadata.contentType; + if (fileExtension in contentTypeOverrides) { + contentType = + contentTypeOverrides[fileExtension as keyof typeof contentTypeOverrides]; } else { - // Serve .tab files as text files - contentType = object.key.endsWith('.tab') - ? 'text/plain' - : 'application/octet-stream'; + contentType = + object.httpMetadata?.contentType ?? 'application/octet-stream'; } return { From 54e82f6b3ca141e0d0f2579f08750c833a14744f Mon Sep 17 00:00:00 2001 From: flakey5 <73616808+flakey5@users.noreply.github.com> Date: Mon, 6 Jan 2025 10:35:05 -0800 Subject: [PATCH 3/3] review Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com> --- src/providers/r2Provider.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/providers/r2Provider.ts b/src/providers/r2Provider.ts index 77743b3..d6eef2c 100644 --- a/src/providers/r2Provider.ts +++ b/src/providers/r2Provider.ts @@ -101,14 +101,10 @@ function r2MetadataToHeaders( const fileExtension = object.key.substring(object.key.lastIndexOf('.') + 1); - let contentType: string; - if (fileExtension in contentTypeOverrides) { - contentType = - contentTypeOverrides[fileExtension as keyof typeof contentTypeOverrides]; - } else { - contentType = - object.httpMetadata?.contentType ?? 'application/octet-stream'; - } + const contentType = + contentTypeOverrides[fileExtension as keyof typeof contentTypeOverrides] ?? + object.httpMetadata?.contentType ?? + 'application/octet-stream'; return { etag: object.httpEtag,