forked from queueit/KnownUser.V3.Cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
95 lines (76 loc) · 3.76 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict'
const QUEUEIT_CUSTOMERID = "YOUR CUSTOMERID";
const QUEUEIT_SECRETKEY = "YOUR SECRET KEY";
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));
})
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!");
}
let integrationConfigJson = await integrationConfigProvider.getIntegrationConfig(IntegrationConfigKV) || "";
helpers.configureKnownUserHashing();
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 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');
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');
}
else {
// Render page
response = await fetch(request);
}
}
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);
}
return response;
}