@wooksjs/http-proxy 0.5.20 → 0.6.0
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.
- package/LICENSE +21 -0
- package/dist/index.cjs +149 -141
- package/dist/index.mjs +126 -140
- package/package.json +12 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 wooksjs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
CHANGED
|
@@ -1,151 +1,159 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
2
23
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
24
|
+
//#endregion
|
|
25
|
+
const __wooksjs_event_core = __toESM(require("@wooksjs/event-core"));
|
|
26
|
+
const __wooksjs_event_http = __toESM(require("@wooksjs/event-http"));
|
|
27
|
+
const node_fetch_native = __toESM(require("node-fetch-native"));
|
|
6
28
|
|
|
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
|
-
|
|
29
|
+
//#region packages/http-proxy/src/proxy-utils.ts
|
|
30
|
+
var IterableRecords = class {
|
|
31
|
+
[Symbol.iterator]() {
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
index = 0;
|
|
35
|
+
next() {
|
|
36
|
+
return {
|
|
37
|
+
value: undefined,
|
|
38
|
+
done: true
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
var CookiesIterable = class extends IterableRecords {
|
|
43
|
+
cookies;
|
|
44
|
+
constructor(cookiesString) {
|
|
45
|
+
super();
|
|
46
|
+
this.cookies = cookiesString.split(/,\s(?!\d{2}[\s-])/u);
|
|
47
|
+
}
|
|
48
|
+
next() {
|
|
49
|
+
const str = this.cookies[this.index++];
|
|
50
|
+
const ind = str ? str.indexOf("=") : 0;
|
|
51
|
+
return this.index <= this.cookies.length ? {
|
|
52
|
+
value: [str.slice(0, ind), str.slice(ind + 1)],
|
|
53
|
+
done: false
|
|
54
|
+
} : {
|
|
55
|
+
value: undefined,
|
|
56
|
+
done: true
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
var HeadersIterable = class extends IterableRecords {
|
|
61
|
+
entries;
|
|
62
|
+
constructor(headers) {
|
|
63
|
+
super();
|
|
64
|
+
this.entries = Object.entries(headers);
|
|
65
|
+
}
|
|
66
|
+
next() {
|
|
67
|
+
return this.index < this.entries.length ? {
|
|
68
|
+
value: this.entries[this.index++],
|
|
69
|
+
done: false
|
|
70
|
+
} : {
|
|
71
|
+
value: undefined,
|
|
72
|
+
done: true
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
};
|
|
45
76
|
function applyProxyControls(records, controls, additionalBlockers) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
result[name] = value;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (overwrite) {
|
|
64
|
-
if (typeof overwrite === 'function') {
|
|
65
|
-
result = overwrite(result);
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
result = { ...result, ...overwrite };
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return result;
|
|
77
|
+
let result = {};
|
|
78
|
+
const { allow, block, overwrite } = controls;
|
|
79
|
+
const defaultedAllow = allow || "*";
|
|
80
|
+
if (defaultedAllow) for (const [name, value] of records) {
|
|
81
|
+
const add = block !== "*" && (!additionalBlockers || !additionalBlockers.includes(name)) && (defaultedAllow === "*" || defaultedAllow.find((item) => typeof item === "string" && name.toLowerCase() === item.toLowerCase() || item instanceof RegExp && item.test(name))) && !block?.find((item) => typeof item === "string" && name.toLowerCase() === item.toLowerCase() || item instanceof RegExp && item.test(name));
|
|
82
|
+
if (add) result[name] = value;
|
|
83
|
+
}
|
|
84
|
+
if (overwrite) if (typeof overwrite === "function") result = overwrite(result);
|
|
85
|
+
else result = {
|
|
86
|
+
...result,
|
|
87
|
+
...overwrite
|
|
88
|
+
};
|
|
89
|
+
return result;
|
|
72
90
|
}
|
|
73
91
|
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region packages/http-proxy/src/proxy.ts
|
|
74
94
|
const reqHeadersToBlock = [
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
95
|
+
"connection",
|
|
96
|
+
"accept-encoding",
|
|
97
|
+
"content-length",
|
|
98
|
+
"upgrade-insecure-requests",
|
|
99
|
+
"cookie"
|
|
100
|
+
];
|
|
101
|
+
const resHeadersToBlock = [
|
|
102
|
+
"transfer-encoding",
|
|
103
|
+
"content-encoding",
|
|
104
|
+
"set-cookie"
|
|
80
105
|
];
|
|
81
|
-
const resHeadersToBlock = ['transfer-encoding', 'content-encoding', 'set-cookie'];
|
|
82
106
|
function useProxy() {
|
|
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
|
-
if (resCookies) {
|
|
137
|
-
setHeadersObject['set-cookie'] = (setHeadersObject['set-cookie'] || []);
|
|
138
|
-
for (const [name, value] of Object.entries(resCookies)) {
|
|
139
|
-
if (name) {
|
|
140
|
-
setHeadersObject['set-cookie'].push(`${name}=${value}`);
|
|
141
|
-
if (opts?.debug) {
|
|
142
|
-
logger.info(`\t${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return resp;
|
|
148
|
-
};
|
|
107
|
+
const status = (0, __wooksjs_event_http.useStatus)();
|
|
108
|
+
const { setHeader, headers: getSetHeaders } = (0, __wooksjs_event_http.useSetHeaders)();
|
|
109
|
+
const { getCtx } = (0, __wooksjs_event_http.useHttpContext)();
|
|
110
|
+
const { req } = getCtx().event;
|
|
111
|
+
const logger = (0, __wooksjs_event_core.useEventLogger)("http-proxy");
|
|
112
|
+
const setHeadersObject = getSetHeaders();
|
|
113
|
+
return async function proxy(target, opts) {
|
|
114
|
+
const targetUrl = new URL(target);
|
|
115
|
+
const path = targetUrl.pathname || "/";
|
|
116
|
+
const url = new URL(path, targetUrl.origin).toString() + targetUrl.search;
|
|
117
|
+
const modifiedHeaders = {
|
|
118
|
+
...req.headers,
|
|
119
|
+
host: targetUrl.hostname
|
|
120
|
+
};
|
|
121
|
+
const headers = opts?.reqHeaders ? applyProxyControls(new HeadersIterable(modifiedHeaders), opts.reqHeaders, reqHeadersToBlock) : {};
|
|
122
|
+
const cookies = opts?.reqCookies && req.headers.cookie ? applyProxyControls(new CookiesIterable(req.headers.cookie), opts.reqCookies) : null;
|
|
123
|
+
if (cookies) headers.cookie = Object.entries(cookies).map((v) => v.join("=")).join("; ");
|
|
124
|
+
const method = opts?.method || req.method;
|
|
125
|
+
if (opts?.debug) {
|
|
126
|
+
logger.info(`${"\x1B[32m"}${req.method} ${req.url}${"\x1B[33m"} → ${"\x1B[36m"}${method} ${url}${"\x1B[33m"}`);
|
|
127
|
+
logger.info(`${"\x1B[33m"}headers:`, JSON.stringify(headers, null, " "), "\x1B[39m");
|
|
128
|
+
}
|
|
129
|
+
const resp = await (0, node_fetch_native.fetch)(url, {
|
|
130
|
+
method,
|
|
131
|
+
body: ["GET", "HEAD"].includes(method) ? undefined : req,
|
|
132
|
+
headers
|
|
133
|
+
});
|
|
134
|
+
status.value = resp.status;
|
|
135
|
+
if (opts?.debug) {
|
|
136
|
+
logger.info(`${resp.status} ${"\x1B[32m"}${req.method} ${req.url}${"\x1B[33m"} → ${"\x1B[36m"}${method} ${url}${"\x1B[33m"}`);
|
|
137
|
+
logger.info(`${"\x1B[33m"}response headers:${"\x1B[39m"}`);
|
|
138
|
+
}
|
|
139
|
+
const resHeaders = opts?.resHeaders ? applyProxyControls(resp.headers.entries(), opts.resHeaders, resHeadersToBlock) : null;
|
|
140
|
+
const resCookies = opts?.resCookies ? applyProxyControls(new CookiesIterable(resp.headers.get("set-cookie") || ""), opts.resCookies) : null;
|
|
141
|
+
if (resHeaders) {
|
|
142
|
+
for (const [name, value] of Object.entries(resHeaders)) if (name) {
|
|
143
|
+
setHeader(name, value);
|
|
144
|
+
if (opts?.debug) logger.info(`\t${"\x1B[33m"}${name}=${"\x1B[32m"}${value}${"\x1B[39m"}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (resCookies) {
|
|
148
|
+
setHeadersObject["set-cookie"] = setHeadersObject["set-cookie"] || [];
|
|
149
|
+
for (const [name, value] of Object.entries(resCookies)) if (name) {
|
|
150
|
+
setHeadersObject["set-cookie"].push(`${name}=${value}`);
|
|
151
|
+
if (opts?.debug) logger.info(`\t${"\x1B[1m"}${"\x1B[33m"}set-cookie=${"\x1B[32m"}${name}=${value}${"\x1B[0m"}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return resp;
|
|
155
|
+
};
|
|
149
156
|
}
|
|
150
157
|
|
|
151
|
-
|
|
158
|
+
//#endregion
|
|
159
|
+
exports.useProxy = useProxy
|
package/dist/index.mjs
CHANGED
|
@@ -1,149 +1,135 @@
|
|
|
1
|
-
import { useEventLogger } from
|
|
2
|
-
import {
|
|
3
|
-
import { fetch } from
|
|
1
|
+
import { useEventLogger } from "@wooksjs/event-core";
|
|
2
|
+
import { useHttpContext, useSetHeaders, useStatus } from "@wooksjs/event-http";
|
|
3
|
+
import { fetch } from "node-fetch-native";
|
|
4
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
|
-
|
|
5
|
+
//#region packages/http-proxy/src/proxy-utils.ts
|
|
6
|
+
var IterableRecords = class {
|
|
7
|
+
[Symbol.iterator]() {
|
|
8
|
+
return this;
|
|
9
|
+
}
|
|
10
|
+
index = 0;
|
|
11
|
+
next() {
|
|
12
|
+
return {
|
|
13
|
+
value: undefined,
|
|
14
|
+
done: true
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var CookiesIterable = class extends IterableRecords {
|
|
19
|
+
cookies;
|
|
20
|
+
constructor(cookiesString) {
|
|
21
|
+
super();
|
|
22
|
+
this.cookies = cookiesString.split(/,\s(?!\d{2}[\s-])/u);
|
|
23
|
+
}
|
|
24
|
+
next() {
|
|
25
|
+
const str = this.cookies[this.index++];
|
|
26
|
+
const ind = str ? str.indexOf("=") : 0;
|
|
27
|
+
return this.index <= this.cookies.length ? {
|
|
28
|
+
value: [str.slice(0, ind), str.slice(ind + 1)],
|
|
29
|
+
done: false
|
|
30
|
+
} : {
|
|
31
|
+
value: undefined,
|
|
32
|
+
done: true
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var HeadersIterable = class extends IterableRecords {
|
|
37
|
+
entries;
|
|
38
|
+
constructor(headers) {
|
|
39
|
+
super();
|
|
40
|
+
this.entries = Object.entries(headers);
|
|
41
|
+
}
|
|
42
|
+
next() {
|
|
43
|
+
return this.index < this.entries.length ? {
|
|
44
|
+
value: this.entries[this.index++],
|
|
45
|
+
done: false
|
|
46
|
+
} : {
|
|
47
|
+
value: undefined,
|
|
48
|
+
done: true
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
};
|
|
43
52
|
function applyProxyControls(records, controls, additionalBlockers) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
result[name] = value;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
if (overwrite) {
|
|
62
|
-
if (typeof overwrite === 'function') {
|
|
63
|
-
result = overwrite(result);
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
result = { ...result, ...overwrite };
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return result;
|
|
53
|
+
let result = {};
|
|
54
|
+
const { allow, block, overwrite } = controls;
|
|
55
|
+
const defaultedAllow = allow || "*";
|
|
56
|
+
if (defaultedAllow) for (const [name, value] of records) {
|
|
57
|
+
const add = block !== "*" && (!additionalBlockers || !additionalBlockers.includes(name)) && (defaultedAllow === "*" || defaultedAllow.find((item) => typeof item === "string" && name.toLowerCase() === item.toLowerCase() || item instanceof RegExp && item.test(name))) && !block?.find((item) => typeof item === "string" && name.toLowerCase() === item.toLowerCase() || item instanceof RegExp && item.test(name));
|
|
58
|
+
if (add) result[name] = value;
|
|
59
|
+
}
|
|
60
|
+
if (overwrite) if (typeof overwrite === "function") result = overwrite(result);
|
|
61
|
+
else result = {
|
|
62
|
+
...result,
|
|
63
|
+
...overwrite
|
|
64
|
+
};
|
|
65
|
+
return result;
|
|
70
66
|
}
|
|
71
67
|
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region packages/http-proxy/src/proxy.ts
|
|
72
70
|
const reqHeadersToBlock = [
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
71
|
+
"connection",
|
|
72
|
+
"accept-encoding",
|
|
73
|
+
"content-length",
|
|
74
|
+
"upgrade-insecure-requests",
|
|
75
|
+
"cookie"
|
|
76
|
+
];
|
|
77
|
+
const resHeadersToBlock = [
|
|
78
|
+
"transfer-encoding",
|
|
79
|
+
"content-encoding",
|
|
80
|
+
"set-cookie"
|
|
78
81
|
];
|
|
79
|
-
const resHeadersToBlock = ['transfer-encoding', 'content-encoding', 'set-cookie'];
|
|
80
82
|
function useProxy() {
|
|
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
|
-
if (resCookies) {
|
|
135
|
-
setHeadersObject['set-cookie'] = (setHeadersObject['set-cookie'] || []);
|
|
136
|
-
for (const [name, value] of Object.entries(resCookies)) {
|
|
137
|
-
if (name) {
|
|
138
|
-
setHeadersObject['set-cookie'].push(`${name}=${value}`);
|
|
139
|
-
if (opts?.debug) {
|
|
140
|
-
logger.info(`\t${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return resp;
|
|
146
|
-
};
|
|
83
|
+
const status = useStatus();
|
|
84
|
+
const { setHeader, headers: getSetHeaders } = useSetHeaders();
|
|
85
|
+
const { getCtx } = useHttpContext();
|
|
86
|
+
const { req } = getCtx().event;
|
|
87
|
+
const logger = useEventLogger("http-proxy");
|
|
88
|
+
const setHeadersObject = getSetHeaders();
|
|
89
|
+
return async function proxy(target, opts) {
|
|
90
|
+
const targetUrl = new URL(target);
|
|
91
|
+
const path = targetUrl.pathname || "/";
|
|
92
|
+
const url = new URL(path, targetUrl.origin).toString() + targetUrl.search;
|
|
93
|
+
const modifiedHeaders = {
|
|
94
|
+
...req.headers,
|
|
95
|
+
host: targetUrl.hostname
|
|
96
|
+
};
|
|
97
|
+
const headers = opts?.reqHeaders ? applyProxyControls(new HeadersIterable(modifiedHeaders), opts.reqHeaders, reqHeadersToBlock) : {};
|
|
98
|
+
const cookies = opts?.reqCookies && req.headers.cookie ? applyProxyControls(new CookiesIterable(req.headers.cookie), opts.reqCookies) : null;
|
|
99
|
+
if (cookies) headers.cookie = Object.entries(cookies).map((v) => v.join("=")).join("; ");
|
|
100
|
+
const method = opts?.method || req.method;
|
|
101
|
+
if (opts?.debug) {
|
|
102
|
+
logger.info(`${"\x1B[32m"}${req.method} ${req.url}${"\x1B[33m"} → ${"\x1B[36m"}${method} ${url}${"\x1B[33m"}`);
|
|
103
|
+
logger.info(`${"\x1B[33m"}headers:`, JSON.stringify(headers, null, " "), "\x1B[39m");
|
|
104
|
+
}
|
|
105
|
+
const resp = await fetch(url, {
|
|
106
|
+
method,
|
|
107
|
+
body: ["GET", "HEAD"].includes(method) ? undefined : req,
|
|
108
|
+
headers
|
|
109
|
+
});
|
|
110
|
+
status.value = resp.status;
|
|
111
|
+
if (opts?.debug) {
|
|
112
|
+
logger.info(`${resp.status} ${"\x1B[32m"}${req.method} ${req.url}${"\x1B[33m"} → ${"\x1B[36m"}${method} ${url}${"\x1B[33m"}`);
|
|
113
|
+
logger.info(`${"\x1B[33m"}response headers:${"\x1B[39m"}`);
|
|
114
|
+
}
|
|
115
|
+
const resHeaders = opts?.resHeaders ? applyProxyControls(resp.headers.entries(), opts.resHeaders, resHeadersToBlock) : null;
|
|
116
|
+
const resCookies = opts?.resCookies ? applyProxyControls(new CookiesIterable(resp.headers.get("set-cookie") || ""), opts.resCookies) : null;
|
|
117
|
+
if (resHeaders) {
|
|
118
|
+
for (const [name, value] of Object.entries(resHeaders)) if (name) {
|
|
119
|
+
setHeader(name, value);
|
|
120
|
+
if (opts?.debug) logger.info(`\t${"\x1B[33m"}${name}=${"\x1B[32m"}${value}${"\x1B[39m"}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (resCookies) {
|
|
124
|
+
setHeadersObject["set-cookie"] = setHeadersObject["set-cookie"] || [];
|
|
125
|
+
for (const [name, value] of Object.entries(resCookies)) if (name) {
|
|
126
|
+
setHeadersObject["set-cookie"].push(`${name}=${value}`);
|
|
127
|
+
if (opts?.debug) logger.info(`\t${"\x1B[1m"}${"\x1B[33m"}set-cookie=${"\x1B[32m"}${name}=${value}${"\x1B[0m"}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return resp;
|
|
131
|
+
};
|
|
147
132
|
}
|
|
148
133
|
|
|
149
|
-
|
|
134
|
+
//#endregion
|
|
135
|
+
export { useProxy };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/http-proxy",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Proxy Wooks composable",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -39,11 +39,18 @@
|
|
|
39
39
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
|
-
"@wooksjs/event-
|
|
43
|
-
"@wooksjs/event-
|
|
42
|
+
"@wooksjs/event-core": "^0.6.0",
|
|
43
|
+
"@wooksjs/event-http": "^0.6.0"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"node-fetch-native": "^1.0.1"
|
|
47
47
|
},
|
|
48
|
-
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/http-proxy#readme"
|
|
49
|
-
|
|
48
|
+
"homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/http-proxy#readme",
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"typescript": "^5.7.3",
|
|
51
|
+
"vitest": "^2.1.8"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "rolldown -c ../../rolldown.config.mjs"
|
|
55
|
+
}
|
|
56
|
+
}
|