Open ryaninvents opened 4 hours ago
For anyone else who's stuck on the same thing: here's a temporary script I created to work around this for the time being.
import { createServer } from "node:http";
import { Miniflare, Request } from "miniflare";
const mf = new Miniflare({
modules: true,
script: `
export default {
async fetch(request, env, ctx) {
return new Response("Hello Miniflare!");
}
}
`,
});
function rawHeadersToHeaders(rawHeaders: string[]): Headers {
const headers = new Headers();
for (let i = 0; i < rawHeaders.length; i += 2) {
headers.append(rawHeaders[i], rawHeaders[i + 1]);
}
return headers;
}
createServer(async (req, res) => {
// Create a Request object based on the incoming Node request
const request = new Request(`http://${req.headers.host}${req.url}`, {
method: req.method,
headers: rawHeadersToHeaders(req.rawHeaders),
body: req.method !== "GET" && req.method !== "HEAD" ? req : null,
duplex: "half",
});
const response = await mf.dispatchFetch(request);
// Write the Response back to the Node server's res
res.writeHead(
response.status,
Object.fromEntries(response.headers.entries()),
);
const body = await response.text();
res.end(body);
}).listen(3000, () => {
console.log("Server running on http://localhost:3000");
});
Context: I'm trying to set up a new project using Miniflare, but the latest version of
miniflare
(3.20240925.1
) does not seem to match the docs.What I've tried: I'm trying to start an HTTP server with the code listed on the "Get Started" page (reproduced below):
Unfortunately, it looks like the
mf.startServer
API is no longer supported. After some searching, I discovered the@miniflare/http-server
package. I looked at the package and tried the script in the README, with some modifications and additions to work around missing pieces:After installing the required packages, this resulted in an error:
I tried adding
--experimental-vm-modules
on the command line while starting my scripts, and I also searched the API of thenode:vm
module, but couldn't figure out what it's asking for here.If someone could point me in the right direction, I'd be happy to contribute documentation! I'm a fan of Cloudflare and Miniflare, just a little blocked on my current project.
Other things I've tried: