@queue-it/fastly 2.0.0-beta.2 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,131 +1,131 @@
1
- import { KnownUser } from "./sdk/KnownUser";
2
- import { QueueITHelper } from "./helper";
3
- import { FastlyHttpContextProvider, getHttpHandler } from "./contextProvider";
4
- import {
5
- getIntegrationConfig,
6
- resolveIntegrationDetails,
7
- IntegrationDetails,
8
- QueueItIntegrationEndpointProvider,
9
- } from "./integrationConfigProvider";
10
- import { Utils } from "./sdk/QueueITHelpers";
11
-
12
- const QUEUEIT_FAILED_HEADERNAME = "x-queueit-failed";
13
- let httpProvider: FastlyHttpContextProvider | null = null;
14
-
15
- export async function onQueueITRequest(
16
- req: Request,
17
- conf: IntegrationDetails | null = null
18
- ): Promise<Response | null> {
19
- if (conf == null) {
20
- conf = resolveIntegrationDetails();
21
- }
22
- if (conf == null) {
23
- return new Response("No integration details found.", {
24
- headers: new Headers(),
25
- status: 404,
26
- });
27
- }
28
-
29
- const integrationProvider =
30
- conf.provider == null
31
- ? new QueueItIntegrationEndpointProvider()
32
- : conf.provider;
33
- QueueITHelper.configureKnownUserHashing();
34
- httpProvider = getHttpHandler(req);
35
-
36
- let integrationConfigJson = await getIntegrationConfig(conf, integrationProvider);
37
- const requestUrl: string = conf.resolveWorkerRequestUrl(req.url);
38
-
39
- const queueItToken = Utils.getParameterByName(
40
- requestUrl,
41
- KnownUser.QueueITTokenKey
42
- );
43
- const requestUrlWithoutToken: string = Utils.removeQueueItToken(requestUrl);
44
-
45
- // The requestUrlWithoutToken is used to match Triggers and as the Target url (where to return the users to).
46
- // It is therefor important that this is exactly the url of the users browsers. So, if your webserver is
47
- // behind e.g. a load balancer that modifies the host name or port, reformat requestUrlWithoutToken before proceeding.
48
- const validationResultPair = KnownUser.validateRequestByIntegrationConfig(
49
- requestUrlWithoutToken,
50
- queueItToken,
51
- integrationConfigJson,
52
- conf.customerId,
53
- conf.secretKey,
54
- httpProvider!
55
- );
56
-
57
- if (
58
- validationResultPair.first != null &&
59
- validationResultPair.first!.doRedirect()
60
- ) {
61
- const validationResult = validationResultPair.first!;
62
-
63
- if (validationResult.isAjaxResult) {
64
- let response = new Response(null, {
65
- status: 200,
66
- headers: httpProvider!.getHttpResponse().getHeaders(),
67
- });
68
- // 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
69
- response.headers.set("Access-Control-Expose-Headers", validationResult.getAjaxQueueRedirectHeaderKey());
70
- response.headers.set(
71
- validationResult.getAjaxQueueRedirectHeaderKey(),
72
- QueueITHelper.addKUPlatformVersion(
73
- validationResult.getAjaxRedirectUrl()
74
- )
75
- );
76
- Utils.addNoCacheHeaders(response);
77
- return response;
78
- } else {
79
- let response = new Response(null, {
80
- status: 302,
81
- headers: httpProvider!.getHttpResponse().getHeaders(),
82
- });
83
- // Send the user to the queue - either because hash was missing or because is was invalid
84
- response.headers.set(
85
- "Location",
86
- QueueITHelper.addKUPlatformVersion(validationResult.redirectUrl)
87
- );
88
- Utils.addNoCacheHeaders(response);
89
- return response;
90
- }
91
- } else if (validationResultPair.first != null) {
92
- const validationResult = validationResultPair.first!;
93
- // Request can continue - we remove queueittoken form querystring parameter to avoid sharing of user specific token
94
- // Support mobile scenario adding the condition !validationResult.isAjaxResult
95
- if (
96
- queueItToken != "" &&
97
- !validationResult.isAjaxResult &&
98
- validationResult.actionType == "Queue"
99
- ) {
100
- let response = new Response(null, {
101
- status: 302,
102
- headers: httpProvider!.getHttpResponse().getHeaders(),
103
- });
104
- response.headers.set("Location", requestUrlWithoutToken);
105
- Utils.addNoCacheHeaders(response);
106
- return response;
107
- } else {
108
- // lets caller decide the next step, or just serve the request normally
109
- return null;
110
- }
111
- } else if (validationResultPair.second != null) {
112
- httpProvider!.isError = true;
113
- }
114
-
115
- return null;
116
- }
117
-
118
- //Fill in the Queue-it headers
119
- export function onQueueITResponse(res: Response): void {
120
- const contextHeaders = httpProvider!.getHttpResponse().getHeaders();
121
-
122
- if (httpProvider!.isError) {
123
- res.headers.append(QUEUEIT_FAILED_HEADERNAME, "true");
124
- }
125
- for (const key of contextHeaders.keys()) {
126
- if (key.length == 0) continue;
127
- const value = contextHeaders.get(key);
128
- if (value != null && value.length > 0)
129
- res.headers.append(key, value);
130
- }
131
- }
1
+ import { KnownUser } from "./sdk/KnownUser";
2
+ import { QueueITHelper } from "./helper";
3
+ import { FastlyHttpContextProvider, getHttpHandler } from "./contextProvider";
4
+ import {
5
+ getIntegrationConfig,
6
+ resolveIntegrationDetails,
7
+ IntegrationDetails,
8
+ QueueItIntegrationEndpointProvider,
9
+ } from "./integrationConfigProvider";
10
+ import { Utils } from "./sdk/QueueITHelpers";
11
+
12
+ const QUEUEIT_FAILED_HEADERNAME = "x-queueit-failed";
13
+ let httpProvider: FastlyHttpContextProvider | null = null;
14
+
15
+ export async function onQueueITRequest(
16
+ req: Request,
17
+ conf: IntegrationDetails | null = null
18
+ ): Promise<Response | null> {
19
+ if (conf == null) {
20
+ conf = resolveIntegrationDetails();
21
+ }
22
+ if (conf == null) {
23
+ return new Response("No integration details found.", {
24
+ headers: new Headers(),
25
+ status: 404,
26
+ });
27
+ }
28
+
29
+ const integrationProvider =
30
+ conf.provider == null
31
+ ? new QueueItIntegrationEndpointProvider()
32
+ : conf.provider;
33
+ QueueITHelper.configureKnownUserHashing();
34
+ httpProvider = getHttpHandler(req);
35
+
36
+ let integrationConfigJson = await getIntegrationConfig(conf, integrationProvider);
37
+ const requestUrl: string = conf.resolveWorkerRequestUrl(req.url);
38
+
39
+ const queueItToken = Utils.getParameterByName(
40
+ requestUrl,
41
+ KnownUser.QueueITTokenKey
42
+ );
43
+ const requestUrlWithoutToken: string = Utils.removeQueueItToken(requestUrl);
44
+
45
+ // The requestUrlWithoutToken is used to match Triggers and as the Target url (where to return the users to).
46
+ // It is therefor important that this is exactly the url of the users browsers. So, if your webserver is
47
+ // behind e.g. a load balancer that modifies the host name or port, reformat requestUrlWithoutToken before proceeding.
48
+ const validationResultPair = KnownUser.validateRequestByIntegrationConfig(
49
+ requestUrlWithoutToken,
50
+ queueItToken,
51
+ integrationConfigJson,
52
+ conf.customerId,
53
+ conf.secretKey,
54
+ httpProvider!
55
+ );
56
+
57
+ if (
58
+ validationResultPair.first != null &&
59
+ validationResultPair.first!.doRedirect()
60
+ ) {
61
+ const validationResult = validationResultPair.first!;
62
+
63
+ if (validationResult.isAjaxResult) {
64
+ let response = new Response(null, {
65
+ status: 200,
66
+ headers: httpProvider!.getHttpResponse().getHeaders(),
67
+ });
68
+ // 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
69
+ response.headers.set("Access-Control-Expose-Headers", validationResult.getAjaxQueueRedirectHeaderKey());
70
+ response.headers.set(
71
+ validationResult.getAjaxQueueRedirectHeaderKey(),
72
+ QueueITHelper.addKUPlatformVersion(
73
+ validationResult.getAjaxRedirectUrl()
74
+ )
75
+ );
76
+ Utils.addNoCacheHeaders(response);
77
+ return response;
78
+ } else {
79
+ let response = new Response(null, {
80
+ status: 302,
81
+ headers: httpProvider!.getHttpResponse().getHeaders(),
82
+ });
83
+ // Send the user to the queue - either because hash was missing or because is was invalid
84
+ response.headers.set(
85
+ "Location",
86
+ QueueITHelper.addKUPlatformVersion(validationResult.redirectUrl)
87
+ );
88
+ Utils.addNoCacheHeaders(response);
89
+ return response;
90
+ }
91
+ } else if (validationResultPair.first != null) {
92
+ const validationResult = validationResultPair.first!;
93
+ // Request can continue - we remove queueittoken form querystring parameter to avoid sharing of user specific token
94
+ // Support mobile scenario adding the condition !validationResult.isAjaxResult
95
+ if (
96
+ queueItToken != "" &&
97
+ !validationResult.isAjaxResult &&
98
+ validationResult.actionType == "Queue"
99
+ ) {
100
+ let response = new Response(null, {
101
+ status: 302,
102
+ headers: httpProvider!.getHttpResponse().getHeaders(),
103
+ });
104
+ response.headers.set("Location", requestUrlWithoutToken);
105
+ Utils.addNoCacheHeaders(response);
106
+ return response;
107
+ } else {
108
+ // lets caller decide the next step, or just serve the request normally
109
+ return null;
110
+ }
111
+ } else if (validationResultPair.second != null) {
112
+ httpProvider!.isError = true;
113
+ }
114
+
115
+ return null;
116
+ }
117
+
118
+ //Fill in the Queue-it headers
119
+ export function onQueueITResponse(res: Response): void {
120
+ const contextHeaders = httpProvider!.getHttpResponse().getHeaders();
121
+
122
+ if (httpProvider!.isError) {
123
+ res.headers.append(QUEUEIT_FAILED_HEADERNAME, "true");
124
+ }
125
+ for (const key of contextHeaders.keys()) {
126
+ if (key.length == 0) continue;
127
+ const value = contextHeaders.get(key);
128
+ if (value != null && value.length > 0)
129
+ res.headers.append(key, value);
130
+ }
131
+ }
@@ -1,23 +1,23 @@
1
-
2
- export interface IHttpRequest {
3
- getUserAgent(): string;
4
- getHeader(name: string): string;
5
- getAbsoluteUri(): string;
6
- getUserHostAddress(): string;
7
- getCookieValue(cookieKey: string): string;
8
- getRequestBodyAsString(): string;
9
- }
10
-
11
- export interface IHttpResponse {
12
- setCookie(cookieName: string, cookieValue: string, domain: string, expiration: number): void;
13
- getHeaders(): Headers;
14
- }
15
-
16
- export interface IHttpContextProvider {
17
- getHttpRequest(): IHttpRequest;
18
- getHttpResponse(): IHttpResponse;
19
- }
20
-
21
- export interface IDateTimeProvider {
22
- getCurrentTime(): Date
23
- }
1
+
2
+ export interface IHttpRequest {
3
+ getUserAgent(): string;
4
+ getHeader(name: string): string;
5
+ getAbsoluteUri(): string;
6
+ getUserHostAddress(): string;
7
+ getCookieValue(cookieKey: string): string;
8
+ getRequestBodyAsString(): string;
9
+ }
10
+
11
+ export interface IHttpResponse {
12
+ setCookie(cookieName: string, cookieValue: string, domain: string, expiration: number): void;
13
+ getHeaders(): Headers;
14
+ }
15
+
16
+ export interface IHttpContextProvider {
17
+ getHttpRequest(): IHttpRequest;
18
+ getHttpResponse(): IHttpResponse;
19
+ }
20
+
21
+ export interface IDateTimeProvider {
22
+ getCurrentTime(): Date
23
+ }
@@ -1,57 +1,57 @@
1
- import {
2
- CustomerIntegration,
3
- IntegrationConfigModel,
4
- TriggerModel,
5
- TriggerPart
6
- } from "./IntegrationConfigModel";
7
-
8
- export class CustomerIntegrationDecodingHandler {
9
- static deserialize(integrationsConfigString: string): CustomerIntegration {
10
- const result = new CustomerIntegration();
11
- if (!integrationsConfigString) return result;
12
-
13
- const parsed = JSON.parse(integrationsConfigString);
14
- result.Version = parsed.Version ?? 0;
15
- result.Description = parsed.Description ?? '';
16
-
17
- if (Array.isArray(parsed.Integrations)) {
18
- result.Integrations = parsed.Integrations.map((item: any) => {
19
- const model = new IntegrationConfigModel();
20
- model.Name = item.Name ?? '';
21
- model.EventId = item.EventId ?? '';
22
- model.CookieDomain = item.CookieDomain ?? '';
23
- model.LayoutName = item.LayoutName ?? '';
24
- model.Culture = item.Culture ?? '';
25
- model.ExtendCookieValidity = item.ExtendCookieValidity ?? false;
26
- model.CookieValidityMinute = item.CookieValidityMinute ?? 0;
27
- model.QueueDomain = item.QueueDomain ?? '';
28
- model.RedirectLogic = item.RedirectLogic ?? '';
29
- model.ForcedTargetUrl = item.ForcedTargetUrl ?? '';
30
- model.ActionType = item.ActionType ?? '';
31
-
32
- model.Triggers = (item.Triggers ?? []).map((trigger: any) => {
33
- const t = new TriggerModel();
34
- t.LogicalOperator = trigger.LogicalOperator ?? '';
35
- t.TriggerParts = (trigger.TriggerParts ?? []).map((part: any) => {
36
- const tp = new TriggerPart();
37
- tp.ValidatorType = part.ValidatorType ?? '';
38
- tp.Operator = part.Operator ?? '';
39
- tp.ValueToCompare = part.ValueToCompare ?? '';
40
- tp.ValuesToCompare = part.ValuesToCompare ?? [];
41
- tp.IsNegative = part.IsNegative ?? false;
42
- tp.IsIgnoreCase = part.IsIgnoreCase ?? false;
43
- tp.UrlPart = part.UrlPart ?? '';
44
- tp.CookieName = part.CookieName ?? '';
45
- tp.HttpHeaderName = part.HttpHeaderName ?? '';
46
- return tp;
47
- });
48
- return t;
49
- });
50
-
51
- return model;
52
- });
53
- }
54
-
55
- return result;
56
- }
57
- }
1
+ import {
2
+ CustomerIntegration,
3
+ IntegrationConfigModel,
4
+ TriggerModel,
5
+ TriggerPart
6
+ } from "./IntegrationConfigModel";
7
+
8
+ export class CustomerIntegrationDecodingHandler {
9
+ static deserialize(integrationsConfigString: string): CustomerIntegration {
10
+ const result = new CustomerIntegration();
11
+ if (!integrationsConfigString) return result;
12
+
13
+ const parsed = JSON.parse(integrationsConfigString);
14
+ result.Version = parsed.Version ?? 0;
15
+ result.Description = parsed.Description ?? '';
16
+
17
+ if (Array.isArray(parsed.Integrations)) {
18
+ result.Integrations = parsed.Integrations.map((item: any) => {
19
+ const model = new IntegrationConfigModel();
20
+ model.Name = item.Name ?? '';
21
+ model.EventId = item.EventId ?? '';
22
+ model.CookieDomain = item.CookieDomain ?? '';
23
+ model.LayoutName = item.LayoutName ?? '';
24
+ model.Culture = item.Culture ?? '';
25
+ model.ExtendCookieValidity = item.ExtendCookieValidity ?? false;
26
+ model.CookieValidityMinute = item.CookieValidityMinute ?? 0;
27
+ model.QueueDomain = item.QueueDomain ?? '';
28
+ model.RedirectLogic = item.RedirectLogic ?? '';
29
+ model.ForcedTargetUrl = item.ForcedTargetUrl ?? '';
30
+ model.ActionType = item.ActionType ?? '';
31
+
32
+ model.Triggers = (item.Triggers ?? []).map((trigger: any) => {
33
+ const t = new TriggerModel();
34
+ t.LogicalOperator = trigger.LogicalOperator ?? '';
35
+ t.TriggerParts = (trigger.TriggerParts ?? []).map((part: any) => {
36
+ const tp = new TriggerPart();
37
+ tp.ValidatorType = part.ValidatorType ?? '';
38
+ tp.Operator = part.Operator ?? '';
39
+ tp.ValueToCompare = part.ValueToCompare ?? '';
40
+ tp.ValuesToCompare = part.ValuesToCompare ?? [];
41
+ tp.IsNegative = part.IsNegative ?? false;
42
+ tp.IsIgnoreCase = part.IsIgnoreCase ?? false;
43
+ tp.UrlPart = part.UrlPart ?? '';
44
+ tp.CookieName = part.CookieName ?? '';
45
+ tp.HttpHeaderName = part.HttpHeaderName ?? '';
46
+ return tp;
47
+ });
48
+ return t;
49
+ });
50
+
51
+ return model;
52
+ });
53
+ }
54
+
55
+ return result;
56
+ }
57
+ }