UNPKG

3.65 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright 2018 gRPC authors.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19Object.defineProperty(exports, "__esModule", { value: true });
20const fs = require("fs");
21const path = require("path");
22const Protobuf = require("protobufjs");
23function addIncludePathResolver(root, includePaths) {
24 const originalResolvePath = root.resolvePath;
25 root.resolvePath = (origin, target) => {
26 if (path.isAbsolute(target)) {
27 return target;
28 }
29 for (const directory of includePaths) {
30 const fullPath = path.join(directory, target);
31 try {
32 fs.accessSync(fullPath, fs.constants.R_OK);
33 return fullPath;
34 }
35 catch (err) {
36 continue;
37 }
38 }
39 process.emitWarning(`${target} not found in any of the include paths ${includePaths}`);
40 return originalResolvePath(origin, target);
41 };
42}
43async function loadProtosWithOptions(filename, options) {
44 const root = new Protobuf.Root();
45 options = options || {};
46 if (!!options.includeDirs) {
47 if (!Array.isArray(options.includeDirs)) {
48 return Promise.reject(new Error('The includeDirs option must be an array'));
49 }
50 addIncludePathResolver(root, options.includeDirs);
51 }
52 const loadedRoot = await root.load(filename, options);
53 loadedRoot.resolveAll();
54 return loadedRoot;
55}
56exports.loadProtosWithOptions = loadProtosWithOptions;
57function loadProtosWithOptionsSync(filename, options) {
58 const root = new Protobuf.Root();
59 options = options || {};
60 if (!!options.includeDirs) {
61 if (!Array.isArray(options.includeDirs)) {
62 throw new Error('The includeDirs option must be an array');
63 }
64 addIncludePathResolver(root, options.includeDirs);
65 }
66 const loadedRoot = root.loadSync(filename, options);
67 loadedRoot.resolveAll();
68 return loadedRoot;
69}
70exports.loadProtosWithOptionsSync = loadProtosWithOptionsSync;
71/**
72 * Load Google's well-known proto files that aren't exposed by Protobuf.js.
73 */
74function addCommonProtos() {
75 // Protobuf.js exposes: any, duration, empty, field_mask, struct, timestamp,
76 // and wrappers. compiler/plugin is excluded in Protobuf.js and here.
77 // Using constant strings for compatibility with tools like Webpack
78 const apiDescriptor = require('protobufjs/google/protobuf/api.json');
79 const descriptorDescriptor = require('protobufjs/google/protobuf/descriptor.json');
80 const sourceContextDescriptor = require('protobufjs/google/protobuf/source_context.json');
81 const typeDescriptor = require('protobufjs/google/protobuf/type.json');
82 Protobuf.common('api', apiDescriptor.nested.google.nested.protobuf.nested);
83 Protobuf.common('descriptor', descriptorDescriptor.nested.google.nested.protobuf.nested);
84 Protobuf.common('source_context', sourceContextDescriptor.nested.google.nested.protobuf.nested);
85 Protobuf.common('type', typeDescriptor.nested.google.nested.protobuf.nested);
86}
87exports.addCommonProtos = addCommonProtos;
88//# sourceMappingURL=util.js.map
\No newline at end of file