From 6be733082bf03b0d0a4bc0d3e16830a219169b13 Mon Sep 17 00:00:00 2001 From: Ivan Hernandez Date: Tue, 22 Apr 2025 20:47:41 +0000 Subject: [PATCH 1/6] chore(deps): replace grpc with @grpc/grpc-js and @grpc/proto-loader --- endpoints/getting-started-grpc/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/endpoints/getting-started-grpc/package.json b/endpoints/getting-started-grpc/package.json index 0d3b6b935a..6f8631cdce 100644 --- a/endpoints/getting-started-grpc/package.json +++ b/endpoints/getting-started-grpc/package.json @@ -17,7 +17,8 @@ "test": "mocha --exit system-test/*.test.js --timeout=60000" }, "dependencies": { - "grpc": "^1.18.0", + "@grpc/grpc-js": "^1.13.3", + "@grpc/proto-loader": "^0.7.15", "yargs": "^17.0.0" }, "devDependencies": { @@ -26,4 +27,3 @@ "wait-port": "^1.0.4" } } - From 36e7a777547d2ab36f0bf33336c31543fd71d544 Mon Sep 17 00:00:00 2001 From: Ivan Hernandez Date: Tue, 22 Apr 2025 20:55:51 +0000 Subject: [PATCH 2/6] refactor(server): migrate grpc server to @grpc/grpc-js and proto-loader --- endpoints/getting-started-grpc/server.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/endpoints/getting-started-grpc/server.js b/endpoints/getting-started-grpc/server.js index 40806a8018..0e7e0d4d8f 100644 --- a/endpoints/getting-started-grpc/server.js +++ b/endpoints/getting-started-grpc/server.js @@ -17,8 +17,17 @@ const path = require('path'); const PROTO_PATH = path.join(__dirname, '/protos/helloworld.proto'); -const grpc = require('grpc'); -const helloProto = grpc.load(PROTO_PATH).helloworld; +const grpc = require('@grpc/grpc-js'); +const protoLoader = require('@grpc/proto-loader'); + +const packageDefinition = protoLoader.loadSync(PROTO_PATH, { + keepCase: true, + longs: String, + enums: String, + defaults: true, + oneofs: true, +}); +const helloProto = grpc.loadPackageDefinition(packageDefinition).helloworld; // Implement the SayHello RPC method. const sayHello = (call, callback) => { @@ -28,9 +37,14 @@ const sayHello = (call, callback) => { // Start an RPC server to handle Greeter service requests const startServer = PORT => { const server = new grpc.Server(); - server.addProtoService(helloProto.Greeter.service, {sayHello: sayHello}); - server.bind(`0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure()); - server.start(); + server.addService(helloProto.Greeter.service, {sayHello: sayHello}); + server.bindAsync( + `0.0.0.0:${PORT}`, + grpc.ServerCredentials.createInsecure(), + () => { + server.start(); + } + ); }; // The command-line program From 6dc3bc6e9df5a301a224ac6788c9b4a3a5e1acba Mon Sep 17 00:00:00 2001 From: Ivan Hernandez Date: Tue, 22 Apr 2025 21:03:38 +0000 Subject: [PATCH 3/6] refactor(client): migrate grpc client to @grpc/grpc-js and proto-loader --- endpoints/getting-started-grpc/client.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/endpoints/getting-started-grpc/client.js b/endpoints/getting-started-grpc/client.js index 92b6c9cc57..aec3e3b351 100644 --- a/endpoints/getting-started-grpc/client.js +++ b/endpoints/getting-started-grpc/client.js @@ -23,12 +23,22 @@ const makeGrpcRequest = (JWT_AUTH_TOKEN, API_KEY, HOST, GREETEE) => { // const GREETEE = 'world'; // Import required libraries - const grpc = require('grpc'); + const grpc = require('@grpc/grpc-js'); + const protoLoader = require('@grpc/proto-loader'); const path = require('path'); // Load protobuf spec for an example API const PROTO_PATH = path.join(__dirname, '/protos/helloworld.proto'); - const protoObj = grpc.load(PROTO_PATH).helloworld; + + const packageDefinition = protoLoader.loadSync(PROTO_PATH, { + keepCase: true, + longs: String, + enums: String, + defaults: true, + oneofs: true, + }); + + const protoObj = grpc.loadPackageDefinition(packageDefinition).helloworld; // Create a client for the protobuf spec const client = new protoObj.Greeter(HOST, grpc.credentials.createInsecure()); From e0dac63a5f7c8f747c288a8a781ca2d404eb23df Mon Sep 17 00:00:00 2001 From: Ivan Hernandez Date: Wed, 23 Apr 2025 14:55:16 +0000 Subject: [PATCH 4/6] chore(server): remove deprecated server.start() call after bindAsync --- endpoints/getting-started-grpc/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/endpoints/getting-started-grpc/server.js b/endpoints/getting-started-grpc/server.js index 0e7e0d4d8f..a8001149b4 100644 --- a/endpoints/getting-started-grpc/server.js +++ b/endpoints/getting-started-grpc/server.js @@ -42,7 +42,7 @@ const startServer = PORT => { `0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure(), () => { - server.start(); + console.log(`gRPC server started on port ${PORT}`); } ); }; From 8243bf1f72a2a3679536da4c729cef48911de86d Mon Sep 17 00:00:00 2001 From: Ivan Hernandez Date: Wed, 23 Apr 2025 16:06:58 +0000 Subject: [PATCH 5/6] docs(readme): improve client.js usage docs with JWT and API examples --- endpoints/getting-started-grpc/README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/endpoints/getting-started-grpc/README.md b/endpoints/getting-started-grpc/README.md index 34571a3ae2..6a6c7ee739 100644 --- a/endpoints/getting-started-grpc/README.md +++ b/endpoints/getting-started-grpc/README.md @@ -13,10 +13,29 @@ $ node server.js -p 50051 ``` ### Run the client +For running the client locally, you'll need either an API key or a JWT auth token: + +#### Using a JWT token (recommended for local development) +You can generate a development-only JWT token using Google Cloud CLI: + +``` +$ gcloud auth print-identity-token +``` +See [print-identity-token](https://cloud.google.com/sdk/gcloud/reference/auth/print-identity-token) for more info. + +Then run the client using this token: ``` -$ node client.js -h localhost:50051 +$ node client.js -h localhost:50051 -j YOUR_JWT_TOKEN ``` +#### Using an API key +Alternatively, you can use an API key from your Google Cloud project: +``` +$ node client.js -h localhost:50051 -k YOUR_API_KEY +``` + +You can create API keys in the [Google Cloud Console](https://console.cloud.google.com/apis/credentials). + ## Running on Google Cloud Platform ### Setup Make sure you have [gcloud][gcloud] and [Node.js][nodejs] installed. From dc0d2ca89f36ecd8484077e075ca0edb18c65116 Mon Sep 17 00:00:00 2001 From: Ivan Hernandez Date: Wed, 23 Apr 2025 16:10:29 +0000 Subject: [PATCH 6/6] docs(readme): update get-id-token README.md reference --- endpoints/getting-started-grpc/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/endpoints/getting-started-grpc/README.md b/endpoints/getting-started-grpc/README.md index 6a6c7ee739..590e50f3df 100644 --- a/endpoints/getting-started-grpc/README.md +++ b/endpoints/getting-started-grpc/README.md @@ -21,7 +21,7 @@ You can generate a development-only JWT token using Google Cloud CLI: ``` $ gcloud auth print-identity-token ``` -See [print-identity-token](https://cloud.google.com/sdk/gcloud/reference/auth/print-identity-token) for more info. +See [get-id-token#generic-dev](https://cloud.google.com/docs/authentication/get-id-token#generic-dev) for more info. Then run the client using this token: ```