@queue-it/fastly 2.0.3-beta.1 → 2.0.3-beta.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@queue-it/fastly",
3
- "version": "2.0.3-beta.1",
3
+ "version": "2.0.3-beta.3",
4
4
  "description": "Queue-it connector for Fastly",
5
5
  "main": "src/index.ts",
6
6
  "author": "devs@queue-it.com",
@@ -30,6 +30,10 @@ export class FastlyHttpContextProvider implements IConnectorContextProvider {
30
30
  getEnqueueTokenProvider(): null {
31
31
  return null;
32
32
  }
33
+
34
+ getResponseHeaders(): Headers {
35
+ return (this.res as FastlyHttpResponse).getHeaders();
36
+ }
33
37
  }
34
38
 
35
39
  export class FastlyHttpRequest implements IHttpRequest {
@@ -57,14 +61,6 @@ export class FastlyHttpRequest implements IHttpRequest {
57
61
  return;
58
62
  }
59
63
  this.bodyFetched = true;
60
- const rawBody: ArrayBuffer | null = null; // this.context.req.arrayBuffer();
61
- if (rawBody == null) {
62
- return;
63
- }
64
- if (rawBody!.byteLength == 0) {
65
- return;
66
- }
67
-
68
64
  this.body = ''; // this.context.req.text()
69
65
  }
70
66
 
@@ -1,4 +1,4 @@
1
- import { KnownUser, Utils } from "@queue-it/connector-javascript";
1
+ import { KnownUser } from "@queue-it/connector-javascript";
2
2
  import { QueueITHelper } from "./helper";
3
3
  import { FastlyHttpContextProvider, getHttpHandler } from "./contextProvider";
4
4
  import {
@@ -8,6 +8,29 @@ import {
8
8
  QueueItIntegrationEndpointProvider,
9
9
  } from "./integrationConfigProvider";
10
10
 
11
+ function getParameterByName(url: string, name: string): string {
12
+ name = name.replace(/[\[\]]/g, '\\$&');
13
+ const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
14
+ const results = regex.exec(url);
15
+ if (!results) return '';
16
+ if (!results[2]) return '';
17
+ return results[2].replace(/\+/g, ' ');
18
+ }
19
+
20
+ function removeQueueItToken(url: string): string {
21
+ const pattern = new RegExp("([?&])(" + KnownUser.QueueITTokenKey + "=[^&]*)", 'gi');
22
+ const match = pattern.exec(url);
23
+ if (match === null) return url;
24
+ url = url.replaceAll(match[0], '');
25
+ return url;
26
+ }
27
+
28
+ function addNoCacheHeaders(res: Response): void {
29
+ res.headers.set('Cache-Control', 'no-cache, no-store, must-revalidate, max-age=0');
30
+ res.headers.set('Pragma', 'no-cache');
31
+ res.headers.set('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT');
32
+ }
33
+
11
34
  const QUEUEIT_FAILED_HEADERNAME = "x-queueit-failed";
12
35
  let httpProvider: FastlyHttpContextProvider | null = null;
13
36
 
@@ -34,79 +57,72 @@ export async function onQueueITRequest(
34
57
  let integrationConfigJson = await getIntegrationConfig(conf, integrationProvider);
35
58
  const requestUrl: string = conf.resolveWorkerRequestUrl(req.url);
36
59
 
37
- const queueItToken = Utils.getParameterByName(
38
- requestUrl,
39
- KnownUser.QueueITTokenKey
40
- );
41
- const requestUrlWithoutToken: string = Utils.removeQueueItToken(requestUrl);
60
+ const queueItToken = getParameterByName(requestUrl, KnownUser.QueueITTokenKey);
61
+ const requestUrlWithoutToken: string = removeQueueItToken(requestUrl);
42
62
 
43
63
  // The requestUrlWithoutToken is used to match Triggers and as the Target url (where to return the users to).
44
64
  // It is therefor important that this is exactly the url of the users browsers. So, if your webserver is
45
65
  // behind e.g. a load balancer that modifies the host name or port, reformat requestUrlWithoutToken before proceeding.
46
- const validationResultPair = KnownUser.validateRequestByIntegrationConfig(
47
- requestUrlWithoutToken,
48
- queueItToken,
49
- integrationConfigJson,
50
- conf.customerId,
51
- conf.secretKey,
52
- httpProvider!
53
- );
54
-
55
- if (
56
- validationResultPair.first != null &&
57
- validationResultPair.first!.doRedirect()
58
- ) {
59
- const validationResult = validationResultPair.first!;
66
+ try {
67
+ const validationResult = await KnownUser.validateRequestByIntegrationConfig(
68
+ requestUrlWithoutToken,
69
+ queueItToken,
70
+ integrationConfigJson,
71
+ conf.customerId,
72
+ conf.secretKey,
73
+ httpProvider!
74
+ );
60
75
 
61
- if (validationResult.isAjaxResult) {
62
- let response = new Response(null, {
63
- status: 200,
64
- headers: httpProvider!.getHttpResponse().getHeaders(),
65
- });
66
- // 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
67
- response.headers.set("Access-Control-Expose-Headers", validationResult.getAjaxQueueRedirectHeaderKey());
68
- response.headers.set(
69
- validationResult.getAjaxQueueRedirectHeaderKey(),
70
- QueueITHelper.addKUPlatformVersion(
71
- validationResult.getAjaxRedirectUrl()
72
- )
73
- );
74
- Utils.addNoCacheHeaders(response);
75
- return response;
76
- } else {
77
- let response = new Response(null, {
78
- status: 302,
79
- headers: httpProvider!.getHttpResponse().getHeaders(),
80
- });
81
- // Send the user to the queue - either because hash was missing or because is was invalid
82
- response.headers.set(
83
- "Location",
84
- QueueITHelper.addKUPlatformVersion(validationResult.redirectUrl)
85
- );
86
- Utils.addNoCacheHeaders(response);
87
- return response;
88
- }
89
- } else if (validationResultPair.first != null) {
90
- const validationResult = validationResultPair.first!;
91
- // Request can continue - we remove queueittoken form querystring parameter to avoid sharing of user specific token
92
- // Support mobile scenario adding the condition !validationResult.isAjaxResult
93
- if (
94
- queueItToken != "" &&
95
- !validationResult.isAjaxResult &&
96
- validationResult.actionType == "Queue"
97
- ) {
98
- let response = new Response(null, {
99
- status: 302,
100
- headers: httpProvider!.getHttpResponse().getHeaders(),
101
- });
102
- response.headers.set("Location", requestUrlWithoutToken);
103
- Utils.addNoCacheHeaders(response);
104
- return response;
76
+ if (validationResult.doRedirect()) {
77
+ if (validationResult.isAjaxResult) {
78
+ let response = new Response(null, {
79
+ status: 200,
80
+ headers: httpProvider!.getResponseHeaders(),
81
+ });
82
+ // 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
83
+ response.headers.set("Access-Control-Expose-Headers", validationResult.getAjaxQueueRedirectHeaderKey());
84
+ response.headers.set(
85
+ validationResult.getAjaxQueueRedirectHeaderKey(),
86
+ QueueITHelper.addKUPlatformVersion(
87
+ validationResult.getAjaxRedirectUrl()
88
+ )
89
+ );
90
+ addNoCacheHeaders(response);
91
+ return response;
92
+ } else {
93
+ let response = new Response(null, {
94
+ status: 302,
95
+ headers: httpProvider!.getResponseHeaders(),
96
+ });
97
+ // Send the user to the queue - either because hash was missing or because is was invalid
98
+ response.headers.set(
99
+ "Location",
100
+ QueueITHelper.addKUPlatformVersion(validationResult.redirectUrl)
101
+ );
102
+ addNoCacheHeaders(response);
103
+ return response;
104
+ }
105
105
  } else {
106
- // lets caller decide the next step, or just serve the request normally
107
- return null;
106
+ // Request can continue - we remove queueittoken from querystring parameter to avoid sharing of user specific token
107
+ // Support mobile scenario adding the condition !validationResult.isAjaxResult
108
+ if (
109
+ queueItToken != "" &&
110
+ !validationResult.isAjaxResult &&
111
+ validationResult.actionType == "Queue"
112
+ ) {
113
+ let response = new Response(null, {
114
+ status: 302,
115
+ headers: httpProvider!.getResponseHeaders(),
116
+ });
117
+ response.headers.set("Location", requestUrlWithoutToken);
118
+ addNoCacheHeaders(response);
119
+ return response;
120
+ } else {
121
+ // lets caller decide the next step, or just serve the request normally
122
+ return null;
123
+ }
108
124
  }
109
- } else if (validationResultPair.second != null) {
125
+ } catch {
110
126
  httpProvider!.isError = true;
111
127
  }
112
128
 
@@ -115,7 +131,7 @@ export async function onQueueITRequest(
115
131
 
116
132
  //Fill in the Queue-it headers
117
133
  export function onQueueITResponse(res: Response): void {
118
- const contextHeaders = httpProvider!.getHttpResponse().getHeaders();
134
+ const contextHeaders = httpProvider!.getResponseHeaders();
119
135
 
120
136
  if (httpProvider!.isError) {
121
137
  res.headers.append(QUEUEIT_FAILED_HEADERNAME, "true");