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

Uncaught ReferenceError: Buffer is not defined in Partykit (Cloudflare Worker) #1003

Closed
danieldunderfelt opened this issue May 5, 2024 · 4 comments · Fixed by #1015
Closed
Labels

Comments

@danieldunderfelt
Copy link

Describe the bug

I am trying to use EdgeDB in Partykit, which runs on Cloudflare Workers. When starting the Partykit dev server locally, it throws the error: Uncaught ReferenceError: Buffer is not defined

I am not sure how far the local dev server goes to simulate a Cloudflare Worker environment, but CF Workers do support Buffer: https://developers.cloudflare.com/workers/runtime-apis/nodejs

It has to be imported, it can't be used as a global.

The error happens immediately when the dev server is started.

Reproduction

Any use of the edgedb-js client.

Expected behavior

I do not expect an error.

Versions (please complete the following information):

  • OS:
  • EdgeDB version (e.g. 2.0): 5.2+1e07d04
  • EdgeDB CLI version (e.g. 2.0): EdgeDB CLI 5.1.0+8ff9f85
  • edgedb-js version (e.g. 0.20.10;): 1.5.5
  • Node/Deno version: 20.12.2

Additional context

I am running the Partykit dev server with Bun, but I also tried with Node and it had the same result.

@danieldunderfelt
Copy link
Author

Quick fix:

Put import { Buffer } from 'node:buffer' on line 52 of node_modules/edgedb/dist/primitives/buffer.js, before if (Buffer === "function"). Then it works. Use patch-package to make it stick.

@scotttrinh
Copy link
Collaborator

Appreciate the note about the workaround, hopefully that'll hold over anyone who might be running into this until we can get out a proper fix.

For a fix, we should either use a browser compatible API that also works with Node and doesn't sacrifice performance (might be hard...) or use our existing "adapter" pattern to chose either a Node-like or Browser-like adapter like we do for crypto.

@danieldunderfelt
Copy link
Author

danieldunderfelt commented May 9, 2024

The workaround doesn't seem to work when using @edgedb/generate 🤦‍♂️

This works better: instead of importing Buffer, check Buffer from globalThis:

if (typeof globalThis.Buffer === "function")

Thanks for this @scotttrinh!

@scotttrinh
Copy link
Collaborator

@danieldunderfelt

Thanks for the investigation there! That's definitely an easy fix, so good to know that works for the Cloudflare context. Will update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants