@wooksjs/http-proxy 0.4.10 → 0.4.12

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,68 @@ 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
+ const modifiedHeaders = { ...req.headers, host: targetUrl.hostname };
101
+ const headers = opts?.reqHeaders
102
+ ? applyProxyControls(new HeadersIterable(modifiedHeaders), opts?.reqHeaders, reqHeadersToBlock)
103
+ : {};
104
+ const cookies = opts?.reqCookies && req.headers.cookie
105
+ ? applyProxyControls(new CookiesIterable(req.headers.cookie), opts?.reqCookies)
106
+ : null;
107
+ if (cookies) {
108
+ headers.cookie = Object.entries(cookies)
109
+ .map((v) => v.join('='))
110
+ .join('; ');
111
+ }
112
+ const method = opts?.method || req.method;
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 nodeFetchNative.fetch(url, {
118
+ method,
119
+ body: ['GET', 'HEAD'].includes(method)
120
+ ? undefined
121
+ : req,
122
+ headers: headers,
123
+ });
124
+ status.value = resp.status;
125
+ if (opts?.debug) {
126
+ logger.info(`${resp.status} ${''}${req.method} ${req.url}${''} → ${''}${method} ${url}${''}`);
127
+ logger.info(`${''}response headers:${''}`);
128
+ }
129
+ const resHeaders = opts?.resHeaders
130
+ ? applyProxyControls(resp.headers.entries(), opts?.resHeaders, resHeadersToBlock)
131
+ : null;
132
+ const resCookies = opts?.resCookies
133
+ ? applyProxyControls(new CookiesIterable(resp.headers.get('set-cookie') || ''), opts?.resCookies)
134
+ : null;
135
+ if (resHeaders) {
136
+ for (const [name, value] of Object.entries(resHeaders)) {
137
+ if (name) {
138
+ setHeader(name, value);
139
+ if (opts?.debug) {
140
+ logger.info(`\t${''}${name}=${''}${value}${''}`);
179
141
  }
180
142
  }
181
143
  }
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
- }
144
+ }
145
+ if (resCookies) {
146
+ setHeadersObject['set-cookie'] = (setHeadersObject['set-cookie'] ||
147
+ []);
148
+ for (const [name, value] of Object.entries(resCookies)) {
149
+ if (name) {
150
+ setHeadersObject['set-cookie'].push(`${name}=${value}`);
151
+ if (opts?.debug) {
152
+ logger.info(`\t${''}${''}set-cookie=${''}${name}=${value}${''}`);
191
153
  }
192
154
  }
193
155
  }
194
- return resp;
195
- });
156
+ }
157
+ return resp;
196
158
  };
197
159
  }
198
160
 
package/dist/index.d.ts CHANGED
@@ -1,18 +1,17 @@
1
- export declare interface TWooksProxyControls {
2
- overwrite?: Record<string, string> | ((data: Record<string, string>) => Record<string, string>);
3
- allow?: (string | RegExp)[] | '*';
4
- block?: (string | RegExp)[] | '*';
5
- }
6
-
7
- export declare interface TWooksProxyOptions {
8
- method?: string;
9
- reqHeaders?: TWooksProxyControls;
10
- reqCookies?: TWooksProxyControls;
11
- resHeaders?: TWooksProxyControls;
12
- resCookies?: TWooksProxyControls;
13
- debug?: boolean;
14
- }
15
-
16
- export declare function useProxy(): (target: string, opts?: TWooksProxyOptions) => Promise<Response>;
17
-
18
- export { }
1
+ interface TWooksProxyControls {
2
+ overwrite?: Record<string, string> | ((data: Record<string, string>) => Record<string, string>);
3
+ allow?: (string | RegExp)[] | '*';
4
+ block?: (string | RegExp)[] | '*';
5
+ }
6
+ interface TWooksProxyOptions {
7
+ method?: string;
8
+ reqHeaders?: TWooksProxyControls;
9
+ reqCookies?: TWooksProxyControls;
10
+ resHeaders?: TWooksProxyControls;
11
+ resCookies?: TWooksProxyControls;
12
+ debug?: boolean;
13
+ }
14
+
15
+ declare function useProxy(): (target: string, opts?: TWooksProxyOptions) => Promise<Response>;
16
+
17
+ export { type TWooksProxyControls, type TWooksProxyOptions, useProxy };
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,68 @@ 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
+ const modifiedHeaders = { ...req.headers, host: targetUrl.hostname };
99
+ const headers = opts?.reqHeaders
100
+ ? applyProxyControls(new HeadersIterable(modifiedHeaders), opts?.reqHeaders, reqHeadersToBlock)
101
+ : {};
102
+ const cookies = opts?.reqCookies && req.headers.cookie
103
+ ? applyProxyControls(new CookiesIterable(req.headers.cookie), opts?.reqCookies)
104
+ : null;
105
+ if (cookies) {
106
+ headers.cookie = Object.entries(cookies)
107
+ .map((v) => v.join('='))
108
+ .join('; ');
109
+ }
110
+ const method = opts?.method || req.method;
111
+ if (opts?.debug) {
112
+ logger.info(`${''}${req.method} ${req.url}${''} → ${''}${method} ${url}${''}`);
113
+ logger.info('' + 'headers:', JSON.stringify(headers, null, ' '), '');
114
+ }
115
+ const resp = await fetch(url, {
116
+ method,
117
+ body: ['GET', 'HEAD'].includes(method)
118
+ ? undefined
119
+ : req,
120
+ headers: headers,
121
+ });
122
+ status.value = resp.status;
123
+ if (opts?.debug) {
124
+ logger.info(`${resp.status} ${''}${req.method} ${req.url}${''} → ${''}${method} ${url}${''}`);
125
+ logger.info(`${''}response headers:${''}`);
126
+ }
127
+ const resHeaders = opts?.resHeaders
128
+ ? applyProxyControls(resp.headers.entries(), opts?.resHeaders, resHeadersToBlock)
129
+ : null;
130
+ const resCookies = opts?.resCookies
131
+ ? applyProxyControls(new CookiesIterable(resp.headers.get('set-cookie') || ''), opts?.resCookies)
132
+ : null;
133
+ if (resHeaders) {
134
+ for (const [name, value] of Object.entries(resHeaders)) {
135
+ if (name) {
136
+ setHeader(name, value);
137
+ if (opts?.debug) {
138
+ logger.info(`\t${''}${name}=${''}${value}${''}`);
177
139
  }
178
140
  }
179
141
  }
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
- }
142
+ }
143
+ if (resCookies) {
144
+ setHeadersObject['set-cookie'] = (setHeadersObject['set-cookie'] ||
145
+ []);
146
+ for (const [name, value] of Object.entries(resCookies)) {
147
+ if (name) {
148
+ setHeadersObject['set-cookie'].push(`${name}=${value}`);
149
+ if (opts?.debug) {
150
+ logger.info(`\t${''}${''}set-cookie=${''}${name}=${value}${''}`);
189
151
  }
190
152
  }
191
153
  }
192
- return resp;
193
- });
154
+ }
155
+ return resp;
194
156
  };
195
157
  }
196
158
 
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.12",
4
4
  "description": "Proxy Wooks composable",
5
5
  "main": "dist/index.cjs",
6
6
  "module": "dist/index.mjs",
@@ -8,6 +8,14 @@
8
8
  "files": [
9
9
  "dist"
10
10
  ],
11
+ "exports": {
12
+ "./package.json": "./package.json",
13
+ ".": {
14
+ "require": "./dist/index.cjs",
15
+ "import": "./dist/index.mjs",
16
+ "types": "./dist/index.d.ts"
17
+ }
18
+ },
11
19
  "repository": {
12
20
  "type": "git",
13
21
  "url": "git+https://github.com/wooksjs/wooksjs.git",
@@ -31,8 +39,8 @@
31
39
  "url": "https://github.com/wooksjs/wooksjs/issues"
32
40
  },
33
41
  "peerDependencies": {
34
- "@wooksjs/event-http": "0.4.10",
35
- "@wooksjs/event-core": "0.4.10"
42
+ "@wooksjs/event-http": "0.4.12",
43
+ "@wooksjs/event-core": "0.4.12"
36
44
  },
37
45
  "dependencies": {
38
46
  "node-fetch-native": "^1.0.1"