cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.56k stars 657 forks source link

🐛 BUG: Failed to parse URL error when using DO with next-on-pages #5947

Open majames opened 3 months ago

majames commented 3 months ago

Which Cloudflare product(s) does this pertain to?

Pages, Miniflare

What version(s) of the tool(s) are you using?

3.57.0 (wrangler), 3.20240512.0 (miniflare), 1 (@cloudflare/next-on-pages)

What version of Node are you using?

20.13.1

What operating system and version are you using?

Mac Sonoma 14.5

Describe the Bug

Observed behavior

I have a app/ws/route.ts GET handler that attempts to pass the request object to a durable objects (DO) fetch method.

// app/ws/route.ts
import type { NextRequest } from 'next/server';
import { getRequestContext } from '@cloudflare/next-on-pages';

export const runtime = 'edge';

export async function GET(request: NextRequest) {
  const signalingNS = getRequestContext().env.signaling;
  const id = signalingNS.idFromName('production');

  const stub = signalingNS.get(id);

  const response = await stub.fetch(request);
  return response;
}

This is result in the following issue:

 ⨯ Error [TypeError]: Failed to parse URL from [object Request]
    at new Request (file:///Users/mj/repos/xtalk/node_modules/miniflare/node_modules/undici/lib/fetch/request.js:88:15)
    at new Request (file:///Users/mj/repos/xtalk/node_modules/miniflare/dist/src/index.js:3692:5)
    at #fetcherFetchCall (file:///Users/mj/repos/xtalk/node_modules/miniflare/dist/src/index.js:6375:21)
    at #call (file:///Users/mj/repos/xtalk/node_modules/miniflare/dist/src/index.js:6292:36)
    at Proxy.fetch (file:///Users/mj/repos/xtalk/node_modules/miniflare/dist/src/index.js:6280:34)
    at GET (webpack-internal:///(rsc)/./app/ws/route.ts:14:33)
    ...

This issue seems very similar to https://github.com/cloudflare/miniflare/issues/454 however I'm on wrangler 3. After tracing it through in the debugger:

> typeof V
'object'
> V instanceof Request
false
> V.url
'http://localhost:3000/ws'
> V.constructor.name
'bound NextRequestHint'

Expected behavior

The request to be successfully passed to the DO.

Steps to reproduce

Follow next-on-pages dev guide to setup a dummy DO and observe the error.

bjerggaard commented 2 months ago

I have the exact same issue in Nuxt 3 Pages project bound (using nitro-cloudflare-dev module) to a Durable Object in a separate wrangler worker project.

zwily commented 2 months ago

I'm hitting the same thing:

  const doId = CHATSTORE.idFromName(`chat-${id}`);
  const doStub = CHATSTORE.get(doId);
  const doResponse = await doStub.fetch(new Request("http://foo/"));

This fails on the fetch because that line of code expects a string (URL), not a Request object. In this case, I can pass a string instead of a Request, but when I want to write to the DO, that workaround doesn't work:

  return doStub.fetch(
    new Request(`http://foo${path}`, {
      method: "PUT",
      body: JSON.stringify(value),
    })
  );

Following the chain of calls, it looks like this argument gets passed to https://github.com/cloudflare/workers-sdk/blob/c643a8193a3c0739b33d3c0072ae716bc8f1565b/packages/miniflare/src/http/request.ts#L33 which then passes the Request to the Undici Request initializer, which doesn't appear to take a Request but wants a URL?