unjs / httpxy Goto Github PK
View Code? Open in Web Editor NEW๐ A Full-Featured HTTP and WebSocket Proxy for Node.js
License: Other
๐ A Full-Featured HTTP and WebSocket Proxy for Node.js
License: Other
httpxy 0.1.5
node 20 (but doesn't really matter)
not needed, I think
If the server responds with a relativ URL in the Location header (e.g. in case of 201 Created), it causes an "Uncaught TypeError: Invalid URL" error, since there is no second parameter to the URL constructor in:
httpxy/src/middleware/web-outgoing.ts
Line 38 in 1f62fff
For example:
HTTP/1.1 201 Created
Location: /api/books/1
The Location response header is allowed to contain a relativ URL, according to: https://www.rfc-editor.org/rfc/rfc9110#field.location
The URL constructor needs a base
argument in that case: https://nodejs.org/api/url.html#new-urlinput-base
No response
import { createServer } from "node:http";
import { createProxyServer } from "httpxy";
const proxy = createProxyServer({});
const server = createServer(async (req, res) => {
try {
await proxy.web(req, res, {
target: main.url,
});
} catch (error) {
console.error(error);
res.statusCode = 500;
res.end("Proxy error: " + error.toString());
}
});
server.listen(3000, () => {
console.log("Proxy is listening on http://localhost:3000");
});
The above code runs with an error
No response
There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.
Error type: Cannot find preset's package (github>unjs/renovate-config)
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
@vitest/coverage-v8
, vitest
).github/workflows/autofix.yml
actions/checkout v4
actions/setup-node v3
autofix-ci/action ea32e3a12414e6d3183163c3424a7d7a8631ad84
.github/workflows/ci.yml
actions/checkout v4
actions/setup-node v3
codecov/codecov-action v3
package.json
@types/node ^20.11.25
@vitest/coverage-v8 ^0.34.6
changelogen ^0.5.5
eslint ^8.57.0
eslint-config-unjs ^0.2.1
jiti ^1.21.0
listhen ^1.7.2
ofetch ^1.3.3
prettier ^3.2.5
typescript ^5.4.2
unbuild ^2.0.0
vitest ^0.34.6
pnpm 8.15.4
Use the official tutorial
import { createServer } from "node:http";
import { createProxyServer } from "httpxy";
const proxy = createProxyServer({});
const server = createServer(async (req, res) => {
try {
await proxy.web(req, res, {
target: main.url,
});
} catch (error) {
console.error(error);
res.statusCode = 500;
res.end("Proxy error: " + error.toString());
}
});
server.listen(3000, () => {
console.log("Proxy is listening on http://localhost:3000");
});
The above code runs with an error
No response
No response
No response
It could be a very nice feature to be able to rewrite request and response headers/body.
The goal would be to have a full-featured MITM proxy with programmatic rules that trigger based on patterns
e.g. on hostname my-domain.com, run the provided function
Something like Charles Proxy or Burp Proxy, but with a modern syntax and access to the wide JavaScript dependencies ecosystem
TLS encryption-decryption should be implemented so that we can MITM HTTPS connections.
What do you think?
Hi,
It would be really nice if we could proxy to a UNIX socket :-)
Thanks in advance!
I would happily help implement this feature but can't do it myself right now due to time constraints (I already helped implement a similar feature in listhen)
Package version 0.1.2
Node.js 18.16.0
I initially reproduced the error by using nitro and configuring the devProxy
like so:
devProxy: {
"/posts": {
target: "https://jsonplaceholder.typicode.com/posts",
changeOrigin: true,
prependPath: true,
},
},
It turned out to be a bug with httpxy instead, but I figured the reproduction would be obvious enough using the nitro playground with the above configuration rather than forking the repo.
Using the provided configuration, I assume the expected behavior would be to proxy a request to the /posts
path to https://jsonplaceholder.typicode.com/posts
, but it seems to be forwarded to just https://jsonplaceholder.typicode.com instead. I (supposedly) fixed the bug by changing the property used when determining the target path from path
to pathname
as per the URL class API documentation. I'll submit a PR shortly.
Another issue reporting the bug: unjs/nitro#1643
No response
Darwin
v20.5.1
3.7.0
3.7.2
2.6.2
[email protected]
-
-
-
-
I confess I haven't made one up-front. I think the explanation below might be enough, but if not I can try to figure out a reproduction.
When making a DELETE request, the request has a "Content-Length: 0" header added to it, which causes Node's undici to respond with the following error:
TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async sendProxy (file:///myprojectpath/node_modules/h3/dist/index.mjs:1049:20)
at async Object.handler (file:///myprojectpath/node_modules/h3/dist/index.mjs:1646:19)
at async Server.toNodeHandle (file:///myprojectpath/node_modules/h3/dist/index.mjs:1857:7) {
cause: RequestContentLengthMismatchError: Request body length does not match content-length header
at write (node:internal/deps/undici/undici:10059:41)
at _resume (node:internal/deps/undici/undici:10037:33)
at resume (node:internal/deps/undici/undici:9938:7)
at [dispatch] (node:internal/deps/undici/undici:9286:11)
at Client.Intercept (node:internal/deps/undici/undici:9017:20)
at Client.dispatch (node:internal/deps/undici/undici:7772:44)
at [dispatch] (node:internal/deps/undici/undici:7991:32)
at Pool.dispatch (node:internal/deps/undici/undici:7772:44)
at [dispatch] (node:internal/deps/undici/undici:10556:27)
at Agent.Intercept (node:internal/deps/undici/undici:9017:20) {
code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
}
More context has been provided here:
nodejs/undici#2046 (comment)
Here's the same code in this fork:
httpxy/src/middleware/web-incoming.ts
Line 10 in f8cde14
This is all somewhat beyond me, but I would think it's not the responsibility of the proxy to be adding HTTP headers that weren't provided in the initial request, unless it's intentional behaviour written in middleware or proxy route rules.
I appreciate that Node may end up fixing their underlying behaviour to allow DELETE requests with a Content-Length, but that may not solve the problem for people proxying to other non-JS back-ends.
Similarly, I have read in the linked undici issue that Safari does send Content-Length: 0 with DELETE requests, so it would be useful to know how I can filter those out. I am using extendRouteRules
in a Nuxt module to register my proxy rules, and am doing so as a wildcard for a whole path prefix. Is that currently possible?
No response
No response
With https://citty.unjs.io and https://listhen.unjs.io we can provide a nice CLI to easily create local proxy servers. Could be useful to start a local HTTPs server for example. (like #19)
Initially disabled to make refactor faster. It should be straight forward now if someone wants to pick this.
Upstream has so many open PRs and Issues potentially some are valid bugs. Need to investigate and make a quick list of them:
Support for HTTP2/3 in order to be a replacement for Vite dev-server and other build tools.
Upstream: http-party/node-http-proxy#1237
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.