Skip to content

Commit c49c0db

Browse files
committed
fix: edge cases due to koa-connect
Remove the koa-connect dependency in favor of a lightweight custom implementation. The new connectToKoa adapter provides the same functionality for bridging Vite's Connect-style middleware to Koa, with comprehensive documentation and support for Vite 4.x-7.x.
1 parent ad89abc commit c49c0db

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

packages/ladle/lib/cli/vite-dev.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import koa from "koa";
33
import http from "http";
44
import http2 from "http2";
55
import https from "https";
6-
import c2k from "koa-connect";
76
import path from "path";
87
import getPort from "get-port";
98
import { globby } from "globby";
@@ -14,6 +13,7 @@ import debug from "./debug.js";
1413
import getBaseViteConfig from "./vite-base.js";
1514
import { getMetaJsonObject } from "./vite-plugin/generate/get-meta-json.js";
1615
import { getEntryData } from "./vite-plugin/parse/get-entry-data.js";
16+
import { connectToKoa } from "./vite-plugin/connect-to-koa.js";
1717

1818
/**
1919
* @param config {import("../shared/types").Config}
@@ -91,7 +91,7 @@ const bundler = async (config, configFolder) => {
9191
}
9292
await next();
9393
});
94-
app.use(c2k(vite.middlewares));
94+
app.use(connectToKoa(vite.middlewares));
9595

9696
// activate https if key and cert are provided
9797
const useHttps =
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Converts Connect/Express-style middleware to Koa middleware.
3+
*
4+
* This adapter bridges the gap between Vite's Connect-based middleware system
5+
* and Ladle's Koa-based server architecture.
6+
*
7+
* @param {Function} connectMiddleware - Connect/Express middleware function with signature (req, res, next)
8+
* @returns {Function} Koa middleware function with signature (ctx, next)
9+
*
10+
* @description
11+
* Vite uses Connect middleware internally (similar to Express), which expects
12+
* (req, res, next) function signatures. Koa uses a different pattern with a context
13+
* object (ctx) and async/await. This adapter wraps Connect middleware to work with Koa.
14+
*
15+
* **Supported Vite Versions:**
16+
* - Vite 7.x
17+
* - Vite 6.x
18+
* - Vite 5.x
19+
* - Vite 4.x
20+
*
21+
* **How it works:**
22+
* 1. Receives a Connect middleware that expects Node.js req/res objects
23+
* 2. Extracts req and res from the Koa context (ctx.req, ctx.res)
24+
* 3. Wraps the Connect middleware call in a Promise
25+
* 4. Handles errors from the Connect middleware by rejecting the Promise
26+
* 5. Continues the Koa middleware chain by calling next()
27+
*
28+
* @example
29+
* import { connectToKoa } from './connect-to-koa.js';
30+
* import vite from 'vite';
31+
*
32+
* const viteServer = await vite.createServer();
33+
* app.use(connectToKoa(viteServer.middlewares));
34+
*/
35+
export const connectToKoa = (connectMiddleware) => {
36+
return async (ctx, next) => {
37+
await new Promise((resolve, reject) => {
38+
connectMiddleware(ctx.req, ctx.res, (err) => {
39+
if (err) reject(err);
40+
else resolve();
41+
});
42+
});
43+
await next();
44+
};
45+
};

packages/ladle/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
"globby": "^14.0.2",
6060
"history": "^5.3.0",
6161
"koa": "^2.15.4",
62-
"koa-connect": "^2.1.0",
6362
"lodash.merge": "^4.6.2",
6463
"msw": "^2.7.0",
6564
"open": "^10.1.0",

pnpm-lock.yaml

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)