-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAuthXKeyCache.ts
178 lines (150 loc) · 4.41 KB
/
AuthXKeyCache.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { EventEmitter } from "events";
interface Config {
/**
* The root URL to AuthX server.
*/
readonly authxUrl: string;
/**
* The number of seconds between successful attempts at refreshing public keys
* from the AuthX server.
*
* @defaultValue `60`
*/
readonly authxPublicKeyRefreshInterval?: number;
/**
* The number of seconds to wait before aborting and retrying a request for
* public keys from the AuthX server.
*
* @defaultValue `30`
*/
readonly authxPublicKeyRefreshRequestTimeout?: number;
/**
* The number of seconds between failed attempts at refreshing public keys
* from the AuthX server.
*
* @defaultValue `10`
*/
readonly authxPublicKeyRetryInterval?: number;
}
export class AuthXKeyCache extends EventEmitter {
private readonly _config: Config;
private _fetchTimeout: null | ReturnType<typeof setTimeout> = null;
private _fetchAbortController: null | AbortController = null;
private _fetchAbortTimeout: null | ReturnType<typeof setTimeout> = null;
public active: boolean = false;
public keys: null | ReadonlyArray<string> = null;
public constructor(config: Config) {
super();
this._config = config;
}
protected _fetch = async (): Promise<void> => {
this._fetchTimeout = null;
// Don't fetch unless the cache is active.
if (!this.active) {
return;
}
this._fetchAbortController = new AbortController();
this._fetchAbortTimeout = setTimeout(
() => {
if (this._fetchAbortController) {
this._fetchAbortController.abort();
}
},
(this._config.authxPublicKeyRefreshRequestTimeout || 30) * 1000,
);
try {
// Fetch the keys from AuthX.
const response = await (
await fetch(this._config.authxUrl + "/graphql", {
signal: this._fetchAbortController.signal,
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: '{"query": "query { keys }"}',
})
).json();
if (typeof response !== "object" || response === null) {
throw new Error("The response from AuthX is not an object.");
}
// Make sure we don't have any errors.
if (
"errors" in response &&
response.errors &&
Array.isArray(response.errors) &&
response.errors[0]
)
throw new Error(response.errors[0]);
if (
!("data" in response) ||
response.data === null ||
typeof response.data !== "object" ||
!("keys" in response.data) ||
!Array.isArray(response.data.keys)
) {
throw new Error("The response from AuthX is missing keys.");
}
const keys: string[] = response.data.keys;
// Ensure that there is at least one valid key in the response.
if (
!keys ||
!Array.isArray(keys) ||
!keys.length ||
!keys.every((k) => typeof k === "string")
) {
throw new Error("An array of least one key must be returned by AuthX.");
}
if (!this.active) {
return;
}
// Cache the keys.
this.keys = keys;
// Fire off a ready event.
this.emit("ready");
// Fetch again in 1 minute.
if (this.active && !this._fetchTimeout) {
this._fetchTimeout = setTimeout(
this._fetch,
(this._config.authxPublicKeyRefreshInterval || 60) * 1000,
);
}
} catch (error) {
this.emit("error", error);
// Fetch again in 10 seconds.
if (this.active && !this._fetchTimeout) {
this._fetchTimeout = setTimeout(
this._fetch,
(this._config.authxPublicKeyRetryInterval || 10) * 1000,
);
}
} finally {
this._fetchAbortController = null;
clearTimeout(this._fetchAbortTimeout);
this._fetchAbortTimeout = null;
}
};
public start(): void {
if (this.active) return;
this.active = true;
this._fetch();
}
public stop(): void {
if (!this.active) return;
this.active = false;
// Clear any pending timeouts.
const timeout = this._fetchTimeout;
if (timeout) {
clearTimeout(timeout);
}
const abortTimeout = this._fetchAbortTimeout;
if (abortTimeout) {
clearTimeout(abortTimeout);
}
// Abort any in-flight key requests.
const abort = this._fetchAbortController;
if (abort) {
abort.abort();
}
this.keys = null;
}
}