@wooksjs/http-proxy 0.4.10 → 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 CHANGED
@@ -4,38 +4,6 @@ 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
- Copyright (c) Microsoft Corporation.
9
-
10
- Permission to use, copy, modify, and/or distribute this software for any
11
- purpose with or without fee is hereby granted.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
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
- /* global Reflect, Promise, SuppressedError, Symbol */
22
-
23
-
24
- function __awaiter(thisArg, _arguments, P, generator) {
25
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
26
- return new (P || (P = Promise))(function (resolve, reject) {
27
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
28
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
29
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
30
- step((generator = generator.apply(thisArg, _arguments || [])).next());
31
- });
32
- }
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
7
  class IterableRecords {
40
8
  constructor() {
41
9
  this.index = 0;
@@ -100,7 +68,7 @@ function applyProxyControls(records, controls, additionalBlockers) {
100
68
  result = overwrite(result);
101
69
  }
102
70
  else {
103
- result = Object.assign(Object.assign({}, result), overwrite);
71
+ result = { ...result, ...overwrite };
104
72
  }
105
73
  }
106
74
  return result;
@@ -125,74 +93,72 @@ function useProxy() {
125
93
  const { req } = getCtx().event;
126
94
  const logger = eventCore.useEventLogger('http-proxy');
127
95
  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
- }
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(`${''}${req.method} ${req.url}${''} ${''}${method} ${url}${''}`);
117
+ logger.info('' + 'headers:', JSON.stringify(headers, null, ' '), '');
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} ${''}${req.method} ${req.url}${''} ${''}${method} ${url}${''}`);
130
+ logger.info(`${''}response headers:${''}`);
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${''}${name}=${''}${value}${''}`);
179
145
  }
180
146
  }
181
147
  }
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
- }
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${''}${''}set-cookie=${''}${name}=${value}${''}`);
191
157
  }
192
158
  }
193
159
  }
194
- return resp;
195
- });
160
+ }
161
+ return resp;
196
162
  };
197
163
  }
198
164
 
package/dist/index.mjs CHANGED
@@ -2,38 +2,6 @@ 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
- Copyright (c) Microsoft Corporation.
7
-
8
- Permission to use, copy, modify, and/or distribute this software for any
9
- purpose with or without fee is hereby granted.
10
-
11
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
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
- /* global Reflect, Promise, SuppressedError, Symbol */
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
- });
30
- }
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
5
  class IterableRecords {
38
6
  constructor() {
39
7
  this.index = 0;
@@ -98,7 +66,7 @@ function applyProxyControls(records, controls, additionalBlockers) {
98
66
  result = overwrite(result);
99
67
  }
100
68
  else {
101
- result = Object.assign(Object.assign({}, result), overwrite);
69
+ result = { ...result, ...overwrite };
102
70
  }
103
71
  }
104
72
  return result;
@@ -123,74 +91,72 @@ function useProxy() {
123
91
  const { req } = getCtx().event;
124
92
  const logger = useEventLogger('http-proxy');
125
93
  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
- }
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(`${''}${req.method} ${req.url}${''} ${''}${method} ${url}${''}`);
115
+ logger.info('' + 'headers:', JSON.stringify(headers, null, ' '), '');
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} ${''}${req.method} ${req.url}${''} ${''}${method} ${url}${''}`);
128
+ logger.info(`${''}response headers:${''}`);
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${''}${name}=${''}${value}${''}`);
177
143
  }
178
144
  }
179
145
  }
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
- }
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${''}${''}set-cookie=${''}${name}=${value}${''}`);
189
155
  }
190
156
  }
191
157
  }
192
- return resp;
193
- });
158
+ }
159
+ return resp;
194
160
  };
195
161
  }
196
162
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/http-proxy",
3
- "version": "0.4.10",
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.10",
35
- "@wooksjs/event-core": "0.4.10"
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"