From 5d16ac47e966bc9d21b0b2f07fb629c339c27c00 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 23 Aug 2023 03:20:11 +0200 Subject: [PATCH 1/4] refactor: migrate from `http-proxy` to `unjs/httpxy` --- docs/content/3.config.md | 2 +- package.json | 2 +- pnpm-lock.yaml | 44 +++++++++++----------------------------- src/dev/server.ts | 28 ++++++++++--------------- src/types/nitro.ts | 4 ++-- 5 files changed, 27 insertions(+), 53 deletions(-) diff --git a/docs/content/3.config.md b/docs/content/3.config.md index ba5bc5ae0e..a60973640a 100644 --- a/docs/content/3.config.md +++ b/docs/content/3.config.md @@ -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. ### `errorHandler` diff --git a/package.json b/package.json index 81227960d8..ed10414ddf 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4f6e27ddd2..f36bca8711 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -105,9 +105,9 @@ importers: hookable: specifier: ^5.5.3 version: 5.5.3 - http-proxy: - specifier: ^1.18.1 - version: 1.18.1 + httpxy: + specifier: ^0.1.1 + version: 0.1.1 is-primitive: specifier: ^3.0.1 version: 3.0.1 @@ -1537,6 +1537,7 @@ packages: dependencies: is-glob: 4.0.3 micromatch: 4.0.5 + napi-wasm: 1.1.0 dev: false bundledDependencies: - napi-wasm @@ -3725,10 +3726,6 @@ packages: dev: true optional: true - /eventemitter3@4.0.7: - resolution: {integrity: sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==} - dev: false - /execa@5.1.1: resolution: {integrity: sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==} engines: {node: '>=10'} @@ -3990,16 +3987,6 @@ packages: resolution: {integrity: sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==} dev: true - /follow-redirects@1.15.2: - resolution: {integrity: sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==} - engines: {node: '>=4.0'} - peerDependencies: - debug: '*' - peerDependenciesMeta: - debug: - optional: true - dev: false - /for-each@0.3.3: resolution: {integrity: sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==} dependencies: @@ -4452,17 +4439,6 @@ packages: dev: true optional: true - /http-proxy@1.18.1: - resolution: {integrity: sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==} - engines: {node: '>=8.0.0'} - dependencies: - eventemitter3: 4.0.7 - follow-redirects: 1.15.2 - requires-port: 1.0.0 - transitivePeerDependencies: - - debug - dev: false - /http-shutdown@1.2.2: resolution: {integrity: sha512-S9wWkJ/VSY9/k4qcjG318bqJNruzE4HySUhFYknwmu6LBP97KLLfwNf+n4V1BHurvFNkSKLFnK/RsuUnRTf9Vw==} engines: {iojs: '>= 1.0.0', node: '>= 0.12.0'} @@ -4477,6 +4453,10 @@ packages: transitivePeerDependencies: - supports-color + /httpxy@0.1.1: + resolution: {integrity: sha512-dYx6Onf6/BrzaHhUKo02IrzWYskol6Atj2GtDOsch4jKk2mmP4N3NuX0VhJ8bnNNFSyMgb1sBgR25fCAxTh5Gg==} + dev: false + /human-signals@2.1.0: resolution: {integrity: sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==} engines: {node: '>=10.17.0'} @@ -5440,6 +5420,10 @@ packages: hasBin: true dev: true + /napi-wasm@1.1.0: + resolution: {integrity: sha512-lHwIAJbmLSjF9VDRm9GoVOy9AGp3aIvkjv+Kvz9h16QR3uSVYH78PNQUnT2U4X53mhlnV2M7wrhibQ3GHicDmg==} + dev: false + /natural-compare-lite@1.4.0: resolution: {integrity: sha512-Tj+HTDSJJKaZnfiuw+iaF9skdPpTo2GtEly5JHnWV/hfv2Qj/9RKsGISQtLh2ox3l5EAGw487hnBee0sIJ6v2g==} dev: true @@ -6094,10 +6078,6 @@ packages: resolution: {integrity: sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==} engines: {node: '>=0.10.0'} - /requires-port@1.0.0: - resolution: {integrity: sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==} - dev: false - /requizzle@0.2.4: resolution: {integrity: sha512-JRrFk1D4OQ4SqovXOgdav+K8EAhSB/LJZqCz8tbX0KObcdeM15Ss59ozWMBWmmINMagCwmqn4ZNryUGpBsl6Jw==} requiresBuild: true diff --git a/src/dev/server.ts b/src/dev/server.ts index 667d762097..9ae3e71c6b 100644 --- a/src/dev/server.ts +++ b/src/dev/server.ts @@ -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"; @@ -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((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, diff --git a/src/types/nitro.ts b/src/types/nitro.ts index ea5bfdf568..6055490476 100644 --- a/src/types/nitro.ts +++ b/src/types/nitro.ts @@ -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"; @@ -273,7 +273,7 @@ export interface NitroOptions extends PresetOptions { dev: boolean; devServer: DevServerOptions; watchOptions: WatchOptions; - devProxy: Record; + devProxy: Record; // Routing baseURL: string; From c9350570fa65dd49c034db4ae7298e55cd7b26d3 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Wed, 23 Aug 2023 03:21:38 +0200 Subject: [PATCH 2/4] Update docs/content/3.config.md --- docs/content/3.config.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/3.config.md b/docs/content/3.config.md index a60973640a..a78f3f8267 100644 --- a/docs/content/3.config.md +++ b/docs/content/3.config.md @@ -282,7 +282,7 @@ You can use this option to override development server routes and proxy-pass req } ``` -See [https://github.com/unjs/httpxy](https://github.com/unjs/httpxy) for all available target options. +See [unjs/httpxy](https://github.com/unjs/httpxy) for all available target options. ### `errorHandler` From 4ef8303f5c4e568f758126fa0ec8236c3d158f3b Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 23 Aug 2023 03:24:02 +0200 Subject: [PATCH 3/4] pass defaults to main proxy --- src/dev/server.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dev/server.ts b/src/dev/server.ts index 9ae3e71c6b..d81a89fe7a 100644 --- a/src/dev/server.ts +++ b/src/dev/server.ts @@ -255,10 +255,10 @@ export function createDevServer(nitro: Nitro): NitroDevServer { } function createProxy(defaults: ProxyServerOptions = {}) { - const proxy = createProxyServer({}); + const proxy = createProxyServer(defaults); const handle = async (event: H3Event, opts: ProxyServerOptions = {}) => { try { - await proxy.web(event.node.req, event.node.res, { ...defaults, ...opts }); + await proxy.web(event.node.req, event.node.res, opts); } catch (error) { if (error.code !== "ECONNRESET") { throw error; From b4dfdf7697fb0ef79812e8129e3d8b52800b775d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 23 Aug 2023 03:31:12 +0200 Subject: [PATCH 4/4] update httpxy --- package.json | 2 +- pnpm-lock.yaml | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index ed10414ddf..b6154db107 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,7 @@ "gzip-size": "^7.0.0", "h3": "^1.8.0", "hookable": "^5.5.3", - "httpxy": "^0.1.1", + "httpxy": "^0.1.2", "is-primitive": "^3.0.1", "jiti": "^1.19.3", "klona": "^2.0.6", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f36bca8711..1ba34a6078 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -106,8 +106,8 @@ importers: specifier: ^5.5.3 version: 5.5.3 httpxy: - specifier: ^0.1.1 - version: 0.1.1 + specifier: ^0.1.2 + version: 0.1.2 is-primitive: specifier: ^3.0.1 version: 3.0.1 @@ -4453,8 +4453,8 @@ packages: transitivePeerDependencies: - supports-color - /httpxy@0.1.1: - resolution: {integrity: sha512-dYx6Onf6/BrzaHhUKo02IrzWYskol6Atj2GtDOsch4jKk2mmP4N3NuX0VhJ8bnNNFSyMgb1sBgR25fCAxTh5Gg==} + /httpxy@0.1.2: + resolution: {integrity: sha512-8IldwriN7eS7Pe1pPRE6L7S1LW551lBJ3N9oI4HLYGZTXl2JBkdINEi+mDZwhwykw1OBVI2li3zhPSi7r1N8lQ==} dev: false /human-signals@2.1.0: