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

(feat): support devProxy nitro options, in vinxi dev server #58

Merged
merged 3 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/swift-files-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vinxi": patch
---

(feat): support devProxy nitro options, in vinxi dev server
82 changes: 41 additions & 41 deletions packages/vinxi/lib/nitro-dev.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// import httpProxy from "http-proxy";
import { listen } from "@vinxi/listhen";
import { watch } from "chokidar";
import {
Expand All @@ -9,6 +8,7 @@ import {
promisifyNodeListener,
toNodeListener,
} from "h3";
import httpProxy from "http-proxy";
import { servePlaceholder } from "serve-placeholder";
import serveStatic from "serve-static";
import { joinURL } from "ufo";
Expand All @@ -35,7 +35,7 @@ import { createRouteRulesHandler } from "./route-rules.js";
// worker.once("exit", (code) => {
// reject(
// new Error(
// code ? "[worker] exited with code: " + code : "[worker] exited"
// code ?"[worker] exited with code: " + code : "[worker] exited"
// )
// );
// });
Expand Down Expand Up @@ -166,6 +166,21 @@ export function createDevServer(nitro) {
*/
const ws = {};

// User defined dev proxy
for (const route of Object.keys(nitro.options.devProxy).sort().reverse()) {
let opts = nitro.options.devProxy[route];
if (typeof opts === "string") {
opts = { target: opts };
}
const proxy = createProxy(opts);
app.use(
route,
eventHandler(async (event) => {
await proxy.handle(event);
}),
);
}

// Dev-only handlers
for (const handler of nitro.options.devHandlers) {
app.use(
Expand All @@ -174,21 +189,6 @@ export function createDevServer(nitro) {
);
}

// User defined dev proxy
// for (const route of Object.keys(nitro.options.devProxy).sort().reverse()) {
// let opts = nitro.options.devProxy[route];
// if (typeof opts === "string") {
// opts = { target: opts };
// }
// const proxy = createProxy(opts);
// app.use(
// route,
// eventHandler(async (event) => {
// await proxy.handle(event);
// }),
// );
// }

// Main worker proxy
// const proxy = createProxy();
// proxy.proxy.on("proxyReq", (proxyReq, req) => {
Expand Down Expand Up @@ -237,12 +237,12 @@ export function createDevServer(nitro) {
// );

// Listen
/** @type {import("listhen").Listener[]} */
/** @type {import("@vinxi/listhen").Listener[]} */
let listeners = [];
/**
*
* @param {number} port
* @param {Partial<import("listhen").ListenOptions>} opts
* @param {Partial<import("@vinxi/listhen").ListenOptions>} opts
* @returns
*/
const _listen = async (port, opts) => {
Expand Down Expand Up @@ -284,25 +284,25 @@ export function createDevServer(nitro) {
};
}

// function createProxy(defaults = {}) {
// const proxy = httpProxy.createProxy();
// const handle = (event, opts = {}) => {
// return new Promise((resolve, reject) => {
// proxy.web(
// event.node.req,
// event.node.res,
// { ...defaults, ...opts },
// (error) => {
// if (error.code !== "ECONNRESET") {
// reject(error);
// }
// resolve();
// },
// );
// });
// };
// return {
// proxy,
// handle,
// };
// }
function createProxy(defaults = {}) {
const proxy = httpProxy.createProxy();
const handle = (event, opts = {}) => {
return new Promise((resolve, reject) => {
proxy.web(
event.node.req,
event.node.res,
{ ...defaults, ...opts },
(error) => {
if (error.code !== "ECONNRESET") {
reject(error);
}
resolve();
},
);
});
};
return {
proxy,
handle,
};
}
Loading
Loading