Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate from http-proxy to unjs/httpxy #1623

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/3.config.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ You can use this option to override development server routes and proxy-pass req
}
```

See [https://github.com/http-party/node-http-proxy#options](https://github.com/http-party/node-http-proxy#options) for all available target options.
See [https://github.com/unjs/httpxy](https://github.com/unjs/httpxy) for all available target options.
pi0 marked this conversation as resolved.
Show resolved Hide resolved

### `errorHandler`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"gzip-size": "^7.0.0",
"h3": "^1.8.0",
"hookable": "^5.5.3",
"http-proxy": "^1.18.1",
"httpxy": "^0.1.1",
"is-primitive": "^3.0.1",
"jiti": "^1.19.3",
"klona": "^2.0.6",
Expand Down
44 changes: 12 additions & 32 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 11 additions & 17 deletions src/dev/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
H3Event,
toNodeListener,
} from "h3";
import httpProxy, { ServerOptions as HTTPProxyOptions } from "http-proxy";
import { createProxyServer, ProxyServerOptions } from "httpxy";
import { listen, Listener, ListenOptions } from "listhen";
import { servePlaceholder } from "serve-placeholder";
import serveStatic from "serve-static";
Expand Down Expand Up @@ -254,22 +254,16 @@ export function createDevServer(nitro: Nitro): NitroDevServer {
};
}

function createProxy(defaults: HTTPProxyOptions = {}) {
const proxy = httpProxy.createProxy();
const handle = (event: H3Event, opts: HTTPProxyOptions = {}) => {
return new Promise<void>((resolve, reject) => {
proxy.web(
event.node.req,
event.node.res,
{ ...defaults, ...opts },
(error: any) => {
if (error.code !== "ECONNRESET") {
reject(error);
}
resolve();
}
);
});
function createProxy(defaults: ProxyServerOptions = {}) {
const proxy = createProxyServer({});
const handle = async (event: H3Event, opts: ProxyServerOptions = {}) => {
try {
await proxy.web(event.node.req, event.node.res, { ...defaults, ...opts });
} catch (error) {
if (error.code !== "ECONNRESET") {
throw error;
}
}
};
return {
proxy,
Expand Down
4 changes: 2 additions & 2 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { WatchOptions } from "chokidar";
import type { RollupCommonJSOptions } from "@rollup/plugin-commonjs";
import type { RollupWasmOptions } from "@rollup/plugin-wasm";
import type { Storage, BuiltinDriverName } from "unstorage";
import type { ServerOptions as HTTPProxyOptions } from "http-proxy";
import type { ProxyServerOptions } from "httpxy";
import type { ProxyOptions } from "h3";
import type { ResolvedConfig, ConfigWatcher } from "c12";
import type { TSConfig } from "pkg-types";
Expand Down Expand Up @@ -273,7 +273,7 @@ export interface NitroOptions extends PresetOptions {
dev: boolean;
devServer: DevServerOptions;
watchOptions: WatchOptions;
devProxy: Record<string, string | HTTPProxyOptions>;
devProxy: Record<string, string | ProxyServerOptions>;

// Routing
baseURL: string;
Expand Down
Loading