@queue-it/fastly 1.0.4 → 1.1.0

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,137 +1,137 @@
1
- import { Request, Response, Headers } from "@fastly/as-compute";
2
- import { KnownUser } from "./sdk/KnownUser";
3
- import { QueueITHelper } from "./helper";
4
- import { FastlyHttpContextProvider, getHttpHandler } from "./contextProvider";
5
- import {
6
- getIntegrationConfig,
7
- resolveIntegrationDetails,
8
- IntegrationDetails,
9
- QueueItIntegrationEndpointProvider,
10
- } from "./integrationConfigProvider";
11
- import { Utils } from "./sdk/QueueITHelpers";
12
-
13
- const QUEUEIT_FAILED_HEADERNAME = "x-queueit-failed";
14
- let httpProvider: FastlyHttpContextProvider | null = null;
15
-
16
- export function onQueueITRequest(
17
- req: Request,
18
- conf: IntegrationDetails | null = null
19
- ): Response | null {
20
- if (conf == null) {
21
- conf = resolveIntegrationDetails();
22
- }
23
- if (conf == null) {
24
- return new Response(String.UTF8.encode("No integration details found."), {
25
- headers: new Headers(),
26
- status: 404,
27
- url: "",
28
- });
29
- }
30
-
31
- const integrationProvider =
32
- conf.provider == null
33
- ? new QueueItIntegrationEndpointProvider()
34
- : conf.provider;
35
- QueueITHelper.configureKnownUserHashing();
36
- httpProvider = getHttpHandler(req);
37
-
38
- let integrationConfigJson = getIntegrationConfig(conf, integrationProvider);
39
- const requestUrl: string = conf.resolveWorkerRequestUrl(req.url);
40
-
41
- const queueItToken = Utils.getParameterByName(
42
- requestUrl,
43
- KnownUser.QueueITTokenKey
44
- );
45
- const requestUrlWithoutToken: string = Utils.removeQueueItToken(requestUrl);
46
-
47
- // The requestUrlWithoutToken is used to match Triggers and as the Target url (where to return the users to).
48
- // It is therefor important that this is exactly the url of the users browsers. So, if your webserver is
49
- // behind e.g. a load balancer that modifies the host name or port, reformat requestUrlWithoutToken before proceeding.
50
- const validationResultPair = KnownUser.validateRequestByIntegrationConfig(
51
- requestUrlWithoutToken,
52
- queueItToken,
53
- integrationConfigJson,
54
- conf.customerId,
55
- conf.secretKey,
56
- httpProvider!
57
- );
58
-
59
- if (
60
- validationResultPair.first != null &&
61
- validationResultPair.first!.doRedirect()
62
- ) {
63
- const validationResult = validationResultPair.first!;
64
-
65
- if (validationResult.isAjaxResult) {
66
- let response = new Response(null, {
67
- status: 200,
68
- headers: httpProvider!.getHttpResponse().getHeaders(),
69
- url: "",
70
- });
71
- // 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
72
- response.headers.set("Access-Control-Expose-Headers", validationResult.getAjaxQueueRedirectHeaderKey());
73
- response.headers.set(
74
- validationResult.getAjaxQueueRedirectHeaderKey(),
75
- QueueITHelper.addKUPlatformVersion(
76
- validationResult.getAjaxRedirectUrl()
77
- )
78
- );
79
- Utils.addNoCacheHeaders(response);
80
- return response;
81
- } else {
82
- let response = new Response(null, {
83
- status: 302,
84
- headers: httpProvider!.getHttpResponse().getHeaders(),
85
- url: "",
86
- });
87
- // Send the user to the queue - either because hash was missing or because is was invalid
88
- response.headers.set(
89
- "Location",
90
- QueueITHelper.addKUPlatformVersion(validationResult.redirectUrl)
91
- );
92
- Utils.addNoCacheHeaders(response);
93
- return response;
94
- }
95
- } else if (validationResultPair.first != null) {
96
- const validationResult = validationResultPair.first!;
97
- // Request can continue - we remove queueittoken form querystring parameter to avoid sharing of user specific token
98
- // Support mobile scenario adding the condition !validationResult.isAjaxResult
99
- if (
100
- queueItToken != "" &&
101
- !validationResult.isAjaxResult &&
102
- validationResult.actionType == "Queue"
103
- ) {
104
- let response = new Response(null, {
105
- status: 302,
106
- headers: httpProvider!.getHttpResponse().getHeaders(),
107
- url: requestUrlWithoutToken,
108
- });
109
- response.headers.set("Location", requestUrlWithoutToken);
110
- Utils.addNoCacheHeaders(response);
111
- return response;
112
- } else {
113
- // lets caller decide the next step, or just serve the request normally
114
- return null;
115
- }
116
- } else if (validationResultPair.second != null) {
117
- httpProvider!.isError = true;
118
- }
119
-
120
- return null;
121
- }
122
-
123
- //Fill in the Queue-it headers
124
- export function onQueueITResponse(res: Response): void {
125
- const contextHeaders = httpProvider!.getHttpResponse().getHeaders();
126
- const contextHeaderKeys = contextHeaders.keys();
127
-
128
- if (httpProvider!.isError) {
129
- res.headers.append(QUEUEIT_FAILED_HEADERNAME, "true");
130
- }
131
- for (let i = 0; i < contextHeaderKeys.length; i++) {
132
- if (contextHeaderKeys[i].length == 0) continue;
133
- let value = contextHeaders.get(contextHeaderKeys[i]);
134
- if (value != null && value!.length > 0)
135
- res.headers.append(contextHeaderKeys[i], value!);
136
- }
137
- }
1
+ import { Request, Response, Headers } from "@fastly/as-compute";
2
+ import { KnownUser } from "./sdk/KnownUser";
3
+ import { QueueITHelper } from "./helper";
4
+ import { FastlyHttpContextProvider, getHttpHandler } from "./contextProvider";
5
+ import {
6
+ getIntegrationConfig,
7
+ resolveIntegrationDetails,
8
+ IntegrationDetails,
9
+ QueueItIntegrationEndpointProvider,
10
+ } from "./integrationConfigProvider";
11
+ import { Utils } from "./sdk/QueueITHelpers";
12
+
13
+ const QUEUEIT_FAILED_HEADERNAME = "x-queueit-failed";
14
+ let httpProvider: FastlyHttpContextProvider | null = null;
15
+
16
+ export function onQueueITRequest(
17
+ req: Request,
18
+ conf: IntegrationDetails | null = null
19
+ ): Response | null {
20
+ if (conf == null) {
21
+ conf = resolveIntegrationDetails();
22
+ }
23
+ if (conf == null) {
24
+ return new Response(String.UTF8.encode("No integration details found."), {
25
+ headers: new Headers(),
26
+ status: 404,
27
+ url: "",
28
+ });
29
+ }
30
+
31
+ const integrationProvider =
32
+ conf.provider == null
33
+ ? new QueueItIntegrationEndpointProvider()
34
+ : conf.provider;
35
+ QueueITHelper.configureKnownUserHashing();
36
+ httpProvider = getHttpHandler(req);
37
+
38
+ let integrationConfigJson = getIntegrationConfig(conf, integrationProvider);
39
+ const requestUrl: string = conf.resolveWorkerRequestUrl(req.url);
40
+
41
+ const queueItToken = Utils.getParameterByName(
42
+ requestUrl,
43
+ KnownUser.QueueITTokenKey
44
+ );
45
+ const requestUrlWithoutToken: string = Utils.removeQueueItToken(requestUrl);
46
+
47
+ // The requestUrlWithoutToken is used to match Triggers and as the Target url (where to return the users to).
48
+ // It is therefor important that this is exactly the url of the users browsers. So, if your webserver is
49
+ // behind e.g. a load balancer that modifies the host name or port, reformat requestUrlWithoutToken before proceeding.
50
+ const validationResultPair = KnownUser.validateRequestByIntegrationConfig(
51
+ requestUrlWithoutToken,
52
+ queueItToken,
53
+ integrationConfigJson,
54
+ conf.customerId,
55
+ conf.secretKey,
56
+ httpProvider!
57
+ );
58
+
59
+ if (
60
+ validationResultPair.first != null &&
61
+ validationResultPair.first!.doRedirect()
62
+ ) {
63
+ const validationResult = validationResultPair.first!;
64
+
65
+ if (validationResult.isAjaxResult) {
66
+ let response = new Response(null, {
67
+ status: 200,
68
+ headers: httpProvider!.getHttpResponse().getHeaders(),
69
+ url: "",
70
+ });
71
+ // 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
72
+ response.headers.set("Access-Control-Expose-Headers", validationResult.getAjaxQueueRedirectHeaderKey());
73
+ response.headers.set(
74
+ validationResult.getAjaxQueueRedirectHeaderKey(),
75
+ QueueITHelper.addKUPlatformVersion(
76
+ validationResult.getAjaxRedirectUrl()
77
+ )
78
+ );
79
+ Utils.addNoCacheHeaders(response);
80
+ return response;
81
+ } else {
82
+ let response = new Response(null, {
83
+ status: 302,
84
+ headers: httpProvider!.getHttpResponse().getHeaders(),
85
+ url: "",
86
+ });
87
+ // Send the user to the queue - either because hash was missing or because is was invalid
88
+ response.headers.set(
89
+ "Location",
90
+ QueueITHelper.addKUPlatformVersion(validationResult.redirectUrl)
91
+ );
92
+ Utils.addNoCacheHeaders(response);
93
+ return response;
94
+ }
95
+ } else if (validationResultPair.first != null) {
96
+ const validationResult = validationResultPair.first!;
97
+ // Request can continue - we remove queueittoken form querystring parameter to avoid sharing of user specific token
98
+ // Support mobile scenario adding the condition !validationResult.isAjaxResult
99
+ if (
100
+ queueItToken != "" &&
101
+ !validationResult.isAjaxResult &&
102
+ validationResult.actionType == "Queue"
103
+ ) {
104
+ let response = new Response(null, {
105
+ status: 302,
106
+ headers: httpProvider!.getHttpResponse().getHeaders(),
107
+ url: requestUrlWithoutToken,
108
+ });
109
+ response.headers.set("Location", requestUrlWithoutToken);
110
+ Utils.addNoCacheHeaders(response);
111
+ return response;
112
+ } else {
113
+ // lets caller decide the next step, or just serve the request normally
114
+ return null;
115
+ }
116
+ } else if (validationResultPair.second != null) {
117
+ httpProvider!.isError = true;
118
+ }
119
+
120
+ return null;
121
+ }
122
+
123
+ //Fill in the Queue-it headers
124
+ export function onQueueITResponse(res: Response): void {
125
+ const contextHeaders = httpProvider!.getHttpResponse().getHeaders();
126
+ const contextHeaderKeys = contextHeaders.keys();
127
+
128
+ if (httpProvider!.isError) {
129
+ res.headers.append(QUEUEIT_FAILED_HEADERNAME, "true");
130
+ }
131
+ for (let i = 0; i < contextHeaderKeys.length; i++) {
132
+ if (contextHeaderKeys[i].length == 0) continue;
133
+ let value = contextHeaders.get(contextHeaderKeys[i]);
134
+ if (value != null && value!.length > 0)
135
+ res.headers.append(contextHeaderKeys[i], value!);
136
+ }
137
+ }
@@ -1,24 +1,24 @@
1
- import { Headers } from "@fastly/as-compute";
2
-
3
- export interface IHttpRequest {
4
- getUserAgent(): string;
5
- getHeader(name: string): string;
6
- getAbsoluteUri(): string;
7
- getUserHostAddress(): string;
8
- getCookieValue(cookieKey: string): string;
9
- getRequestBodyAsString(): string;
10
- }
11
-
12
- export interface IHttpResponse {
13
- setCookie(cookieName: string, cookieValue: string, domain: string, expiration: i64): void;
14
- getHeaders(): Headers;
15
- }
16
-
17
- export interface IHttpContextProvider {
18
- getHttpRequest(): IHttpRequest;
19
- getHttpResponse(): IHttpResponse;
20
- }
21
-
22
- export interface IDateTimeProvider {
23
- getCurrentTime(): Date
24
- }
1
+ import { Headers } from "@fastly/as-compute";
2
+
3
+ export interface IHttpRequest {
4
+ getUserAgent(): string;
5
+ getHeader(name: string): string;
6
+ getAbsoluteUri(): string;
7
+ getUserHostAddress(): string;
8
+ getCookieValue(cookieKey: string): string;
9
+ getRequestBodyAsString(): string;
10
+ }
11
+
12
+ export interface IHttpResponse {
13
+ setCookie(cookieName: string, cookieValue: string, domain: string, expiration: i64): void;
14
+ getHeaders(): Headers;
15
+ }
16
+
17
+ export interface IHttpContextProvider {
18
+ getHttpRequest(): IHttpRequest;
19
+ getHttpResponse(): IHttpResponse;
20
+ }
21
+
22
+ export interface IDateTimeProvider {
23
+ getCurrentTime(): Date
24
+ }