Skip to content

Commit

Permalink
avoid regex matching
Browse files Browse the repository at this point in the history
Signed-off-by: Prince Mendiratta <prince.mendi@gmail.com>
  • Loading branch information
Prince-Mendiratta committed Mar 3, 2023
1 parent 030cbb2 commit 7323183
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions web/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,25 @@ const server = http.createServer((request, response) => {
let hostname = host;
let requestPath = request.url;

// Regex not declared globally to avoid internal regex pointer reset for each request.
const apiRegex = /(\/staging.*)api/g;

/**
* We only match staging api root to redirect requests to the staging server if the request
* is an API call, except chat attachments. By default, requests are sent to the prod server.
* When a request is matching a proxy config path we might direct it to a different host (e.g. staging)
* For requests matching proxy config patterns we replace the mapping url (prefix) with the actual path.
* This is done because the staging api root is only intended for the proxy,
* the actual server request must use the /api path.
* For example,
* /api?command=OpenReport => request sent to production server
* /staging-api?command=OpenReport => request sent to staging server
* /staging-secure-api?command=OpenReport => request sent to secure staging server
* /chat-attachments/46545... => request sent to production server
*/
const apiRootMatch = apiRegex.exec(requestPath);

// Switch host only if API call, not on chat attachments.
if (apiRootMatch) {
const apiRoot = apiRootMatch[1];

// apiRoot can only be staging or secure staging
hostname = apiRoot === proxyConfig.STAGING_SECURE ? stagingSecureHost : stagingHost;

/**
* Replace the mapping url with the actual path.
* This is done because the staging api root is only intended for the proxy,
* the actual server request must use the /api path.
*/
requestPath = request.url.replace(apiRoot, '/');
if (request.url.startsWith(proxyConfig.STAGING_SECURE)) {
hostname = stagingSecureHost;
requestPath = request.url.replace(proxyConfig.STAGING_SECURE, '/');
} else if (request.url.startsWith(proxyConfig.STAGING)) {
hostname = stagingHost;
requestPath = request.url.replace(proxyConfig.STAGING, '/');
}

const proxyRequest = https.request({
hostname,
method: 'POST',
Expand Down

0 comments on commit 7323183

Please sign in to comment.