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

importing yjs makes EdgeRuntime crash #243

Open
threepointone opened this issue Jan 25, 2023 · 0 comments
Open

importing yjs makes EdgeRuntime crash #243

threepointone opened this issue Jan 25, 2023 · 0 comments

Comments

@threepointone
Copy link

Bug Report

Current behavior

Consider the following code to be run inside EdgeRuntime

import * as Y from "yjs";

addEventListener("fetch", (event) => {
  console.log(Y);
  return event.respondWith(new Response("hello world"));
});

All it does is import the yjs library and log it.

Now, we have a separate script that actually bundles it and runs it:

import { EdgeRuntime } from "edge-runtime";
import * as esbuild from "esbuild";

// bundle the code in initialCode.js
const initialCode = esbuild.buildSync({
  entryPoints: ["./initialCode.js"],
  bundle: true,
  write: false,
  format: "esm",
  target: "es2019",
  // minify: true, // uncommenting this line "fixes" the issue
}).outputFiles[0].text;

const runtime = new EdgeRuntime({
  initialCode,
});

const response = await runtime.dispatchFetch("https://example.com");

await response.waitUntil();

This throws the following error:

evalmachine.<anonymous>:6991
            throw new TypeError("Failed to parse URL from " + input, { cause: err });
                  ^

TypeError: Failed to parse URL from https://example.com
    at new Request (evalmachine.<anonymous>:6991:19)
    at EdgeRuntime.dispatchFetch (evalmachine.<anonymous>:2:21)
    at file:///Users/threepointone/code/edge-runtime-bug/index.mjs:17:32
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
  [cause]: RangeError: Invalid code point NaN
      at Function.fromCodePoint (<anonymous>)
      at new URLStateMachine (evalmachine.<anonymous>:1533:49)
      at module2.exports.basicURLParse (evalmachine.<anonymous>:2100:19)
      at new URLImpl (evalmachine.<anonymous>:2825:31)
      at exports.setup (evalmachine.<anonymous>:3020:16)
      at new URL (evalmachine.<anonymous>:3074:26)
      at new Request (evalmachine.<anonymous>:6989:25)
      at EdgeRuntime.dispatchFetch (evalmachine.<anonymous>:2:21)
      at file:///Users/threepointone/code/edge-runtime-bug/index.mjs:17:32
      at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
}

The workaround for this is to enable minify:true when bundling, and that makes the code work as expected.

Now, I'm not certain whether this is a bug with yjs, esbuild, or edge-runtime, but I suspect it's edge-runtime, because importing yjs in node works fine, as well as when it's bundled with esbuild. It's only when trying to import it inside edge-runtime that the error pops up. The stack trace implies an error with the url, which doesn't seem right.

Expected behavior/code

The code doesn't crash.

Additional context/screenshots

I made a sample repo that reproduces the bug as described above - https://github.com/threepointone/edge-runtime-bug

threepointone added a commit to partykit/partykit that referenced this issue Jan 25, 2023
We'd previously vendored the libs used by y-partykit (yjs, lib0, etc) to workaround a bug in edge-runtime vercel/edge-runtime#243, but it makes using other libs that include those libs difficult. So instead this patch removes the vendoring, and applies the other workaround (which is to set `minify:true`). The tradeoff for this workaround is that any "dev mode" code (i.e. code wrapped with `if (process.env.NODE_ENV !== 'production')`) will be removed. This is temporary and we'll remove it once the bug is fixed by edge-runtime.
threepointone added a commit to partykit/partykit that referenced this issue Jan 25, 2023
We'd previously vendored the libs used by y-partykit (yjs, lib0, etc) to workaround a bug in edge-runtime vercel/edge-runtime#243, but it makes using other libs that include those libs difficult. So instead this patch removes the vendoring, and applies the other workaround (which is to set `minify:true`). The tradeoff for this workaround is that any "dev mode" code (i.e. code wrapped with `if (process.env.NODE_ENV !== 'production')`) will be removed. This is temporary and we'll remove it once the bug is fixed by edge-runtime.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant