|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 | 17 |
|
| 18 | +import * as protoLoader from '@grpc/proto-loader'; |
18 | 19 | import * as grpc from 'grpc';
|
19 |
| -import { resolve } from 'path'; |
| 20 | +import * as path from 'path'; |
| 21 | +import * as ProtobufJS from 'protobufjs'; |
| 22 | + |
| 23 | +/** Used by tests so we can match @grpc/proto-loader behavior. */ |
| 24 | +export const protoLoaderOptions: ProtobufJS.IConversionOptions = { |
| 25 | + longs: String, |
| 26 | + enums: String, |
| 27 | + defaults: true, |
| 28 | + oneofs: false |
| 29 | +}; |
20 | 30 |
|
21 | 31 | /**
|
22 | 32 | * Loads the protocol buffer definitions for Firestore.
|
23 | 33 | *
|
24 | 34 | * @returns The GrpcObject representing our protos.
|
25 | 35 | */
|
26 | 36 | export function loadProtos(): grpc.GrpcObject {
|
27 |
| - const options = { |
28 |
| - // Beware that converting fields to camel case does not convert the tag |
29 |
| - // fields in oneof groups (!!!). This will likely be fixed when we upgrade |
30 |
| - // to protobufjs 6.x |
31 |
| - convertFieldsToCamelCase: true |
32 |
| - }; |
33 |
| - const root = resolve( |
| 37 | + const root = path.resolve( |
| 38 | + __dirname, |
| 39 | + process.env.FIRESTORE_PROTO_ROOT || '../protos' |
| 40 | + ); |
| 41 | + const firestoreProtoFile = path.join( |
| 42 | + root, |
| 43 | + 'google/firestore/v1/firestore.proto' |
| 44 | + ); |
| 45 | + |
| 46 | + const packageDefinition = protoLoader.loadSync(firestoreProtoFile, { |
| 47 | + ...protoLoaderOptions, |
| 48 | + includeDirs: [root] |
| 49 | + }); |
| 50 | + |
| 51 | + return grpc.loadPackageDefinition(packageDefinition); |
| 52 | +} |
| 53 | + |
| 54 | +/** Used by tests so we can directly create ProtobufJS proto message objects from JSON protos. */ |
| 55 | +export function loadRawProtos(): ProtobufJS.Root { |
| 56 | + const root = path.resolve( |
34 | 57 | __dirname,
|
35 | 58 | process.env.FIRESTORE_PROTO_ROOT || '../protos'
|
36 | 59 | );
|
37 |
| - const firestoreProtoFile = { |
| 60 | + const firestoreProtoFile = path.join( |
38 | 61 | root,
|
39 |
| - file: 'google/firestore/v1/firestore.proto' |
| 62 | + 'google/firestore/v1/firestore.proto' |
| 63 | + ); |
| 64 | + |
| 65 | + const protoRoot = new ProtobufJS.Root(); |
| 66 | + // Override the resolvePath function to look for protos in the 'root' |
| 67 | + // directory. |
| 68 | + protoRoot.resolvePath = (origin: string, target: string) => { |
| 69 | + if (path.isAbsolute(target)) { |
| 70 | + return target; |
| 71 | + } |
| 72 | + return path.join(root, target); |
40 | 73 | };
|
41 |
| - return grpc.load(firestoreProtoFile, /*format=*/ 'proto', options); |
| 74 | + |
| 75 | + protoRoot.loadSync(firestoreProtoFile); |
| 76 | + protoRoot.resolveAll(); |
| 77 | + return protoRoot; |
42 | 78 | }
|
0 commit comments