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 camelCase = require("lodash.camelcase");
|
21 | const Protobuf = require("protobufjs");
|
22 | const descriptor = require("protobufjs/ext/descriptor");
|
23 | const util_1 = require("./util");
|
24 | function isAnyExtension(obj) {
|
25 | return ('@type' in obj) && (typeof obj['@type'] === 'string');
|
26 | }
|
27 | exports.isAnyExtension = isAnyExtension;
|
28 | const descriptorOptions = {
|
29 | longs: String,
|
30 | enums: String,
|
31 | bytes: String,
|
32 | defaults: true,
|
33 | oneofs: true,
|
34 | json: true,
|
35 | };
|
36 | function joinName(baseName, name) {
|
37 | if (baseName === '') {
|
38 | return name;
|
39 | }
|
40 | else {
|
41 | return baseName + '.' + name;
|
42 | }
|
43 | }
|
44 | function isHandledReflectionObject(obj) {
|
45 | return (obj instanceof Protobuf.Service ||
|
46 | obj instanceof Protobuf.Type ||
|
47 | obj instanceof Protobuf.Enum);
|
48 | }
|
49 | function isNamespaceBase(obj) {
|
50 | return obj instanceof Protobuf.Namespace || obj instanceof Protobuf.Root;
|
51 | }
|
52 | function getAllHandledReflectionObjects(obj, parentName) {
|
53 | const objName = joinName(parentName, obj.name);
|
54 | if (isHandledReflectionObject(obj)) {
|
55 | return [[objName, obj]];
|
56 | }
|
57 | else {
|
58 | if (isNamespaceBase(obj) && typeof obj.nested !== 'undefined') {
|
59 | return Object.keys(obj.nested)
|
60 | .map(name => {
|
61 | return getAllHandledReflectionObjects(obj.nested[name], objName);
|
62 | })
|
63 | .reduce((accumulator, currentValue) => accumulator.concat(currentValue), []);
|
64 | }
|
65 | }
|
66 | return [];
|
67 | }
|
68 | function createDeserializer(cls, options) {
|
69 | return function deserialize(argBuf) {
|
70 | return cls.toObject(cls.decode(argBuf), options);
|
71 | };
|
72 | }
|
73 | function createSerializer(cls) {
|
74 | return function serialize(arg) {
|
75 | if (Array.isArray(arg)) {
|
76 | throw new Error(`Failed to serialize message: expected object with ${cls.name} structure, got array instead`);
|
77 | }
|
78 | const message = cls.fromObject(arg);
|
79 | return cls.encode(message).finish();
|
80 | };
|
81 | }
|
82 | function createMethodDefinition(method, serviceName, options, fileDescriptors) {
|
83 | |
84 |
|
85 | const requestType = method.resolvedRequestType;
|
86 | const responseType = method.resolvedResponseType;
|
87 | return {
|
88 | path: '/' + serviceName + '/' + method.name,
|
89 | requestStream: !!method.requestStream,
|
90 | responseStream: !!method.responseStream,
|
91 | requestSerialize: createSerializer(requestType),
|
92 | requestDeserialize: createDeserializer(requestType, options),
|
93 | responseSerialize: createSerializer(responseType),
|
94 | responseDeserialize: createDeserializer(responseType, options),
|
95 |
|
96 | originalName: camelCase(method.name),
|
97 | requestType: createMessageDefinition(requestType, fileDescriptors),
|
98 | responseType: createMessageDefinition(responseType, fileDescriptors),
|
99 | };
|
100 | }
|
101 | function createServiceDefinition(service, name, options, fileDescriptors) {
|
102 | const def = {};
|
103 | for (const method of service.methodsArray) {
|
104 | def[method.name] = createMethodDefinition(method, name, options, fileDescriptors);
|
105 | }
|
106 | return def;
|
107 | }
|
108 | function createMessageDefinition(message, fileDescriptors) {
|
109 | const messageDescriptor = message.toDescriptor('proto3');
|
110 | return {
|
111 | format: 'Protocol Buffer 3 DescriptorProto',
|
112 | type: messageDescriptor.$type.toObject(messageDescriptor, descriptorOptions),
|
113 | fileDescriptorProtos: fileDescriptors,
|
114 | };
|
115 | }
|
116 | function createEnumDefinition(enumType, fileDescriptors) {
|
117 | const enumDescriptor = enumType.toDescriptor('proto3');
|
118 | return {
|
119 | format: 'Protocol Buffer 3 EnumDescriptorProto',
|
120 | type: enumDescriptor.$type.toObject(enumDescriptor, descriptorOptions),
|
121 | fileDescriptorProtos: fileDescriptors,
|
122 | };
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | function createDefinition(obj, name, options, fileDescriptors) {
|
132 | if (obj instanceof Protobuf.Service) {
|
133 | return createServiceDefinition(obj, name, options, fileDescriptors);
|
134 | }
|
135 | else if (obj instanceof Protobuf.Type) {
|
136 | return createMessageDefinition(obj, fileDescriptors);
|
137 | }
|
138 | else if (obj instanceof Protobuf.Enum) {
|
139 | return createEnumDefinition(obj, fileDescriptors);
|
140 | }
|
141 | else {
|
142 | throw new Error('Type mismatch in reflection object handling');
|
143 | }
|
144 | }
|
145 | function createPackageDefinition(root, options) {
|
146 | const def = {};
|
147 | root.resolveAll();
|
148 | const descriptorList = root.toDescriptor('proto3').file;
|
149 | const bufferList = descriptorList.map(value => Buffer.from(descriptor.FileDescriptorProto.encode(value).finish()));
|
150 | for (const [name, obj] of getAllHandledReflectionObjects(root, '')) {
|
151 | def[name] = createDefinition(obj, name, options, bufferList);
|
152 | }
|
153 | return def;
|
154 | }
|
155 | function createPackageDefinitionFromDescriptorSet(decodedDescriptorSet, options) {
|
156 | options = options || {};
|
157 | const root = Protobuf.Root.fromDescriptor(decodedDescriptorSet);
|
158 | root.resolveAll();
|
159 | return createPackageDefinition(root, options);
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | function load(filename, options) {
|
188 | return util_1.loadProtosWithOptions(filename, options).then(loadedRoot => {
|
189 | return createPackageDefinition(loadedRoot, options);
|
190 | });
|
191 | }
|
192 | exports.load = load;
|
193 | function loadSync(filename, options) {
|
194 | const loadedRoot = util_1.loadProtosWithOptionsSync(filename, options);
|
195 | return createPackageDefinition(loadedRoot, options);
|
196 | }
|
197 | exports.loadSync = loadSync;
|
198 | function fromJSON(json, options) {
|
199 | options = options || {};
|
200 | const loadedRoot = Protobuf.Root.fromJSON(json);
|
201 | loadedRoot.resolveAll();
|
202 | return createPackageDefinition(loadedRoot, options);
|
203 | }
|
204 | exports.fromJSON = fromJSON;
|
205 | function loadFileDescriptorSetFromBuffer(descriptorSet, options) {
|
206 | const decodedDescriptorSet = descriptor.FileDescriptorSet.decode(descriptorSet);
|
207 | return createPackageDefinitionFromDescriptorSet(decodedDescriptorSet, options);
|
208 | }
|
209 | exports.loadFileDescriptorSetFromBuffer = loadFileDescriptorSetFromBuffer;
|
210 | function loadFileDescriptorSetFromObject(descriptorSet, options) {
|
211 | const decodedDescriptorSet = descriptor.FileDescriptorSet.fromObject(descriptorSet);
|
212 | return createPackageDefinitionFromDescriptorSet(decodedDescriptorSet, options);
|
213 | }
|
214 | exports.loadFileDescriptorSetFromObject = loadFileDescriptorSetFromObject;
|
215 | util_1.addCommonProtos();
|
216 |
|
\ | No newline at end of file |