gRPC Metadata Creator Utility

4.0.1 · active · verified Sun Apr 19

This utility package, `grpc-create-metadata`, provides a straightforward helper function to convert plain JavaScript objects into gRPC `Metadata` instances. It automatically handles type conversion for common primitives like numbers and booleans by calling their `toString()` method, while `Buffer` and `String` values are passed directly. The package is currently at version 4.0.1 and primarily receives updates for dependency maintenance, especially related to its `@grpc/grpc-js` peer dependency. Its core differentiation lies in simplifying the common task of populating `grpc.Metadata` objects, which strictly require string or buffer values, by abstracting the conversion logic, making it easier for developers to work with gRPC services in Node.js. It maintains a stable, albeit infrequent, release cadence driven by underlying gRPC dependency updates and security patches.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to use the 'grpc-create-metadata' utility to transform a plain JavaScript object into a gRPC Metadata object, inspect its contents, and understand its behavior with existing Metadata instances.

import create from 'grpc-create-metadata';
import { Metadata } from '@grpc/grpc-js';

const userDetails = {
  name: 'Alice',
  age: 30,
  isAdmin: true,
  // Buffers are passed as-is
  token: Buffer.from('my-secret-token')
};

const metadata = create(userDetails);

console.log('Is instance of Metadata?', metadata instanceof Metadata); // true
console.log('Metadata map:', metadata.getMap());
// Example output might be: { name: 'Alice', age: '30', isadmin: 'true', token: <Buffer ...> }

// Demonstrating passing an existing Metadata object (it's returned as-is)
const existingMeta = new Metadata();
existingMeta.add('correlation-id', 'abc-123');
const sameMeta = create(existingMeta);
console.log('Existing Metadata handled:', sameMeta.getMap());

view raw JSON →