Skip to content

Add gRPC '-bin' metadata support #233

@ya-kumo

Description

@ya-kumo

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])
  }
}

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

Status

To do

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions