├── .github ├── CODEOWNERS ├── dependabot.yml └── workflows │ └── test.yml ├── README.md └── examples ├── gradle-ts-protoc-gen ├── .gitattributes ├── .gitignore ├── .npmrc ├── .prettierignore ├── .vscode │ ├── extensions.json │ └── settings.json ├── README.md ├── build.gradle ├── client.ts ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── package-lock.json ├── package.json ├── proto │ ├── example.proto │ ├── example_grpc_pb.d.ts │ ├── example_grpc_pb.js │ ├── example_pb.d.ts │ └── example_pb.js ├── server.ts ├── settings.gradle └── tsconfig.json ├── grpc-proto-loader ├── .gitignore ├── .npmrc ├── .nvmrc ├── .prettierignore ├── .vscode │ ├── extensions.json │ └── settings.json ├── README.md ├── client.ts ├── package-lock.json ├── package.json ├── proto │ ├── example.proto │ ├── example.ts │ └── example_package │ │ ├── ClientMessage.ts │ │ ├── Example.ts │ │ └── ServerMessage.ts ├── server.ts └── tsconfig.json ├── grpc-web ├── .gitignore ├── .npmrc ├── .prettierignore ├── .vscode │ ├── extensions.json │ └── settings.json ├── README.md ├── client.ts ├── compile-proto.sh ├── envoy.yaml ├── index.html ├── package-lock.json ├── package.json ├── proto │ └── chat.proto ├── server.ts ├── tsconfig.json └── webpack.config.ts ├── grpc_tools_node_protoc_ts ├── .gitignore ├── .npmrc ├── .prettierignore ├── .vscode │ ├── extensions.json │ └── settings.json ├── README.md ├── client.ts ├── compile-proto.sh ├── package-lock.json ├── package.json ├── proto │ ├── example.proto │ ├── example_grpc_pb.d.ts │ ├── example_grpc_pb.js │ ├── example_pb.d.ts │ └── example_pb.js ├── server.ts └── tsconfig.json └── ts-protoc-gen ├── .gitignore ├── .npmrc ├── .prettierignore ├── .vscode ├── extensions.json └── settings.json ├── README.md ├── client.ts ├── compile-proto.sh ├── package-lock.json ├── package.json ├── proto ├── example.proto ├── example_grpc_pb.d.ts ├── example_grpc_pb.js ├── example_pb.d.ts └── example_pb.js ├── server.ts └── tsconfig.json /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @badsyntax 2 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: github-actions 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-20.04 12 | name: Build examples 13 | steps: 14 | - uses: actions/checkout@v4.2.2 15 | - uses: actions/setup-node@v3.4.1 16 | with: 17 | node-version: 16 18 | - uses: actions/setup-java@v4.5.0 19 | with: 20 | java-version: 15 21 | architecture: x64 22 | distribution: zulu 23 | - name: Build gradle-ts-protoc-gen 24 | run: | 25 | cd examples/gradle-ts-protoc-gen 26 | npm i 27 | npm run build 28 | - name: Build grpc_tools_node_protoc_ts 29 | run: | 30 | cd examples/grpc_tools_node_protoc_ts 31 | npm i 32 | npm run build 33 | - name: Build grpc-proto-loader 34 | run: | 35 | cd examples/grpc-proto-loader 36 | npm i 37 | npm run build 38 | - name: Build grpc-web 39 | run: | 40 | cd examples/grpc-web 41 | npm i 42 | npm run build 43 | - name: Build ts-protoc-gen 44 | run: | 45 | cd examples/ts-protoc-gen 46 | npm i 47 | npm run build 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gRPC TypeScript 2 | 3 | [![Test](https://github.com/badsyntax/grpc-js-typescript/actions/workflows/test.yml/badge.svg)](https://github.com/badsyntax/grpc-js-typescript/actions/workflows/test.yml) 4 | 5 | This repo shows how to use gRPC with TypeScript & Node.js. 6 | 7 | In all cases, [`@grpc/grpc-js`](https://www.npmjs.com/package/@grpc/grpc-js) is used as [`grpc`](https://www.npmjs.com/package/grpc) is now [effectively deprecated](https://grpc.io/blog/grpc-js-1.0/). 8 | 9 | TypeScript types are generated at build time from the proto files using different tools as demonstrated in this repo. 10 | 11 | ## Examples 12 | 13 | Each example is a separate application. Refer to the README in each project for more information. 14 | 15 | - [grpc-proto-loader](./examples/grpc-proto-loader) 16 | - [ts-protoc-gen](./examples/ts-protoc-gen) 17 | - [gradle-ts-protoc-gen](./examples/gradle-ts-protoc-gen) 18 | - [grpc_tools_node_protoc_ts](./examples/grpc_tools_node_protoc_ts) 19 | - [grpc-web](./examples/grpc-web) 20 | 21 | ## Type Parity 22 | 23 | Thanks to [this issue](https://github.com/agreatfool/grpc_tools_node_protoc_ts/issues/79) being fixed, we now have type parity across the different type generator packages. 🎉 24 | 25 | ## Contributions 26 | 27 |
Contributions I made to help with gRPC & TypeScript support 28 | 29 | - https://github.com/grpc/grpc-node/pull/1368 30 | - https://github.com/grpc/grpc-node/pull/1380 31 | - https://github.com/grpc/grpc-node/pull/1387 32 | - https://github.com/grpc/grpc-node/pull/1419 33 | - https://github.com/grpc/grpc-node/pull/1454 34 | - https://github.com/grpc/grpc-node/pull/1587 35 | - https://github.com/grpc/grpc-node/pull/1590 36 | - https://github.com/murgatroid99/grpc-node/pull/1 37 | - https://github.com/murgatroid99/grpc-node/pull/2 38 | - https://github.com/murgatroid99/grpc-node/pull/3 39 | - https://github.com/improbable-eng/ts-protoc-gen/pull/236 40 | - https://github.com/improbable-eng/ts-protoc-gen/pull/247 41 | 42 |
43 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/.gitattributes: -------------------------------------------------------------------------------- 1 | # 2 | # https://help.github.com/articles/dealing-with-line-endings/ 3 | # 4 | # These are explicitly windows files and should use crlf 5 | *.bat text eol=crlf 6 | 7 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | **/*.js 3 | !proto/*.js 4 | 5 | # Ignore Gradle project-specific cache directory 6 | .gradle 7 | 8 | # Ignore Gradle build output directory 9 | build 10 | 11 | # Eclipse files 12 | .settings 13 | .classpath 14 | .project 15 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/.npmrc: -------------------------------------------------------------------------------- 1 | registry=https://registry.npmjs.org/ -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/.prettierignore: -------------------------------------------------------------------------------- 1 | package-lock.json 2 | proto/*.ts 3 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "dbaeumer.vscode-eslint", 4 | "EditorConfig.EditorConfig", 5 | "esbenp.prettier-vscode", 6 | "streetsidesoftware.code-spell-checker", 7 | "zxh404.vscode-proto3" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsc.autoDetect": "off", 3 | "typescript.updateImportsOnFileMove.enabled": "always", 4 | "files.insertFinalNewline": true, 5 | "files.trimTrailingWhitespace": true, 6 | "eslint.validate": ["javascript", "typescript"], 7 | "editor.formatOnSave": false, 8 | "editor.codeActionsOnSave": { 9 | "source.fixAll.eslint": true, 10 | "source.organizeImports": false 11 | }, 12 | "[markdown]": { 13 | "editor.defaultFormatter": "esbenp.prettier-vscode" 14 | }, 15 | "[json]": { 16 | "editor.defaultFormatter": "esbenp.prettier-vscode" 17 | }, 18 | "[yaml]": { 19 | "editor.defaultFormatter": "esbenp.prettier-vscode" 20 | }, 21 | "cSpell.language": "en-GB", 22 | "cSpell.words": [ 23 | "grpc" 24 | ], 25 | "java.configuration.updateBuildConfiguration": "automatic" 26 | } 27 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/README.md: -------------------------------------------------------------------------------- 1 | # gradle-ts-protoc-gen example 2 | 3 | This examples shows how to uses [Gradle](https://gradle.org/), the [proto compiler](https://www.npmjs.com/package/grpc-tools), [`ts-protoc-gen`](https://www.npmjs.com/package/ts-protoc-gen) & [`@grpc/grpc-js`](https://www.npmjs.com/package/@grpc/grpc-js) to build a fully typed gRPC application that runs on Node.js. 4 | 5 | The [proto compiler](https://www.npmjs.com/package/grpc-tools) and [`ts-protoc-gen`](https://www.npmjs.com/package/ts-protoc-gen) compiler plugin are used to generate JavaScript & TypeScript files from the proto definitions. The [`protobuf-gradle-plugin`](https://github.com/google/protobuf-gradle-plugin) Gradle plugin is used to run the compiler from Gradle. 6 | 7 | [`google-protobuf`](https://www.npmjs.com/package/google-protobuf) & [`@grpc/grpc-js`](https://www.npmjs.com/package/@grpc/grpc-js) are used at runtime. 8 | 9 | ## App layout 10 | 11 | - [package.json](./package.json) - Dependencies and node build scripts 12 | - [build.gradle](./build.gradle) - The Gradle build file 13 | - [proto/](./proto/) - Protobuf definitions and generated types 14 | - [server.ts](./server.ts) - The grpc server 15 | - [client.ts](./client.ts) - The grpc client 16 | 17 | ## Generating the Types 18 | 19 | Install dependencies: 20 | 21 | ```sh 22 | npm install 23 | 24 | # Apple M1 users should install for x64 due to grpc-tools not supporting arm64 25 | npm i --target_arch=x64 26 | ``` 27 | 28 | Use Gradle & [`ts-protoc-gen`](https://www.npmjs.com/package/ts-protoc-gen) to generate the TypeScript files: 29 | 30 | ```sh 31 | ./gradlew generateProto 32 | ``` 33 | 34 | This is aliased as a npm script: 35 | 36 | ```sh 37 | npm run build:proto 38 | ``` 39 | 40 | ### Running the App 41 | 42 | This simple app demonstrates the different gRPC calls you can perform. 43 | 44 | First generated the types and build the application files: 45 | 46 | ```sh 47 | npm run build 48 | ``` 49 | 50 | Start the server: 51 | 52 | ```sh 53 | npm run start:server 54 | ``` 55 | 56 | Now run the client by specifying which example you want to run: 57 | 58 | ```bash 59 | npm run start:client -- --unary 60 | npm run start:client -- --server-streaming 61 | npm run start:client -- --client-streaming 62 | npm run start:client -- --bidi-streaming 63 | ``` 64 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/build.gradle: -------------------------------------------------------------------------------- 1 | 2 | plugins { 3 | id 'java' 4 | id 'com.google.protobuf' version '0.8.16' 5 | id 'com.github.jlouns.cpe' version '0.5.0' 6 | } 7 | 8 | project.ext.set('protobufVersion', '3.16.0') 9 | project.ext.set('protocVersion', project.protobufVersion) 10 | 11 | def isWindows = System.getProperty('os.name').toLowerCase().contains('windows') 12 | 13 | repositories { 14 | mavenCentral() 15 | } 16 | 17 | sourceSets { 18 | main { 19 | proto { 20 | srcDir file('./proto') 21 | } 22 | } 23 | } 24 | 25 | protobuf { 26 | protoc { 27 | if (osdetector.os == 'osx') { 28 | // Required for arm64 support on MacOS 29 | artifact = "com.google.protobuf:protoc:${protocVersion}:osx-x86_64" 30 | } else { 31 | artifact = "com.google.protobuf:protoc:${protocVersion}" 32 | } 33 | } 34 | plugins { 35 | grpc { 36 | path = file( 37 | './node_modules/.bin/grpc_tools_node_protoc_plugin' + (isWindows ? '.cmd' : '') 38 | ) 39 | } 40 | ts { 41 | path = file( 42 | './node_modules/.bin/protoc-gen-ts' + (isWindows ? '.cmd' : '') 43 | ) 44 | } 45 | } 46 | generateProtoTasks { 47 | generateProto.finalizedBy copyGeneratedProto 48 | all().each { task -> 49 | task.plugins { 50 | grpc { 51 | outputSubDir = 'js' 52 | option 'grpc_js' 53 | } 54 | ts { 55 | option 'service=grpc-node,mode=grpc-js' 56 | } 57 | } 58 | task.builtins { 59 | remove java 60 | js { 61 | option 'import_style=commonjs' 62 | } 63 | } 64 | task.dependsOn npmInstall 65 | } 66 | } 67 | } 68 | 69 | task copyGeneratedProto(type: Copy) { 70 | from "$protobuf.generatedFilesBaseDir/main/ts", "$protobuf.generatedFilesBaseDir/main/js" 71 | into 'proto' 72 | } 73 | 74 | task npmInstall(type: CrossPlatformExec) { 75 | description 'Installs node dependencies' 76 | inputs.file('package-lock.json').withPathSensitivity(PathSensitivity.RELATIVE) 77 | outputs.dir('node_modules') 78 | outputs.cacheIf { true } 79 | commandLine 'npm', 'install' 80 | } 81 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/client.ts: -------------------------------------------------------------------------------- 1 | import * as grpc from '@grpc/grpc-js'; 2 | import { ExampleClient } from './proto/example_grpc_pb'; 3 | import { ClientMessage, ServerMessage } from './proto/example_pb'; 4 | 5 | const host = '0.0.0.0:9090'; 6 | 7 | const client = new ExampleClient(host, grpc.credentials.createInsecure()); 8 | 9 | const deadline = new Date(); 10 | deadline.setSeconds(deadline.getSeconds() + 5); 11 | client.waitForReady(deadline, (error?: Error) => { 12 | if (error) { 13 | console.log(`Client connect error: ${error.message}`); 14 | } else { 15 | onClientReady(); 16 | } 17 | }); 18 | 19 | function onClientReady() { 20 | switch (process.argv[process.argv.length - 1]) { 21 | case '--unary': 22 | doUnaryCall(); 23 | break; 24 | case '--server-streaming': 25 | doServerStreamingCall(); 26 | break; 27 | case '--client-streaming': 28 | doClientStreamingCall(); 29 | break; 30 | case '--bidi-streaming': 31 | doBidirectionalStreamingCall(); 32 | break; 33 | default: 34 | throw new Error('Example not specified'); 35 | } 36 | } 37 | 38 | function doUnaryCall() { 39 | const clientMessage = new ClientMessage(); 40 | clientMessage.setClientMessage('Message from client'); 41 | client.unaryCall( 42 | clientMessage, 43 | (error: grpc.ServiceError | null, serverMessage?: ServerMessage) => { 44 | if (error) { 45 | console.error(error.message); 46 | } else if (serverMessage) { 47 | console.log( 48 | `(client) Got server message: ${serverMessage.getServerMessage()}` 49 | ); 50 | } 51 | } 52 | ); 53 | } 54 | 55 | function doServerStreamingCall() { 56 | const clientMessage = new ClientMessage(); 57 | clientMessage.setClientMessage('Message from client'); 58 | const stream = client.serverStreamingCall(clientMessage); 59 | stream.on('data', (serverMessage: ServerMessage) => { 60 | console.log( 61 | `(client) Got server message: ${serverMessage.getServerMessage()}` 62 | ); 63 | }); 64 | } 65 | 66 | function doClientStreamingCall() { 67 | const stream = client.clientStreamingCall( 68 | (error: grpc.ServiceError | null) => { 69 | if (error) { 70 | console.error(error.message); 71 | } 72 | } 73 | ); 74 | const clientMessage = new ClientMessage(); 75 | clientMessage.setClientMessage('Message from client'); 76 | stream.write(clientMessage); 77 | } 78 | 79 | function doBidirectionalStreamingCall() { 80 | const stream = client.bidirectionalStreamingCall(); 81 | 82 | // Server stream 83 | stream.on('data', (serverMessage: ServerMessage) => { 84 | console.log( 85 | `(client) Got server message: ${serverMessage.getServerMessage()}` 86 | ); 87 | }); 88 | 89 | // Client stream 90 | const clientMessage = new ClientMessage(); 91 | clientMessage.setClientMessage('Message from client'); 92 | stream.write(clientMessage); 93 | } 94 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/badsyntax/grpc-js-typescript/349e6d2f96819af2afb1f6a001e9e728534ed703/examples/gradle-ts-protoc-gen/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gradle-ts-protoc-gen", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "npm run build:typescript", 8 | "build:typescript": "tsc -p .", 9 | "build:proto": "./gradlew generateProto", 10 | "watch": "npm run build:typescript -- -w", 11 | "prewatch": "npm run build:proto", 12 | "prebuild": "npm run build:proto", 13 | "lint": "npm run lint:prettier && npm run lint:eslint", 14 | "lint:prettier": "prettier --check \"**/*.{ts,js,json,svg,md,yml}\"", 15 | "lint:eslint": "eslint . --ext .js,.ts", 16 | "lint:fix": "npm run lint:fix:prettier && npm run lint:eslint -- --fix", 17 | "lint:fix:prettier": "prettier --write '**/*.{ts,tsx,js,json,svg,md,yml}'", 18 | "start:server": "node server", 19 | "start:client": "node client" 20 | }, 21 | "keywords": [], 22 | "author": "", 23 | "license": "ISC", 24 | "dependencies": { 25 | "@grpc/grpc-js": "^1.6.8", 26 | "google-protobuf": "^3.21.0" 27 | }, 28 | "devDependencies": { 29 | "@tsconfig/node12": "^1.0.11", 30 | "@types/google-protobuf": "^3.15.6", 31 | "@types/node": "^15.0.3", 32 | "@typescript-eslint/eslint-plugin": "^5.31.0", 33 | "@typescript-eslint/parser": "^5.31.0", 34 | "eslint": "^8.20.0", 35 | "eslint-config-prettier": "^8.5.0", 36 | "eslint-plugin-node": "^11.1.0", 37 | "eslint-plugin-prettier": "^4.2.1", 38 | "grpc-tools": "^1.11.2", 39 | "prettier": "^2.7.1", 40 | "ts-protoc-gen": "^0.15.0", 41 | "typescript": "^4.7.4" 42 | }, 43 | "eslintConfig": { 44 | "ignorePatterns": [ 45 | "**/*.js", 46 | "proto/*.ts" 47 | ], 48 | "env": { 49 | "browser": false, 50 | "es6": true, 51 | "node": true 52 | }, 53 | "parserOptions": { 54 | "project": "./tsconfig.json", 55 | "ecmaVersion": 2018, 56 | "sourceType": "module" 57 | }, 58 | "extends": [ 59 | "plugin:@typescript-eslint/recommended", 60 | "plugin:prettier/recommended" 61 | ] 62 | }, 63 | "prettier": { 64 | "singleQuote": true, 65 | "tabWidth": 2, 66 | "printWidth": 80, 67 | "useTabs": false 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/proto/example.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package example_package; 4 | 5 | message ServerMessage { 6 | string server_message = 1; 7 | } 8 | 9 | message ClientMessage { 10 | string client_message = 1; 11 | } 12 | 13 | service Example { 14 | rpc unaryCall(ClientMessage) returns (ServerMessage) {} 15 | rpc serverStreamingCall(ClientMessage) returns (stream ServerMessage) {} 16 | rpc clientStreamingCall(stream ClientMessage) returns (ServerMessage) {} 17 | rpc bidirectionalStreamingCall(stream ClientMessage) returns (stream ServerMessage) {} 18 | } 19 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/proto/example_grpc_pb.d.ts: -------------------------------------------------------------------------------- 1 | // GENERATED CODE -- DO NOT EDIT! 2 | 3 | // package: example_package 4 | // file: example.proto 5 | 6 | import * as example_pb from "./example_pb"; 7 | import * as grpc from "@grpc/grpc-js"; 8 | 9 | interface IExampleService extends grpc.ServiceDefinition { 10 | unaryCall: grpc.MethodDefinition; 11 | serverStreamingCall: grpc.MethodDefinition; 12 | clientStreamingCall: grpc.MethodDefinition; 13 | bidirectionalStreamingCall: grpc.MethodDefinition; 14 | } 15 | 16 | export const ExampleService: IExampleService; 17 | 18 | export interface IExampleServer extends grpc.UntypedServiceImplementation { 19 | unaryCall: grpc.handleUnaryCall; 20 | serverStreamingCall: grpc.handleServerStreamingCall; 21 | clientStreamingCall: grpc.handleClientStreamingCall; 22 | bidirectionalStreamingCall: grpc.handleBidiStreamingCall; 23 | } 24 | 25 | export class ExampleClient extends grpc.Client { 26 | constructor(address: string, credentials: grpc.ChannelCredentials, options?: object); 27 | unaryCall(argument: example_pb.ClientMessage, callback: grpc.requestCallback): grpc.ClientUnaryCall; 28 | unaryCall(argument: example_pb.ClientMessage, metadataOrOptions: grpc.Metadata | grpc.CallOptions | null, callback: grpc.requestCallback): grpc.ClientUnaryCall; 29 | unaryCall(argument: example_pb.ClientMessage, metadata: grpc.Metadata | null, options: grpc.CallOptions | null, callback: grpc.requestCallback): grpc.ClientUnaryCall; 30 | serverStreamingCall(argument: example_pb.ClientMessage, metadataOrOptions?: grpc.Metadata | grpc.CallOptions | null): grpc.ClientReadableStream; 31 | serverStreamingCall(argument: example_pb.ClientMessage, metadata?: grpc.Metadata | null, options?: grpc.CallOptions | null): grpc.ClientReadableStream; 32 | clientStreamingCall(callback: grpc.requestCallback): grpc.ClientWritableStream; 33 | clientStreamingCall(metadataOrOptions: grpc.Metadata | grpc.CallOptions | null, callback: grpc.requestCallback): grpc.ClientWritableStream; 34 | clientStreamingCall(metadata: grpc.Metadata | null, options: grpc.CallOptions | null, callback: grpc.requestCallback): grpc.ClientWritableStream; 35 | bidirectionalStreamingCall(metadataOrOptions?: grpc.Metadata | grpc.CallOptions | null): grpc.ClientDuplexStream; 36 | bidirectionalStreamingCall(metadata?: grpc.Metadata | null, options?: grpc.CallOptions | null): grpc.ClientDuplexStream; 37 | } 38 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/proto/example_grpc_pb.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE -- DO NOT EDIT! 2 | 3 | 'use strict'; 4 | var grpc = require('@grpc/grpc-js'); 5 | var example_pb = require('./example_pb.js'); 6 | 7 | function serialize_example_package_ClientMessage(arg) { 8 | if (!(arg instanceof example_pb.ClientMessage)) { 9 | throw new Error('Expected argument of type example_package.ClientMessage'); 10 | } 11 | return Buffer.from(arg.serializeBinary()); 12 | } 13 | 14 | function deserialize_example_package_ClientMessage(buffer_arg) { 15 | return example_pb.ClientMessage.deserializeBinary(new Uint8Array(buffer_arg)); 16 | } 17 | 18 | function serialize_example_package_ServerMessage(arg) { 19 | if (!(arg instanceof example_pb.ServerMessage)) { 20 | throw new Error('Expected argument of type example_package.ServerMessage'); 21 | } 22 | return Buffer.from(arg.serializeBinary()); 23 | } 24 | 25 | function deserialize_example_package_ServerMessage(buffer_arg) { 26 | return example_pb.ServerMessage.deserializeBinary(new Uint8Array(buffer_arg)); 27 | } 28 | 29 | 30 | var ExampleService = exports.ExampleService = { 31 | unaryCall: { 32 | path: '/example_package.Example/unaryCall', 33 | requestStream: false, 34 | responseStream: false, 35 | requestType: example_pb.ClientMessage, 36 | responseType: example_pb.ServerMessage, 37 | requestSerialize: serialize_example_package_ClientMessage, 38 | requestDeserialize: deserialize_example_package_ClientMessage, 39 | responseSerialize: serialize_example_package_ServerMessage, 40 | responseDeserialize: deserialize_example_package_ServerMessage, 41 | }, 42 | serverStreamingCall: { 43 | path: '/example_package.Example/serverStreamingCall', 44 | requestStream: false, 45 | responseStream: true, 46 | requestType: example_pb.ClientMessage, 47 | responseType: example_pb.ServerMessage, 48 | requestSerialize: serialize_example_package_ClientMessage, 49 | requestDeserialize: deserialize_example_package_ClientMessage, 50 | responseSerialize: serialize_example_package_ServerMessage, 51 | responseDeserialize: deserialize_example_package_ServerMessage, 52 | }, 53 | clientStreamingCall: { 54 | path: '/example_package.Example/clientStreamingCall', 55 | requestStream: true, 56 | responseStream: false, 57 | requestType: example_pb.ClientMessage, 58 | responseType: example_pb.ServerMessage, 59 | requestSerialize: serialize_example_package_ClientMessage, 60 | requestDeserialize: deserialize_example_package_ClientMessage, 61 | responseSerialize: serialize_example_package_ServerMessage, 62 | responseDeserialize: deserialize_example_package_ServerMessage, 63 | }, 64 | bidirectionalStreamingCall: { 65 | path: '/example_package.Example/bidirectionalStreamingCall', 66 | requestStream: true, 67 | responseStream: true, 68 | requestType: example_pb.ClientMessage, 69 | responseType: example_pb.ServerMessage, 70 | requestSerialize: serialize_example_package_ClientMessage, 71 | requestDeserialize: deserialize_example_package_ClientMessage, 72 | responseSerialize: serialize_example_package_ServerMessage, 73 | responseDeserialize: deserialize_example_package_ServerMessage, 74 | }, 75 | }; 76 | 77 | exports.ExampleClient = grpc.makeGenericClientConstructor(ExampleService); 78 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/proto/example_pb.d.ts: -------------------------------------------------------------------------------- 1 | // package: example_package 2 | // file: example.proto 3 | 4 | import * as jspb from "google-protobuf"; 5 | 6 | export class ServerMessage extends jspb.Message { 7 | getServerMessage(): string; 8 | setServerMessage(value: string): void; 9 | 10 | serializeBinary(): Uint8Array; 11 | toObject(includeInstance?: boolean): ServerMessage.AsObject; 12 | static toObject(includeInstance: boolean, msg: ServerMessage): ServerMessage.AsObject; 13 | static extensions: {[key: number]: jspb.ExtensionFieldInfo}; 14 | static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo}; 15 | static serializeBinaryToWriter(message: ServerMessage, writer: jspb.BinaryWriter): void; 16 | static deserializeBinary(bytes: Uint8Array): ServerMessage; 17 | static deserializeBinaryFromReader(message: ServerMessage, reader: jspb.BinaryReader): ServerMessage; 18 | } 19 | 20 | export namespace ServerMessage { 21 | export type AsObject = { 22 | serverMessage: string, 23 | } 24 | } 25 | 26 | export class ClientMessage extends jspb.Message { 27 | getClientMessage(): string; 28 | setClientMessage(value: string): void; 29 | 30 | serializeBinary(): Uint8Array; 31 | toObject(includeInstance?: boolean): ClientMessage.AsObject; 32 | static toObject(includeInstance: boolean, msg: ClientMessage): ClientMessage.AsObject; 33 | static extensions: {[key: number]: jspb.ExtensionFieldInfo}; 34 | static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo}; 35 | static serializeBinaryToWriter(message: ClientMessage, writer: jspb.BinaryWriter): void; 36 | static deserializeBinary(bytes: Uint8Array): ClientMessage; 37 | static deserializeBinaryFromReader(message: ClientMessage, reader: jspb.BinaryReader): ClientMessage; 38 | } 39 | 40 | export namespace ClientMessage { 41 | export type AsObject = { 42 | clientMessage: string, 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/proto/example_pb.js: -------------------------------------------------------------------------------- 1 | // source: example.proto 2 | /** 3 | * @fileoverview 4 | * @enhanceable 5 | * @suppress {missingRequire} reports error on implicit type usages. 6 | * @suppress {messageConventions} JS Compiler reports an error if a variable or 7 | * field starts with 'MSG_' and isn't a translatable message. 8 | * @public 9 | */ 10 | // GENERATED CODE -- DO NOT EDIT! 11 | /* eslint-disable */ 12 | // @ts-nocheck 13 | 14 | var jspb = require('google-protobuf'); 15 | var goog = jspb; 16 | var global = Function('return this')(); 17 | 18 | goog.exportSymbol('proto.example_package.ClientMessage', null, global); 19 | goog.exportSymbol('proto.example_package.ServerMessage', null, global); 20 | /** 21 | * Generated by JsPbCodeGenerator. 22 | * @param {Array=} opt_data Optional initial data array, typically from a 23 | * server response, or constructed directly in Javascript. The array is used 24 | * in place and becomes part of the constructed object. It is not cloned. 25 | * If no data is provided, the constructed object will be empty, but still 26 | * valid. 27 | * @extends {jspb.Message} 28 | * @constructor 29 | */ 30 | proto.example_package.ServerMessage = function(opt_data) { 31 | jspb.Message.initialize(this, opt_data, 0, -1, null, null); 32 | }; 33 | goog.inherits(proto.example_package.ServerMessage, jspb.Message); 34 | if (goog.DEBUG && !COMPILED) { 35 | /** 36 | * @public 37 | * @override 38 | */ 39 | proto.example_package.ServerMessage.displayName = 'proto.example_package.ServerMessage'; 40 | } 41 | /** 42 | * Generated by JsPbCodeGenerator. 43 | * @param {Array=} opt_data Optional initial data array, typically from a 44 | * server response, or constructed directly in Javascript. The array is used 45 | * in place and becomes part of the constructed object. It is not cloned. 46 | * If no data is provided, the constructed object will be empty, but still 47 | * valid. 48 | * @extends {jspb.Message} 49 | * @constructor 50 | */ 51 | proto.example_package.ClientMessage = function(opt_data) { 52 | jspb.Message.initialize(this, opt_data, 0, -1, null, null); 53 | }; 54 | goog.inherits(proto.example_package.ClientMessage, jspb.Message); 55 | if (goog.DEBUG && !COMPILED) { 56 | /** 57 | * @public 58 | * @override 59 | */ 60 | proto.example_package.ClientMessage.displayName = 'proto.example_package.ClientMessage'; 61 | } 62 | 63 | 64 | 65 | if (jspb.Message.GENERATE_TO_OBJECT) { 66 | /** 67 | * Creates an object representation of this proto. 68 | * Field names that are reserved in JavaScript and will be renamed to pb_name. 69 | * Optional fields that are not set will be set to undefined. 70 | * To access a reserved field use, foo.pb_, eg, foo.pb_default. 71 | * For the list of reserved names please see: 72 | * net/proto2/compiler/js/internal/generator.cc#kKeyword. 73 | * @param {boolean=} opt_includeInstance Deprecated. whether to include the 74 | * JSPB instance for transitional soy proto support: 75 | * http://goto/soy-param-migration 76 | * @return {!Object} 77 | */ 78 | proto.example_package.ServerMessage.prototype.toObject = function(opt_includeInstance) { 79 | return proto.example_package.ServerMessage.toObject(opt_includeInstance, this); 80 | }; 81 | 82 | 83 | /** 84 | * Static version of the {@see toObject} method. 85 | * @param {boolean|undefined} includeInstance Deprecated. Whether to include 86 | * the JSPB instance for transitional soy proto support: 87 | * http://goto/soy-param-migration 88 | * @param {!proto.example_package.ServerMessage} msg The msg instance to transform. 89 | * @return {!Object} 90 | * @suppress {unusedLocalVariables} f is only used for nested messages 91 | */ 92 | proto.example_package.ServerMessage.toObject = function(includeInstance, msg) { 93 | var f, obj = { 94 | serverMessage: jspb.Message.getFieldWithDefault(msg, 1, "") 95 | }; 96 | 97 | if (includeInstance) { 98 | obj.$jspbMessageInstance = msg; 99 | } 100 | return obj; 101 | }; 102 | } 103 | 104 | 105 | /** 106 | * Deserializes binary data (in protobuf wire format). 107 | * @param {jspb.ByteSource} bytes The bytes to deserialize. 108 | * @return {!proto.example_package.ServerMessage} 109 | */ 110 | proto.example_package.ServerMessage.deserializeBinary = function(bytes) { 111 | var reader = new jspb.BinaryReader(bytes); 112 | var msg = new proto.example_package.ServerMessage; 113 | return proto.example_package.ServerMessage.deserializeBinaryFromReader(msg, reader); 114 | }; 115 | 116 | 117 | /** 118 | * Deserializes binary data (in protobuf wire format) from the 119 | * given reader into the given message object. 120 | * @param {!proto.example_package.ServerMessage} msg The message object to deserialize into. 121 | * @param {!jspb.BinaryReader} reader The BinaryReader to use. 122 | * @return {!proto.example_package.ServerMessage} 123 | */ 124 | proto.example_package.ServerMessage.deserializeBinaryFromReader = function(msg, reader) { 125 | while (reader.nextField()) { 126 | if (reader.isEndGroup()) { 127 | break; 128 | } 129 | var field = reader.getFieldNumber(); 130 | switch (field) { 131 | case 1: 132 | var value = /** @type {string} */ (reader.readString()); 133 | msg.setServerMessage(value); 134 | break; 135 | default: 136 | reader.skipField(); 137 | break; 138 | } 139 | } 140 | return msg; 141 | }; 142 | 143 | 144 | /** 145 | * Serializes the message to binary data (in protobuf wire format). 146 | * @return {!Uint8Array} 147 | */ 148 | proto.example_package.ServerMessage.prototype.serializeBinary = function() { 149 | var writer = new jspb.BinaryWriter(); 150 | proto.example_package.ServerMessage.serializeBinaryToWriter(this, writer); 151 | return writer.getResultBuffer(); 152 | }; 153 | 154 | 155 | /** 156 | * Serializes the given message to binary data (in protobuf wire 157 | * format), writing to the given BinaryWriter. 158 | * @param {!proto.example_package.ServerMessage} message 159 | * @param {!jspb.BinaryWriter} writer 160 | * @suppress {unusedLocalVariables} f is only used for nested messages 161 | */ 162 | proto.example_package.ServerMessage.serializeBinaryToWriter = function(message, writer) { 163 | var f = undefined; 164 | f = message.getServerMessage(); 165 | if (f.length > 0) { 166 | writer.writeString( 167 | 1, 168 | f 169 | ); 170 | } 171 | }; 172 | 173 | 174 | /** 175 | * optional string server_message = 1; 176 | * @return {string} 177 | */ 178 | proto.example_package.ServerMessage.prototype.getServerMessage = function() { 179 | return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 1, "")); 180 | }; 181 | 182 | 183 | /** 184 | * @param {string} value 185 | * @return {!proto.example_package.ServerMessage} returns this 186 | */ 187 | proto.example_package.ServerMessage.prototype.setServerMessage = function(value) { 188 | return jspb.Message.setProto3StringField(this, 1, value); 189 | }; 190 | 191 | 192 | 193 | 194 | 195 | if (jspb.Message.GENERATE_TO_OBJECT) { 196 | /** 197 | * Creates an object representation of this proto. 198 | * Field names that are reserved in JavaScript and will be renamed to pb_name. 199 | * Optional fields that are not set will be set to undefined. 200 | * To access a reserved field use, foo.pb_, eg, foo.pb_default. 201 | * For the list of reserved names please see: 202 | * net/proto2/compiler/js/internal/generator.cc#kKeyword. 203 | * @param {boolean=} opt_includeInstance Deprecated. whether to include the 204 | * JSPB instance for transitional soy proto support: 205 | * http://goto/soy-param-migration 206 | * @return {!Object} 207 | */ 208 | proto.example_package.ClientMessage.prototype.toObject = function(opt_includeInstance) { 209 | return proto.example_package.ClientMessage.toObject(opt_includeInstance, this); 210 | }; 211 | 212 | 213 | /** 214 | * Static version of the {@see toObject} method. 215 | * @param {boolean|undefined} includeInstance Deprecated. Whether to include 216 | * the JSPB instance for transitional soy proto support: 217 | * http://goto/soy-param-migration 218 | * @param {!proto.example_package.ClientMessage} msg The msg instance to transform. 219 | * @return {!Object} 220 | * @suppress {unusedLocalVariables} f is only used for nested messages 221 | */ 222 | proto.example_package.ClientMessage.toObject = function(includeInstance, msg) { 223 | var f, obj = { 224 | clientMessage: jspb.Message.getFieldWithDefault(msg, 1, "") 225 | }; 226 | 227 | if (includeInstance) { 228 | obj.$jspbMessageInstance = msg; 229 | } 230 | return obj; 231 | }; 232 | } 233 | 234 | 235 | /** 236 | * Deserializes binary data (in protobuf wire format). 237 | * @param {jspb.ByteSource} bytes The bytes to deserialize. 238 | * @return {!proto.example_package.ClientMessage} 239 | */ 240 | proto.example_package.ClientMessage.deserializeBinary = function(bytes) { 241 | var reader = new jspb.BinaryReader(bytes); 242 | var msg = new proto.example_package.ClientMessage; 243 | return proto.example_package.ClientMessage.deserializeBinaryFromReader(msg, reader); 244 | }; 245 | 246 | 247 | /** 248 | * Deserializes binary data (in protobuf wire format) from the 249 | * given reader into the given message object. 250 | * @param {!proto.example_package.ClientMessage} msg The message object to deserialize into. 251 | * @param {!jspb.BinaryReader} reader The BinaryReader to use. 252 | * @return {!proto.example_package.ClientMessage} 253 | */ 254 | proto.example_package.ClientMessage.deserializeBinaryFromReader = function(msg, reader) { 255 | while (reader.nextField()) { 256 | if (reader.isEndGroup()) { 257 | break; 258 | } 259 | var field = reader.getFieldNumber(); 260 | switch (field) { 261 | case 1: 262 | var value = /** @type {string} */ (reader.readString()); 263 | msg.setClientMessage(value); 264 | break; 265 | default: 266 | reader.skipField(); 267 | break; 268 | } 269 | } 270 | return msg; 271 | }; 272 | 273 | 274 | /** 275 | * Serializes the message to binary data (in protobuf wire format). 276 | * @return {!Uint8Array} 277 | */ 278 | proto.example_package.ClientMessage.prototype.serializeBinary = function() { 279 | var writer = new jspb.BinaryWriter(); 280 | proto.example_package.ClientMessage.serializeBinaryToWriter(this, writer); 281 | return writer.getResultBuffer(); 282 | }; 283 | 284 | 285 | /** 286 | * Serializes the given message to binary data (in protobuf wire 287 | * format), writing to the given BinaryWriter. 288 | * @param {!proto.example_package.ClientMessage} message 289 | * @param {!jspb.BinaryWriter} writer 290 | * @suppress {unusedLocalVariables} f is only used for nested messages 291 | */ 292 | proto.example_package.ClientMessage.serializeBinaryToWriter = function(message, writer) { 293 | var f = undefined; 294 | f = message.getClientMessage(); 295 | if (f.length > 0) { 296 | writer.writeString( 297 | 1, 298 | f 299 | ); 300 | } 301 | }; 302 | 303 | 304 | /** 305 | * optional string client_message = 1; 306 | * @return {string} 307 | */ 308 | proto.example_package.ClientMessage.prototype.getClientMessage = function() { 309 | return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 1, "")); 310 | }; 311 | 312 | 313 | /** 314 | * @param {string} value 315 | * @return {!proto.example_package.ClientMessage} returns this 316 | */ 317 | proto.example_package.ClientMessage.prototype.setClientMessage = function(value) { 318 | return jspb.Message.setProto3StringField(this, 1, value); 319 | }; 320 | 321 | 322 | goog.object.extend(exports, proto.example_package); 323 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/server.ts: -------------------------------------------------------------------------------- 1 | import * as grpc from '@grpc/grpc-js'; 2 | import { ExampleService, IExampleServer } from './proto/example_grpc_pb'; 3 | import { ClientMessage, ServerMessage } from './proto/example_pb'; 4 | 5 | const host = '0.0.0.0:9090'; 6 | 7 | const exampleServer: IExampleServer = { 8 | unaryCall( 9 | call: grpc.ServerUnaryCall, 10 | callback: grpc.sendUnaryData 11 | ) { 12 | if (call.request) { 13 | console.log( 14 | `(server) Got client message: ${call.request.getClientMessage()}` 15 | ); 16 | } 17 | const serverMessage = new ServerMessage(); 18 | serverMessage.setServerMessage('Message from server'); 19 | callback(null, serverMessage); 20 | }, 21 | 22 | serverStreamingCall( 23 | call: grpc.ServerWritableStream 24 | ) { 25 | const serverMessage = new ServerMessage(); 26 | serverMessage.setServerMessage('Message from server'); 27 | call.write(serverMessage); 28 | }, 29 | 30 | clientStreamingCall( 31 | call: grpc.ServerReadableStream 32 | ) { 33 | call.on('data', (clientMessage: ClientMessage) => { 34 | console.log( 35 | `(server) Got client message: ${clientMessage.getClientMessage()}` 36 | ); 37 | }); 38 | }, 39 | 40 | bidirectionalStreamingCall( 41 | call: grpc.ServerDuplexStream 42 | ) { 43 | call.on('data', (clientMessage: ClientMessage) => { 44 | console.log( 45 | `(server) Got client message: ${clientMessage.getClientMessage()}` 46 | ); 47 | }); 48 | 49 | const serverMessage = new ServerMessage(); 50 | serverMessage.setServerMessage('Message from server'); 51 | call.write(serverMessage); 52 | }, 53 | }; 54 | 55 | function getServer(): grpc.Server { 56 | const server = new grpc.Server(); 57 | server.addService(ExampleService, exampleServer); 58 | return server; 59 | } 60 | 61 | if (require.main === module) { 62 | const server = getServer(); 63 | server.bindAsync( 64 | host, 65 | grpc.ServerCredentials.createInsecure(), 66 | (err: Error | null, port: number) => { 67 | if (err) { 68 | console.error(`Server error: ${err.message}`); 69 | } else { 70 | console.log(`Server bound on port: ${port}`); 71 | server.start(); 72 | } 73 | } 74 | ); 75 | } 76 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This file was generated by the Gradle 'init' task. 3 | * 4 | * The settings file is used to specify which projects to include in your build. 5 | * 6 | * Detailed information about configuring a multi-project build in Gradle can be found 7 | * in the user manual at https://docs.gradle.org/6.6.1/userguide/multi_project_builds.html 8 | */ 9 | 10 | rootProject.name = 'gradle-ts-protoc-gen' 11 | -------------------------------------------------------------------------------- /examples/gradle-ts-protoc-gen/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/node12/tsconfig.json", 3 | "exclude": ["node_modules"] 4 | } 5 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | generated 3 | *.js 4 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/.npmrc: -------------------------------------------------------------------------------- 1 | registry=https://registry.npmjs.org/ -------------------------------------------------------------------------------- /examples/grpc-proto-loader/.nvmrc: -------------------------------------------------------------------------------- 1 | 14 2 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/.prettierignore: -------------------------------------------------------------------------------- 1 | package-lock.json 2 | generated 3 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "dbaeumer.vscode-eslint", 4 | "EditorConfig.EditorConfig", 5 | "esbenp.prettier-vscode", 6 | "streetsidesoftware.code-spell-checker", 7 | "zxh404.vscode-proto3" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsc.autoDetect": "off", 3 | "typescript.updateImportsOnFileMove.enabled": "always", 4 | "files.insertFinalNewline": true, 5 | "files.trimTrailingWhitespace": true, 6 | "eslint.validate": ["javascript", "typescript"], 7 | "editor.codeActionsOnSave": { 8 | "source.fixAll.eslint": true 9 | }, 10 | "[markdown]": { 11 | "editor.defaultFormatter": "esbenp.prettier-vscode" 12 | }, 13 | "[json]": { 14 | "editor.defaultFormatter": "esbenp.prettier-vscode" 15 | }, 16 | "[yaml]": { 17 | "editor.defaultFormatter": "esbenp.prettier-vscode" 18 | }, 19 | "cSpell.language": "en-GB", 20 | "cSpell.words": ["grpc", "unary", "bidi"] 21 | } 22 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/README.md: -------------------------------------------------------------------------------- 1 | # grpc-proto-loader example 2 | 3 | This example shows how to use [`@grpc/proto-loader`](https://www.npmjs.com/package/@grpc/proto-loader) & [`@grpc/grpc-js`](https://www.npmjs.com/package/@grpc/grpc-js) to build a fully typed CLI application that runs on Node.js. 4 | 5 | [`@grpc/proto-loader`](https://www.npmjs.com/package/@grpc/proto-loader) to used to [generate the types](https://github.com/grpc/grpc-node/pull/1474), as well as load the protobuf files at runtime using [`protobuf.js`](https://www.npmjs.com/package/protobufjs) instead of [`google-protobuf`](https://www.npmjs.com/package/google-protobuf). 6 | 7 | _No proto compiler is used. It's all JavaScript._ 8 | 9 | ## App layout 10 | 11 | - [package.json](./package.json) - Dependencies and build scripts 12 | - [proto/](./proto/) - Protobuf definitions and generated types 13 | - [server.ts](./server.ts) - The grpc server 14 | - [client.ts](./client.ts) - The grpc client 15 | 16 | ## Generating the Types 17 | 18 | Install dependencies: 19 | 20 | ```sh 21 | npm install 22 | ``` 23 | 24 | Use [`@grpc/proto-loader`](https://www.npmjs.com/package/@grpc/proto-loader) to generate the TypeScript files: 25 | 26 | ```sh 27 | $(npm bin)/proto-loader-gen-types --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=proto/ proto/*.proto 28 | ``` 29 | 30 | This is aliased as a npm script: 31 | 32 | ```sh 33 | npm run build:proto 34 | ``` 35 | 36 | ### Running the App 37 | 38 | This simple app demonstrates the different gRPC calls you can perform. 39 | 40 | First generated the types and build the application files: 41 | 42 | ```sh 43 | npm run build 44 | ``` 45 | 46 | Start the server: 47 | 48 | ```sh 49 | npm run start:server 50 | ``` 51 | 52 | Now run the client by specifying which example you want to run: 53 | 54 | ```bash 55 | npm run start:client -- --unary 56 | npm run start:client -- --server-streaming 57 | npm run start:client -- --client-streaming 58 | npm run start:client -- --bidi-streaming 59 | ``` 60 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/client.ts: -------------------------------------------------------------------------------- 1 | import * as grpc from '@grpc/grpc-js'; 2 | import * as protoLoader from '@grpc/proto-loader'; 3 | import { ProtoGrpcType } from './proto/example'; 4 | import { ServerMessage } from './proto/example_package/ServerMessage'; 5 | 6 | const host = '0.0.0.0:9090'; 7 | const packageDefinition = protoLoader.loadSync('./proto/example.proto', { 8 | longs: String, 9 | enums: String, 10 | defaults: true, 11 | oneofs: true, 12 | }); 13 | const proto = grpc.loadPackageDefinition( 14 | packageDefinition 15 | ) as unknown as ProtoGrpcType; 16 | 17 | const client = new proto.example_package.Example( 18 | host, 19 | grpc.credentials.createInsecure() 20 | ); 21 | 22 | const deadline = new Date(); 23 | deadline.setSeconds(deadline.getSeconds() + 5); 24 | client.waitForReady(deadline, (error?: Error) => { 25 | if (error) { 26 | console.log(`Client connect error: ${error.message}`); 27 | } else { 28 | onClientReady(); 29 | } 30 | }); 31 | 32 | function onClientReady() { 33 | switch (process.argv[process.argv.length - 1]) { 34 | case '--unary': 35 | doUnaryCall(); 36 | break; 37 | case '--server-streaming': 38 | doServerStreamingCall(); 39 | break; 40 | case '--client-streaming': 41 | doClientStreamingCall(); 42 | break; 43 | case '--bidi-streaming': 44 | doBidirectionalStreamingCall(); 45 | break; 46 | default: 47 | throw new Error('Example not specified'); 48 | } 49 | } 50 | 51 | function doUnaryCall() { 52 | client.unaryCall( 53 | { 54 | clientMessage: 'Message from client', 55 | }, 56 | (error?: grpc.ServiceError | null, serverMessage?: ServerMessage) => { 57 | if (error) { 58 | console.error(error.message); 59 | } else if (serverMessage) { 60 | console.log( 61 | `(client) Got server message: ${serverMessage.serverMessage}` 62 | ); 63 | } 64 | } 65 | ); 66 | } 67 | 68 | function doServerStreamingCall() { 69 | const stream = client.serverStreamingCall({ 70 | clientMessage: 'Message from client', 71 | }); 72 | stream.on('data', (serverMessage: ServerMessage) => { 73 | console.log(`(client) Got server message: ${serverMessage.serverMessage}`); 74 | }); 75 | } 76 | 77 | function doClientStreamingCall() { 78 | const stream = client.clientStreamingCall((error?: grpc.ServiceError | null) => { 79 | if (error) { 80 | console.error(error.message); 81 | } 82 | }); 83 | stream.write({ 84 | clientMessage: 'Message from client', 85 | }); 86 | } 87 | 88 | function doBidirectionalStreamingCall() { 89 | const stream = client.bidirectionalStreamingCall(); 90 | 91 | // Server stream 92 | stream.on('data', (serverMessage: ServerMessage) => { 93 | console.log(`(client) Got server message: ${serverMessage.serverMessage}`); 94 | }); 95 | 96 | // Client stream 97 | stream.write({ 98 | clientMessage: 'Message from client', 99 | }); 100 | } 101 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grpc-proto-loader-example", 3 | "version": "1.0.0", 4 | "description": "", 5 | "keywords": [], 6 | "author": "", 7 | "license": "ISC", 8 | "bugs": { 9 | "url": "https://github.com/badsyntax/grpc-js-types/issues" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "https://github.com/badsyntax/grpc-js-types" 14 | }, 15 | "scripts": { 16 | "build": "npm run build:typescript", 17 | "build:typescript": "tsc -p .", 18 | "watch": "npm run build:typescript -- -w", 19 | "build:proto": "proto-loader-gen-types --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=proto/ proto/*.proto", 20 | "prewatch": "npm run build:proto", 21 | "prebuild": "npm run build:proto", 22 | "lint": "npm run lint:prettier && npm run lint:eslint", 23 | "lint:prettier": "prettier --check \"**/*.{ts,js,json,svg,md,yml}\"", 24 | "lint:eslint": "eslint . --ext .js,.ts", 25 | "lint:fix": "npm run lint:fix:prettier && npm run lint:eslint -- --fix", 26 | "lint:fix:prettier": "prettier --write '**/*.{ts,tsx,js,json,svg,md,yml}'", 27 | "start:server": "node server", 28 | "start:client": "node client" 29 | }, 30 | "dependencies": { 31 | "@grpc/grpc-js": "^1.6.8", 32 | "@grpc/proto-loader": "^0.7.0" 33 | }, 34 | "devDependencies": { 35 | "@tsconfig/node12": "^1.0.11", 36 | "@types/node": "^15.0.3", 37 | "@typescript-eslint/eslint-plugin": "^5.31.0", 38 | "@typescript-eslint/parser": "^5.31.0", 39 | "eslint": "^8.20.0", 40 | "eslint-config-prettier": "^8.5.0", 41 | "eslint-plugin-node": "^11.1.0", 42 | "eslint-plugin-prettier": "^4.2.1", 43 | "prettier": "^2.7.1", 44 | "typescript": "^4.7.4" 45 | }, 46 | "eslintConfig": { 47 | "ignorePatterns": [ 48 | "**/*.js", 49 | "proto/**/*.ts" 50 | ], 51 | "env": { 52 | "browser": false, 53 | "es6": true, 54 | "node": true 55 | }, 56 | "parserOptions": { 57 | "project": "./tsconfig.json", 58 | "ecmaVersion": 2018, 59 | "sourceType": "module" 60 | }, 61 | "extends": [ 62 | "plugin:@typescript-eslint/recommended", 63 | "plugin:prettier/recommended" 64 | ] 65 | }, 66 | "prettier": { 67 | "singleQuote": true, 68 | "tabWidth": 2, 69 | "printWidth": 80, 70 | "useTabs": false 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/proto/example.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package example_package; 4 | 5 | message ServerMessage { 6 | string server_message = 1; 7 | } 8 | 9 | message ClientMessage { 10 | string client_message = 1; 11 | } 12 | 13 | service Example { 14 | rpc unaryCall(ClientMessage) returns (ServerMessage) {} 15 | rpc serverStreamingCall(ClientMessage) returns (stream ServerMessage) {} 16 | rpc clientStreamingCall(stream ClientMessage) returns (ServerMessage) {} 17 | rpc bidirectionalStreamingCall(stream ClientMessage) returns (stream ServerMessage) {} 18 | } 19 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/proto/example.ts: -------------------------------------------------------------------------------- 1 | import type * as grpc from '@grpc/grpc-js'; 2 | import type { MessageTypeDefinition } from '@grpc/proto-loader'; 3 | 4 | import type { ExampleClient as _example_package_ExampleClient, ExampleDefinition as _example_package_ExampleDefinition } from './example_package/Example'; 5 | 6 | type SubtypeConstructor any, Subtype> = { 7 | new(...args: ConstructorParameters): Subtype; 8 | }; 9 | 10 | export interface ProtoGrpcType { 11 | example_package: { 12 | ClientMessage: MessageTypeDefinition 13 | Example: SubtypeConstructor & { service: _example_package_ExampleDefinition } 14 | ServerMessage: MessageTypeDefinition 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/proto/example_package/ClientMessage.ts: -------------------------------------------------------------------------------- 1 | // Original file: proto/example.proto 2 | 3 | 4 | export interface ClientMessage { 5 | 'clientMessage'?: (string); 6 | } 7 | 8 | export interface ClientMessage__Output { 9 | 'clientMessage': (string); 10 | } 11 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/proto/example_package/Example.ts: -------------------------------------------------------------------------------- 1 | // Original file: proto/example.proto 2 | 3 | import type * as grpc from '@grpc/grpc-js' 4 | import type { MethodDefinition } from '@grpc/proto-loader' 5 | import type { ClientMessage as _example_package_ClientMessage, ClientMessage__Output as _example_package_ClientMessage__Output } from '../example_package/ClientMessage'; 6 | import type { ServerMessage as _example_package_ServerMessage, ServerMessage__Output as _example_package_ServerMessage__Output } from '../example_package/ServerMessage'; 7 | 8 | export interface ExampleClient extends grpc.Client { 9 | bidirectionalStreamingCall(metadata: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientDuplexStream<_example_package_ClientMessage, _example_package_ServerMessage__Output>; 10 | bidirectionalStreamingCall(options?: grpc.CallOptions): grpc.ClientDuplexStream<_example_package_ClientMessage, _example_package_ServerMessage__Output>; 11 | bidirectionalStreamingCall(metadata: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientDuplexStream<_example_package_ClientMessage, _example_package_ServerMessage__Output>; 12 | bidirectionalStreamingCall(options?: grpc.CallOptions): grpc.ClientDuplexStream<_example_package_ClientMessage, _example_package_ServerMessage__Output>; 13 | 14 | clientStreamingCall(metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientWritableStream<_example_package_ClientMessage>; 15 | clientStreamingCall(metadata: grpc.Metadata, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientWritableStream<_example_package_ClientMessage>; 16 | clientStreamingCall(options: grpc.CallOptions, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientWritableStream<_example_package_ClientMessage>; 17 | clientStreamingCall(callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientWritableStream<_example_package_ClientMessage>; 18 | clientStreamingCall(metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientWritableStream<_example_package_ClientMessage>; 19 | clientStreamingCall(metadata: grpc.Metadata, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientWritableStream<_example_package_ClientMessage>; 20 | clientStreamingCall(options: grpc.CallOptions, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientWritableStream<_example_package_ClientMessage>; 21 | clientStreamingCall(callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientWritableStream<_example_package_ClientMessage>; 22 | 23 | serverStreamingCall(argument: _example_package_ClientMessage, metadata: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientReadableStream<_example_package_ServerMessage__Output>; 24 | serverStreamingCall(argument: _example_package_ClientMessage, options?: grpc.CallOptions): grpc.ClientReadableStream<_example_package_ServerMessage__Output>; 25 | serverStreamingCall(argument: _example_package_ClientMessage, metadata: grpc.Metadata, options?: grpc.CallOptions): grpc.ClientReadableStream<_example_package_ServerMessage__Output>; 26 | serverStreamingCall(argument: _example_package_ClientMessage, options?: grpc.CallOptions): grpc.ClientReadableStream<_example_package_ServerMessage__Output>; 27 | 28 | unaryCall(argument: _example_package_ClientMessage, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientUnaryCall; 29 | unaryCall(argument: _example_package_ClientMessage, metadata: grpc.Metadata, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientUnaryCall; 30 | unaryCall(argument: _example_package_ClientMessage, options: grpc.CallOptions, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientUnaryCall; 31 | unaryCall(argument: _example_package_ClientMessage, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientUnaryCall; 32 | unaryCall(argument: _example_package_ClientMessage, metadata: grpc.Metadata, options: grpc.CallOptions, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientUnaryCall; 33 | unaryCall(argument: _example_package_ClientMessage, metadata: grpc.Metadata, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientUnaryCall; 34 | unaryCall(argument: _example_package_ClientMessage, options: grpc.CallOptions, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientUnaryCall; 35 | unaryCall(argument: _example_package_ClientMessage, callback: grpc.requestCallback<_example_package_ServerMessage__Output>): grpc.ClientUnaryCall; 36 | 37 | } 38 | 39 | export interface ExampleHandlers extends grpc.UntypedServiceImplementation { 40 | bidirectionalStreamingCall: grpc.handleBidiStreamingCall<_example_package_ClientMessage__Output, _example_package_ServerMessage>; 41 | 42 | clientStreamingCall: grpc.handleClientStreamingCall<_example_package_ClientMessage__Output, _example_package_ServerMessage>; 43 | 44 | serverStreamingCall: grpc.handleServerStreamingCall<_example_package_ClientMessage__Output, _example_package_ServerMessage>; 45 | 46 | unaryCall: grpc.handleUnaryCall<_example_package_ClientMessage__Output, _example_package_ServerMessage>; 47 | 48 | } 49 | 50 | export interface ExampleDefinition extends grpc.ServiceDefinition { 51 | bidirectionalStreamingCall: MethodDefinition<_example_package_ClientMessage, _example_package_ServerMessage, _example_package_ClientMessage__Output, _example_package_ServerMessage__Output> 52 | clientStreamingCall: MethodDefinition<_example_package_ClientMessage, _example_package_ServerMessage, _example_package_ClientMessage__Output, _example_package_ServerMessage__Output> 53 | serverStreamingCall: MethodDefinition<_example_package_ClientMessage, _example_package_ServerMessage, _example_package_ClientMessage__Output, _example_package_ServerMessage__Output> 54 | unaryCall: MethodDefinition<_example_package_ClientMessage, _example_package_ServerMessage, _example_package_ClientMessage__Output, _example_package_ServerMessage__Output> 55 | } 56 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/proto/example_package/ServerMessage.ts: -------------------------------------------------------------------------------- 1 | // Original file: proto/example.proto 2 | 3 | 4 | export interface ServerMessage { 5 | 'serverMessage'?: (string); 6 | } 7 | 8 | export interface ServerMessage__Output { 9 | 'serverMessage': (string); 10 | } 11 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/server.ts: -------------------------------------------------------------------------------- 1 | import * as grpc from '@grpc/grpc-js'; 2 | import * as protoLoader from '@grpc/proto-loader'; 3 | import { ProtoGrpcType } from './proto/example'; 4 | import { ClientMessage } from './proto/example_package/ClientMessage'; 5 | import { ExampleHandlers } from './proto/example_package/Example'; 6 | import { ServerMessage } from './proto/example_package/ServerMessage'; 7 | 8 | const host = '0.0.0.0:9090'; 9 | 10 | const exampleServer: ExampleHandlers = { 11 | unaryCall( 12 | call: grpc.ServerUnaryCall, 13 | callback: grpc.sendUnaryData 14 | ) { 15 | if (call.request) { 16 | console.log(`(server) Got client message: ${call.request.clientMessage}`); 17 | } 18 | callback(null, { 19 | serverMessage: 'Message from server', 20 | }); 21 | }, 22 | 23 | serverStreamingCall( 24 | call: grpc.ServerWritableStream 25 | ) { 26 | call.write({ 27 | serverMessage: 'Message from server', 28 | }); 29 | }, 30 | 31 | clientStreamingCall( 32 | call: grpc.ServerReadableStream 33 | ) { 34 | call.on('data', (clientMessage: ClientMessage) => { 35 | console.log( 36 | `(server) Got client message: ${clientMessage.clientMessage}` 37 | ); 38 | }); 39 | }, 40 | 41 | bidirectionalStreamingCall( 42 | call: grpc.ServerDuplexStream 43 | ) { 44 | call.write({ 45 | serverMessage: 'Message from server', 46 | }); 47 | call.on('data', (clientMessage: ClientMessage) => { 48 | console.log( 49 | `(server) Got client message: ${clientMessage.clientMessage}` 50 | ); 51 | }); 52 | }, 53 | }; 54 | 55 | function getServer(): grpc.Server { 56 | const packageDefinition = protoLoader.loadSync('./proto/example.proto', { 57 | longs: String, 58 | enums: String, 59 | defaults: true, 60 | oneofs: true, 61 | }); 62 | const proto = grpc.loadPackageDefinition( 63 | packageDefinition 64 | ) as unknown as ProtoGrpcType; 65 | const server = new grpc.Server(); 66 | server.addService(proto.example_package.Example.service, exampleServer); 67 | return server; 68 | } 69 | 70 | if (require.main === module) { 71 | const server = getServer(); 72 | server.bindAsync( 73 | host, 74 | grpc.ServerCredentials.createInsecure(), 75 | (err: Error | null, port: number) => { 76 | if (err) { 77 | console.error(`Server error: ${err.message}`); 78 | } else { 79 | console.log(`Server bound on port: ${port}`); 80 | server.start(); 81 | } 82 | } 83 | ); 84 | } 85 | -------------------------------------------------------------------------------- /examples/grpc-proto-loader/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/node12/tsconfig.json", 3 | "exclude": ["node_modules"] 4 | } 5 | -------------------------------------------------------------------------------- /examples/grpc-web/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.js 3 | proto/*.ts 4 | dist/ 5 | -------------------------------------------------------------------------------- /examples/grpc-web/.npmrc: -------------------------------------------------------------------------------- 1 | registry=https://registry.npmjs.org/ -------------------------------------------------------------------------------- /examples/grpc-web/.prettierignore: -------------------------------------------------------------------------------- 1 | package-lock.json 2 | dist/ 3 | -------------------------------------------------------------------------------- /examples/grpc-web/.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "dbaeumer.vscode-eslint", 4 | "EditorConfig.EditorConfig", 5 | "esbenp.prettier-vscode", 6 | "streetsidesoftware.code-spell-checker", 7 | "zxh404.vscode-proto3" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /examples/grpc-web/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsc.autoDetect": "off", 3 | "typescript.updateImportsOnFileMove.enabled": "always", 4 | "files.insertFinalNewline": true, 5 | "files.trimTrailingWhitespace": true, 6 | "eslint.validate": ["javascript", "typescript"], 7 | "editor.formatOnSave": false, 8 | "editor.codeActionsOnSave": { 9 | "source.fixAll.eslint": true, 10 | "source.organizeImports": false 11 | }, 12 | "[markdown]": { 13 | "editor.defaultFormatter": "esbenp.prettier-vscode" 14 | }, 15 | "[json]": { 16 | "editor.defaultFormatter": "esbenp.prettier-vscode" 17 | }, 18 | "[yaml]": { 19 | "editor.defaultFormatter": "esbenp.prettier-vscode" 20 | }, 21 | "cSpell.language": "en-GB", 22 | "cSpell.words": ["grpc"] 23 | } 24 | -------------------------------------------------------------------------------- /examples/grpc-web/README.md: -------------------------------------------------------------------------------- 1 | # grpc-web 2 | 3 | This examples shows how to use the [proto compiler](https://www.npmjs.com/package/grpc-tools), [`ts-protoc-gen`](https://www.npmjs.com/package/ts-protoc-gen), [`@grpc/grpc-js`](https://www.npmjs.com/package/@grpc/grpc-js), [`protoc-gen-grpc-web`](https://www.npmjs.com/package/protoc-gen-grpc-web) & [`grpc-web`](https://www.npmjs.com/package/grpc-web) to build a fully typed gRPC CLI chat application that runs on Node.js and your browser. 4 | 5 | The [`ts-protoc-gen`](https://www.npmjs.com/package/ts-protoc-gen) compiler plugin is used to generate TypeScript files and the [`protoc-gen-grpc-web`](https://www.npmjs.com/package/protoc-gen-grpc-web) compiler plugin is used to generate the grpc-web files from the proto definitions. The generated code uses [`google-protobuf`](https://www.npmjs.com/package/google-protobuf) & [`@grpc/grpc-js`](https://www.npmjs.com/package/@grpc/grpc-js) at runtime. 6 | 7 | ## App layout 8 | 9 | - [package.json](./package.json) - Dependencies and node build scripts 10 | - [compile-proto.sh](./compile-proto.sh) - The proto compiler script 11 | - [proto/](./proto/) - Protobuf definitions and generated types 12 | - [server.ts](./server.ts) - The grpc server 13 | - [client.ts](./client.ts) - The grpc client 14 | 15 | ## Generating the Types 16 | 17 | ```sh 18 | npm install 19 | 20 | # Apple M1 users should install for x64 due to grpc-tools not supporting arm64 21 | npm install --target_arch=x64 22 | 23 | ./compile-proto.sh 24 | ``` 25 | 26 | ### Running the app 27 | 28 | (Note, docker is required to run the envoy proxy.) 29 | 30 | Run the demo: 31 | 32 | ```bash 33 | npm install 34 | npm run build 35 | npm run start:proxy 36 | npm run start:server 37 | python3 -m http.server 8081 38 | ``` 39 | 40 | Open http://localhost:8081/ in multiple browser windows and chat to yourself. 41 | 42 | Development workflow: 43 | 44 | ```bash 45 | npm run watch:typescript 46 | npm run watch:webpack 47 | npm run start:proxy 48 | npm run start:server 49 | python3 -m http.server 8081 50 | ``` 51 | 52 | ## Credits 53 | 54 | The chat application idea is inspired from https://techblog.fexcofts.com/2018/07/20/grpc-nodejs-chat-example/ 55 | -------------------------------------------------------------------------------- /examples/grpc-web/client.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable @typescript-eslint/no-non-null-assertion */ 2 | 3 | import { RpcError as GrpcWebError } from 'grpc-web'; 4 | import { ChatClient } from './proto/chat_grpc_web_pb'; 5 | import { ClientMessage, ServerMessage } from './proto/chat_pb'; 6 | 7 | let user: string; 8 | 9 | window.addEventListener('DOMContentLoaded', () => { 10 | document.getElementById('submit-name')?.addEventListener( 11 | 'submit', 12 | async (e) => { 13 | e.preventDefault(); 14 | user = (document.getElementById('name') as HTMLFormElement)?.value; 15 | if (user) { 16 | document.getElementById('submit-message')!.style.display = 'block'; 17 | document.getElementById('submit-name')!.style.display = 'none'; 18 | initChat(user); 19 | } 20 | }, 21 | false 22 | ); 23 | }); 24 | 25 | async function initChat(user: string) { 26 | const client = new ChatClient('http://' + window.location.hostname + ':8080'); 27 | 28 | const clientMessage = new ClientMessage(); 29 | clientMessage.setUser(user); 30 | 31 | const stream = client.join(clientMessage); 32 | stream.on('data', (message: ServerMessage) => { 33 | const messageElement = document.createElement('div'); 34 | messageElement.innerText = `${message.getUser()}: ${message.getText()}`; 35 | document.getElementById('messages')!.appendChild(messageElement); 36 | }); 37 | 38 | function sendMessage(message: string) { 39 | const clientMessage = new ClientMessage(); 40 | clientMessage.setUser(user); 41 | clientMessage.setText(message); 42 | client.send( 43 | clientMessage, 44 | undefined, 45 | (err: GrpcWebError, response: ServerMessage) => { 46 | if (err) { 47 | console.error(err); 48 | } else { 49 | console.log('response', response.toObject()); 50 | (document.getElementById('message') as HTMLFormElement)!.value = ''; 51 | } 52 | } 53 | ); 54 | } 55 | 56 | document.getElementById('submit-message')?.addEventListener( 57 | 'submit', 58 | (e) => { 59 | e.preventDefault(); 60 | const message = (document.getElementById('message') as HTMLFormElement) 61 | ?.value; 62 | sendMessage(message); 63 | }, 64 | false 65 | ); 66 | } 67 | -------------------------------------------------------------------------------- /examples/grpc-web/compile-proto.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | OUT_DIR="." 4 | TS_OUT_DIR="." 5 | IN_DIR="./proto" 6 | PROTOC="$(npm bin)/grpc_tools_node_protoc" 7 | PROTOC_GEN_TS_PATH="$(npm bin)/protoc-gen-ts" 8 | PROTOC_GEN_GRPC_PATH="$(npm bin)/grpc_tools_node_protoc_plugin" 9 | PROTOC_GEN_GRPC_WEB_PATH="$(npm bin)/protoc-gen-grpc-web" 10 | 11 | # generate grpc-web files & types 12 | $PROTOC \ 13 | -I="./" \ 14 | --plugin=protoc-gen-grpc-web=${PROTOC_GEN_GRPC_WEB_PATH} \ 15 | --js_out=import_style=commonjs:$OUT_DIR \ 16 | --grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext:$OUT_DIR \ 17 | "$IN_DIR"/*.proto 18 | 19 | # generate grpc-js files & types 20 | $PROTOC \ 21 | -I="./" \ 22 | --plugin=protoc-gen-ts=$PROTOC_GEN_TS_PATH \ 23 | --plugin=protoc-gen-grpc=${PROTOC_GEN_GRPC_PATH} \ 24 | --js_out=import_style=commonjs:$OUT_DIR \ 25 | --ts_out=service=grpc-node,mode=grpc-js:$TS_OUT_DIR \ 26 | --grpc_out=grpc_js:$OUT_DIR \ 27 | "$IN_DIR"/*.proto 28 | -------------------------------------------------------------------------------- /examples/grpc-web/envoy.yaml: -------------------------------------------------------------------------------- 1 | static_resources: 2 | listeners: 3 | - name: listener_0 4 | address: 5 | socket_address: { address: 0.0.0.0, port_value: 8080 } 6 | filter_chains: 7 | - filters: 8 | - name: envoy.http_connection_manager 9 | config: 10 | codec_type: auto 11 | stat_prefix: ingress_http 12 | route_config: 13 | name: local_route 14 | virtual_hosts: 15 | - name: local_service 16 | domains: ["*"] 17 | routes: 18 | - match: { prefix: "/" } 19 | route: 20 | cluster: greeter_service 21 | max_grpc_timeout: 0s 22 | cors: 23 | allow_origin_string_match: 24 | - prefix: "*" 25 | allow_methods: GET, PUT, DELETE, POST, OPTIONS 26 | allow_headers: keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout 27 | max_age: "1728000" 28 | expose_headers: custom-header-1,grpc-status,grpc-message 29 | http_filters: 30 | - name: envoy.grpc_web 31 | - name: envoy.cors 32 | - name: envoy.router 33 | clusters: 34 | - name: greeter_service 35 | connect_timeout: 0.25s 36 | type: logical_dns 37 | http2_protocol_options: {} 38 | lb_policy: round_robin 39 | hosts: [{ socket_address: { address: host.docker.internal, port_value: 9090 }}] 40 | -------------------------------------------------------------------------------- /examples/grpc-web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | gRPC-Web Example 6 | 7 | 20 | 21 | 22 |
23 |
24 |
25 |

Enter your name to join the chat

26 | 27 | 28 |
29 |
30 | Chat 31 | 32 | 33 |
34 |
35 | 36 | 37 | -------------------------------------------------------------------------------- /examples/grpc-web/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grpc-web-example", 3 | "version": "1.0.0", 4 | "description": "", 5 | "private": true, 6 | "scripts": { 7 | "build": "npm run build:typescript && npm run build:webpack", 8 | "build:typescript": "tsc -p .", 9 | "build:proto": "./compile-proto.sh", 10 | "build:webpack": "webpack ./client.js -c webpack.config.js", 11 | "watch:typescript": "npm run build:typescript -- -w", 12 | "watch:webpack": "npm run build:webpack -- -w", 13 | "prebuild": "npm run build:proto", 14 | "lint": "npm run lint:prettier && npm run lint:eslint", 15 | "lint:prettier": "prettier --check \"**/*.{ts,js,json,svg,md,yml}\"", 16 | "lint:eslint": "eslint . --ext .js,.ts", 17 | "lint:fix": "npm run lint:fix:prettier && npm run lint:eslint -- --fix", 18 | "lint:fix:prettier": "prettier --write '**/*.{ts,tsx,js,json,svg,md,yml}'", 19 | "start:server": "node server", 20 | "start:proxy": "docker run -d -p 8080:8080 -v \"$(pwd)\"/envoy.yaml:/etc/envoy/envoy.yaml:ro envoyproxy/envoy:v1.15.0" 21 | }, 22 | "keywords": [], 23 | "author": "", 24 | "license": "ISC", 25 | "devDependencies": { 26 | "@grpc/grpc-js": "^1.6.8", 27 | "@grpc/proto-loader": "^0.7.0", 28 | "@tsconfig/node12": "^1.0.11", 29 | "@types/google-protobuf": "^3.15.6", 30 | "@types/node": "^14.14.14", 31 | "@typescript-eslint/eslint-plugin": "^5.31.0", 32 | "@typescript-eslint/parser": "^5.31.0", 33 | "eslint": "^8.20.0", 34 | "eslint-config-prettier": "^8.5.0", 35 | "eslint-plugin-import": "^2.26.0", 36 | "eslint-plugin-node": "^11.1.0", 37 | "eslint-plugin-prettier": "^4.2.1", 38 | "google-protobuf": "^3.21.0", 39 | "grpc-tools": "^1.11.2", 40 | "grpc-web": "^1.3.1", 41 | "prettier": "^2.7.1", 42 | "prettier-plugin-organize-imports": "^3.0.0", 43 | "protoc-gen-grpc-web": "^1.4.0", 44 | "ts-protoc-gen": "^0.15.0", 45 | "typescript": "^4.7.4", 46 | "webpack": "^5.74.0", 47 | "webpack-cli": "^4.10.0" 48 | }, 49 | "eslintConfig": { 50 | "ignorePatterns": [ 51 | "**/*.js", 52 | "proto/*.ts", 53 | "generated" 54 | ], 55 | "env": { 56 | "browser": false, 57 | "es6": true, 58 | "node": true 59 | }, 60 | "parserOptions": { 61 | "project": "./tsconfig.json", 62 | "ecmaVersion": 2018, 63 | "sourceType": "module" 64 | }, 65 | "extends": [ 66 | "plugin:@typescript-eslint/recommended", 67 | "prettier/@typescript-eslint", 68 | "plugin:prettier/recommended", 69 | "plugin:import/errors", 70 | "plugin:import/warnings", 71 | "plugin:import/typescript" 72 | ] 73 | }, 74 | "prettier": { 75 | "singleQuote": true, 76 | "tabWidth": 2, 77 | "printWidth": 80, 78 | "useTabs": false 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /examples/grpc-web/proto/chat.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package chat_package; 4 | 5 | message ServerMessage { 6 | string user = 1; 7 | string text = 2; 8 | } 9 | 10 | message ClientMessage { 11 | string user = 1; 12 | string text = 2; 13 | } 14 | 15 | service Chat { 16 | rpc join(ClientMessage) returns (stream ServerMessage) {} 17 | rpc send(ClientMessage) returns (ServerMessage) {} 18 | } 19 | -------------------------------------------------------------------------------- /examples/grpc-web/server.ts: -------------------------------------------------------------------------------- 1 | import * as grpc from '@grpc/grpc-js'; 2 | import { ChatService, IChatServer } from './proto/chat_grpc_pb'; 3 | import { ClientMessage, ServerMessage } from './proto/chat_pb'; 4 | 5 | const users: grpc.ServerWritableStream[] = []; 6 | const messages: ClientMessage[] = []; 7 | 8 | function notifyChat(message: ServerMessage) { 9 | messages.push(message); 10 | users.forEach((user) => { 11 | user.write(message); 12 | }); 13 | } 14 | 15 | const chatServer: IChatServer = { 16 | join(call: grpc.ServerWritableStream): void { 17 | users.push(call); 18 | const serverMessage = new ServerMessage(); 19 | serverMessage.setUser('Server'); 20 | serverMessage.setText(`${call.request?.getUser()} joined`); 21 | notifyChat(serverMessage); 22 | }, 23 | send( 24 | call: grpc.ServerUnaryCall, 25 | callback: grpc.sendUnaryData 26 | ): void { 27 | if (call.request) { 28 | const serverMessage = new ServerMessage(); 29 | serverMessage.setUser(call.request.getUser()); 30 | serverMessage.setText(call.request.getText()); 31 | notifyChat(serverMessage); 32 | callback(null, serverMessage); 33 | } 34 | }, 35 | }; 36 | 37 | export function getServer(): grpc.Server { 38 | const server = new grpc.Server(); 39 | server.addService(ChatService, chatServer); 40 | return server; 41 | } 42 | 43 | if (require.main === module) { 44 | const server = getServer(); 45 | server.bindAsync( 46 | '0.0.0.0:9090', 47 | grpc.ServerCredentials.createInsecure(), 48 | (err: Error | null, port: number) => { 49 | if (err) { 50 | console.error(`Server error: ${err.message}`); 51 | } else { 52 | console.log(`Server bound on port: ${port}`); 53 | server.start(); 54 | } 55 | } 56 | ); 57 | } 58 | -------------------------------------------------------------------------------- /examples/grpc-web/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/node12/tsconfig.json", 3 | "compilerOptions": { 4 | "lib": ["DOM"] 5 | }, 6 | "exclude": ["node_modules"] 7 | } 8 | -------------------------------------------------------------------------------- /examples/grpc-web/webpack.config.ts: -------------------------------------------------------------------------------- 1 | import * as path from 'path'; 2 | 3 | export default { 4 | mode: 'production', 5 | entry: { 6 | index: './client.js', 7 | }, 8 | output: { 9 | filename: '[name].js', 10 | chunkFilename: '[name].js', 11 | path: path.resolve(__dirname, 'dist'), 12 | }, 13 | }; 14 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | **/*.js 3 | !proto/*.js 4 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/.npmrc: -------------------------------------------------------------------------------- 1 | registry=https://registry.npmjs.org/ -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/.prettierignore: -------------------------------------------------------------------------------- 1 | package-lock.json 2 | proto/*.ts 3 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "dbaeumer.vscode-eslint", 4 | "EditorConfig.EditorConfig", 5 | "esbenp.prettier-vscode", 6 | "streetsidesoftware.code-spell-checker", 7 | "zxh404.vscode-proto3" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsc.autoDetect": "off", 3 | "typescript.updateImportsOnFileMove.enabled": "always", 4 | "files.insertFinalNewline": true, 5 | "files.trimTrailingWhitespace": true, 6 | "eslint.validate": ["javascript", "typescript"], 7 | "editor.formatOnSave": false, 8 | "editor.codeActionsOnSave": { 9 | "source.fixAll.eslint": true, 10 | "source.organizeImports": false 11 | }, 12 | "[markdown]": { 13 | "editor.defaultFormatter": "esbenp.prettier-vscode" 14 | }, 15 | "[json]": { 16 | "editor.defaultFormatter": "esbenp.prettier-vscode" 17 | }, 18 | "[yaml]": { 19 | "editor.defaultFormatter": "esbenp.prettier-vscode" 20 | }, 21 | "cSpell.language": "en-GB", 22 | "cSpell.words": [ 23 | "Unary", 24 | "grpc" 25 | ] 26 | } 27 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/README.md: -------------------------------------------------------------------------------- 1 | # grpc_tools_node_protoc_ts example 2 | 3 | This example shows how to use the [proto compiler](https://www.npmjs.com/package/grpc-tools), [`grpc_tools_node_protoc_ts`](https://www.npmjs.com/package/grpc_tools_node_protoc_ts) & [`@grpc/grpc-js`](https://www.npmjs.com/package/@grpc/grpc-js) to build a fully typed gRPC application that runs on Node.js. 4 | 5 | The [proto compiler](https://www.npmjs.com/package/grpc-tools) and [`grpc_tools_node_protoc_ts`](https://www.npmjs.com/package/grpc_tools_node_protoc_ts) compiler plugin are used to generate JavaScript & TypeScript files from the proto definitions and [`google-protobuf`](https://www.npmjs.com/package/google-protobuf) & [`@grpc/grpc-js`](https://www.npmjs.com/package/@grpc/grpc-js) is used at runtime. 6 | 7 | ## App layout 8 | 9 | - [package.json](./package.json) - Dependencies and node build scripts 10 | - [compile-proto.sh](./compile-proto.sh) - The proto compiler script 11 | - [server.ts](./server.ts) - The grpc server 12 | - [client.ts](./client.ts) - The grpc client 13 | 14 | ## Generating the Types 15 | 16 | Install dependencies: 17 | 18 | ```sh 19 | npm install 20 | 21 | # Apple M1 users should install for x64 due to grpc-tools not supporting arm64 22 | npm i --target_arch=x64 23 | ``` 24 | 25 | Use [`grpc_tools_node_protoc_ts`](https://www.npmjs.com/package/grpc_tools_node_protoc_ts) to generate the TypeScript files: 26 | 27 | ```sh 28 | ./compile-proto.sh 29 | ``` 30 | 31 | This is aliased as a npm script: 32 | 33 | ```sh 34 | npm run build:proto 35 | ``` 36 | 37 | ### Running the App 38 | 39 | This simple app demonstrates the different gRPC calls you can perform. 40 | 41 | First generated the types and build the application files: 42 | 43 | ```sh 44 | npm run build 45 | ``` 46 | 47 | Start the server: 48 | 49 | ```sh 50 | npm run start:server 51 | ``` 52 | 53 | Now run the client by specifying which example you want to run: 54 | 55 | ```bash 56 | npm run start:client -- --unary 57 | npm run start:client -- --server-streaming 58 | npm run start:client -- --client-streaming 59 | npm run start:client -- --bidi-streaming 60 | ``` 61 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/client.ts: -------------------------------------------------------------------------------- 1 | import * as grpc from '@grpc/grpc-js'; 2 | import { ExampleClient } from './proto/example_grpc_pb'; 3 | import { ClientMessage, ServerMessage } from './proto/example_pb'; 4 | 5 | const host = '0.0.0.0:9090'; 6 | 7 | const client = new ExampleClient(host, grpc.credentials.createInsecure()); 8 | 9 | const deadline = new Date(); 10 | deadline.setSeconds(deadline.getSeconds() + 5); 11 | client.waitForReady(deadline, (error?: Error) => { 12 | if (error) { 13 | console.log(`Client connect error: ${error.message}`); 14 | } else { 15 | onClientReady(); 16 | } 17 | }); 18 | 19 | function onClientReady() { 20 | switch (process.argv[process.argv.length - 1]) { 21 | case '--unary': 22 | doUnaryCall(); 23 | break; 24 | case '--server-streaming': 25 | doServerStreamingCall(); 26 | break; 27 | case '--client-streaming': 28 | doClientStreamingCall(); 29 | break; 30 | case '--bidi-streaming': 31 | doBidirectionalStreamingCall(); 32 | break; 33 | default: 34 | throw new Error('Example not specified'); 35 | } 36 | } 37 | 38 | function doUnaryCall() { 39 | const clientMessage = new ClientMessage(); 40 | clientMessage.setClientMessage('Message from client'); 41 | client.unaryCall( 42 | clientMessage, 43 | (error: grpc.ServiceError | null, serverMessage?: ServerMessage) => { 44 | if (error) { 45 | console.error(error.message); 46 | } else if (serverMessage) { 47 | console.log( 48 | `(client) Got server message: ${serverMessage.getServerMessage()}` 49 | ); 50 | } 51 | } 52 | ); 53 | } 54 | 55 | function doServerStreamingCall() { 56 | const clientMessage = new ClientMessage(); 57 | clientMessage.setClientMessage('Message from client'); 58 | const stream = client.serverStreamingCall(clientMessage); 59 | stream.on('data', (serverMessage: ServerMessage) => { 60 | console.log( 61 | `(client) Got server message: ${serverMessage.getServerMessage()}` 62 | ); 63 | }); 64 | } 65 | 66 | function doClientStreamingCall() { 67 | const stream = client.clientStreamingCall( 68 | (error: grpc.ServiceError | null) => { 69 | if (error) { 70 | console.error(error.message); 71 | } 72 | } 73 | ); 74 | const clientMessage = new ClientMessage(); 75 | clientMessage.setClientMessage('Message from client'); 76 | stream.write(clientMessage); 77 | } 78 | 79 | function doBidirectionalStreamingCall() { 80 | const stream = client.bidirectionalStreamingCall(); 81 | 82 | // Server stream 83 | stream.on('data', (serverMessage: ServerMessage) => { 84 | console.log( 85 | `(client) Got server message: ${serverMessage.getServerMessage()}` 86 | ); 87 | }); 88 | 89 | // Client stream 90 | const clientMessage = new ClientMessage(); 91 | clientMessage.setClientMessage('Message from client'); 92 | stream.write(clientMessage); 93 | } 94 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/compile-proto.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | OUT_DIR="." 4 | TS_OUT_DIR="." 5 | IN_DIR="./proto" 6 | PROTOC="$(npm bin)/grpc_tools_node_protoc" 7 | PROTOC_GEN_TS_PATH="$(npm bin)/protoc-gen-ts" 8 | PROTOC_GEN_GRPC_PATH="$(npm bin)/grpc_tools_node_protoc_plugin" 9 | 10 | $PROTOC \ 11 | -I="./" \ 12 | --plugin=protoc-gen-ts=$PROTOC_GEN_TS_PATH \ 13 | --plugin=protoc-gen-grpc=${PROTOC_GEN_GRPC_PATH} \ 14 | --js_out=import_style=commonjs:$OUT_DIR \ 15 | --grpc_out=grpc_js:$OUT_DIR \ 16 | --ts_out=grpc_js:$TS_OUT_DIR \ 17 | "$IN_DIR"/*.proto 18 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grpc-js-example", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "npm run build:typescript", 8 | "build:typescript": "tsc -p .", 9 | "build:proto": "./compile-proto.sh", 10 | "watch": "npm run build:typescript -- -w", 11 | "prewatch": "npm run build:proto", 12 | "prebuild": "npm run build:proto", 13 | "lint": "npm run lint:prettier && npm run lint:eslint", 14 | "lint:prettier": "prettier --check \"**/*.{ts,js,json,svg,md,yml}\"", 15 | "lint:eslint": "eslint . --ext .js,.ts", 16 | "lint:fix": "npm run lint:fix:prettier && npm run lint:eslint -- --fix", 17 | "lint:fix:prettier": "prettier --write '**/*.{ts,tsx,js,json,svg,md,yml}'", 18 | "start:server": "node server", 19 | "start:client": "node client" 20 | }, 21 | "keywords": [], 22 | "author": "", 23 | "license": "ISC", 24 | "dependencies": { 25 | "@grpc/grpc-js": "^1.6.8", 26 | "google-protobuf": "^3.21.0" 27 | }, 28 | "devDependencies": { 29 | "@tsconfig/node12": "^1.0.11", 30 | "@types/google-protobuf": "^3.15.6", 31 | "@types/node": "^15.0.3", 32 | "@typescript-eslint/eslint-plugin": "^5.31.0", 33 | "@typescript-eslint/parser": "^5.31.0", 34 | "eslint": "^8.20.0", 35 | "eslint-config-prettier": "^8.5.0", 36 | "eslint-plugin-node": "^11.1.0", 37 | "eslint-plugin-prettier": "^4.2.1", 38 | "grpc_tools_node_protoc_ts": "^5.3.2", 39 | "grpc-tools": "^1.11.2", 40 | "prettier": "^2.7.1", 41 | "typescript": "^4.7.4" 42 | }, 43 | "eslintConfig": { 44 | "ignorePatterns": [ 45 | "**/*.js", 46 | "proto/*.ts" 47 | ], 48 | "env": { 49 | "browser": false, 50 | "es6": true, 51 | "node": true 52 | }, 53 | "parserOptions": { 54 | "project": "./tsconfig.json", 55 | "ecmaVersion": 2018, 56 | "sourceType": "module" 57 | }, 58 | "extends": [ 59 | "plugin:@typescript-eslint/recommended", 60 | "plugin:prettier/recommended" 61 | ] 62 | }, 63 | "prettier": { 64 | "singleQuote": true, 65 | "tabWidth": 2, 66 | "printWidth": 80, 67 | "useTabs": false 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/proto/example.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package example_package; 4 | 5 | message ServerMessage { 6 | string server_message = 1; 7 | } 8 | 9 | message ClientMessage { 10 | string client_message = 1; 11 | } 12 | 13 | service Example { 14 | rpc unaryCall(ClientMessage) returns (ServerMessage) {} 15 | rpc serverStreamingCall(ClientMessage) returns (stream ServerMessage) {} 16 | rpc clientStreamingCall(stream ClientMessage) returns (ServerMessage) {} 17 | rpc bidirectionalStreamingCall(stream ClientMessage) returns (stream ServerMessage) {} 18 | } 19 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/proto/example_grpc_pb.d.ts: -------------------------------------------------------------------------------- 1 | // package: example_package 2 | // file: proto/example.proto 3 | 4 | /* tslint:disable */ 5 | /* eslint-disable */ 6 | 7 | import * as grpc from "@grpc/grpc-js"; 8 | import * as proto_example_pb from "../proto/example_pb"; 9 | 10 | interface IExampleService extends grpc.ServiceDefinition { 11 | unaryCall: IExampleService_IunaryCall; 12 | serverStreamingCall: IExampleService_IserverStreamingCall; 13 | clientStreamingCall: IExampleService_IclientStreamingCall; 14 | bidirectionalStreamingCall: IExampleService_IbidirectionalStreamingCall; 15 | } 16 | 17 | interface IExampleService_IunaryCall extends grpc.MethodDefinition { 18 | path: "/example_package.Example/unaryCall"; 19 | requestStream: false; 20 | responseStream: false; 21 | requestSerialize: grpc.serialize; 22 | requestDeserialize: grpc.deserialize; 23 | responseSerialize: grpc.serialize; 24 | responseDeserialize: grpc.deserialize; 25 | } 26 | interface IExampleService_IserverStreamingCall extends grpc.MethodDefinition { 27 | path: "/example_package.Example/serverStreamingCall"; 28 | requestStream: false; 29 | responseStream: true; 30 | requestSerialize: grpc.serialize; 31 | requestDeserialize: grpc.deserialize; 32 | responseSerialize: grpc.serialize; 33 | responseDeserialize: grpc.deserialize; 34 | } 35 | interface IExampleService_IclientStreamingCall extends grpc.MethodDefinition { 36 | path: "/example_package.Example/clientStreamingCall"; 37 | requestStream: true; 38 | responseStream: false; 39 | requestSerialize: grpc.serialize; 40 | requestDeserialize: grpc.deserialize; 41 | responseSerialize: grpc.serialize; 42 | responseDeserialize: grpc.deserialize; 43 | } 44 | interface IExampleService_IbidirectionalStreamingCall extends grpc.MethodDefinition { 45 | path: "/example_package.Example/bidirectionalStreamingCall"; 46 | requestStream: true; 47 | responseStream: true; 48 | requestSerialize: grpc.serialize; 49 | requestDeserialize: grpc.deserialize; 50 | responseSerialize: grpc.serialize; 51 | responseDeserialize: grpc.deserialize; 52 | } 53 | 54 | export const ExampleService: IExampleService; 55 | 56 | export interface IExampleServer extends grpc.UntypedServiceImplementation { 57 | unaryCall: grpc.handleUnaryCall; 58 | serverStreamingCall: grpc.handleServerStreamingCall; 59 | clientStreamingCall: grpc.handleClientStreamingCall; 60 | bidirectionalStreamingCall: grpc.handleBidiStreamingCall; 61 | } 62 | 63 | export interface IExampleClient { 64 | unaryCall(request: proto_example_pb.ClientMessage, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientUnaryCall; 65 | unaryCall(request: proto_example_pb.ClientMessage, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientUnaryCall; 66 | unaryCall(request: proto_example_pb.ClientMessage, metadata: grpc.Metadata, options: Partial, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientUnaryCall; 67 | serverStreamingCall(request: proto_example_pb.ClientMessage, options?: Partial): grpc.ClientReadableStream; 68 | serverStreamingCall(request: proto_example_pb.ClientMessage, metadata?: grpc.Metadata, options?: Partial): grpc.ClientReadableStream; 69 | clientStreamingCall(callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientWritableStream; 70 | clientStreamingCall(metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientWritableStream; 71 | clientStreamingCall(options: Partial, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientWritableStream; 72 | clientStreamingCall(metadata: grpc.Metadata, options: Partial, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientWritableStream; 73 | bidirectionalStreamingCall(): grpc.ClientDuplexStream; 74 | bidirectionalStreamingCall(options: Partial): grpc.ClientDuplexStream; 75 | bidirectionalStreamingCall(metadata: grpc.Metadata, options?: Partial): grpc.ClientDuplexStream; 76 | } 77 | 78 | export class ExampleClient extends grpc.Client implements IExampleClient { 79 | constructor(address: string, credentials: grpc.ChannelCredentials, options?: Partial); 80 | public unaryCall(request: proto_example_pb.ClientMessage, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientUnaryCall; 81 | public unaryCall(request: proto_example_pb.ClientMessage, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientUnaryCall; 82 | public unaryCall(request: proto_example_pb.ClientMessage, metadata: grpc.Metadata, options: Partial, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientUnaryCall; 83 | public serverStreamingCall(request: proto_example_pb.ClientMessage, options?: Partial): grpc.ClientReadableStream; 84 | public serverStreamingCall(request: proto_example_pb.ClientMessage, metadata?: grpc.Metadata, options?: Partial): grpc.ClientReadableStream; 85 | public clientStreamingCall(callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientWritableStream; 86 | public clientStreamingCall(metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientWritableStream; 87 | public clientStreamingCall(options: Partial, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientWritableStream; 88 | public clientStreamingCall(metadata: grpc.Metadata, options: Partial, callback: (error: grpc.ServiceError | null, response: proto_example_pb.ServerMessage) => void): grpc.ClientWritableStream; 89 | public bidirectionalStreamingCall(options?: Partial): grpc.ClientDuplexStream; 90 | public bidirectionalStreamingCall(metadata?: grpc.Metadata, options?: Partial): grpc.ClientDuplexStream; 91 | } 92 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/proto/example_grpc_pb.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE -- DO NOT EDIT! 2 | 3 | 'use strict'; 4 | var grpc = require('@grpc/grpc-js'); 5 | var proto_example_pb = require('../proto/example_pb.js'); 6 | 7 | function serialize_example_package_ClientMessage(arg) { 8 | if (!(arg instanceof proto_example_pb.ClientMessage)) { 9 | throw new Error('Expected argument of type example_package.ClientMessage'); 10 | } 11 | return Buffer.from(arg.serializeBinary()); 12 | } 13 | 14 | function deserialize_example_package_ClientMessage(buffer_arg) { 15 | return proto_example_pb.ClientMessage.deserializeBinary(new Uint8Array(buffer_arg)); 16 | } 17 | 18 | function serialize_example_package_ServerMessage(arg) { 19 | if (!(arg instanceof proto_example_pb.ServerMessage)) { 20 | throw new Error('Expected argument of type example_package.ServerMessage'); 21 | } 22 | return Buffer.from(arg.serializeBinary()); 23 | } 24 | 25 | function deserialize_example_package_ServerMessage(buffer_arg) { 26 | return proto_example_pb.ServerMessage.deserializeBinary(new Uint8Array(buffer_arg)); 27 | } 28 | 29 | 30 | var ExampleService = exports.ExampleService = { 31 | unaryCall: { 32 | path: '/example_package.Example/unaryCall', 33 | requestStream: false, 34 | responseStream: false, 35 | requestType: proto_example_pb.ClientMessage, 36 | responseType: proto_example_pb.ServerMessage, 37 | requestSerialize: serialize_example_package_ClientMessage, 38 | requestDeserialize: deserialize_example_package_ClientMessage, 39 | responseSerialize: serialize_example_package_ServerMessage, 40 | responseDeserialize: deserialize_example_package_ServerMessage, 41 | }, 42 | serverStreamingCall: { 43 | path: '/example_package.Example/serverStreamingCall', 44 | requestStream: false, 45 | responseStream: true, 46 | requestType: proto_example_pb.ClientMessage, 47 | responseType: proto_example_pb.ServerMessage, 48 | requestSerialize: serialize_example_package_ClientMessage, 49 | requestDeserialize: deserialize_example_package_ClientMessage, 50 | responseSerialize: serialize_example_package_ServerMessage, 51 | responseDeserialize: deserialize_example_package_ServerMessage, 52 | }, 53 | clientStreamingCall: { 54 | path: '/example_package.Example/clientStreamingCall', 55 | requestStream: true, 56 | responseStream: false, 57 | requestType: proto_example_pb.ClientMessage, 58 | responseType: proto_example_pb.ServerMessage, 59 | requestSerialize: serialize_example_package_ClientMessage, 60 | requestDeserialize: deserialize_example_package_ClientMessage, 61 | responseSerialize: serialize_example_package_ServerMessage, 62 | responseDeserialize: deserialize_example_package_ServerMessage, 63 | }, 64 | bidirectionalStreamingCall: { 65 | path: '/example_package.Example/bidirectionalStreamingCall', 66 | requestStream: true, 67 | responseStream: true, 68 | requestType: proto_example_pb.ClientMessage, 69 | responseType: proto_example_pb.ServerMessage, 70 | requestSerialize: serialize_example_package_ClientMessage, 71 | requestDeserialize: deserialize_example_package_ClientMessage, 72 | responseSerialize: serialize_example_package_ServerMessage, 73 | responseDeserialize: deserialize_example_package_ServerMessage, 74 | }, 75 | }; 76 | 77 | exports.ExampleClient = grpc.makeGenericClientConstructor(ExampleService); 78 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/proto/example_pb.d.ts: -------------------------------------------------------------------------------- 1 | // package: example_package 2 | // file: proto/example.proto 3 | 4 | /* tslint:disable */ 5 | /* eslint-disable */ 6 | 7 | import * as jspb from "google-protobuf"; 8 | 9 | export class ServerMessage extends jspb.Message { 10 | getServerMessage(): string; 11 | setServerMessage(value: string): ServerMessage; 12 | 13 | serializeBinary(): Uint8Array; 14 | toObject(includeInstance?: boolean): ServerMessage.AsObject; 15 | static toObject(includeInstance: boolean, msg: ServerMessage): ServerMessage.AsObject; 16 | static extensions: {[key: number]: jspb.ExtensionFieldInfo}; 17 | static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo}; 18 | static serializeBinaryToWriter(message: ServerMessage, writer: jspb.BinaryWriter): void; 19 | static deserializeBinary(bytes: Uint8Array): ServerMessage; 20 | static deserializeBinaryFromReader(message: ServerMessage, reader: jspb.BinaryReader): ServerMessage; 21 | } 22 | 23 | export namespace ServerMessage { 24 | export type AsObject = { 25 | serverMessage: string, 26 | } 27 | } 28 | 29 | export class ClientMessage extends jspb.Message { 30 | getClientMessage(): string; 31 | setClientMessage(value: string): ClientMessage; 32 | 33 | serializeBinary(): Uint8Array; 34 | toObject(includeInstance?: boolean): ClientMessage.AsObject; 35 | static toObject(includeInstance: boolean, msg: ClientMessage): ClientMessage.AsObject; 36 | static extensions: {[key: number]: jspb.ExtensionFieldInfo}; 37 | static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo}; 38 | static serializeBinaryToWriter(message: ClientMessage, writer: jspb.BinaryWriter): void; 39 | static deserializeBinary(bytes: Uint8Array): ClientMessage; 40 | static deserializeBinaryFromReader(message: ClientMessage, reader: jspb.BinaryReader): ClientMessage; 41 | } 42 | 43 | export namespace ClientMessage { 44 | export type AsObject = { 45 | clientMessage: string, 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/proto/example_pb.js: -------------------------------------------------------------------------------- 1 | // source: proto/example.proto 2 | /** 3 | * @fileoverview 4 | * @enhanceable 5 | * @suppress {missingRequire} reports error on implicit type usages. 6 | * @suppress {messageConventions} JS Compiler reports an error if a variable or 7 | * field starts with 'MSG_' and isn't a translatable message. 8 | * @public 9 | */ 10 | // GENERATED CODE -- DO NOT EDIT! 11 | /* eslint-disable */ 12 | // @ts-nocheck 13 | 14 | var jspb = require('google-protobuf'); 15 | var goog = jspb; 16 | var global = Function('return this')(); 17 | 18 | goog.exportSymbol('proto.example_package.ClientMessage', null, global); 19 | goog.exportSymbol('proto.example_package.ServerMessage', null, global); 20 | /** 21 | * Generated by JsPbCodeGenerator. 22 | * @param {Array=} opt_data Optional initial data array, typically from a 23 | * server response, or constructed directly in Javascript. The array is used 24 | * in place and becomes part of the constructed object. It is not cloned. 25 | * If no data is provided, the constructed object will be empty, but still 26 | * valid. 27 | * @extends {jspb.Message} 28 | * @constructor 29 | */ 30 | proto.example_package.ServerMessage = function(opt_data) { 31 | jspb.Message.initialize(this, opt_data, 0, -1, null, null); 32 | }; 33 | goog.inherits(proto.example_package.ServerMessage, jspb.Message); 34 | if (goog.DEBUG && !COMPILED) { 35 | /** 36 | * @public 37 | * @override 38 | */ 39 | proto.example_package.ServerMessage.displayName = 'proto.example_package.ServerMessage'; 40 | } 41 | /** 42 | * Generated by JsPbCodeGenerator. 43 | * @param {Array=} opt_data Optional initial data array, typically from a 44 | * server response, or constructed directly in Javascript. The array is used 45 | * in place and becomes part of the constructed object. It is not cloned. 46 | * If no data is provided, the constructed object will be empty, but still 47 | * valid. 48 | * @extends {jspb.Message} 49 | * @constructor 50 | */ 51 | proto.example_package.ClientMessage = function(opt_data) { 52 | jspb.Message.initialize(this, opt_data, 0, -1, null, null); 53 | }; 54 | goog.inherits(proto.example_package.ClientMessage, jspb.Message); 55 | if (goog.DEBUG && !COMPILED) { 56 | /** 57 | * @public 58 | * @override 59 | */ 60 | proto.example_package.ClientMessage.displayName = 'proto.example_package.ClientMessage'; 61 | } 62 | 63 | 64 | 65 | if (jspb.Message.GENERATE_TO_OBJECT) { 66 | /** 67 | * Creates an object representation of this proto. 68 | * Field names that are reserved in JavaScript and will be renamed to pb_name. 69 | * Optional fields that are not set will be set to undefined. 70 | * To access a reserved field use, foo.pb_, eg, foo.pb_default. 71 | * For the list of reserved names please see: 72 | * net/proto2/compiler/js/internal/generator.cc#kKeyword. 73 | * @param {boolean=} opt_includeInstance Deprecated. whether to include the 74 | * JSPB instance for transitional soy proto support: 75 | * http://goto/soy-param-migration 76 | * @return {!Object} 77 | */ 78 | proto.example_package.ServerMessage.prototype.toObject = function(opt_includeInstance) { 79 | return proto.example_package.ServerMessage.toObject(opt_includeInstance, this); 80 | }; 81 | 82 | 83 | /** 84 | * Static version of the {@see toObject} method. 85 | * @param {boolean|undefined} includeInstance Deprecated. Whether to include 86 | * the JSPB instance for transitional soy proto support: 87 | * http://goto/soy-param-migration 88 | * @param {!proto.example_package.ServerMessage} msg The msg instance to transform. 89 | * @return {!Object} 90 | * @suppress {unusedLocalVariables} f is only used for nested messages 91 | */ 92 | proto.example_package.ServerMessage.toObject = function(includeInstance, msg) { 93 | var f, obj = { 94 | serverMessage: jspb.Message.getFieldWithDefault(msg, 1, "") 95 | }; 96 | 97 | if (includeInstance) { 98 | obj.$jspbMessageInstance = msg; 99 | } 100 | return obj; 101 | }; 102 | } 103 | 104 | 105 | /** 106 | * Deserializes binary data (in protobuf wire format). 107 | * @param {jspb.ByteSource} bytes The bytes to deserialize. 108 | * @return {!proto.example_package.ServerMessage} 109 | */ 110 | proto.example_package.ServerMessage.deserializeBinary = function(bytes) { 111 | var reader = new jspb.BinaryReader(bytes); 112 | var msg = new proto.example_package.ServerMessage; 113 | return proto.example_package.ServerMessage.deserializeBinaryFromReader(msg, reader); 114 | }; 115 | 116 | 117 | /** 118 | * Deserializes binary data (in protobuf wire format) from the 119 | * given reader into the given message object. 120 | * @param {!proto.example_package.ServerMessage} msg The message object to deserialize into. 121 | * @param {!jspb.BinaryReader} reader The BinaryReader to use. 122 | * @return {!proto.example_package.ServerMessage} 123 | */ 124 | proto.example_package.ServerMessage.deserializeBinaryFromReader = function(msg, reader) { 125 | while (reader.nextField()) { 126 | if (reader.isEndGroup()) { 127 | break; 128 | } 129 | var field = reader.getFieldNumber(); 130 | switch (field) { 131 | case 1: 132 | var value = /** @type {string} */ (reader.readString()); 133 | msg.setServerMessage(value); 134 | break; 135 | default: 136 | reader.skipField(); 137 | break; 138 | } 139 | } 140 | return msg; 141 | }; 142 | 143 | 144 | /** 145 | * Serializes the message to binary data (in protobuf wire format). 146 | * @return {!Uint8Array} 147 | */ 148 | proto.example_package.ServerMessage.prototype.serializeBinary = function() { 149 | var writer = new jspb.BinaryWriter(); 150 | proto.example_package.ServerMessage.serializeBinaryToWriter(this, writer); 151 | return writer.getResultBuffer(); 152 | }; 153 | 154 | 155 | /** 156 | * Serializes the given message to binary data (in protobuf wire 157 | * format), writing to the given BinaryWriter. 158 | * @param {!proto.example_package.ServerMessage} message 159 | * @param {!jspb.BinaryWriter} writer 160 | * @suppress {unusedLocalVariables} f is only used for nested messages 161 | */ 162 | proto.example_package.ServerMessage.serializeBinaryToWriter = function(message, writer) { 163 | var f = undefined; 164 | f = message.getServerMessage(); 165 | if (f.length > 0) { 166 | writer.writeString( 167 | 1, 168 | f 169 | ); 170 | } 171 | }; 172 | 173 | 174 | /** 175 | * optional string server_message = 1; 176 | * @return {string} 177 | */ 178 | proto.example_package.ServerMessage.prototype.getServerMessage = function() { 179 | return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 1, "")); 180 | }; 181 | 182 | 183 | /** 184 | * @param {string} value 185 | * @return {!proto.example_package.ServerMessage} returns this 186 | */ 187 | proto.example_package.ServerMessage.prototype.setServerMessage = function(value) { 188 | return jspb.Message.setProto3StringField(this, 1, value); 189 | }; 190 | 191 | 192 | 193 | 194 | 195 | if (jspb.Message.GENERATE_TO_OBJECT) { 196 | /** 197 | * Creates an object representation of this proto. 198 | * Field names that are reserved in JavaScript and will be renamed to pb_name. 199 | * Optional fields that are not set will be set to undefined. 200 | * To access a reserved field use, foo.pb_, eg, foo.pb_default. 201 | * For the list of reserved names please see: 202 | * net/proto2/compiler/js/internal/generator.cc#kKeyword. 203 | * @param {boolean=} opt_includeInstance Deprecated. whether to include the 204 | * JSPB instance for transitional soy proto support: 205 | * http://goto/soy-param-migration 206 | * @return {!Object} 207 | */ 208 | proto.example_package.ClientMessage.prototype.toObject = function(opt_includeInstance) { 209 | return proto.example_package.ClientMessage.toObject(opt_includeInstance, this); 210 | }; 211 | 212 | 213 | /** 214 | * Static version of the {@see toObject} method. 215 | * @param {boolean|undefined} includeInstance Deprecated. Whether to include 216 | * the JSPB instance for transitional soy proto support: 217 | * http://goto/soy-param-migration 218 | * @param {!proto.example_package.ClientMessage} msg The msg instance to transform. 219 | * @return {!Object} 220 | * @suppress {unusedLocalVariables} f is only used for nested messages 221 | */ 222 | proto.example_package.ClientMessage.toObject = function(includeInstance, msg) { 223 | var f, obj = { 224 | clientMessage: jspb.Message.getFieldWithDefault(msg, 1, "") 225 | }; 226 | 227 | if (includeInstance) { 228 | obj.$jspbMessageInstance = msg; 229 | } 230 | return obj; 231 | }; 232 | } 233 | 234 | 235 | /** 236 | * Deserializes binary data (in protobuf wire format). 237 | * @param {jspb.ByteSource} bytes The bytes to deserialize. 238 | * @return {!proto.example_package.ClientMessage} 239 | */ 240 | proto.example_package.ClientMessage.deserializeBinary = function(bytes) { 241 | var reader = new jspb.BinaryReader(bytes); 242 | var msg = new proto.example_package.ClientMessage; 243 | return proto.example_package.ClientMessage.deserializeBinaryFromReader(msg, reader); 244 | }; 245 | 246 | 247 | /** 248 | * Deserializes binary data (in protobuf wire format) from the 249 | * given reader into the given message object. 250 | * @param {!proto.example_package.ClientMessage} msg The message object to deserialize into. 251 | * @param {!jspb.BinaryReader} reader The BinaryReader to use. 252 | * @return {!proto.example_package.ClientMessage} 253 | */ 254 | proto.example_package.ClientMessage.deserializeBinaryFromReader = function(msg, reader) { 255 | while (reader.nextField()) { 256 | if (reader.isEndGroup()) { 257 | break; 258 | } 259 | var field = reader.getFieldNumber(); 260 | switch (field) { 261 | case 1: 262 | var value = /** @type {string} */ (reader.readString()); 263 | msg.setClientMessage(value); 264 | break; 265 | default: 266 | reader.skipField(); 267 | break; 268 | } 269 | } 270 | return msg; 271 | }; 272 | 273 | 274 | /** 275 | * Serializes the message to binary data (in protobuf wire format). 276 | * @return {!Uint8Array} 277 | */ 278 | proto.example_package.ClientMessage.prototype.serializeBinary = function() { 279 | var writer = new jspb.BinaryWriter(); 280 | proto.example_package.ClientMessage.serializeBinaryToWriter(this, writer); 281 | return writer.getResultBuffer(); 282 | }; 283 | 284 | 285 | /** 286 | * Serializes the given message to binary data (in protobuf wire 287 | * format), writing to the given BinaryWriter. 288 | * @param {!proto.example_package.ClientMessage} message 289 | * @param {!jspb.BinaryWriter} writer 290 | * @suppress {unusedLocalVariables} f is only used for nested messages 291 | */ 292 | proto.example_package.ClientMessage.serializeBinaryToWriter = function(message, writer) { 293 | var f = undefined; 294 | f = message.getClientMessage(); 295 | if (f.length > 0) { 296 | writer.writeString( 297 | 1, 298 | f 299 | ); 300 | } 301 | }; 302 | 303 | 304 | /** 305 | * optional string client_message = 1; 306 | * @return {string} 307 | */ 308 | proto.example_package.ClientMessage.prototype.getClientMessage = function() { 309 | return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 1, "")); 310 | }; 311 | 312 | 313 | /** 314 | * @param {string} value 315 | * @return {!proto.example_package.ClientMessage} returns this 316 | */ 317 | proto.example_package.ClientMessage.prototype.setClientMessage = function(value) { 318 | return jspb.Message.setProto3StringField(this, 1, value); 319 | }; 320 | 321 | 322 | goog.object.extend(exports, proto.example_package); 323 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/server.ts: -------------------------------------------------------------------------------- 1 | import * as grpc from '@grpc/grpc-js'; 2 | import { ExampleService, IExampleServer } from './proto/example_grpc_pb'; 3 | import { ClientMessage, ServerMessage } from './proto/example_pb'; 4 | 5 | const host = '0.0.0.0:9090'; 6 | 7 | const exampleServer: IExampleServer = { 8 | unaryCall( 9 | call: grpc.ServerUnaryCall, 10 | callback: grpc.sendUnaryData 11 | ) { 12 | if (call.request) { 13 | console.log( 14 | `(server) Got client message: ${call.request.getClientMessage()}` 15 | ); 16 | } 17 | const serverMessage = new ServerMessage(); 18 | serverMessage.setServerMessage('Message from server'); 19 | callback(null, serverMessage); 20 | }, 21 | 22 | serverStreamingCall( 23 | call: grpc.ServerWritableStream 24 | ) { 25 | const serverMessage = new ServerMessage(); 26 | serverMessage.setServerMessage('Message from server'); 27 | call.write(serverMessage); 28 | }, 29 | 30 | clientStreamingCall( 31 | call: grpc.ServerReadableStream 32 | ) { 33 | call.on('data', (clientMessage: ClientMessage) => { 34 | console.log( 35 | `(server) Got client message: ${clientMessage.getClientMessage()}` 36 | ); 37 | }); 38 | }, 39 | 40 | bidirectionalStreamingCall( 41 | call: grpc.ServerDuplexStream 42 | ) { 43 | call.on('data', (clientMessage: ClientMessage) => { 44 | console.log( 45 | `(server) Got client message: ${clientMessage.getClientMessage()}` 46 | ); 47 | }); 48 | 49 | const serverMessage = new ServerMessage(); 50 | serverMessage.setServerMessage('Message from server'); 51 | call.write(serverMessage); 52 | }, 53 | }; 54 | 55 | function getServer(): grpc.Server { 56 | const server = new grpc.Server(); 57 | server.addService(ExampleService, exampleServer); 58 | return server; 59 | } 60 | 61 | if (require.main === module) { 62 | const server = getServer(); 63 | server.bindAsync( 64 | host, 65 | grpc.ServerCredentials.createInsecure(), 66 | (err: Error | null, port: number) => { 67 | if (err) { 68 | console.error(`Server error: ${err.message}`); 69 | } else { 70 | console.log(`Server bound on port: ${port}`); 71 | server.start(); 72 | } 73 | } 74 | ); 75 | } 76 | -------------------------------------------------------------------------------- /examples/grpc_tools_node_protoc_ts/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/node12/tsconfig.json", 3 | "exclude": ["node_modules"] 4 | } 5 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.js 3 | !proto/*.js 4 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/.npmrc: -------------------------------------------------------------------------------- 1 | registry=https://registry.npmjs.org/ -------------------------------------------------------------------------------- /examples/ts-protoc-gen/.prettierignore: -------------------------------------------------------------------------------- 1 | package-lock.json 2 | proto/*.ts 3 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "dbaeumer.vscode-eslint", 4 | "EditorConfig.EditorConfig", 5 | "esbenp.prettier-vscode", 6 | "streetsidesoftware.code-spell-checker", 7 | "zxh404.vscode-proto3" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsc.autoDetect": "off", 3 | "typescript.updateImportsOnFileMove.enabled": "always", 4 | "files.insertFinalNewline": true, 5 | "files.trimTrailingWhitespace": true, 6 | "eslint.validate": ["javascript", "typescript"], 7 | "editor.formatOnSave": false, 8 | "editor.codeActionsOnSave": { 9 | "source.fixAll.eslint": true, 10 | "source.organizeImports": false 11 | }, 12 | "[markdown]": { 13 | "editor.defaultFormatter": "esbenp.prettier-vscode" 14 | }, 15 | "[json]": { 16 | "editor.defaultFormatter": "esbenp.prettier-vscode" 17 | }, 18 | "[yaml]": { 19 | "editor.defaultFormatter": "esbenp.prettier-vscode" 20 | }, 21 | "cSpell.language": "en-GB", 22 | "cSpell.words": ["grpc"] 23 | } 24 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/README.md: -------------------------------------------------------------------------------- 1 | # ts-protoc-gen 2 | 3 | This examples shows how to use the [proto compiler](https://www.npmjs.com/package/grpc-tools), [`ts-protoc-gen`](https://www.npmjs.com/package/ts-protoc-gen) & [`@grpc/grpc-js`](https://www.npmjs.com/package/@grpc/grpc-js) to build a fully typed gRPC CLI application that runs on Node.js. 4 | 5 | The [proto compiler](https://www.npmjs.com/package/grpc-tools) and [`ts-protoc-gen`](https://www.npmjs.com/package/ts-protoc-gen) compiler plugin are used to generate JavaScript & TypeScript files from the proto definitions. 6 | 7 | [`google-protobuf`](https://www.npmjs.com/package/google-protobuf) & [`@grpc/grpc-js`](https://www.npmjs.com/package/@grpc/grpc-js) are used at runtime. 8 | 9 | ## App layout 10 | 11 | - [package.json](./package.json) - Dependencies and node build scripts 12 | - [compile-proto.sh](./compile-proto.sh) - The proto compiler script 13 | - [proto/](./proto/) - Protobuf definitions and generated types 14 | - [server.ts](./server.ts) - The grpc server 15 | - [client.ts](./client.ts) - The grpc client 16 | 17 | ## Generating the Types 18 | 19 | Install dependencies: 20 | 21 | ```sh 22 | npm install 23 | 24 | # Apple M1 users should install for x64 due to grpc-tools not supporting arm64 25 | npm install --target_arch=x64 26 | ``` 27 | 28 | Use [proto compiler](https://www.npmjs.com/package/grpc-tools) and [`ts-protoc-gen`](https://www.npmjs.com/package/ts-protoc-gen) to generate the TypeScript files: 29 | 30 | ```sh 31 | ./compile-proto.sh 32 | ``` 33 | 34 | This is aliased as a npm script: 35 | 36 | ```sh 37 | npm run build:proto 38 | ``` 39 | 40 | ### Running the App 41 | 42 | This simple app demonstrates the different gRPC calls you can perform. 43 | 44 | First generate the types and build the application files: 45 | 46 | ```sh 47 | npm run build 48 | ``` 49 | 50 | Start the server: 51 | 52 | ```sh 53 | npm run start:server 54 | ``` 55 | 56 | Now run the client by specifying which example you want to run: 57 | 58 | ```bash 59 | npm run start:client -- --unary 60 | npm run start:client -- --server-streaming 61 | npm run start:client -- --client-streaming 62 | npm run start:client -- --bidi-streaming 63 | ``` 64 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/client.ts: -------------------------------------------------------------------------------- 1 | import * as grpc from '@grpc/grpc-js'; 2 | import { ExampleClient } from './proto/example_grpc_pb'; 3 | import { ClientMessage, ServerMessage } from './proto/example_pb'; 4 | 5 | const host = '0.0.0.0:9090'; 6 | 7 | const client = new ExampleClient(host, grpc.credentials.createInsecure()); 8 | 9 | const deadline = new Date(); 10 | deadline.setSeconds(deadline.getSeconds() + 5); 11 | client.waitForReady(deadline, (error?: Error) => { 12 | if (error) { 13 | console.log(`Client connect error: ${error.message}`); 14 | } else { 15 | onClientReady(); 16 | } 17 | }); 18 | 19 | function onClientReady() { 20 | switch (process.argv[process.argv.length - 1]) { 21 | case '--unary': 22 | doUnaryCall(); 23 | break; 24 | case '--server-streaming': 25 | doServerStreamingCall(); 26 | break; 27 | case '--client-streaming': 28 | doClientStreamingCall(); 29 | break; 30 | case '--bidi-streaming': 31 | doBidirectionalStreamingCall(); 32 | break; 33 | default: 34 | throw new Error('Example not specified'); 35 | } 36 | } 37 | 38 | function doUnaryCall() { 39 | const clientMessage = new ClientMessage(); 40 | clientMessage.setClientMessage('Message from client'); 41 | client.unaryCall( 42 | clientMessage, 43 | (error: grpc.ServiceError | null, serverMessage?: ServerMessage) => { 44 | if (error) { 45 | console.error(error.message); 46 | } else if (serverMessage) { 47 | console.log( 48 | `(client) Got server message: ${serverMessage.getServerMessage()}` 49 | ); 50 | } 51 | } 52 | ); 53 | } 54 | 55 | function doServerStreamingCall() { 56 | const clientMessage = new ClientMessage(); 57 | clientMessage.setClientMessage('Message from client'); 58 | const stream = client.serverStreamingCall(clientMessage); 59 | stream.on('data', (serverMessage: ServerMessage) => { 60 | console.log( 61 | `(client) Got server message: ${serverMessage.getServerMessage()}` 62 | ); 63 | }); 64 | } 65 | 66 | function doClientStreamingCall() { 67 | const stream = client.clientStreamingCall( 68 | (error: grpc.ServiceError | null) => { 69 | if (error) { 70 | console.error(error.message); 71 | } 72 | } 73 | ); 74 | const clientMessage = new ClientMessage(); 75 | clientMessage.setClientMessage('Message from client'); 76 | stream.write(clientMessage); 77 | } 78 | 79 | function doBidirectionalStreamingCall() { 80 | const stream = client.bidirectionalStreamingCall(); 81 | 82 | // Server stream 83 | stream.on('data', (serverMessage: ServerMessage) => { 84 | console.log( 85 | `(client) Got server message: ${serverMessage.getServerMessage()}` 86 | ); 87 | }); 88 | 89 | // Client stream 90 | const clientMessage = new ClientMessage(); 91 | clientMessage.setClientMessage('Message from client'); 92 | stream.write(clientMessage); 93 | } 94 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/compile-proto.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | OUT_DIR="." 4 | TS_OUT_DIR="." 5 | IN_DIR="./proto" 6 | PROTOC="$(npm bin)/grpc_tools_node_protoc" 7 | PROTOC_GEN_TS_PATH="$(npm bin)/protoc-gen-ts" 8 | PROTOC_GEN_GRPC_PATH="$(npm bin)/grpc_tools_node_protoc_plugin" 9 | 10 | $PROTOC \ 11 | -I="./" \ 12 | --plugin=protoc-gen-ts=$PROTOC_GEN_TS_PATH \ 13 | --plugin=protoc-gen-grpc=$PROTOC_GEN_GRPC_PATH \ 14 | --js_out=import_style=commonjs:$OUT_DIR \ 15 | --grpc_out=grpc_js:$OUT_DIR \ 16 | --ts_out=service=grpc-node,mode=grpc-js:$TS_OUT_DIR \ 17 | "$IN_DIR"/*.proto 18 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grpc-js-example", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "npm run build:typescript", 8 | "build:typescript": "tsc -p .", 9 | "build:proto": "./compile-proto.sh", 10 | "watch": "npm run build:typescript -- -w", 11 | "prewatch": "npm run build:proto", 12 | "prebuild": "npm run build:proto", 13 | "lint": "npm run lint:prettier && npm run lint:eslint", 14 | "lint:prettier": "prettier --check \"**/*.{ts,js,json,svg,md,yml}\"", 15 | "lint:eslint": "eslint . --ext .js,.ts", 16 | "lint:fix": "npm run lint:fix:prettier && npm run lint:eslint -- --fix", 17 | "lint:fix:prettier": "prettier --write '**/*.{ts,tsx,js,json,svg,md,yml}'", 18 | "start:server": "node server", 19 | "start:client": "node client" 20 | }, 21 | "keywords": [], 22 | "author": "", 23 | "license": "ISC", 24 | "dependencies": { 25 | "@grpc/grpc-js": "^1.6.8", 26 | "google-protobuf": "^3.21.0" 27 | }, 28 | "devDependencies": { 29 | "@tsconfig/node12": "^1.0.11", 30 | "@types/google-protobuf": "^3.15.6", 31 | "@types/node": "^14.14.14", 32 | "@typescript-eslint/eslint-plugin": "^5.31.0", 33 | "@typescript-eslint/parser": "^5.31.0", 34 | "eslint": "^8.20.0", 35 | "eslint-config-prettier": "^8.5.0", 36 | "eslint-plugin-node": "^11.1.0", 37 | "eslint-plugin-prettier": "^4.2.1", 38 | "grpc-tools": "^1.11.2", 39 | "prettier": "^2.7.1", 40 | "ts-protoc-gen": "^0.15.0", 41 | "typescript": "^4.7.4" 42 | }, 43 | "eslintConfig": { 44 | "ignorePatterns": [ 45 | "**/*.js", 46 | "proto/*.ts" 47 | ], 48 | "env": { 49 | "browser": false, 50 | "es6": true, 51 | "node": true 52 | }, 53 | "parserOptions": { 54 | "project": "./tsconfig.json", 55 | "ecmaVersion": 2018, 56 | "sourceType": "module" 57 | }, 58 | "extends": [ 59 | "plugin:@typescript-eslint/recommended", 60 | "prettier/@typescript-eslint", 61 | "plugin:prettier/recommended" 62 | ] 63 | }, 64 | "prettier": { 65 | "singleQuote": true, 66 | "tabWidth": 2, 67 | "printWidth": 80, 68 | "useTabs": false 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/proto/example.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package example_package; 4 | 5 | message ServerMessage { 6 | string server_message = 1; 7 | } 8 | 9 | message ClientMessage { 10 | string client_message = 1; 11 | } 12 | 13 | service Example { 14 | rpc unaryCall(ClientMessage) returns (ServerMessage) {} 15 | rpc serverStreamingCall(ClientMessage) returns (stream ServerMessage) {} 16 | rpc clientStreamingCall(stream ClientMessage) returns (ServerMessage) {} 17 | rpc bidirectionalStreamingCall(stream ClientMessage) returns (stream ServerMessage) {} 18 | } 19 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/proto/example_grpc_pb.d.ts: -------------------------------------------------------------------------------- 1 | // GENERATED CODE -- DO NOT EDIT! 2 | 3 | // package: example_package 4 | // file: proto/example.proto 5 | 6 | import * as proto_example_pb from "../proto/example_pb"; 7 | import * as grpc from "@grpc/grpc-js"; 8 | 9 | interface IExampleService extends grpc.ServiceDefinition { 10 | unaryCall: grpc.MethodDefinition; 11 | serverStreamingCall: grpc.MethodDefinition; 12 | clientStreamingCall: grpc.MethodDefinition; 13 | bidirectionalStreamingCall: grpc.MethodDefinition; 14 | } 15 | 16 | export const ExampleService: IExampleService; 17 | 18 | export interface IExampleServer extends grpc.UntypedServiceImplementation { 19 | unaryCall: grpc.handleUnaryCall; 20 | serverStreamingCall: grpc.handleServerStreamingCall; 21 | clientStreamingCall: grpc.handleClientStreamingCall; 22 | bidirectionalStreamingCall: grpc.handleBidiStreamingCall; 23 | } 24 | 25 | export class ExampleClient extends grpc.Client { 26 | constructor(address: string, credentials: grpc.ChannelCredentials, options?: object); 27 | unaryCall(argument: proto_example_pb.ClientMessage, callback: grpc.requestCallback): grpc.ClientUnaryCall; 28 | unaryCall(argument: proto_example_pb.ClientMessage, metadataOrOptions: grpc.Metadata | grpc.CallOptions | null, callback: grpc.requestCallback): grpc.ClientUnaryCall; 29 | unaryCall(argument: proto_example_pb.ClientMessage, metadata: grpc.Metadata | null, options: grpc.CallOptions | null, callback: grpc.requestCallback): grpc.ClientUnaryCall; 30 | serverStreamingCall(argument: proto_example_pb.ClientMessage, metadataOrOptions?: grpc.Metadata | grpc.CallOptions | null): grpc.ClientReadableStream; 31 | serverStreamingCall(argument: proto_example_pb.ClientMessage, metadata?: grpc.Metadata | null, options?: grpc.CallOptions | null): grpc.ClientReadableStream; 32 | clientStreamingCall(callback: grpc.requestCallback): grpc.ClientWritableStream; 33 | clientStreamingCall(metadataOrOptions: grpc.Metadata | grpc.CallOptions | null, callback: grpc.requestCallback): grpc.ClientWritableStream; 34 | clientStreamingCall(metadata: grpc.Metadata | null, options: grpc.CallOptions | null, callback: grpc.requestCallback): grpc.ClientWritableStream; 35 | bidirectionalStreamingCall(metadataOrOptions?: grpc.Metadata | grpc.CallOptions | null): grpc.ClientDuplexStream; 36 | bidirectionalStreamingCall(metadata?: grpc.Metadata | null, options?: grpc.CallOptions | null): grpc.ClientDuplexStream; 37 | } 38 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/proto/example_grpc_pb.js: -------------------------------------------------------------------------------- 1 | // GENERATED CODE -- DO NOT EDIT! 2 | 3 | 'use strict'; 4 | var grpc = require('@grpc/grpc-js'); 5 | var proto_example_pb = require('../proto/example_pb.js'); 6 | 7 | function serialize_example_package_ClientMessage(arg) { 8 | if (!(arg instanceof proto_example_pb.ClientMessage)) { 9 | throw new Error('Expected argument of type example_package.ClientMessage'); 10 | } 11 | return Buffer.from(arg.serializeBinary()); 12 | } 13 | 14 | function deserialize_example_package_ClientMessage(buffer_arg) { 15 | return proto_example_pb.ClientMessage.deserializeBinary(new Uint8Array(buffer_arg)); 16 | } 17 | 18 | function serialize_example_package_ServerMessage(arg) { 19 | if (!(arg instanceof proto_example_pb.ServerMessage)) { 20 | throw new Error('Expected argument of type example_package.ServerMessage'); 21 | } 22 | return Buffer.from(arg.serializeBinary()); 23 | } 24 | 25 | function deserialize_example_package_ServerMessage(buffer_arg) { 26 | return proto_example_pb.ServerMessage.deserializeBinary(new Uint8Array(buffer_arg)); 27 | } 28 | 29 | 30 | var ExampleService = exports.ExampleService = { 31 | unaryCall: { 32 | path: '/example_package.Example/unaryCall', 33 | requestStream: false, 34 | responseStream: false, 35 | requestType: proto_example_pb.ClientMessage, 36 | responseType: proto_example_pb.ServerMessage, 37 | requestSerialize: serialize_example_package_ClientMessage, 38 | requestDeserialize: deserialize_example_package_ClientMessage, 39 | responseSerialize: serialize_example_package_ServerMessage, 40 | responseDeserialize: deserialize_example_package_ServerMessage, 41 | }, 42 | serverStreamingCall: { 43 | path: '/example_package.Example/serverStreamingCall', 44 | requestStream: false, 45 | responseStream: true, 46 | requestType: proto_example_pb.ClientMessage, 47 | responseType: proto_example_pb.ServerMessage, 48 | requestSerialize: serialize_example_package_ClientMessage, 49 | requestDeserialize: deserialize_example_package_ClientMessage, 50 | responseSerialize: serialize_example_package_ServerMessage, 51 | responseDeserialize: deserialize_example_package_ServerMessage, 52 | }, 53 | clientStreamingCall: { 54 | path: '/example_package.Example/clientStreamingCall', 55 | requestStream: true, 56 | responseStream: false, 57 | requestType: proto_example_pb.ClientMessage, 58 | responseType: proto_example_pb.ServerMessage, 59 | requestSerialize: serialize_example_package_ClientMessage, 60 | requestDeserialize: deserialize_example_package_ClientMessage, 61 | responseSerialize: serialize_example_package_ServerMessage, 62 | responseDeserialize: deserialize_example_package_ServerMessage, 63 | }, 64 | bidirectionalStreamingCall: { 65 | path: '/example_package.Example/bidirectionalStreamingCall', 66 | requestStream: true, 67 | responseStream: true, 68 | requestType: proto_example_pb.ClientMessage, 69 | responseType: proto_example_pb.ServerMessage, 70 | requestSerialize: serialize_example_package_ClientMessage, 71 | requestDeserialize: deserialize_example_package_ClientMessage, 72 | responseSerialize: serialize_example_package_ServerMessage, 73 | responseDeserialize: deserialize_example_package_ServerMessage, 74 | }, 75 | }; 76 | 77 | exports.ExampleClient = grpc.makeGenericClientConstructor(ExampleService); 78 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/proto/example_pb.d.ts: -------------------------------------------------------------------------------- 1 | // package: example_package 2 | // file: proto/example.proto 3 | 4 | import * as jspb from "google-protobuf"; 5 | 6 | export class ServerMessage extends jspb.Message { 7 | getServerMessage(): string; 8 | setServerMessage(value: string): void; 9 | 10 | serializeBinary(): Uint8Array; 11 | toObject(includeInstance?: boolean): ServerMessage.AsObject; 12 | static toObject(includeInstance: boolean, msg: ServerMessage): ServerMessage.AsObject; 13 | static extensions: {[key: number]: jspb.ExtensionFieldInfo}; 14 | static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo}; 15 | static serializeBinaryToWriter(message: ServerMessage, writer: jspb.BinaryWriter): void; 16 | static deserializeBinary(bytes: Uint8Array): ServerMessage; 17 | static deserializeBinaryFromReader(message: ServerMessage, reader: jspb.BinaryReader): ServerMessage; 18 | } 19 | 20 | export namespace ServerMessage { 21 | export type AsObject = { 22 | serverMessage: string, 23 | } 24 | } 25 | 26 | export class ClientMessage extends jspb.Message { 27 | getClientMessage(): string; 28 | setClientMessage(value: string): void; 29 | 30 | serializeBinary(): Uint8Array; 31 | toObject(includeInstance?: boolean): ClientMessage.AsObject; 32 | static toObject(includeInstance: boolean, msg: ClientMessage): ClientMessage.AsObject; 33 | static extensions: {[key: number]: jspb.ExtensionFieldInfo}; 34 | static extensionsBinary: {[key: number]: jspb.ExtensionFieldBinaryInfo}; 35 | static serializeBinaryToWriter(message: ClientMessage, writer: jspb.BinaryWriter): void; 36 | static deserializeBinary(bytes: Uint8Array): ClientMessage; 37 | static deserializeBinaryFromReader(message: ClientMessage, reader: jspb.BinaryReader): ClientMessage; 38 | } 39 | 40 | export namespace ClientMessage { 41 | export type AsObject = { 42 | clientMessage: string, 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/proto/example_pb.js: -------------------------------------------------------------------------------- 1 | // source: proto/example.proto 2 | /** 3 | * @fileoverview 4 | * @enhanceable 5 | * @suppress {missingRequire} reports error on implicit type usages. 6 | * @suppress {messageConventions} JS Compiler reports an error if a variable or 7 | * field starts with 'MSG_' and isn't a translatable message. 8 | * @public 9 | */ 10 | // GENERATED CODE -- DO NOT EDIT! 11 | /* eslint-disable */ 12 | // @ts-nocheck 13 | 14 | var jspb = require('google-protobuf'); 15 | var goog = jspb; 16 | var global = Function('return this')(); 17 | 18 | goog.exportSymbol('proto.example_package.ClientMessage', null, global); 19 | goog.exportSymbol('proto.example_package.ServerMessage', null, global); 20 | /** 21 | * Generated by JsPbCodeGenerator. 22 | * @param {Array=} opt_data Optional initial data array, typically from a 23 | * server response, or constructed directly in Javascript. The array is used 24 | * in place and becomes part of the constructed object. It is not cloned. 25 | * If no data is provided, the constructed object will be empty, but still 26 | * valid. 27 | * @extends {jspb.Message} 28 | * @constructor 29 | */ 30 | proto.example_package.ServerMessage = function(opt_data) { 31 | jspb.Message.initialize(this, opt_data, 0, -1, null, null); 32 | }; 33 | goog.inherits(proto.example_package.ServerMessage, jspb.Message); 34 | if (goog.DEBUG && !COMPILED) { 35 | /** 36 | * @public 37 | * @override 38 | */ 39 | proto.example_package.ServerMessage.displayName = 'proto.example_package.ServerMessage'; 40 | } 41 | /** 42 | * Generated by JsPbCodeGenerator. 43 | * @param {Array=} opt_data Optional initial data array, typically from a 44 | * server response, or constructed directly in Javascript. The array is used 45 | * in place and becomes part of the constructed object. It is not cloned. 46 | * If no data is provided, the constructed object will be empty, but still 47 | * valid. 48 | * @extends {jspb.Message} 49 | * @constructor 50 | */ 51 | proto.example_package.ClientMessage = function(opt_data) { 52 | jspb.Message.initialize(this, opt_data, 0, -1, null, null); 53 | }; 54 | goog.inherits(proto.example_package.ClientMessage, jspb.Message); 55 | if (goog.DEBUG && !COMPILED) { 56 | /** 57 | * @public 58 | * @override 59 | */ 60 | proto.example_package.ClientMessage.displayName = 'proto.example_package.ClientMessage'; 61 | } 62 | 63 | 64 | 65 | if (jspb.Message.GENERATE_TO_OBJECT) { 66 | /** 67 | * Creates an object representation of this proto. 68 | * Field names that are reserved in JavaScript and will be renamed to pb_name. 69 | * Optional fields that are not set will be set to undefined. 70 | * To access a reserved field use, foo.pb_, eg, foo.pb_default. 71 | * For the list of reserved names please see: 72 | * net/proto2/compiler/js/internal/generator.cc#kKeyword. 73 | * @param {boolean=} opt_includeInstance Deprecated. whether to include the 74 | * JSPB instance for transitional soy proto support: 75 | * http://goto/soy-param-migration 76 | * @return {!Object} 77 | */ 78 | proto.example_package.ServerMessage.prototype.toObject = function(opt_includeInstance) { 79 | return proto.example_package.ServerMessage.toObject(opt_includeInstance, this); 80 | }; 81 | 82 | 83 | /** 84 | * Static version of the {@see toObject} method. 85 | * @param {boolean|undefined} includeInstance Deprecated. Whether to include 86 | * the JSPB instance for transitional soy proto support: 87 | * http://goto/soy-param-migration 88 | * @param {!proto.example_package.ServerMessage} msg The msg instance to transform. 89 | * @return {!Object} 90 | * @suppress {unusedLocalVariables} f is only used for nested messages 91 | */ 92 | proto.example_package.ServerMessage.toObject = function(includeInstance, msg) { 93 | var f, obj = { 94 | serverMessage: jspb.Message.getFieldWithDefault(msg, 1, "") 95 | }; 96 | 97 | if (includeInstance) { 98 | obj.$jspbMessageInstance = msg; 99 | } 100 | return obj; 101 | }; 102 | } 103 | 104 | 105 | /** 106 | * Deserializes binary data (in protobuf wire format). 107 | * @param {jspb.ByteSource} bytes The bytes to deserialize. 108 | * @return {!proto.example_package.ServerMessage} 109 | */ 110 | proto.example_package.ServerMessage.deserializeBinary = function(bytes) { 111 | var reader = new jspb.BinaryReader(bytes); 112 | var msg = new proto.example_package.ServerMessage; 113 | return proto.example_package.ServerMessage.deserializeBinaryFromReader(msg, reader); 114 | }; 115 | 116 | 117 | /** 118 | * Deserializes binary data (in protobuf wire format) from the 119 | * given reader into the given message object. 120 | * @param {!proto.example_package.ServerMessage} msg The message object to deserialize into. 121 | * @param {!jspb.BinaryReader} reader The BinaryReader to use. 122 | * @return {!proto.example_package.ServerMessage} 123 | */ 124 | proto.example_package.ServerMessage.deserializeBinaryFromReader = function(msg, reader) { 125 | while (reader.nextField()) { 126 | if (reader.isEndGroup()) { 127 | break; 128 | } 129 | var field = reader.getFieldNumber(); 130 | switch (field) { 131 | case 1: 132 | var value = /** @type {string} */ (reader.readString()); 133 | msg.setServerMessage(value); 134 | break; 135 | default: 136 | reader.skipField(); 137 | break; 138 | } 139 | } 140 | return msg; 141 | }; 142 | 143 | 144 | /** 145 | * Serializes the message to binary data (in protobuf wire format). 146 | * @return {!Uint8Array} 147 | */ 148 | proto.example_package.ServerMessage.prototype.serializeBinary = function() { 149 | var writer = new jspb.BinaryWriter(); 150 | proto.example_package.ServerMessage.serializeBinaryToWriter(this, writer); 151 | return writer.getResultBuffer(); 152 | }; 153 | 154 | 155 | /** 156 | * Serializes the given message to binary data (in protobuf wire 157 | * format), writing to the given BinaryWriter. 158 | * @param {!proto.example_package.ServerMessage} message 159 | * @param {!jspb.BinaryWriter} writer 160 | * @suppress {unusedLocalVariables} f is only used for nested messages 161 | */ 162 | proto.example_package.ServerMessage.serializeBinaryToWriter = function(message, writer) { 163 | var f = undefined; 164 | f = message.getServerMessage(); 165 | if (f.length > 0) { 166 | writer.writeString( 167 | 1, 168 | f 169 | ); 170 | } 171 | }; 172 | 173 | 174 | /** 175 | * optional string server_message = 1; 176 | * @return {string} 177 | */ 178 | proto.example_package.ServerMessage.prototype.getServerMessage = function() { 179 | return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 1, "")); 180 | }; 181 | 182 | 183 | /** 184 | * @param {string} value 185 | * @return {!proto.example_package.ServerMessage} returns this 186 | */ 187 | proto.example_package.ServerMessage.prototype.setServerMessage = function(value) { 188 | return jspb.Message.setProto3StringField(this, 1, value); 189 | }; 190 | 191 | 192 | 193 | 194 | 195 | if (jspb.Message.GENERATE_TO_OBJECT) { 196 | /** 197 | * Creates an object representation of this proto. 198 | * Field names that are reserved in JavaScript and will be renamed to pb_name. 199 | * Optional fields that are not set will be set to undefined. 200 | * To access a reserved field use, foo.pb_, eg, foo.pb_default. 201 | * For the list of reserved names please see: 202 | * net/proto2/compiler/js/internal/generator.cc#kKeyword. 203 | * @param {boolean=} opt_includeInstance Deprecated. whether to include the 204 | * JSPB instance for transitional soy proto support: 205 | * http://goto/soy-param-migration 206 | * @return {!Object} 207 | */ 208 | proto.example_package.ClientMessage.prototype.toObject = function(opt_includeInstance) { 209 | return proto.example_package.ClientMessage.toObject(opt_includeInstance, this); 210 | }; 211 | 212 | 213 | /** 214 | * Static version of the {@see toObject} method. 215 | * @param {boolean|undefined} includeInstance Deprecated. Whether to include 216 | * the JSPB instance for transitional soy proto support: 217 | * http://goto/soy-param-migration 218 | * @param {!proto.example_package.ClientMessage} msg The msg instance to transform. 219 | * @return {!Object} 220 | * @suppress {unusedLocalVariables} f is only used for nested messages 221 | */ 222 | proto.example_package.ClientMessage.toObject = function(includeInstance, msg) { 223 | var f, obj = { 224 | clientMessage: jspb.Message.getFieldWithDefault(msg, 1, "") 225 | }; 226 | 227 | if (includeInstance) { 228 | obj.$jspbMessageInstance = msg; 229 | } 230 | return obj; 231 | }; 232 | } 233 | 234 | 235 | /** 236 | * Deserializes binary data (in protobuf wire format). 237 | * @param {jspb.ByteSource} bytes The bytes to deserialize. 238 | * @return {!proto.example_package.ClientMessage} 239 | */ 240 | proto.example_package.ClientMessage.deserializeBinary = function(bytes) { 241 | var reader = new jspb.BinaryReader(bytes); 242 | var msg = new proto.example_package.ClientMessage; 243 | return proto.example_package.ClientMessage.deserializeBinaryFromReader(msg, reader); 244 | }; 245 | 246 | 247 | /** 248 | * Deserializes binary data (in protobuf wire format) from the 249 | * given reader into the given message object. 250 | * @param {!proto.example_package.ClientMessage} msg The message object to deserialize into. 251 | * @param {!jspb.BinaryReader} reader The BinaryReader to use. 252 | * @return {!proto.example_package.ClientMessage} 253 | */ 254 | proto.example_package.ClientMessage.deserializeBinaryFromReader = function(msg, reader) { 255 | while (reader.nextField()) { 256 | if (reader.isEndGroup()) { 257 | break; 258 | } 259 | var field = reader.getFieldNumber(); 260 | switch (field) { 261 | case 1: 262 | var value = /** @type {string} */ (reader.readString()); 263 | msg.setClientMessage(value); 264 | break; 265 | default: 266 | reader.skipField(); 267 | break; 268 | } 269 | } 270 | return msg; 271 | }; 272 | 273 | 274 | /** 275 | * Serializes the message to binary data (in protobuf wire format). 276 | * @return {!Uint8Array} 277 | */ 278 | proto.example_package.ClientMessage.prototype.serializeBinary = function() { 279 | var writer = new jspb.BinaryWriter(); 280 | proto.example_package.ClientMessage.serializeBinaryToWriter(this, writer); 281 | return writer.getResultBuffer(); 282 | }; 283 | 284 | 285 | /** 286 | * Serializes the given message to binary data (in protobuf wire 287 | * format), writing to the given BinaryWriter. 288 | * @param {!proto.example_package.ClientMessage} message 289 | * @param {!jspb.BinaryWriter} writer 290 | * @suppress {unusedLocalVariables} f is only used for nested messages 291 | */ 292 | proto.example_package.ClientMessage.serializeBinaryToWriter = function(message, writer) { 293 | var f = undefined; 294 | f = message.getClientMessage(); 295 | if (f.length > 0) { 296 | writer.writeString( 297 | 1, 298 | f 299 | ); 300 | } 301 | }; 302 | 303 | 304 | /** 305 | * optional string client_message = 1; 306 | * @return {string} 307 | */ 308 | proto.example_package.ClientMessage.prototype.getClientMessage = function() { 309 | return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 1, "")); 310 | }; 311 | 312 | 313 | /** 314 | * @param {string} value 315 | * @return {!proto.example_package.ClientMessage} returns this 316 | */ 317 | proto.example_package.ClientMessage.prototype.setClientMessage = function(value) { 318 | return jspb.Message.setProto3StringField(this, 1, value); 319 | }; 320 | 321 | 322 | goog.object.extend(exports, proto.example_package); 323 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/server.ts: -------------------------------------------------------------------------------- 1 | import * as grpc from '@grpc/grpc-js'; 2 | import { ExampleService, IExampleServer } from './proto/example_grpc_pb'; 3 | import { ClientMessage, ServerMessage } from './proto/example_pb'; 4 | 5 | const host = '0.0.0.0:9090'; 6 | 7 | const exampleServer: IExampleServer = { 8 | unaryCall( 9 | call: grpc.ServerUnaryCall, 10 | callback: grpc.sendUnaryData 11 | ) { 12 | if (call.request) { 13 | console.log( 14 | `(server) Got client message: ${call.request.getClientMessage()}` 15 | ); 16 | } 17 | const serverMessage = new ServerMessage(); 18 | serverMessage.setServerMessage('Message from server'); 19 | callback(null, serverMessage); 20 | }, 21 | 22 | serverStreamingCall( 23 | call: grpc.ServerWritableStream 24 | ) { 25 | const serverMessage = new ServerMessage(); 26 | serverMessage.setServerMessage('Message from server'); 27 | call.write(serverMessage); 28 | }, 29 | 30 | clientStreamingCall( 31 | call: grpc.ServerReadableStream 32 | ) { 33 | call.on('data', (clientMessage: ClientMessage) => { 34 | console.log( 35 | `(server) Got client message: ${clientMessage.getClientMessage()}` 36 | ); 37 | }); 38 | }, 39 | 40 | bidirectionalStreamingCall( 41 | call: grpc.ServerDuplexStream 42 | ) { 43 | call.on('data', (clientMessage: ClientMessage) => { 44 | console.log( 45 | `(server) Got client message: ${clientMessage.getClientMessage()}` 46 | ); 47 | }); 48 | 49 | const serverMessage = new ServerMessage(); 50 | serverMessage.setServerMessage('Message from server'); 51 | call.write(serverMessage); 52 | }, 53 | }; 54 | 55 | function getServer(): grpc.Server { 56 | const server = new grpc.Server(); 57 | server.addService(ExampleService, exampleServer); 58 | return server; 59 | } 60 | 61 | if (require.main === module) { 62 | const server = getServer(); 63 | server.bindAsync( 64 | host, 65 | grpc.ServerCredentials.createInsecure(), 66 | (err: Error | null, port: number) => { 67 | if (err) { 68 | console.error(`Server error: ${err.message}`); 69 | } else { 70 | console.log(`Server bound on port: ${port}`); 71 | server.start(); 72 | } 73 | } 74 | ); 75 | } 76 | -------------------------------------------------------------------------------- /examples/ts-protoc-gen/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/node12/tsconfig.json", 3 | "exclude": ["node_modules"] 4 | } 5 | --------------------------------------------------------------------------------