@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 +61 -95
- package/dist/index.mjs +61 -95
- package/package.json +3 -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,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
|
-
|
|
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
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
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
|
+
// 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(`${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
117
|
+
logger.info('[33m' + 'headers:', JSON.stringify(headers, null, ' '), '[39m');
|
|
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} ${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
130
|
+
logger.info(`${'[33m'}response headers:${'[39m'}`);
|
|
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${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
179
145
|
}
|
|
180
146
|
}
|
|
181
147
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
191
157
|
}
|
|
192
158
|
}
|
|
193
159
|
}
|
|
194
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
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
|
+
// 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(`${'[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 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} ${'[32m'}${req.method} ${req.url}${'[33m'} → ${'[36m'}${method} ${url}${'[33m'}`);
|
|
128
|
+
logger.info(`${'[33m'}response headers:${'[39m'}`);
|
|
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${'[33m'}${name}=${'[32m'}${value}${'[39m'}`);
|
|
177
143
|
}
|
|
178
144
|
}
|
|
179
145
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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${'[1m'}${'[33m'}set-cookie=${'[32m'}${name}=${value}${'[0m'}`);
|
|
189
155
|
}
|
|
190
156
|
}
|
|
191
157
|
}
|
|
192
|
-
|
|
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.
|
|
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.
|
|
35
|
-
"@wooksjs/event-core": "0.4.
|
|
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"
|