oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.53k stars 2.79k forks source link

@grpc/grpc-js not returning object as expected, trace included #7630

Closed morpig closed 1 month ago

morpig commented 11 months ago

What version of Bun is running?

1.0.17

What platform is your computer?

Darwin 23.2.0 arm64 arm

What steps can reproduce the bug?

use grpc-js related modules. in my case @google-cloud/pubsub

const { PubSub } = require('@google-cloud/pubsub');

const pubsub = new PubSub({
    projectId: '///',
    credentials: {
    }
});

    const messageId = await pubsub
      .topic('email-broker')
      .publishMessage({
             //
        }))
      });

What is the expected behavior?

successfull grpc send to gcp pubsub

What do you see instead?

no errors/throws. promise stuck at pending.

Additional information

for some reason, bun is not returning the following object to the send function.

https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/client.ts#L231

  private checkOptionalUnaryResponseArguments<ResponseType>(
    arg1: Metadata | CallOptions | UnaryCallback<ResponseType>,
    arg2?: CallOptions | UnaryCallback<ResponseType>,
    arg3?: UnaryCallback<ResponseType>
  ): {
    metadata: Metadata;
    options: CallOptions;
    callback: UnaryCallback<ResponseType>;
  } {
    if (isFunction(arg1)) {
      return { metadata: new Metadata(), options: {}, callback: arg1 };
    } else if (isFunction(arg2)) {
      if (arg1 instanceof Metadata) {
        return { metadata: arg1, options: {}, callback: arg2 };
      } else {
        return { metadata: new Metadata(), options: arg1, callback: arg2 };
      }
    } else {
      if (
        !(
          arg1 instanceof Metadata &&
          arg2 instanceof Object &&
          isFunction(arg3)
        )
      ) {
        throw new Error('Incorrect arguments passed');
      }
      return { metadata: arg1, options: arg2, callback: arg3 }; // STUCK HERE
    }
  }

checkOptionalUnaryResponseArguments is triggered by makeUnaryRequest https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/client.ts#L276

  makeUnaryRequest<RequestType, ResponseType>(
    method: string,
    serialize: (value: RequestType) => Buffer,
    deserialize: (value: Buffer) => ResponseType,
    argument: RequestType,
    metadata: Metadata | CallOptions | UnaryCallback<ResponseType>,
    options?: CallOptions | UnaryCallback<ResponseType>,
    callback?: UnaryCallback<ResponseType>
  ): ClientUnaryCall {
    const checkedArguments =
      this.checkOptionalUnaryResponseArguments<ResponseType>(
        metadata,
        options,
        callback
      );
      // stuck here bc no object returned.

console.log shows bun returning all functions within metadata object, meanwhile node does not.

bun console.log-ing the object

{
  metadata: Metadata {
    internalRepr: Map(2) {
      "x-goog-api-client": [ "gax/4.0.5 gapic/4.0.7 gl-node/20.8.0 grpc/1.9.11 gccl/4.0.7"
      ],
      "x-goog-request-params": [ "topic=projects%2Fserverdream%2Ftopics%2Femail-broker"
      ],
    },
    options: {},
    set: [Function: set],
    add: [Function: add],
    remove: [Function: remove],
    get: [Function: get],
    getMap: [Function: getMap],
    clone: [Function: clone],
    merge: [Function: merge],
    setOptions: [Function: setOptions],
    getOptions: [Function: getOptions],
    toHttp2Headers: [Function: toHttp2Headers],
    toJSON: [Function: toJSON],
  },
  options: {
    deadline: 2023-12-13T13:20:23.870Z,
  },
  callback: [Function],
}

node console.log-ing the object

{
  metadata: Metadata {
    internalRepr: Map(2) {
      'x-goog-api-client' => [Array],
      'x-goog-request-params' => [Array]
    },
    options: {}
  },
  options: { deadline: 2023-12-13T13:09:32.436Z },
  callback: [Function (anonymous)]
}
rafaelcorreiapoli commented 11 months ago

Any news on this? This is issue is blocking our company to move from Node to Bun

seo-rii commented 10 months ago

This error also happens with @google-cloud/datastore module.

murgatroid99 commented 10 months ago

The difference in output from checkOptionalUnaryResponseArguments is probably not related to the problem. It looks like the same object is returned in both cases, but Bun is printing the contents of the prototype and Node is not.

seo-rii commented 10 months ago

I believe https://github.com/firebase/firebase-admin-node/issues/2300 and https://github.com/oven-sh/bun/issues/4746 are duplication of this error.

Bun has announced that gRPC works from last September. Maybe it's a bug after update on package? https://bun.sh/blog/bun-v1.0.13#grpc-works-now

aN00B1s commented 9 months ago

Any updates?

Goddard commented 8 months ago

Tried bun for the first time, but ran into this issue.

cirospaciari commented 1 month ago

Cant reproduce this issue anymore affter https://github.com/oven-sh/bun/pull/14286 feel free to reopen if is still happening.

coupez commented 4 hours ago

This issue is still happening @cirospaciari after bun build ... --target bun. Works fine with just bun run .... This seems to be an issue specifically when building to format esm. When building to cjs, it seems to work.