|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2023 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +const grpc = require('@grpc/grpc-js'); |
| 20 | +const protoLoader = require('@grpc/proto-loader'); |
| 21 | +const parseArgs = require('minimist'); |
| 22 | +const os = require('os'); |
| 23 | + |
| 24 | +const PROTO_PATH = __dirname + '/../protos/helloworld.proto'; |
| 25 | + |
| 26 | +const packageDefinition = protoLoader.loadSync( |
| 27 | + PROTO_PATH, |
| 28 | + {keepCase: true, |
| 29 | + longs: String, |
| 30 | + enums: String, |
| 31 | + defaults: true, |
| 32 | + oneofs: true |
| 33 | + }); |
| 34 | +const helloProto = grpc.loadPackageDefinition(packageDefinition).helloworld; |
| 35 | + |
| 36 | +function unaryCall(client, requestId, name, expectedCode) { |
| 37 | + console.log(`[${requestId}] Calling SayHello with name:"${name}"`); |
| 38 | + return new Promise((resolve, reject) => { |
| 39 | + client.sayHello({name: name}, (error, value) => { |
| 40 | + if (error) { |
| 41 | + if (error.code === expectedCode) { |
| 42 | + console.log(`[${requestId}] Received error ${error.message}`); |
| 43 | + } else { |
| 44 | + console.log(`[${requestId}] Received unexpected error ${error.message}`); |
| 45 | + } |
| 46 | + } |
| 47 | + if (value) { |
| 48 | + console.log(`[${requestId}] Received response ${value.message}`); |
| 49 | + } |
| 50 | + resolve(); |
| 51 | + }); |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +function streamingCall(client, requestId, name, expectedCode) { |
| 56 | + console.log(`[${requestId}] Calling SayHelloStreamReply with name:"${name}"`); |
| 57 | + return new Promise((resolve, reject) => { |
| 58 | + const call = client.sayHelloStreamReply({name: name}); |
| 59 | + call.on('data', value => { |
| 60 | + console.log(`[${requestId}] Received response ${value.message}`); |
| 61 | + }); |
| 62 | + call.on('status', status => { |
| 63 | + console.log(`[${requestId}] Received status with code=${grpc.status[status.code]} details=${status.details}`); |
| 64 | + resolve(); |
| 65 | + }); |
| 66 | + call.on('error', error => { |
| 67 | + if (error.code === expectedCode) { |
| 68 | + console.log(`[${requestId}] Received expected error ${error.message}`); |
| 69 | + } else { |
| 70 | + console.log(`[${requestId}] Received unexpected error ${error.message}`); |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +async function main() { |
| 77 | + let argv = parseArgs(process.argv.slice(2), { |
| 78 | + string: 'target', |
| 79 | + default: {target: 'localhost:50052'} |
| 80 | + }); |
| 81 | + const client = new helloProto.Greeter(argv.target, grpc.credentials.createInsecure()); |
| 82 | + const name = os.userInfo().username ?? 'unknown'; |
| 83 | + await unaryCall(client, 1, '', grpc.status.INVALID_ARGUMENT); |
| 84 | + await unaryCall(client, 2, name, grpc.status.OK); |
| 85 | + await streamingCall(client, 3, '', grpc.status.INVALID_ARGUMENT); |
| 86 | + await streamingCall(client, 4, name, grpc.status.OK); |
| 87 | +} |
| 88 | + |
| 89 | +main(); |
0 commit comments