|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +const {dirname, resolve} = require('node:path'); |
| 16 | + |
| 17 | +const PROTO_PATH = __dirname + '/../protos/google/datastore/v1/datastore.proto'; |
| 18 | +const DATASTORE_PROTOS = __dirname + '/../protos'; |
| 19 | +const GAX_PROTOS_DIR = resolve( |
| 20 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 21 | + // @ts-ignore |
| 22 | + // eslint-disable-next-line n/no-extraneous-require |
| 23 | + dirname(require.resolve('google-gax')), |
| 24 | + '../protos' |
| 25 | +); |
| 26 | + |
| 27 | +const grpc = require('@grpc/grpc-js'); |
| 28 | +const protoLoader = require('@grpc/proto-loader'); |
| 29 | + |
| 30 | +const packageDefinition = protoLoader.loadSync(PROTO_PATH, { |
| 31 | + keepCase: true, |
| 32 | + longs: String, |
| 33 | + enums: String, |
| 34 | + defaults: true, |
| 35 | + oneofs: true, |
| 36 | + includeDirs: [DATASTORE_PROTOS, GAX_PROTOS_DIR], |
| 37 | +}); |
| 38 | +const descriptor = grpc.loadPackageDefinition(packageDefinition); |
| 39 | + |
| 40 | +/** |
| 41 | + * Implements the runQuery RPC method. |
| 42 | + */ |
| 43 | +function grpcEndpoint( |
| 44 | + call: {}, |
| 45 | + callback: (arg1: string | null, arg2: {}) => {} |
| 46 | +) { |
| 47 | + // SET A BREAKPOINT HERE AND EXPLORE `call` TO SEE THE REQUEST. |
| 48 | + callback(null, {message: 'Hello'}); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Starts an RPC server that receives requests for datastore |
| 53 | + */ |
| 54 | +export function startServer(cb: () => void) { |
| 55 | + const server = new grpc.Server(); |
| 56 | + const service = descriptor.google.datastore.v1.Datastore.service; |
| 57 | + // On the next line, change runQuery to the grpc method you want to investigate |
| 58 | + server.addService(service, {runQuery: grpcEndpoint}); |
| 59 | + server.bindAsync( |
| 60 | + '0.0.0.0:50051', |
| 61 | + grpc.ServerCredentials.createInsecure(), |
| 62 | + () => { |
| 63 | + console.log('server started'); |
| 64 | + cb(); |
| 65 | + } |
| 66 | + ); |
| 67 | +} |
0 commit comments