@queue-it/fastly 2.0.2 → 2.0.3-beta.1
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/README.md +134 -134
- package/package.json +6 -11
- package/src/contextProvider.ts +134 -126
- package/src/fastlyCryptoProvider.ts +8 -0
- package/src/helper.ts +1 -8
- package/src/{sdk/helpers → helpers}/crypto.ts +338 -338
- package/src/index.ts +3 -3
- package/src/integrationConfigProvider.ts +1 -0
- package/src/requestResponseHandler.ts +129 -131
- package/LICENSE +0 -21
- package/src/sdk/HttpContextProvider.ts +0 -23
- package/src/sdk/IntegrationConfig/CustomerIntegrationDecodingHandler.ts +0 -57
- package/src/sdk/IntegrationConfig/IntegrationConfigHelpers.ts +0 -230
- package/src/sdk/IntegrationConfig/IntegrationConfigModel.ts +0 -93
- package/src/sdk/KnownUser.ts +0 -393
- package/src/sdk/Models.ts +0 -105
- package/src/sdk/QueueITHelpers.ts +0 -267
- package/src/sdk/UserInQueueService.ts +0 -245
- package/src/sdk/UserInQueueStateCookieRepository.ts +0 -188
- package/src/sdk/helpers/Uri.ts +0 -4
package/README.md
CHANGED
|
@@ -1,134 +1,134 @@
|
|
|
1
|
-
# KnownUser.V3.Fastly
|
|
2
|
-
|
|
3
|
-
The Queue-it Security Framework ensures that end-users are not able to access your online application without first
|
|
4
|
-
going through the queue for any and all `protected` areas and paths on your sites. The queue system is implemented by
|
|
5
|
-
adding a server-side (request-level) integration that protects your online application by redirecting users to a waiting
|
|
6
|
-
room according to web traffic settings in the Queue-it GO Platform. After the integration is complete, queue system
|
|
7
|
-
behavior and operations are managed in Queue-it's Go Platform and/or via the Queue-it Admin API.
|
|
8
|
-
|
|
9
|
-
This Fastly Queue-it Connector SDK (aka, Queue-it's server-side KnownUser connector) uses a Compute@Edge service to
|
|
10
|
-
integrate Queue-it's functionality into Fastly's network.
|
|
11
|
-
|
|
12
|
-
A Wasm service is required to utilize this connector.
|
|
13
|
-
|
|
14
|
-
> You can find the latest released version [here](https://github.com/queueit/KnownUser.V3.Fastly/releases/latest).
|
|
15
|
-
|
|
16
|
-
## Introduction
|
|
17
|
-
|
|
18
|
-
When a user makes a request to your Fastly service our connector validates the request and if it is needed, it will
|
|
19
|
-
redirect the user to the waiting room. After waiting in the waiting room, the queue engine will redirect the user back
|
|
20
|
-
to your end attaching a query string parameter ( `queueittoken` ) containing some information about the user to the URL.
|
|
21
|
-
The most important fields of the `queueittoken` are:
|
|
22
|
-
|
|
23
|
-
- q - The user's unique queue identifier
|
|
24
|
-
- ts - A timestamp of how long this redirect is valid
|
|
25
|
-
- h - A hash of the token
|
|
26
|
-
|
|
27
|
-
After the user returns from the queue, the connector will let the user continue his request to your backend ( without
|
|
28
|
-
redirecting to the queue since the request has a valid queueittoken as query string) .
|
|
29
|
-
|
|
30
|
-
## Installation
|
|
31
|
-
|
|
32
|
-
There are two methods of installation:
|
|
33
|
-
|
|
34
|
-
### As a standalone service
|
|
35
|
-
|
|
36
|
-
- Go to the Fastly services page and create a new **Wasm** service.
|
|
37
|
-
- Go to Domains and fill in the domain that you want your service to be reachable at. You may need to register a CNAME
|
|
38
|
-
record if you have your own domain.
|
|
39
|
-
- Then click on *Origins* and add a new host that has the hostname of your origin server.
|
|
40
|
-
You need to edit the Host and name it **origin**.
|
|
41
|
-
- Create a second host that has the hostname of `{yourCustomerId}.queue-it.net` and name it **queue-it**.
|
|
42
|
-
Edit the host, go to advanced options and fill in the same hostname in **Override host**
|
|
43
|
-
- Go to **Resources -> Config stores** and create a new config store named `IntegrationConfiguration`.
|
|
44
|
-
- Add the following items in the config store (you can find these values in the Go Queue-It self-service platform):
|
|
45
|
-
- customerId: Your customer ID
|
|
46
|
-
- apiKey: The API key for your account
|
|
47
|
-
- secret: Your KnownUserV3 secret
|
|
48
|
-
- queueItOrigin: The name of the queue-it host, in this case it's `queue-it`
|
|
49
|
-
- Go to **Linked Services** and click on `Link Service`
|
|
50
|
-
- Select your service and click `Next`
|
|
51
|
-
- Click `Link and activate` to link the `Config store` to your service. It should generate a new version
|
|
52
|
-
- You can verify that the `Config store` has been linked by going to your service and see it in the section `Config stores`
|
|
53
|
-
- Download the latest package from the releases page and unarchive it.
|
|
54
|
-
- Edit the `fastly.toml` file and copy the ID of your service (you can see this by opening up the service in Fastly) and
|
|
55
|
-
replace **{YourServiceId}** with it.
|
|
56
|
-
- Archive the directory in the same format (tar.gz).
|
|
57
|
-
- Go to `Package` in the Fastly service page and upload the package.
|
|
58
|
-
- To finish up and deploy your service click on the **Activate** button.
|
|
59
|
-
|
|
60
|
-
### Customizable service with the connector
|
|
61
|
-
|
|
62
|
-
- Go to the Fastly services page and create a new **Wasm** service and copy it's ID.
|
|
63
|
-
- Clone this repository and edit the fastly.toml file, make sure to set the `service_id` field to the ID you copied.
|
|
64
|
-
- Then click on *Origins* and add a new host that has the hostname of your origin server.
|
|
65
|
-
You can name the host **origin** or whatever you choose.
|
|
66
|
-
- Create a host that has the hostname of `{yourCustomerId}.queue-it.net` and name it **queue-it**.
|
|
67
|
-
Edit the host, go to advanced options and fill in the same hostname in **Override host**
|
|
68
|
-
- Go to **Resources -> Config stores** and create a new config store named `IntegrationConfiguration`.
|
|
69
|
-
- Add the following items in the config store (you can find these values in the Go Queue-It self-service platform):
|
|
70
|
-
- customerId: Your customer ID
|
|
71
|
-
- apiKey: The API key for your account
|
|
72
|
-
- secret: Your KnownUserV3 secret
|
|
73
|
-
- queueItOrigin: The name of the queue-it host, in this case it's `queue-it`
|
|
74
|
-
- Go to **Linked Services** and click on `Link Service`
|
|
75
|
-
- Select your service and click `Next`
|
|
76
|
-
- Click `Link and activate` to link the `Config store` to your service. It should generate a new version
|
|
77
|
-
- You can verify that the `Config store` has been linked by going to your service and see it in the section `Config stores`
|
|
78
|
-
- You need to add some code that uses this connector. Here is an example:
|
|
79
|
-
|
|
80
|
-
```ts
|
|
81
|
-
import {Fastly} from "@fastly/as-compute";
|
|
82
|
-
import {onQueueITRequest, IntegrationDetails, onQueueITResponse} from "@queue-it/fastly";
|
|
83
|
-
|
|
84
|
-
const req = Fastly.getClientRequest();
|
|
85
|
-
|
|
86
|
-
// This is optional and can be null if it's specified in your Config Store
|
|
87
|
-
const integrationDetails = new IntegrationDetails(
|
|
88
|
-
"QueueItOriginName",
|
|
89
|
-
"CustomerId",
|
|
90
|
-
"SecretKey",
|
|
91
|
-
"ApiKey");
|
|
92
|
-
let res = onQueueITRequest(req, integrationDetails);
|
|
93
|
-
|
|
94
|
-
if (res != null) {
|
|
95
|
-
Fastly.respondWith(res!);
|
|
96
|
-
} else {
|
|
97
|
-
const myOrigin = 'Ticketania';
|
|
98
|
-
const cacheOverride = new Fastly.CacheOverride();
|
|
99
|
-
const res = Fastly.fetch(req, {
|
|
100
|
-
backend: myOrigin,
|
|
101
|
-
cacheOverride,
|
|
102
|
-
}).wait();
|
|
103
|
-
onQueueITResponse(res);
|
|
104
|
-
Fastly.respondWith(res);
|
|
105
|
-
}
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
- Build and deploy the package running `fastly compute build` and `fastly compute deploy` in the same directory.
|
|
109
|
-
- Create desired waiting room(s), triggers, and actions in the Go Queue-It self-service platform.
|
|
110
|
-
Then, save/publish the configuration.
|
|
111
|
-
|
|
112
|
-
## Providing the queue configuration
|
|
113
|
-
|
|
114
|
-
The recommended way is to use the Go Queue-it self-service portal to setup the configuration. The configuration
|
|
115
|
-
specifies a set of Triggers and Actions. A Trigger is an expression matching one, more or all URLs on your website. When
|
|
116
|
-
a user enter your website and the URL matches a Trigger-expression the corresponding Action will be triggered. The
|
|
117
|
-
Action specifies which waiting room the users should be send to. In this way you can specify which waiting room(s)
|
|
118
|
-
should protect which page(s) on the fly without changing the server-side integration.
|
|
119
|
-
|
|
120
|
-
## Protecting AJAX calls
|
|
121
|
-
|
|
122
|
-
If you need to protect AJAX calls beside page loads you need to add the below JavaScript tags to your pages:
|
|
123
|
-
|
|
124
|
-
```html
|
|
125
|
-
|
|
126
|
-
<script type="text/javascript" src="//static.queue-it.net/script/queueclient.min.js"></script>
|
|
127
|
-
<script
|
|
128
|
-
data-queueit-intercept-domain="{YOUR_CURRENT_DOMAIN}"
|
|
129
|
-
data-queueit-intercept="true"
|
|
130
|
-
data-queueit-c="{YOUR_CUSTOMER_ID}"
|
|
131
|
-
type="text/javascript"
|
|
132
|
-
src="//static.queue-it.net/script/queueconfigloader.min.js">
|
|
133
|
-
</script>
|
|
134
|
-
```
|
|
1
|
+
# KnownUser.V3.Fastly
|
|
2
|
+
|
|
3
|
+
The Queue-it Security Framework ensures that end-users are not able to access your online application without first
|
|
4
|
+
going through the queue for any and all `protected` areas and paths on your sites. The queue system is implemented by
|
|
5
|
+
adding a server-side (request-level) integration that protects your online application by redirecting users to a waiting
|
|
6
|
+
room according to web traffic settings in the Queue-it GO Platform. After the integration is complete, queue system
|
|
7
|
+
behavior and operations are managed in Queue-it's Go Platform and/or via the Queue-it Admin API.
|
|
8
|
+
|
|
9
|
+
This Fastly Queue-it Connector SDK (aka, Queue-it's server-side KnownUser connector) uses a Compute@Edge service to
|
|
10
|
+
integrate Queue-it's functionality into Fastly's network.
|
|
11
|
+
|
|
12
|
+
A Wasm service is required to utilize this connector.
|
|
13
|
+
|
|
14
|
+
> You can find the latest released version [here](https://github.com/queueit/KnownUser.V3.Fastly/releases/latest).
|
|
15
|
+
|
|
16
|
+
## Introduction
|
|
17
|
+
|
|
18
|
+
When a user makes a request to your Fastly service our connector validates the request and if it is needed, it will
|
|
19
|
+
redirect the user to the waiting room. After waiting in the waiting room, the queue engine will redirect the user back
|
|
20
|
+
to your end attaching a query string parameter ( `queueittoken` ) containing some information about the user to the URL.
|
|
21
|
+
The most important fields of the `queueittoken` are:
|
|
22
|
+
|
|
23
|
+
- q - The user's unique queue identifier
|
|
24
|
+
- ts - A timestamp of how long this redirect is valid
|
|
25
|
+
- h - A hash of the token
|
|
26
|
+
|
|
27
|
+
After the user returns from the queue, the connector will let the user continue his request to your backend ( without
|
|
28
|
+
redirecting to the queue since the request has a valid queueittoken as query string) .
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
There are two methods of installation:
|
|
33
|
+
|
|
34
|
+
### As a standalone service
|
|
35
|
+
|
|
36
|
+
- Go to the Fastly services page and create a new **Wasm** service.
|
|
37
|
+
- Go to Domains and fill in the domain that you want your service to be reachable at. You may need to register a CNAME
|
|
38
|
+
record if you have your own domain.
|
|
39
|
+
- Then click on *Origins* and add a new host that has the hostname of your origin server.
|
|
40
|
+
You need to edit the Host and name it **origin**.
|
|
41
|
+
- Create a second host that has the hostname of `{yourCustomerId}.queue-it.net` and name it **queue-it**.
|
|
42
|
+
Edit the host, go to advanced options and fill in the same hostname in **Override host**
|
|
43
|
+
- Go to **Resources -> Config stores** and create a new config store named `IntegrationConfiguration`.
|
|
44
|
+
- Add the following items in the config store (you can find these values in the Go Queue-It self-service platform):
|
|
45
|
+
- customerId: Your customer ID
|
|
46
|
+
- apiKey: The API key for your account
|
|
47
|
+
- secret: Your KnownUserV3 secret
|
|
48
|
+
- queueItOrigin: The name of the queue-it host, in this case it's `queue-it`
|
|
49
|
+
- Go to **Linked Services** and click on `Link Service`
|
|
50
|
+
- Select your service and click `Next`
|
|
51
|
+
- Click `Link and activate` to link the `Config store` to your service. It should generate a new version
|
|
52
|
+
- You can verify that the `Config store` has been linked by going to your service and see it in the section `Config stores`
|
|
53
|
+
- Download the latest package from the releases page and unarchive it.
|
|
54
|
+
- Edit the `fastly.toml` file and copy the ID of your service (you can see this by opening up the service in Fastly) and
|
|
55
|
+
replace **{YourServiceId}** with it.
|
|
56
|
+
- Archive the directory in the same format (tar.gz).
|
|
57
|
+
- Go to `Package` in the Fastly service page and upload the package.
|
|
58
|
+
- To finish up and deploy your service click on the **Activate** button.
|
|
59
|
+
|
|
60
|
+
### Customizable service with the connector
|
|
61
|
+
|
|
62
|
+
- Go to the Fastly services page and create a new **Wasm** service and copy it's ID.
|
|
63
|
+
- Clone this repository and edit the fastly.toml file, make sure to set the `service_id` field to the ID you copied.
|
|
64
|
+
- Then click on *Origins* and add a new host that has the hostname of your origin server.
|
|
65
|
+
You can name the host **origin** or whatever you choose.
|
|
66
|
+
- Create a host that has the hostname of `{yourCustomerId}.queue-it.net` and name it **queue-it**.
|
|
67
|
+
Edit the host, go to advanced options and fill in the same hostname in **Override host**
|
|
68
|
+
- Go to **Resources -> Config stores** and create a new config store named `IntegrationConfiguration`.
|
|
69
|
+
- Add the following items in the config store (you can find these values in the Go Queue-It self-service platform):
|
|
70
|
+
- customerId: Your customer ID
|
|
71
|
+
- apiKey: The API key for your account
|
|
72
|
+
- secret: Your KnownUserV3 secret
|
|
73
|
+
- queueItOrigin: The name of the queue-it host, in this case it's `queue-it`
|
|
74
|
+
- Go to **Linked Services** and click on `Link Service`
|
|
75
|
+
- Select your service and click `Next`
|
|
76
|
+
- Click `Link and activate` to link the `Config store` to your service. It should generate a new version
|
|
77
|
+
- You can verify that the `Config store` has been linked by going to your service and see it in the section `Config stores`
|
|
78
|
+
- You need to add some code that uses this connector. Here is an example:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import {Fastly} from "@fastly/as-compute";
|
|
82
|
+
import {onQueueITRequest, IntegrationDetails, onQueueITResponse} from "@queue-it/fastly";
|
|
83
|
+
|
|
84
|
+
const req = Fastly.getClientRequest();
|
|
85
|
+
|
|
86
|
+
// This is optional and can be null if it's specified in your Config Store
|
|
87
|
+
const integrationDetails = new IntegrationDetails(
|
|
88
|
+
"QueueItOriginName",
|
|
89
|
+
"CustomerId",
|
|
90
|
+
"SecretKey",
|
|
91
|
+
"ApiKey");
|
|
92
|
+
let res = onQueueITRequest(req, integrationDetails);
|
|
93
|
+
|
|
94
|
+
if (res != null) {
|
|
95
|
+
Fastly.respondWith(res!);
|
|
96
|
+
} else {
|
|
97
|
+
const myOrigin = 'Ticketania';
|
|
98
|
+
const cacheOverride = new Fastly.CacheOverride();
|
|
99
|
+
const res = Fastly.fetch(req, {
|
|
100
|
+
backend: myOrigin,
|
|
101
|
+
cacheOverride,
|
|
102
|
+
}).wait();
|
|
103
|
+
onQueueITResponse(res);
|
|
104
|
+
Fastly.respondWith(res);
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
- Build and deploy the package running `fastly compute build` and `fastly compute deploy` in the same directory.
|
|
109
|
+
- Create desired waiting room(s), triggers, and actions in the Go Queue-It self-service platform.
|
|
110
|
+
Then, save/publish the configuration.
|
|
111
|
+
|
|
112
|
+
## Providing the queue configuration
|
|
113
|
+
|
|
114
|
+
The recommended way is to use the Go Queue-it self-service portal to setup the configuration. The configuration
|
|
115
|
+
specifies a set of Triggers and Actions. A Trigger is an expression matching one, more or all URLs on your website. When
|
|
116
|
+
a user enter your website and the URL matches a Trigger-expression the corresponding Action will be triggered. The
|
|
117
|
+
Action specifies which waiting room the users should be send to. In this way you can specify which waiting room(s)
|
|
118
|
+
should protect which page(s) on the fly without changing the server-side integration.
|
|
119
|
+
|
|
120
|
+
## Protecting AJAX calls
|
|
121
|
+
|
|
122
|
+
If you need to protect AJAX calls beside page loads you need to add the below JavaScript tags to your pages:
|
|
123
|
+
|
|
124
|
+
```html
|
|
125
|
+
|
|
126
|
+
<script type="text/javascript" src="//static.queue-it.net/script/queueclient.min.js"></script>
|
|
127
|
+
<script
|
|
128
|
+
data-queueit-intercept-domain="{YOUR_CURRENT_DOMAIN}"
|
|
129
|
+
data-queueit-intercept="true"
|
|
130
|
+
data-queueit-c="{YOUR_CUSTOMER_ID}"
|
|
131
|
+
type="text/javascript"
|
|
132
|
+
src="//static.queue-it.net/script/queueconfigloader.min.js">
|
|
133
|
+
</script>
|
|
134
|
+
```
|
package/package.json
CHANGED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@queue-it/fastly",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3-beta.1",
|
|
4
4
|
"description": "Queue-it connector for Fastly",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"author": "devs@queue-it.com",
|
|
7
|
-
"repository":
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/queueit/KnownUser.V3.Fastly.git"
|
|
10
|
-
},
|
|
7
|
+
"repository": "https://github.com/queueit/KnownUser.V3.Fastly",
|
|
11
8
|
"license": "MIT",
|
|
12
9
|
"files": [
|
|
13
10
|
"package.json",
|
|
14
11
|
"README.md",
|
|
15
|
-
"src/
|
|
12
|
+
"src/helpers",
|
|
13
|
+
"src/fastlyCryptoProvider.ts",
|
|
16
14
|
"src/contextProvider.ts",
|
|
17
15
|
"src/helper.ts",
|
|
18
16
|
"src/integrationConfigProvider.ts",
|
|
@@ -20,19 +18,16 @@
|
|
|
20
18
|
"src/index.ts"
|
|
21
19
|
],
|
|
22
20
|
"scripts": {
|
|
23
|
-
"test": "jest",
|
|
24
21
|
"bundle": "esbuild src/index.ts --bundle --outfile=bin/index.js --platform=node --target=es2022 && js-compute-runtime bin/index.js bin/main.wasm",
|
|
25
22
|
"build": "fastly compute build",
|
|
26
23
|
"deploy": "fastly compute deploy"
|
|
27
24
|
},
|
|
28
25
|
"dependencies": {
|
|
29
|
-
"@fastly/js-compute": "^3.0.0"
|
|
26
|
+
"@fastly/js-compute": "^3.0.0",
|
|
27
|
+
"@queue-it/connector-javascript": "^5.0.1"
|
|
30
28
|
},
|
|
31
29
|
"devDependencies": {
|
|
32
|
-
"@types/jest": "^29.5.0",
|
|
33
30
|
"esbuild": "^0.27.3",
|
|
34
|
-
"jest": "^29.5.0",
|
|
35
|
-
"ts-jest": "^29.1.0",
|
|
36
31
|
"typescript": "^5.4.0"
|
|
37
32
|
}
|
|
38
33
|
}
|
package/src/contextProvider.ts
CHANGED
|
@@ -1,126 +1,134 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export function getHttpHandler(req: Request): FastlyHttpContextProvider {
|
|
5
|
-
return new FastlyHttpContextProvider(req);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export class FastlyHttpContextProvider implements
|
|
9
|
-
isError: boolean = false;
|
|
10
|
-
private readonly req: FastlyHttpRequest;
|
|
11
|
-
private readonly res: FastlyHttpResponse;
|
|
12
|
-
|
|
13
|
-
constructor(fReq: Request) {
|
|
14
|
-
this.req = new FastlyHttpRequest(fReq);
|
|
15
|
-
this.res = new FastlyHttpResponse();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
getHttpRequest(): IHttpRequest {
|
|
19
|
-
return this.req;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
getHttpResponse(): IHttpResponse {
|
|
23
|
-
return this.res;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
private
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (
|
|
57
|
-
return;
|
|
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
|
-
return
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return this.
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
this.headers
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
1
|
+
import { IConnectorContextProvider, IHttpRequest, IHttpResponse } from '@queue-it/connector-javascript';
|
|
2
|
+
import { FastlyCryptoProvider } from './fastlyCryptoProvider';
|
|
3
|
+
|
|
4
|
+
export function getHttpHandler(req: Request): FastlyHttpContextProvider {
|
|
5
|
+
return new FastlyHttpContextProvider(req);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class FastlyHttpContextProvider implements IConnectorContextProvider {
|
|
9
|
+
isError: boolean = false;
|
|
10
|
+
private readonly req: FastlyHttpRequest;
|
|
11
|
+
private readonly res: FastlyHttpResponse;
|
|
12
|
+
|
|
13
|
+
constructor(fReq: Request) {
|
|
14
|
+
this.req = new FastlyHttpRequest(fReq);
|
|
15
|
+
this.res = new FastlyHttpResponse();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
getHttpRequest(): IHttpRequest {
|
|
19
|
+
return this.req;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
getHttpResponse(): IHttpResponse {
|
|
23
|
+
return this.res;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getCryptoProvider(): FastlyCryptoProvider {
|
|
27
|
+
return new FastlyCryptoProvider();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
getEnqueueTokenProvider(): null {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class FastlyHttpRequest implements IHttpRequest {
|
|
36
|
+
private parsedCookieDic: Map<string, string>
|
|
37
|
+
private bodyFetched: boolean = false;
|
|
38
|
+
private body: string = '';
|
|
39
|
+
|
|
40
|
+
constructor(private baseReq: Request) {
|
|
41
|
+
this.parsedCookieDic = new Map();
|
|
42
|
+
this.bodyFetched = false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private parseCookies(cookieValue: string): void {
|
|
46
|
+
const cookies = cookieValue.split(';');
|
|
47
|
+
for (let i = 0; i < cookies.length; i++) {
|
|
48
|
+
let cookieKV = cookies[i].split('=', 2);
|
|
49
|
+
if (cookieKV.length >= 2) {
|
|
50
|
+
this.parsedCookieDic.set(cookieKV[0].trim(), cookieKV[1].trim())
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private handleBody(): void {
|
|
56
|
+
if (this.baseReq.bodyUsed || this.bodyFetched) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
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
|
+
this.body = ''; // this.context.req.text()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
getAbsoluteUri(): string {
|
|
72
|
+
return this.baseReq.url;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
getCookieValue(cookieKey: string): string | undefined {
|
|
76
|
+
if (this.parsedCookieDic.size == 0) {
|
|
77
|
+
this.parseCookies(this.getHeader('cookie'))
|
|
78
|
+
}
|
|
79
|
+
if (!this.parsedCookieDic.has(cookieKey)) return undefined;
|
|
80
|
+
const cookieVal = this.parsedCookieDic.get(cookieKey)!;
|
|
81
|
+
try {
|
|
82
|
+
return decodeURIComponent(cookieVal);
|
|
83
|
+
} catch {
|
|
84
|
+
return cookieVal;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
getHeader(name: string): string {
|
|
89
|
+
if (name.length == 0) return "";
|
|
90
|
+
if (!this.baseReq.headers.has(name)) {
|
|
91
|
+
return '';
|
|
92
|
+
}
|
|
93
|
+
const value = this.baseReq.headers.get(name);
|
|
94
|
+
if (value == null) return '';
|
|
95
|
+
return value!;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
getRequestBodyAsString(): string {
|
|
99
|
+
if (!this.bodyFetched) {
|
|
100
|
+
this.handleBody();
|
|
101
|
+
}
|
|
102
|
+
return this.body;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
getUserAgent(): string {
|
|
106
|
+
return this.baseReq.headers.has('user-agent') ? this.baseReq.headers.get('user-agent')! : '';
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getUserHostAddress(): string {
|
|
110
|
+
return this.baseReq.headers.get('Fastly-Client-IP') ?? '';
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export class FastlyHttpResponse implements IHttpResponse {
|
|
115
|
+
private readonly headers: Headers;
|
|
116
|
+
|
|
117
|
+
constructor() {
|
|
118
|
+
this.headers = new Headers();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
setCookie(cookieName: string, cookieValue: string, domain: string, expiration: number, _isHttpOnly: boolean, _isSecure: boolean): void {
|
|
122
|
+
const expirationDate = new Date(expiration * 1000);
|
|
123
|
+
let setCookieString = cookieName + "=" + encodeURIComponent(cookieValue) + "; expires=" + expirationDate.toUTCString() + ";";
|
|
124
|
+
if (domain != "") {
|
|
125
|
+
setCookieString += " domain=" + domain + ";";
|
|
126
|
+
}
|
|
127
|
+
setCookieString += " path=/;";
|
|
128
|
+
this.headers.set('set-cookie', setCookieString);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
getHeaders(): Headers {
|
|
132
|
+
return this.headers;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ICryptoProvider } from '@queue-it/connector-javascript';
|
|
2
|
+
import { hmacString } from './helpers/crypto';
|
|
3
|
+
|
|
4
|
+
export class FastlyCryptoProvider implements ICryptoProvider {
|
|
5
|
+
getSha256Hash(secretKey: string, plaintext: string): string {
|
|
6
|
+
return hmacString(secretKey, plaintext);
|
|
7
|
+
}
|
|
8
|
+
}
|
package/src/helper.ts
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
import { Utils } from "./sdk/QueueITHelpers";
|
|
2
|
-
import { hmacString } from "./sdk/helpers/crypto";
|
|
3
|
-
|
|
4
1
|
export class QueueITHelper {
|
|
5
|
-
static readonly KUP_VERSION: string = "fastly-2.0.
|
|
6
|
-
|
|
7
|
-
static configureKnownUserHashing(): void {
|
|
8
|
-
Utils.generateSHA256Hash = hmacString;
|
|
9
|
-
}
|
|
2
|
+
static readonly KUP_VERSION: string = "fastly-2.0.3-beta.1";
|
|
10
3
|
|
|
11
4
|
static addKUPlatformVersion(redirectQueueUrl: string): string {
|
|
12
5
|
return redirectQueueUrl + "&kupver=" + QueueITHelper.KUP_VERSION;
|