gRPC on Node.js with Buf and TypeScript — Part 1

Buf + gRPC + TypeScript + Node.js = ❤️

Project configuration

$ mkdir grpc-node-buf-typescript
$ cd grpc-node-buf-typescript

Protobuf messages and service definitions

$ mkdir proto
$ cd proto
$ pwd
/Users/me/src/grpc-node-buf-typescript/proto

Quality control with Buf

$ brew tap bufbuild/buf
$ brew install buf
$ buf ls-filescom/language/language.proto
services/hello/hello_service.proto
$ buf check lintcom/language/language.proto:3:1:Package name "com.language" should be suffixed with a correctly formed version, such as "com.language.v1".
com/language/language.proto:10:5:Enum value name "EN" should be prefixed with "CODE_".
com/language/language.proto:10:5:Enum zero value name "EN" should be suffixed with "_UNSPECIFIED".
services/hello/hello_service.proto:5:1:Package name "services.hello" should be suffixed with a correctly formed version, such as "services.hello.v1".
$ buf check lintcom/language/language.proto:3:1:Files with package "com.language.v1" must be within a directory "com/language/v1" relative to root but were in directory "com/language".
services/hello/hello_service.proto:5:1:Files with package "services.hello.v1" must be within a directory "services/hello/v1" relative to root but were in directory "services/hello".
$ buf ls-filescom/language/v1/language.proto
services/hello/v1/hello_service.proto
$ buf check lint# No output means errors :)

FileDescriptorSets, Images, and detecting breaking changes

buf build -o image.json
$ buf check breaking --against image.jsonservices/hello/v1/hello_service.proto:11:1:Previously present field "2" with name "language_code" on message "GreetRequest" was deleted without reserving the name "language_code".
services/hello/v1/hello_service.proto:11:1:Previously present field "2" with name "language_code" on message "GreetRequest" was deleted without reserving the number "2".

Protobuf generation for JavaScript

$ protoc --versionlibprotoc 3.13.0
$ tree build/nodejsbuild/nodejs
├── com
│ └── language
│ └── v1
│ └── language_pb.js
└── services
└── hello
└── v1
└── hello_service_pb.js
6 directories, 2 files

Stub generation for gRPC on Node.js

$ npm install grpc-tools@1.9.1 --global
$ which grpc_tools_node_protoc_plugin
/Users/me/.nvm/versions/node/v14.15.0/bin/grpc_tools_node_protoc_plugin
$ tree build/nodejsbuild/nodejs
├── com
│ └── language
│ └── v1
│ ├── language_grpc_pb.js
│ └── language_pb.js
└── services
└── hello
└── v1
├── hello_service_grpc_pb.js
└── hello_service_pb.js
6 directories, 4 files

Code generation for TypeScript (d.ts)

$ npm install grpc_tools_node_protoc_ts@5.0.1 --global
$ which protoc-gen-ts
~/.nvm/versions/node/v14.15.0/bin/protoc-gen-ts
$ tree build/nodejsbuild/nodejs
├── com
│ └── language
│ └── v1
│ ├── language_grpc_pb.js
│ ├── language_pb.d.ts
│ └── language_pb.js
└── services
└── hello
└── v1
├── hello_service_grpc_pb.d.ts
├── hello_service_grpc_pb.js
├── hello_service_pb.d.ts
└── hello_service_pb.js
6 directories, 7 files

Creating business value through technology; Software Engineering, DevOps, Agile, team leadership and capability building.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store