@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 +57 -95
- package/dist/index.d.ts +17 -18
- package/dist/index.mjs +57 -95
- package/package.json +11 -3
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 =
|
|
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
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
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(`${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
115
|
+
logger.info('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
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} ${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
127
|
+
logger.info(`${'[33m'}response headers:${'[39m'}`);
|
|
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${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
179
141
|
}
|
|
180
142
|
}
|
|
181
143
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
191
153
|
}
|
|
192
154
|
}
|
|
193
155
|
}
|
|
194
|
-
|
|
195
|
-
|
|
156
|
+
}
|
|
157
|
+
return resp;
|
|
196
158
|
};
|
|
197
159
|
}
|
|
198
160
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
overwrite?: Record<string, string> | ((data: Record<string, string>) => Record<string, string>);
|
|
3
|
-
allow?: (string | RegExp)[] | '*';
|
|
4
|
-
block?: (string | RegExp)[] | '*';
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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 =
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
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${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
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(`${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
113
|
+
logger.info('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
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} ${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
125
|
+
logger.info(`${'[33m'}response headers:${'[39m'}`);
|
|
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${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
177
139
|
}
|
|
178
140
|
}
|
|
179
141
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
189
151
|
}
|
|
190
152
|
}
|
|
191
153
|
}
|
|
192
|
-
|
|
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.
|
|
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.
|
|
35
|
-
"@wooksjs/event-core": "0.4.
|
|
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"
|