Registry / http-networking / grpc-create-metadata

grpc-create-metadata

JSON →
library4.0.1jsnpmunverified

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.

npm install grpc-create-metadata
INSTALL
IMPORT
SIG · GRPC-CREATE-METADA
G
grpc-create-metadata
http-networkingjavascriptv4.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

create
import create from 'grpc-create-metadata';
const create = require('grpc-create-metadata');
The library provides a single default export. While CommonJS `require` is compatible and shown in older examples, ESM `import` is preferred for new projects and modern Node.js environments.
Metadata
import { Metadata } from '@grpc/grpc-js';
const grpc = require('grpc'); const Metadata = grpc.Metadata;
The `create` function returns an instance of `grpc.Metadata`. This import from the peer dependency `@grpc/grpc-js` is necessary for type checking, `instanceof` checks, or direct interaction with gRPC metadata objects.
Client
import { Client, credentials } from '@grpc/grpc-js';
const grpc = require('grpc');
Although not directly part of `grpc-create-metadata`, usage often involves importing gRPC client or server components from `@grpc/grpc-js` to integrate the created metadata into service calls. Ensure consistency with `@grpc/grpc-js` versioning.

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());
Debug
Known issues
deprecatedVersion 3.1.0 of `grpc-create-metadata` is explicitly deprecated. Users should upgrade to version 3.0.0 or any 4.x.x release for continued support and security updates, as v3.1.0 contained an accidental re-release of v4.0.0's dependencies.
fix
Upgrade package to `^3.0.0` (excluding 3.1.0) or `^4.0.0`. For example: `npm install grpc-create-metadata@^4.0.0`.
affects: =3.1.0
breakingVersion 4.0.0 updated the underlying `@grpc/grpc-js` peer dependency. While `grpc-create-metadata`'s API remains consistent, ensure your project's `@grpc/grpc-js` version is compatible (>=1.2.5 as specified in peer dependencies) to avoid runtime issues related to gRPC version mismatches.
fix
Verify and update your project's `@grpc/grpc-js` installation to a compatible version, e.g., `npm install @grpc/grpc-js@^1.2.5`.
affects: >=4.0.0
gotchagRPC `Metadata` objects, and consequently the `create` utility, strictly process values as strings or Buffers. Non-string/Buffer JavaScript types (e.g., numbers, booleans, objects) are automatically converted to strings via their `toString()` method, which might not always yield the desired serialization format for complex data.
fix
For complex data types or specific serialization needs, manually convert them to a string (e.g., `JSON.stringify()`) or a Buffer before passing them to `grpc-create-metadata` if the default `toString()` behavior is insufficient.
affects: >=1.0.0
gotchaMetadata keys must conform to gRPC naming conventions (e.g., lowercase, hyphens allowed, no special characters or spaces). Supplying invalid keys in the input object will result in them being ignored by the underlying gRPC `Metadata` object or causing runtime errors within gRPC itself, not `grpc-create-metadata`.
fix
Validate input object keys to ensure they are valid gRPC metadata keys (e.g., `/^[a-z0-9][a-z0-9_-]*$/`).
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: The 'metadata' argument must be an instance of 'Metadata'.
Attempting to pass a plain JavaScript object directly to a gRPC client method's metadata argument instead of an `grpc.Metadata` instance created by `grpc-create-metadata`.
fix
Ensure that plain JavaScript objects intended as metadata are first processed by `create()` from `grpc-create-metadata` before being passed to gRPC client calls: `client.someMethod(request, create(myObject))`.
TypeError: Value must be a string or buffer (argument 2).
Attempting to manually add a non-string or non-buffer value directly to a `grpc.Metadata` object using `metadata.add(key, value)` without `grpc-create-metadata`'s type conversion.
fix
Use the `grpc-create-metadata` utility, which handles `toString()` conversion for non-string/buffer types, or manually convert values to string/buffer before adding them to `grpc.Metadata`.
Upgrade
Version history
4.0.1latest on npm
Audit
Dependencies
@grpc/grpc-jsrequiredPeer dependency providing the core gRPC Metadata object and related functionalities.
Agent activity
4 hits · last 30 days
node
4
Resources
grpc-create-metadata — npm install grpc-create-metadata · libregistry