GRPC

Setup micro-services talking through gRPC calls.

/* @runkit */
// register extension
const GRPC = Grown.use(require('@grown/grpc'));

// ./index.proto
// syntax = "proto3";
// package API;
// service Test {
//   rpc is(Input) returns (Output) {}
// }
// message Input {
//   int32 truth = 1;
// }
// message Output {
//   string reveal = 1;
// }

// ./handlers/Test/is/index.js
// module.exports = function ({ request }) {
//   return {
//     reveal: request.truth === 42,
//   };
// };

const Gateway = Grown.GRPC.Gateway({
  include: [
    GRPC.Loader.scan(`${__dirname}/index.proto`),
  ],
});

const API = Grown.load(`${__dirname}/handlers`);
const gateway = Gateway.setup(API, {
  timeout: 10,
}).start();

const result = await gateway.API.Test.is({ truth: 42 });

console.log('GOT:', result);

Loader

Public methods static

  • scan(file[, options]) — Returns a mapping of all services found in the given file, options are given to @grpc/proto-loader when loading the file.

Gateway

Public methods static

  • setup(controllers[, options]) — Returns a gateway instance with services and controllers merged. Invoke them as map.send<Service>(...) or as map.Service.method({ ... }) indistinctly.

    Supported options are:

    • hostname(serviceName) — Function, used to determine a host for service registration, fallback to 0.0.0.0 otherwise.
    • timeout — Number, timeout in seconds for gRPC deadline option; default to undefined.
    • port — Number, custom port for service registration; default to 50051.

Calls to hostname(...) would return a port too, in such case is preferred and used instead.

Private* methods static

  • _callService(options, client, method, data) — Wrap used methods when calling services.
  • _getService(name, controller) — Decorate all methods on given handlers.
  • _onError(e) — Decorate errors thrown by calling services.

➯ Next: Extensions ⟩ Logger