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

Add gRPC '-bin' metadata support #233

Open
ya-kumo opened this issue Aug 10, 2023 · 0 comments
Open

Add gRPC '-bin' metadata support #233

ya-kumo opened this issue Aug 10, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@ya-kumo
Copy link

ya-kumo commented Aug 10, 2023

I want to mock gRPC response with a metadata which has '-bin' name suffix.

According to document gRPC over HTTP2, a header name ends with '-bin' indicates that it has binary value, and the value should be base64 encoded.

Binary-Header → {Header-Name "-bin" } {base64 encoded value}

It seems that metadata is not treated properly in GrpcParser, and cause an error. (v0.14.2)

error: keys that end with '-bin' must have Buffer values

This error is comes from @grpc/grpc-js/src/metadata.ts

function validate(key: string, value?: MetadataValue): void {
  // ...
  if (isBinaryKey(key)) {
    if (!Buffer.isBuffer(value)) {
      throw new Error("keys that end with '-bin' must have Buffer values");
    }
  }
  // ...
}

So grpc-js is forcing their users to use Buffer, and I don't think there is any problem with that.

I think adding a simple if-else may be the solution. I tried something like this and it works for me.

// src/parser/GrpcParser.ts

for (const key in metadata) {
  if (key.endsWith("-bin")) {
    trailers.add(key, Buffer.from(metadata[key], "base64"))
  } else {
    trailers.add(key, metadata[key])
  }
}
@ya-kumo ya-kumo added the enhancement New feature or request label Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

No branches or pull requests

2 participants