1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | Object.defineProperty(exports, "__esModule", { value: true });
|
20 | const fs = require("fs");
|
21 | const path = require("path");
|
22 | const Protobuf = require("protobufjs");
|
23 | function 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 | }
|
43 | async 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 | }
|
56 | exports.loadProtosWithOptions = loadProtosWithOptions;
|
57 | function 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 | }
|
70 | exports.loadProtosWithOptionsSync = loadProtosWithOptionsSync;
|
71 |
|
72 |
|
73 |
|
74 | function addCommonProtos() {
|
75 |
|
76 |
|
77 |
|
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 | }
|
87 | exports.addCommonProtos = addCommonProtos;
|
88 |
|
\ | No newline at end of file |