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