coding-tool-x 3.9.5-261009 → 3.9.5-261013

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ 'use strict';
2
+
3
+ // Connection headers describe one transport hop, not the downstream socket.
4
+ function downstreamHeaders(headers = {}) {
5
+ const result = { ...headers };
6
+ const connectionHeaders = String(headers.connection || '').split(',').map(name => name.trim().toLowerCase());
7
+ for (const name of ['connection', 'keep-alive', 'proxy-connection', 'proxy-authenticate',
8
+ 'proxy-authorization', 'te', 'trailer', 'transfer-encoding', 'upgrade', ...connectionHeaders]) {
9
+ delete result[name];
10
+ }
11
+ return result;
12
+ }
13
+
14
+ function bindProxyRequest(proxyReq, res) {
15
+ if (!res) return;
16
+ const abort = () => {
17
+ if (!res.writableEnded && typeof proxyReq.destroy === 'function') proxyReq.destroy();
18
+ };
19
+ res.once('close', abort);
20
+ proxyReq.once('close', () => res.removeListener('close', abort));
21
+ if (res.destroyed) abort();
22
+ }
23
+
24
+ function prepareProxyResponse(proxyRes, res) {
25
+ proxyRes.headers = downstreamHeaders(proxyRes.headers);
26
+ const fail = error => {
27
+ if (res && !res.destroyed) res.destroy(error);
28
+ };
29
+ proxyRes.once('error', fail);
30
+ proxyRes.once('aborted', () => fail(new Error('Upstream response aborted')));
31
+ }
32
+
33
+ module.exports = { downstreamHeaders, bindProxyRequest, prepareProxyResponse };