-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathintegrationConfigProvider.ts
146 lines (126 loc) · 4.22 KB
/
integrationConfigProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { Fastly, Headers, Request } from "@fastly/as-compute";
import { RequestLogger } from "./helper";
export function getIntegrationConfig(
details: IntegrationDetails,
endpointProvider: IntegrationEndpointProvider
): string {
const headers = new Headers();
headers.set("api-key", details.apiKey);
headers.set("host", endpointProvider.getHostname(details.customerId));
const request = new Request(
endpointProvider.getEndpoint(details.customerId),
{
method: "GET",
body: null,
headers: headers,
}
);
let cacheOverride = new Fastly.CacheOverride();
let cacheConf = endpointProvider.getCacheConfig();
if (cacheConf.maxAge != -1) {
cacheOverride.setTTL(cacheConf.maxAge);
}
if (cacheConf.staleWhileRevalidate != -1) {
cacheOverride.setSWR(cacheConf.staleWhileRevalidate);
}
let beresp = Fastly.fetch(request, {
backend: details.queueItOrigin,
cacheOverride: cacheOverride,
}).wait();
if (!(details.logger instanceof MockLogger)) {
let cacheState = beresp.headers.get("x-cache");
let hits = beresp.headers.get("x-cache-hits");
if (hits == null) hits = "-1";
if (cacheState == null) cacheState = "n";
details.logger.log("IgnFetch:" + cacheState! + ":" + hits!);
}
if (beresp.status != 200) {
return "";
}
return beresp.text();
}
const integrationCustomerId = "customerId",
integrationApiKey = "apiKey",
integrationSecret = "secret",
integrationQueueItOrigin = "queueItOrigin",
integrationDictionary = "IntegrationConfiguration",
workerHost = "workerHost";
export function resolveIntegrationDetails(): IntegrationDetails | null {
const dict = new Fastly.Dictionary(integrationDictionary);
if (
!dict.contains(integrationCustomerId) ||
!dict.contains(integrationApiKey) ||
!dict.contains(integrationSecret) ||
!dict.contains(integrationQueueItOrigin)
) {
return null;
}
let workerHostValue = "";
if (dict.contains(workerHost)) {
workerHostValue = dict.get(workerHost)!;
}
return new IntegrationDetails(
dict.get(integrationQueueItOrigin)!,
dict.get(integrationCustomerId)!,
dict.get(integrationSecret)!,
dict.get(integrationApiKey)!,
workerHostValue
);
}
export class IntegrationEndpointCacheConfig {
maxAge: i16 = -1;
staleWhileRevalidate: i16 = -1;
}
export interface IntegrationEndpointProvider {
getHostname(customerId: string): string;
getEndpoint(customerId: string): string;
getCacheConfig(): IntegrationEndpointCacheConfig;
}
export class QueueItIntegrationEndpointProvider
implements IntegrationEndpointProvider
{
private readonly cacheConfig: IntegrationEndpointCacheConfig;
constructor() {
this.cacheConfig = new IntegrationEndpointCacheConfig();
this.cacheConfig.maxAge = 60 * 5;
this.cacheConfig.staleWhileRevalidate = 60 * 5;
}
getCacheConfig(): IntegrationEndpointCacheConfig {
return this.cacheConfig;
}
getHostname(customerId: string): string {
return customerId + ".queue-it.net";
}
getEndpoint(customerId: string): string {
const host = this.getHostname(customerId);
return "https://" + host + "/status/integrationconfig/secure/" + customerId;
}
}
class MockLogger implements RequestLogger {
log(message: string): void {}
}
export class IntegrationDetails {
constructor(
public queueItOrigin: string,
public customerId: string,
public secretKey: string,
public apiKey: string,
public workerHost: string,
public provider: IntegrationEndpointProvider = new QueueItIntegrationEndpointProvider(),
public logger: RequestLogger = new MockLogger()
) {}
resolveWorkerRequestUrl(pureUrl: string): string {
if (this.workerHost == "") {
return pureUrl;
}
const protoEnding = pureUrl.indexOf("://") + 3;
const protocol = pureUrl.substr(0, protoEnding);
const urlWithoutProto = pureUrl.substr(protoEnding);
const pathAndQuery =
urlWithoutProto.indexOf("/") != -1
? urlWithoutProto.substr(urlWithoutProto.indexOf("/"))
: "";
const rewrittenUrl = protocol + this.workerHost + pathAndQuery;
return rewrittenUrl;
}
}