Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grpc-core

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grpc-core

A simple & minimal helpers for creating gRPC microservices

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
8
Maintainers
1
Weekly downloads
 
Created
Source

gRPC-Core

A simple & minimal helpers for creating high performance gRPC microservices.

Node Version npm version Build Status coverage status License

This package will simplify the creation of gRPC servers and services, you will just need to extend base GrpcService and implement remote functions, this library will turn your normal functions into gRPC callable functions, which makes it easy to create microservices in a clean way.

Basic microservice archticture using helpers in this library

gRPC-Core Diagram

  • You need to be familiar with gRPC and protocol buffers.
  • Example in this package uses the proto3 version of the protocol buffers language.
  • You can find out more about gRPC and proto3 in References section.
Installation
npm install grpc-core --save
Package Components
  • GrpcServer: Main entry point to gRPC microservice
  • GrpcService: Service endpoint
  • GrpcClient: gRPC client to call service methods remotly
1- gRPC Server

You will need to create a new instance from GrpcServer with appropriate configurations then start your server.

You can register one or more gRPC services with the same server

Example:

const { GrpcServer } = require('grpc-core');
const UsersService = require('./UsersService');
const database = require('./database');

const server = new GrpcServer({
	host: 'localhost',
	port: 3000,
	services: [
		UsersService
	],
	onStart: async () => {
		return database.connect();
	},
	onConnected: () => {
		console.log('Users service is up');
	}
});

server.start(); 

GrpcService constructor(config)

NameTypeDescription
hoststringServer host 
portnumberServer port 
servicesArray.<GrpcService>Array of GrpcService derived classes to be exposed 
onConnectedfunctionFunction to be executed after server connectionOptional
onStartfunctionFunction to be executed before server start (ex. db connection)Optional
onClosefunctionFunction to be executed before server shutdown (ex. close db connection)Optional
2- gRPC Service

You will need to create your own service class which extends GrpcService class, and normal class methods will ne callable from your GrpcClient.

  • Async methods are supported.
  • Methods starts with _ underscore will be ignored and considered private, so it will not be exposed into your gRPC service, even if you expose it in your protobuf schema, so it will get missing implementation error.

Example:

const path = require('path');
const { GrpcService } = require('grpc-core');
const hooks = require('../grpc/serviceHooks');
const middlewares = require('../grpc/serviceMiddlewares');
const PROTO = path.join(__dirname, '../config/proto/users.proto');
const User = require('../database/models/user');

class UsersService extends GrpcService {
    constructor(){
        super({
            proto: PROTO,
            hooks: hooks,
            middlewares: middlewares
        });
    }

    // Private method (will not be callable)	
    async _getUserId(user) {
        return user._id;
    }

    async getUsers(ctx) {
        return User.find({});
    }

    async create(ctx){
        try {
            const body = ctx.request;
            await ctx.validateParams(userCreateSchema, body);
            return User.create(body);
        } catch(err) {
            throw err;
        }
    }

    async update(ctx){
        try {
            const _id = ctx.request._id;          
	    // Your update code
            return { updated: true};
        } catch(err) {
            throw err;
        }
    }

    async delete(ctx) {
        try {
            const _id = ctx.request._id;          
	    // Your delete code
            return { delete: true};
        } catch(err){
            throw err;
        }
    }
}

GrpcService Hooks

Hooks are simply helper functions to be attached to request context (ctx) object, which will be passed later to callee function.

Hooks are passed to GrpcService as an object which contains all hook functions

Ex. if we need the request context object to have our own validation function, we will need to define this in hooks:

// hooks.js
module.exports = {
  validate: (body) => {
    // Validation logic here
  }
};
// Then use it in service definition
const { GrpcService } = require('grpc-core');
const hooks = require('./hooks');
const proto = path.join(__dirname, './users.proto');

class UsersService extends GrpcService {
  constructor() {
    super({
      proto,
      hooks
    });
  }
  
  // within our remote function implementation, 
  // hook function will be attached to context object for convenience.
  async create(ctx) {
      try {
        const user = ctx.request;
        // validate function attached to context object
        ctx.validate(user);
        // add user logic
        return user;
      }
      catch(err){
        throw err;
      }
  }
}

module.exports = new UsersService();

GrpcService Middlewares

Middlewares are just functions that will be execute in order before service function and will have the request context as an argument, so inside middleware you can manipulate request data, or request context itself

Middlewares are passed to GrpcService as an array of functions

Ex. Adding middleware to parse request metadata and add it to params field:

// middlewares.js
module.exports = [
  // Metadata parser
  (ctx) => {
    try {
      const md = ctx.metadata.getMap();
      if (md) {
        ctx.params = md;
      }
    }catch (err) {
      throw err;
    }
  }
];
// Then use it in service definition
const { GrpcService } = require('grpc-core');
const middlewares = require('./middlewares');
const proto = path.join(__dirname, './users.proto');

class UsersService extends GrpcService {
  constructor() {
    super({
      proto,
      middlewares
    });
  }
  
  // within our remote function implementation, 
  // ctx object should contain [params] field if caller passed any metadata
  async create(ctx) {
      try {
        const params = ctx.params;
        return params;
      }
      catch(err){
        throw err;
      }
  }
}
3- gRPC Client

GrpcClient instance is required to call remote GrpcService functions, It has only one function "call" which enable a simple way call remote functions.

Creating new GrpcClient:

// usersClient.js
const path = require('path');
const { GrpcClient } = require('grpc-core');
const usersProto = path.join(__dirname, './users.proto');

module.exports = new GrpcClient({
  proto: usersProto,
  service: 'UsersService',
  url: 'localhost:3000',
});

Using GrpcClient:

const client = require('./usersClient');

async function createUser() {
    try {
      const result = await client.call('create', {email: 'test@grpccore.com'});

    } catch(err){
      throw err;
    }
}

TODO

  • Example - API Gateway with simple services
  • gRPC errors handling
References
License

MIT

Keywords

FAQs

Package last updated on 31 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc