Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kenriortega/fastify-protobufjs
Fastify and protobufjs, together at last. Uses protobufjs by default.
https://github.com/kenriortega/fastify-protobufjs
fastify fastify-plugin
Last synced: 2 months ago
JSON representation
Fastify and protobufjs, together at last. Uses protobufjs by default.
- Host: GitHub
- URL: https://github.com/kenriortega/fastify-protobufjs
- Owner: kenriortega
- License: mit
- Created: 2021-05-17T01:16:28.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-05-18T22:50:45.000Z (over 3 years ago)
- Last Synced: 2024-09-28T11:22:59.318Z (3 months ago)
- Topics: fastify, fastify-plugin
- Language: JavaScript
- Homepage:
- Size: 163 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Fastify-protobufjs
Fastify and protobufjs, together at last. Uses protobufjs by default.
## Functionality
Provides transparent middleware that can be used to support clients requesting Accept: application/x-protobuf from endpoints using res.json or sending Content-Type: application/x-protobufjs to any endpoint. You can continue to use req.body and res.json and fastifyProtobuf will handle the conversion in the background using protobufjs.
Installation
------------```bash
$ npm install --save fastify-protobufjs
// or
$ yarn add fastify-protobufjs
```
Test
------------```bash
$ npm test```
Usage
-----```javascript
const fastifyProtobuf = require("fastify-protobufjs");// ...
// custom pluginfastify.register(fastifyProtobuf), {
protoloadPath: path.join(__dirname, 'schema', 'package.proto'),
messagePackage: 'Package'
})
````Usage in simple app
-------------------```javascript
'use strict'
const path = require('path')const fastify = require('fastify')({
logger: true
})fastify.register(require('fastify-protobufjs'), {
protoloadPath: path.join(__dirname, 'schema', 'package.proto'),
messagePackage: 'Package'
})fastify.post('/decode', (req, reply) => {
// http POST http://localhost:5000/decode Accept:application/x-protobuf Content-Type:application/x-protobuf @grpc\package-protobuf.dat
const body = req.body
return body
})
fastify.get('/encode', (req, reply) => {
// http http://localhost:5000/encode Accept:application/x-protobufreply.send({
name: "binari-encodings",
private: true,
version: "1.0.0",
main: "index.js",
licence: "MIT",
value: 42
})
})const start = async () => {
try {
await fastify.listen(5000)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}start()
```