@queue-it/fastly 2.0.0 → 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.
- package/LICENSE +21 -0
- package/README.md +134 -134
- package/package.json +5 -2
- package/src/contextProvider.ts +126 -126
- package/src/helper.ts +1 -1
- package/src/index.ts +3 -3
- package/src/integrationConfigProvider.ts +0 -1
- package/src/requestResponseHandler.ts +131 -131
- package/src/sdk/HttpContextProvider.ts +23 -23
- package/src/sdk/IntegrationConfig/CustomerIntegrationDecodingHandler.ts +57 -57
- package/src/sdk/IntegrationConfig/IntegrationConfigHelpers.ts +230 -230
- package/src/sdk/IntegrationConfig/IntegrationConfigModel.ts +93 -93
- package/src/sdk/KnownUser.ts +393 -393
- package/src/sdk/Models.ts +105 -105
- package/src/sdk/QueueITHelpers.ts +267 -267
- package/src/sdk/UserInQueueService.ts +245 -245
- package/src/sdk/UserInQueueStateCookieRepository.ts +188 -188
- package/src/sdk/helpers/Uri.ts +4 -4
- package/src/sdk/helpers/crypto.ts +338 -338
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Queue-it
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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
|
|
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
|
|
8
|
-
|
|
9
|
-
This Fastly Queue-it Connector SDK (aka, Queue-it
|
|
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
|
|
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,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@queue-it/fastly",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Queue-it connector for Fastly",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"author": "devs@queue-it.com",
|
|
7
|
-
"repository":
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/queueit/KnownUser.V3.Fastly.git"
|
|
10
|
+
},
|
|
8
11
|
"license": "MIT",
|
|
9
12
|
"files": [
|
|
10
13
|
"package.json",
|
package/src/contextProvider.ts
CHANGED
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
import {IHttpContextProvider, IHttpRequest, IHttpResponse} from "./sdk/HttpContextProvider";
|
|
2
|
-
import {decodeURIComponent, encodeURIComponent} from "./sdk/helpers/Uri";
|
|
3
|
-
|
|
4
|
-
export function getHttpHandler(req: Request): FastlyHttpContextProvider {
|
|
5
|
-
return new FastlyHttpContextProvider(req);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export class FastlyHttpContextProvider implements IHttpContextProvider {
|
|
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
|
-
export class FastlyHttpRequest implements IHttpRequest {
|
|
28
|
-
private parsedCookieDic: Map<string, string>
|
|
29
|
-
private bodyFetched: boolean = false;
|
|
30
|
-
private body: string = '';
|
|
31
|
-
|
|
32
|
-
constructor(private baseReq: Request) {
|
|
33
|
-
this.parsedCookieDic = new Map();
|
|
34
|
-
this.bodyFetched = false;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
private parseCookies(cookieValue: string): void {
|
|
38
|
-
const cookies = cookieValue.split(';');
|
|
39
|
-
for (let i = 0; i < cookies.length; i++) {
|
|
40
|
-
let cookieKV = cookies[i].split('=', 2);
|
|
41
|
-
if (cookieKV.length >= 2) {
|
|
42
|
-
this.parsedCookieDic.set(cookieKV[0].trim(), cookieKV[1].trim())
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
private handleBody(): void {
|
|
48
|
-
if (this.baseReq.bodyUsed || this.bodyFetched) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
this.bodyFetched = true;
|
|
52
|
-
const rawBody: ArrayBuffer | null = null; // this.context.req.arrayBuffer();
|
|
53
|
-
if (rawBody == null) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
if (rawBody!.byteLength == 0) {
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
this.body = ''; // this.context.req.text()
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
getAbsoluteUri(): string {
|
|
64
|
-
return this.baseReq.url;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
getCookieValue(cookieKey: string): string {
|
|
68
|
-
if (this.parsedCookieDic.size == 0) {
|
|
69
|
-
this.parseCookies(this.getHeader('cookie'))
|
|
70
|
-
}
|
|
71
|
-
if (!this.parsedCookieDic.has(cookieKey)) return '';
|
|
72
|
-
const cookieVal = this.parsedCookieDic.get(cookieKey)!;
|
|
73
|
-
try {
|
|
74
|
-
return decodeURIComponent(cookieVal);
|
|
75
|
-
} catch {
|
|
76
|
-
return cookieVal;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
getHeader(name: string): string {
|
|
81
|
-
if (name.length == 0) return "";
|
|
82
|
-
if (!this.baseReq.headers.has(name)) {
|
|
83
|
-
return '';
|
|
84
|
-
}
|
|
85
|
-
const value = this.baseReq.headers.get(name);
|
|
86
|
-
if (value == null) return '';
|
|
87
|
-
return value!;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
getRequestBodyAsString(): string {
|
|
91
|
-
if (!this.bodyFetched) {
|
|
92
|
-
this.handleBody();
|
|
93
|
-
}
|
|
94
|
-
return this.body;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
getUserAgent(): string {
|
|
98
|
-
return this.baseReq.headers.has('user-agent') ? this.baseReq.headers.get('user-agent')! : '';
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
getUserHostAddress(): string {
|
|
102
|
-
return this.baseReq.headers.get('Fastly-Client-IP') ?? '';
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export class FastlyHttpResponse implements IHttpResponse {
|
|
107
|
-
private readonly headers: Headers;
|
|
108
|
-
|
|
109
|
-
constructor() {
|
|
110
|
-
this.headers = new Headers();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
setCookie(cookieName: string, cookieValue: string, domain: string, expiration: number): void {
|
|
114
|
-
const expirationDate = new Date(expiration * 1000);
|
|
115
|
-
let setCookieString = cookieName + "=" + encodeURIComponent(cookieValue) + "; expires=" + expirationDate.toUTCString() + ";";
|
|
116
|
-
if (domain != "") {
|
|
117
|
-
setCookieString += " domain=" + domain + ";";
|
|
118
|
-
}
|
|
119
|
-
setCookieString += " path=/;";
|
|
120
|
-
this.headers.set('set-cookie', setCookieString);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
getHeaders(): Headers {
|
|
124
|
-
return this.headers;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
1
|
+
import {IHttpContextProvider, IHttpRequest, IHttpResponse} from "./sdk/HttpContextProvider";
|
|
2
|
+
import {decodeURIComponent, encodeURIComponent} from "./sdk/helpers/Uri";
|
|
3
|
+
|
|
4
|
+
export function getHttpHandler(req: Request): FastlyHttpContextProvider {
|
|
5
|
+
return new FastlyHttpContextProvider(req);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class FastlyHttpContextProvider implements IHttpContextProvider {
|
|
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
|
+
export class FastlyHttpRequest implements IHttpRequest {
|
|
28
|
+
private parsedCookieDic: Map<string, string>
|
|
29
|
+
private bodyFetched: boolean = false;
|
|
30
|
+
private body: string = '';
|
|
31
|
+
|
|
32
|
+
constructor(private baseReq: Request) {
|
|
33
|
+
this.parsedCookieDic = new Map();
|
|
34
|
+
this.bodyFetched = false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private parseCookies(cookieValue: string): void {
|
|
38
|
+
const cookies = cookieValue.split(';');
|
|
39
|
+
for (let i = 0; i < cookies.length; i++) {
|
|
40
|
+
let cookieKV = cookies[i].split('=', 2);
|
|
41
|
+
if (cookieKV.length >= 2) {
|
|
42
|
+
this.parsedCookieDic.set(cookieKV[0].trim(), cookieKV[1].trim())
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private handleBody(): void {
|
|
48
|
+
if (this.baseReq.bodyUsed || this.bodyFetched) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
this.bodyFetched = true;
|
|
52
|
+
const rawBody: ArrayBuffer | null = null; // this.context.req.arrayBuffer();
|
|
53
|
+
if (rawBody == null) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (rawBody!.byteLength == 0) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this.body = ''; // this.context.req.text()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
getAbsoluteUri(): string {
|
|
64
|
+
return this.baseReq.url;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
getCookieValue(cookieKey: string): string {
|
|
68
|
+
if (this.parsedCookieDic.size == 0) {
|
|
69
|
+
this.parseCookies(this.getHeader('cookie'))
|
|
70
|
+
}
|
|
71
|
+
if (!this.parsedCookieDic.has(cookieKey)) return '';
|
|
72
|
+
const cookieVal = this.parsedCookieDic.get(cookieKey)!;
|
|
73
|
+
try {
|
|
74
|
+
return decodeURIComponent(cookieVal);
|
|
75
|
+
} catch {
|
|
76
|
+
return cookieVal;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
getHeader(name: string): string {
|
|
81
|
+
if (name.length == 0) return "";
|
|
82
|
+
if (!this.baseReq.headers.has(name)) {
|
|
83
|
+
return '';
|
|
84
|
+
}
|
|
85
|
+
const value = this.baseReq.headers.get(name);
|
|
86
|
+
if (value == null) return '';
|
|
87
|
+
return value!;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
getRequestBodyAsString(): string {
|
|
91
|
+
if (!this.bodyFetched) {
|
|
92
|
+
this.handleBody();
|
|
93
|
+
}
|
|
94
|
+
return this.body;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
getUserAgent(): string {
|
|
98
|
+
return this.baseReq.headers.has('user-agent') ? this.baseReq.headers.get('user-agent')! : '';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
getUserHostAddress(): string {
|
|
102
|
+
return this.baseReq.headers.get('Fastly-Client-IP') ?? '';
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export class FastlyHttpResponse implements IHttpResponse {
|
|
107
|
+
private readonly headers: Headers;
|
|
108
|
+
|
|
109
|
+
constructor() {
|
|
110
|
+
this.headers = new Headers();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
setCookie(cookieName: string, cookieValue: string, domain: string, expiration: number): void {
|
|
114
|
+
const expirationDate = new Date(expiration * 1000);
|
|
115
|
+
let setCookieString = cookieName + "=" + encodeURIComponent(cookieValue) + "; expires=" + expirationDate.toUTCString() + ";";
|
|
116
|
+
if (domain != "") {
|
|
117
|
+
setCookieString += " domain=" + domain + ";";
|
|
118
|
+
}
|
|
119
|
+
setCookieString += " path=/;";
|
|
120
|
+
this.headers.set('set-cookie', setCookieString);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
getHeaders(): Headers {
|
|
124
|
+
return this.headers;
|
|
125
|
+
}
|
|
126
|
+
}
|
package/src/helper.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Utils } from "./sdk/QueueITHelpers";
|
|
|
2
2
|
import { hmacString } from "./sdk/helpers/crypto";
|
|
3
3
|
|
|
4
4
|
export class QueueITHelper {
|
|
5
|
-
static readonly KUP_VERSION: string = "fastly-2.0.
|
|
5
|
+
static readonly KUP_VERSION: string = "fastly-2.0.2";
|
|
6
6
|
|
|
7
7
|
static configureKnownUserHashing(): void {
|
|
8
8
|
Utils.generateSHA256Hash = hmacString;
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {IntegrationDetails, IntegrationEndpointProvider, IntegrationEndpointCacheConfig, resolveIntegrationDetails} from "./integrationConfigProvider"
|
|
2
|
-
export {onQueueITRequest, onQueueITResponse} from "./requestResponseHandler";
|
|
3
|
-
export {RequestLogger} from "./helper";
|
|
1
|
+
export {IntegrationDetails, IntegrationEndpointProvider, IntegrationEndpointCacheConfig, resolveIntegrationDetails} from "./integrationConfigProvider"
|
|
2
|
+
export {onQueueITRequest, onQueueITResponse} from "./requestResponseHandler";
|
|
3
|
+
export {RequestLogger} from "./helper";
|