denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
94.01k stars 5.23k forks source link

Support for miniflare on windows #25599

Open ryuapp opened 6 days ago

ryuapp commented 6 days ago

Miniflare is a simulator for developing and testing Cloudflare Workers, powered by workerd. This library is used by many frameworks as Cloudflare adapter. e.g.) SvelteKit, Astro, HonoX

Currently, the example written in README does not work either.

PS miniflare> deno --version
deno 2.0.0-rc.2+0a4a8c7 (canary, release, x86_64-pc-windows-msvc)
v8 12.9.202.13-rusty
typescript 5.5.2

miniflare> me mini.ts

import { Miniflare } from "npm:miniflare";

// Create a new Miniflare instance, starting a workerd server
const mf = new Miniflare({
        script: `addEventListener("fetch", (event) => {
    event.respondWith(new Response("Hello Miniflare!"));
  })`,
});

// Send a request to the workerd server, the host is ignored
const response = await mf.dispatchFetch("http://localhost:8787/");
console.log(await response.text()); // Hello Miniflare!

// Cleanup Miniflare, shutting down the workerd server
await mf.dispose();

PS miniflare> deno -A mini.ts
error: Uncaught (in promise) TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
    at Function.from (<anonymous>)
    at Object.<anonymous> (file:///C:/Users/xxxx/AppData/Local/deno/npm/registry.npmjs.org/miniflare/3.20240821.1/dist/src/index.js:6744:64)
    at Object.<anonymous> (file:///C:/Users/xxxx/AppData/Local/deno/npm/registry.npmjs.org/miniflare/3.20240821.1/dist/src/index.js:10197:4)
    at Module._compile (node:module:735:34)
    at Object.Module._extensions..js (node:module:756:11)
    at Module.load (node:module:655:32)
    at Function.Module._load (node:module:523:13)
    at Module.require (node:module:674:19)
    at require (node:module:800:16)
    at file:///C:/Users/xxxx/AppData/Local/deno/npm/registry.npmjs.org/miniflare/3.20240821.1/dist/src/index.js:3:13

Related: https://github.com/denoland/deno/issues/25513

lucacasonato commented 6 days ago

It works for me using both node_modules and npm: specifiers and a global module directory:

$ npm install miniflare
$ cat main.ts
import { Miniflare } from "miniflare";

// Create a new Miniflare instance, starting a workerd server
const mf = new Miniflare({
    script: `addEventListener("fetch", (event) => {
    event.respondWith(new Response("Hello Miniflare!"));
  })`,
});

// Send a request to the workerd server, the host is ignored
const response = await mf.dispatchFetch("http://localhost:8787/");
console.log(await response.text()); // Hello Miniflare!

// Cleanup Miniflare, shutting down the workerd server
await mf.dispose();

$ deno -V
deno 1.46.3
$ deno run -A main.ts
Hello Miniflare!
$ cat main.ts
import { Miniflare } from "npm:miniflare";

// Create a new Miniflare instance, starting a workerd server
const mf = new Miniflare({
    script: `addEventListener("fetch", (event) => {
    event.respondWith(new Response("Hello Miniflare!"));
  })`,
});

// Send a request to the workerd server, the host is ignored
const response = await mf.dispatchFetch("http://localhost:8787/");
console.log(await response.text()); // Hello Miniflare!

// Cleanup Miniflare, shutting down the workerd server
await mf.dispose();

$ deno -V
deno 1.46.3
$ deno run -A main.ts
Hello Miniflare!

Maybe this only happens on Windows?

ryuapp commented 6 days ago

Maybe this only happens on Windows?

I guess so. There were no errors when I ran it on WSL.

nathanwhit commented 6 days ago

Confirmed it reproduces on windows. The immediate issue is https://github.com/denoland/deno/issues/25604, but supporting this will also require fixing https://github.com/denoland/deno/issues/23524