-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathhttp-caching-proxy.ts
More file actions
280 lines (242 loc) · 7.39 KB
/
http-caching-proxy.ts
File metadata and controls
280 lines (242 loc) · 7.39 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
// Node module: @loopback/http-caching-proxy
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import axios, {AxiosInstance} from 'axios';
import debugFactory from 'debug';
import {once} from 'node:events';
import {
createServer,
Server as HttpServer,
IncomingMessage,
OutgoingHttpHeaders,
ServerResponse,
} from 'node:http';
import {AddressInfo} from 'node:net';
const cacache = require('cacache');
const debug = debugFactory('loopback:http-caching-proxy');
export interface ProxyOptions {
/**
* Directory where to keep the cached snapshots.
*/
cachePath: string;
/**
* How long to keep snapshots before making a new request to the backend.
* The value is in milliseconds.
*
* Default: one day
*/
ttl?: number;
/**
* The port where the HTTP proxy should listen at.
* Default: 0 (let the system pick a free port)
*/
port?: number;
/**
* A flag if the error should be logged
*/
logError?: boolean;
/**
* Timeout to connect to the target service
*/
timeout?: number;
}
const DEFAULT_OPTIONS = {
port: 0,
ttl: 24 * 60 * 60 * 1000,
logError: true,
timeout: 0,
};
interface CachedMetadata {
statusCode: number;
headers: OutgoingHttpHeaders;
createdAt: number;
}
/**
* The HTTP proxy implementation.
*/
export class HttpCachingProxy {
private _axios: AxiosInstance;
private _options: Required<ProxyOptions>;
private _server?: HttpServer;
/**
* URL where the proxy is listening on.
* Provide this value to your HTTP client as the proxy configuration.
*/
public url: string;
constructor(options: ProxyOptions) {
this._options = Object.assign({}, DEFAULT_OPTIONS, options);
if (!this._options.cachePath) {
throw new Error('Required option missing: "cachePath"');
}
this.url = 'http://proxy-not-running';
this._server = undefined;
this._axios = axios.create({
// Provide a custom function to control when Axios throws errors based on
// http status code. Please note that Axios creates a new error in such
// condition and the original low-level error is lost
validateStatus: () => true,
// Disable SSL certificate validation for HTTPS requests
// This is acceptable for a testing/caching proxy
httpsAgent: new (require('node:https').Agent)({
rejectUnauthorized: false,
}),
});
}
/**
* Start listening.
*/
async start() {
this._server = createServer(
(request: IncomingMessage, response: ServerResponse) => {
this._handle(request, response);
},
);
this._server.on('connect', (req, socket) => {
// Reject tunneling requests
socket.write('HTTP/1.1 501 Not Implemented\r\n\r\n');
socket.destroy();
});
this._server.listen(this._options.port);
await once(this._server, 'listening');
const address = this._server.address() as AddressInfo;
this.url = `http://127.0.0.1:${address.port}`;
}
/**
* Stop listening.
*/
async stop() {
if (!this._server) return;
this.url = 'http://proxy-not-running';
const server = this._server;
this._server = undefined;
server.close();
await once(server, 'close');
}
private _handle(request: IncomingMessage, response: ServerResponse) {
const onerror = (error: Error) => {
this.logError(request, error);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response.statusCode = (error as any).statusCode || 502;
response.end(`${error.name}: ${error.message}`);
};
try {
this._handleAsync(request, response).catch(onerror);
} catch (err) {
onerror(err);
}
}
private async _handleAsync(
request: IncomingMessage,
response: ServerResponse,
) {
debug(
'Incoming request %s %s',
request.method,
request.url,
request.headers,
);
const cacheKey = this._getCacheKey(request);
try {
const entry = await cacache.get(this._options.cachePath, cacheKey);
if (entry.metadata.createdAt + this._options.ttl > Date.now()) {
debug('Sending cached response for %s', cacheKey);
this._sendCachedEntry(entry.data, entry.metadata, response);
return;
}
debug('Cache entry expired for %s', cacheKey);
// (continue to forward the request)
} catch (error) {
if (error.code !== 'ENOENT') {
console.warn('Cannot load cached entry.', error);
}
debug('Cache miss for %s', cacheKey);
// (continue to forward the request)
}
await this._forwardRequest(request, response);
}
private _getCacheKey(request: IncomingMessage): string {
// TODO(bajtos) consider adding selected/all headers to the key
return `${request.method} ${request.url}`;
}
private _sendCachedEntry(
data: Buffer,
metadata: CachedMetadata,
response: ServerResponse,
) {
response.writeHead(metadata.statusCode, metadata.headers);
response.end(data);
}
private async _forwardRequest(
clientRequest: IncomingMessage,
clientResponse: ServerResponse,
) {
debug('Forward request to %s %s', clientRequest.method, clientRequest.url);
const backendResponse = await this._axios({
method: clientRequest.method,
url: clientRequest.url!,
headers: clientRequest.headers,
data: clientRequest,
// Set the response type to `arraybuffer` to force the `data` to be a
// Buffer to allow ease of caching
// Since this proxy is for testing only, buffering the entire
// response body is acceptable.
responseType: 'arraybuffer',
timeout: this._options.timeout || undefined,
});
// If not removed, returns an "Expected http/" error.
delete backendResponse.headers['content-length'];
debug(
'Got response for %s %s -> %s',
clientRequest.method,
clientRequest.url,
backendResponse.status,
backendResponse.headers,
backendResponse.data,
);
const metadata: CachedMetadata = {
statusCode: backendResponse.status,
headers: backendResponse.headers as OutgoingHttpHeaders,
createdAt: Date.now(),
};
// Ideally, we should pipe the backend response to both
// client response and cachache.put.stream.
// r.pipe(clientResponse);
// r.pipe(cacache.put.stream(...))
// To do so, we would have to defer .end() call on the client
// response until the content is stored in the cache,
// which is rather complex and involved.
// Without that synchronization, the client can start sending
// follow-up requests that won't be served from the cache as
// the cache has not been updated yet.
const data = backendResponse.data;
await cacache.put(
this._options.cachePath,
this._getCacheKey(clientRequest),
data,
{metadata},
);
clientResponse.writeHead(
backendResponse.status,
backendResponse.headers as OutgoingHttpHeaders,
);
clientResponse.end(data);
}
public logError(request: IncomingMessage, error: Error) {
if (this._options.logError) {
console.error(
'Cannot proxy %s %s.',
request.method,
request.url,
error.stack ?? error,
);
} else {
debug(
'Cannot proxy %s %s.',
request.method,
request.url,
error.stack ?? error,
);
}
}
}