@wooksjs/http-proxy 0.6.6 → 0.7.1
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 +8 -86
- package/dist/index.cjs +7 -11
- package/dist/index.mjs +8 -12
- package/package.json +3 -5
package/README.md
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
**!!! This is work-in-progress library, breaking changes are expected !!!**
|
|
1
|
+
# @wooksjs/http-proxy
|
|
4
2
|
|
|
5
3
|
<p align="center">
|
|
6
4
|
<img src="../../wooks-logo.png" width="450px"><br>
|
|
@@ -9,94 +7,18 @@
|
|
|
9
7
|
</a>
|
|
10
8
|
</p>
|
|
11
9
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
🔥 An easy way to proxy request!
|
|
10
|
+
Composable HTTP proxy for [@wooksjs/event-http](https://github.com/wooksjs/wooksjs/tree/main/packages/event-http). Proxy requests with fine-grained control over headers and cookies filtering.
|
|
15
11
|
|
|
16
12
|
## Installation
|
|
17
13
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
## Usage
|
|
21
|
-
|
|
22
|
-
```ts
|
|
23
|
-
import { useProxy } from '@wooksjs/http-proxy'
|
|
24
|
-
app.get('/to-proxy', () => {
|
|
25
|
-
const proxy = useProxy()
|
|
26
|
-
return proxy('https://target-website.com/target-path?query=123')
|
|
27
|
-
})
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
### Restrict cookies/headers to pass
|
|
31
|
-
|
|
32
|
-
```ts
|
|
33
|
-
import { useProxy } from '@wooksjs/http-proxy'
|
|
34
|
-
app.get('/to-proxy', () => {
|
|
35
|
-
const proxy = useProxy()
|
|
36
|
-
return proxy('https://target-website.com/target-path?query=123', {
|
|
37
|
-
reqHeaders: { block: ['referer'] }, // block referer header
|
|
38
|
-
reqCookies: { block: '*' }, // block all req cookies
|
|
39
|
-
})
|
|
40
|
-
})
|
|
14
|
+
```sh
|
|
15
|
+
npm install @wooksjs/http-proxy
|
|
41
16
|
```
|
|
42
17
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
It's easy as `proxy` returns fetch response
|
|
46
|
-
|
|
47
|
-
```ts
|
|
48
|
-
import { useProxy } from '@wooksjs/http-proxy'
|
|
49
|
-
app.get('/to-proxy', async () => {
|
|
50
|
-
const proxy = useProxy()
|
|
51
|
-
const response = await proxy('https://mayapi.com/json-api')
|
|
52
|
-
const data = { ...(await response.json()), newField: 'new value' }
|
|
53
|
-
return data
|
|
54
|
-
})
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
## Proxy advanced options
|
|
58
|
-
|
|
59
|
-
```ts
|
|
60
|
-
import { useProxy } from '@wooksjs/http-proxy'
|
|
61
|
-
import { useRequest } from '@wooksjs/event-http'
|
|
62
|
-
//...
|
|
63
|
-
app.get('*', async () => {
|
|
64
|
-
const proxy = useProxy()
|
|
65
|
-
const { url } = useRequest()
|
|
66
|
-
const fetchResponse = await proxy('https://www.google.com' + url, {
|
|
67
|
-
// optional method, be default is set with
|
|
68
|
-
// the original request method
|
|
69
|
-
method: 'GET',
|
|
70
|
-
|
|
71
|
-
// the next four options help to filter out
|
|
72
|
-
// request/response headers/cookies
|
|
73
|
-
// each of the option accepts an object with:
|
|
74
|
-
// - allow: '*' | (string | RegExp)[] - a list to allow (default '*')
|
|
75
|
-
// - block: '*' | (string | RegExp)[] - a list to block
|
|
76
|
-
// - overwrite: Record<string| string> | ((data: object) -> object) - object or fn to overwrite data
|
|
77
|
-
reqHeaders: { block: ['referer'] },
|
|
78
|
-
reqCookies: { allow: ['cookie-to-pass-upstream'] },
|
|
79
|
-
resHeaders: { overwrite: { 'x-proxied-by': 'wooks-proxy' } },
|
|
80
|
-
resCookies: { allow: ['cookie-to-pass-downstream'] },
|
|
81
|
-
|
|
82
|
-
// debug: true - will print proxy paths and headers/cookies
|
|
83
|
-
debug: true,
|
|
84
|
-
})
|
|
85
|
-
return fetchResponse // fetch response is supported, the body will be downstreamed
|
|
86
|
-
|
|
87
|
-
// > you can also return fully buffered body as Uint8Array
|
|
88
|
-
// return new Uint8Array(await fetchResponse.arrayBuffer())
|
|
89
|
-
|
|
90
|
-
// > or as string
|
|
91
|
-
// return fetchResponse.text()
|
|
18
|
+
## Documentation
|
|
92
19
|
|
|
93
|
-
|
|
94
|
-
// const data = await fetchResponse.text() + '<new data>'
|
|
95
|
-
// return data
|
|
96
|
-
})
|
|
97
|
-
//...
|
|
98
|
-
```
|
|
20
|
+
For full documentation, visit [wooks.moost.org/webapp/proxy](https://wooks.moost.org/webapp/proxy).
|
|
99
21
|
|
|
100
|
-
##
|
|
22
|
+
## License
|
|
101
23
|
|
|
102
|
-
|
|
24
|
+
MIT
|
package/dist/index.cjs
CHANGED
|
@@ -22,7 +22,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
22
22
|
|
|
23
23
|
//#endregion
|
|
24
24
|
const node_url = __toESM(require("node:url"));
|
|
25
|
-
const __wooksjs_event_core = __toESM(require("@wooksjs/event-core"));
|
|
26
25
|
const __wooksjs_event_http = __toESM(require("@wooksjs/event-http"));
|
|
27
26
|
|
|
28
27
|
//#region packages/http-proxy/src/proxy-utils.ts
|
|
@@ -121,12 +120,10 @@ const resHeadersToBlock = [
|
|
|
121
120
|
* @returns An async `proxy(target, opts?)` function that forwards the current request to the given target URL.
|
|
122
121
|
*/
|
|
123
122
|
function useProxy() {
|
|
124
|
-
const
|
|
125
|
-
const {
|
|
126
|
-
const
|
|
127
|
-
const
|
|
128
|
-
const logger = (0, __wooksjs_event_core.useEventLogger)("http-proxy");
|
|
129
|
-
const setHeadersObject = getSetHeaders();
|
|
123
|
+
const response = (0, __wooksjs_event_http.useResponse)();
|
|
124
|
+
const { raw } = (0, __wooksjs_event_http.useRequest)();
|
|
125
|
+
const req = raw;
|
|
126
|
+
const logger = (0, __wooksjs_event_http.useLogger)();
|
|
130
127
|
return async function proxy(target, opts) {
|
|
131
128
|
const targetUrl = new node_url.URL(target);
|
|
132
129
|
const path = targetUrl.pathname || "/";
|
|
@@ -148,7 +145,7 @@ function useProxy() {
|
|
|
148
145
|
body: ["GET", "HEAD"].includes(method) ? void 0 : req,
|
|
149
146
|
headers
|
|
150
147
|
});
|
|
151
|
-
status
|
|
148
|
+
response.status = resp.status;
|
|
152
149
|
if (opts?.debug) {
|
|
153
150
|
logger.info(`${resp.status} [32m${req.method} ${req.url}[33m → [36m${method} ${url}[33m`);
|
|
154
151
|
logger.info(`[33mresponse headers:[39m`);
|
|
@@ -157,14 +154,13 @@ function useProxy() {
|
|
|
157
154
|
const resCookies = opts?.resCookies ? applyProxyControls(new CookiesIterable(resp.headers.get(SET_COOKIE) || ""), opts.resCookies) : null;
|
|
158
155
|
if (resHeaders) {
|
|
159
156
|
for (const [name, value] of Object.entries(resHeaders)) if (name) {
|
|
160
|
-
setHeader(name, value);
|
|
157
|
+
response.setHeader(name, value);
|
|
161
158
|
if (opts?.debug) logger.info(`\t[33m${name}=[32m${value}[39m`);
|
|
162
159
|
}
|
|
163
160
|
}
|
|
164
161
|
if (resCookies) {
|
|
165
|
-
setHeadersObject[SET_COOKIE] = setHeadersObject[SET_COOKIE] || [];
|
|
166
162
|
for (const [name, value] of Object.entries(resCookies)) if (name) {
|
|
167
|
-
|
|
163
|
+
response.setCookieRaw(`${name}=${value}`);
|
|
168
164
|
if (opts?.debug) logger.info(`\t[1m[33m${SET_COOKIE}=[32m${name}=${value}[0m`);
|
|
169
165
|
}
|
|
170
166
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { URL } from "node:url";
|
|
2
|
-
import {
|
|
3
|
-
import { useHttpContext, useSetHeaders, useStatus } from "@wooksjs/event-http";
|
|
2
|
+
import { useLogger, useRequest, useResponse } from "@wooksjs/event-http";
|
|
4
3
|
|
|
5
4
|
//#region packages/http-proxy/src/proxy-utils.ts
|
|
6
5
|
var IterableRecords = class {
|
|
@@ -98,12 +97,10 @@ const resHeadersToBlock = [
|
|
|
98
97
|
* @returns An async `proxy(target, opts?)` function that forwards the current request to the given target URL.
|
|
99
98
|
*/
|
|
100
99
|
function useProxy() {
|
|
101
|
-
const
|
|
102
|
-
const {
|
|
103
|
-
const
|
|
104
|
-
const
|
|
105
|
-
const logger = useEventLogger("http-proxy");
|
|
106
|
-
const setHeadersObject = getSetHeaders();
|
|
100
|
+
const response = useResponse();
|
|
101
|
+
const { raw } = useRequest();
|
|
102
|
+
const req = raw;
|
|
103
|
+
const logger = useLogger();
|
|
107
104
|
return async function proxy(target, opts) {
|
|
108
105
|
const targetUrl = new URL(target);
|
|
109
106
|
const path = targetUrl.pathname || "/";
|
|
@@ -125,7 +122,7 @@ function useProxy() {
|
|
|
125
122
|
body: ["GET", "HEAD"].includes(method) ? void 0 : req,
|
|
126
123
|
headers
|
|
127
124
|
});
|
|
128
|
-
status
|
|
125
|
+
response.status = resp.status;
|
|
129
126
|
if (opts?.debug) {
|
|
130
127
|
logger.info(`${resp.status} [32m${req.method} ${req.url}[33m → [36m${method} ${url}[33m`);
|
|
131
128
|
logger.info(`[33mresponse headers:[39m`);
|
|
@@ -134,14 +131,13 @@ function useProxy() {
|
|
|
134
131
|
const resCookies = opts?.resCookies ? applyProxyControls(new CookiesIterable(resp.headers.get(SET_COOKIE) || ""), opts.resCookies) : null;
|
|
135
132
|
if (resHeaders) {
|
|
136
133
|
for (const [name, value] of Object.entries(resHeaders)) if (name) {
|
|
137
|
-
setHeader(name, value);
|
|
134
|
+
response.setHeader(name, value);
|
|
138
135
|
if (opts?.debug) logger.info(`\t[33m${name}=[32m${value}[39m`);
|
|
139
136
|
}
|
|
140
137
|
}
|
|
141
138
|
if (resCookies) {
|
|
142
|
-
setHeadersObject[SET_COOKIE] = setHeadersObject[SET_COOKIE] || [];
|
|
143
139
|
for (const [name, value] of Object.entries(resCookies)) if (name) {
|
|
144
|
-
|
|
140
|
+
response.setCookieRaw(`${name}=${value}`);
|
|
145
141
|
if (opts?.debug) logger.info(`\t[1m[33m${SET_COOKIE}=[32m${name}=${value}[0m`);
|
|
146
142
|
}
|
|
147
143
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wooksjs/http-proxy",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "Proxy Wooks composable",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -42,12 +42,10 @@
|
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"typescript": "^5.9.3",
|
|
44
44
|
"vitest": "^3.2.4",
|
|
45
|
-
"@wooksjs/event-
|
|
46
|
-
"@wooksjs/event-http": "^0.6.6"
|
|
45
|
+
"@wooksjs/event-http": "^0.7.1"
|
|
47
46
|
},
|
|
48
47
|
"peerDependencies": {
|
|
49
|
-
"@wooksjs/event-
|
|
50
|
-
"@wooksjs/event-http": "^0.6.6"
|
|
48
|
+
"@wooksjs/event-http": "^0.7.1"
|
|
51
49
|
},
|
|
52
50
|
"scripts": {
|
|
53
51
|
"build": "rolldown -c ../../rolldown.config.mjs"
|