@wooksjs/http-proxy 0.2.18 → 0.2.20

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/README.md CHANGED
@@ -9,7 +9,6 @@
9
9
  </a>
10
10
  </p>
11
11
 
12
-
13
12
  Wooks Proxy is composable proxy for [@wooksjs/event-http](https://github.com/wooksjs/wooksjs/tree/main/packages/event-http)
14
13
 
15
14
  🔥 An easy way to proxy request!
@@ -26,7 +25,6 @@ app.get('/to-proxy', () => {
26
25
  const proxy = useProxy()
27
26
  return proxy('https://target-website.com/target-path?query=123')
28
27
  })
29
-
30
28
  ```
31
29
 
32
30
  ### Restrict cookies/headers to pass
@@ -40,7 +38,6 @@ app.get('/to-proxy', () => {
40
38
  reqCookies: { block: '*' }, // block all req cookies
41
39
  })
42
40
  })
43
-
44
41
  ```
45
42
 
46
43
  ### Change response
@@ -55,10 +52,10 @@ app.get('/to-proxy', async () => {
55
52
  const data = { ...(await response.json()), newField: 'new value' }
56
53
  return data
57
54
  })
58
-
59
55
  ```
60
56
 
61
57
  ## Proxy advanced options
58
+
62
59
  ```ts
63
60
  import { useProxy } from '@wooksjs/http-proxy'
64
61
  import { useRequest } from '@wooksjs/composables'
@@ -103,5 +100,3 @@ app.get('*', async () => {
103
100
  ## Documentation
104
101
 
105
102
  To check out docs, visit [wooksjs.org](https://wooksjs.org/).
106
-
107
-
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 */
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); }); }
@@ -48,9 +50,12 @@ class CookiesIterable extends IterableRecords {
48
50
  next() {
49
51
  const str = this.cookies[this.index++];
50
52
  const ind = str ? str.indexOf('=') : 0;
51
- return this.index <= this.cookies.length ?
52
- { value: [str.slice(0, ind), str.slice(ind + 1)], done: false } :
53
- { value: undefined, done: true };
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 };
54
59
  }
55
60
  }
56
61
  class HeadersIterable extends IterableRecords {
@@ -59,9 +64,9 @@ class HeadersIterable extends IterableRecords {
59
64
  this.entries = Object.entries(headers);
60
65
  }
61
66
  next() {
62
- return this.index < this.entries.length ?
63
- { value: this.entries[this.index++], done: false } :
64
- { value: undefined, done: true };
67
+ return this.index < this.entries.length
68
+ ? { value: this.entries[this.index++], done: false }
69
+ : { value: undefined, done: true };
65
70
  }
66
71
  }
67
72
  function applyProxyControls(records, controls, additionalBlockers) {
@@ -71,8 +76,15 @@ function applyProxyControls(records, controls, additionalBlockers) {
71
76
  if (defaultedAllow) {
72
77
  for (const [name, value] of records) {
73
78
  const add = block !== '*' &&
74
- (!additionalBlockers || !additionalBlockers.includes(name)) && (defaultedAllow === '*' ||
75
- (defaultedAllow.find(item => typeof item === 'string' && name.toLowerCase() === item.toLowerCase() || item instanceof RegExp && item.test(name)))) && (!block || !block.find(item => typeof item === 'string' && name.toLowerCase() === item.toLowerCase() || item instanceof RegExp && item.test(name)));
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))));
76
88
  if (add) {
77
89
  result[name] = value;
78
90
  }
@@ -249,7 +261,12 @@ function createConsoleTransort(opts) {
249
261
  console.debug(formatted);
250
262
  break;
251
263
  case 6:
252
- console.trace(formatted);
264
+ if (opts === null || opts === void 0 ? void 0 : opts.trace) {
265
+ console.trace(formatted);
266
+ }
267
+ else {
268
+ console.debug(formatted);
269
+ }
253
270
  break;
254
271
  default:
255
272
  console.log(formatted);
@@ -339,13 +356,19 @@ function useProxy() {
339
356
  return __awaiter(this, void 0, void 0, function* () {
340
357
  const targetUrl = new URL(target);
341
358
  const path = targetUrl.pathname || '/';
342
- const url = new URL(path, targetUrl.origin).toString() + (targetUrl.search);
359
+ const url = new URL(path, targetUrl.origin).toString() + targetUrl.search;
343
360
  // preparing request headers and cookies
344
361
  const modifiedHeaders = Object.assign(Object.assign({}, req.headers), { host: targetUrl.hostname });
345
- const headers = (opts === null || opts === void 0 ? void 0 : opts.reqHeaders) ? applyProxyControls(new HeadersIterable(modifiedHeaders), opts === null || opts === void 0 ? void 0 : opts.reqHeaders, reqHeadersToBlock) : {};
346
- const cookies = (opts === null || opts === void 0 ? void 0 : opts.reqCookies) && req.headers.cookie ? applyProxyControls(new CookiesIterable(req.headers.cookie), opts === null || opts === void 0 ? void 0 : opts.reqCookies) : null;
362
+ const headers = (opts === null || opts === void 0 ? void 0 : opts.reqHeaders)
363
+ ? applyProxyControls(new HeadersIterable(modifiedHeaders), opts === null || opts === void 0 ? void 0 : opts.reqHeaders, reqHeadersToBlock)
364
+ : {};
365
+ const cookies = (opts === null || opts === void 0 ? void 0 : opts.reqCookies) && req.headers.cookie
366
+ ? applyProxyControls(new CookiesIterable(req.headers.cookie), opts === null || opts === void 0 ? void 0 : opts.reqCookies)
367
+ : null;
347
368
  if (cookies) {
348
- headers.cookie = Object.entries(cookies).map(v => v.join('=')).join('; ');
369
+ headers.cookie = Object.entries(cookies)
370
+ .map((v) => v.join('='))
371
+ .join('; ');
349
372
  }
350
373
  const method = (opts === null || opts === void 0 ? void 0 : opts.method) || req.method;
351
374
  // actual request
@@ -355,7 +378,9 @@ function useProxy() {
355
378
  }
356
379
  const resp = yield nodeFetchNative.fetch(url, {
357
380
  method,
358
- body: ['GET', 'HEAD'].includes(method) ? undefined : req,
381
+ body: ['GET', 'HEAD'].includes(method)
382
+ ? undefined
383
+ : req,
359
384
  headers: headers,
360
385
  });
361
386
  // preparing response
@@ -365,8 +390,12 @@ function useProxy() {
365
390
  logger.info(`${''}response headers:${''}`);
366
391
  }
367
392
  // preparing response headers
368
- const resHeaders = (opts === null || opts === void 0 ? void 0 : opts.resHeaders) ? applyProxyControls(resp.headers.entries(), opts === null || opts === void 0 ? void 0 : opts.resHeaders, resHeadersToBlock) : null;
369
- const resCookies = (opts === null || opts === void 0 ? void 0 : opts.resCookies) ? applyProxyControls(new CookiesIterable(resp.headers.get('set-cookie') || ''), opts === null || opts === void 0 ? void 0 : opts.resCookies) : null;
393
+ const resHeaders = (opts === null || opts === void 0 ? void 0 : opts.resHeaders)
394
+ ? applyProxyControls(resp.headers.entries(), opts === null || opts === void 0 ? void 0 : opts.resHeaders, resHeadersToBlock)
395
+ : null;
396
+ const resCookies = (opts === null || opts === void 0 ? void 0 : opts.resCookies)
397
+ ? applyProxyControls(new CookiesIterable(resp.headers.get('set-cookie') || ''), opts === null || opts === void 0 ? void 0 : opts.resCookies)
398
+ : null;
370
399
  if (resHeaders) {
371
400
  for (const [name, value] of Object.entries(resHeaders)) {
372
401
  if (name) {
@@ -378,7 +407,8 @@ function useProxy() {
378
407
  }
379
408
  }
380
409
  if (resCookies) {
381
- setHeadersObject['set-cookie'] = (setHeadersObject['set-cookie'] || []);
410
+ setHeadersObject['set-cookie'] = (setHeadersObject['set-cookie'] ||
411
+ []);
382
412
  for (const [name, value] of Object.entries(resCookies)) {
383
413
  if (name) {
384
414
  setHeadersObject['set-cookie'].push(`${name}=${value}`);
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 */
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); }); }
@@ -46,9 +48,12 @@ class CookiesIterable extends IterableRecords {
46
48
  next() {
47
49
  const str = this.cookies[this.index++];
48
50
  const ind = str ? str.indexOf('=') : 0;
49
- return this.index <= this.cookies.length ?
50
- { value: [str.slice(0, ind), str.slice(ind + 1)], done: false } :
51
- { value: undefined, done: true };
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 };
52
57
  }
53
58
  }
54
59
  class HeadersIterable extends IterableRecords {
@@ -57,9 +62,9 @@ class HeadersIterable extends IterableRecords {
57
62
  this.entries = Object.entries(headers);
58
63
  }
59
64
  next() {
60
- return this.index < this.entries.length ?
61
- { value: this.entries[this.index++], done: false } :
62
- { value: undefined, done: true };
65
+ return this.index < this.entries.length
66
+ ? { value: this.entries[this.index++], done: false }
67
+ : { value: undefined, done: true };
63
68
  }
64
69
  }
65
70
  function applyProxyControls(records, controls, additionalBlockers) {
@@ -69,8 +74,15 @@ function applyProxyControls(records, controls, additionalBlockers) {
69
74
  if (defaultedAllow) {
70
75
  for (const [name, value] of records) {
71
76
  const add = block !== '*' &&
72
- (!additionalBlockers || !additionalBlockers.includes(name)) && (defaultedAllow === '*' ||
73
- (defaultedAllow.find(item => typeof item === 'string' && name.toLowerCase() === item.toLowerCase() || item instanceof RegExp && item.test(name)))) && (!block || !block.find(item => typeof item === 'string' && name.toLowerCase() === item.toLowerCase() || item instanceof RegExp && item.test(name)));
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))));
74
86
  if (add) {
75
87
  result[name] = value;
76
88
  }
@@ -247,7 +259,12 @@ function createConsoleTransort(opts) {
247
259
  console.debug(formatted);
248
260
  break;
249
261
  case 6:
250
- console.trace(formatted);
262
+ if (opts === null || opts === void 0 ? void 0 : opts.trace) {
263
+ console.trace(formatted);
264
+ }
265
+ else {
266
+ console.debug(formatted);
267
+ }
251
268
  break;
252
269
  default:
253
270
  console.log(formatted);
@@ -337,13 +354,19 @@ function useProxy() {
337
354
  return __awaiter(this, void 0, void 0, function* () {
338
355
  const targetUrl = new URL(target);
339
356
  const path = targetUrl.pathname || '/';
340
- const url = new URL(path, targetUrl.origin).toString() + (targetUrl.search);
357
+ const url = new URL(path, targetUrl.origin).toString() + targetUrl.search;
341
358
  // preparing request headers and cookies
342
359
  const modifiedHeaders = Object.assign(Object.assign({}, req.headers), { host: targetUrl.hostname });
343
- const headers = (opts === null || opts === void 0 ? void 0 : opts.reqHeaders) ? applyProxyControls(new HeadersIterable(modifiedHeaders), opts === null || opts === void 0 ? void 0 : opts.reqHeaders, reqHeadersToBlock) : {};
344
- const cookies = (opts === null || opts === void 0 ? void 0 : opts.reqCookies) && req.headers.cookie ? applyProxyControls(new CookiesIterable(req.headers.cookie), opts === null || opts === void 0 ? void 0 : opts.reqCookies) : null;
360
+ const headers = (opts === null || opts === void 0 ? void 0 : opts.reqHeaders)
361
+ ? applyProxyControls(new HeadersIterable(modifiedHeaders), opts === null || opts === void 0 ? void 0 : opts.reqHeaders, reqHeadersToBlock)
362
+ : {};
363
+ const cookies = (opts === null || opts === void 0 ? void 0 : opts.reqCookies) && req.headers.cookie
364
+ ? applyProxyControls(new CookiesIterable(req.headers.cookie), opts === null || opts === void 0 ? void 0 : opts.reqCookies)
365
+ : null;
345
366
  if (cookies) {
346
- headers.cookie = Object.entries(cookies).map(v => v.join('=')).join('; ');
367
+ headers.cookie = Object.entries(cookies)
368
+ .map((v) => v.join('='))
369
+ .join('; ');
347
370
  }
348
371
  const method = (opts === null || opts === void 0 ? void 0 : opts.method) || req.method;
349
372
  // actual request
@@ -353,7 +376,9 @@ function useProxy() {
353
376
  }
354
377
  const resp = yield fetch(url, {
355
378
  method,
356
- body: ['GET', 'HEAD'].includes(method) ? undefined : req,
379
+ body: ['GET', 'HEAD'].includes(method)
380
+ ? undefined
381
+ : req,
357
382
  headers: headers,
358
383
  });
359
384
  // preparing response
@@ -363,8 +388,12 @@ function useProxy() {
363
388
  logger.info(`${''}response headers:${''}`);
364
389
  }
365
390
  // preparing response headers
366
- const resHeaders = (opts === null || opts === void 0 ? void 0 : opts.resHeaders) ? applyProxyControls(resp.headers.entries(), opts === null || opts === void 0 ? void 0 : opts.resHeaders, resHeadersToBlock) : null;
367
- const resCookies = (opts === null || opts === void 0 ? void 0 : opts.resCookies) ? applyProxyControls(new CookiesIterable(resp.headers.get('set-cookie') || ''), opts === null || opts === void 0 ? void 0 : opts.resCookies) : null;
391
+ const resHeaders = (opts === null || opts === void 0 ? void 0 : opts.resHeaders)
392
+ ? applyProxyControls(resp.headers.entries(), opts === null || opts === void 0 ? void 0 : opts.resHeaders, resHeadersToBlock)
393
+ : null;
394
+ const resCookies = (opts === null || opts === void 0 ? void 0 : opts.resCookies)
395
+ ? applyProxyControls(new CookiesIterable(resp.headers.get('set-cookie') || ''), opts === null || opts === void 0 ? void 0 : opts.resCookies)
396
+ : null;
368
397
  if (resHeaders) {
369
398
  for (const [name, value] of Object.entries(resHeaders)) {
370
399
  if (name) {
@@ -376,7 +405,8 @@ function useProxy() {
376
405
  }
377
406
  }
378
407
  if (resCookies) {
379
- setHeadersObject['set-cookie'] = (setHeadersObject['set-cookie'] || []);
408
+ setHeadersObject['set-cookie'] = (setHeadersObject['set-cookie'] ||
409
+ []);
380
410
  for (const [name, value] of Object.entries(resCookies)) {
381
411
  if (name) {
382
412
  setHeadersObject['set-cookie'].push(`${name}=${value}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wooksjs/http-proxy",
3
- "version": "0.2.18",
3
+ "version": "0.2.20",
4
4
  "description": "Proxy Wooks composable",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -31,7 +31,7 @@
31
31
  "url": "https://github.com/wooksjs/wooksjs/issues"
32
32
  },
33
33
  "peerDependencies": {
34
- "@wooksjs/event-http": "0.2.18"
34
+ "@wooksjs/event-http": "0.2.20"
35
35
  },
36
36
  "dependencies": {
37
37
  "node-fetch-native": "^1.0.1"