@maiyunnet/kebab 9.26.3 → 9.26.5

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,9 @@
1
+ import type * as http from 'http';
2
+ import type * as http2 from 'http2';
3
+ import type { IProxyHeaderOptions } from '#kebab/lib/undici/proxy.js';
4
+ import type { THttpHeaders } from '#kebab/lib/undici.js';
5
+ export declare function filterProxyHeaders(headers: http.IncomingHttpHeaders | http2.IncomingHttpHeaders | THttpHeaders, res?: http2.Http2ServerResponse | http.ServerResponse, filter?: (h: string) => boolean): Record<string, string | string[]>;
6
+ export declare function buildProxyHeaders(req: http.IncomingMessage | http2.Http2ServerRequest, opt?: IProxyHeaderOptions & {
7
+ headers?: http.IncomingHttpHeaders;
8
+ filter?: (h: string) => boolean;
9
+ }): http.IncomingHttpHeaders;
package/abox/proxy.js ADDED
@@ -0,0 +1,62 @@
1
+ const excludedHeaders = new Set([
2
+ 'host', 'connection', 'keep-alive', 'http-code', 'http-url',
3
+ 'transfer-encoding', 'upgrade', 'proxy-connection',
4
+ ]);
5
+ const privateHeaders = new Set([
6
+ 'forwarded', 'via', 'x-real-ip', 'true-client-ip', 'client-ip', 'x-client-ip',
7
+ 'fastly-client-ip', 'x-cluster-client-ip', 'x-envoy-external-address',
8
+ 'x-original-forwarded-for', 'x-original-host', 'x-original-url', 'x-rewrite-url',
9
+ ]);
10
+ export function filterProxyHeaders(headers, res, filter) {
11
+ const result = {};
12
+ const sent = res?.headersSent ?? false;
13
+ for (const name in headers) {
14
+ if (excludedHeaders.has(name) || name.includes(':') || name.includes('(')) {
15
+ continue;
16
+ }
17
+ const value = headers[name];
18
+ if (value === undefined || (filter && !filter(name))) {
19
+ continue;
20
+ }
21
+ if (res) {
22
+ if (!sent) {
23
+ res.setHeader(name, value);
24
+ }
25
+ }
26
+ else {
27
+ result[name] = value;
28
+ }
29
+ }
30
+ return result;
31
+ }
32
+ export function buildProxyHeaders(req, opt = {}) {
33
+ const headers = filterProxyHeaders(req.headers, undefined, opt.filter);
34
+ if (opt.proxyMode === undefined || opt.proxyMode === 'passthrough') {
35
+ return Object.assign(headers, opt.headers);
36
+ }
37
+ const result = {};
38
+ for (const name in headers) {
39
+ const key = name.toLowerCase();
40
+ if (key === 'host' || privateHeaders.has(key) || key.startsWith('x-forwarded-') ||
41
+ key.startsWith('cf-') || key.startsWith('eo-')) {
42
+ continue;
43
+ }
44
+ result[key] = headers[name];
45
+ }
46
+ if (opt.proxyMode === 'reverse') {
47
+ const host = req.headers[':authority'] ?? req.headers['host'];
48
+ if (typeof host === 'string' && host) {
49
+ result['host'] = host;
50
+ result['x-forwarded-host'] = host;
51
+ }
52
+ result['x-forwarded-proto'] = req.socket.encrypted ? 'https' : 'http';
53
+ if (req.socket.remoteAddress) {
54
+ result['x-forwarded-for'] = req.socket.remoteAddress;
55
+ result['x-real-ip'] = req.socket.remoteAddress;
56
+ }
57
+ }
58
+ for (const name in opt.headers) {
59
+ result[name.toLowerCase()] = opt.headers[name];
60
+ }
61
+ return result;
62
+ }