Skip to content

Commit

Permalink
Release 1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
frwiqueueit committed Aug 28, 2020
1 parent af11b54 commit 1dd4ab7
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 322 deletions.
122 changes: 60 additions & 62 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,93 @@
'use strict'
const QUEUEIT_CUSTOMERID = "YOUR CUSTOMERID";
const QUEUEIT_CUSTOMERID = "YOUR CUSTOMERID";
const QUEUEIT_SECRETKEY = "YOUR SECRET KEY";

// Set to true, if you have any trigger(s) containing experimental 'RequestBody' condition.
const READ_REQUEST_BODY = false;

const QueueIT = require("./sdk/queueit-knownuserv3-sdk.js");
const contextProvider = require("./contextProvider.js");
const helpers = require("./queueitHelpers.js");
const integrationConfigProvider = require("./integrationConfigProvider.js");

addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request));
})

addEventListener('fetch', event => {
async function fetchAndApply(request) {

event.respondWith( fetchAndApply(event.request));
})
async function fetchAndApply(request) {

if(request.url.indexOf('__push_queueit_config') > 0)
{
var result = await integrationConfigProvider.tryStoreIntegrationConfig(request,IntegrationConfigKV,QUEUEIT_SECRETKEY);
return new Response(result? "Success!":"Fail!");
if (request.url.indexOf('__push_queueit_config') > 0) {
var result = await integrationConfigProvider.tryStoreIntegrationConfig(request, IntegrationConfigKV, QUEUEIT_SECRETKEY);
return new Response(result ? "Success!" : "Fail!");
}

let integrationConfigJson = await integrationConfigProvider.getIntegrationConfig(IntegrationConfigKV) || "";

helpers.configureKnownUserHashing();

let bodyText = "";
if (READ_REQUEST_BODY) {
bodyText = await request.clone().text();
}
const httpProvider = contextProvider.getHttpHandler(request, bodyText);

const bodyText = await request.clone().text();
const httpProvider = contextProvider.getHttpHandler(request,bodyText);

var knownUser = QueueIT.KnownUserV3.SDK.KnownUser;
var queueitToken = helpers.getParameterByName(request.url,knownUser.QueueITTokenKey );
var queueitToken = helpers.getParameterByName(request.url, knownUser.QueueITTokenKey);
var requestUrl = request.url;
var requestUrlWithoutToken = requestUrl.replace(new RegExp("([\?&])(" + knownUser.QueueITTokenKey + "=[^&]*)", 'i'), "");
// The requestUrlWithoutToken is used to match Triggeclearrs and as the Target url (where to return the users to).
// It is therefor important that this is exactly the url of the users browsers. So, if your webserver is
// behind e.g. a load balancer that modifies the host name or port, reformat requestUrlWithoutToken before proceeding.

let response = null;
try
{
var validationResult = knownUser.validateRequestByIntegrationConfig(
requestUrlWithoutToken, queueitToken, integrationConfigJson,
QUEUEIT_CUSTOMERID, QUEUEIT_SECRETKEY, httpProvider);

if (validationResult.doRedirect()) {
response = new Response();
// Adding no cache headers to prevent browsers to cache requests
response.headers.set('Cache-Control', 'no-cache, no-store, must-revalidate, max-age=0');
response.headers.set('Pragma', 'no-cache');
response.headers.set('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');

if (validationResult.isAjaxResult) {
// In case of ajax call send the user to the queue by sending a custom queue-it header and redirecting user to queue from javascript
response.headers.set(validationResult.getAjaxQueueRedirectHeaderKey(),helpers.addKUPlatformVersion(validationResult.getAjaxRedirectUrl()));

}
else {
// Send the user to the queue - either because hash was missing or because is was invalid
response = Response.redirect(helpers.addKUPlatformVersion(validationResult.redirectUrl),'302');
}
}
else {

// Request can continue - we remove queueittoken form querystring parameter to avoid sharing of user specific token
if (requestUrl !== requestUrlWithoutToken && validationResult.actionType) {
response = new Response();
response = Response.redirect(requestUrlWithoutToken,'302');
try {
var validationResult = knownUser.validateRequestByIntegrationConfig(
requestUrlWithoutToken, queueitToken, integrationConfigJson,
QUEUEIT_CUSTOMERID, QUEUEIT_SECRETKEY, httpProvider);

if (validationResult.doRedirect()) {
response = new Response();
// Adding no cache headers to prevent browsers to cache requests
response.headers.set('Cache-Control', 'no-cache, no-store, must-revalidate, max-age=0');
response.headers.set('Pragma', 'no-cache');
response.headers.set('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');

if (validationResult.isAjaxResult) {
// In case of ajax call send the user to the queue by sending a custom queue-it header and redirecting user to queue from javascript
response.headers.set(validationResult.getAjaxQueueRedirectHeaderKey(), helpers.addKUPlatformVersion(validationResult.getAjaxRedirectUrl()));

}
else {
// Send the user to the queue - either because hash was missing or because is was invalid
response = Response.redirect(helpers.addKUPlatformVersion(validationResult.redirectUrl), '302');
}
}
else {
// Render page
response = await fetch(request);
// Request can continue - we remove queueittoken form querystring parameter to avoid sharing of user specific token
if (requestUrl !== requestUrlWithoutToken && validationResult.actionType === 'Queue') {
response = new Response();
response = Response.redirect(requestUrlWithoutToken, '302');
}
else {
// Render page
response = await fetch(request);
}
}
}

if(httpProvider.outputCookie)
{
response = new Response(response.body, response);
response.headers.append("Set-Cookie",httpProvider.outputCookie);
}
if (httpProvider.outputCookie) {
response = new Response(response.body, response);
response.headers.append("Set-Cookie", httpProvider.outputCookie);
}
}
catch (e) {
// There was an error validationg the request
// Use your own logging framework to log the Exception
if(console && console.log)
{
console.log("ERROR:" + e);
}
response = await fetch(request);
// There was an error validationg the request
// Use your own logging framework to log the Exception
if (console && console.log) {
console.log("ERROR:" + e);
}
response = await fetch(request);
}

return response;
}

}


3 changes: 1 addition & 2 deletions contextProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ exports.getHttpHandler = function(request, bodyString)

return cookieValue;
},
getBodyAsString:function()
{
getRequestBodyAsString: function() {
return bodyString;
},
__parseCookies:function(cookieValue) {
Expand Down
2 changes: 1 addition & 1 deletion queueitHelpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

const QueueIT = require("./sdk/queueit-knownuserv3-sdk.js");
const crypto = require('js-sha256');
const CLOUDFLARE_SDK_VERSION = "1.1.1";
const CLOUDFLARE_SDK_VERSION = "1.1.2";
exports.getParameterByName = function( url, name) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
Expand Down
Loading

0 comments on commit 1dd4ab7

Please sign in to comment.