@wooksjs/http-proxy 0.4.9 → 0.4.10

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