Skip to content

Commit

Permalink
Add JS library serving
Browse files Browse the repository at this point in the history
  • Loading branch information
Bukashk0zzz committed Feb 16, 2022
1 parent 5765f81 commit 4702cf6
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
39 changes: 39 additions & 0 deletions template.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,41 @@ const JSON = require('JSON');
const logToConsole = require('logToConsole');
const returnResponse = require('returnResponse');
const setCookie = require('setCookie');
const templateDataStorage = require('templateDataStorage');
const sendHttpGet = require('sendHttpGet');
const getTimestampMillis = require('getTimestampMillis');
const sendHttpRequest = require('sendHttpRequest');
const setResponseBody = require('setResponseBody');
const setResponseHeader = require('setResponseHeader');
const setResponseStatus = require('setResponseStatus');
const getContainerVersion = require('getContainerVersion');
const path = getRequestPath();

// Check if this Client should serve exponea.js file
if (path === data.proxyJsFilePath) {
claimRequest();

const now = getTimestampMillis();
const thirty_minutes_ago = now - (30 * 60 * 1000);

if (templateDataStorage.getItemCopy('exponea_js') == null || templateDataStorage.getItemCopy('exponea_stored_at') < thirty_minutes_ago) {
sendHttpGet('https://api.exponea.com/js/exponea.min.js', (statusCode, headers, body) => {
if (statusCode === 200) {
templateDataStorage.setItemCopy('exponea_js', body);
templateDataStorage.setItemCopy('exponea_headers', headers);
templateDataStorage.setItemCopy('exponea_stored_at', now);
}
sendProxyResponse(body, headers, statusCode);
});
} else {
sendProxyResponse(
templateDataStorage.getItemCopy('exponea_js'),
templateDataStorage.getItemCopy('exponea_headers'),
200
);
}
}

// Check if this Client should claim request
if (path !== '/bulk' && path !== '/managed-tags/show' && path !== ('/webxp/projects/'+data.projectToken+'/bundle')) {
return;
Expand Down Expand Up @@ -136,6 +164,17 @@ function setResponseCookies(setCookieHeader) {
}
}

function sendProxyResponse(response, headers, statusCode) {
setResponseStatus(statusCode);
setResponseBody(response);

for (const key in headers) {
setResponseHeader(key, headers[key]);
}

returnResponse();
}

function determinateIsLoggingEnabled() {
if (!data.logType) {
return isDebug;
Expand Down
64 changes: 63 additions & 1 deletion template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ ___TEMPLATE_PARAMETERS___
}
]
},
{
"type": "TEXT",
"name": "proxyJsFilePath",
"displayName": "A path that will be used for the exponea.js serving",
"simpleValueType": true,
"valueValidators": [
{
"type": "NON_EMPTY"
}
],
"defaultValue": "/js/exponea.min.js"
},
{
"type": "GROUP",
"name": "logsGroup",
Expand Down Expand Up @@ -95,14 +107,43 @@ const JSON = require('JSON');
const logToConsole = require('logToConsole');
const returnResponse = require('returnResponse');
const setCookie = require('setCookie');
const templateDataStorage = require('templateDataStorage');
const sendHttpGet = require('sendHttpGet');
const getTimestampMillis = require('getTimestampMillis');
const sendHttpRequest = require('sendHttpRequest');
const setResponseBody = require('setResponseBody');
const setResponseHeader = require('setResponseHeader');
const setResponseStatus = require('setResponseStatus');
const getContainerVersion = require('getContainerVersion');
const path = getRequestPath();

// Check if this Client should serve exponea.js file
if (path === data.proxyJsFilePath) {
claimRequest();
const now = getTimestampMillis();
const thirty_minutes_ago = now - (30 * 60 * 1000);
if (templateDataStorage.getItemCopy('exponea_js') == null || templateDataStorage.getItemCopy('exponea_stored_at') < thirty_minutes_ago) {
sendHttpGet('https://api.exponea.com/js/exponea.min.js', (statusCode, headers, body) => {
if (statusCode === 200) {
templateDataStorage.setItemCopy('exponea_js', body);
templateDataStorage.setItemCopy('exponea_headers', headers);
templateDataStorage.setItemCopy('exponea_stored_at', now);
}
sendProxyResponse(body, headers, statusCode);
});
} else {
sendProxyResponse(
templateDataStorage.getItemCopy('exponea_js'),
templateDataStorage.getItemCopy('exponea_headers'),
200
);
}
}

// Check if this Client should claim request
if (getRequestPath() !== '/exponea') {
if (path !== '/bulk' && path !== '/managed-tags/show' && path !== ('/webxp/projects/'+data.projectToken+'/bundle')) {
return;
}

Expand Down Expand Up @@ -221,6 +262,17 @@ function setResponseCookies(setCookieHeader) {
}
}

function sendProxyResponse(response, headers, statusCode) {
setResponseStatus(statusCode);
setResponseBody(response);
for (const key in headers) {
setResponseHeader(key, headers[key]);
}

returnResponse();
}

function determinateIsLoggingEnabled() {
if (!data.logType) {
return isDebug;
Expand Down Expand Up @@ -455,6 +507,16 @@ ___SERVER_PERMISSIONS___
"param": []
},
"isRequired": true
},
{
"instance": {
"key": {
"publicId": "access_template_storage",
"versionId": "1"
},
"param": []
},
"isRequired": true
}
]

Expand Down

0 comments on commit 4702cf6

Please sign in to comment.