Skip to content

Commit 6eb9f8a

Browse files
committed
Worker host can now be configured.
1 parent 2585a76 commit 6eb9f8a

4 files changed

Lines changed: 844 additions & 565 deletions

File tree

Lines changed: 146 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,146 @@
1-
import {Fastly, Headers, Request} from "@fastly/as-compute";
2-
import {RequestLogger} from "./helper";
3-
4-
export function getIntegrationConfig(details: IntegrationDetails, endpointProvider: IntegrationEndpointProvider): string {
5-
const headers = new Headers();
6-
headers.set('api-key', details.apiKey);
7-
headers.set('host', endpointProvider.getHostname(details.customerId));
8-
const request = new Request(endpointProvider.getEndpoint(details.customerId), {
9-
method: 'GET', body: null, headers: headers
10-
})
11-
let cacheOverride = new Fastly.CacheOverride();
12-
let cacheConf = endpointProvider.getCacheConfig();
13-
if (cacheConf.maxAge != -1) {
14-
cacheOverride.setTTL(cacheConf.maxAge);
15-
}
16-
if (cacheConf.staleWhileRevalidate != -1) {
17-
cacheOverride.setSWR(cacheConf.staleWhileRevalidate);
18-
}
19-
20-
let beresp = Fastly.fetch(request, {
21-
backend: details.queueItOrigin,
22-
cacheOverride: cacheOverride
23-
}).wait();
24-
25-
if (!(details.logger instanceof MockLogger)) {
26-
let cacheState = beresp.headers.get('x-cache');
27-
let hits = beresp.headers.get('x-cache-hits');
28-
if (hits == null) hits = '-1';
29-
if (cacheState == null) cacheState = 'n';
30-
details.logger.log("IgnFetch:" + cacheState! + ":" + hits!);
31-
}
32-
33-
if (beresp.status != 200) {
34-
return '';
35-
}
36-
return beresp.text();
37-
}
38-
39-
const integrationCustomerId = "customerId",
40-
integrationApiKey = 'apiKey',
41-
integrationSecret = 'secret',
42-
integrationQueueItOrigin = 'queueItOrigin',
43-
integrationDictionary = "IntegrationConfiguration";
44-
45-
export function resolveIntegrationDetails(): IntegrationDetails | null {
46-
const dict = new Fastly.Dictionary(integrationDictionary);
47-
if (!dict.contains(integrationCustomerId)
48-
|| !dict.contains(integrationApiKey)
49-
|| !dict.contains(integrationSecret)
50-
|| !dict.contains(integrationQueueItOrigin)) {
51-
return null;
52-
}
53-
54-
return new IntegrationDetails(
55-
dict.get(integrationQueueItOrigin)!,
56-
dict.get(integrationCustomerId)!,
57-
dict.get(integrationSecret)!,
58-
dict.get(integrationApiKey)!)
59-
}
60-
61-
export class IntegrationEndpointCacheConfig {
62-
maxAge: i16 = -1;
63-
staleWhileRevalidate: i16 = -1;
64-
}
65-
66-
export interface IntegrationEndpointProvider {
67-
getHostname(customerId: string): string;
68-
69-
getEndpoint(customerId: string): string;
70-
71-
getCacheConfig(): IntegrationEndpointCacheConfig;
72-
}
73-
74-
export class QueueItIntegrationEndpointProvider implements IntegrationEndpointProvider {
75-
private readonly cacheConfig: IntegrationEndpointCacheConfig;
76-
77-
constructor() {
78-
this.cacheConfig = new IntegrationEndpointCacheConfig();
79-
this.cacheConfig.maxAge = 60 * 5;
80-
this.cacheConfig.staleWhileRevalidate = 60 * 5;
81-
}
82-
83-
getCacheConfig(): IntegrationEndpointCacheConfig {
84-
return this.cacheConfig;
85-
}
86-
87-
getHostname(customerId: string): string {
88-
return customerId + ".queue-it.net";
89-
}
90-
91-
getEndpoint(customerId: string): string {
92-
const host = this.getHostname(customerId);
93-
return "https://" + host + "/status/integrationconfig/secure/" + customerId;
94-
}
95-
}
96-
97-
class MockLogger implements RequestLogger {
98-
log(message: string): void {
99-
}
100-
}
101-
102-
export class IntegrationDetails {
103-
constructor(public queueItOrigin: string,
104-
public customerId: string,
105-
public secretKey: string,
106-
public apiKey: string,
107-
public provider: IntegrationEndpointProvider = new QueueItIntegrationEndpointProvider(),
108-
public logger: RequestLogger = new MockLogger()) {
109-
}
110-
}
1+
import { Fastly, Headers, Request } from "@fastly/as-compute";
2+
import { RequestLogger } from "./helper";
3+
4+
export function getIntegrationConfig(
5+
details: IntegrationDetails,
6+
endpointProvider: IntegrationEndpointProvider
7+
): string {
8+
const headers = new Headers();
9+
headers.set("api-key", details.apiKey);
10+
headers.set("host", endpointProvider.getHostname(details.customerId));
11+
const request = new Request(
12+
endpointProvider.getEndpoint(details.customerId),
13+
{
14+
method: "GET",
15+
body: null,
16+
headers: headers,
17+
}
18+
);
19+
let cacheOverride = new Fastly.CacheOverride();
20+
let cacheConf = endpointProvider.getCacheConfig();
21+
if (cacheConf.maxAge != -1) {
22+
cacheOverride.setTTL(cacheConf.maxAge);
23+
}
24+
if (cacheConf.staleWhileRevalidate != -1) {
25+
cacheOverride.setSWR(cacheConf.staleWhileRevalidate);
26+
}
27+
28+
let beresp = Fastly.fetch(request, {
29+
backend: details.queueItOrigin,
30+
cacheOverride: cacheOverride,
31+
}).wait();
32+
33+
if (!(details.logger instanceof MockLogger)) {
34+
let cacheState = beresp.headers.get("x-cache");
35+
let hits = beresp.headers.get("x-cache-hits");
36+
if (hits == null) hits = "-1";
37+
if (cacheState == null) cacheState = "n";
38+
details.logger.log("IgnFetch:" + cacheState! + ":" + hits!);
39+
}
40+
41+
if (beresp.status != 200) {
42+
return "";
43+
}
44+
return beresp.text();
45+
}
46+
47+
const integrationCustomerId = "customerId",
48+
integrationApiKey = "apiKey",
49+
integrationSecret = "secret",
50+
integrationQueueItOrigin = "queueItOrigin",
51+
integrationDictionary = "IntegrationConfiguration",
52+
workerHost = "workerHost";
53+
54+
export function resolveIntegrationDetails(): IntegrationDetails | null {
55+
const dict = new Fastly.Dictionary(integrationDictionary);
56+
if (
57+
!dict.contains(integrationCustomerId) ||
58+
!dict.contains(integrationApiKey) ||
59+
!dict.contains(integrationSecret) ||
60+
!dict.contains(integrationQueueItOrigin)
61+
) {
62+
return null;
63+
}
64+
65+
let workerHostValue = "";
66+
if (dict.contains(workerHost)) {
67+
workerHostValue = dict.get(workerHost)!;
68+
}
69+
70+
return new IntegrationDetails(
71+
dict.get(integrationQueueItOrigin)!,
72+
dict.get(integrationCustomerId)!,
73+
dict.get(integrationSecret)!,
74+
dict.get(integrationApiKey)!,
75+
workerHostValue
76+
);
77+
}
78+
79+
export class IntegrationEndpointCacheConfig {
80+
maxAge: i16 = -1;
81+
staleWhileRevalidate: i16 = -1;
82+
}
83+
84+
export interface IntegrationEndpointProvider {
85+
getHostname(customerId: string): string;
86+
87+
getEndpoint(customerId: string): string;
88+
89+
getCacheConfig(): IntegrationEndpointCacheConfig;
90+
}
91+
92+
export class QueueItIntegrationEndpointProvider
93+
implements IntegrationEndpointProvider
94+
{
95+
private readonly cacheConfig: IntegrationEndpointCacheConfig;
96+
97+
constructor() {
98+
this.cacheConfig = new IntegrationEndpointCacheConfig();
99+
this.cacheConfig.maxAge = 60 * 5;
100+
this.cacheConfig.staleWhileRevalidate = 60 * 5;
101+
}
102+
103+
getCacheConfig(): IntegrationEndpointCacheConfig {
104+
return this.cacheConfig;
105+
}
106+
107+
getHostname(customerId: string): string {
108+
return customerId + ".queue-it.net";
109+
}
110+
111+
getEndpoint(customerId: string): string {
112+
const host = this.getHostname(customerId);
113+
return "https://" + host + "/status/integrationconfig/secure/" + customerId;
114+
}
115+
}
116+
117+
class MockLogger implements RequestLogger {
118+
log(message: string): void {}
119+
}
120+
121+
export class IntegrationDetails {
122+
constructor(
123+
public queueItOrigin: string,
124+
public customerId: string,
125+
public secretKey: string,
126+
public apiKey: string,
127+
public workerHost: string,
128+
public provider: IntegrationEndpointProvider = new QueueItIntegrationEndpointProvider(),
129+
public logger: RequestLogger = new MockLogger()
130+
) {}
131+
132+
resolveWorkerRequestUrl(pureUrl: string): string {
133+
if (this.workerHost == "") {
134+
return pureUrl;
135+
}
136+
const protoEnding = pureUrl.indexOf("://") + 3;
137+
const protocol = pureUrl.substr(0, protoEnding);
138+
const urlWithoutProto = pureUrl.substr(protoEnding);
139+
const pathAndQuery =
140+
urlWithoutProto.indexOf("/") != -1
141+
? urlWithoutProto.substr(urlWithoutProto.indexOf("/"))
142+
: "";
143+
const rewrittenUrl = protocol + this.workerHost + pathAndQuery;
144+
return rewrittenUrl;
145+
}
146+
}

0 commit comments

Comments
 (0)