UNPKG

3.16 kBJavaScriptView Raw
1"use strict";
2// Copyright 2020 Google LLC
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15Object.defineProperty(exports, "__esModule", { value: true });
16exports.load = exports.loadSync = exports.GoogleProtoFilesRoot = void 0;
17const path = require("path");
18const protobuf = require("protobufjs");
19const walk = require("walkdir");
20let COMMON_PROTO_FILES;
21class GoogleProtoFilesRoot extends protobuf.Root {
22 constructor(options) {
23 super(options);
24 }
25 // Caches and returns an array of the local common/google core proto files
26 // exist on disk.
27 static getCommonProtoFiles() {
28 const commonProtoDirs = [
29 // This list of directories is defined here:
30 // https://github.com/googleapis/googleapis/blob/master/gapic/packaging/common_protos.yaml
31 'api',
32 path.join('logging', 'type'),
33 path.join('cloud', 'speech', 'v1'),
34 'longrunning',
35 'protobuf', // This is an additional path that the common protos depend
36 // on.
37 'rpc',
38 'type',
39 ].map(dir => path.join(__dirname, '..', '..', 'google', dir));
40 if (!COMMON_PROTO_FILES) {
41 COMMON_PROTO_FILES = commonProtoDirs
42 .map(dir => {
43 return walk.sync(dir)
44 .filter(f => path.extname(f) === '.proto')
45 .map(path.normalize);
46 })
47 .reduce((a, c) => a.concat(c), []);
48 }
49 return COMMON_PROTO_FILES;
50 }
51 // Causes the loading of an included proto to check if it is a common
52 // proto. If it is a common proto, use the google-proto-files proto.
53 resolvePath(_, includePath, alreadyNormalized) {
54 includePath = path.normalize(includePath);
55 // Fully qualified paths don't need to be resolved.
56 if (path.isAbsolute(includePath)) {
57 return includePath;
58 }
59 const fullIncludePath = path.join(__dirname, '..', '..', includePath);
60 const commonProtoFiles = GoogleProtoFilesRoot.getCommonProtoFiles();
61 if (commonProtoFiles.indexOf(fullIncludePath) > -1) {
62 return fullIncludePath;
63 }
64 return protobuf.util.path.resolve(_, includePath, alreadyNormalized);
65 }
66}
67exports.GoogleProtoFilesRoot = GoogleProtoFilesRoot;
68function loadSync(filename, options) {
69 const root = new GoogleProtoFilesRoot();
70 return root.loadSync(filename, options);
71}
72exports.loadSync = loadSync;
73function load(filename, options) {
74 const root = new GoogleProtoFilesRoot();
75 return root.load(filename, options);
76}
77exports.load = load;
78//# sourceMappingURL=load.js.map
\No newline at end of file