Skip to content

Commit

Permalink
Move call to proxyResponseBodyFromUpstream into a new function
Browse files Browse the repository at this point in the history
This allows for reuse later with other resource extractors.
  • Loading branch information
victorlin committed Jul 14, 2022
1 parent 0010a64 commit 547b03b
Showing 1 changed file with 44 additions and 31 deletions.
75 changes: 44 additions & 31 deletions src/endpoints/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}));
}


Expand Down

0 comments on commit 547b03b

Please sign in to comment.