proto-loader: include Google well known protos

This commit ensures that the Google well known protos are
available because protobufjs only exposes a subset of them.
This commit is contained in:
cjihrig 2019-03-31 22:16:13 -04:00
parent f345593d1d
commit cbff0b4601
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
3 changed files with 44 additions and 1 deletions

View File

@ -293,3 +293,20 @@ export function loadSync(filename: string, options?: Options): PackageDefinition
loadedRoot.resolveAll(); loadedRoot.resolveAll();
return createPackageDefinition(root, options!); 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);
}
}

View File

@ -67,4 +67,9 @@ describe('Descriptor types', () => {
done(); done();
}, (error) => {done(error);}); }, (error) => {done(error);});
}); });
});
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`);
});
});

View File

@ -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;
}