Skip to content

Impossible to set custom User Agent #2325

@liamcottle

Description

@liamcottle

Problem description

It seems that it's not possible to use a custom user agent exactly as it is provided. The library always adds its own user agent to the beginning or end, depending on whether we use grpc.primary_user_agent or grpc.secondary_user_agent options.

Looking in transport.ts, this is the code that decides what the user agent should be:

https://github.com/grpc/grpc-node/blob/%40grpc/grpc-js%401.8.x/packages/grpc-js/src/transport.ts#L128

this.userAgent = [
  options['grpc.primary_user_agent'],
  `grpc-node-js/${clientVersion}`,
  options['grpc.secondary_user_agent'],
]

It doesn't matter how I provide my user agent, grpc-node-js/x.x.x will always be injected into my user agent.

I'm connecting to a service which enforces that user agents must match a specific pattern, or a list of allowed user agents. I'm not able to change this list of user agents, so having this internal user agent injected into to the custom user agent I set makes it impossible to use grpc-js to connect.

Is there any way to override this without forking the project, removing the internal user agent, and releasing my own version of the package?

Reproduction steps

Setting the user agent via any of the following code snippets still results in the internal user agent being injected.

// results in user agent being "ExampleUserAgent/1.2.3.4 grpc-node-js/x.x.x"
const client = new ExampleService("api.example.com", credentials, {
    "grpc.primary_user_agent": "ExampleUserAgent/1.2.3.4",
});
// also results in user agent being "ExampleUserAgent/1.2.3.4 grpc-node-js/x.x.x"
const client = new ExampleService("api.example.com", credentials);
var metadata = new grpc.Metadata();
metadata.add('user-agent', 'ExampleUserAgent/1.2.3.4');
const ret = client.ExampleMethod({}, metadata, (error, result) => {
    // ...
});

Environment

  • OS name, version and architecture: macOS 12.4
  • Node version: v18.12.1
  • Node installation method: unsure
  • Package name and version: @grpc/grpc-js v1.8.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions