diff --git a/packages/proto-loader/src/index.ts b/packages/proto-loader/src/index.ts index 2e1a2b84..32e85a78 100644 --- a/packages/proto-loader/src/index.ts +++ b/packages/proto-loader/src/index.ts @@ -293,3 +293,20 @@ export function loadSync(filename: string, options?: Options): PackageDefinition loadedRoot.resolveAll(); return createPackageDefinition(root, options!); } + +// Load Google's well-known proto files that aren't exposed by Protobuf.js. +{ + // Protobuf.js exposes: any, duration, empty, field_mask, struct, timestamp, + // and wrappers. compiler/plugin is excluded in Protobuf.js and here. + const wellKnownProtos = ['api', 'descriptor', 'source_context', 'type']; + const sourceDir = path.join(path.dirname(require.resolve('protobufjs')), + 'google', 'protobuf'); + + for (const proto of wellKnownProtos) { + const file = path.join(sourceDir, `${proto}.proto`); + const descriptor = Protobuf.loadSync(file).toJSON(); + + // @ts-ignore + Protobuf.common(proto, descriptor.nested.google.nested); + } +} diff --git a/packages/proto-loader/test/descriptor_type_test.ts b/packages/proto-loader/test/descriptor_type_test.ts index ae4db014..e550a613 100644 --- a/packages/proto-loader/test/descriptor_type_test.ts +++ b/packages/proto-loader/test/descriptor_type_test.ts @@ -67,4 +67,9 @@ describe('Descriptor types', () => { done(); }, (error) => {done(error);}); }); -}); \ No newline at end of file + + it('Can use well known Google protos', () => { + // This will throw if the well known protos are not available. + proto_loader.loadSync(`${TEST_PROTO_DIR}/well_known.proto`); + }); +}); diff --git a/packages/proto-loader/test_protos/well_known.proto b/packages/proto-loader/test_protos/well_known.proto new file mode 100644 index 00000000..dd70402b --- /dev/null +++ b/packages/proto-loader/test_protos/well_known.proto @@ -0,0 +1,21 @@ +// Copyright 2019 gRPC authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +syntax = "proto3"; + +import "google/protobuf/descriptor.proto"; + +extend google.protobuf.FieldOptions { + bool redact = 52000; +}