
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
grpc-devtool
Advanced tools
WIP - beta expected soon
gRPC-devtool is a cli program to monitor, record and playback gRPC traffic.
Features
A sample demonstration of creating a project from scratch
Run as node module
# Install with npm
npm install -g grpc-devtool
# Create a project
grpc create --protos path/to/protos
# Recrod traffic
grpc record
# Serve mapped responses
grpc start
Run as docker container
TODO
Use API in nodejs project
TODO
Sample strucuture of config folder
my-stub/
- config/
- grpc.yaml
- greet/
- default.yaml
- rohit.yaml
- virat.yaml
- prices/
- defualt.yaml
- common/
- message.yaml
Configuration file shoud be placed in the config
dir and named as grpc.yaml
Example of a cofiguration file :
protos : '../Protos'
host : localhost
port : 3000
trimmedStreamSize : 10
All configuration values can be configured at runtime (e.g. in your ci build).
e.g.
grpc start --port 50055
Configuration options
Name | default | |
---|---|---|
host | localhost | Address of server ran by grpc-devtool (use it in you app) |
port | 3009 | Port of server exposed by grpc-devtool (use it in you app) |
protos | Relative or absolute path to the directory containing proto files | |
expressionSymbols | ["{{", "}}"] | e.g {{request.body.name}} |
sessionEnabled | true | Enable session support |
keywordSuffix | @ | ends with @ |
trimmedStreamSize | 10 | number for responses for a streaming server to keep in mappings file. Will repeast the responses unless configured otherwise per request basis. |
To define a stubbed grpc endpoint, first update the config/mappings.yaml
in config directory
# Name of endpoint and response rules in order
helloworld.greet.Greeter.SayHello : [
"greet/rohit.js.yaml",
"greet/virat.js.yaml",
"greet/default.js.yaml",
]
prices.streaming.Pricing.Subscribe : [
"prices/211-Stock.js.yaml",
]
Remember that the responses are applied in the order declared in mappings file. So if "greet/rohit.js.yaml"
matches the request, the files next to it won't be matched with the request.
A simple unary request :
request@ : {
name: "rohit"
}
response@ : {
message : "Hello Rohit"
}
For a streaming response :
# prices/211-Stock.js.yaml
request@ : {
uic: 211,
assetType: "Stock"
}
# Response for a streaming request
response@ : {
stream@ : [
{quote: "quote:one"},
{quote: "quote:two"},
{quote: "quote:three"}
],
doNotRepeat@ : true, # if not defined, will stream infinitely
streamInterval@ : 500 # 500 ms of delay between consecutive messages
}
For some endpoint, it is must to have part of response based on request body. In such case we can use the template :
# Responds to any request
request@ : {
name: "any@"
}
# Using expressions in response
response@ : {
message : "Glad to meet you {{request.body.name}}"
}
Specs below this are not implemented or guranteed to work for now.
If your response is extremly complex and you need to parameterize certaion parts of it, you can use partial templates. E.g.
request@: {
uic: 211,
assetType: "Stock"
}
reply@: {
stream@: [
{ message: "this is your first message"},
{ message: "this is your second message"},
{
# path is relative to config directory
include@: "shared/message-template.js.yaml",
param@: {username: "{{request.body.name}}"}
},
],
repeat@: false
}
You can add custom script to handle complex scnarios :
request@ : {
name : "any@"
lastName: "Singh"
# applied only if rest of body matches
# template ignored if it return false
js@: `
return request.matches && request.body.age > 18
`
}
You can even write your own code to create responses dynamically using javascript :
request@: {
stream@: [
{name: "first-user"},
{name: "second-user"},
]
}
@reply: {
stream@: {
js@: "
endpoint.calls = endpoint.calls || 0;
endpoint.calls++;
scope.calls = scope.calls || 0;
scope.calls++;
var message = `
hello : ${request.body.name},
total calls to this endpoint : ${endpoint.calls},
total replies by this rule : ${scope.calls},
sequence in stream : ${stream.$index}`;
return {message};
"
}
}
You can write your own javascript code to add custom logic to templates. Create a directory config/ext
and simply put you javascript file there.
To create custom matcher, create a file ``config/ext /asset-types.js` as :
const matchers = require('miraje/matchers');
const validAssetTypes = ['Stock', 'CfdOnFutures', 'FxSpot'];
module.exports = {
appliesTo : (str) => str === 'assetTypes@',
matches : (value) => validAssetTypes.includes(value)
}
In case you wan't to build a stateful stub (not recommened), you can use sessions. To enable sessions make sure thatsessionEnabled: true
is set inconfig/grpc.yaml
Then you can use sessions in your matchers or templates :
request@: {
name: 'rohit'
}
@reply: {
stream@: {
response: {
message: 'You called me {{session[request.name]}} times.'
}
js@: '
session[request.name] = session[request.name] || 0;
session[request.name] += session[request.name];
'
}
}
FAQs
Record, Playback and Monitor gRPC traffic.
We found that grpc-devtool demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.