158
}
159
160
>
function postOnce(mod: typeof http | typeof https, options: IPostOptions, body: Buffer): Promise<void> {
outboundForwarder.ts
161
>
return new Promise<void>((resolve, reject) => {
162
>
const req = mod.request({
163
>
host: options.host,
164
>
port: options.port,
165
>
path: options.path,
166
>
method: options.method,
167
>
headers: options.headers,
168
>
timeout: options.timeoutMs,
169
>
});
170
>
const onError = (err: Error) => { req.destroy(); reject(err); };
171
>
req.on('error', onError);
172
>
req.on('timeout', () => onError(new Error(`request timeout after ${options.timeoutMs}ms`)));
173
>
req.on('response', res => {
174
>
// Drain the body so the connection can be reused.
175
>
res.resume();
176
>
res.on('end', () => {
177
>
const status = res.statusCode ?? 0;
178
>
if (status >= 200 && status < 300) {
179
resolve();
181
reject(new Error(`upstream returned HTTP ${status}`));
182
}
184
>
res.on('error', onError);
185
>
});
186
>
req.end(body);
187
>
});
188
>
}
189
190
// ---------------------------------------------------------------------------