firebase / firebase-admin-node

Firebase Admin Node.js SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
1.63k stars 371 forks source link

[Firestore] grpc error on Bun standalone executable file #2744

Open mavyfaby opened 2 weeks ago

mavyfaby commented 2 weeks ago

Environment

Description

I'm using firestore and it works when using bun src/index.ts but not when using single executable file.

NODE_ENV=production bun build --compile --sourcemap --minify ./src/index.ts --outfile app

and this happens when executing a firestore code, for instance this one:

 await firestore.collection("customers").doc(fbaseId).set({
    data1: "ex1",
    data2: "ex2"
 });

and the error:

 26 |  * error is not necessarily a problem in gRPC itself.
 27 |  * @param status
 28 |  */
 29 | function callErrorFromStatus(status, callerStack) {
 30 |     const message = `${status.code} ${constants_1.Status[status.code]}: ${status.details}`;
 31 |     const error = new Error(message);
                            ^
 error: undefined undefined: undefined
       at X$f (node_modules/@grpc/grpc-js/build/src/call.js:31:23)
       at onReceiveStatus (node_modules/@grpc/grpc-js/build/src/client.js:193:56)
       at onReceiveStatus (node_modules/@grpc/grpc-js/build/src/client-interceptors.js:360:141)
       at onReceiveStatus (node_modules/@grpc/grpc-js/build/src/client-interceptors.js:323:181)
       at node_modules/@grpc/grpc-js/build/src/resolving-call.js:129:78
       at processTicksAndRejections (native:7:39)
       at makeUnaryRequest (node_modules/@grpc/grpc-js/build/src/client.js:161:32)
       at v (node_modules/google-gax/build/src/normalCalls/retries.js:81:81)
       at node_modules/google-gax/build/src/normalCalls/retries.js:125:13
       at call (node_modules/google-gax/build/src/call.js:67:27)
       at call (node_modules/google-gax/build/src/normalCalls/normalApiCaller.js:34:19)
       at node_modules/google-gax/build/src/createApiCall.js:112:30
       at processTicksAndRejections (unknown:7:39)
       at commit (node_modules/@google-cloud/firestore/build/src/write-batch.js:436:23)
       at set (node_modules/@google-cloud/firestore/build/src/reference/document-reference.js:344:27)
       at src/api/customers/register.ts:80:58
       at processTicksAndRejections (native:7:39)
google-oss-bot commented 2 weeks ago

I found a few problems with this issue:

mavyfaby commented 2 weeks ago

cc: @cirospaciari

dconeybe commented 2 weeks ago

Note that Bun is not an officially-supported runtime, although it has been repeatedly requested and is on our radar: https://github.com/firebase/firebase-admin-node/issues/2572

Notably, grpc is known to not work on Bun. Some customers have reported success using the "preferRest" option:

const firestore = initializeFirestore(app, {
  preferRest: true,
});

This causes the network stack to use simple HTTP requests when communicating with the backend, eliding grpc. Note, however, that "rest" does not support streaming, so adding a snapshot listener will try to "upgrade" the connection to grpc, which will likely result in the error that you reported. But as long as you don't need realtime updates, preferRest may help resolve your issue.

Unfortunatley, I cannot make any commitments as to a fix due to its lack of official support. If you find some low-hanging fruit in the https://github.com/googleapis/nodejs-firestore SDK then I'd be open to review a pull request.

Jarred-Sumner commented 2 weeks ago

Notably, grpc is known to not work on Bun. Some customers have reported success using the "preferRest" option:

In Bun v1.1.32, we added server support for our node:http2 implementation and pass almost all of Node.js' http2 tests. We also now run grpc-js's test suite on every commit and 95.25% pass. The failing tests are mostly related to http2 push or the http1 fallback.

Please let us know if you continue to run into issues with gRPC, Firebase, or Firestore in Bun (and file issues in our repo). If it works in Node and doesn't work in Bun, it is a bug in Bun.

In this particular issue's case, this appears to be a bundler bug (bun build --compile runs the bundler). One thing you could try: bun build --compile --format=cjs <file>. This will use CommonJS instead of ESM as the entry point, which might help here.

dconeybe commented 2 weeks ago

Hi @Jarred-Sumner! Thank you for chiming in and providing a suggestion. I'll admit, it's been a while since I tried Firestore with grpc in Bun, and I have very minimal experience with Bun. Again, the team's current priorities don't include Bun support, although I can say we've received a strong demand for it so it is on our radar.