diff --git a/src/endpoints/sources.js b/src/endpoints/sources.js index e963f5167..a703948ed 100644 --- a/src/endpoints/sources.js +++ b/src/endpoints/sources.js @@ -305,40 +305,53 @@ function sendSubresource(subresourceExtractor) { authz.assertAuthorized(req.user, authz.actions.Read, subresource.resource); - const subresourceUrl = await subresource.url(); + return await sendAny(req, res, + await subresource.url(), + subresource.accept + ); + }; +} - /* Proxy the data through us: - * - * client (browser, CLI, etc) ⟷ us (nextstrain.org) ⟷ upstream source - */ - return await proxyResponseBodyFromUpstream(req, res, new Request(subresourceUrl, { - headers: { - Accept: subresource.accept, - - /* Normalize Accept-Encoding to gzip or nothing, as realistically we - * only expect upstreams to support gzip and normalizing reduces the - * cache burden of varying on many equivalent but effectively the same - * client Accept-Encoding values. - */ - "Accept-Encoding": req.acceptsEncodings("gzip") - ? "gzip" - : "identity", - }, - - /* Use "no-cache" mode to always revalidate with the upstream but avoid - * re-transferring the content if we have a cached copy that matches - * upstream. - */ - cache: "no-cache", - /* Disable automatic body decompression. Instead, we pass through the - * Accept-Encoding of the client to the upstream and return the - * upstream Content-Encoding and body directly. This avoids needlessly - * decompressing and recompressing ourselves. +/** + * Proxy the data through us: + * + * client (browser, CLI, etc) ⟷ us (nextstrain.org) ⟷ upstream source + * + * @param {express.request} req - Express-style request instance + * @param {express.response} res - Express-style response instance + * @param {string} url - Resource URL + * @param {string} accept - HTTP Accept header value + * @returns {expressEndpointAsync} + */ +async function sendAny(req, res, url, accept) { + return await proxyResponseBodyFromUpstream(req, res, new Request(url, { + headers: { + Accept: accept, + + /* Normalize Accept-Encoding to gzip or nothing, as realistically we + * only expect upstreams to support gzip and normalizing reduces the + * cache burden of varying on many equivalent but effectively the same + * client Accept-Encoding values. */ - compress: false, - })); - }; + "Accept-Encoding": req.acceptsEncodings("gzip") + ? "gzip" + : "identity", + }, + + /* Use "no-cache" mode to always revalidate with the upstream but avoid + * re-transferring the content if we have a cached copy that matches + * upstream. + */ + cache: "no-cache", + + /* Disable automatic body decompression. Instead, we pass through the + * Accept-Encoding of the client to the upstream and return the + * upstream Content-Encoding and body directly. This avoids needlessly + * decompressing and recompressing ourselves. + */ + compress: false, + })); }