supabase / edge-runtime

A server based on Deno runtime, capable of running JavaScript, TypeScript, and WASM services.
MIT License
639 stars 55 forks source link

running supabase functions serve fails. running supabase functions serve <name> causes postgres connection to fail #115

Open oldbettie opened 1 year ago

oldbettie commented 1 year ago

Bug report

Describe the bug

seems like there is an issue with a deno package when running supabase functions serve I get this following error.

worker "/home/deno/functions/scraper" returned an error: Uncaught SyntaxError: The requested module '/v125/jszip@3.5.0/esnext/lib/readable-stream-browser.js' does not provide an export named 'default'
    at https://esm.sh/v125/jszip@3.5.0/esnext/jszip.mjs:2:291
InvalidWorkerCreation: worker boot error
    at async Function.create (ext:sb_user_workers/user_workers.js:80:21)
    at async Server.<anonymous> (file:///home/deno/main/index.ts:95:24)
    at async Server.#respond (https://deno.land/std@0.182.0/http/server.ts:220:24)

if I run supabase functions serve <function-name> --env-file supabase/.env then it will work once but needs to be restarted after every run.

seems to only be when done locally. which makes it incrediby frustrating to have to deploy changes everytime we want to test a function.

To Reproduce

start supabase and run supabase functions serve

Expected behavior

the functions should work

A clear and concise description of what you expected to happen. the function should execute and hot reload locally as expected. deno seems to be flawed

System information

Additional context

seems like local lambda is still quite buggy.

gregnr commented 1 year ago

Thanks for reporting @oldbettie. That would definitely be frustrating having to deploy each time just to test - not at all the intended pattern.

I took a look into jszip and it looks like they are doing some advanced import logic with readable-stream (where you are getting the error).

@supabase/edge-functions it's not clear to me why this would work on Deno Deploy but not edge-runtime - any chance the import logic is slightly different between the two (eg. User-Agent sent to esm.sh to determine the build target)?

@oldbettie in the mean time I was able to solve this specific problem by adding ?bundle to the esm.sh import:

import JSZip from 'https://esm.sh/v125/jszip@3.5.0?bundle';

which tells esm.sh to bundle all dependencies into a single JS file.

Let me know if this helps.

laktek commented 1 year ago

@gregnr Did the import work when function was deployed to Deno Deploy? Will try to reproduce it.

oldbettie commented 1 year ago

everything worked fine when deployed to supabase I am not importing anything for JSZip it seems to be something done under the hood. @gregnr Should I be cloning and running my functions in the edge-runtime rather then the supabase cli?

northfacejmb commented 1 year ago

I think I might be having a similar issue. Thank you for posting, I hope you're able to get to the bottom of it

gregnr commented 1 year ago

@laktek sorry I didn't actually attempt on Deno Deploy - was making that assumption based on the original post, but I could be wrong.

@oldbettie the latest Supabase CLI uses edge-runtime under the hood when you run supabase functions serve.

I am not importing anything for JSZip it seems to be something done under the hood

Do you mind sharing the code you are running? Guessing you have another dependency that imports JSZip.

oldbettie commented 1 year ago
{
  "imports": {
    "postgres": "https://deno.land/x/postgres@v0.17.0/mod.ts",
    "puppeteer": "https://deno.land/x/puppeteer@16.2.0/mod.ts"
  }
}

here is my import map. I am alse importing serve directly into the functions as it seemed to be a bit faster

gregnr commented 1 year ago

Thanks. Looks like puppeteer is likely the one importing JSZip: https://github.com/lucacasonato/deno-puppeteer/blob/45b3162585b98ad8d54abeb56f48ecbb17c614eb/vendor/puppeteer-core/vendor/zip/mod.ts#L1

You can try overriding imports to force puppeteer to pull in JSZip with the bundle param (untested):

Edit for future viewers - this works:

{
  "imports": {
    "postgres": "https://deno.land/x/postgres@v0.17.0/mod.ts",
    "puppeteer": "https://deno.land/x/puppeteer@16.2.0/mod.ts"
  },
  "scopes": {
    "https://deno.land/x/puppeteer@16.2.0/vendor/puppeteer-core/vendor/zip/mod.ts": {
      "https://esm.sh/jszip@3.5.0": "https://esm.sh/jszip@3.5.0?bundle"
    }
  }
}
byteDJINN commented 1 year ago

I am also experiencing this exact issue. I'm following the example for puppeteer used in the documentation https://supabase.com/docs/guides/functions/examples/screenshots. I tried to override the imports so my import_map.json looks like:

{
  "imports": {
    "std/server": "https://deno.land/std@0.177.0/http/server.ts",
    "puppeteer": "https://deno.land/x/puppeteer@16.2.0/mod.ts"
  },
  "scopes": {
    "puppeteer": {
      "https://esm.sh/jszip@3.5.0/": "https://esm.sh/jszip@3.5.0?bundle/"
    }
  }
}

However this had no effect, same error.

oldbettie commented 1 year ago

@Elodin77 I have just been running the function individually and it seems to work. Bit annoying as I cant test the functions that call other functions locally but at least its something

gregnr commented 1 year ago

Okay found some time to test this out - I was able to get it working using this import map:

{
  "imports": {
    "postgres": "https://deno.land/x/postgres@v0.17.0/mod.ts",
    "puppeteer": "https://deno.land/x/puppeteer@16.2.0/mod.ts"
  },
  "scopes": {
    "https://deno.land/x/puppeteer@16.2.0/vendor/puppeteer-core/vendor/zip/mod.ts": {
      "https://esm.sh/jszip@3.5.0": "https://esm.sh/jszip@3.5.0?bundle"
    }
  }
}

Or if you just want to override jszip everywhere:

{
  "imports": {
    "postgres": "https://deno.land/x/postgres@v0.17.0/mod.ts",
    "puppeteer": "https://deno.land/x/puppeteer@16.2.0/mod.ts",
    "https://esm.sh/jszip@3.5.0": "https://esm.sh/jszip@3.5.0?bundle"
  }
}

Worth noting this is a workaround - ideally we find out what's really happening here and see what is different between edge-runtime and Deno.

andreespirela commented 1 year ago
andrespirela@Andress-MBP edge-runtime % deno run --allow-all examples/empty-response/index.ts
error: Uncaught SyntaxError: The requested module '/v125/jszip@3.5.0/esnext/lib/readable-stream-browser.js' does not provide an export named 'default'
    at <anonymous> (https://esm.sh/v125/jszip@3.5.0/esnext/jszip.mjs:2:291)

@laktek

skikrumb-ryan commented 1 month ago

Also getting this issue which works on a different project. Went back and used the same function I wrote on another project and I get this error when trying to serve:

Could not resolve './models/EmailJSResponseStatus' from 'file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@emailjs/nodejs/4.0.4/mjs/index.js'. An error has occured InvalidWorkerCreation: worker boot error Could not resolve './models/EmailJSResponseStatus' from 'file:///tmp/sb-compile-edge-runtime/node_modules/localhost/@emailjs/nodejs/4.0.4/mjs/index.js'. at async UserWorker.create (ext:sb_user_workers/user_workers.js:144:15)

AliBdeir commented 3 weeks ago

It's been almost a year. Is there really no official response or at least acknowledgment of this?