-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2a5aef7
Showing
25 changed files
with
824 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.proto] | ||
indent_style = space | ||
indent_size = 2 | ||
max_line_length = 80 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": [], | ||
"include": ["src/**/*", "dangerfile.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { | ||
...require('@ackee/styleguide-backend-config/eslint'), | ||
ignorePatterns: ['dist'], | ||
parserOptions: { | ||
project: '.eslint.tsconfig.json', | ||
}, | ||
rules: { | ||
...require('@ackee/styleguide-backend-config/eslint').rules, | ||
'@typescript-eslint/no-misused-promises': 1, | ||
'@typescript-eslint/ban-ts-ignore': 1, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules | ||
package-lock.json | ||
dist | ||
docs | ||
coverage | ||
tsconfig.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"*.ts": ["prettier --write", "eslint --fix"], | ||
"*.{ts,js,json,md}": "prettier --write", | ||
"*": ["eclint fix", "eclint check"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
language: node_js | ||
node_js: | ||
- '10' | ||
- '12' | ||
- '13' | ||
- '14' | ||
|
||
script: | ||
- npm run cs:prettier:check | ||
- npm run cs:eslint:check | ||
- npm run build | ||
- npm run test:coverage | ||
- npm run test:codecov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Jaroslav Šmolík | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<div align="center"> | ||
|
||
![](https://gist.githubusercontent.com/grissius/2125f85d5983d980e1696f99b9b77bc7/raw/9c82b3406e4b1c9eb9d21746b67e22fc8f56dfe1/logo.png) | ||
|
||
# protocat | ||
|
||
Modern, minimalist type-safe gRPC framework for Node.js | ||
|
||
</div> | ||
|
||
## Quickstart | ||
|
||
```typescript | ||
import { Server } from 'protocat' | ||
import { CatService } from '../dist/cat_grpc_pb' // Generated service definition | ||
server = new Server() | ||
server.addService(CatService, { | ||
getCat: async call => { | ||
const cat = await getCatByName(call.request?.getName() ?? '') | ||
call.response.setName(cat.name) | ||
.setHealth(cat.health) | ||
.setLevel(cat.level) | ||
.setClass(cat.profession ?? 'warrior') | ||
} | ||
} | ||
|
||
server.start('0.0.0.0:3000', ServerCredentials.createInsecure()) | ||
``` | ||
## Roadmap | ||
- [ ] Middleware | ||
- [ ] Error handling | ||
- [ ] Type safety | ||
- [ ] Call pool | ||
- [ ] Partial definition | ||
- [ ] Serialization level caching | ||
- [ ] Docs | ||
## See also | ||
- [Mali](https://mali.js.org/) - Minimalistic Node.js gRPC microservice framework | ||
- [BloomRPC](https://github.com/uw-labs/bloomrpc) - GUI Client for GRPC Services | ||
- [ghz](https://github.com/bojand/ghz) - Simple gRPC benchmarking and load testing tool | ||
- [grpc-health-probe](https://github.com/grpc-ecosystem/grpc-health-probe) - A command-line tool to perform health-checks for gRPC applications in Kubernetes etc. | ||
## License | ||
This project is licensed under [MIT](./LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
danger, | ||
warn, | ||
message, | ||
fail, | ||
markdown, | ||
schedule, | ||
peril, | ||
results, | ||
} from 'danger' | ||
import { runDangerRules } from '@ackee/styleguide-backend-config/danger' | ||
|
||
runDangerRules({ | ||
danger, | ||
warn, | ||
message, | ||
fail, | ||
markdown, | ||
schedule, | ||
peril, | ||
results, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testMatch: ['<rootDir>/src/**/*.test.ts'], | ||
collectCoverageFrom: ['<rootDir>/src/lib/**'], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"name": "protocat", | ||
"description": "Modern, minimalist type-safe gRPC framework for Node.js", | ||
"version": "0.0.0", | ||
"author": "Jaroslav Šmolík <grissius@gmail.com>", | ||
"main": "./dist/index.js", | ||
"files": [ | ||
"dist/*" | ||
], | ||
"license": "MIT", | ||
"keywords": [ | ||
"grpc", | ||
"middleware", | ||
"framework", | ||
"node", | ||
"microservice", | ||
"api", | ||
"server" | ||
], | ||
"scripts": { | ||
"build": "tsc", | ||
"build:proto": "rm -rf ./dist/test/api && mkdir -p ./dist/test/api && grpc_tools_node_protoc --js_out=import_style=commonjs,binary:./dist/test/api --ts_out=generate_package_definition:./dist/test/api --grpc_out=grpc_js:./dist/test/api -I ./src/test/api ./src/test/api/**/*.proto", | ||
"postinstall": "npm run build:proto", | ||
"pretest": "npm run build:proto", | ||
"test": "jest --runInBand --verbose", | ||
"test:coverage": "npm run test -- --collectCoverage", | ||
"test:codecov": "codecov", | ||
"prepare": "npm run build", | ||
"check": "npm-check -i app -i errors -i config -i index & exit 0", | ||
"cs:eslint:check": "eslint --ignore-path .gitignore '**/*.ts' -f codeframe", | ||
"cs:eslint:fix": "npm run cs:eslint:check -- --fix", | ||
"cs:prettier:check": "prettier --ignore-path .gitignore --check '**/*.{ts,js,json,md}'", | ||
"cs:prettier:fix": "npm run cs:prettier:check -- --write '**/*.{ts,js,json,md}'", | ||
"cs:eclint:check": "eclint check '**/*'", | ||
"cs:eclint:fix": "eclint fix '**/*'" | ||
}, | ||
"dependencies": { | ||
"@grpc/grpc-js": "^1.0.5" | ||
}, | ||
"devDependencies": { | ||
"@ackee/styleguide-backend-config": "^0.1.6", | ||
"@types/google-protobuf": "^3.7.2", | ||
"@types/jest": "^25.2.1", | ||
"codecov": "^3.6.5", | ||
"google-protobuf": "^3.12.2", | ||
"grpc-tools": "^1.9.0", | ||
"grpc_tools_node_protoc_ts": "^4.1.0", | ||
"husky": "^4.2.5", | ||
"jest": "^26.0.1", | ||
"lint-staged": "^10.2.1", | ||
"npm-check": "^5.9.2", | ||
"prettier": "^2.0.5", | ||
"ts-jest": "^26.1.0", | ||
"typescript": "^3.8.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('@ackee/styleguide-backend-config/prettier') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { Server } from './lib/server' | ||
export { CallType } from './lib/call-types' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** Call types aligned with grpc core library */ | ||
export enum CallType { | ||
BIDI = 'bidi', | ||
SERVER_STREAM = 'serverStream', | ||
CLIENT_STREAM = 'clientStream', | ||
UNARY = 'unary', | ||
} | ||
|
||
/** Assign call type from generated definition */ | ||
export const stubToType = ( | ||
s: Record<'responseStream' | 'requestStream', boolean> | ||
) => | ||
s.responseStream | ||
? s.requestStream | ||
? CallType.BIDI | ||
: CallType.SERVER_STREAM | ||
: s.requestStream | ||
? CallType.CLIENT_STREAM | ||
: CallType.UNARY |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as grpc from '@grpc/grpc-js' | ||
import { RemoveIdxSgn } from './type-helpers' | ||
import { CallType } from './call-types' | ||
|
||
/** ProtoCat context enrichment for incoming calls */ | ||
export type ProtoCatContext<Res, Type extends CallType> = { | ||
/** Server initial metadata (sent automatically for unary / client stream) */ | ||
initialMetadata: grpc.Metadata | ||
/** Manually send initial metadata for server stream / bidi */ | ||
flushInitialMetadata: () => void | ||
/** Trailing metadata (sent automatically) */ | ||
trailingMetadata: grpc.Metadata | ||
/** Type of call */ | ||
type: Type | ||
} & (Type extends CallType.UNARY | CallType.CLIENT_STREAM | ||
? { | ||
/** Ready response instance initialized for every call */ | ||
response: Res | ||
} | ||
: {}) | ||
|
||
export type NextFn = () => Promise<void> | ||
export type ServiceHandlerUnary<Req, Res> = ( | ||
call: ProtoCatContext<Res, CallType.UNARY> & grpc.ServerUnaryCall<Req, Res>, | ||
next: NextFn | ||
) => any | ||
export type ServiceHandlerServerStream<Req, Res> = ( | ||
call: ProtoCatContext<Res, CallType.SERVER_STREAM> & | ||
grpc.ServerWritableStream<Req, Res>, | ||
next: NextFn | ||
) => any | ||
export type ServiceHandlerClientStream<Req, Res> = ( | ||
call: ProtoCatContext<Res, CallType.CLIENT_STREAM> & | ||
grpc.ServerReadableStream<Req, Res>, | ||
next: NextFn | ||
) => any | ||
export type ServiceHandlerBidi<Req, Res> = ( | ||
call: ProtoCatContext<Res, CallType.BIDI> & grpc.ServerDuplexStream<Req, Res>, | ||
next: NextFn | ||
) => any | ||
|
||
export type Middleware = | ||
| ServiceHandlerUnary<any, any> | ||
| ServiceHandlerServerStream<any, any> | ||
| ServiceHandlerClientStream<any, any> | ||
| ServiceHandlerBidi<any, any> | ||
|
||
/** Convert a single method definition to service handler type */ | ||
type MethodDef2ServiceHandler<H> = H extends grpc.MethodDefinition< | ||
infer Request, | ||
infer Res | ||
> | ||
? H['requestStream'] extends true | ||
? H['responseStream'] extends true | ||
? ServiceHandlerBidi<Request, Res> | ||
: ServiceHandlerClientStream<Request, Res> | ||
: H['responseStream'] extends true | ||
? ServiceHandlerServerStream<Request, Res> | ||
: ServiceHandlerUnary<Request, Res> | ||
: never | ||
|
||
/** Create service handler type for whole client definition */ | ||
export type ServiceImplementation<T> = RemoveIdxSgn< | ||
{ [M in keyof T]: MethodDef2ServiceHandler<T[M]> } | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as grpc from '@grpc/grpc-js' | ||
|
||
/** Promise wrapper for callback tryShutdown */ | ||
export const tryShutdown = (server: grpc.Server) => | ||
new Promise((resolve, reject) => | ||
server.tryShutdown(err => (err ? reject(err) : resolve())) | ||
) | ||
|
||
/** Promise wrapper for callback bindAsync */ | ||
export const bindAsync = ( | ||
server: grpc.Server, | ||
address: string, | ||
creds: grpc.ServerCredentials | ||
) => | ||
new Promise<number>((resolve, reject) => | ||
server.bindAsync(address, creds, (err, port) => | ||
err ? reject(err) : resolve(port) | ||
) | ||
) |
Oops, something went wrong.