@wooksjs/http-proxy 0.4.9 → 0.4.11
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/dist/index.cjs +153 -180
- package/dist/index.mjs +153 -180
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -4,189 +4,162 @@ var nodeFetchNative = require('node-fetch-native');
|
|
|
4
4
|
var eventHttp = require('@wooksjs/event-http');
|
|
5
5
|
var eventCore = require('@wooksjs/event-core');
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
-
***************************************************************************** */
|
|
21
|
-
|
|
22
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
23
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
24
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
25
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
26
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
27
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
28
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
29
|
-
});
|
|
7
|
+
class IterableRecords {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.index = 0;
|
|
10
|
+
}
|
|
11
|
+
[Symbol.iterator]() {
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
next() {
|
|
15
|
+
return { value: undefined, done: true };
|
|
16
|
+
}
|
|
30
17
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.
|
|
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
|
-
if (overwrite) {
|
|
92
|
-
if (typeof overwrite === 'function') {
|
|
93
|
-
result = overwrite(result);
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
result = Object.assign(Object.assign({}, result), overwrite);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return result;
|
|
18
|
+
class CookiesIterable extends IterableRecords {
|
|
19
|
+
constructor(cookiesString) {
|
|
20
|
+
super();
|
|
21
|
+
this.cookies = cookiesString.split(/,\s(?!\d{2}[\s-])/);
|
|
22
|
+
}
|
|
23
|
+
next() {
|
|
24
|
+
const str = this.cookies[this.index++];
|
|
25
|
+
const ind = str ? str.indexOf('=') : 0;
|
|
26
|
+
return this.index <= this.cookies.length
|
|
27
|
+
? {
|
|
28
|
+
value: [str.slice(0, ind), str.slice(ind + 1)],
|
|
29
|
+
done: false,
|
|
30
|
+
}
|
|
31
|
+
: { value: undefined, done: true };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
class HeadersIterable extends IterableRecords {
|
|
35
|
+
constructor(headers) {
|
|
36
|
+
super();
|
|
37
|
+
this.entries = Object.entries(headers);
|
|
38
|
+
}
|
|
39
|
+
next() {
|
|
40
|
+
return this.index < this.entries.length
|
|
41
|
+
? { value: this.entries[this.index++], done: false }
|
|
42
|
+
: { value: undefined, done: true };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function applyProxyControls(records, controls, additionalBlockers) {
|
|
46
|
+
let result = {};
|
|
47
|
+
const { allow, block, overwrite } = controls;
|
|
48
|
+
const defaultedAllow = allow || '*';
|
|
49
|
+
if (defaultedAllow) {
|
|
50
|
+
for (const [name, value] of records) {
|
|
51
|
+
const add = block !== '*' &&
|
|
52
|
+
(!additionalBlockers || !additionalBlockers.includes(name)) &&
|
|
53
|
+
(defaultedAllow === '*' ||
|
|
54
|
+
defaultedAllow.find((item) => (typeof item === 'string' &&
|
|
55
|
+
name.toLowerCase() === item.toLowerCase()) ||
|
|
56
|
+
(item instanceof RegExp && item.test(name)))) &&
|
|
57
|
+
(!block ||
|
|
58
|
+
!block.find((item) => (typeof item === 'string' &&
|
|
59
|
+
name.toLowerCase() === item.toLowerCase()) ||
|
|
60
|
+
(item instanceof RegExp && item.test(name))));
|
|
61
|
+
if (add) {
|
|
62
|
+
result[name] = value;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (overwrite) {
|
|
67
|
+
if (typeof overwrite === 'function') {
|
|
68
|
+
result = overwrite(result);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
result = { ...result, ...overwrite };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
100
75
|
}
|
|
101
76
|
|
|
102
|
-
const reqHeadersToBlock = [
|
|
103
|
-
'connection',
|
|
104
|
-
'accept-encoding',
|
|
105
|
-
'content-length',
|
|
106
|
-
'upgrade-insecure-requests',
|
|
107
|
-
'cookie',
|
|
108
|
-
];
|
|
109
|
-
const resHeadersToBlock = [
|
|
110
|
-
'transfer-encoding',
|
|
111
|
-
'content-encoding',
|
|
112
|
-
'set-cookie',
|
|
113
|
-
];
|
|
114
|
-
function useProxy() {
|
|
115
|
-
const status = eventHttp.useStatus();
|
|
116
|
-
const { setHeader, headers: getSetHeaders } = eventHttp.useSetHeaders();
|
|
117
|
-
const { getCtx } = eventHttp.useHttpContext();
|
|
118
|
-
const { req } = getCtx().event;
|
|
119
|
-
const logger = eventCore.useEventLogger('http-proxy');
|
|
120
|
-
const setHeadersObject = getSetHeaders();
|
|
121
|
-
return function proxy(target, opts) {
|
|
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
|
-
};
|
|
77
|
+
const reqHeadersToBlock = [
|
|
78
|
+
'connection',
|
|
79
|
+
'accept-encoding',
|
|
80
|
+
'content-length',
|
|
81
|
+
'upgrade-insecure-requests',
|
|
82
|
+
'cookie',
|
|
83
|
+
];
|
|
84
|
+
const resHeadersToBlock = [
|
|
85
|
+
'transfer-encoding',
|
|
86
|
+
'content-encoding',
|
|
87
|
+
'set-cookie',
|
|
88
|
+
];
|
|
89
|
+
function useProxy() {
|
|
90
|
+
const status = eventHttp.useStatus();
|
|
91
|
+
const { setHeader, headers: getSetHeaders } = eventHttp.useSetHeaders();
|
|
92
|
+
const { getCtx } = eventHttp.useHttpContext();
|
|
93
|
+
const { req } = getCtx().event;
|
|
94
|
+
const logger = eventCore.useEventLogger('http-proxy');
|
|
95
|
+
const setHeadersObject = getSetHeaders();
|
|
96
|
+
return async function proxy(target, opts) {
|
|
97
|
+
const targetUrl = new URL(target);
|
|
98
|
+
const path = targetUrl.pathname || '/';
|
|
99
|
+
const url = new URL(path, targetUrl.origin).toString() + targetUrl.search;
|
|
100
|
+
// preparing request headers and cookies
|
|
101
|
+
const modifiedHeaders = { ...req.headers, host: targetUrl.hostname };
|
|
102
|
+
const headers = opts?.reqHeaders
|
|
103
|
+
? applyProxyControls(new HeadersIterable(modifiedHeaders), opts?.reqHeaders, reqHeadersToBlock)
|
|
104
|
+
: {};
|
|
105
|
+
const cookies = opts?.reqCookies && req.headers.cookie
|
|
106
|
+
? applyProxyControls(new CookiesIterable(req.headers.cookie), opts?.reqCookies)
|
|
107
|
+
: null;
|
|
108
|
+
if (cookies) {
|
|
109
|
+
headers.cookie = Object.entries(cookies)
|
|
110
|
+
.map((v) => v.join('='))
|
|
111
|
+
.join('; ');
|
|
112
|
+
}
|
|
113
|
+
const method = opts?.method || req.method;
|
|
114
|
+
// actual request
|
|
115
|
+
if (opts?.debug) {
|
|
116
|
+
logger.info(`${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
117
|
+
logger.info('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
118
|
+
}
|
|
119
|
+
const resp = await nodeFetchNative.fetch(url, {
|
|
120
|
+
method,
|
|
121
|
+
body: ['GET', 'HEAD'].includes(method)
|
|
122
|
+
? undefined
|
|
123
|
+
: req,
|
|
124
|
+
headers: headers,
|
|
125
|
+
});
|
|
126
|
+
// preparing response
|
|
127
|
+
status.value = resp.status;
|
|
128
|
+
if (opts?.debug) {
|
|
129
|
+
logger.info(`${resp.status} ${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
130
|
+
logger.info(`${'[33m'}response headers:${'[39m'}`);
|
|
131
|
+
}
|
|
132
|
+
// preparing response headers
|
|
133
|
+
const resHeaders = opts?.resHeaders
|
|
134
|
+
? applyProxyControls(resp.headers.entries(), opts?.resHeaders, resHeadersToBlock)
|
|
135
|
+
: null;
|
|
136
|
+
const resCookies = opts?.resCookies
|
|
137
|
+
? applyProxyControls(new CookiesIterable(resp.headers.get('set-cookie') || ''), opts?.resCookies)
|
|
138
|
+
: null;
|
|
139
|
+
if (resHeaders) {
|
|
140
|
+
for (const [name, value] of Object.entries(resHeaders)) {
|
|
141
|
+
if (name) {
|
|
142
|
+
setHeader(name, value);
|
|
143
|
+
if (opts?.debug) {
|
|
144
|
+
logger.info(`\t${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (resCookies) {
|
|
150
|
+
setHeadersObject['set-cookie'] = (setHeadersObject['set-cookie'] ||
|
|
151
|
+
[]);
|
|
152
|
+
for (const [name, value] of Object.entries(resCookies)) {
|
|
153
|
+
if (name) {
|
|
154
|
+
setHeadersObject['set-cookie'].push(`${name}=${value}`);
|
|
155
|
+
if (opts?.debug) {
|
|
156
|
+
logger.info(`\t${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return resp;
|
|
162
|
+
};
|
|
190
163
|
}
|
|
191
164
|
|
|
192
165
|
exports.useProxy = useProxy;
|
package/dist/index.mjs
CHANGED
|
@@ -2,189 +2,162 @@ import { fetch } from 'node-fetch-native';
|
|
|
2
2
|
import { useStatus, useSetHeaders, useHttpContext } from '@wooksjs/event-http';
|
|
3
3
|
import { useEventLogger } from '@wooksjs/event-core';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
16
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
17
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
18
|
-
***************************************************************************** */
|
|
19
|
-
|
|
20
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
21
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
22
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
23
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
24
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
25
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
26
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
27
|
-
});
|
|
5
|
+
class IterableRecords {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.index = 0;
|
|
8
|
+
}
|
|
9
|
+
[Symbol.iterator]() {
|
|
10
|
+
return this;
|
|
11
|
+
}
|
|
12
|
+
next() {
|
|
13
|
+
return { value: undefined, done: true };
|
|
14
|
+
}
|
|
28
15
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.
|
|
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
|
-
if (overwrite) {
|
|
90
|
-
if (typeof overwrite === 'function') {
|
|
91
|
-
result = overwrite(result);
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
result = Object.assign(Object.assign({}, result), overwrite);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return result;
|
|
16
|
+
class CookiesIterable extends IterableRecords {
|
|
17
|
+
constructor(cookiesString) {
|
|
18
|
+
super();
|
|
19
|
+
this.cookies = cookiesString.split(/,\s(?!\d{2}[\s-])/);
|
|
20
|
+
}
|
|
21
|
+
next() {
|
|
22
|
+
const str = this.cookies[this.index++];
|
|
23
|
+
const ind = str ? str.indexOf('=') : 0;
|
|
24
|
+
return this.index <= this.cookies.length
|
|
25
|
+
? {
|
|
26
|
+
value: [str.slice(0, ind), str.slice(ind + 1)],
|
|
27
|
+
done: false,
|
|
28
|
+
}
|
|
29
|
+
: { value: undefined, done: true };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
class HeadersIterable extends IterableRecords {
|
|
33
|
+
constructor(headers) {
|
|
34
|
+
super();
|
|
35
|
+
this.entries = Object.entries(headers);
|
|
36
|
+
}
|
|
37
|
+
next() {
|
|
38
|
+
return this.index < this.entries.length
|
|
39
|
+
? { value: this.entries[this.index++], done: false }
|
|
40
|
+
: { value: undefined, done: true };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function applyProxyControls(records, controls, additionalBlockers) {
|
|
44
|
+
let result = {};
|
|
45
|
+
const { allow, block, overwrite } = controls;
|
|
46
|
+
const defaultedAllow = allow || '*';
|
|
47
|
+
if (defaultedAllow) {
|
|
48
|
+
for (const [name, value] of records) {
|
|
49
|
+
const add = block !== '*' &&
|
|
50
|
+
(!additionalBlockers || !additionalBlockers.includes(name)) &&
|
|
51
|
+
(defaultedAllow === '*' ||
|
|
52
|
+
defaultedAllow.find((item) => (typeof item === 'string' &&
|
|
53
|
+
name.toLowerCase() === item.toLowerCase()) ||
|
|
54
|
+
(item instanceof RegExp && item.test(name)))) &&
|
|
55
|
+
(!block ||
|
|
56
|
+
!block.find((item) => (typeof item === 'string' &&
|
|
57
|
+
name.toLowerCase() === item.toLowerCase()) ||
|
|
58
|
+
(item instanceof RegExp && item.test(name))));
|
|
59
|
+
if (add) {
|
|
60
|
+
result[name] = value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (overwrite) {
|
|
65
|
+
if (typeof overwrite === 'function') {
|
|
66
|
+
result = overwrite(result);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
result = { ...result, ...overwrite };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
98
73
|
}
|
|
99
74
|
|
|
100
|
-
const reqHeadersToBlock = [
|
|
101
|
-
'connection',
|
|
102
|
-
'accept-encoding',
|
|
103
|
-
'content-length',
|
|
104
|
-
'upgrade-insecure-requests',
|
|
105
|
-
'cookie',
|
|
106
|
-
];
|
|
107
|
-
const resHeadersToBlock = [
|
|
108
|
-
'transfer-encoding',
|
|
109
|
-
'content-encoding',
|
|
110
|
-
'set-cookie',
|
|
111
|
-
];
|
|
112
|
-
function useProxy() {
|
|
113
|
-
const status = useStatus();
|
|
114
|
-
const { setHeader, headers: getSetHeaders } = useSetHeaders();
|
|
115
|
-
const { getCtx } = useHttpContext();
|
|
116
|
-
const { req } = getCtx().event;
|
|
117
|
-
const logger = useEventLogger('http-proxy');
|
|
118
|
-
const setHeadersObject = getSetHeaders();
|
|
119
|
-
return function proxy(target, opts) {
|
|
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
|
-
};
|
|
75
|
+
const reqHeadersToBlock = [
|
|
76
|
+
'connection',
|
|
77
|
+
'accept-encoding',
|
|
78
|
+
'content-length',
|
|
79
|
+
'upgrade-insecure-requests',
|
|
80
|
+
'cookie',
|
|
81
|
+
];
|
|
82
|
+
const resHeadersToBlock = [
|
|
83
|
+
'transfer-encoding',
|
|
84
|
+
'content-encoding',
|
|
85
|
+
'set-cookie',
|
|
86
|
+
];
|
|
87
|
+
function useProxy() {
|
|
88
|
+
const status = useStatus();
|
|
89
|
+
const { setHeader, headers: getSetHeaders } = useSetHeaders();
|
|
90
|
+
const { getCtx } = useHttpContext();
|
|
91
|
+
const { req } = getCtx().event;
|
|
92
|
+
const logger = useEventLogger('http-proxy');
|
|
93
|
+
const setHeadersObject = getSetHeaders();
|
|
94
|
+
return async function proxy(target, opts) {
|
|
95
|
+
const targetUrl = new URL(target);
|
|
96
|
+
const path = targetUrl.pathname || '/';
|
|
97
|
+
const url = new URL(path, targetUrl.origin).toString() + targetUrl.search;
|
|
98
|
+
// preparing request headers and cookies
|
|
99
|
+
const modifiedHeaders = { ...req.headers, host: targetUrl.hostname };
|
|
100
|
+
const headers = opts?.reqHeaders
|
|
101
|
+
? applyProxyControls(new HeadersIterable(modifiedHeaders), opts?.reqHeaders, reqHeadersToBlock)
|
|
102
|
+
: {};
|
|
103
|
+
const cookies = opts?.reqCookies && req.headers.cookie
|
|
104
|
+
? applyProxyControls(new CookiesIterable(req.headers.cookie), opts?.reqCookies)
|
|
105
|
+
: null;
|
|
106
|
+
if (cookies) {
|
|
107
|
+
headers.cookie = Object.entries(cookies)
|
|
108
|
+
.map((v) => v.join('='))
|
|
109
|
+
.join('; ');
|
|
110
|
+
}
|
|
111
|
+
const method = opts?.method || req.method;
|
|
112
|
+
// actual request
|
|
113
|
+
if (opts?.debug) {
|
|
114
|
+
logger.info(`${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
115
|
+
logger.info('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
116
|
+
}
|
|
117
|
+
const resp = await fetch(url, {
|
|
118
|
+
method,
|
|
119
|
+
body: ['GET', 'HEAD'].includes(method)
|
|
120
|
+
? undefined
|
|
121
|
+
: req,
|
|
122
|
+
headers: headers,
|
|
123
|
+
});
|
|
124
|
+
// preparing response
|
|
125
|
+
status.value = resp.status;
|
|
126
|
+
if (opts?.debug) {
|
|
127
|
+
logger.info(`${resp.status} ${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
128
|
+
logger.info(`${'[33m'}response headers:${'[39m'}`);
|
|
129
|
+
}
|
|
130
|
+
// preparing response headers
|
|
131
|
+
const resHeaders = opts?.resHeaders
|
|
132
|
+
? applyProxyControls(resp.headers.entries(), opts?.resHeaders, resHeadersToBlock)
|
|
133
|
+
: null;
|
|
134
|
+
const resCookies = opts?.resCookies
|
|
135
|
+
? applyProxyControls(new CookiesIterable(resp.headers.get('set-cookie') || ''), opts?.resCookies)
|
|
136
|
+
: null;
|
|
137
|
+
if (resHeaders) {
|
|
138
|
+
for (const [name, value] of Object.entries(resHeaders)) {
|
|
139
|
+
if (name) {
|
|
140
|
+
setHeader(name, value);
|
|
141
|
+
if (opts?.debug) {
|
|
142
|
+
logger.info(`\t${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (resCookies) {
|
|
148
|
+
setHeadersObject['set-cookie'] = (setHeadersObject['set-cookie'] ||
|
|
149
|
+
[]);
|
|
150
|
+
for (const [name, value] of Object.entries(resCookies)) {
|
|
151
|
+
if (name) {
|
|
152
|
+
setHeadersObject['set-cookie'].push(`${name}=${value}`);
|
|
153
|
+
if (opts?.debug) {
|
|
154
|
+
logger.info(`\t${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return resp;
|
|
160
|
+
};
|
|
188
161
|
}
|
|
189
162
|
|
|
190
163
|
export { useProxy };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/http-proxy",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.11",
|
|
4
4
|
"description": "Proxy Wooks composable",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"url": "https://github.com/wooksjs/wooksjs/issues"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"@wooksjs/event-http": "0.4.
|
|
35
|
-
"@wooksjs/event-core": "0.4.
|
|
34
|
+
"@wooksjs/event-http": "0.4.11",
|
|
35
|
+
"@wooksjs/event-core": "0.4.11"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"node-fetch-native": "^1.0.1"
|